File: level.cc

package info (click to toggle)
glbsp 2.24-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,220 kB
  • sloc: cpp: 10,762; ansic: 6,953; makefile: 121; sh: 14
file content (906 lines) | stat: -rw-r--r-- 17,952 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
//------------------------------------------------------------------------
//  LEVEL : Level structure read/write functions.
//------------------------------------------------------------------------
//
//  GL-Node Viewer (C) 2004-2007 Andrew Apted
//
//  This program is free software; you can redistribute it and/or
//  modify it under the terms of the GNU General Public License
//  as published by the Free Software Foundation; either version 2
//  of the License, or (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//------------------------------------------------------------------------

// this includes everything we need
#include "defs.h"

#define DEBUG_LOAD      0
#define DEBUG_BSP       0


// per-level variables

bool lev_doing_hexen;


#define LEVELARRAY(TYPE, BASEVAR, NAMESTR)  \
    container_tp<TYPE> BASEVAR(NAMESTR);

LEVELARRAY(vertex_c,  lev_vertices, "vertex")
LEVELARRAY(vertex_c,  lev_gl_verts, "gl_vert")
LEVELARRAY(linedef_c, lev_linedefs, "linedef")
LEVELARRAY(sidedef_c, lev_sidedefs, "sidedef")
LEVELARRAY(sector_c,  lev_sectors,  "sector")
LEVELARRAY(thing_c,   lev_things,   "thing")
LEVELARRAY(seg_c,     lev_segs,     "seg")
LEVELARRAY(subsec_c,  lev_subsecs,  "subsector")
LEVELARRAY(node_c,    lev_nodes,    "node")


/* ----- reading routines ------------------------------ */

static const uint8_g *lev_v2_magic = (uint8_g *)"gNd2";
static const uint8_g *lev_v3_magic = (uint8_g *)"gNd3";

// forward decls
void GetLinedefsHexen(wad_c *base);
void GetThingsHexen(wad_c *base);

vertex_c::vertex_c(int _idx, const raw_vertex_t *raw)
{
  index = _idx;

  x = (double) SINT16(raw->x);
  y = (double) SINT16(raw->y);
}

vertex_c::vertex_c(int _idx, const raw_v2_vertex_t *raw)
{
  index = _idx;

  x = (double) SINT32(raw->x) / 65536.0;
  y = (double) SINT32(raw->y) / 65536.0;
}

vertex_c::~vertex_c()
{
}

//
// GetVertices
//
void GetVertices(wad_c *base)
{
  lump_c *lump = base->FindLumpInLevel("VERTEXES");
  int count = -1;

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_vertex_t);
  }

# if DEBUG_LOAD
  PrintDebug("GetVertices: num = %d\n", count);
# endif

  if (!lump || count == 0)
    FatalError("Couldn't find any Vertices");

  lev_vertices.Allocate(count);

  raw_vertex_t *raw = (raw_vertex_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_vertices.Set(i, new vertex_c(i, raw));
  }
}

//
// GetV2Verts
//
void GetV2Verts(const uint8_g *data, int length)
{
  int count = length / sizeof(raw_v2_vertex_t);

  lev_gl_verts.Allocate(count);

  raw_v2_vertex_t *raw = (raw_v2_vertex_t *) data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_gl_verts.Set(i, new vertex_c(i, raw));
  }
}

//
// GetGLVerts
//
void GetGLVerts(wad_c *base)
{
  lump_c *lump = base->FindLumpInLevel("GL_VERT");

# if DEBUG_LOAD
  PrintDebug("GetVertices: num = %d\n", count);
# endif

  if (!lump)
    FatalError("Couldn't find any GL Vertices");

  base->CacheLump(lump);

  if (lump->length >= 4 && memcmp(lump->data, lev_v2_magic, 4) == 0)
  {
    GetV2Verts((uint8_g *)lump->data + 4, lump->length - 4);
    return;
  }

  int count = lump->length / sizeof(raw_vertex_t);

  lev_gl_verts.Allocate(count);

  raw_vertex_t *raw = (raw_vertex_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_gl_verts.Set(i, new vertex_c(i, raw));
  }
}

sector_c::sector_c(int _idx, const raw_sector_t *raw)
{
  index = _idx;

  floor_h = SINT16(raw->floor_h);
  ceil_h  = SINT16(raw->ceil_h);

  memcpy(floor_tex, raw->floor_tex, sizeof(floor_tex));
  memcpy(ceil_tex,  raw->ceil_tex,  sizeof(ceil_tex));

  light   = UINT16(raw->light);
  special = UINT16(raw->special);
  tag     = SINT16(raw->tag);
}

sector_c::~sector_c()
{
}

//
// GetSectors
//
void GetSectors(wad_c *base)
{
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("SECTORS");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_sector_t);
  }

  if (!lump || count == 0)
    FatalError("Couldn't find any Sectors");

# if DEBUG_LOAD
  PrintDebug("GetSectors: num = %d\n", count);
# endif

  lev_sectors.Allocate(count);

  raw_sector_t *raw = (raw_sector_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_sectors.Set(i, new sector_c(i, raw));
  }
}

thing_c::thing_c(int _idx, const raw_thing_t *raw)
{
  index = _idx;

  x = SINT16(raw->x);
  y = SINT16(raw->y);

  type    = UINT16(raw->type);
  options = UINT16(raw->options);
}

thing_c::thing_c(int _idx, const raw_hexen_thing_t *raw)
{
  index = _idx;

  x = SINT16(raw->x);
  y = SINT16(raw->y);

  type    = UINT16(raw->type);
  options = UINT16(raw->options);
}

thing_c::~thing_c()
{
}

//
// GetThings
//
void GetThings(wad_c *base)
{
  if (lev_doing_hexen)
  {
    GetThingsHexen(base);
    return;
  }
  
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("THINGS");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_thing_t);
  }

  if (!lump || count == 0)
  {
    // Note: no error if no things exist, even though technically a map
    // will be unplayable without the player starts.
    PrintWarn("Couldn't find any Things");
    return;
  }

# if DEBUG_LOAD
  PrintDebug("GetThings: num = %d\n", count);
# endif

  lev_things.Allocate(count);

  raw_thing_t *raw = (raw_thing_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_things.Set(i, new thing_c(i, raw));
  }
}

//
// GetThingsHexen
//
void GetThingsHexen(wad_c *base)
{
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("THINGS");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_hexen_thing_t);
  }

  if (!lump || count == 0)
  {
    // Note: no error if no things exist, even though technically a map
    // will be unplayable without the player starts.
    PrintWarn("Couldn't find any Things");
    return;
  }

# if DEBUG_LOAD
  PrintDebug("GetThingsHexen: num = %d\n", count);
# endif

  lev_things.Allocate(count);

  raw_hexen_thing_t *raw = (raw_hexen_thing_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_things.Set(i, new thing_c(i, raw));
  }
}

sidedef_c::sidedef_c(int _idx, const raw_sidedef_t *raw)
{
  index = _idx;

  sector = (SINT16(raw->sector) == -1) ? NULL :
    lev_sectors.Get(UINT16(raw->sector));

  x_offset = SINT16(raw->x_offset);
  y_offset = SINT16(raw->y_offset);

  memcpy(upper_tex, raw->upper_tex, sizeof(upper_tex));
  memcpy(lower_tex, raw->lower_tex, sizeof(lower_tex));
  memcpy(mid_tex,   raw->mid_tex,   sizeof(mid_tex));
}

sidedef_c::~sidedef_c()
{
}

//
// GetSidedefs
//
void GetSidedefs(wad_c *base)
{
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("SIDEDEFS");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_sidedef_t);
  }

  if (!lump || count == 0)
    FatalError("Couldn't find any Sidedefs");

# if DEBUG_LOAD
  PrintDebug("GetSidedefs: num = %d\n", count);
# endif

  lev_sidedefs.Allocate(count);

  raw_sidedef_t *raw = (raw_sidedef_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_sidedefs.Set(i, new sidedef_c(i, raw));
  }
}

static sidedef_c *SafeLookupSidedef(uint16_g num)
{
  if (num == 0xFFFF)
    return NULL;

  if ((int)num >= lev_sidedefs.num && (sint16_g)(num) < 0)
    return NULL;

  return lev_sidedefs.Get(num);
}

linedef_c::linedef_c(int _idx, const raw_linedef_t *raw)
{
  index = _idx;

  start = lev_vertices.Get(UINT16(raw->start));
  end   = lev_vertices.Get(UINT16(raw->end));

  /* check for zero-length line */
  zero_len = (fabs(start->x - end->x) < DIST_EPSILON) && 
         (fabs(start->y - end->y) < DIST_EPSILON);

  flags = UINT16(raw->flags);
  type  = UINT16(raw->type);
  tag   = SINT16(raw->tag);

  two_sided = (flags & LINEFLAG_TWO_SIDED) ? true : false;

  right = SafeLookupSidedef(UINT16(raw->sidedef1));
  left  = SafeLookupSidedef(UINT16(raw->sidedef2));
}

linedef_c::linedef_c(int _idx, const raw_hexen_linedef_t *raw)
{
  index = _idx;

  start = lev_vertices.Get(UINT16(raw->start));
  end   = lev_vertices.Get(UINT16(raw->end));

  // check for zero-length line
  zero_len = (fabs(start->x - end->x) < DIST_EPSILON) && 
             (fabs(start->y - end->y) < DIST_EPSILON);

  flags = UINT16(raw->flags);
  type  = UINT8(raw->type);
  tag   = 0;

  /* read specials */
  for (int j = 0; j < 5; j++)
    specials[j] = UINT8(raw->specials[j]);

  // -JL- Added missing twosided flag handling that caused a broken reject
  two_sided = (flags & LINEFLAG_TWO_SIDED) ? true : false;

  right = SafeLookupSidedef(UINT16(raw->sidedef1));
  left  = SafeLookupSidedef(UINT16(raw->sidedef2));
}

linedef_c::~linedef_c()
{
}

//
// GetLinedefs
//
void GetLinedefs(wad_c *base)
{
  if (lev_doing_hexen)
  {
    GetLinedefsHexen(base);
    return;
  }
  
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("LINEDEFS");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_linedef_t);
  }

  if (!lump || count == 0)
    FatalError("Couldn't find any Linedefs");

# if DEBUG_LOAD
  PrintDebug("GetLinedefs: num = %d\n", count);
# endif

  lev_linedefs.Allocate(count);

  raw_linedef_t *raw = (raw_linedef_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_linedefs.Set(i, new linedef_c(i, raw));
  }
}

//
// GetLinedefsHexen
//
void GetLinedefsHexen(wad_c *base)
{
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("LINEDEFS");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_hexen_linedef_t);
  }

  if (!lump || count == 0)
    FatalError("Couldn't find any Linedefs");

# if DEBUG_LOAD
  PrintDebug("GetLinedefsHexen: num = %d\n", count);
# endif

  lev_linedefs.Allocate(count);

  raw_hexen_linedef_t *raw = (raw_hexen_linedef_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_linedefs.Set(i, new linedef_c(i, raw));
  }
}

static vertex_c *FindVertex16(uint16_g index)
{
  if (index & 0x8000)
    return lev_gl_verts.Get(index & 0x7FFF);

  return lev_vertices.Get(index);
}

static vertex_c *FindVertex32(int index)
{
  if (index & IS_GL_VERTEX)
    return lev_gl_verts.Get(index & ~IS_GL_VERTEX);

  return lev_vertices.Get(index);
}

seg_c::seg_c(int _idx, const raw_gl_seg_t *raw)
{
  index = _idx;

  next = NULL;

/// fprintf(stderr, "SEG %d  V %d..%d line %d side %d\n",
/// index, UINT16(raw->start), UINT16(raw->end), SINT16(raw->linedef), SINT16(raw->side));

  start = FindVertex16(UINT16(raw->start));
  end   = FindVertex16(UINT16(raw->end));

  linedef = (SINT16(raw->linedef) == -1) ? NULL :
    lev_linedefs.Get(UINT16(raw->linedef));

  side = UINT16(raw->side);

  sidedef_c *sss = NULL;

  if (linedef)
    sss = side ? linedef->left : linedef->right;
  
  sector = sss ? sss->sector : NULL;

  // partner is currently ignored

  precompute_data();
}

seg_c::seg_c(int _idx, const raw_v3_seg_t *raw)
{
  index = _idx;

  next = NULL;

  start = FindVertex32(UINT32(raw->start));
  end   = FindVertex32(UINT32(raw->end));

  linedef = (SINT16(raw->linedef) == -1) ? NULL :
    lev_linedefs.Get(UINT16(raw->linedef));

  side = UINT16(raw->flags) & V3SEG_F_LEFT;

  sidedef_c *sss = NULL;

  if (linedef)
    sss = side ? linedef->left : linedef->right;

  sector = sss ? sss->sector : NULL;

  // partner is currently ignored

  precompute_data();
}

seg_c::~seg_c()
{
}

void seg_c::precompute_data()
{
  psx = start->x;
  psy = start->y;

  pex = end->x;
  pey = end->y;

  pdx = pex - psx;
  pdy = pey - psy;

  p_length = UtilComputeDist(pdx, pdy);
  p_angle  = UtilComputeAngle(pdx, pdy);

/// fprintf(stderr, "|  (%1.0f,%1.0f)->(%1.0f,%1.0f)  Delta (%1.6f, %1.6f)\n",
/// psx, psy, pex, pey, pdx, pdy);

  if (p_length <= 0)
    PrintWarn("Seg %d has zero p_length.\n", index);

  p_perp =  psy * pdx - psx * pdy;
  p_para = -psx * pdx - psy * pdy;
}

//
// GetV3Segs
//
void GetV3Segs(const uint8_g *data, int length)
{
  int count = length / sizeof(raw_v3_seg_t);

  lev_segs.Allocate(count);

  raw_v3_seg_t *raw = (raw_v3_seg_t *) data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_segs.Set(i, new seg_c(i, raw));
  }
}

//
// GetGLSegs
//
void GetGLSegs(wad_c *base)
{
  lump_c *lump = base->FindLumpInLevel("GL_SEGS");

# if DEBUG_LOAD
  PrintDebug("GetVertices: num = %d\n", count);
# endif

  if (!lump)
    FatalError("Couldn't find any GL Segs");

  base->CacheLump(lump);

  if (lump->length >= 4 && memcmp(lump->data, lev_v3_magic, 4) == 0)
  {
    GetV3Segs((uint8_g *)lump->data + 4, lump->length - 4);
    return;
  }

  int count = lump->length / sizeof(raw_gl_seg_t);

  lev_segs.Allocate(count);

  raw_gl_seg_t *raw = (raw_gl_seg_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_segs.Set(i, new seg_c(i, raw));
  }
}


subsec_c::subsec_c(int _idx, const raw_subsec_t *raw)
{
  index = _idx;

  seg_count = UINT16(raw->num);
  int first = UINT16(raw->first);

  build_seg_list(first, seg_count);
}

subsec_c::subsec_c(int _idx, const raw_v3_subsec_t *raw)
{
  index = _idx;

  seg_count = UINT32(raw->num);
  int first = UINT32(raw->first);

  build_seg_list(first, seg_count);
}

subsec_c::~subsec_c()
{
}

void subsec_c::append_seg(seg_c *cur)
{
  if (! cur)
    InternalError("Missing seg for subsector !\n");

  mid_x += (cur->start->x + cur->end->x) / 2.0;
  mid_y += (cur->start->y + cur->end->y) / 2.0;

  // add it to the tail

  if (! seg_list)
  {
    seg_list = cur;
    return;
  }

  seg_c *tail = seg_list;

  while (tail->next)
    tail = tail->next;
  
  tail->next = cur;
}

void subsec_c::build_seg_list(int first, int count)
{
  seg_list = NULL;

  mid_x = 0;
  mid_y = 0;

  for (; count > 0; first++, count--)
  {
    seg_c *seg = lev_segs.Get(first);

    append_seg(seg);
  }

  mid_x /= count;
  mid_y /= count;
}

//
// GetV3Subsecs
//
void GetV3Subsecs(const uint8_g *data, int length)
{
  int count = length / sizeof(raw_v3_subsec_t);

  lev_subsecs.Allocate(count);

  raw_v3_subsec_t *raw = (raw_v3_subsec_t *) data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_subsecs.Set(i, new subsec_c(i, raw));
  }
}

//
// GetGLSubsecs
//
void GetGLSubsecs(wad_c *base)
{
  lump_c *lump = base->FindLumpInLevel("GL_SSECT");

# if DEBUG_LOAD
  PrintDebug("GetVertices: num = %d\n", count);
# endif

  if (!lump)
    FatalError("Couldn't find any GL Subsectors");

  base->CacheLump(lump);

  if (lump->length >= 4 && memcmp(lump->data, lev_v3_magic, 4) == 0)
  {
    GetV3Subsecs((uint8_g *)lump->data + 4, lump->length - 4);
    return;
  }

  int count = lump->length / sizeof(raw_subsec_t);

  lev_subsecs.Allocate(count);

  raw_subsec_t *raw = (raw_subsec_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_subsecs.Set(i, new subsec_c(i, raw));
  }
}

void bbox_t::from_raw(const raw_bbox_t *raw)
{
  minx = SINT16(raw->minx);
  miny = SINT16(raw->miny);
  maxx = SINT16(raw->maxx);
  maxy = SINT16(raw->maxy);
}

node_c::node_c(int _idx, const raw_node_t *raw)
{
  index = _idx;

  x  = SINT16(raw->x);
  y  = SINT16(raw->y);
  dx = SINT16(raw->dx);
  dy = SINT16(raw->dy);

  r.bounds.from_raw(&raw->b1);
  l.bounds.from_raw(&raw->b2);

  r.node = l.node = NULL;
  r.subsec = l.subsec = NULL;

  int r_idx = UINT16(raw->right);
  int l_idx = UINT16(raw->left);

  if (r_idx & 0x8000)
    r.subsec = lev_subsecs.Get(r_idx & 0x7FFF);
  else
    r.node = lev_nodes.Get(r_idx);

  if (l_idx & 0x8000)
    l.subsec = lev_subsecs.Get(l_idx & 0x7FFF);
  else
    l.node = lev_nodes.Get(l_idx);
}

node_c::~node_c()
{
}

//
// GetGLNodes
//
void GetGLNodes(wad_c *base)
{
  int count = -1;
  lump_c *lump = base->FindLumpInLevel("GL_NODES");

  if (lump)
  {
    base->CacheLump(lump);
    count = lump->length / sizeof(raw_node_t);
  }

  if (!lump || count < 1)
    return;

# if DEBUG_LOAD
  PrintDebug("GetStaleNodes: num = %d\n", count);
# endif

  lev_nodes.Allocate(count);

  raw_node_t *raw = (raw_node_t *) lump->data;

  for (int i = 0; i < count; i++, raw++)
  {
    lev_nodes.Set(i, new node_c(i, raw));
  }
}



/* ----- whole-level routines --------------------------- */

//
// LoadLevel
//
void LoadLevel(const char *level_name)
{
  // ---- Normal stuff ----

  if (! the_wad->FindLevel(level_name))
    FatalError("Unable to find level: %s\n", level_name);

  // -JL- Identify Hexen mode by presence of BEHAVIOR lump
  lev_doing_hexen = (the_wad->FindLumpInLevel("BEHAVIOR") != NULL);

  GetVertices(the_wad);
  GetSectors(the_wad);
  GetSidedefs(the_wad);
  GetLinedefs(the_wad);
  GetThings(the_wad);

  // ---- GL stuff ----

  char gl_name[16];
  
  sprintf(gl_name, "GL_%s", the_wad->current_level->name);

  wad_c *gl_wad = the_wad;

  if (! gl_wad->FindLevel(gl_name))
  {
    gl_wad = the_gwa;

    if (! gl_wad || ! gl_wad->FindLevel(gl_name))
      FatalError("Unable to find GL info (%s lump)\n", gl_name);
  }

  GetGLVerts(gl_wad);
  GetGLSegs(gl_wad);
  GetGLSubsecs(gl_wad);
  GetGLNodes(gl_wad);

/// PrintMsg("Loaded %d vertices, %d sectors, %d sides, %d lines, %d things\n", 
///     num_vertices, num_sectors, num_sidedefs, num_linedefs, num_things);

}

//
// FreeLevel
//
void FreeLevel(void)
{
  lev_vertices.FreeAll();
  lev_gl_verts.FreeAll();
  lev_linedefs.FreeAll();
  lev_sidedefs.FreeAll();
  lev_sectors.FreeAll();
  lev_things.FreeAll();
  lev_segs.FreeAll();
  lev_subsecs.FreeAll();
  lev_nodes.FreeAll();
}

//
// LevelGetBounds
//
void LevelGetBounds(double *lx, double *ly, double *hx, double *hy)
{
  node_c *root = lev_nodes.Get(lev_nodes.num - 1);

  *lx = MIN(root->l.bounds.minx, root->r.bounds.minx);
  *ly = MIN(root->l.bounds.miny, root->r.bounds.miny);
  *hx = MAX(root->l.bounds.maxx, root->r.bounds.maxx);
  *hy = MAX(root->l.bounds.maxy, root->r.bounds.maxy);
}