]> icculus.org git repositories - btb/d2x.git/blob - main/segment.h
macro for Segment pointer indexing
[btb/d2x.git] / main / segment.h
1 /* $Id: segment.h,v 1.7 2005-06-22 09:08:21 chris Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Include file for functions which need to access segment data structure.
18  *
19  */
20
21 #ifndef _SEGMENT_H
22 #define _SEGMENT_H
23
24 #include "pstypes.h"
25 #include "fix.h"
26 #include "vecmat.h"
27 //#include "3d.h"
28 //#include "inferno.h"
29 #include "cfile.h"
30
31 // Version 1 - Initial version
32 // Version 2 - Mike changed some shorts to bytes in segments, so incompatible!
33
34 #define SIDE_IS_QUAD    1   // render side as quadrilateral
35 #define SIDE_IS_TRI_02  2   // render side as two triangles, triangulated along edge from 0 to 2
36 #define SIDE_IS_TRI_13  3   // render side as two triangles, triangulated along edge from 1 to 3
37
38 // Set maximum values for segment and face data structures.
39 #define MAX_VERTICES_PER_SEGMENT    8
40 #define MAX_SIDES_PER_SEGMENT       6
41 #define MAX_VERTICES_PER_POLY       4
42 #define WLEFT                       0
43 #define WTOP                        1
44 #define WRIGHT                      2
45 #define WBOTTOM                     3
46 #define WBACK                       4
47 #define WFRONT                      5
48
49 #if defined(SHAREWARE)
50 # define MAX_SEGMENTS           800
51 # define MAX_SEGMENT_VERTICES   2800
52 #else
53 # define MAX_SEGMENTS           900
54 # define MAX_SEGMENT_VERTICES   3600
55 #endif
56
57 //normal everyday vertices
58
59 #define DEFAULT_LIGHTING        0   // (F1_0/2)
60
61 #ifdef EDITOR   //verts for the new segment
62 # define NUM_NEW_SEG_VERTICES   8
63 # define NEW_SEGMENT_VERTICES   (MAX_SEGMENT_VERTICES)
64 # define MAX_VERTICES           (MAX_SEGMENT_VERTICES+NUM_NEW_SEG_VERTICES)
65 #else           //No editor
66 # define MAX_VERTICES           (MAX_SEGMENT_VERTICES)
67 #endif
68
69 // Returns true if segnum references a child, else returns false.
70 // Note that -1 means no connection, -2 means a connection to the outside world.
71 #define IS_CHILD(segnum) (segnum > -1)
72
73 //Structure for storing u,v,light values.
74 //NOTE: this structure should be the same as the one in 3d.h
75 typedef struct uvl {
76         fix u, v, l;
77 } uvl;
78
79 #ifdef COMPACT_SEGS
80 typedef struct side {
81         sbyte   type;           // replaces num_faces and tri_edge, 1 = quad, 2 = 0:2 triangulation, 3 = 1:3 triangulation
82         ubyte   pad;            //keep us longword alligned
83         short   wall_num;
84         short   tmap_num;
85         short   tmap_num2;
86         uvl     uvls[4];
87         //vms_vector normals[2];  // 2 normals, if quadrilateral, both the same.
88 } side;
89 #else
90 typedef struct side {
91         sbyte   type;           // replaces num_faces and tri_edge, 1 = quad, 2 = 0:2 triangulation, 3 = 1:3 triangulation
92         ubyte   pad;            //keep us longword alligned
93         short   wall_num;
94         short   tmap_num;
95         short   tmap_num2;
96         uvl     uvls[4];
97         vms_vector normals[2];  // 2 normals, if quadrilateral, both the same.
98 } side;
99 #endif
100
101 typedef struct segment {
102 #ifdef EDITOR
103         short   segnum;     // segment number, not sure what it means
104 #endif
105         side    sides[MAX_SIDES_PER_SEGMENT];       // 6 sides
106         short   children[MAX_SIDES_PER_SEGMENT];    // indices of 6 children segments, front, left, top, right, bottom, back
107         short   verts[MAX_VERTICES_PER_SEGMENT];    // vertex ids of 4 front and 4 back vertices
108 #ifdef EDITOR
109         short   group;      // group number to which the segment belongs.
110         short   objects;    // pointer to objects in this segment
111 #else
112         int     objects;    // pointer to objects in this segment
113 #endif
114
115         // -- Moved to segment2 to make this struct 512 bytes long --
116         //ubyte   special;    // what type of center this is
117         //sbyte   matcen_num; // which center segment is associated with.
118         //short   value;
119         //fix     static_light; //average static light in segment
120         //#ifndef EDITOR
121         //short   pad;        //make structure longword aligned
122         //#endif
123 } segment;
124
125 #define S2F_AMBIENT_WATER   0x01
126 #define S2F_AMBIENT_LAVA    0x02
127
128 typedef struct segment2 {
129         ubyte   special;
130         sbyte   matcen_num;
131         sbyte   value;
132         ubyte   s2_flags;
133         fix     static_light;
134 } segment2;
135
136 //values for special field
137 #define SEGMENT_IS_NOTHING      0
138 #define SEGMENT_IS_FUELCEN      1
139 #define SEGMENT_IS_REPAIRCEN    2
140 #define SEGMENT_IS_CONTROLCEN   3
141 #define SEGMENT_IS_ROBOTMAKER   4
142 #define SEGMENT_IS_GOAL_BLUE    5
143 #define SEGMENT_IS_GOAL_RED     6
144 #define MAX_CENTER_TYPES        7
145
146 #ifdef COMPACT_SEGS
147 extern void get_side_normal(segment *sp, int sidenum, int normal_num, vms_vector * vm );
148 extern void get_side_normals(segment *sp, int sidenum, vms_vector * vm1, vms_vector *vm2 );
149 #endif
150
151 // Local segment data.
152 // This is stuff specific to a segment that does not need to get
153 // written to disk.  This is a handy separation because we can add to
154 // this structure without obsoleting existing data on disk.
155
156 #define SS_REPAIR_CENTER    0x01    // Bitmask for this segment being part of repair center.
157
158 //--repair-- typedef struct {
159 //--repair--    int     special_type;
160 //--repair--    short   special_segment; // if special_type indicates repair center, this is the base of the repair center
161 //--repair-- } lsegment;
162
163 typedef struct {
164         int     num_segments;
165         int     num_vertices;
166         short   segments[MAX_SEGMENTS];
167         short   vertices[MAX_VERTICES];
168 } group;
169
170 // Globals from mglobal.c
171 extern vms_vector   Vertices[];
172 extern segment      Segments[];
173 extern segment2     Segment2s[];
174 extern int          Num_segments;
175 extern int          Num_vertices;
176 #define SEGMENT_NUMBER(seg) ((int)((seg) - Segments))
177
178 // Get pointer to the segment2 for the given segment pointer
179 #define s2s2(segp) (&Segment2s[SEGMENT_NUMBER(segp)])
180
181 extern sbyte Side_to_verts[MAX_SIDES_PER_SEGMENT][4];       // Side_to_verts[my_side] is list of vertices forming side my_side.
182 extern int  Side_to_verts_int[MAX_SIDES_PER_SEGMENT][4];    // Side_to_verts[my_side] is list of vertices forming side my_side.
183 extern char Side_opposite[];                                // Side_opposite[my_side] returns side opposite cube from my_side.
184
185 #define SEG_PTR_2_NUM(segptr) (Assert( (unsigned)SEGMENT_NUMBER(segptr) < MAX_SEGMENTS ), SEGMENT_NUMBER(segptr))
186
187 // New stuff, 10/14/95: For shooting out lights and monitors.
188 // Light cast upon vert_light vertices in segnum:sidenum by some light
189 typedef struct {
190         short   segnum;
191         sbyte   sidenum;
192         sbyte   dummy;
193         ubyte   vert_light[4];
194 } delta_light;
195
196 // Light at segnum:sidenum casts light on count sides beginning at index (in array Delta_lights)
197 typedef struct {
198         short   segnum;
199         sbyte   sidenum;
200         sbyte   count;
201         short   index;
202 } dl_index;
203
204 #define MAX_DL_INDICES      500
205 #define MAX_DELTA_LIGHTS    10000
206
207 #define DL_SCALE            2048    // Divide light to allow 3 bits integer, 5 bits fraction.
208
209 extern dl_index     Dl_indices[MAX_DL_INDICES];
210 extern delta_light  Delta_lights[MAX_DELTA_LIGHTS];
211 extern int          Num_static_lights;
212
213 extern int subtract_light(int segnum, int sidenum);
214 extern int add_light(int segnum, int sidenum);
215 extern void restore_all_lights_in_mine(void);
216 extern void clear_light_subtracted(void);
217
218 extern ubyte Light_subtracted[MAX_SEGMENTS];
219
220 // ----------------------------------------------------------------------------
221 // --------------------- Segment interrogation functions ----------------------
222 // Do NOT read the segment data structure directly.  Use these
223 // functions instead.  The segment data structure is GUARANTEED to
224 // change MANY TIMES.  If you read the segment data structure
225 // directly, your code will break, I PROMISE IT!
226
227 // Return a pointer to the list of vertex indices for the current
228 // segment in vp and the number of vertices in *nv.
229 extern void med_get_vertex_list(segment *s,int *nv,short **vp);
230
231 // Return a pointer to the list of vertex indices for face facenum in
232 // vp and the number of vertices in *nv.
233 extern void med_get_face_vertex_list(segment *s,int side, int facenum,int *nv,short **vp);
234
235 // Set *nf = number of faces in segment s.
236 extern void med_get_num_faces(segment *s,int *nf);
237
238 void med_validate_segment_side(segment *sp,int side);
239
240 // Delete segment function added for curves.c
241 extern int med_delete_segment(segment *sp);
242
243 // Delete segment from group
244 extern void delete_segment_from_group(int segment_num, int group_num);
245
246 // Add segment to group
247 extern void add_segment_to_group(int segment_num, int group_num);
248
249 // Verify that all vertices are legal.
250 extern void med_check_all_vertices();
251
252 #ifdef FAST_FILE_IO
253 #define segment2_read(s2, fp) cfread(s2, sizeof(segment2), 1, fp)
254 #define delta_light_read(dl, fp) cfread(dl, sizeof(delta_light), 1, fp)
255 #define dl_index_read(di, fp) cfread(di, sizeof(dl_index), 1, fp)
256 #else
257 /*
258  * reads a segment2 structure from a CFILE
259  */
260 void segment2_read(segment2 *s2, CFILE *fp);
261
262 /*
263  * reads a delta_light structure from a CFILE
264  */
265 void delta_light_read(delta_light *dl, CFILE *fp);
266
267 /*
268  * reads a dl_index structure from a CFILE
269  */
270 void dl_index_read(dl_index *di, CFILE *fp);
271 #endif
272
273 void segment2_write(segment2 *s2, PHYSFS_file *fp);
274 void delta_light_write(delta_light *dl, PHYSFS_file *fp);
275 void dl_index_write(dl_index *di, PHYSFS_file *fp);
276
277 #endif