1 //------------------------------------------------------------------------
2 // LEVEL : Level structures & read/write functions.
3 //------------------------------------------------------------------------
4 //
5 //  GL-Friendly Node Builder (C) 2000-2007 Andrew Apted
6 //
7 //  Based on 'BSP 2.3' by Colin Reed, Lee Killough and others.
8 //
9 //  This program is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU General Public License
11 //  as published by the Free Software Foundation; either version 2
12 //  of the License, or (at your option) any later version.
13 //
14 //  This program is distributed in the hope that it will be useful,
15 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 //  GNU General Public License for more details.
18 //
19 //------------------------------------------------------------------------
20 
21 #ifndef __GLBSP_LEVEL_H__
22 #define __GLBSP_LEVEL_H__
23 
24 #include "structs.h"
25 #include "wad.h"
26 
27 
28 struct node_s;
29 struct sector_s;
30 struct superblock_s;
31 
32 
33 // a wall_tip is where a wall meets a vertex
34 typedef struct wall_tip_s
35 {
36   // link in list.  List is kept in ANTI-clockwise order.
37   struct wall_tip_s *next;
38   struct wall_tip_s *prev;
39 
40   // angle that line makes at vertex (degrees).
41   angle_g angle;
42 
43   // sectors on each side of wall.  Left is the side of increasing
44   // angles, right is the side of decreasing angles.  Either can be
45   // NULL for one sided walls.
46   struct sector_s *left;
47   struct sector_s *right;
48 }
49 wall_tip_t;
50 
51 
52 typedef struct vertex_s
53 {
54   // coordinates
55   float_g x, y;
56 
57   // vertex index.  Always valid after loading and pruning of unused
58   // vertices has occurred.  For GL vertices, bit 30 will be set.
59   int index;
60 
61   // reference count.  When building normal node info, unused vertices
62   // will be pruned.
63   int ref_count;
64 
65   // usually NULL, unless this vertex occupies the same location as a
66   // previous vertex.  Only used during the pruning phase.
67   struct vertex_s *equiv;
68 
69   // set of wall_tips
70   wall_tip_t *tip_set;
71 
72   // contains a duplicate vertex, needed when both normal and V2 GL
73   // nodes are being built at the same time (this is the vertex used
74   // for the normal segs).  Normally NULL.  Note: the wall tips on
75   // this vertex are not created.
76   struct vertex_s *normal_dup;
77 }
78 vertex_t;
79 
80 #define IS_GL_VERTEX  (1 << 30)
81 
82 
83 typedef struct sector_s
84 {
85   // sector index.  Always valid after loading & pruning.
86   int index;
87 
88   // allow segs from other sectors to coexist in a subsector.
89   char coalesce;
90 
91   // -JL- non-zero if this sector contains a polyobj.
92   int has_polyobj;
93 
94   // reference count.  When building normal nodes, unused sectors will
95   // be pruned.
96   int ref_count;
97 
98   // heights
99   int floor_h, ceil_h;
100 
101   // textures
102   char floor_tex[8];
103   char ceil_tex[8];
104 
105   // attributes
106   int light;
107   int special;
108   int tag;
109 
110   // used when building REJECT table.  Each set of sectors that are
111   // isolated from other sectors will have a different group number.
112   // Thus: on every 2-sided linedef, the sectors on both sides will be
113   // in the same group.  The rej_next, rej_prev fields are a link in a
114   // RING, containing all sectors of the same group.
115   int rej_group;
116 
117   struct sector_s *rej_next;
118   struct sector_s *rej_prev;
119 
120   // suppress superfluous mini warnings
121   int warned_facing;
122   char warned_unclosed;
123 }
124 sector_t;
125 
126 
127 typedef struct sidedef_s
128 {
129   // adjacent sector.  Can be NULL (invalid sidedef)
130   sector_t *sector;
131 
132   // offset values
133   int x_offset, y_offset;
134 
135   // texture names
136   char upper_tex[8];
137   char lower_tex[8];
138   char mid_tex[8];
139 
140   // sidedef index.  Always valid after loading & pruning.
141   int index;
142 
143   // reference count.  When building normal nodes, unused sidedefs will
144   // be pruned.
145   int ref_count;
146 
147   // usually NULL, unless this sidedef is exactly the same as a
148   // previous one.  Only used during the pruning phase.
149   struct sidedef_s *equiv;
150 
151   // this is true if the sidedef is on a special line.  We don't merge
152   // these sidedefs together, as they might scroll, or change texture
153   // when a switch is pressed.
154   int on_special;
155 }
156 sidedef_t;
157 
158 
159 typedef struct linedef_s
160 {
161   // link for list
162   struct linedef_s *next;
163 
164   vertex_t *start;    // from this vertex...
165   vertex_t *end;      // ... to this vertex
166 
167   sidedef_t *right;   // right sidedef
168   sidedef_t *left;    // left sidede, or NULL if none
169 
170   // line is marked two-sided
171   char two_sided;
172 
173   // prefer not to split
174   char is_precious;
175 
176   // zero length (line should be totally ignored)
177   char zero_len;
178 
179   // sector is the same on both sides
180   char self_ref;
181 
182   // one-sided linedef used for a special effect (windows).
183   // The value refers to the opposite sector on the back side.
184   sector_t * window_effect;
185 
186   int flags;
187   int type;
188   int tag;
189 
190   // Hexen support
191   int specials[5];
192 
193   // normally NULL, except when this linedef directly overlaps an earlier
194   // one (a rarely-used trick to create higher mid-masked textures).
195   // No segs should be created for these overlapping linedefs.
196   struct linedef_s *overlap;
197 
198   // linedef index.  Always valid after loading & pruning of zero
199   // length lines has occurred.
200   int index;
201 }
202 linedef_t;
203 
204 
205 typedef struct thing_s
206 {
207   int x, y;
208   int type;
209   int options;
210 
211   // other info (angle, and hexen stuff) omitted.  We don't need to
212   // write the THING lump, only read it.
213 
214   // Always valid (thing indices never change).
215   int index;
216 }
217 thing_t;
218 
219 
220 typedef struct seg_s
221 {
222   // link for list
223   struct seg_s *next;
224 
225   vertex_t *start;   // from this vertex...
226   vertex_t *end;     // ... to this vertex
227 
228   // linedef that this seg goes along, or NULL if miniseg
229   linedef_t *linedef;
230 
231   // adjacent sector, or NULL if invalid sidedef or miniseg
232   sector_t *sector;
233 
234   // 0 for right, 1 for left
235   int side;
236 
237   // seg on other side, or NULL if one-sided.  This relationship is
238   // always one-to-one -- if one of the segs is split, the partner seg
239   // must also be split.
240   struct seg_s *partner;
241 
242   // seg index.  Only valid once the seg has been added to a
243   // subsector.  A negative value means it is invalid -- there
244   // shouldn't be any of these once the BSP tree has been built.
245   int index;
246 
247   // when 1, this seg has become zero length (integer rounding of the
248   // start and end vertices produces the same location).  It should be
249   // ignored when writing the SEGS or V1 GL_SEGS lumps.  [Note: there
250   // won't be any of these when writing the V2 GL_SEGS lump].
251   int degenerate;
252 
253   // the superblock that contains this seg, or NULL if the seg is no
254   // longer in any superblock (e.g. now in a subsector).
255   struct superblock_s *block;
256 
257   // precomputed data for faster calculations
258   float_g psx, psy;
259   float_g pex, pey;
260   float_g pdx, pdy;
261 
262   float_g p_length;
263   float_g p_angle;
264   float_g p_para;
265   float_g p_perp;
266 
267   // linedef that this seg initially comes from.  For "real" segs,
268   // this is just the same as the 'linedef' field above.  For
269   // "minisegs", this is the linedef of the partition line.
270   linedef_t *source_line;
271 }
272 seg_t;
273 
274 
275 typedef struct subsec_s
276 {
277   // list of segs
278   seg_t *seg_list;
279 
280   // count of segs
281   int seg_count;
282 
283   // subsector index.  Always valid, set when the subsector is
284   // initially created.
285   int index;
286 
287   // approximate middle point
288   float_g mid_x;
289   float_g mid_y;
290 }
291 subsec_t;
292 
293 
294 typedef struct bbox_s
295 {
296   int minx, miny;
297   int maxx, maxy;
298 }
299 bbox_t;
300 
301 
302 typedef struct child_s
303 {
304   // child node or subsector (one must be NULL)
305   struct node_s *node;
306   subsec_t *subsec;
307 
308   // child bounding box
309   bbox_t bounds;
310 }
311 child_t;
312 
313 
314 typedef struct node_s
315 {
316   int x, y;     // starting point
317   int dx, dy;   // offset to ending point
318 
319   // right & left children
320   child_t r;
321   child_t l;
322 
323   // node index.  Only valid once the NODES or GL_NODES lump has been
324   // created.
325   int index;
326 
327   // the node is too long, and the (dx,dy) values should be halved
328   // when writing into the NODES lump.
329   int too_long;
330 }
331 node_t;
332 
333 
334 typedef struct superblock_s
335 {
336   // parent of this block, or NULL for a top-level block
337   struct superblock_s *parent;
338 
339   // coordinates on map for this block, from lower-left corner to
340   // upper-right corner.  Pseudo-inclusive, i.e (x,y) is inside block
341   // if and only if x1 <= x < x2 and y1 <= y < y2.
342   int x1, y1;
343   int x2, y2;
344 
345   // sub-blocks.  NULL when empty.  [0] has the lower coordinates, and
346   // [1] has the higher coordinates.  Division of a square always
347   // occurs horizontally (e.g. 512x512 -> 256x512 -> 256x256).
348   struct superblock_s *subs[2];
349 
350   // number of real segs and minisegs contained by this block
351   // (including all sub-blocks below it).
352   int real_num;
353   int mini_num;
354 
355   // list of segs completely contained by this block.
356   seg_t *segs;
357 }
358 superblock_t;
359 
360 #define SUPER_IS_LEAF(s)  \
361     ((s)->x2-(s)->x1 <= 256 && (s)->y2-(s)->y1 <= 256)
362 
363 
364 /* ----- Level data arrays ----------------------- */
365 
366 extern int num_vertices;
367 extern int num_linedefs;
368 extern int num_sidedefs;
369 extern int num_sectors;
370 extern int num_things;
371 extern int num_segs;
372 extern int num_subsecs;
373 extern int num_nodes;
374 extern int num_stale_nodes;
375 
376 extern int num_normal_vert;
377 extern int num_gl_vert;
378 extern int num_complete_seg;
379 
380 
381 /* ----- function prototypes ----------------------- */
382 
383 // allocation routines
384 vertex_t *NewVertex(void);
385 linedef_t *NewLinedef(void);
386 sidedef_t *NewSidedef(void);
387 sector_t *NewSector(void);
388 thing_t *NewThing(void);
389 seg_t *NewSeg(void);
390 subsec_t *NewSubsec(void);
391 node_t *NewNode(void);
392 node_t *NewStaleNode(void);
393 wall_tip_t *NewWallTip(void);
394 
395 // lookup routines
396 vertex_t *LookupVertex(int index);
397 linedef_t *LookupLinedef(int index);
398 sidedef_t *LookupSidedef(int index);
399 sector_t *LookupSector(int index);
400 thing_t *LookupThing(int index);
401 seg_t *LookupSeg(int index);
402 subsec_t *LookupSubsec(int index);
403 node_t *LookupNode(int index);
404 node_t *LookupStaleNode(int index);
405 
406 // check whether the current level already has normal nodes
407 int CheckForNormalNodes(void);
408 
409 // load all level data for the current level
410 void LoadLevel(void);
411 
412 // free all level data
413 void FreeLevel(void);
414 
415 // save the newly computed NODE info etc..
416 void SaveLevel(node_t *root_node);
417 
418 #endif /* __GLBSP_LEVEL_H__ */
419