-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLoadLevel.cpp
4528 lines (3657 loc) · 135 KB
/
LoadLevel.cpp
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
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF OUTRAGE
ENTERTAINMENT, INC. ("OUTRAGE"). OUTRAGE, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1996-2000 OUTRAGE ENTERTAINMENT, INC. ALL RIGHTS RESERVED.
*/
#ifdef NEWEDITOR
#include "..\neweditor\stdafx.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "LoadLevel.h"
#include "CFILE.H"
#include "descent.h"
#include "object.h"
#include "gametexture.h"
#ifdef NEWEDITOR
#include "..\neweditor\ned_gametexture.h"
#include "..\neweditor\ned_Object.h"
#include "editor\Erooms.h"
#endif
#include "trigger.h"
#include "doorway.h"
#include "terrain.h"
#include "player.h"
#include "door.h"
#include "objinit.h"
#include "room.h"
#include "objinfo.h"
#include "lightmap.h"
#include "lightmap_info.h"
#include "FindIntersection.h"
#include "polymodel.h"
#include "object_lighting.h"
#include "bsp.h"
#include "gamepath.h"
#include "game.h"
#include "BOA.h"
#include "mem.h"
#include "lighting.h"
#include "Mission.h"
#include "render.h"
#include "weapon.h"
#include "special_face.h"
#include "stringtable.h"
#include "ambient.h"
#include "matcen.h"
//#include "dedicated_server.h"
#include "PHYSICS.H"
#include "levelgoal.h"
#include "aiambient.h"
#include "args.h"
#include "ddio.h"
#include "ship.h"
#include "fireball.h"
#include "sounds.h"
#include "soundload.h"
#include "bnode.h"
#include "localization.h"
#ifdef EDITOR
#include "editor\d3edit.h"
#include "editor\HFile.h"
#include "editor\Erooms.h"
#include "editor\moveworld.h"
#include "editor\editor_lighting.h"
#endif
#ifdef NEWEDITOR
#include "..\neweditor\neweditor.h"
#include "..\neweditor\globals.h"
#endif
char *LocalizeLevelName(char *level);
#define LEVEL_FILE_TAG "D3LV"
// Lightmap remap array
int Num_lightmap_infos_read = 0;
ushort LightmapInfoRemap[MAX_LIGHTMAP_INFOS];
// Arrays for mapping saved data to the current data
short texture_xlate[MAX_TEXTURES];
short door_xlate[MAX_DOORS];
short generic_xlate[MAX_OBJECT_IDS];
#ifdef EDITOR
extern float GlobalMultiplier;
#endif
// Xlate types
#define XT_GENERIC 0
#define XT_DOOR 1
#if (defined(EDITOR) || defined(NEWEDITOR))
// Code to keep track of failed xlate items so can print out a nice message if the item is used
#define MAX_FAILED_XLATE_ITEMS 1500
struct {
short type, id;
char name[PAGENAME_LEN];
} Failed_xlate_items[MAX_FAILED_XLATE_ITEMS];
int Num_failed_xlate_items;
void AddFailedXLateItem(int type, int id, char *name) {
ASSERT(Num_failed_xlate_items < MAX_FAILED_XLATE_ITEMS);
Failed_xlate_items[Num_failed_xlate_items].type = type;
Failed_xlate_items[Num_failed_xlate_items].id = id;
strcpy(Failed_xlate_items[Num_failed_xlate_items].name, name);
Num_failed_xlate_items++;
}
char *GetFailedXLateItemName(int type, int id) {
for (int i = 0; i < Num_failed_xlate_items; i++) {
if ((Failed_xlate_items[i].type == type) && (Failed_xlate_items[i].id == id))
return Failed_xlate_items[i].name;
}
return "<unknown>";
}
#endif
// Useful macros
#define cf_ReadVector(f, v) \
do { \
(v)->x = cf_ReadFloat(f); \
(v)->y = cf_ReadFloat(f); \
(v)->z = cf_ReadFloat(f); \
} while (0)
#define cf_ReadMatrix(f, m) \
do { \
cf_ReadVector((f), &(m)->rvec); \
cf_ReadVector((f), &(m)->uvec); \
cf_ReadVector((f), &(m)->fvec); \
} while (0)
// Lets put some function prototypes here
void ReadAllTriggers(CFILE *ifile);
void ReadAllDoorways(CFILE *ifile);
void WriteAllTriggers(CFILE *ofile);
void WriteAllDoorways(CFILE *ofile);
// Prior to version 49, terrain objects had this flag set
#define OLD_OBJECT_OVER_TERRAIN_FLAG 256
// Level checksum
int Level_checksum = -1;
// Macro to translate old file handles (pre-version 94) to new ones
#define OLD_HANDLE_OBJNUM_MASK 0x3ff // to mask off the object number part of the handle
#define OLD_HANDLE_COUNT_MASK 0xfffffc00 // to maks off the count part of the handle
#define XLATE_HANDLE(handle) ((((handle)&OLD_HANDLE_COUNT_MASK) << 1) + ((handle)&OLD_HANDLE_OBJNUM_MASK))
/*
The following struct/array is used for (right now only in OEM) to convert
a type/id object when loading a level to another type/id. It's only for
generic objects. For the object_convert[]:
index 0 = old object index 1 = new object
index 2 = old object index 3 = new object
...
Note: Set the id to -2, if it is -2, then I resolve it.
*/
#define CONV_MULTI 0x01
#define CONV_SINGLE 0x02
typedef struct {
int type, id;
char *name;
ubyte flag;
} tConvertObject;
tConvertObject object_convert[] = {
#ifdef DEMO
{OBJ_POWERUP, -2, "Blackshark", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "ImpactMortar", 0},
{OBJ_POWERUP, -2, "Mega", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "ImpactMortar", 0},
{OBJ_POWERUP, -2, "EMDlauncher", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "SuperLaser", 0},
{OBJ_POWERUP, -2, "Omegacannon", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Massdriver", 0},
#else
{OBJ_POWERUP, -2, "Blackshark", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "SuperLaser", 0},
{OBJ_POWERUP, -2, "Mega", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Plasmacannon", 0},
{OBJ_POWERUP, -2, "EMDlauncher", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Microwave", 0},
{OBJ_POWERUP, -2, "Omegacannon", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Massdriver", 0},
#endif
#ifdef DEMO
{OBJ_POWERUP, -2, "Plasmacannon", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "SuperLaser", 0},
{OBJ_POWERUP, -2, "Microwave", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "SuperLaser", 0},
{OBJ_POWERUP, -2, "NapalmRocket", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Homing", 0},
{OBJ_POWERUP, -2, "Guided", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Homing", 0},
{OBJ_POWERUP, -2, "4packGuided", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Homing", 0},
{OBJ_POWERUP, -2, "Cyclone", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "ImpactMortar", 0},
#endif
{OBJ_POWERUP, -2, "Fusioncannon", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Vauss", 0},
{OBJ_POWERUP, -2, "Smart", CONV_MULTI | CONV_SINGLE},
{OBJ_POWERUP, -2, "Homing", 0},
{OBJ_POWERUP, -2, "chaff", CONV_MULTI},
{OBJ_POWERUP, -2, "energy", 0},
{OBJ_POWERUP, -2, "Betty4pack", CONV_MULTI},
{OBJ_POWERUP, -2, "energy", 0},
{OBJ_POWERUP, -2, "Seeker3pack", CONV_MULTI},
{OBJ_POWERUP, -2, "energy", 0},
{OBJ_POWERUP, -2, "ProxMinepowerup", CONV_MULTI},
{OBJ_POWERUP, -2, "energy", 0}};
int object_convert_size = sizeof(object_convert) / sizeof(tConvertObject);
int chunk_start, chunk_size, filelen;
int CountDataToPageIn();
// Find a valid, usable ID of the specified type
int FindValidID(int type) {
int id = -1;
switch (type) {
case OBJ_ROBOT:
case OBJ_POWERUP:
case OBJ_BUILDING:
case OBJ_CLUTTER:
id = GetObjectID(type);
break;
case OBJ_DOOR: {
int i;
for (i = 0; i < MAX_DOORS; i++)
if (Doors[i].used) {
id = i;
break;
}
ASSERT(i < MAX_DOORS);
break;
}
default:
Int3();
}
return id;
}
// bash/converts generics, from those not allowed, to something that is allowed
void ConvertObject(int *type, int *id) {
// NOTE: Only type/id are valid at this point
#if ((!defined(OEM)) && (!defined(DEMO)))
// Non-OEM build
return;
#else
#ifdef EDITOR
// Editor OEM?
return;
#else
int new_type = *type, new_id = *id;
bool is_multi = (Game_mode & GM_MULTI) ? true : false;
bool c_multi, c_single, convert;
// bash/convert
// look at even numbered items, they are the object to convert
// odd number items are destination
for (int i = 0; i < object_convert_size; i += 2) {
if (*type == object_convert[i].type) {
// type match, check id's
if (object_convert[i].id == -2) {
object_convert[i].id = FindObjectIDName(object_convert[i].name);
}
ASSERT(object_convert[i].id >= 0);
if (*id == object_convert[i].id) {
convert = false;
c_multi = (object_convert[i].flag & CONV_MULTI) ? true : false;
c_single = (object_convert[i].flag & CONV_SINGLE) ? true : false;
if (is_multi && c_multi)
convert = true;
if ((!is_multi) && c_single)
convert = true;
// convert!
if (convert) {
int convert_to = i + 1;
if (object_convert[convert_to].id == -2) {
object_convert[convert_to].id = FindObjectIDName(object_convert[convert_to].name);
}
ASSERT(object_convert[convert_to].id >= 0);
if (object_convert[convert_to].id >= 0) {
mprintf(
(0, "LEVELLOAD: Converting: '%s' -> '%s'\n", object_convert[i].name, object_convert[convert_to].name));
new_id = object_convert[convert_to].id;
new_type = object_convert[convert_to].type;
}
}
}
}
}
*type = new_type;
*id = new_id;
#endif
#endif
}
// reads an object
// returns 1 if read ok
int ReadObject(CFILE *ifile, object *objp, int handle, int fileversion) {
int type, id, old_id, i;
int roomnum;
float door_shields;
char tempname[OBJ_NAME_LEN + 1] = "";
type = cf_ReadByte(ifile);
if (fileversion >= 34)
id = cf_ReadShort(ifile);
else
id = (ubyte)cf_ReadByte(ifile);
// Translate id
old_id = id;
switch (type) {
case OBJ_ROBOT:
case OBJ_POWERUP:
case OBJ_BUILDING:
case OBJ_CLUTTER:
id = generic_xlate[id];
break;
case OBJ_DOOR:
id = door_xlate[id];
break;
}
// Check for object not found
if (id == -1) {
id = FindValidID(type); // find any valid id
ASSERT(id != -1);
#if (defined(EDITOR) || defined(NEWEDITOR))
if (GetFunctionMode() == EDITOR_MODE) {
char *old_name = GetFailedXLateItemName((type == OBJ_DOOR) ? XT_DOOR : XT_GENERIC, old_id);
char *new_name = (type == OBJ_DOOR) ? Doors[id].name : Object_info[id].name;
OutrageMessageBox("Object %d (type %s) has undefined ID %d, \"%s\".\n\n"
"This object will be converted to ID %d, \"%s\".",
objp - Objects, Object_type_names[type], old_id, old_name, id, new_name);
}
#endif
}
// Convert un-allowed OEM objects
if (IS_GENERIC(type)) {
ConvertObject(&type, &id);
}
// Read the object's name
if (fileversion >= 95) {
cf_ReadString(tempname, sizeof(tempname), ifile);
if (strlen(tempname)) {
if (type == OBJ_PLAYER) {
#if (defined(EDITOR) || defined(NEWEDITOR))
if (GetFunctionMode() == EDITOR_MODE)
OutrageMessageBox("Object %d, Player %d has a name (\"%s\") which has been deleted.", objp - Objects, id,
tempname);
#endif
tempname[0] = 0; // kill name
}
}
}
// Read the flags
uint flags;
if (fileversion >= 101)
flags = cf_ReadInt(ifile);
else
flags = (ushort)cf_ReadShort(ifile);
// Make sure no objects except viewers have the outside mine flags set
ASSERT((type == OBJ_VIEWER) || !(flags & OF_OUTSIDE_MINE));
// If a door, read the shields
if ((type == OBJ_DOOR) && (fileversion >= 109))
door_shields = cf_ReadShort(ifile);
// Get the room number
roomnum = cf_ReadInt(ifile);
// For old files, check if this object is on terrain
if (fileversion < 49) {
if (flags & OLD_OBJECT_OVER_TERRAIN_FLAG) {
flags &= ~OLD_OBJECT_OVER_TERRAIN_FLAG;
roomnum = MAKE_ROOMNUM(roomnum);
}
}
// Get the position
vector pos;
cf_ReadVector(ifile, &pos);
// Initialize the object
ObjInit(objp, type, id, handle, &pos, 0.0);
// Set the stuff we've already read in
objp->flags |= (flags & OBJECT_SAVE_LOAD_FLAGS);
objp->roomnum = roomnum;
// Set shields if this is a door
if ((type == OBJ_DOOR) && (fileversion >= 109))
objp->shields = door_shields;
// Do some wacky weapon thing
if (fileversion <= 36) {
if (objp->type == OBJ_PLAYER || objp->type == OBJ_POWERUP || objp->type == OBJ_ROBOT ||
objp->type == OBJ_BUILDING || objp->type == OBJ_DOOR || objp->type == OBJ_DEBRIS)
objp->flags |= OF_POLYGON_OBJECT;
if (objp->type == OBJ_WEAPON) {
if (!((Weapons[objp->id].flags & WF_IMAGE_BITMAP) || (Weapons[objp->id].flags & WF_IMAGE_VCLIP)))
objp->flags |= OF_POLYGON_OBJECT;
}
}
// Load and set the orientation
matrix orient;
cf_ReadMatrix(ifile, &orient);
vm_Orthogonalize(&orient);
ObjSetOrient(objp, &orient);
ObjSetAABB(objp);
// Set the name
if (tempname[0]) {
objp->name = (char *)mem_malloc(strlen(tempname) + 1);
strcpy(objp->name, tempname);
}
// Update checksum
Level_checksum += (((int)objp->pos.x) >> 4);
Level_checksum += (((int)objp->pos.y) >> 4);
Level_checksum += (((int)objp->pos.z) >> 4);
objp->contains_type = cf_ReadByte(ifile);
objp->contains_id = cf_ReadByte(ifile);
objp->contains_count = cf_ReadByte(ifile);
objp->lifeleft = cf_ReadFloat(ifile);
if ((fileversion >= 65) && (fileversion < 111))
cf_ReadInt(ifile); // was parent_handle
// Read sound info if this is a soundsource object
if (objp->control_type == CT_SOUNDSOURCE) {
ASSERT(objp->type == OBJ_SOUNDSOURCE);
if (fileversion < 119)
objp->ctype.soundsource_info.sound_index = cf_ReadInt(ifile);
else {
char soundname[PAGENAME_LEN];
cf_ReadString(soundname, sizeof(soundname), ifile);
objp->ctype.soundsource_info.sound_index = soundname[0] ? FindSoundName(IGNORE_TABLE(soundname)) : -1;
}
objp->ctype.soundsource_info.volume = cf_ReadFloat(ifile);
}
if (fileversion >= 105) {
// read in default script override information
i = cf_ReadByte(ifile);
if (i > 0) {
objp->custom_default_script_name = (char *)mem_malloc(i + 1);
cf_ReadBytes((ubyte *)objp->custom_default_script_name, i, ifile);
objp->custom_default_script_name[i] = '\0';
} else {
objp->custom_default_script_name = NULL;
}
i = cf_ReadByte(ifile);
if (i > 0) {
objp->custom_default_module_name = (char *)mem_malloc(i + 1);
cf_ReadBytes((ubyte *)objp->custom_default_module_name, i, ifile);
objp->custom_default_module_name[i] = '\0';
} else {
objp->custom_default_module_name = NULL;
}
} else {
objp->custom_default_module_name = NULL;
objp->custom_default_script_name = NULL;
}
// load in obsolete script info
if (fileversion < LEVEL_FILE_OSIRIS1DEAD) {
if (fileversion < LEVEL_FILE_SCRIPTNAMES) {
cf_ReadInt(ifile);
} else {
char name[MAX_D3XID_NAME];
cf_ReadString(name, MAX_D3XID_NAME, ifile);
}
if (fileversion >= LEVEL_FILE_SCRIPTPARMS) {
short s = cf_ReadShort(ifile);
for (i = 0; i < s; i++) {
sbyte stype = cf_ReadByte(ifile);
if (stype == PARMTYPE_NUMBER || stype == PARMTYPE_REF) {
cf_ReadFloat(ifile);
} else if (stype == PARMTYPE_VECTOR) {
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
} else {
Int3(); // -get samir.
}
}
}
if (fileversion >= LEVEL_FILE_SCRIPTCHECK) {
cf_ReadByte(ifile);
}
}
// lightmap data.
int lmdata;
if (fileversion <= 34)
lmdata = 0;
else
lmdata = cf_ReadByte(ifile);
if (lmdata) // lightmap data follows
{
poly_model *pm = &Poly_models[objp->rtype.pobj_info.model_num];
int num_models, num_faces, num_verts;
int i, t, k;
int model_changed = 0, clear_lightmaps = 0;
num_models = cf_ReadByte(ifile);
if (pm->n_models != num_models) {
model_changed = 1;
mprintf((0, "Polymodel %s has changed since this level was lit!\n", pm->name));
}
if (!model_changed)
SetupObjectLightmapMemory(objp);
for (i = 0; i < num_models; i++) {
int submodel_changed = 0;
num_faces = cf_ReadShort(ifile);
if (num_faces != objp->lm_object.num_faces[i]) {
submodel_changed = 1;
clear_lightmaps = 1;
}
for (t = 0; t < num_faces; t++) {
if (submodel_changed == 0 && model_changed == 0) {
lightmap_object_face *fp = &objp->lm_object.lightmap_faces[i][t];
fp->lmi_handle = LightmapInfoRemap[(ushort)cf_ReadShort(ifile)];
if (!Dedicated_server)
LightmapInfo[fp->lmi_handle].used++;
else
clear_lightmaps = 1;
if (fileversion <= 88) {
cf_ReadByte(ifile);
cf_ReadByte(ifile);
cf_ReadByte(ifile);
cf_ReadByte(ifile);
}
if (fileversion >= 58) {
// Read normal,uvec,rvec
if (fileversion <= 59) {
vector tvec;
cf_ReadVector(ifile, &tvec);
}
cf_ReadVector(ifile, &fp->rvec);
cf_ReadVector(ifile, &fp->uvec);
} else {
vm_MakeZero(&fp->rvec);
vm_MakeZero(&fp->uvec);
fp->rvec.x = 1;
fp->uvec.y = 1;
}
num_verts = cf_ReadByte(ifile);
if (num_verts == fp->num_verts) {
for (k = 0; k < num_verts; k++) {
fp->u2[k] = cf_ReadFloat(ifile);
fp->v2[k] = cf_ReadFloat(ifile);
}
} else {
for (k = 0; k < num_verts; k++) {
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
}
}
} else {
cf_ReadShort(ifile);
if (fileversion <= 88) {
cf_ReadByte(ifile);
cf_ReadByte(ifile);
cf_ReadByte(ifile);
cf_ReadByte(ifile);
}
if (fileversion >= 58) {
vector tvec;
// Read normal,uvec,rvec
if (fileversion <= 59) {
cf_ReadVector(ifile, &tvec);
}
cf_ReadVector(ifile, &tvec);
cf_ReadVector(ifile, &tvec);
}
num_verts = cf_ReadByte(ifile);
for (k = 0; k < num_verts; k++) {
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
}
}
}
}
if (clear_lightmaps) {
mprintf((0, "Freeing lightmaps because model %s has changed since this level was saved!\n", pm->name));
ClearObjectLightmaps(objp);
}
}
return 1;
}
// Old portal trigger flag
#define OLD_TF_PORTAL 1
// Reads a trigger
// Returns: true if read ok, else false
int ReadTrigger(CFILE *ifile, trigger *tp, int fileversion) {
if (fileversion >= 99)
cf_ReadString(tp->name, sizeof(tp->name), ifile);
else
tp->name[0] = 0;
tp->roomnum = cf_ReadShort(ifile);
tp->facenum = cf_ReadShort(ifile);
tp->flags = cf_ReadShort(ifile);
tp->activator = cf_ReadShort(ifile);
// Kill old portal trigger flag
if (fileversion < 103) {
if (tp->flags & OLD_TF_PORTAL) {
tp->facenum = Rooms[tp->roomnum].portals[tp->facenum].portal_face;
tp->flags &= ~OLD_TF_PORTAL;
}
}
ASSERT(Rooms[tp->roomnum].faces[tp->facenum].flags & FF_HAS_TRIGGER);
if (fileversion < LEVEL_FILE_OSIRIS1DEAD) {
if (fileversion < LEVEL_FILE_SCRIPTNAMES)
cf_ReadInt(ifile);
else {
char name[MAX_D3XID_NAME];
cf_ReadString(name, MAX_D3XID_NAME, ifile);
if (fileversion >= LEVEL_FILE_TRIGPARMS) {
int i;
short s = cf_ReadShort(ifile);
for (i = 0; i < s; i++) {
sbyte type = cf_ReadByte(ifile);
if (type == PARMTYPE_NUMBER || type == PARMTYPE_REF) {
cf_ReadFloat(ifile);
} else if (type == PARMTYPE_VECTOR) {
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
} else {
Int3(); // -get samir.
}
}
}
}
if (fileversion >= LEVEL_FILE_SCRIPTCHECK) {
cf_ReadByte(ifile);
}
}
return 1;
}
// Old portal trigger face flag
#define OLD_FF_PORTAL_TRIG 0x0020
// Reads a face from a disk file
// Parameters: ifile - file to read from
// fp - face to read
// version - the version number of the file being read
// Returns: 1 if read ok, else 0
int ReadFace(CFILE *ifile, face *fp, int version) {
int nverts, i;
// Get number of verts
nverts = cf_ReadByte(ifile);
// Initialize & alloc memory
InitRoomFace(fp, nverts);
// Read vertices
for (i = 0; i < fp->num_verts; i++)
fp->face_verts[i] = cf_ReadShort(ifile);
// Read uvls, and adjust alpha settings
int alphaed = 0;
for (i = 0; i < fp->num_verts; i++) {
fp->face_uvls[i].u = cf_ReadFloat(ifile);
fp->face_uvls[i].v = cf_ReadFloat(ifile);
if (version < 56) {
// Read old lrgb stuff
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
cf_ReadFloat(ifile);
}
if (version >= 21) {
if (version < 61)
fp->face_uvls[i].alpha = Float_to_ubyte(cf_ReadFloat(ifile));
else
fp->face_uvls[i].alpha = cf_ReadByte(ifile);
} else
fp->face_uvls[i].alpha = 1.0;
if (fp->face_uvls[i].alpha != 255)
alphaed = 1;
}
// Read flags
if (version < 27)
fp->flags = cf_ReadByte(ifile);
else
fp->flags = cf_ReadShort(ifile);
// Kill old portal trigger flag
if (version < 103)
fp->flags &= ~OLD_FF_PORTAL_TRIG;
// Set vertex alpha flag
if (alphaed)
fp->flags |= FF_VERTEX_ALPHA;
else
fp->flags &= ~FF_VERTEX_ALPHA;
// Read the portal number
if (version >= 23)
fp->portal_num = cf_ReadByte(ifile);
else
fp->portal_num = cf_ReadShort(ifile);
// Read and translate the texture number
fp->tmap = texture_xlate[cf_ReadShort(ifile)];
// Check for failed xlate
if (fp->tmap == -1) {
fp->tmap = 0;
}
// Check to see if there is a lightmap
if ((fp->flags & FF_LIGHTMAP) && (version >= 19)) {
if (version <= 29) {
ubyte w, h;
w = cf_ReadByte(ifile);
h = cf_ReadByte(ifile);
for (i = 0; i < w * h; i++)
cf_ReadShort(ifile);
fp->flags &= ~FF_LIGHTMAP;
} else {
// Read lightmap info handle
int lmi_handle = (ushort)cf_ReadShort(ifile);
if (!Dedicated_server) {
fp->lmi_handle = LightmapInfoRemap[lmi_handle];
LightmapInfo[fp->lmi_handle].used++;
}
if (version <= 88) {
cf_ReadByte(ifile);
cf_ReadByte(ifile);
cf_ReadByte(ifile);
cf_ReadByte(ifile);
}
}
// Read UV2s
for (i = 0; i < fp->num_verts; i++) {
fp->face_uvls[i].u2 = cf_ReadFloat(ifile);
fp->face_uvls[i].v2 = cf_ReadFloat(ifile);
// Stupid fix for bad lightmap uvs
if (fp->face_uvls[i].u2 < 0)
fp->face_uvls[i].u2 = 0;
if (fp->face_uvls[i].u2 > 1)
fp->face_uvls[i].u2 = 1.0;
if (fp->face_uvls[i].v2 < 0)
fp->face_uvls[i].v2 = 0;
if (fp->face_uvls[i].v2 > 1)
fp->face_uvls[i].v2 = 1.0;
}
}
if (version >= 22 && version <= 29) {
vector vec;
cf_ReadVector(ifile, &vec);
}
if (version >= 40 && version <= 60) // was shadow room,face
{
cf_ReadShort(ifile);
cf_ReadShort(ifile);
}
if (version >= 50) {
fp->light_multiple = cf_ReadByte(ifile);
if (fp->light_multiple == 186)
fp->light_multiple =
4; // Get Jason, I'm looking for this bug! Its safe to go past it, but I'm just on the lookout
if (version <= 52) {
if (fp->light_multiple >= 32)
Int3(); // Get Jason
fp->light_multiple *= 4;
}
} else
fp->light_multiple = 4;
if (version >= 71) {
ubyte special = cf_ReadByte(ifile);
if (special) {
if (version < 77) // Ignore old specular data
{
vector center;
cf_ReadByte(ifile);
cf_ReadVector(ifile, ¢er);
cf_ReadShort(ifile);
} else {
vector center;
ubyte smooth = 0;
ubyte num_smooth_verts = 0;
ubyte type = cf_ReadByte(ifile);
ubyte num = cf_ReadByte(ifile);
if (version >= 117) {
// Read if smoothed
smooth = cf_ReadByte(ifile);
if (smooth) {
num_smooth_verts = cf_ReadByte(ifile);
fp->special_handle = AllocSpecialFace(type, num, true, num_smooth_verts);
} else
fp->special_handle = AllocSpecialFace(type, num);
} else
fp->special_handle = AllocSpecialFace(type, num);
ASSERT(fp->special_handle != BAD_SPECIAL_FACE_INDEX);
for (i = 0; i < num; i++) {
cf_ReadVector(ifile, ¢er);
ushort color = cf_ReadShort(ifile);
SpecialFaces[fp->special_handle].spec_instance[i].bright_center = center;
SpecialFaces[fp->special_handle].spec_instance[i].bright_color = color;
}
if (smooth) {
for (i = 0; i < num_smooth_verts; i++) {
vector vertnorm;
cf_ReadVector(ifile, &vertnorm);
SpecialFaces[fp->special_handle].vertnorms[i] = vertnorm;
}
}
}
}
}
return 1;
}
// Old has trigger portal flag
#define OLD_PF_HAS_TRIGGER 4
// Reads a portal to a disk file
// Parameters: ifile - file to Read to
// pp - portal to Read
// Returns: 1 if written ok, else 0
int ReadPortal(CFILE *ifile, portal *pp, int version) {
int i, num_faces;
pp->flags = cf_ReadInt(ifile);
// Kill old trigger flag
if (version < 103)
pp->flags &= ~OLD_PF_HAS_TRIGGER;
if (version < 80) { // read old list of portal verts
int num_verts = cf_ReadShort(ifile);
for (i = 0; i < num_verts; i++)
cf_ReadShort(ifile); // was pp->portal_verts[i]
num_faces = cf_ReadShort(ifile);
ASSERT(num_faces == 1);
}
pp->portal_face = cf_ReadShort(ifile);
pp->croom = cf_ReadInt(ifile);
pp->cportal = cf_ReadInt(ifile);
if (version >= 123)
pp->bnode_index = cf_ReadShort(ifile);
else
pp->bnode_index = -1;
if (version >= 63) {
cf_ReadVector(ifile, &pp->path_pnt);
}
if (version >= 100)
pp->combine_master = cf_ReadInt(ifile);
return 1;
}
int n_degenerate_faces_removed;
// Remove faces that have no area, as determined by their surface normal being NULL
// This code should come out after the clipper is fixed to not create degenerate faces
#ifdef EDITOR
void RemoveDegenerateFaces(room *rp) {
int f;
int n_removed = 0;
for (f = rp->num_faces - 1; f >= 0; f--) {
face *fp = &rp->faces[f];
if ((fp->normal.x == 0.0) && (fp->normal.y == 0.0) && (fp->normal.z == 0.0)) {
mprintf((0, "Deleting face %d from room %d\n", f, ROOMNUM(rp)));
DeleteRoomFace(rp, f);
n_degenerate_faces_removed++;
}
}
}
#endif
#define NO_COMPRESS 0
#define COMPRESS 1
// Does a RLE compression run of the values given the byte array 'val'.