Debian Codegraph @

Descent3/LoadLevel.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/main/LoadLevel.cpp $
21 * $Revision: 346 $
22 * $Date: 7/09/01 4:24p $
23 * $Author: Matt $
24 *
25 * Routines to read and write D3 level files
26 *
27 * $Log: /DescentIII/main/LoadLevel.cpp $
28 *
29 * 346 7/09/01 4:24p Matt
30 * Changed the code that added normals into the level checksum to fix the
31 * incompatibility between Windows, Linux, and Mac.
32 *
33 * 345 5/02/00 6:16p Matt
34 * Added Ceiling_height to the level checksum.
35 *
36 * 344 4/19/00 5:16p Matt
37 * From Duane for 1.4
38 * Added assert()
39 *
40 * 343 4/07/00 5:52p Matt
41 * Incorporate face normals into the level checksum to keep people from
42 * using levels with flipped normals that let them leave the mine.
43 *
44 * 342 4/05/00 9:06a Matt
45 * Disable loading of lightmaps in a dedicated server. They were being
46 * loaded buy never freed, causing a memory leak.
47 *
48 * 341 3/20/00 12:13p Matt
49 * Merge of Duane's post-1.3 changes.
50 * Removed dynamic lightmap info system.
51 *
52 * 340 12/22/99 4:09p Gwar
53 * fixed a bug that was killing portions of LightmapInfo in NEWEDITOR
54 *
55 * 339 10/25/99 2:52p Kevin
56 * fixed bug with lightmap infos
57 *
58 * 338 10/25/99 2:30p Jeff
59 * fixed bug from mac merge
60 *
61 * 337 10/25/99 9:48a Matt
62 * Mac code that should enable the dynamic allocation of lightmap_info
63 * structures. This should be disabled for editor builds.
64 *
65 * 336 10/22/99 11:05p Jeff
66 * mac merge
67 *
68 * 335 10/22/99 10:53p Matt
69 * Mac merge
70 *
71 * 334 10/22/99 3:43p Kevin
72 * Fixes for mac merge
73 *
74 * 333 10/21/99 3:27p Gwar
75 * changes to keep NEWEDITOR compatibility with D3 v1.2 and earlier (for
76 * now)
77 *
78 * 332 10/14/99 1:08p Chris
79 * Added another custom bounce force field texture slot
80 *
81 * 331 10/13/99 2:19p Chris
82 * Added the ability to disable wind for powerups on a level
83 *
84 * 330 10/13/99 10:41a Chris
85 * Added 'special forcefields' that have custom bounce factors
86 *
87 * 329 10/12/99 11:18a Gwar
88 * fixed compilation errors
89 *
90 * 328 10/12/99 10:14a Gwar
91 * adding terrain support to NEWEDITOR
92 *
93 * 327 10/08/99 4:28p Chris
94 * Added the forcefield and glass breaking override options
95 *
96 * 326 10/08/99 3:17p Matt
97 * Added -noshells command-line option to disable shell computation on
98 * level save.
99 *
100 * 325 9/30/99 4:40p Jeff
101 * command line option to disable terrain fog for Linux build
102 *
103 * 324 9/18/99 9:22p Jeff
104 * initialize all the object's position history (reset them) after the
105 * objects are loaded (needed for Motion blur)
106 *
107 * 323 8/12/99 5:26p Matt
108 * Added a chunk to store room wind vectors.
109 *
110 * 322 8/11/99 9:14p Gwar
111 * matcen support for NEWEDITOR
112 *
113 * 321 7/20/99 12:59p Jason
114 * added auto katmai support
115 *
116 * 320 6/18/99 11:53a Chris
117 * Fixed problem where BOA wasn't being saved for neweditor levels
118 *
119 * 319 6/16/99 4:25p Jason
120 * first pass at supporting object lighting in the new editor
121 *
122 * 318 6/16/99 2:16a Gwar
123 * adding level goals support to new editor
124 *
125 * 317 6/14/99 2:09a Gwar
126 * adding door support and various cleanups for NEWEDITOR
127 *
128 * 316 6/13/99 5:56p Jeff
129 * fixed ALife chunk for neweditor
130 *
131 * 315 6/12/99 6:57p Jeff
132 * call FreeTriggers and ClearTerrainSound
133 *
134 * 314 6/11/99 4:53p Gwar
135 * added a necessary #ifndef NEWEDITOR
136 *
137 * 313 6/10/99 4:12p Kevin
138 * Fixed SetLevel in dedicated server for HEAT.NET, and added
139 * level_names.str for level name localization.
140 *
141 * 312 6/10/99 1:11p Gwar
142 * adding door support for NEWEDITOR
143 *
144 * 311 5/25/99 1:00a Jeff
145 * changed the way level checksum is generated so it works with Linux
146 * (some levels didn't)
147 *
148 * 310 5/23/99 3:06a Jason
149 * save editor lighting settings
150 *
151 * 309 5/19/99 12:19a Gwar
152 * #define Curroomp, Curface, etc as members of prim struct in NEWEDITOR
153 * -- this allows the main room frame and level window to share these
154 * variables and makes them non-global (i.e. no more sync problem between
155 * world and room views)
156 *
157 * 308 5/18/99 10:55p Chris
158 * Improved the Bnode verification process
159 *
160 * 307 5/18/99 11:13a Matt
161 * Added variable ceiling height.
162 *
163 * 306 5/18/99 10:31a Gwar
164 * quick change that writes out a default level goal chunk. now the old
165 * editor can load levels saved in the new editor (at least for the few
166 * multiplayer levels that i tested)
167 *
168 * 305 5/17/99 6:27p Chris
169 * Added a bnode verify to the level save
170 *
171 * 304 5/17/99 11:49a Kevin
172 * Added the ability to save a level from the new editor
173 *
174 * 303 5/13/99 5:57p Matt
175 * Changed a bunch more static arrays to be malloc'd, to get around the
176 * Mac's limit of 32K of local data.
177 *
178 * 302 5/12/99 11:40a Matt
179 * Changed some arrays to be malloc'd instead of declared locally on the
180 * stack, because the Mac limits local data to 32K.
181 *
182 * 301 5/10/99 10:22p Ardussi
183 * changes to compile on Mac
184 *
185 * 300 5/06/99 5:18p Chris
186 * Updated the BNode system - vastly improved error checking and verify
187 *
188 * 299 5/05/99 10:54p Gwar
189 * adding object support to new editor -- place objects with right-click
190 * in orthogonal views (still needs many bug fixes :)
191 *
192 * 298 5/03/99 3:28p Jason
193 * save out lightmap spacing
194 *
195 * 297 5/02/99 6:34a Gwar
196 * prepared editor for RC1...various tweaks and such
197 *
198 * 296 5/02/99 6:47a Chris
199 * Final patch for the bnode system.... I hope. :)
200 *
201 * 295 5/02/99 6:36a Chris
202 * Patched Bnode system
203 *
204 * 294 4/29/99 5:41p Jeff
205 * fixed accidental bug due to mix of new editor and old editor with
206 * unlinking the objects
207 *
208 * 293 4/29/99 3:10a Gwar
209 * added UI code for objects (basically a close copy of the texture code
210 * that does the same thing for textures)
211 *
212 * 292 4/29/99 1:59a Chris
213 * Added the portal blockage support
214 *
215 * 291 4/28/99 7:39p Jason
216 * fixed loadlevel lightmap bug
217 *
218 * 290 4/26/99 10:32p Chris
219 * Improving the BNode system
220 *
221 * 289 4/26/99 11:11a Chris
222 * Updated Bnode system
223 *
224 * 288 4/22/99 9:46a Kevin
225 * Fixed compiler error for non EDITOR builds
226 *
227 * 287 4/21/99 11:43p Matt
228 * Added code to save the names of items we couldn't xlate on load, so we
229 * can give a good message for objects that use those items, and we don't
230 * have to give a message for the item itself (which may not be used).
231 *
232 * 286 4/21/99 12:40p Chris
233 * Added the FVI_always_check_ceiling flag
234 *
235 * 285 4/20/99 8:14p Chris
236 * Added support for object's that hit the ceiling and for making the
237 * level always check for the ceiling (inside and outside the mine)
238 *
239 * 284 4/20/99 5:12p Matt
240 * Fixed stupid shields bug.
241 *
242 * 283 4/20/99 12:24p Matt
243 * Re-revised the ObjInit() system. ObjInit() again does all the
244 * initialization stuff, but now it's passed a lot more information so
245 * those fields can be set before the rest of the initialization takes
246 * place.
247 *
248 * 282 4/19/99 10:23p Kevin
249 * A bunch of little things for the new demo
250 *
251 * 281 4/19/99 6:09p Kevin
252 * put vital memset back in
253 *
254 * 280 4/19/99 9:10a Kevin
255 * Fixes for demo2
256 *
257 * 279 4/18/99 5:32p Jason
258 * fixed object problem in multiplayer
259 *
260 * 278 4/18/99 6:02a Chris
261 * Fixed a small bug with writing BNode chunks
262 *
263 * 277 4/18/99 5:39a Chris
264 * Vastly improved the path node system
265 *
266 * 276 4/17/99 3:59p Kevin
267 * Made convertobjects happing in the demo as well as the OEM
268 *
269 * 275 4/15/99 12:18p Jason
270 * made mirrors more robust and able to have multiple mirrored faces in
271 * the same room (as long as they are all on the same plane)
272 *
273 * 274 4/14/99 2:50a Jeff
274 * fixed some case mismatched #includes
275 *
276 * 273 4/12/99 3:04p Jason
277 * changes for low memory
278 *
279 * 272 4/11/99 3:02p Matt
280 * Fixed small bug
281 *
282 * 271 4/11/99 2:41a Jeff
283 * had to include stdafx.h for neweditor
284 *
285 * 270 4/10/99 5:56p Matt
286 * Cleaned up object initialization code, including create object & load
287 * object.
288 *
289 * 269 4/07/99 5:46p Kevin
290 * Support for the editor chunk in the new editor
291 *
292 * 268 4/06/99 5:22p Matt
293 * On level save, move the BOA, AABB, & shell stuff before the file open,
294 * so crashes in these functions won't trash the file. Also, check for
295 * too many portals and warn the user.
296 *
297 * 267 4/05/99 3:34p Matt
298 * Cleaned up player start flags
299 *
300 * 266 4/04/99 6:26p Matt
301 * Don't require specific cases for all object types for ID translation;
302 * let most objects be handled by the default case.
303 *
304 * 265 4/03/99 7:38p Matt
305 * Don't read in names for player objects.
306 *
307 * 264 4/01/99 9:42a Matt
308 * Compute room shells when saving a level.
309 *
310 * 263 3/31/99 12:43p Kevin
311 * Fixed some object stuff
312 *
313 * 262 3/30/99 2:43p Kevin
314 * Got gr support in the new editor project
315 *
316 * 261 3/29/99 5:32p Kevin
317 * Build fixes
318 *
319 * 260 3/28/99 3:55p Kevin
320 * Working on getting the new editor framework working.
321 *
322 * 259 3/27/99 5:36p Matt
323 * Save sounds by name instead of index
324 *
325 * 258 3/24/99 5:52p Jason
326 * added per room ambience settings
327 *
328 * 257 3/22/99 6:26p Matt
329 * Cleaned up error handling in cfile and editor level loads.
330 *
331 * 256 3/10/99 7:12p Jason
332 * added smooth specular shading for curved surfaces
333 *
334 * 255 3/05/99 6:50p Jason
335 * fixed sound bands for OEM
336 *
337 * 254 3/05/99 2:31p Jeff
338 * filter out fusion and smart missile for oem
339 *
340 * 253 3/04/99 5:03p Jeff
341 * filter countermeasures in multiplayer
342 *
343 * 252 3/04/99 1:40p Jeff
344 * created a level filter array to convert objects from one type to
345 * another on level load
346 *
347 * 251 3/01/99 10:39p Matt
348 * Deleted obsolete code for reading/writing powerup ammo
349 *
350 * 250 2/28/99 9:28p Matt
351 * Cleaned up load level error handing and version checking.
352 *
353 * 249 2/26/99 3:26p Jason
354 * made satellites clip properly
355 *
356 * 248 2/25/99 10:45p Matt
357 * Small fix
358 *
359 * 247 2/23/99 11:59a Jason
360 * Fixed bug with lowlightmapped levels not being able to be loaded
361 *
362 * 246 2/21/99 4:20p Matt
363 * Added SoundSource objects (and reformatted parts of the object header
364 * files).
365 *
366 * 245 2/21/99 12:29p Matt
367 * Added terrain sound system
368 *
369 * 244 2/20/99 8:58p Jeff
370 * fixed deleted string
371 *
372 * 243 2/19/99 4:30p Jason
373 * took out int3
374 *
375 * 242 2/19/99 4:28p Jason
376 * fixed defines for katmai
377 *
378 * 241 2/19/99 3:18p Jason
379 * won't let you save levels that have bad light multiples...
380 *
381 * 240 2/19/99 2:13p Jason
382 * looking for light multiple bug...
383 *
384 * 239 2/19/99 12:04p Jason
385 * took out sky band
386 *
387 * 238 2/18/99 2:21p Kevin
388 * More/better progress indicators for load level
389 *
390 * 237 2/18/99 12:32p Jason
391 * added room multiplier
392 *
393 * 236 2/16/99 12:38p Kevin
394 * Improved paging data progress indicator
395 *
396 * 235 2/14/99 3:26p Chris
397 * FIxed a load/save bug... hehe
398 *
399 * 234 2/10/99 4:56p Kevin
400 * Better progress indicator & prepare for Descent message
401 *
402 * 233 2/10/99 4:38p Matt
403 * Misc. changes for table file parsing
404 *
405 * 232 2/10/99 1:49p Matt
406 * Don't have parent_handle, since it's always none.
407 *
408 * 231 2/09/99 7:01p Kevin
409 * First work for new and improved progress screen while loading a level.
410 * Note that this is a hack at this point, while I get the details worked
411 * out, then I'll make it cleaner.
412 *
413 * 230 2/09/99 12:36p Matt
414 * Added code to save out old level scripts
415 *
416 * 229 2/09/99 9:58a Chris
417 * Massive BOA update :) Terrain happy now. Vis happy now. Sound happy
418 * now.
419 *
420 * 228 2/04/99 2:05p Matt
421 * Added blastable doors
422 *
423 * 227 2/03/99 5:48p Matt
424 * Added room damage system
425 *
426 * 226 2/03/99 12:15p Jason
427 * added multiplayer vis optimizations
428 *
429 * 225 2/02/99 10:53a Matt
430 * Made warning messages only come up if in the editor
431 *
432 * 224 2/02/99 10:46a Matt
433 * Only reset the wireframe view radius when loading an actual new level,
434 * and not whenever a level is loading, which incudes going from the game
435 * to the editor.
436 *
437 * 223 2/02/99 10:29a Kevin
438 * Fixed code so the main project will build
439 *
440 * 222 2/01/99 11:36p Matt
441 * Improved handling of missing object types
442 *
443 * 221 1/29/99 5:22p Jeff
444 * localization
445 *
446 * 220 1/29/99 12:48p Matt
447 * Rewrote the doorway system
448 *
449 * 219 1/26/99 11:38a Jason
450 * small changes for level loading
451 *
452 * 218 1/23/99 4:05p Matt
453 * Fixed two stupid bugs from when I removed the portal triggers. I'm so
454 * embarrassed.
455 *
456 * 217 1/22/99 8:53p Jeff
457 * added custom-default script overrides
458 *
459 * 216 1/22/99 4:45p Jason
460 * fixed load problem
461 *
462 * 215 1/22/99 4:35p Jason
463 * made a default sky
464 *
465 * 214 1/22/99 3:59p Jason
466 * added 256x256 textures to help with terrain skies
467 *
468 * 213 1/21/99 11:34a Matt
469 * Got rid of portal triggers. Since we don't have multi-face portals, a
470 * face trigger works fine for a portal. Also fixed a few editor/trigger
471 * bugs.
472 *
473 * 212 1/20/99 10:50a Jason
474 * added new terrain
475 *
476 * 211 1/18/99 2:46p Matt
477 * Combined flags & flags2 fields in object struct
478 *
479 * 210 1/15/99 5:57p Chris
480 *
481 * 209 1/15/99 3:14p Jason
482 * added combinable portals
483 *
484 * 208 1/14/99 11:06a Matt
485 * Added names to triggers
486 *
487 * 207 1/08/99 5:37p Samir
488 * reverb values per room.
489 *
490 * 206 1/08/99 2:55p Samir
491 * Ripped out OSIRIS1.
492 *
493 * 205 12/22/98 2:03p Matt
494 * Added room names, and made rooms not compress so that room numbers are
495 * suitable for persistant uses.
496 *
497 * 204 12/21/98 11:30p Matt
498 * Added names for objects
499 *
500 * 203 12/21/98 6:00p Matt
501 * Changed the object handle bit allocation to handle object numbers up to
502 * 2047.
503 * Increased MAX_OBJECTS to 1500.
504 *
505 * 202 12/18/98 5:40p Chris
506 * Ambient life is now all sequenced up
507 *
508 * 201 12/14/98 6:31p Jason
509 * fixed extra-dumb bug
510 *
511 * 200 12/14/98 3:50p Jason
512 * fixed stupid loadlevel error with old levels and 128x128 lightmaps
513 *
514 * 199 12/11/98 1:55p Chris
515 * Reduced BOA's size and changed how it works with no path situations
516 *
517 * 198 12/08/98 12:20p Jason
518 * fixed versioning problem with Squeezing
519 *
520 * 197 12/08/98 12:08p Jason
521 * fixed bogus lightmap bug
522 *
523 * 196 12/07/98 7:12p Jay
524 * Removed call to RemoveDegenerateFaces on room load, because future
525 * chunks depend on the number of faces not to change during level load,
526 * these faces get removed later during a verify (according to MattT).
527 * -Jeff
528 *
529 * 195 12/07/98 10:28a Sean
530 * fixed bug with changed lightmaps
531 *
532 * 194 12/03/98 5:02p Jason
533 * changed squeeze lightmap parameters
534 *
535 * 193 12/03/98 12:33p Jason
536 * more changes for new lightmap system
537 *
538 * 192 12/02/98 5:06p Jason
539 * First pass at getting mega-lightmaps wprking
540 *
541 * 191 11/30/98 5:26p Jason
542 * more lightmap changes
543 *
544 * 190 11/30/98 3:59p Jason
545 * changed dynamic lighting to be better
546 *
547 * 189 11/24/98 10:36a Jason
548 * undid stupid bug
549 *
550 * 188 11/23/98 4:54p Jason
551 * added squeeze lightmaps code, but commented it out
552 *
553 * 187 11/18/98 4:30p Jason
554 * added some functionality to terrain fog
555 *
556 * 186 11/11/98 11:41a Jason
557 * added sunlight damage
558 *
559 * 185 11/02/98 6:15p Chris
560 * Room AABBs get saved with the level and the sort_face and dec_sort_face
561 * list s have been removed
562 *
563 * 184 10/23/98 8:28p Samir
564 * enhanced load level screen.
565 *
566 * 183 10/19/98 5:40p Samir
567 * added loading level callback.
568 *
569 * 182 10/19/98 10:47a Jay
570 * fixed stupid array indexing bug
571 *
572 * 181 10/17/98 11:11p Matt
573 * Added room memory allocation system
574 *
575 * 180 10/14/98 4:39p Matt
576 * Changed OutrageMessageBox() calls to use either Error() or
577 * EditorMessageBox()
578 *
579 * 179 10/11/98 6:13p Matt
580 * Added code to prevent zero-length mallocs
581 *
582 * 178 10/08/98 4:23p Kevin
583 * Changed code to comply with memory library usage. Always use mem_malloc
584 * , mem_free and mem_strdup
585 *
586 * 177 10/05/98 2:42a Chris
587 * Level goals are relatively functional
588 *
589 * 176 10/04/98 1:25p Matt
590 *
591 * 175 9/24/98 5:54p Chris
592 *
593 * 174 9/24/98 5:41p Chris
594 * Designers can save out gravity changes
595 *
596 * 173 9/18/98 7:25p Matt
597 * Put in code to handle a bunch of renamed doors.
598 *
599 * 172 9/16/98 12:07p Chris
600 * Improved BOA AABB computation with room checksums
601 *
602 * 171 9/15/98 4:31p Jason
603 * added more functionality for the dedicated server
604 *
605 * 170 9/15/98 3:42p Chris
606 * Fixed a bug with matcen saving
607 *
608 * 169 9/10/98 11:32a Chris
609 * Matcens are improving
610 *
611 * 168 9/08/98 4:56p Chris
612 * Matcens rev.5 almost functional
613 *
614 * 167 9/04/98 12:29p Matt
615 * Added marked edge & vertex in the editor
616 *
617 * 166 9/03/98 6:56p Sean
618 * Added some more NULLing of pointers
619 *
620 * 165 9/01/98 12:04p Matt
621 * Ripped out multi-face portal code
622 *
623 * 164 8/27/98 5:19p Jason
624 * added first rev of reflected surfaces
625 *
626 * 163 8/26/98 1:27p Dan
627 * added lightmap safety feature
628 *
629 * 162 8/25/98 5:21p Jason
630 * fixed bug with my last rev
631 *
632 * 161 8/25/98 5:19p Jason
633 * checked in to verify object list
634 *
635 * 160 8/24/98 12:24p Jason
636 * added waypoints and player start position flags
637 *
638 * 159 8/17/98 6:40p Matt
639 * Added ambient sound system
640 *
641 * 158 8/10/98 11:25a Jason
642 * added better specular lighting
643 *
644 * 157 8/04/98 2:32p Chris
645 * Improved attach code added more fixes to the AABB partial computation
646 * patch
647 *
648 * 156 8/03/98 5:47p Chris
649 * Improved the partial AABB computation
650 *
651 * 155 7/20/98 6:07p Jason
652 * Save BOA vis checksum to file
653 *
654 * 154 7/20/98 12:04p Jason
655 * added per level satellite lighting
656 *
657 * 153 6/16/98 10:54a Jeff
658 *
659 * 152 6/10/98 12:44p Jason
660 * fixed stupid bug
661 *
662 * 151 6/10/98 12:23p Jason
663 * added new skies
664 *
665 * 150 6/05/98 5:22p Jason
666 * added volumetric fog
667 *
668 * 149 6/04/98 6:16p Jason
669 * added terrain occlusion system
670 *
671 * 148 6/02/98 6:03p Jason
672 * added specular lightmaps
673 *
674 * 147 5/26/98 7:34p Dan
675 * some assertion (someone else added this, and I'm checking it on Dan's
676 * machine- samir)
677 *
678 * 146 5/26/98 7:07p Samir
679 * write out is_custom element of script structure for obejcts and
680 * triggers.
681 *
682 * 145 5/25/98 6:49p Matt
683 * Added needed include.
684 *
685 * 144 5/25/98 3:46p Jason
686 * added better light glows
687 *
688 * 143 5/22/98 5:12p Jason
689 * save out sky radius
690 *
691 * 142 5/22/98 3:27p Jason
692 * added specular lighting
693 *
694 * 141 5/15/98 5:41p Jason
695 * implemented volume lighting system
696 *
697 * 140 5/14/98 12:56p Jason
698 * changes to help lower memory usage
699 *
700 * 139 5/06/98 1:08p Sean
701 * fixed object relinking bug
702 *
703 * 138 5/04/98 1:12a Chris
704 * Added parenting bug checking
705 *
706 * 137 5/01/98 3:51p Jason
707 * sped up terrain rendering by precaching all megacell lod bitmaps
708 *
709 * 136 4/23/98 7:48p Jason
710 * made older levels work with new 1555 format
711 *
712 * 135 4/24/98 1:53a Samir
713 * took care of a lot of scripting memory leaks.
714 *
715 * 134 4/22/98 12:38p Chris
716 * Added path points to portals and rooms. Improved BOA auto-making
717 * process.
718 *
719 * 133 4/20/98 5:48p Chris
720 * Patching a collision bug
721 *
722 * 132 4/20/98 12:46p Jason
723 * added level checksumming for multiplayer games
724 *
725 * 131 4/15/98 3:28p Jason
726 * changed glow stuff to work with new system
727 *
728 * 130 4/14/98 7:50p Matt
729 * Added system to keep info for each level
730 *
731 * 129 4/14/98 11:00a Matt
732 * Fixed problem reading in lightmaps for objects whose model has changed.
733 *
734 * 128 4/03/98 10:23a Chris
735 * BOA cost array is saved out
736 *
737 * 127 4/02/98 12:23p Jason
738 * trimmed some fat from our structures
739 *
740 * 126 3/31/98 4:23p Chris
741 * Made it conform with the new mem_malloc function call
742 *
743 * 125 3/31/98 3:49p Jason
744 * added memory lib
745 *
746 * 124 3/29/98 11:58p Jason
747 * saved some memory with object lightmaps
748 *
749 * 123 3/27/98 7:29p Jason
750 * changed lightmap normals to work correctly
751 *
752 * 122 3/26/98 2:14p Jason
753 * added dynamic lighting for objects
754 *
755 * 121 3/17/98 10:07a Chris
756 * Fixed problem that only levels with paths cleared the previous level's
757 * paths
758 *
759 * 120 3/11/98 5:14p Jason
760 * incremental lighting checkin
761 *
762 * 119 2/27/98 3:15p Jason
763 * Don't save terrain pixel error stuff
764 *
765 * 118 2/26/98 6:00p Jason
766 * fixed another object lightmap problem
767 *
768 * 117 2/26/98 5:21p Jason
769 * fixed lightmap rempapping bug
770 *
771 * 116 2/26/98 2:13p Jason
772 * Clear object lightmaps if object has changed between save and loading
773 * of level
774 *
775 * 115 2/23/98 12:24p Jason
776 * took out bsp stuff for now
777 *
778 * 114 2/19/98 11:17a Chris
779 * Tweaked the BIG_OBJECT system
780 *
781 * 113 2/18/98 8:43p Chris
782 * Made BOA get saved with the level. It should only get built when it
783 * needs to be updated.
784 *
785 * 112 2/18/98 5:44p Jason
786 * fixed potential lightmap reloading problem
787 *
788 * 111 2/18/98 1:23p Jason
789 * fixed lightmap problem that slipped through my last rev
790 *
791 * 110 2/18/98 1:21p Jason
792 * upped max lightmap count
793 *
794 * 109 2/17/98 8:57p Jason
795 * took out loading/saving of useless rgbl data
796 *
797 * 108 2/15/98 9:06p Matt
798 * Added nice error message when couldn't open level file for writing.
799 *
800 * 107 2/15/98 8:01p Matt
801 * Save wireframe view info in level file
802 *
803 * 106 2/15/98 7:44p Matt
804 * Added groovy try/catch/throw error checking for cfile functions
805 *
806 * 105 2/13/98 12:57p Jason
807 * changes for adjusting light multiples
808 *
809 * 104 2/13/98 12:52p Jason
810 * made lighting multipliers work in quarter steps instead of whole steps
811 *
812 * 103 2/10/98 3:49p Jason
813 * added pulsing walls
814 *
815 * 102 2/05/98 4:25p Chris
816 * Fixed a few bugs in the path system, improved the insert node function,
817 * and made the fvec and uvec of the node work (is even loaded and saved)
818 * correctly.
819 *
820 * 101 2/04/98 8:25p Jason
821 * added light multiplier for faces
822 *
823 * 100 2/04/98 6:09p Matt
824 * Changed object room number to indicate a terrain cell via a flag. Got
825 * rid of the object flag which used to indicate terrain.
826 *
827 * 99 2/03/98 5:01p Chris
828 * UPdated the path system
829 *
830 * 98 2/02/98 4:21p Samir
831 * Now we save trigger script parameter info properly.
832 *
833 * 97 1/29/98 5:49p Matt
834 * Changed old camera object type to be viewer object (for editor), and
835 * now camera objects will just be for game cameras.
836 *
837 * 96 1/27/98 1:21p Samir
838 * Make sure player object is initialized after loading level.
839 *
840 * 95 1/27/98 12:01p Chris
841 * Game path system now works with new, load, and save in the editor.
842 * Also, the game path system is further bug proved.
843 *
844 * 94 1/23/98 6:25p Jason
845 * Got spray weapons working
846 *
847 * 93 1/21/98 12:32p Matt
848 * Revamped viewer system
849 *
850 * 92 1/20/98 4:12p Samir
851 * Load and save script parameters.
852 *
853 * 91 1/19/98 10:04a Matt
854 * Added new object handle system
855 *
856 * 90 1/16/98 8:06p Jason
857 * added selectable halos and atmosphere blends to satellites
858 *
859 * 89 1/15/98 7:34p Matt
860 * Revamped error checking when computing face normals
861 *
862 * 88 1/15/98 12:42p Jason
863 * big fix for terrain sky rendering
864 *
865 * 87 1/14/98 7:35p Jason
866 * fixed horizon texture shifting between saves/loads
867 *
868 * 86 1/13/98 3:09p Jason
869 * added glow effect for engines
870 *
871 * 85 1/07/98 4:19p Jason
872 * added dome to terrain sky
873 *
874 * 84 12/31/97 12:51p Jason
875 * debugged and implementd BSP tree for lighting
876 *
877 * 83 12/17/97 5:26p Jason
878 * added support for selectable wraparound sky
879 *
880 * 82 11/25/97 12:28p Jason
881 * another pass at shadows
882 *
883 * 81 11/19/97 11:17a Jason
884 * fixed it so that if a designer changes a model after a level has been
885 * lit, then that level can still be loaded. Previously this was not so.
886 *
887 * 80 11/17/97 8:40p Jason
888 * added better error checking for lightmaps...trying to track down bug
889 *
890 * 79 11/17/97 3:14p Jason
891 * Fixed bug with loading in unused lightmaps
892 *
893 * 78 11/10/97 5:22p Jason
894 * made lightmap spacing use floats instead of ubytes
895 *
896 * 77 11/03/97 1:52p Jason
897 * compress lightmaps when reading/writing
898 *
899 * 76 10/31/97 12:35p Jason
900 * fixed bug that my previous bug fix introduced
901 *
902 * 75 10/30/97 6:31p Jason
903 * fixed potential lightmap bugs
904 *
905 * 74 10/21/97 11:08a Jason
906 * added check for OF_POLYGON_OBJECT
907 *
908 * 73 10/13/97 3:23p Samir
909 * Nullify script name entry for older levels.
910 *
911 * 72 10/08/97 3:41p Matt
912 * Xlate door objects
913 *
914 * 71 10/03/97 5:47p Matt
915 * Fixed stupid bug I introduced a couple versions ago
916 *
917 * 70 10/03/97 12:42p Jason
918 * save "keys_needed" info about doorways
919 *
920 * 69 10/02/97 1:04p Jason
921 * implemented FreeAllObjects
922 *
923 * 68 10/02/97 12:09p Jason
924 * made object lightmaps save and load
925 *
926 * 67 10/01/97 7:51p Matt
927 * Added code for external rooms
928 *
929 * 66 9/29/97 12:09p Jason
930 * added functionality to doorway system
931 *
932 * 65 9/25/97 5:28p Samir
933 * Even newer script code due to more changes in ObjCScript.cpp
934 *
935 * 64 9/25/97 3:49p Jason
936 * sped up terrain reading/writing
937 *
938 * 63 9/24/97 6:18p Samir
939 * Use script names instead of script id values to identify scripts.
940 *
941 * 62 9/22/97 5:59p Samir
942 * Changed ObjCScript system, so everything is broken, but it shouldn't
943 * crash the game.
944 *
945 * 61 9/21/97 7:17p Jason
946 * Made terrain lightmaps work again
947 *
948 * 60 9/19/97 8:09p Jason
949 * optimizations for dynamic lighting
950 *
951 * 59 9/19/97 6:58p Chris
952 * Fixed some bugs with the big object system and removed some annoying
953 * mprintf's
954 *
955 * 58 9/19/97 2:52p Jason
956 * changes to fix lightmap seam problem
957 *
958 * 57 9/18/97 10:42a Jason
959 * took out alpha resetting
960 *
961 * 56 9/17/97 1:24p Matt
962 * Ripped out segment code
963 *
964 * 55 9/17/97 12:50p Samir
965 * BIG SEGMENT RIPOUT
966 *
967 * 54 9/16/97 4:22p Matt
968 * Don't save/load the (now deleted) static_light field in a room
969 *
970 * 53 9/15/97 5:32p Jason
971 * did a hack for alphas (to be taken out after the milestone)
972 *
973 * 52 9/15/97 12:52p Samir
974 * ifdefed out some non editor code checking degen faces.
975 *
976 * 51 9/12/97 9:01p Matt
977 * Added code to remove degenerate faces
978 *
979 * 50 9/12/97 5:38p Jason
980 * got doors working
981 *
982 * 49 9/12/97 3:58p Matt
983 * Ripped out more segment code
984 *
985 * 48 9/12/97 2:35p Matt
986 * Save/load face flags as a word
987 *
988 * 47 9/11/97 2:00p Matt
989 * Added code to read & write triggers
990 *
991 * 46 9/10/97 11:14p Matt
992 * Fixed bug caused when a line of code was accidentally deleted
993 *
994 * 45 9/10/97 1:56p Samir
995 * Added code for saving and loading levels with level scripts.
996 *
997 * 44 9/09/97 6:15p Jason
998 * Added groovy dynamic terrain lighting system
999 *
1000 * 43 9/09/97 2:13p Samir
1001 * Added code to write out script filename in script chunk.
1002 *
1003 * 42 9/09/97 12:22p Matt
1004 * Renamed a flag
1005 *
1006 * 41 9/08/97 4:13p Jason
1007 * added colored lighting for the terrain
1008 *
1009 * 40 9/08/97 12:16p Matt
1010 * Save & Load portal number as byte
1011 *
1012 * 39 9/05/97 11:03p Matt
1013 * Ripped out segment code
1014 *
1015 * 38 9/02/97 6:40p Matt
1016 * Fixed code & added functions for group save/load
1017 *
1018 * 37 9/02/97 5:17p Jason
1019 * changes for dynamic lighting
1020 *
1021 * 36 9/02/97 12:54p Jason
1022 * classify faces as alphaed or not
1023 *
1024 * 35 9/02/97 11:47a Jason
1025 * Got alpha per vertex working
1026 *
1027 * 34 8/26/97 4:53p Jason
1028 * fixed terrain lighting bug
1029 *
1030 * 33 8/26/97 4:36p Jason
1031 * added terrain radiosity
1032 *
1033 * 32 8/25/97 7:33p Chris
1034 * Added a ResetTerrain() call to a REALLY REALLY annoying lockup.
1035 *
1036 * 31 8/25/97 12:41p Chris
1037 * Made sure that all object movements and mine changes result in AABB
1038 * updates.
1039 *
1040 * 30 8/22/97 2:42p Jason
1041 * added sky saving to loadlevel routines
1042 *
1043 * 29 8/22/97 10:35a Jason
1044 * fixed a problem where I was saving out the fact that a terrain segment
1045 * was linked to the mine, but wasn't saving what segment it was linked to
1046 * - duh!
1047 *
1048 * 28 8/20/97 7:31p Mark
1049 * Fixed object read code, which wasn't remapping clutter or building
1050 * object ids (Matt on Mark's machine)
1051 *
1052 * 27 8/20/97 7:18p Craig
1053 * Killed troublesome assert (Matt on Craig's machine)
1054 *
1055 * 26 8/20/97 10:54a Matt
1056 * Added an assert
1057 *
1058 * 25 8/18/97 4:06p Samir
1059 * Added include to stdlib.h
1060 *
1061 * 24 8/18/97 1:45a Chris
1062 * Samir enabled the editor to function
1063 *
1064 * 23 8/15/97 2:33p Jason
1065 * fixed misplaced texture problem with terrain
1066 *
1067 * 22 8/13/97 11:52a Jason
1068 * now saves lightmap info
1069 *
1070 * 21 8/12/97 10:28a Samir
1071 * Got rid of obsolete script stuff and read in a script handle for each
1072 * object.
1073 *
1074 * 20 8/11/97 1:53p Matt
1075 * Ripped out robot & powerup pages, and added generic page
1076 *
1077 * 19 8/07/97 3:19p Jason
1078 * replaced old terrain tmap system with new one
1079 *
1080 * 18 8/06/97 11:43a Jason
1081 * made lightmaps work correctly in the terrain
1082 *
1083 * 17 8/04/97 10:48a Matt
1084 * Translate textures when reading in a room face
1085 *
1086 * 16 8/01/97 5:37p Jason
1087 * Now saves terrain uv's in chunk format
1088 *
1089 * 15 8/01/97 4:36p Jason
1090 * stopped redundant resetting of terrain
1091 *
1092 * 14 8/01/97 3:59p Jason
1093 * added loading and saving of terrain chunks
1094 *
1095 * 13 7/28/97 1:15p Chris
1096 * Removed powerup->creation_time
1097 *
1098 * 12 7/24/97 3:23p Matt
1099 * Saved several additional editor state variables
1100 *
1101 * 11 7/23/97 6:27p Jason
1102 * added code to support terrain simplification
1103 *
1104 * 10 7/22/97 10:32a Matt
1105 * Added code to load and save portals
1106 *
1107 * 9 7/20/97 7:37p Jason
1108 * added new sky
1109 *
1110 * 8 7/17/97 7:22p Matt
1111 * Save and load level scripts
1112 *
1113 * 7 7/17/97 3:57p Matt
1114 * Re-orthogonalize the orientation matrices of objects when read it
1115 *
1116 * 6 7/17/97 3:00p Jason
1117 * changed "moons" to be called satellites since we're going to have suns
1118 * and other things in orbit
1119 *
1120 *
1121 * 35 6/24/97 4:23p Jason
1122 * changes for y only terrain positions
1123 *
1124 * 34 6/17/97 4:16p Jason
1125 * added some terrain features
1126 *
1127 * 33 6/02/97 1:06p Samir
1128 * Initialize scripts for objects (for now, NULL)
1129 *
1130 * 32 5/22/97 5:16p Jason
1131 * added tmap2 capability to the terrain
1132 *
1133 * 31 5/15/97 2:16p Matt
1134 * Set Highest_segment_index when reading in a level (for when no editor)
1135 *
1136 * 30 5/13/97 10:29p Matt
1137 * Fixed a stupid bug
1138 *
1139 * 29 5/13/97 7:47p Matt
1140 * Added code to compress mine before saving.
1141 * Cleaned up code dealing with num vertices & segments.
1142 *
1143 * 28 5/13/97 12:01p Matt
1144 * Added support for floating segments
1145 *
1146 * 27 5/09/97 1:44p Matt
1147 * Fixed code that was deleting a terrain object because it thought it had
1148 * a bad segnum. Not only that, but it was deleting the object
1149 * improperly.
1150 *
1151 * 26 5/08/97 3:21p Jason
1152 * made terrain save/restore mine links correctly
1153 *
1154 * 25 5/06/97 3:46p Matt
1155 * Init terrain data structs before loading from disk
1156 *
1157 * 24 5/01/97 6:15p Jason
1158 * fixed stupid sky bug...I wasn't reading in the correct number of satellite
1159 * vectors
1160 *
1161 * 23 4/17/97 12:14p Jason
1162 * now saves terrain sky info
1163 *
1164 * 22 4/04/97 2:57p Matt
1165 * Added code to initialize all the type-specific data for an object from
1166 * the page for that object type. This means that we need to pass less
1167 * info to ObjCreate(), and that we save less info in the level save file.
1168 * It also makes it easy to reset all the objects when an object page has
1169 * changed.
1170 *
1171 * 21 4/03/97 12:46p Matt
1172 * Added code to remap robots, powerups, & doors
1173 *
1174 * 20 4/01/97 10:48p Matt
1175 * Record player starts after loading in level
1176 *
1177 * 19 3/21/97 5:25p Matt
1178 * Fixed problem with writing out textures for remapping if array had
1179 * unused textures in it.
1180 *
1181 * 18 3/21/97 5:01p Jason
1182 * incremental terrain improvments
1183 *
1184 *
1185 * 17 3/17/97 7:36p Samir
1186 * Stupid index counter bugs.
1187 *
1188 * 16 3/14/97 12:38p Samir
1189 * Resets active doorways to 0 when loading level.
1190 *
1191 * 15 3/14/97 12:17p Chris
1192 * Abstract type for the low level sound lib
1193 *
1194 * 14 3/12/97 3:23p Matt
1195 * Make ReadSegment() and WriteSegment() public, and removed a bunch of
1196 * version number passing that wasn't being used.
1197 *
1198 * 13 3/05/97 3:33p Samir
1199 * Saving and loading of triggers, doorways.
1200 *
1201 * 12 3/03/97 6:21p Matt
1202 * Changed cfile functions to use D3 naming convention
1203 *
1204 * 11 3/03/97 11:29a Matt
1205 * Save & load selected segment list
1206 *
1207 * 10 2/28/97 2:29 PM Jeremy
1208 * #ifdef EDITOR around ResetFreeSegList and ResetVertices calls.
1209 *
1210 * 9 2/27/97 4:35p Matt
1211 * Fixed up loading of objects
1212 *
1213 * 8 2/27/97 11:04a Matt
1214 * Save texture names to file, & remap textures when loading
1215 *
1216 * 7 2/26/97 12:14p Matt
1217 * Fixed bug when Markedsegp == NULL
1218 *
1219 * 6 2/24/97 5:44p Matt
1220 * Save & load curseg & markedseg
1221 *
1222 * 5 2/21/97 7:30p Matt
1223 * New names for a few segment elements
1224 *
1225 * 4 2/21/97 5:23p Matt
1226 * Added new ResetVertices() to set Vertex_active array after level
1227 * loaded. Renamed old ResetVertices() to InitVertices();
1228 *
1229 * 3 2/11/97 6:52p Matt
1230 * Made level save/read now do objects
1231 *
1232 * 2 2/10/97 5:38p Matt
1233 * Got code working
1234 *
1235 * 1 2/10/97 11:21a Matt
1236 *
1237 * $NoKeywords: $
1238 */
1239
1240 #ifdef NEWEDITOR
1241 #include "..\neweditor\stdafx.h"
1242 #endif
1243
1244 #include <string.h>
1245 #include <stdlib.h>
1246 #include <errno.h>
1247 #include <algorithm>
1248
1249 #include "LoadLevel.h"
1250
1251 #include "cfile.h"
1252
1253 #include "descent.h"
1254 #include "object.h"
1255 #include "gametexture.h"
1256
1257 #ifdef NEWEDITOR
1258 #include "..\neweditor\ned_gametexture.h"
1259 #include "..\neweditor\ned_Object.h"
1260 #include "editor\Erooms.h"
1261 #endif
1262
1263 #include "trigger.h"
1264 #include "doorway.h"
1265 #include "terrain.h"
1266 #include "player.h"
1267 #include "door.h"
1268 #include "objinit.h"
1269 #include "room.h"
1270 #include "objinfo.h"
1271 #include "lightmap.h"
1272 #include "lightmap_info.h"
1273 #include "findintersection.h"
1274 #include "polymodel.h"
1275 #include "object_lighting.h"
1276 #include "bsp.h"
1277 #include "gamepath.h"
1278 #include "game.h"
1279 #include "BOA.h"
1280 #include "mem.h"
1281 #include "lighting.h"
1282 #include "Mission.h"
1283 #include "render.h"
1284 #include "weapon.h"
1285 #include "special_face.h"
1286 #include "stringtable.h"
1287 #include "ambient.h"
1288 #include "matcen.h"
1289 #include "dedicated_server.h"
1290 #include "physics.h"
1291 #include "levelgoal.h"
1292 #include "aiambient.h"
1293 #include "args.h"
1294 #include "ddio.h"
1295 #include "ship.h"
1296 #include "fireball.h"
1297 #include "sounds.h"
1298 #include "soundload.h"
1299 #include "bnode.h"
1300 #include "localization.h"
1301
1302 #ifdef EDITOR
1303 #include "editor\d3edit.h"
1304 #include "editor\HFile.h"
1305 #include "editor\Erooms.h"
1306 #include "editor\moveworld.h"
1307 #include "editor\editor_lighting.h"
1308 #endif
1309
1310 #ifdef NEWEDITOR
1311 #include "..\neweditor\neweditor.h"
1312 #include "..\neweditor\globals.h"
1313 #endif
1314
1315 MD5 *Level_md5 = NULL;
1316
1317 char *LocalizeLevelName(char *level);
1318
1319 #define LEVEL_FILE_TAG "D3LV"
1320
1321 // Lightmap remap array
1322 int Num_lightmap_infos_read = 0;
1323 uint16_t LightmapInfoRemap[MAX_LIGHTMAP_INFOS];
1324
1325 // Arrays for mapping saved data to the current data
1326 int16_t texture_xlate[MAX_TEXTURES];
1327 int16_t door_xlate[MAX_DOORS];
1328 int16_t generic_xlate[MAX_OBJECT_IDS];
1329
1330 #ifdef EDITOR
1331 extern float GlobalMultiplier;
1332 #endif
1333
1334 // Xlate types
1335 #define XT_GENERIC 0
1336 #define XT_DOOR 1
1337
1338 #if (defined(EDITOR) || defined(NEWEDITOR))
1339 // Code to keep track of failed xlate items so can print out a nice message if the item is used
1340
1341 #define MAX_FAILED_XLATE_ITEMS 1500
1342
1343 struct {
1344 int16_t type, id;
1345 char name[PAGENAME_LEN];
1346 } Failed_xlate_items[MAX_FAILED_XLATE_ITEMS];
1347
1348 int Num_failed_xlate_items;
1349
1350 void AddFailedXLateItem(int type, int id, char *name) {
1351 ASSERT(Num_failed_xlate_items < MAX_FAILED_XLATE_ITEMS);
1352
1353 Failed_xlate_items[Num_failed_xlate_items].type = type;
1354 Failed_xlate_items[Num_failed_xlate_items].id = id;
1355 strcpy(Failed_xlate_items[Num_failed_xlate_items].name, name);
1356
1357 Num_failed_xlate_items++;
1358 }
1359
1360 char *GetFailedXLateItemName(int type, int id) {
1361 for (int i = 0; i < Num_failed_xlate_items; i++) {
1362 if ((Failed_xlate_items[i].type == type) && (Failed_xlate_items[i].id == id))
1363 return Failed_xlate_items[i].name;
1364 }
1365
1366 return "<unknown>";
1367 }
1368
1369 #endif
1370
1371 // Useful macros
1372 #define cf_ReadVector(f, v) \
1373 do { \
1374 (v)->x = cf_ReadFloat(f); \
1375 (v)->y = cf_ReadFloat(f); \
1376 (v)->z = cf_ReadFloat(f); \
1377 } while (0)
1378 #define cf_ReadMatrix(f, m) \
1379 do { \
1380 cf_ReadVector((f), &(m)->rvec); \
1381 cf_ReadVector((f), &(m)->uvec); \
1382 cf_ReadVector((f), &(m)->fvec); \
1383 } while (0)
1384
1385 // Lets put some function prototypes here
1386 void ReadAllTriggers(CFILE *ifile);
1387 void ReadAllDoorways(CFILE *ifile);
1388 void WriteAllTriggers(CFILE *ofile);
1389 void WriteAllDoorways(CFILE *ofile);
1390
1391 // Prior to version 49, terrain objects had this flag set
1392 #define OLD_OBJECT_OVER_TERRAIN_FLAG 256
1393
1394 // Macro to translate old file handles (pre-version 94) to new ones
1395 #define OLD_HANDLE_OBJNUM_MASK 0x3ff // to mask off the object number part of the handle
1396 #define OLD_HANDLE_COUNT_MASK 0xfffffc00 // to maks off the count part of the handle
1397 #define XLATE_HANDLE(handle) ((((handle) & OLD_HANDLE_COUNT_MASK) << 1) + ((handle) & OLD_HANDLE_OBJNUM_MASK))
1398
1399 /*
1400
1401 The following struct/array is used for (right now only in OEM) to convert
1402 a type/id object when loading a level to another type/id. It's only for
1403 generic objects. For the object_convert[]:
1404
1405 index 0 = old object index 1 = new object
1406 index 2 = old object index 3 = new object
1407 ...
1408
1409 Note: Set the id to -2, if it is -2, then I resolve it.
1410 */
1411 #define CONV_MULTI 0x01
1412 #define CONV_SINGLE 0x02
1413 struct tConvertObject {
1414 int type, id;
1415 const char *name;
1416 uint8_t flag;
1417 };
1418
1419 tConvertObject object_convert[] = {
1420 #ifdef DEMO
1421 {OBJ_POWERUP, -2, "Blackshark", CONV_MULTI | CONV_SINGLE},
1422 {OBJ_POWERUP, -2, "ImpactMortar", 0},
1423 {OBJ_POWERUP, -2, "Mega", CONV_MULTI | CONV_SINGLE},
1424 {OBJ_POWERUP, -2, "ImpactMortar", 0},
1425 {OBJ_POWERUP, -2, "EMDlauncher", CONV_MULTI | CONV_SINGLE},
1426 {OBJ_POWERUP, -2, "SuperLaser", 0},
1427 {OBJ_POWERUP, -2, "Omegacannon", CONV_MULTI | CONV_SINGLE},
1428 {OBJ_POWERUP, -2, "Massdriver", 0},
1429 #else
1430 {OBJ_POWERUP, -2, "Blackshark", CONV_MULTI | CONV_SINGLE},
1431 {OBJ_POWERUP, -2, "SuperLaser", 0},
1432 {OBJ_POWERUP, -2, "Mega", CONV_MULTI | CONV_SINGLE},
1433 {OBJ_POWERUP, -2, "Plasmacannon", 0},
1434 {OBJ_POWERUP, -2, "EMDlauncher", CONV_MULTI | CONV_SINGLE},
1435 {OBJ_POWERUP, -2, "Microwave", 0},
1436 {OBJ_POWERUP, -2, "Omegacannon", CONV_MULTI | CONV_SINGLE},
1437 {OBJ_POWERUP, -2, "Massdriver", 0},
1438 #endif
1439
1440 #ifdef DEMO
1441 {OBJ_POWERUP, -2, "Plasmacannon", CONV_MULTI | CONV_SINGLE},
1442 {OBJ_POWERUP, -2, "SuperLaser", 0},
1443 {OBJ_POWERUP, -2, "Microwave", CONV_MULTI | CONV_SINGLE},
1444 {OBJ_POWERUP, -2, "SuperLaser", 0},
1445 {OBJ_POWERUP, -2, "NapalmRocket", CONV_MULTI | CONV_SINGLE},
1446 {OBJ_POWERUP, -2, "Homing", 0},
1447 {OBJ_POWERUP, -2, "Guided", CONV_MULTI | CONV_SINGLE},
1448 {OBJ_POWERUP, -2, "Homing", 0},
1449 {OBJ_POWERUP, -2, "4packGuided", CONV_MULTI | CONV_SINGLE},
1450 {OBJ_POWERUP, -2, "Homing", 0},
1451 {OBJ_POWERUP, -2, "Cyclone", CONV_MULTI | CONV_SINGLE},
1452 {OBJ_POWERUP, -2, "ImpactMortar", 0},
1453 #endif
1454 {OBJ_POWERUP, -2, "Fusioncannon", CONV_MULTI | CONV_SINGLE},
1455 {OBJ_POWERUP, -2, "Vauss", 0},
1456 {OBJ_POWERUP, -2, "Smart", CONV_MULTI | CONV_SINGLE},
1457 {OBJ_POWERUP, -2, "Homing", 0},
1458 {OBJ_POWERUP, -2, "chaff", CONV_MULTI},
1459 {OBJ_POWERUP, -2, "energy", 0},
1460 {OBJ_POWERUP, -2, "Betty4pack", CONV_MULTI},
1461 {OBJ_POWERUP, -2, "energy", 0},
1462 {OBJ_POWERUP, -2, "Seeker3pack", CONV_MULTI},
1463 {OBJ_POWERUP, -2, "energy", 0},
1464 {OBJ_POWERUP, -2, "ProxMinepowerup", CONV_MULTI},
1465 {OBJ_POWERUP, -2, "energy", 0}};
1466
1467 int object_convert_size = sizeof(object_convert) / sizeof(tConvertObject);
1468
1469 int chunk_start, chunk_size, filelen;
1470
1471 int CountDataToPageIn();
1472
1473 // Find a valid, usable ID of the specified type
1474 int FindValidID(int type) {
1475 int id = -1;
1476
1477 switch (type) {
1478 case OBJ_ROBOT:
1479 case OBJ_POWERUP:
1480 case OBJ_BUILDING:
1481 case OBJ_CLUTTER:
1482 id = GetObjectID(type);
1483 break;
1484
1485 case OBJ_DOOR: {
1486 int i;
1487
1488 for (i = 0; i < MAX_DOORS; i++)
1489 if (Doors[i].used) {
1490 id = i;
1491 break;
1492 }
1493 ASSERT(i < MAX_DOORS);
1494 break;
1495 }
1496
1497 default:
1498 Int3();
1499 }
1500
1501 return id;
1502 }
1503
1504 // bash/converts generics, from those not allowed, to something that is allowed
1505 void ConvertObject(int *type, int *id) {
1506 // NOTE: Only type/id are valid at this point
1507 #if ((!defined(OEM)) && (!defined(DEMO)))
1508 // Non-OEM build
1509 return;
1510 #else
1511 #ifdef EDITOR
1512 // Editor OEM?
1513 return;
1514 #else
1515 int new_type = *type, new_id = *id;
1516
1517 bool is_multi = (Game_mode & GM_MULTI) ? true : false;
1518 bool c_multi, c_single, convert;
1519
1520 // bash/convert
1521
1522 // look at even numbered items, they are the object to convert
1523 // odd number items are destination
1524 for (int i = 0; i < object_convert_size; i += 2) {
1525 if (*type == object_convert[i].type) {
1526 // type match, check id's
1527 if (object_convert[i].id == -2) {
1528 object_convert[i].id = FindObjectIDName(object_convert[i].name);
1529 }
1530
1531 ASSERT(object_convert[i].id >= 0);
1532
1533 if (*id == object_convert[i].id) {
1534 convert = false;
1535 c_multi = (object_convert[i].flag & CONV_MULTI) ? true : false;
1536 c_single = (object_convert[i].flag & CONV_SINGLE) ? true : false;
1537
1538 if (is_multi && c_multi)
1539 convert = true;
1540 if ((!is_multi) && c_single)
1541 convert = true;
1542
1543 // convert!
1544 if (convert) {
1545 int convert_to = i + 1;
1546
1547 if (object_convert[convert_to].id == -2) {
1548 object_convert[convert_to].id = FindObjectIDName(object_convert[convert_to].name);
1549 }
1550
1551 ASSERT(object_convert[convert_to].id >= 0);
1552
1553 if (object_convert[convert_to].id >= 0) {
1554 mprintf(0, "LEVELLOAD: Converting: '%s' -> '%s'\n", object_convert[i].name, object_convert[convert_to].name);
1555
1556 new_id = object_convert[convert_to].id;
1557 new_type = object_convert[convert_to].type;
1558 }
1559 }
1560 }
1561 }
1562 }
1563
1564 *type = new_type;
1565 *id = new_id;
1566 #endif
1567 #endif
1568 }
1569
1570 // reads an object
1571 // returns 1 if read ok
1572 int ReadObject(CFILE *ifile, object *objp, int handle, int fileversion) {
1573 int type, id, old_id, i;
1574 int roomnum;
1575 float door_shields;
1576 char tempname[OBJ_NAME_LEN + 1] = "";
1577
1578 type = cf_ReadByte(ifile);
1579
1580 if (fileversion >= 34)
1581 id = cf_ReadShort(ifile);
1582 else
1583 id = (uint8_t)cf_ReadByte(ifile);
1584
1585 // Translate id
1586 old_id = id;
1587 switch (type) {
1588 case OBJ_ROBOT:
1589 case OBJ_POWERUP:
1590 case OBJ_BUILDING:
1591 case OBJ_CLUTTER:
1592 id = generic_xlate[id];
1593 break;
1594
1595 case OBJ_DOOR:
1596 id = door_xlate[id];
1597 break;
1598 }
1599 // Check for object not found
1600 if (id == -1) {
1601 id = FindValidID(type); // find any valid id
1602
1603 ASSERT(id != -1);
1604
1605 #if (defined(EDITOR) || defined(NEWEDITOR))
1606 if (GetFunctionMode() == EDITOR_MODE) {
1607 char *old_name = GetFailedXLateItemName((type == OBJ_DOOR) ? XT_DOOR : XT_GENERIC, old_id);
1608 char *new_name = (type == OBJ_DOOR) ? Doors[id].name : Object_info[id].name;
1609 OutrageMessageBox("Object %d (type %s) has undefined ID %d, \"%s\".\n\n"
1610 "This object will be converted to ID %d, \"%s\".",
1611 objp - Objects, Object_type_names[type], old_id, old_name, id, new_name);
1612 }
1613 #endif
1614 }
1615
1616 // Convert un-allowed OEM objects
1617 if (IS_GENERIC(type)) {
1618 ConvertObject(&type, &id);
1619 }
1620
1621 // Read the object's name
1622 if (fileversion >= 95) {
1623 cf_ReadString(tempname, sizeof(tempname), ifile);
1624 if (strlen(tempname)) {
1625 if (type == OBJ_PLAYER) {
1626 #if (defined(EDITOR) || defined(NEWEDITOR))
1627 if (GetFunctionMode() == EDITOR_MODE)
1628 OutrageMessageBox("Object %d, Player %d has a name (\"%s\") which has been deleted.", objp - Objects, id,
1629 tempname);
1630 #endif
1631 tempname[0] = 0; // kill name
1632 }
1633 }
1634 }
1635
1636 // Read the flags
1637 uint32_t flags;
1638 if (fileversion >= 101)
1639 flags = cf_ReadInt(ifile);
1640 else
1641 flags = (uint16_t)cf_ReadShort(ifile);
1642
1643 // Make sure no objects except viewers have the outside mine flags set
1644 ASSERT((type == OBJ_VIEWER) || !(flags & OF_OUTSIDE_MINE));
1645
1646 // If a door, read the shields
1647 if ((type == OBJ_DOOR) && (fileversion >= 109))
1648 door_shields = cf_ReadShort(ifile);
1649
1650 // Get the room number
1651 roomnum = cf_ReadInt(ifile);
1652
1653 // For old files, check if this object is on terrain
1654 if (fileversion < 49) {
1655 if (flags & OLD_OBJECT_OVER_TERRAIN_FLAG) {
1656 flags &= ~OLD_OBJECT_OVER_TERRAIN_FLAG;
1657 roomnum = MAKE_ROOMNUM(roomnum);
1658 }
1659 }
1660
1661 // Get the position
1662 vector pos;
1663 cf_ReadVector(ifile, &pos);
1664
1665 // Initialize the object
1666 ObjInit(objp, type, id, handle, &pos, 0.0);
1667
1668 // Set the stuff we've already read in
1669 objp->flags |= (flags & OBJECT_SAVE_LOAD_FLAGS);
1670 objp->roomnum = roomnum;
1671
1672 // Set shields if this is a door
1673 if ((type == OBJ_DOOR) && (fileversion >= 109))
1674 objp->shields = door_shields;
1675
1676 // Do some wacky weapon thing
1677 if (fileversion <= 36) {
1678 if (objp->type == OBJ_PLAYER || objp->type == OBJ_POWERUP || objp->type == OBJ_ROBOT ||
1679 objp->type == OBJ_BUILDING || objp->type == OBJ_DOOR || objp->type == OBJ_DEBRIS)
1680 objp->flags |= OF_POLYGON_OBJECT;
1681 if (objp->type == OBJ_WEAPON) {
1682 if (!((Weapons[objp->id].flags & WF_IMAGE_BITMAP) || (Weapons[objp->id].flags & WF_IMAGE_VCLIP)))
1683 objp->flags |= OF_POLYGON_OBJECT;
1684 }
1685 }
1686
1687 // Load and set the orientation
1688 matrix orient;
1689 cf_ReadMatrix(ifile, &orient);
1690 vm_Orthogonalize(&orient);
1691 ObjSetOrient(objp, &orient);
1692 ObjSetAABB(objp);
1693
1694 // Set the name
1695 if (tempname[0]) {
1696 objp->name = (char *)mem_malloc(strlen(tempname) + 1);
1697 strcpy(objp->name, tempname);
1698 }
1699
1700 // Update checksum
1701 AppendToLevelChecksum(objp->pos.x);
1702 AppendToLevelChecksum(objp->pos.y);
1703 AppendToLevelChecksum(objp->pos.z);
1704
1705 objp->contains_type = cf_ReadByte(ifile);
1706 objp->contains_id = cf_ReadByte(ifile);
1707 objp->contains_count = cf_ReadByte(ifile);
1708
1709 objp->lifeleft = cf_ReadFloat(ifile);
1710
1711 if ((fileversion >= 65) && (fileversion < 111))
1712 cf_ReadInt(ifile); // was parent_handle
1713
1714 // Read sound info if this is a soundsource object
1715 if (objp->control_type == CT_SOUNDSOURCE) {
1716 ASSERT(objp->type == OBJ_SOUNDSOURCE);
1717 if (fileversion < 119)
1718 objp->ctype.soundsource_info.sound_index = cf_ReadInt(ifile);
1719 else {
1720 char soundname[PAGENAME_LEN];
1721 cf_ReadString(soundname, sizeof(soundname), ifile);
1722 objp->ctype.soundsource_info.sound_index = soundname[0] ? FindSoundName(IGNORE_TABLE(soundname)) : -1;
1723 }
1724 objp->ctype.soundsource_info.volume = cf_ReadFloat(ifile);
1725 }
1726
1727 if (fileversion >= 105) {
1728 // read in default script override information
1729 i = cf_ReadByte(ifile);
1730 if (i > 0) {
1731 objp->custom_default_script_name = (char *)mem_malloc(i + 1);
1732 cf_ReadBytes((uint8_t *)objp->custom_default_script_name, i, ifile);
1733 objp->custom_default_script_name[i] = '\0';
1734 } else {
1735 objp->custom_default_script_name = NULL;
1736 }
1737
1738 i = cf_ReadByte(ifile);
1739 if (i > 0) {
1740 objp->custom_default_module_name = (char *)mem_malloc(i + 1);
1741 cf_ReadBytes((uint8_t *)objp->custom_default_module_name, i, ifile);
1742 objp->custom_default_module_name[i] = '\0';
1743 } else {
1744 objp->custom_default_module_name = NULL;
1745 }
1746 } else {
1747 objp->custom_default_module_name = NULL;
1748 objp->custom_default_script_name = NULL;
1749 }
1750
1751 // load in obsolete script info
1752 if (fileversion < LEVEL_FILE_OSIRIS1DEAD) {
1753 if (fileversion < LEVEL_FILE_SCRIPTNAMES) {
1754 cf_ReadInt(ifile);
1755 } else {
1756 char name[MAX_D3XID_NAME];
1757 cf_ReadString(name, MAX_D3XID_NAME, ifile);
1758 }
1759
1760 if (fileversion >= LEVEL_FILE_SCRIPTPARMS) {
1761 int16_t s = cf_ReadShort(ifile);
1762
1763 for (i = 0; i < s; i++) {
1764 int8_t stype = cf_ReadByte(ifile);
1765 if (stype == PARMTYPE_NUMBER || stype == PARMTYPE_REF) {
1766 cf_ReadFloat(ifile);
1767 } else if (stype == PARMTYPE_VECTOR) {
1768 cf_ReadFloat(ifile);
1769 cf_ReadFloat(ifile);
1770 cf_ReadFloat(ifile);
1771 } else {
1772 Int3(); // -get samir.
1773 }
1774 }
1775 }
1776
1777 if (fileversion >= LEVEL_FILE_SCRIPTCHECK) {
1778 cf_ReadByte(ifile);
1779 }
1780 }
1781
1782 // lightmap data.
1783 int lmdata;
1784 if (fileversion <= 34)
1785 lmdata = 0;
1786 else
1787 lmdata = cf_ReadByte(ifile);
1788
1789 if (lmdata) // lightmap data follows
1790 {
1791 poly_model *pm = &Poly_models[objp->rtype.pobj_info.model_num];
1792 int num_models, num_faces, num_verts;
1793 int i, t, k;
1794 int model_changed = 0, clear_lightmaps = 0;
1795
1796 num_models = cf_ReadByte(ifile);
1797 if (pm->n_models != num_models) {
1798 model_changed = 1;
1799 mprintf(0, "Polymodel %s has changed since this level was lit!\n", pm->name);
1800 }
1801
1802 if (!model_changed)
1803 SetupObjectLightmapMemory(objp);
1804
1805 for (i = 0; i < num_models; i++) {
1806 int submodel_changed = 0;
1807 num_faces = cf_ReadShort(ifile);
1808
1809 if (num_faces != objp->lm_object.num_faces[i]) {
1810 submodel_changed = 1;
1811 clear_lightmaps = 1;
1812 }
1813
1814 for (t = 0; t < num_faces; t++) {
1815 if (submodel_changed == 0 && model_changed == 0) {
1816 lightmap_object_face *fp = &objp->lm_object.lightmap_faces[i][t];
1817 fp->lmi_handle = LightmapInfoRemap[(uint16_t)cf_ReadShort(ifile)];
1818
1819 if (!Dedicated_server)
1820 LightmapInfo[fp->lmi_handle].used++;
1821 else
1822 clear_lightmaps = 1;
1823
1824 if (fileversion <= 88) {
1825 cf_ReadByte(ifile);
1826 cf_ReadByte(ifile);
1827 cf_ReadByte(ifile);
1828 cf_ReadByte(ifile);
1829 }
1830
1831 if (fileversion >= 58) {
1832 // Read normal,uvec,rvec
1833 if (fileversion <= 59) {
1834 vector tvec;
1835 cf_ReadVector(ifile, &tvec);
1836 }
1837 cf_ReadVector(ifile, &fp->rvec);
1838 cf_ReadVector(ifile, &fp->uvec);
1839 } else {
1840 vm_MakeZero(&fp->rvec);
1841 vm_MakeZero(&fp->uvec);
1842 fp->rvec.x = 1;
1843 fp->uvec.y = 1;
1844 }
1845
1846 num_verts = cf_ReadByte(ifile);
1847 if (num_verts == fp->num_verts) {
1848 for (k = 0; k < num_verts; k++) {
1849 fp->u2[k] = cf_ReadFloat(ifile);
1850 fp->v2[k] = cf_ReadFloat(ifile);
1851 }
1852 } else {
1853 for (k = 0; k < num_verts; k++) {
1854 cf_ReadFloat(ifile);
1855 cf_ReadFloat(ifile);
1856 }
1857 }
1858 } else {
1859 cf_ReadShort(ifile);
1860 if (fileversion <= 88) {
1861 cf_ReadByte(ifile);
1862 cf_ReadByte(ifile);
1863 cf_ReadByte(ifile);
1864 cf_ReadByte(ifile);
1865 }
1866
1867 if (fileversion >= 58) {
1868 vector tvec;
1869
1870 // Read normal,uvec,rvec
1871 if (fileversion <= 59) {
1872 cf_ReadVector(ifile, &tvec);
1873 }
1874 cf_ReadVector(ifile, &tvec);
1875 cf_ReadVector(ifile, &tvec);
1876 }
1877
1878 num_verts = cf_ReadByte(ifile);
1879 for (k = 0; k < num_verts; k++) {
1880 cf_ReadFloat(ifile);
1881 cf_ReadFloat(ifile);
1882 }
1883 }
1884 }
1885 }
1886
1887 if (clear_lightmaps) {
1888 mprintf(0, "Freeing lightmaps because model %s has changed since this level was saved!\n", pm->name);
1889 ClearObjectLightmaps(objp);
1890 }
1891 }
1892 return 1;
1893 }
1894
1895 // Old portal trigger flag
1896 #define OLD_TF_PORTAL 1
1897
1898 // Reads a trigger
1899 // Returns: true if read ok, else false
1900 int ReadTrigger(CFILE *ifile, trigger *tp, int fileversion) {
1901 if (fileversion >= 99)
1902 cf_ReadString(tp->name, sizeof(tp->name), ifile);
1903 else
1904 tp->name[0] = 0;
1905
1906 tp->roomnum = cf_ReadShort(ifile);
1907 tp->facenum = cf_ReadShort(ifile);
1908 tp->flags = cf_ReadShort(ifile);
1909 tp->activator = cf_ReadShort(ifile);
1910
1911 // Kill old portal trigger flag
1912 if (fileversion < 103) {
1913 if (tp->flags & OLD_TF_PORTAL) {
1914 tp->facenum = Rooms[tp->roomnum].portals[tp->facenum].portal_face;
1915 tp->flags &= ~OLD_TF_PORTAL;
1916 }
1917 }
1918
1919 ASSERT(Rooms[tp->roomnum].faces[tp->facenum].flags & FF_HAS_TRIGGER);
1920
1921 if (fileversion < LEVEL_FILE_OSIRIS1DEAD) {
1922 if (fileversion < LEVEL_FILE_SCRIPTNAMES)
1923 cf_ReadInt(ifile);
1924 else {
1925 char name[MAX_D3XID_NAME];
1926 cf_ReadString(name, MAX_D3XID_NAME, ifile);
1927 if (fileversion >= LEVEL_FILE_TRIGPARMS) {
1928 int i;
1929 int16_t s = cf_ReadShort(ifile);
1930 for (i = 0; i < s; i++) {
1931 int8_t type = cf_ReadByte(ifile);
1932 if (type == PARMTYPE_NUMBER || type == PARMTYPE_REF) {
1933 cf_ReadFloat(ifile);
1934 } else if (type == PARMTYPE_VECTOR) {
1935 cf_ReadFloat(ifile);
1936 cf_ReadFloat(ifile);
1937 cf_ReadFloat(ifile);
1938 } else {
1939 Int3(); // -get samir.
1940 }
1941 }
1942 }
1943 }
1944
1945 if (fileversion >= LEVEL_FILE_SCRIPTCHECK) {
1946 cf_ReadByte(ifile);
1947 }
1948 }
1949
1950 return 1;
1951 }
1952
1953 // Old portal trigger face flag
1954 #define OLD_FF_PORTAL_TRIG 0x0020
1955
1956 // Reads a face from a disk file
1957 // Parameters: ifile - file to read from
1958 // fp - face to read
1959 // version - the version number of the file being read
1960 // Returns: 1 if read ok, else 0
1961 int ReadFace(CFILE *ifile, face *fp, int version) {
1962 int nverts, i;
1963
1964 // Get number of verts
1965 nverts = cf_ReadByte(ifile);
1966
1967 // Initialize & alloc memory
1968 InitRoomFace(fp, nverts);
1969
1970 // Read vertices
1971 for (i = 0; i < fp->num_verts; i++)
1972 fp->face_verts[i] = cf_ReadShort(ifile);
1973
1974 // Read uvls, and adjust alpha settings
1975 int alphaed = 0;
1976
1977 for (i = 0; i < fp->num_verts; i++) {
1978 fp->face_uvls[i].u = cf_ReadFloat(ifile);
1979 fp->face_uvls[i].v = cf_ReadFloat(ifile);
1980
1981 if (version < 56) {
1982 // Read old lrgb stuff
1983 cf_ReadFloat(ifile);
1984 cf_ReadFloat(ifile);
1985 cf_ReadFloat(ifile);
1986 cf_ReadFloat(ifile);
1987 }
1988
1989 if (version >= 21) {
1990 if (version < 61)
1991 fp->face_uvls[i].alpha = Float_to_ubyte(cf_ReadFloat(ifile));
1992 else
1993 fp->face_uvls[i].alpha = cf_ReadByte(ifile);
1994 } else
1995 fp->face_uvls[i].alpha = 1.0;
1996
1997 if (fp->face_uvls[i].alpha != 255)
1998 alphaed = 1;
1999 }
2000
2001 // Read flags
2002 if (version < 27)
2003 fp->flags = cf_ReadByte(ifile);
2004 else
2005 fp->flags = cf_ReadShort(ifile);
2006
2007 // Kill old portal trigger flag
2008 if (version < 103)
2009 fp->flags &= ~OLD_FF_PORTAL_TRIG;
2010
2011 // Set vertex alpha flag
2012 if (alphaed)
2013 fp->flags |= FF_VERTEX_ALPHA;
2014 else
2015 fp->flags &= ~FF_VERTEX_ALPHA;
2016
2017 // Read the portal number
2018 if (version >= 23)
2019 fp->portal_num = cf_ReadByte(ifile);
2020 else
2021 fp->portal_num = cf_ReadShort(ifile);
2022
2023 // Read and translate the texture number
2024 fp->tmap = texture_xlate[cf_ReadShort(ifile)];
2025
2026 // Check for failed xlate
2027 if (fp->tmap == -1) {
2028 fp->tmap = 0;
2029 }
2030
2031 // Check to see if there is a lightmap
2032 if ((fp->flags & FF_LIGHTMAP) && (version >= 19)) {
2033 if (version <= 29) {
2034 uint8_t w, h;
2035
2036 w = cf_ReadByte(ifile);
2037 h = cf_ReadByte(ifile);
2038
2039 for (i = 0; i < w * h; i++)
2040 cf_ReadShort(ifile);
2041
2042 fp->flags &= ~FF_LIGHTMAP;
2043 } else {
2044 // Read lightmap info handle
2045 int lmi_handle = (uint16_t)cf_ReadShort(ifile);
2046 if (!Dedicated_server) {
2047 fp->lmi_handle = LightmapInfoRemap[lmi_handle];
2048 LightmapInfo[fp->lmi_handle].used++;
2049 }
2050
2051 if (version <= 88) {
2052 cf_ReadByte(ifile);
2053 cf_ReadByte(ifile);
2054 cf_ReadByte(ifile);
2055 cf_ReadByte(ifile);
2056 }
2057 }
2058
2059 // Read UV2s
2060 for (i = 0; i < fp->num_verts; i++) {
2061 fp->face_uvls[i].u2 = cf_ReadFloat(ifile);
2062 fp->face_uvls[i].v2 = cf_ReadFloat(ifile);
2063
2064 // Stupid fix for bad lightmap uvs
2065 if (fp->face_uvls[i].u2 < 0)
2066 fp->face_uvls[i].u2 = 0;
2067 if (fp->face_uvls[i].u2 > 1)
2068 fp->face_uvls[i].u2 = 1.0;
2069
2070 if (fp->face_uvls[i].v2 < 0)
2071 fp->face_uvls[i].v2 = 0;
2072 if (fp->face_uvls[i].v2 > 1)
2073 fp->face_uvls[i].v2 = 1.0;
2074 }
2075 }
2076
2077 if (version >= 22 && version <= 29) {
2078 vector vec;
2079 cf_ReadVector(ifile, &vec);
2080 }
2081
2082 if (version >= 40 && version <= 60) // was shadow room,face
2083 {
2084 cf_ReadShort(ifile);
2085 cf_ReadShort(ifile);
2086 }
2087
2088 if (version >= 50) {
2089 fp->light_multiple = cf_ReadByte(ifile);
2090
2091 if (fp->light_multiple == 186)
2092 fp->light_multiple =
2093 4; // Get Jason, I'm looking for this bug! Its safe to go past it, but I'm just on the lookout
2094
2095 if (version <= 52) {
2096 if (fp->light_multiple >= 32)
2097 Int3(); // Get Jason
2098 fp->light_multiple *= 4;
2099 }
2100 } else
2101 fp->light_multiple = 4;
2102
2103 if (version >= 71) {
2104 uint8_t special = cf_ReadByte(ifile);
2105 if (special) {
2106 if (version < 77) // Ignore old specular data
2107 {
2108 vector center;
2109 cf_ReadByte(ifile);
2110 cf_ReadVector(ifile, &center);
2111 cf_ReadShort(ifile);
2112 } else {
2113 vector center;
2114
2115 uint8_t smooth = 0;
2116 uint8_t num_smooth_verts = 0;
2117 uint8_t type = cf_ReadByte(ifile);
2118 uint8_t num = cf_ReadByte(ifile);
2119
2120 if (version >= 117) {
2121 // Read if smoothed
2122 smooth = cf_ReadByte(ifile);
2123 if (smooth) {
2124 num_smooth_verts = cf_ReadByte(ifile);
2125 fp->special_handle = AllocSpecialFace(type, num, true, num_smooth_verts);
2126 } else
2127 fp->special_handle = AllocSpecialFace(type, num);
2128
2129 } else
2130 fp->special_handle = AllocSpecialFace(type, num);
2131
2132 ASSERT(fp->special_handle != BAD_SPECIAL_FACE_INDEX);
2133
2134 for (i = 0; i < num; i++) {
2135 cf_ReadVector(ifile, &center);
2136 uint16_t color = cf_ReadShort(ifile);
2137
2138 SpecialFaces[fp->special_handle].spec_instance[i].bright_center = center;
2139 SpecialFaces[fp->special_handle].spec_instance[i].bright_color = color;
2140 }
2141
2142 if (smooth) {
2143 for (i = 0; i < num_smooth_verts; i++) {
2144 vector vertnorm;
2145 cf_ReadVector(ifile, &vertnorm);
2146 SpecialFaces[fp->special_handle].vertnorms[i] = vertnorm;
2147 }
2148 }
2149 }
2150 }
2151 }
2152
2153 return 1;
2154 }
2155
2156 // Old has trigger portal flag
2157 #define OLD_PF_HAS_TRIGGER 4
2158
2159 // Reads a portal to a disk file
2160 // Parameters: ifile - file to Read to
2161 // pp - portal to Read
2162 // Returns: 1 if written ok, else 0
2163 int ReadPortal(CFILE *ifile, portal *pp, int version) {
2164 int i, num_faces;
2165
2166 pp->flags = cf_ReadInt(ifile);
2167
2168 // Kill old trigger flag
2169 if (version < 103)
2170 pp->flags &= ~OLD_PF_HAS_TRIGGER;
2171
2172 if (version < 80) { // read old list of portal verts
2173 int num_verts = cf_ReadShort(ifile);
2174 for (i = 0; i < num_verts; i++)
2175 cf_ReadShort(ifile); // was pp->portal_verts[i]
2176
2177 num_faces = cf_ReadShort(ifile);
2178 ASSERT(num_faces == 1);
2179 }
2180
2181 pp->portal_face = cf_ReadShort(ifile);
2182
2183 pp->croom = cf_ReadInt(ifile);
2184 pp->cportal = cf_ReadInt(ifile);
2185
2186 if (version >= 123)
2187 pp->bnode_index = cf_ReadShort(ifile);
2188 else
2189 pp->bnode_index = -1;
2190
2191 if (version >= 63) {
2192 cf_ReadVector(ifile, &pp->path_pnt);
2193 }
2194
2195 if (version >= 100)
2196 pp->combine_master = cf_ReadInt(ifile);
2197
2198 return 1;
2199 }
2200
2201 int n_degenerate_faces_removed;
2202
2203 // Remove faces that have no area, as determined by their surface normal being NULL
2204 // This code should come out after the clipper is fixed to not create degenerate faces
2205 #ifdef EDITOR
2206 void RemoveDegenerateFaces(room *rp) {
2207 int f;
2208 int n_removed = 0;
2209
2210 for (f = rp->num_faces - 1; f >= 0; f--) {
2211 face *fp = &rp->faces[f];
2212
2213 if ((fp->normal.x == 0.0) && (fp->normal.y == 0.0) && (fp->normal.z == 0.0)) {
2214 mprintf(0, "Deleting face %d from room %d\n", f, ROOMNUM(rp));
2215 DeleteRoomFace(rp, f);
2216 n_degenerate_faces_removed++;
2217 }
2218 }
2219 }
2220 #endif
2221
2222 #define NO_COMPRESS 0
2223 #define COMPRESS 1
2224
2225 // Does a RLE compression run of the values given the byte array 'val'.
2226 int WriteCompressionByte(CFILE *fp, uint8_t *val, int total, int just_count, int compress) {
2227 int done = 0;
2228 int written = 0;
2229 int curptr = 0;
2230
2231 ASSERT(!(just_count == 1 && compress == 0));
2232
2233 if (compress == NO_COMPRESS) {
2234 int i;
2235
2236 // Write 0 to indicate no compression
2237 cf_WriteByte(fp, NO_COMPRESS);
2238 for (i = 0; i < total; i++)
2239 cf_WriteByte(fp, val[i]);
2240
2241 return total;
2242 } else {
2243 // Indicate compression
2244 if (!just_count)
2245 cf_WriteByte(fp, COMPRESS);
2246 }
2247
2248 while (!done) {
2249 if (curptr == total) {
2250 done = 1;
2251 continue;
2252 }
2253
2254 ASSERT(curptr < total);
2255
2256 uint8_t curval = val[curptr];
2257 uint8_t count = 1;
2258
2259 while ((curptr + count) < total && val[curptr + count] == curval && count < 250)
2260 count++;
2261
2262 written += 2;
2263
2264 if (just_count) {
2265 curptr += count;
2266 continue;
2267 }
2268
2269 if (count == 1) {
2270 cf_WriteByte(fp, 0);
2271 cf_WriteByte(fp, curval);
2272 } else {
2273 cf_WriteByte(fp, count);
2274 cf_WriteByte(fp, curval);
2275 }
2276
2277 curptr += count;
2278 }
2279
2280 return written;
2281 }
2282
2283 // Does a RLE compression run of the values given the int16_t array 'val'.
2284 // If just_count is 1, doesn't write anything
2285 int WriteCompressionShort(CFILE *fp, uint16_t *val, int total, int just_count, int compress) {
2286 int done = 0, written = 0;
2287
2288 int curptr = 0;
2289
2290 ASSERT(!(just_count == 1 && compress == 0));
2291
2292 if (compress == NO_COMPRESS) {
2293 int i;
2294
2295 // Write 0 to indicate no compression
2296 cf_WriteByte(fp, NO_COMPRESS);
2297
2298 for (i = 0; i < total; i++)
2299 cf_WriteShort(fp, val[i]);
2300
2301 return total;
2302 } else {
2303 // Indicate compression
2304 if (!just_count)
2305 cf_WriteByte(fp, COMPRESS);
2306 }
2307
2308 while (!done) {
2309 if (curptr == total) {
2310 done = 1;
2311 continue;
2312 }
2313
2314 ASSERT(curptr < total);
2315
2316 uint16_t curval = val[curptr];
2317 uint8_t count = 1;
2318
2319 while ((curptr + count) < total && val[curptr + count] == curval && count < 250)
2320 count++;
2321
2322 written += 3;
2323
2324 if (just_count) {
2325 curptr += count;
2326 continue;
2327 }
2328
2329 if (count == 1) {
2330 cf_WriteByte(fp, 0);
2331 cf_WriteShort(fp, curval);
2332 } else {
2333 cf_WriteByte(fp, count);
2334 cf_WriteShort(fp, curval);
2335 }
2336
2337 curptr += count;
2338 }
2339
2340 return written;
2341 }
2342
2343 // Given an array of values, checks to see if it would be better to write it out
2344 // as a raw array or RLE array
2345 void CheckToWriteCompressByte(CFILE *fp, uint8_t *vals, int total) {
2346 int count = WriteCompressionByte(fp, vals, total, 1, COMPRESS);
2347
2348 if (count >= total)
2349 WriteCompressionByte(fp, vals, total, 0, NO_COMPRESS);
2350 else
2351 WriteCompressionByte(fp, vals, total, 0, COMPRESS);
2352 }
2353
2354 // Given an array of values, checks to see if it would be better to write it out
2355 // as a raw array or RLE array
2356 void CheckToWriteCompressShort(CFILE *fp, uint16_t *vals, int total) {
2357 int count = WriteCompressionShort(fp, vals, total, 1, COMPRESS);
2358
2359 if (count >= total)
2360 WriteCompressionShort(fp, vals, total, 0, NO_COMPRESS);
2361 else
2362 WriteCompressionShort(fp, vals, total, 0, COMPRESS);
2363 }
2364
2365 void ReadCompressionByte(CFILE *fp, uint8_t *vals, int total) {
2366 int count = 0;
2367 uint8_t compressed = cf_ReadByte(fp);
2368
2369 if (compressed == 0) {
2370 for (int i = 0; i < total; i++)
2371 vals[i] = cf_ReadByte(fp);
2372
2373 return;
2374 }
2375
2376 while (count != total) {
2377 ASSERT(count < total);
2378 uint8_t command = cf_ReadByte(fp);
2379
2380 if (command == 0) // next byte is raw
2381 {
2382 uint8_t height = cf_ReadByte(fp);
2383
2384 vals[count] = height;
2385 count++;
2386 } else if (command >= 2 && command <= 250) // next pixel is run of pixels
2387 {
2388 uint8_t height = cf_ReadByte(fp);
2389 for (int k = 0; k < command; k++) {
2390 vals[count] = height;
2391 count++;
2392 }
2393 } else
2394 Int3(); // bad compression run
2395 }
2396 }
2397
2398 void ReadCompressionShort(CFILE *fp, uint16_t *vals, int total) {
2399 int count = 0;
2400
2401 uint8_t compressed = cf_ReadByte(fp);
2402
2403 if (compressed == 0) {
2404 for (int i = 0; i < total; i++)
2405 vals[i] = cf_ReadShort(fp);
2406
2407 return;
2408 }
2409
2410 while (count != total) {
2411 ASSERT(count < total);
2412 uint8_t command = cf_ReadByte(fp);
2413
2414 if (command == 0) // next byte is raw
2415 {
2416 uint16_t height = cf_ReadShort(fp);
2417
2418 vals[count] = height;
2419 count++;
2420 } else if (command >= 2 && command <= 250) // next pixel is run of pixels
2421 {
2422 uint16_t height = cf_ReadShort(fp);
2423 for (int k = 0; k < command; k++) {
2424 vals[count] = height;
2425 count++;
2426 }
2427 } else
2428 Int3(); // bad compression run
2429 }
2430 }
2431
2432 // Reads a room from a disk file
2433 // Parameters: ifile - file to read from
2434 // rp - room to read
2435 // version - version number of file
2436 // Returns: 1 if read ok, else 0
2437 int ReadRoom(CFILE *ifile, room *rp, int version) {
2438 int i, nverts, nfaces, nportals;
2439
2440 ASSERT(rp->used == 0); // should be free
2441
2442 // Get counts for room elements
2443 nverts = cf_ReadInt(ifile);
2444 nfaces = cf_ReadInt(ifile);
2445 nportals = cf_ReadInt(ifile);
2446
2447 // Initialize the room
2448 InitRoom(rp, nverts, nfaces, nportals);
2449
2450 // Get room name
2451 rp->name = NULL;
2452 if (version >= 96) {
2453 char tempname[ROOM_NAME_LEN + 1];
2454 cf_ReadString(tempname, sizeof(tempname), ifile);
2455 if (strlen(tempname)) {
2456 rp->name = (char *)mem_malloc(strlen(tempname) + 1);
2457 strcpy(rp->name, tempname);
2458 }
2459 }
2460
2461 // Read path point
2462 if (version >= 63) {
2463 cf_ReadVector(ifile, &rp->path_pnt);
2464 }
2465
2466 // Read in verts
2467 for (i = 0; i < rp->num_verts; i++) {
2468 cf_ReadVector(ifile, &rp->verts[i]);
2469 if (version >= 52 && version <= 67) {
2470 cf_ReadByte(ifile);
2471 cf_ReadByte(ifile);
2472 } else if (version >= 68 && version < 71) {
2473 vector tempv;
2474 cf_ReadVector(ifile, &tempv);
2475 cf_ReadShort(ifile);
2476 }
2477
2478 // Update checksum
2479 AppendToLevelChecksum(rp->verts[i]);
2480 }
2481
2482 // Read in faces
2483 for (i = 0; i < rp->num_faces; i++) {
2484 bool t;
2485
2486 // Load the face
2487 ReadFace(ifile, &rp->faces[i], version);
2488
2489 for (int k = 0; k < rp->faces[i].num_verts; k++) {
2490 AppendToLevelChecksum(rp->faces[i].face_verts[k]);
2491 }
2492 AppendToLevelChecksum(rp->faces[i].tmap);
2493
2494 // Get the surface normal for this face
2495 // Note we're not computing the checksum on the normal because of
2496 // differences between cpu architectures having different internal precision
2497 // models for floating point. Instead we took the face verts into account above.
2498 t = ComputeFaceNormal(rp, i);
2499
2500 // Check for bad normal
2501 if (!t) {
2502 mprintf(1, "WARNING: Room %d face %d has bad normal!\n", rp - Rooms, i);
2503 }
2504 }
2505
2506 // Read in portals
2507 for (i = 0; i < rp->num_portals; i++)
2508 ReadPortal(ifile, &rp->portals[i], version);
2509
2510 // Read rest of fields
2511 rp->flags = cf_ReadInt(ifile);
2512 if (version < 29)
2513 cf_ReadFloat(ifile); // was rp->static_light
2514
2515 if (version >= 68) {
2516 rp->pulse_time = cf_ReadByte(ifile);
2517 rp->pulse_offset = cf_ReadByte(ifile);
2518 }
2519
2520 if (version >= 79) {
2521 rp->mirror_face = cf_ReadShort(ifile);
2522 } else
2523 rp->mirror_face = -1;
2524
2525 if ((rp->flags & RF_DOOR)) {
2526 int doornum = 0, flags = 0, keys = 0, position = 0.0;
2527
2528 if (version >= 28 && version <= 32) {
2529 doornum = door_xlate[cf_ReadInt(ifile)];
2530 cf_ReadFloat(ifile); // was open_time
2531 cf_ReadFloat(ifile); // was wait_time
2532 cf_ReadFloat(ifile); // was close_time
2533 } else if (version >= 33) {
2534 if (version < 106)
2535 cf_ReadInt(ifile); // was doorway_num
2536
2537 flags = cf_ReadByte(ifile);
2538 if (version < 106)
2539 flags |= DF_AUTO;
2540
2541 if (version >= 36)
2542 keys = cf_ReadByte(ifile);
2543
2544 doornum = door_xlate[cf_ReadInt(ifile)];
2545
2546 if (version >= 106)
2547 position = cf_ReadFloat(ifile);
2548
2549 if (version < 106) {
2550 cf_ReadFloat(ifile); // was open_time
2551 cf_ReadFloat(ifile); // was wait_time
2552 cf_ReadFloat(ifile); // was close_time
2553 }
2554 }
2555
2556 if (doornum == -1)
2557 doornum = FindValidID(OBJ_DOOR); // find any valid id
2558
2559 ASSERT(doornum != -1);
2560 doorway *dp = DoorwayAdd(rp, doornum);
2561 dp->position = position;
2562 dp->flags = flags;
2563 dp->keys_needed = keys;
2564 dp->dest_pos = dp->position;
2565 }
2566
2567 if (version >= 67) {
2568 // Read in volume lights
2569 if (cf_ReadByte(ifile) == 1) {
2570 int w = cf_ReadInt(ifile);
2571 int h = cf_ReadInt(ifile);
2572 int d = cf_ReadInt(ifile);
2573
2574 int size = w * h * d;
2575
2576 if (size) {
2577
2578 rp->volume_lights = (uint8_t *)mem_malloc(size);
2579 ASSERT(rp->volume_lights); // ran out of memory!
2580 } else
2581 rp->volume_lights = NULL;
2582
2583 ReadCompressionByte(ifile, rp->volume_lights, w * h * d);
2584
2585 rp->volume_width = w;
2586 rp->volume_height = h;
2587 rp->volume_depth = d;
2588 }
2589 }
2590
2591 if (version >= 73) {
2592 // Read fog stuff
2593 rp->fog_depth = cf_ReadFloat(ifile);
2594 rp->fog_r = cf_ReadFloat(ifile);
2595 rp->fog_g = cf_ReadFloat(ifile);
2596 rp->fog_b = cf_ReadFloat(ifile);
2597 }
2598
2599 // Read ambient sound pattern name
2600 if (version >= 78) {
2601 char tbuf[PAGENAME_LEN];
2602 cf_ReadString(tbuf, sizeof(tbuf), ifile);
2603 rp->ambient_sound = FindAmbientSoundPattern(tbuf);
2604 } else
2605 rp->ambient_sound = -1;
2606
2607 // read reverb value for room.
2608 rp->env_reverb = (version >= 98) ? ((uint8_t)cf_ReadByte(ifile)) : 0;
2609
2610 // Read damage
2611 if (version >= 108) {
2612 rp->damage = cf_ReadFloat(ifile);
2613 rp->damage_type = cf_ReadByte(ifile);
2614 }
2615
2616 #if (defined(EDITOR) || defined(NEWEDITOR))
2617 // Get rid of bogus faces
2618 // RemoveDegenerateFaces(rp);
2619 #endif
2620
2621 if (Katmai) {
2622 // If katmai, copy all of our verts into our verts4 array
2623 for (i = 0; i < nverts; i++) {
2624 rp->verts4[i].x = rp->verts[i].x;
2625 rp->verts4[i].y = rp->verts[i].y;
2626 rp->verts4[i].z = rp->verts[i].z;
2627 }
2628 }
2629
2630 return 1;
2631 }
2632
2633 // Build a translation table for various items
2634 void BuildXlateTable(CFILE *ifile, int (*lookup_func)(const char *), int16_t *xlate_table, int max_items, int type) {
2635 char name[PAGENAME_LEN];
2636 int n;
2637 int i;
2638
2639 n = cf_ReadInt(ifile); // get num items
2640 for (i = 0; i < n; i++) {
2641
2642 cf_ReadString(name, sizeof(name), ifile); // get old name
2643
2644 if (!name[0]) // if null name, don't look up
2645 xlate_table[i] = -1;
2646 else { // look up, and warn if not found
2647 xlate_table[i] = lookup_func(name); // returns -1 if not found
2648 #if (defined(EDITOR) || defined(NEWEDITOR))
2649 if ((xlate_table[i] == -1) && (type != -1))
2650 AddFailedXLateItem(type, i, name);
2651 #endif
2652 }
2653 }
2654 for (; i < max_items; i++) // clear out rest of array
2655 xlate_table[i] = -1;
2656 }
2657
2658 #define ISCHUNK(name) (!strncmp(chunk_name, name, 4))
2659
2660 // Reads info about our lightmaps
2661 void ReadNewLightmapChunk(CFILE *fp, int version) {
2662 int nummaps;
2663 int i;
2664
2665 // Don't read lightmaps for dedicated server
2666 if (Dedicated_server) {
2667 Num_lightmap_infos_read = 0; // I don't think this will be used, but clear it just in case
2668 return;
2669 }
2670
2671 uint16_t *lightmap_remap = (uint16_t *)mem_malloc(MAX_LIGHTMAPS * sizeof(uint16_t));
2672
2673 nummaps = cf_ReadInt(fp);
2674
2675 mprintf(0, "Reading %d unique lightmaps\n", nummaps);
2676
2677 for (i = 0; i < nummaps; i++) {
2678 int w, h;
2679 if (nummaps >= 5) {
2680 if ((i % (nummaps / 5)) == 0) {
2681 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL,
2682 (filelen) ? (float)(chunk_start + ((((chunk_size) / nummaps) * i) / 2)) / (float)filelen
2683 : 0.0f,
2684 CHUNK_LIGHTMAPS);
2685 }
2686 }
2687 w = cf_ReadShort(fp);
2688 h = cf_ReadShort(fp);
2689
2690 int lm_handle = lm_AllocLightmap(w, h);
2691
2692 lightmap_remap[i] = lm_handle;
2693 uint16_t *data = (uint16_t *)lm_data(lm_handle);
2694 ReadCompressionShort(fp, data, w * h);
2695 }
2696
2697 nummaps = cf_ReadInt(fp);
2698
2699 Num_lightmap_infos_read = nummaps;
2700 ASSERT(nummaps < MAX_LIGHTMAP_INFOS);
2701
2702 for (i = 0; i < nummaps; i++) {
2703 int w, h, lmi;
2704 uint8_t type;
2705 if (nummaps >= 50) {
2706 if ((i % (nummaps / 50)) == 0) {
2707 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL,
2708 (filelen) ? (float)(chunk_start + (chunk_size / 2) + (((chunk_size / 2) / nummaps) * i)) /
2709 (float)filelen
2710 : 0.0f,
2711 CHUNK_LIGHTMAPS);
2712 }
2713 }
2714 int remap_handle = cf_ReadShort(fp);
2715 w = cf_ReadShort(fp);
2716 h = cf_ReadShort(fp);
2717 type = cf_ReadByte(fp);
2718
2719 if (!Dedicated_server) {
2720 lmi = AllocLightmapInfo(w, h, type, false);
2721 ASSERT(lmi != BAD_LMI_INDEX);
2722 LightmapInfoRemap[i] = lmi;
2723 LightmapInfo[lmi].lm_handle = lightmap_remap[remap_handle];
2724 }
2725
2726 if (version >= 91) {
2727 int x1 = cf_ReadShort(fp);
2728 int y1 = cf_ReadShort(fp);
2729
2730 if (!Dedicated_server) {
2731 LightmapInfo[lmi].x1 = x1;
2732 LightmapInfo[lmi].y1 = y1;
2733 }
2734 }
2735
2736 int xspacing;
2737 int yspacing;
2738 vector ul, norm;
2739
2740 xspacing = cf_ReadByte(fp);
2741 yspacing = cf_ReadByte(fp);
2742 cf_ReadVector(fp, &ul);
2743 cf_ReadVector(fp, &norm);
2744
2745 if (!Dedicated_server) {
2746 LightmapInfo[lmi].xspacing = xspacing;
2747 LightmapInfo[lmi].yspacing = yspacing;
2748
2749 LightmapInfo[lmi].upper_left = ul;
2750 LightmapInfo[lmi].normal = norm;
2751 }
2752 }
2753
2754 mem_free(lightmap_remap);
2755 }
2756
2757 // Reads info about our lightmaps
2758 void ReadLightmapChunk(CFILE *fp, int version) {
2759 // Don't read lightmaps for dedicated server
2760 if (Dedicated_server) {
2761 Num_lightmap_infos_read = 0; // I don't think this will be used, but clear it just in case
2762 return;
2763 }
2764
2765 int nummaps;
2766 int i, t;
2767 uint16_t *ded_dummy_data = NULL;
2768
2769 if (Dedicated_server) {
2770 ded_dummy_data = (uint16_t *)mem_malloc(128 * 128 * 2);
2771 ASSERT(ded_dummy_data);
2772 }
2773
2774 nummaps = cf_ReadInt(fp);
2775 Num_lightmap_infos_read = nummaps;
2776 ASSERT(nummaps < MAX_LIGHTMAP_INFOS);
2777
2778 for (i = 0; i < nummaps; i++) {
2779
2780 if ((i % 10) == 0) {
2781 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL,
2782 (filelen) ? (float)chunk_start + (chunk_size / nummaps) / (float)filelen : 0.0f,
2783 CHUNK_LIGHTMAPS);
2784 }
2785 int w, h, lmi;
2786 uint8_t type;
2787
2788 w = cf_ReadInt(fp);
2789 h = cf_ReadInt(fp);
2790 type = cf_ReadByte(fp);
2791
2792 if (!Dedicated_server) {
2793 lmi = AllocLightmapInfo(w, h, type);
2794 ASSERT(lmi != BAD_LMI_INDEX);
2795 LightmapInfoRemap[i] = lmi;
2796 }
2797
2798 if (version <= 38) {
2799 LightmapInfo[lmi].xspacing = (float)cf_ReadByte(fp);
2800 LightmapInfo[lmi].yspacing = (float)cf_ReadByte(fp);
2801 } else if (version < 66) {
2802 LightmapInfo[lmi].xspacing = cf_ReadFloat(fp);
2803 LightmapInfo[lmi].yspacing = cf_ReadFloat(fp);
2804 } else {
2805 int x, y;
2806
2807 x = cf_ReadByte(fp);
2808 y = cf_ReadByte(fp);
2809
2810 if (!Dedicated_server) {
2811 LightmapInfo[lmi].xspacing = x;
2812 LightmapInfo[lmi].yspacing = y;
2813 }
2814 }
2815 vector v;
2816
2817 cf_ReadVector(fp, &v);
2818 if (!Dedicated_server)
2819 LightmapInfo[lmi].upper_left = v;
2820
2821 if (version < 58) // Don't read centers anymore
2822 {
2823 vector tvec;
2824 cf_ReadVector(fp, &tvec); // used to be normal
2825 cf_ReadVector(fp, &tvec); // used to be center
2826 }
2827 if (version >= 59) {
2828 cf_ReadVector(fp, &v);
2829 if (!Dedicated_server)
2830 LightmapInfo[lmi].normal = v;
2831 } else {
2832 LightmapInfo[lmi].normal.x = 0;
2833 LightmapInfo[lmi].normal.y = 0;
2834 LightmapInfo[lmi].normal.z = 1;
2835 }
2836
2837 uint16_t *data;
2838
2839 if (Dedicated_server)
2840 data = ded_dummy_data;
2841 else
2842 data = (uint16_t *)lm_data(LightmapInfo[lmi].lm_handle);
2843
2844 if (version <= 37) {
2845 for (t = 0; t < w * h; t++)
2846 data[t] = cf_ReadShort(fp);
2847 } else {
2848 ReadCompressionShort(fp, data, w * h);
2849 }
2850
2851 // Adjust lightmap data to account for our new 1555 format
2852 if (version < 64) {
2853 for (t = 0; t < w * h; t++) {
2854 uint16_t pixel = data[t];
2855
2856 if (pixel == 0x07e0)
2857 pixel = NEW_TRANSPARENT_COLOR;
2858 else {
2859 int r = ((pixel & 0xF800) >> 11) << 3;
2860 int g = ((pixel & 0x07e0) >> 5) << 2;
2861 int b = (pixel & 0x001f) << 3;
2862
2863 pixel = OPAQUE_FLAG | GR_RGB16(r, g, b);
2864 }
2865
2866 data[t] = pixel;
2867 }
2868 }
2869 }
2870
2871 // Free dummy data for dedicated server
2872 if (Dedicated_server)
2873 mem_free(ded_dummy_data);
2874 }
2875
2876 // Reads information about the terrain mesh
2877 void ReadTerrainHeightChunk(CFILE *fp, int version) {
2878 int i;
2879
2880 if (version <= 31) {
2881 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
2882 Terrain_seg[i].ypos = cf_ReadByte(fp);
2883 } else {
2884 uint8_t *byte_vals = (uint8_t *)mem_malloc(TERRAIN_DEPTH * TERRAIN_WIDTH);
2885 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
2886
2887 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++) {
2888 Terrain_seg[i].ypos = byte_vals[i];
2889 AppendToLevelChecksum(Terrain_seg[i].ypos);
2890 }
2891
2892 mem_free(byte_vals);
2893 }
2894 }
2895
2896 void ReadGamePathsChunk(CFILE *fp, int version) {
2897 // Currently, we will have no version specific info
2898
2899 int i, j;
2900
2901 // Get the number of paths
2902 Num_game_paths = cf_ReadShort(fp);
2903
2904 for (i = 0; i < Num_game_paths; i++) {
2905 GamePaths[i].used = 1;
2906 GamePaths[i].name[0] = 0;
2907 GamePaths[i].pathnodes = (node *)mem_malloc(MAX_NODES_PER_PATH * sizeof(node));
2908 GamePaths[i].flags = 0;
2909
2910 // Read in the path's info
2911 cf_ReadString(GamePaths[i].name, PAGENAME_LEN, fp);
2912 GamePaths[i].num_nodes = cf_ReadInt(fp);
2913 GamePaths[i].flags = cf_ReadByte(fp);
2914
2915 for (j = 0; j < GamePaths[i].num_nodes; j++) {
2916 cf_ReadVector(fp, &GamePaths[i].pathnodes[j].pos);
2917 GamePaths[i].pathnodes[j].roomnum = cf_ReadInt(fp);
2918 GamePaths[i].pathnodes[j].flags = cf_ReadInt(fp);
2919
2920 if (version >= 51) {
2921 cf_ReadVector(fp, &GamePaths[i].pathnodes[j].fvec);
2922 cf_ReadVector(fp, &GamePaths[i].pathnodes[j].uvec);
2923 } else {
2924 GamePaths[i].pathnodes[j].fvec.x = 0.0;
2925 GamePaths[i].pathnodes[j].fvec.y = 0.0;
2926 GamePaths[i].pathnodes[j].fvec.z = 1.0;
2927
2928 GamePaths[i].pathnodes[j].uvec.x = 0.0;
2929 GamePaths[i].pathnodes[j].uvec.y = 1.0;
2930 GamePaths[i].pathnodes[j].uvec.z = 0.0;
2931 }
2932 }
2933 }
2934 }
2935
2936 void ReadOverrideSoundChunk(CFILE *fp, int version) {
2937 char soundname[PAGENAME_LEN];
2938
2939 cf_ReadString(soundname, sizeof(soundname), fp);
2940 if (strcmp(soundname, "") != 0) {
2941 sound_override_force_field = FindSoundName(soundname);
2942 }
2943
2944 cf_ReadString(soundname, sizeof(soundname), fp);
2945 if (strcmp(soundname, "") != 0) {
2946 sound_override_glass_breaking = FindSoundName(soundname);
2947 }
2948 }
2949
2950 void ReadFFTMChunk(CFILE *fp, int version) {
2951 char texturename[PAGENAME_LEN];
2952 int i;
2953
2954 int num_items = MAX_FORCE_FIELD_BOUNCE_TEXTURES;
2955
2956 if (version < 132) {
2957 num_items = 2;
2958 }
2959
2960 for (i = 0; i < num_items; i++) {
2961 cf_ReadString(texturename, sizeof(texturename), fp);
2962
2963 if (strcmp(texturename, "") != 0) {
2964 force_field_bounce_texture[i] = FindTextureName(texturename);
2965 force_field_bounce_multiplier[i] = cf_ReadFloat(fp);
2966 GameTextures[force_field_bounce_texture[i]].flags |= TF_FORCEFIELD;
2967 }
2968 }
2969 }
2970
2971 void ReadBNodeChunk(CFILE *fp, int version) {
2972 int i;
2973 int j;
2974 int k;
2975
2976 int hr_index = cf_ReadShort(fp);
2977 ASSERT(hr_index == Highest_room_index + 8);
2978
2979 for (i = 0; i <= hr_index; i++) {
2980 char f_good_room = cf_ReadByte(fp);
2981 if (f_good_room) {
2982 bn_list *bnlist = BNode_GetBNListPtr(i, true);
2983
2984 bnlist->num_nodes = cf_ReadShort(fp);
2985 ASSERT(!(i <= Highest_room_index && (Rooms[i].flags & RF_EXTERNAL) && bnlist->num_nodes > 0));
2986
2987 if (bnlist->num_nodes) {
2988 bnlist->nodes = (bn_node *)mem_malloc(sizeof(bn_node) * bnlist->num_nodes);
2989 for (j = 0; j < bnlist->num_nodes; j++) {
2990 bnlist->nodes[j].pos.x = cf_ReadFloat(fp);
2991 bnlist->nodes[j].pos.y = cf_ReadFloat(fp);
2992 bnlist->nodes[j].pos.z = cf_ReadFloat(fp);
2993
2994 bnlist->nodes[j].num_edges = cf_ReadShort(fp);
2995 if (bnlist->nodes[j].num_edges) {
2996 bnlist->nodes[j].edges = (bn_edge *)mem_malloc(sizeof(bn_edge) * bnlist->nodes[j].num_edges);
2997 for (k = 0; k < bnlist->nodes[j].num_edges; k++) {
2998 bnlist->nodes[j].edges[k].end_room = cf_ReadShort(fp);
2999 bnlist->nodes[j].edges[k].end_index = cf_ReadByte(fp);
3000 if (version < 125)
3001 cf_ReadByte(fp);
3002
3003 bnlist->nodes[j].edges[k].flags = cf_ReadShort(fp);
3004 bnlist->nodes[j].edges[k].cost = cf_ReadShort(fp);
3005
3006 // Chrisnote -- fixes earlier versions of the bnode system that would allow for zero length edges
3007 if (bnlist->nodes[j].edges[k].cost < 1)
3008 bnlist->nodes[j].edges[k].cost = 1;
3009
3010 bnlist->nodes[j].edges[k].max_rad = cf_ReadFloat(fp);
3011 }
3012 } else {
3013 bnlist->nodes[j].edges = NULL;
3014 }
3015 }
3016 } else {
3017 bnlist->nodes = NULL;
3018 }
3019 }
3020 }
3021
3022 if (version <= 123)
3023 BNode_verified = false;
3024 else
3025 BNode_verified = cf_ReadByte(fp) != 0;
3026
3027 BNode_allocated = true;
3028 }
3029
3030 void ReadBOAChunk(CFILE *fp, int version) {
3031 // Currently, we will have no version specific info
3032
3033 int i, j;
3034 int max_rooms;
3035 int max_path_portals;
3036
3037 // Get the number of paths
3038 BOA_AABB_checksum = BOA_mine_checksum = cf_ReadInt(fp);
3039
3040 if (version >= 76)
3041 BOA_vis_checksum = cf_ReadInt(fp);
3042 else
3043 BOA_vis_checksum = 0;
3044
3045 max_rooms = cf_ReadInt(fp);
3046
3047 if (version < 62) {
3048 cfseek(fp, sizeof(int16_t) * max_rooms * max_rooms, SEEK_CUR);
3049
3050 mprintf(0, "We will need to remake boa. New cost structure added\n");
3051 BOA_AABB_checksum = BOA_mine_checksum = 0;
3052 } else {
3053 max_path_portals = cf_ReadInt(fp);
3054
3055 ASSERT(max_rooms - 1 <= MAX_ROOMS + 8);
3056
3057 if (version < 110 || (max_path_portals != MAX_PATH_PORTALS)) {
3058 cfseek(fp, sizeof(int16_t) * max_rooms * max_rooms + max_rooms * max_path_portals * sizeof(float), SEEK_CUR);
3059
3060 if (version >= 107) {
3061 // Read BOA terrain info (temporary, just so vis data works with multiplay)
3062 cfseek(fp, max_rooms * sizeof(float), SEEK_CUR);
3063 }
3064
3065 mprintf(0, "We will need to remake boa. Data size changed\n");
3066 BOA_AABB_checksum = BOA_mine_checksum = 0;
3067 } else {
3068 for (i = 0; i <= max_rooms; i++) {
3069 for (j = 0; j <= max_rooms; j++) {
3070 BOA_Array[i][j] = cf_ReadShort(fp);
3071 }
3072 }
3073
3074 for (i = 0; i <= max_rooms; i++) {
3075 for (j = 0; j < max_path_portals; j++) {
3076 BOA_cost_array[i][j] = cf_ReadFloat(fp);
3077 }
3078 }
3079
3080 BOA_num_mines = cf_ReadInt(fp);
3081 BOA_num_terrain_regions = cf_ReadInt(fp);
3082
3083 if (version < 112) {
3084 mprintf(0, "We will need to remake boa.\n");
3085 BOA_AABB_checksum = BOA_mine_checksum = 0;
3086 } else {
3087 int i, j;
3088
3089 for (i = 0; i < BOA_num_terrain_regions; i++) {
3090 BOA_num_connect[i] = cf_ReadInt(fp);
3091
3092 for (j = 0; j < BOA_num_connect[i]; j++) {
3093 BOA_connect[i][j].roomnum = cf_ReadInt(fp);
3094 BOA_connect[i][j].portal = cf_ReadInt(fp);
3095 }
3096 }
3097 }
3098 }
3099 }
3100 }
3101
3102 void ReadRoomAABBChunk(CFILE *fp, int version) {
3103 int save_highest_room_index;
3104 int i;
3105
3106 save_highest_room_index = cf_ReadInt(fp);
3107 ASSERT(save_highest_room_index < MAX_ROOMS); // Someone decreased the max amount of rooms
3108 ASSERT(save_highest_room_index == Highest_room_index); // Someone decreased the max amount of rooms
3109
3110 for (i = 0; i <= save_highest_room_index; i++) {
3111 BOA_AABB_ROOM_checksum[i] = cf_ReadInt(fp);
3112 }
3113
3114 for (i = 0; i <= Highest_room_index; i++) {
3115 int used = cf_ReadInt(fp);
3116 ASSERT(Rooms[i].used == used);
3117
3118 if (used) {
3119 int j;
3120 int k;
3121
3122 int n_faces = cf_ReadInt(fp);
3123 ASSERT(Rooms[i].num_faces == n_faces);
3124
3125 for (j = 0; j < Rooms[i].num_faces; j++) {
3126 Rooms[i].faces[j].min_xyz.x = cf_ReadFloat(fp);
3127 Rooms[i].faces[j].min_xyz.y = cf_ReadFloat(fp);
3128 Rooms[i].faces[j].min_xyz.z = cf_ReadFloat(fp);
3129
3130 Rooms[i].faces[j].max_xyz.x = cf_ReadFloat(fp);
3131 Rooms[i].faces[j].max_xyz.y = cf_ReadFloat(fp);
3132 Rooms[i].faces[j].max_xyz.z = cf_ReadFloat(fp);
3133 }
3134
3135 BOA_AABB_ROOM_checksum[i] = cf_ReadInt(fp);
3136
3137 Rooms[i].bbf_min_xyz.x = cf_ReadFloat(fp);
3138 Rooms[i].bbf_min_xyz.y = cf_ReadFloat(fp);
3139 Rooms[i].bbf_min_xyz.z = cf_ReadFloat(fp);
3140
3141 Rooms[i].bbf_max_xyz.x = cf_ReadFloat(fp);
3142 Rooms[i].bbf_max_xyz.y = cf_ReadFloat(fp);
3143 Rooms[i].bbf_max_xyz.z = cf_ReadFloat(fp);
3144
3145 Rooms[i].num_bbf_regions = cf_ReadShort(fp);
3146 Rooms[i].num_bbf = (int16_t *)mem_malloc(sizeof(int16_t) * Rooms[i].num_bbf_regions);
3147 Rooms[i].bbf_list = (int16_t **)mem_malloc(sizeof(int16_t *) * Rooms[i].num_bbf_regions);
3148 Rooms[i].bbf_list_min_xyz = (vector *)mem_malloc(sizeof(vector) * Rooms[i].num_bbf_regions);
3149 Rooms[i].bbf_list_max_xyz = (vector *)mem_malloc(sizeof(vector) * Rooms[i].num_bbf_regions);
3150 Rooms[i].bbf_list_sector = (uint8_t *)mem_malloc(sizeof(char) * Rooms[i].num_bbf_regions);
3151
3152 for (j = 0; j < Rooms[i].num_bbf_regions; j++) {
3153 Rooms[i].num_bbf[j] = cf_ReadShort(fp);
3154 Rooms[i].bbf_list[j] = (int16_t *)mem_malloc(sizeof(int16_t) * Rooms[i].num_bbf[j]);
3155 }
3156
3157 for (j = 0; j < Rooms[i].num_bbf_regions; j++) {
3158 for (k = 0; k < Rooms[i].num_bbf[j]; k++) {
3159 Rooms[i].bbf_list[j][k] = cf_ReadShort(fp);
3160 }
3161
3162 Rooms[i].bbf_list_min_xyz[j].x = cf_ReadFloat(fp);
3163 Rooms[i].bbf_list_min_xyz[j].y = cf_ReadFloat(fp);
3164 Rooms[i].bbf_list_min_xyz[j].z = cf_ReadFloat(fp);
3165
3166 Rooms[i].bbf_list_max_xyz[j].x = cf_ReadFloat(fp);
3167 Rooms[i].bbf_list_max_xyz[j].y = cf_ReadFloat(fp);
3168 Rooms[i].bbf_list_max_xyz[j].z = cf_ReadFloat(fp);
3169
3170 Rooms[i].bbf_list_sector[j] = cf_ReadByte(fp);
3171 }
3172 } else {
3173 BOA_AABB_ROOM_checksum[i] = 0; // Not used
3174 }
3175 }
3176 }
3177
3178 void ReadMatcenChunk(CFILE *fp, int version) {
3179 int i;
3180
3181 Num_matcens = cf_ReadInt(fp);
3182 for (i = 0; i < Num_matcens; i++) {
3183 Matcen[i] = new matcen;
3184 Matcen[i]->LoadData(fp);
3185 }
3186 }
3187
3188 void ReadALifeChunk(CFILE *fp, int version) {
3189 #ifndef NEWEDITOR
3190 a_life.LoadData(fp);
3191 #endif
3192 }
3193
3194 void ReadLevelGoalsChunk(CFILE *fp, int version) { Level_goals.LoadLevelGoalInfo(fp); }
3195
3196 // Reads relevant info about the terrain sky and lights
3197 void ReadTerrainSkyAndLightChunk(CFILE *fp, int version) {
3198 int i, num_sats;
3199
3200 if (version >= 41) {
3201 if (version >= 88)
3202 Terrain_sky.fog_scalar = cf_ReadFloat(fp);
3203
3204 if (version >= 87)
3205 Terrain_sky.damage_per_second = cf_ReadFloat(fp);
3206
3207 Terrain_sky.textured = cf_ReadByte(fp);
3208
3209 if (version < 43) {
3210 for (i = 0; i < MAX_HORIZON_PIECES; i++)
3211 cf_ReadShort(fp);
3212 } else if (version < 74) {
3213 for (i = 0; i < MAX_HORIZON_PIECES; i++) {
3214 cf_ReadShort(fp);
3215 cf_ReadShort(fp);
3216 }
3217
3218 } else {
3219 if (version < 104) {
3220 for (i = 0; i < 4; i++)
3221 cf_ReadShort(fp);
3222 } else {
3223 Terrain_sky.dome_texture = texture_xlate[cf_ReadShort(fp)];
3224
3225 // Check for failed xlate
3226 if (Terrain_sky.dome_texture == -1) {
3227 Terrain_sky.dome_texture = 0;
3228 }
3229 }
3230
3231 if (version < 114)
3232 for (i = 0; i < MAX_HORIZON_PIECES; i++)
3233 cf_ReadShort(fp); // Was band textures
3234 }
3235 }
3236
3237 Terrain_sky.sky_color = cf_ReadInt(fp);
3238 Terrain_sky.horizon_color = cf_ReadInt(fp);
3239 Terrain_sky.fog_color = cf_ReadInt(fp);
3240
3241 Terrain_sky.flags = cf_ReadInt(fp);
3242
3243 #if defined(POSIX)
3244 if (FindArg("-noterrainfog") > 0) {
3245 Terrain_sky.flags &= ~TF_FOG;
3246 }
3247 #endif
3248
3249 if (version >= 69) {
3250 Terrain_sky.radius = cf_ReadFloat(fp);
3251 if (version < 74)
3252 Terrain_sky.radius = 2500.0;
3253
3254 SetupSky(Terrain_sky.radius, Terrain_sky.flags, 1);
3255 }
3256
3257 if (version >= 104)
3258 Terrain_sky.rotate_rate = cf_ReadFloat(fp);
3259 else
3260 Terrain_sky.rotate_rate = 0;
3261
3262 num_sats = cf_ReadInt(fp);
3263
3264 ASSERT(num_sats <= MAX_SATELLITES);
3265
3266 Terrain_sky.num_satellites = num_sats;
3267
3268 for (i = 0; i < num_sats; i++) {
3269 Terrain_sky.satellite_texture[i] = texture_xlate[cf_ReadShort(fp)];
3270
3271 // Check for failed xlate
3272 if (Terrain_sky.satellite_texture[i] == -1) {
3273 Terrain_sky.satellite_texture[i] = 0;
3274 }
3275
3276 cf_ReadVector(fp, &Terrain_sky.satellite_vectors[i]);
3277 if (version >= 44)
3278 Terrain_sky.satellite_flags[i] = cf_ReadByte(fp);
3279 else
3280 Terrain_sky.satellite_flags[i] = 0;
3281
3282 if (version >= 116)
3283 Terrain_sky.satellite_size[i] = cf_ReadFloat(fp);
3284 else {
3285 vector subvec = Terrain_sky.satellite_vectors[i];
3286 vm_NormalizeVector(&subvec);
3287
3288 Terrain_sky.satellite_vectors[i] = subvec * (Terrain_sky.radius * 3);
3289
3290 Terrain_sky.satellite_size[i] = 500;
3291 }
3292
3293 if (version >= 75) {
3294 Terrain_sky.satellite_r[i] = cf_ReadFloat(fp);
3295 Terrain_sky.satellite_g[i] = cf_ReadFloat(fp);
3296 Terrain_sky.satellite_b[i] = cf_ReadFloat(fp);
3297 }
3298 }
3299
3300 // Now read misc stuff
3301 if (version <= 56) {
3302 cf_ReadFloat(fp);
3303 cf_ReadFloat(fp);
3304 }
3305
3306 // Read lighting
3307 if (version <= 31) {
3308 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
3309 if (version >= 24) {
3310 Terrain_seg[i].l = cf_ReadByte(fp);
3311 Terrain_seg[i].r = cf_ReadByte(fp);
3312 Terrain_seg[i].g = cf_ReadByte(fp);
3313 Terrain_seg[i].b = cf_ReadByte(fp);
3314 } else {
3315 Terrain_seg[i].l = cf_ReadByte(fp);
3316 Terrain_seg[i].r = Terrain_seg[i].l;
3317 Terrain_seg[i].g = Terrain_seg[i].l;
3318 Terrain_seg[i].b = Terrain_seg[i].l;
3319 }
3320 }
3321 if (version >= 25) {
3322 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
3323 Terrain_dynamic_table[i] = cf_ReadByte(fp);
3324 }
3325 } else {
3326 uint8_t *byte_vals = (uint8_t *)mem_malloc(TERRAIN_DEPTH * TERRAIN_WIDTH);
3327
3328 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3329 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3330 Terrain_seg[i].l = byte_vals[i];
3331
3332 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3333 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3334 Terrain_seg[i].r = byte_vals[i];
3335
3336 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3337 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3338 Terrain_seg[i].g = byte_vals[i];
3339
3340 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3341 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3342 Terrain_seg[i].b = byte_vals[i];
3343
3344 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3345 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3346 Terrain_dynamic_table[i] = byte_vals[i];
3347
3348 if (version >= 72) {
3349 Terrain_occlusion_checksum = cf_ReadInt(fp);
3350 ReadCompressionByte(fp, byte_vals, OCCLUSION_SIZE * OCCLUSION_SIZE * 32);
3351 for (i = 0; i < OCCLUSION_SIZE * OCCLUSION_SIZE * 32; i++) {
3352 int row = i / 32;
3353 int column = i % 32;
3354
3355 Terrain_occlusion_map[row][column] = byte_vals[i];
3356 }
3357 }
3358
3359 mem_free(byte_vals);
3360 }
3361 }
3362
3363 // Reads the tmaps of the terrain, plus flags
3364 void ReadTerrainTmapFlagChunk(CFILE *fp, int version) {
3365 int i, t;
3366
3367 if (version <= 31) {
3368 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3369 cf_ReadShort(fp);
3370 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3371 cf_ReadShort(fp);
3372
3373 if (version >= 16) {
3374 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
3375 Terrain_seg[i].flags = cf_ReadByte(fp);
3376
3377 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
3378 cf_ReadByte(fp);
3379 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
3380 cf_ReadByte(fp);
3381 } else {
3382 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
3383 for (t = 0; t < 4; t++) {
3384 cf_ReadByte(fp);
3385 cf_ReadByte(fp);
3386 }
3387 }
3388 }
3389 } else {
3390 uint8_t *byte_vals = (uint8_t *)mem_malloc(TERRAIN_DEPTH * TERRAIN_WIDTH);
3391 uint16_t *short_vals = (uint16_t *)mem_malloc(2 * TERRAIN_DEPTH * TERRAIN_WIDTH);
3392
3393 if (version < 102) {
3394 // Read tmap1
3395 ReadCompressionShort(fp, short_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3396 // Read tmap2
3397 ReadCompressionShort(fp, short_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3398 } else {
3399 ReadCompressionShort(fp, short_vals, TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH);
3400 for (i = 0; i < TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH; i++) {
3401 Terrain_tex_seg[i].tex_index = texture_xlate[short_vals[i]];
3402
3403 // Check for failed xlate
3404 if (Terrain_tex_seg[i].tex_index == -1) {
3405 Terrain_tex_seg[i].tex_index = 0;
3406 }
3407 }
3408
3409 ReadCompressionByte(fp, byte_vals, TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH);
3410 for (i = 0; i < TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH; i++) {
3411 Terrain_tex_seg[i].rotation = byte_vals[i];
3412 if ((Terrain_tex_seg[i].rotation >> 4) == 0)
3413 Terrain_tex_seg[i].rotation |= (1 << 4);
3414 }
3415 }
3416
3417 // Read flags
3418 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3419 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
3420 Terrain_seg[i].flags = byte_vals[i];
3421
3422 if (version < 102) {
3423 // Read rotation_factor
3424 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3425
3426 // Read tile_factor
3427 ReadCompressionByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
3428 }
3429
3430 mem_free(byte_vals);
3431 mem_free(short_vals);
3432 }
3433 }
3434
3435 // MUST BE SAVED BEFORE OBJECTS because of ResetTerrain
3436
3437 void ReadTerrainChunks(CFILE *fp, int version) {
3438 char chunk_name[4];
3439 int terr_chunk_start, terr_chunk_size;
3440 int done = 0;
3441
3442 ResetTerrain();
3443
3444 while (!done) {
3445 cf_ReadBytes((uint8_t *)chunk_name, 4, fp);
3446 terr_chunk_start = cftell(fp);
3447 terr_chunk_size = cf_ReadInt(fp);
3448
3449 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL,
3450 (filelen) ? (float)(terr_chunk_start + terr_chunk_size) / (float)filelen : 0.0f, NULL);
3451
3452 if (ISCHUNK(CHUNK_TERRAIN_HEIGHT)) {
3453 ReadTerrainHeightChunk(fp, version);
3454 } else if (ISCHUNK(CHUNK_TERRAIN_TMAPS_FLAGS)) {
3455 ReadTerrainTmapFlagChunk(fp, version);
3456 } else if (ISCHUNK(CHUNK_TERRAIN_SKY)) {
3457 ReadTerrainSkyAndLightChunk(fp, version);
3458 } else if (ISCHUNK(CHUNK_TERRAIN_END)) {
3459 done = 1;
3460 cfseek(fp, terr_chunk_start + terr_chunk_size, SEEK_SET);
3461 continue;
3462 }
3463 // else unknown chunk
3464 cfseek(fp, terr_chunk_start + terr_chunk_size, SEEK_SET);
3465 }
3466
3467 // Generate needed info
3468 BuildMinMaxTerrain();
3469 BuildTerrainNormals();
3470 UpdateTerrainLightmaps();
3471
3472 #if (defined(EDITOR) || defined(NEWEDITOR))
3473 memset(TerrainSelected, 0, TERRAIN_WIDTH * TERRAIN_DEPTH);
3474 Num_terrain_selected = 0;
3475 #endif
3476 }
3477
3478 // Read the texture names & build the xlate table
3479 void ReadTextureList(CFILE *ifile) { BuildXlateTable(ifile, FindTextureName, texture_xlate, MAX_TEXTURES, -1); }
3480
3481 void ReadPlayerStarts(CFILE *infile, int fileversion) {
3482 int i, n;
3483
3484 n = (fileversion >= 120) ? cf_ReadShort(infile) : 32;
3485
3486 for (i = 0; i < n; i++) {
3487 Players[i].startpos_flags = cf_ReadInt(infile);
3488 }
3489 }
3490
3491 void VerifyObjectList() {
3492 int i;
3493
3494 uint8_t already_listed[MAX_OBJECTS];
3495 memset(already_listed, 0, MAX_OBJECTS);
3496
3497 for (i = 0; i <= Highest_room_index; i++) {
3498 room *rp = &Rooms[i];
3499 if (rp->flags & RF_EXTERNAL)
3500 continue;
3501 if (rp->used == 0)
3502 continue;
3503 for (int objnum = rp->objects; objnum != -1; objnum = Objects[objnum].next) {
3504 ASSERT(already_listed[objnum] == 0);
3505 already_listed[objnum] = 1;
3506 }
3507 }
3508
3509 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++) {
3510 terrain_segment *tseg = &Terrain_seg[i];
3511
3512 for (int objnum = tseg->objects; objnum != -1; objnum = Objects[objnum].next) {
3513 ASSERT(already_listed[objnum] == 0);
3514 already_listed[objnum] = 1;
3515 }
3516 }
3517 }
3518
3519 // Data to deal with a bunch of renamed doors
3520 const char *Old_door_names[] = {"markroomdoor.OOF1",
3521 "cellblockdoor.OOF1",
3522 "towerringdoor.OOF1",
3523 "hangdoorinverse.oof1",
3524 "Big Lock Door",
3525 "Oval Door",
3526 "Layered Door",
3527 "steamdoor1",
3528 "heatsinkdoornew1",
3529 "Vault Door Large",
3530 "Ceddoor1",
3531 "trapdoor1",
3532 "Lukesecretdoor1",
3533 "ptmc11",
3534 "ptmc31",
3535 "ptmc021",
3536 "Bulkhead1",
3537 "Placeholderdoor1",
3538 "ptmcbd1",
3539 "energysecretdoor1",
3540 "PTMC Industrial 1",
3541 "PTMC Covert 1"};
3542
3543 const char *New_door_names[] = {"MARK'S OLD DOOR",
3544 "SEAN'S NOVAK DOOR 1",
3545 "SEAN'S NOVAK DOOR 2",
3546 "SEAN'S NOVAK DOOR 3",
3547 "PTMC Industrial 2",
3548 "SEAN'S DUCTWORK DOOR",
3549 "PTMC Industrial 4",
3550 "SEAN'S STEAMVENT DOOR",
3551 "SEAN'S HEATSINK DOOR",
3552 "DAN'S VAULT DOOR",
3553 "CED 1",
3554 "SEAN'S TRAPDOOR",
3555 "LUKE'S SECRET DOOR",
3556 "PTMC Industrial 5",
3557 "PTMC Industrial 6",
3558 "PTMC Industrial 7",
3559 "!!!CED BROKEN!!!",
3560 "PLACEHOLDER DOOR",
3561 "PTMC Industrial 3",
3562 "SEAN'S ENERGY SECRET DOOR",
3563 "PTMC Industrial 1",
3564 "PTMC Covert 1"};
3565
3566 #define NUM_RENAMED_DOORS (sizeof(Old_door_names) / sizeof(*Old_door_names))
3567
3568 // Deals with some renamed doors. Translates the old name to the new name, then looks up the id
3569 int SpecialFindDoorName(const char *name) {
3570 int i, id;
3571
3572 // Look up the old name, and return it if found
3573 id = FindDoorName(name);
3574 if (id != -1)
3575 return id;
3576
3577 // Didn't find old name, so see if there's a new name, and look for that
3578 for (i = 0; i < NUM_RENAMED_DOORS; i++) {
3579 if (stricmp(name, Old_door_names[i]) == 0)
3580 return FindDoorName(New_door_names[i]);
3581 }
3582
3583 // Didn't find old name, didn't find xlation for the old name
3584 return -1;
3585 }
3586
3587 static inline char* GetCurrentSumString() {
3588 static char output_buf[100];
3589 output_buf[0] = '\0';
3590 auto digest = Level_md5->digest();
3591 char bytestr[10] = "";
3592 // Do level checksum
3593 for (int i = 0; i < 16; i++) {
3594 snprintf(bytestr, sizeof(bytestr), "%.2x", digest[i]);
3595 strcat(output_buf, bytestr);
3596 }
3597 return output_buf;
3598 }
3599
3600 extern bool Disable_editor_rendering;
3601
3602 #define LEVEL_LOADED_PCT_CALC (filelen) ? (float)(chunk_size + chunk_start) / (float)filelen : 0.0f
3603 // Load a level file
3604 // Returns 1 if file read ok, else 0
3605 int LoadLevel(char *filename, void (*cb_fn)(const char *, int, int)) {
3606 CFILE *ifile;
3607 char tag[4];
3608 int n, i, retval = 1;
3609
3610 int version;
3611 bool f_read_AABB = false;
3612 bool no_128s = true;
3613 int total = 0;
3614 #ifdef EDITOR
3615 Disable_editor_rendering = 1;
3616 Num_failed_xlate_items = 0;
3617 #endif
3618 PrintDedicatedMessage(TXT_DS_OPENLEVEL, filename);
3619
3620 RestartLevelMD5();
3621
3622 ifile = cfopen(filename, "rb");
3623 if (!ifile) {
3624 retval = 0;
3625 goto end_loadlevel;
3626 }
3627
3628 try { // catch cfile errors
3629
3630 filelen = cfilelength(ifile);
3631
3632 // Read & check tag
3633 cf_ReadBytes((uint8_t *)tag, 4, ifile);
3634 if (strncmp(tag, LEVEL_FILE_TAG, 4)) {
3635 cfclose(ifile);
3636 retval = 0;
3637 goto end_loadlevel;
3638 }
3639
3640 // Read & check version number
3641 version = cf_ReadInt(ifile);
3642
3643 // Check for too-new version
3644 if (version > LEVEL_FILE_VERSION) {
3645 cfclose(ifile);
3646 #if (defined(EDITOR) || defined(NEWEDITOR))
3647 if (GetFunctionMode() == EDITOR_MODE)
3648 EditorMessageBox("Can't load level file \"%s\": Its version (%d) is newer than current version (%d).", filename,
3649 version, LEVEL_FILE_VERSION);
3650 #endif
3651 retval = 0;
3652 goto end_loadlevel;
3653 }
3654
3655 // Check for too-old version
3656 if (version < LEVEL_FILE_OLDEST_COMPATIBLE_VERSION) {
3657 cfclose(ifile);
3658 #if (defined(EDITOR) || defined(NEWEDITOR))
3659 if (GetFunctionMode() == EDITOR_MODE)
3660 EditorMessageBox(
3661 "Can't load level file \"%s\": Its version (%d) is older than the oldest compatible version (%d).",
3662 filename, version, LEVEL_FILE_OLDEST_COMPATIBLE_VERSION);
3663 #endif
3664 retval = 0;
3665 goto end_loadlevel;
3666 }
3667
3668 // Get rid of old mine
3669 FreeAllRooms();
3670
3671 // Get rid of old matcens
3672 DestroyAllMatcens();
3673
3674 #ifndef NEWEDITOR
3675 // Resets the ambient life controller
3676 a_life.ALReset();
3677 #endif
3678
3679 // Get rid of old level goals
3680 Level_goals.CleanupAfterLevel();
3681
3682 // Get rid of old objects
3683 FreeAllObjects();
3684
3685 #ifndef NEWEDITOR
3686 // Clear glows
3687 ResetLightGlows();
3688 #endif
3689
3690 // Reset all the objects
3691 ResetObjectList();
3692
3693 // Free all triggers
3694 FreeTriggers();
3695
3696 // Resets the game paths to blank
3697 InitGamePaths();
3698
3699 // Clear terrain sounds
3700 ClearTerrainSound();
3701
3702 // Init level info
3703 strcpy(Level_info.name, "Unnamed");
3704 strcpy(Level_info.designer, "Anonymous");
3705 strcpy(Level_info.copyright, "");
3706 strcpy(Level_info.notes, "");
3707
3708 BOA_AABB_checksum = BOA_mine_checksum = 0;
3709 for (i = 0; i < MAX_ROOMS; i++) {
3710 BOA_AABB_ROOM_checksum[i] = 0;
3711 }
3712
3713 BNode_ClearBNodeInfo();
3714 FVI_always_check_ceiling = false;
3715
3716 sound_override_force_field = -1;
3717 sound_override_glass_breaking = -1;
3718
3719 for (i = 0; i < MAX_FORCE_FIELD_BOUNCE_TEXTURES; i++) {
3720 force_field_bounce_texture[i] = -1;
3721 force_field_bounce_multiplier[i] = 1.0f;
3722 }
3723
3724 Level_powerups_ignore_wind = false;
3725
3726 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL, 0.01f);
3727
3728 // Read and parse chunks
3729 while (!cfeof(ifile)) {
3730 char chunk_name[4];
3731
3732 cf_ReadBytes((uint8_t *)chunk_name, 4, ifile);
3733 chunk_start = cftell(ifile);
3734 chunk_size = cf_ReadInt(ifile);
3735 mprintf(0, "Chunk: %c%c%c%c, size=%d\n", chunk_name[0], chunk_name[1], chunk_name[2], chunk_name[3], chunk_size);
3736
3737 if (ISCHUNK(CHUNK_TEXTURE_NAMES))
3738 ReadTextureList(ifile);
3739 else if (ISCHUNK(CHUNK_GENERIC_NAMES)) {
3740 BuildXlateTable(ifile, FindObjectIDName, generic_xlate, MAX_OBJECT_IDS, XT_GENERIC);
3741 } else if (ISCHUNK(CHUNK_DOOR_NAMES)) {
3742 if (version < 82)
3743 BuildXlateTable(ifile, SpecialFindDoorName, door_xlate, MAX_DOORS, XT_DOOR);
3744 else
3745 BuildXlateTable(ifile, FindDoorName, door_xlate, MAX_DOORS, XT_DOOR);
3746 } else if (ISCHUNK(CHUNK_ROOMS)) {
3747 int num_rooms;
3748
3749 num_rooms = cf_ReadInt(ifile);
3750
3751 extern void RoomMemInit(int nverts, int nfaces, int nfaceverts, int nportals); // MOVE TO HEADER FILE
3752 if (version >= 85) {
3753 int nverts, nfaces, nfaceverts, nportals;
3754 nverts = cf_ReadInt(ifile);
3755 nfaces = cf_ReadInt(ifile);
3756 nfaceverts = cf_ReadInt(ifile);
3757 nportals = cf_ReadInt(ifile);
3758 RoomMemInit(nverts, nfaces, nfaceverts, nportals);
3759 } else
3760 RoomMemInit(0, 0, 0, 0); // 0 means we don't know how much memory we need
3761
3762 n_degenerate_faces_removed = 0;
3763 int roomnum;
3764 for (i = 0; i < num_rooms; i++) {
3765
3766 if ((i % 10) == 0) {
3767 LoadLevelProgress(
3768 LOAD_PROGRESS_LOADING_LEVEL,
3769 (filelen) ? (float)(chunk_start + (((chunk_size) / num_rooms) * i)) / (float)filelen : 0.0f, NULL);
3770 }
3771
3772 roomnum = (version >= 96) ? cf_ReadShort(ifile) : i;
3773 ReadRoom(ifile, &Rooms[roomnum], version);
3774 }
3775 mprintf(1, "%d degenerate faces removed\n", n_degenerate_faces_removed);
3776
3777 Highest_room_index = roomnum;
3778 ASSERT(Highest_room_index < MAX_ROOMS);
3779
3780 } else if (ISCHUNK(CHUNK_ROOM_WIND)) {
3781 int num_rooms = cf_ReadInt(ifile);
3782
3783 for (i = 0; i < num_rooms; i++) {
3784 int roomnum = cf_ReadShort(ifile);
3785 cf_ReadVector(ifile, &Rooms[roomnum].wind);
3786 }
3787 } else if (ISCHUNK(CHUNK_PLAYER_STARTS)) {
3788 ReadPlayerStarts(ifile, version);
3789 } else if (ISCHUNK(CHUNK_OBJECT_HANDLES)) { // Read in any non-zero handles for deleted objects
3790 int handle, objnum;
3791
3792 uint8_t already_loaded[MAX_OBJECTS];
3793 memset(already_loaded, 0, MAX_OBJECTS);
3794
3795 // Get the number of handles in the file
3796 n = cf_ReadInt(ifile);
3797
3798 for (i = 0; i < n; i++) {
3799 handle = cf_ReadInt(ifile);
3800
3801 if (version < 94)
3802 handle = XLATE_HANDLE(handle);
3803
3804 objnum = handle & HANDLE_OBJNUM_MASK;
3805
3806 ASSERT(already_loaded[objnum] == 0);
3807
3808 already_loaded[objnum] = 1;
3809 Objects[objnum].handle = handle;
3810 }
3811 } else if (ISCHUNK(CHUNK_OBJECTS)) {
3812 n = cf_ReadInt(ifile);
3813 for (i = 0; i < n; i++) {
3814 int handle, objnum, roomnum;
3815
3816 if (version >= 45) {
3817 handle = cf_ReadInt(ifile);
3818 if (version < 94)
3819 handle = XLATE_HANDLE(handle);
3820 objnum = handle & HANDLE_OBJNUM_MASK;
3821 } else {
3822 objnum = i;
3823 handle = i + HANDLE_COUNT_INCREMENT;
3824 }
3825
3826 ReadObject(ifile, &Objects[objnum], handle, version);
3827
3828 // Link the object into the mine
3829 roomnum = Objects[objnum].roomnum;
3830
3831 Objects[objnum].roomnum = -1; // ObjLink() expects the roomnum to be -1
3832
3833 if ((roomnum > Highest_room_index) && !ROOMNUM_OUTSIDE(roomnum)) { // bad roomnum
3834 Int3(); // loading object with invalid room number
3835 Objects[objnum].type = OBJ_NONE; // kill the object
3836 } else {
3837 if (!ROOMNUM_OUTSIDE(roomnum) && Rooms[roomnum].flags & RF_EXTERNAL) {
3838 mprintf(0, "Internal object %d linked to external room %d (type = %d)!!!\n", objnum, roomnum, Objects[objnum].type);
3839 if (Objects[objnum].type == OBJ_VIEWER)
3840 Objects[objnum].type = OBJ_NONE; // kill the object
3841 else {
3842 Int3();
3843 int k;
3844 int done = 0;
3845 for (k = 0; k <= Highest_room_index && !done; k++) {
3846
3847 if (Rooms[k].used && !(Rooms[k].flags & RF_EXTERNAL)) {
3848 done = 1;
3849 ObjLink(objnum, k);
3850 }
3851 }
3852 }
3853 } else
3854 ObjLink(objnum, roomnum);
3855 }
3856
3857 #ifndef NEWEDITOR
3858 ObjInitPositionHistory(&Objects[objnum]);
3859 #endif
3860 }
3861
3862 // Rebuild the free object list
3863 ResetFreeObjects();
3864 } else if (ISCHUNK(CHUNK_TRIGGERS)) {
3865 Num_triggers = cf_ReadInt(ifile);
3866 for (i = 0; i < Num_triggers; i++) {
3867 ReadTrigger(ifile, &Triggers[i], version);
3868 }
3869 } else if (ISCHUNK(CHUNK_GAME_PATHS)) {
3870 ReadGamePathsChunk(ifile, version);
3871 } else if (ISCHUNK(CHUNK_BOA)) {
3872 ReadBOAChunk(ifile, version);
3873 } else if (ISCHUNK(CHUNK_BNODES)) {
3874 ReadBNodeChunk(ifile, version);
3875 } else if (ISCHUNK(CHUNK_OVERRIDE_SOUNDS)) {
3876 ReadOverrideSoundChunk(ifile, version);
3877 } else if (ISCHUNK(CHUNK_FFT_MOD)) {
3878 ReadFFTMChunk(ifile, version);
3879 } else if (ISCHUNK(CHUNK_ROOM_AABB)) {
3880 ReadRoomAABBChunk(ifile, version);
3881 f_read_AABB = true;
3882 } else if (ISCHUNK(CHUNK_MATCEN_DATA)) {
3883 ReadMatcenChunk(ifile, version);
3884 } else if (ISCHUNK(CHUNK_LEVEL_GOALS)) {
3885 ReadLevelGoalsChunk(ifile, version);
3886 } else if (ISCHUNK(CHUNK_LIGHTMAPS)) {
3887 ReadLightmapChunk(ifile, version);
3888 } else if (ISCHUNK(CHUNK_NEW_LIGHTMAPS)) {
3889 ReadNewLightmapChunk(ifile, version);
3890 } else if (ISCHUNK(CHUNK_ALIFE_DATA)) {
3891 ReadALifeChunk(ifile, version);
3892 } else if (ISCHUNK(CHUNK_NEW_BSP)) {
3893 InitDefaultBSP();
3894
3895 BSPChecksum = cf_ReadInt(ifile);
3896 LoadBSPNode(ifile, &MineBSP.root);
3897 } else if (ISCHUNK(CHUNK_TERRAIN_SOUND)) {
3898 int n_bands = cf_ReadInt(ifile);
3899 for (int b = 0; b < n_bands; b++) {
3900 if (version < 119)
3901 Terrain_sound_bands[b].sound_index = cf_ReadInt(ifile);
3902 else {
3903 char soundname[PAGENAME_LEN];
3904 cf_ReadString(soundname, sizeof(soundname), ifile);
3905 Terrain_sound_bands[b].sound_index = FindSoundName(IGNORE_TABLE(soundname));
3906 }
3907 Terrain_sound_bands[b].low_alt = cf_ReadByte(ifile);
3908 Terrain_sound_bands[b].high_alt = cf_ReadByte(ifile);
3909 Terrain_sound_bands[b].low_volume = cf_ReadFloat(ifile);
3910 Terrain_sound_bands[b].high_volume = cf_ReadFloat(ifile);
3911 }
3912 }
3913 #if (defined(EDITOR) || defined(NEWEDITOR))
3914 else if (ISCHUNK(CHUNK_SCRIPT)) {
3915 #if (defined(EDITOR) || defined(NEWEDITOR))
3916 if (FindArg("-savescript")) {
3917 char path[_MAX_PATH], name[_MAX_PATH];
3918 ddio_SplitPath(filename, path, name, NULL);
3919 strcat(path, name);
3920 strcat(path, ".scr");
3921 CFILE *ofile = cfopen(path, "wb");
3922 uint8_t buf[1000];
3923 int n = chunk_size, r;
3924 while (n) {
3925 r = cf_ReadBytes(buf, std::min(n, static_cast<int>(sizeof(buf))), ifile);
3926 cf_WriteBytes(buf, r, ofile);
3927 n -= r;
3928 }
3929 cfclose(ofile);
3930 }
3931 #endif // ifdef EDITOR
3932 }
3933 #endif
3934 else if (ISCHUNK(CHUNK_SCRIPT_CODE)) {
3935 // Level_script = D3XLoadProgram(ifile); -- ripped out
3936 } else if (ISCHUNK(CHUNK_TERRAIN)) {
3937 ReadTerrainChunks(ifile, version);
3938 } else if (ISCHUNK(CHUNK_LEVEL_INFO)) {
3939 cf_ReadString(Level_info.name, sizeof(Level_info.name), ifile);
3940
3941 // Localize level name here...
3942 strcpy(Level_info.name, LocalizeLevelName(Level_info.name));
3943
3944 cf_ReadString(Level_info.designer, sizeof(Level_info.designer), ifile);
3945 cf_ReadString(Level_info.copyright, sizeof(Level_info.copyright), ifile);
3946 cf_ReadString(Level_info.notes, sizeof(Level_info.notes), ifile);
3947
3948 if (version >= 83) {
3949 Gravity_strength = cf_ReadFloat(ifile);
3950 } else {
3951 Gravity_strength = -32.2f;
3952 }
3953
3954 if (version >= 131) {
3955 Level_powerups_ignore_wind = (cf_ReadInt(ifile) != 0);
3956 } else {
3957 Level_powerups_ignore_wind = false;
3958 }
3959
3960 if (version >= 122) {
3961 FVI_always_check_ceiling = (cf_ReadInt(ifile) != 0);
3962 } else {
3963 FVI_always_check_ceiling = false;
3964 }
3965
3966 if (version >= 127) {
3967 Ceiling_height = cf_ReadFloat(ifile);
3968 AppendToLevelChecksum(Ceiling_height);
3969 } else
3970 Ceiling_height = MAX_TERRAIN_HEIGHT;
3971 }
3972 #if (defined(EDITOR) || defined(NEWEDITOR))
3973 else if (ISCHUNK(CHUNK_EDITOR_INFO)) {
3974 int t;
3975
3976 // Read misc editor vars
3977 t = cf_ReadShort(ifile);
3978 Curroomp = &Rooms[t];
3979 Curface = cf_ReadShort(ifile);
3980 if (version >= 81) {
3981 Curedge = cf_ReadShort(ifile);
3982 Curvert = cf_ReadShort(ifile);
3983 }
3984 t = cf_ReadShort(ifile);
3985 Markedroomp = (t == -1) ? NULL : &Rooms[t];
3986 Markedface = cf_ReadShort(ifile);
3987 if (version >= 81) {
3988 Markededge = cf_ReadShort(ifile);
3989 Markedvert = cf_ReadShort(ifile);
3990 }
3991
3992 // Read selected list
3993 N_selected_rooms = cf_ReadInt(ifile);
3994 for (i = 0; i < N_selected_rooms; i++)
3995 Selected_rooms[i] = cf_ReadShort(ifile); // saved as shorts to save disk space
3996
3997 // Read other misc stuff
3998 if (version >= 14) {
3999 Cur_object_index = cf_ReadInt(ifile);
4000 Current_trigger = cf_ReadInt(ifile);
4001 if (version < 106)
4002 cf_ReadInt(ifile); // was Current_doorway
4003 Editor_view_mode = cf_ReadInt(ifile);
4004 Editor_viewer_id = cf_ReadInt(ifile);
4005 if (version < 47)
4006 cf_ReadInt(ifile); // was Editor_viewer_id[VM_TERRAIN]
4007 }
4008
4009 if (version >= 55) {
4010 cf_ReadVector(ifile, &Wireframe_view_mine.target);
4011 cf_ReadMatrix(ifile, &Wireframe_view_mine.orient);
4012 Wireframe_view_mine.dist = cf_ReadFloat(ifile);
4013 }
4014
4015 if (version >= 113) {
4016 for (i = 0; i < MAX_ROOMS; i++) {
4017 Room_multiplier[i] = cf_ReadFloat(ifile);
4018 if (version >= 118) {
4019 Room_ambience_r[i] = cf_ReadFloat(ifile);
4020 Room_ambience_g[i] = cf_ReadFloat(ifile);
4021 Room_ambience_b[i] = cf_ReadFloat(ifile);
4022 }
4023 }
4024 }
4025 if (version >= 126)
4026 LightSpacing = cf_ReadInt(ifile);
4027
4028 if (version >= 128) {
4029 GlobalMultiplier = cf_ReadFloat(ifile);
4030 Ambient_red = cf_ReadFloat(ifile);
4031 Ambient_green = cf_ReadFloat(ifile);
4032 Ambient_blue = cf_ReadFloat(ifile);
4033 rad_MaxStep = cf_ReadInt(ifile);
4034 }
4035
4036 }
4037 #endif // ifdef EDITOR
4038 else { // unknown chunk
4039 mprintf(0, " Unknown chunk: %c%c%c%c, size=%d\n", chunk_name[0], chunk_name[1], chunk_name[2], chunk_name[3], chunk_size);
4040 }
4041
4042 // Go to end of chunk
4043 cfseek(ifile, chunk_start + chunk_size, SEEK_SET);
4044
4045 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL, LEVEL_LOADED_PCT_CALC, chunk_name);
4046 }
4047
4048 } // try
4049 catch (cfile_error *cfe) {
4050 mprintf(0, "Error reading: file = <%s>, error = \"%s\"\n", cfe->file->name, cfe->msg);
4051 ASSERT(cfe->read_write == CFE_READING);
4052 #if (defined(EDITOR) || defined(NEWEDITOR))
4053 if (GetFunctionMode() == EDITOR_MODE)
4054 EditorMessageBox("Error reading file \"%s\": %s", cfe->file->name, cfe->msg);
4055 #endif
4056 cfclose(ifile);
4057 return 0;
4058 }
4059
4060 // Close the file
4061 cfclose(ifile);
4062
4063 // Look for player objects & set player starts
4064 FindPlayerStarts();
4065 if (!Player_object && !(Game_mode & GM_MULTI))
4066 Player_object = &Objects[Players[0].objnum];
4067
4068 // aabbs
4069 LoadLevelProgress(LOAD_PROGRESS_LOADING_LEVEL, 1.0f, NULL);
4070
4071 // Compute the bounding boxes
4072 if (!f_read_AABB)
4073 ComputeAABB(true);
4074 else
4075 ComputeAABB(false);
4076
4077 // Create the room objects
4078 CreateRoomObjects();
4079
4080 #ifndef NEWEDITOR /* we call MakeBoa AFTER textures are marked in use */
4081 MakeBOA();
4082 #endif
4083
4084 // Decrement lightmap counters - this must be done because multiple faces can
4085 // share 1 lightmap
4086
4087 if (version >= 34 && !Dedicated_server) {
4088 uint8_t *lightmap_spoken_for = (uint8_t *)mem_malloc(MAX_LIGHTMAPS);
4089 uint8_t *free_lightmap_info = (uint8_t *)mem_malloc(MAX_LIGHTMAP_INFOS);
4090 ASSERT(lightmap_spoken_for);
4091 memset(lightmap_spoken_for, 0, MAX_LIGHTMAPS);
4092
4093 ASSERT(free_lightmap_info);
4094 memset(free_lightmap_info, 0, MAX_LIGHTMAP_INFOS);
4095
4096 for (i = 0; i < Num_lightmap_infos_read; i++) {
4097 int index = LightmapInfoRemap[i];
4098
4099 if (LightmapInfo[index].used == 1) {
4100 free_lightmap_info[index] = 1;
4101
4102 GameLightmaps[LightmapInfo[index].lm_handle].used++;
4103 lightmap_spoken_for[LightmapInfo[index].lm_handle] = 1;
4104 } else if (LightmapInfo[index].used > 1) {
4105 LightmapInfo[index].used--;
4106
4107 // Increase to account for shared
4108 GameLightmaps[LightmapInfo[index].lm_handle].used++;
4109 lightmap_spoken_for[LightmapInfo[index].lm_handle] = 1;
4110
4111 if (LightmapInfo[index].width == 128 || LightmapInfo[index].height == 128)
4112 no_128s = false;
4113 }
4114 }
4115
4116 // Decrease to account for basic allocation
4117 for (i = 0; i < MAX_LIGHTMAPS; i++) {
4118 if (lightmap_spoken_for[i])
4119 GameLightmaps[i].used--;
4120 }
4121 for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) {
4122 if (free_lightmap_info[i])
4123 FreeLightmapInfo(i);
4124 }
4125
4126 mem_free(lightmap_spoken_for);
4127 mem_free(free_lightmap_info);
4128 }
4129
4130 VerifyObjectList();
4131 #ifdef EDITOR
4132 extern void SqueezeLightmaps(int, int);
4133 if (no_128s && version <= 89) {
4134 SqueezeLightmaps(0, -1);
4135 SqueezeLightmaps(1, -1);
4136 }
4137 #endif
4138
4139 ConsolidateMineMirrors();
4140
4141 // paging data.
4142 LoadLevelProgress(LOAD_PROGRESS_PAGING_DATA, 0.0f, NULL);
4143
4144 #ifndef NEWEDITOR
4145 CountDataToPageIn();
4146 #endif
4147 // mprintf(0,"%d bytes of data to page in...\n",total);
4148
4149 end_loadlevel:
4150 #ifdef EDITOR
4151 Disable_editor_rendering = 0;
4152 #endif
4153
4154 if (!BNode_allocated) {
4155 for (int i = 0; i <= Highest_room_index; i++) {
4156 if (Rooms[i].used) {
4157 Rooms[i].bn_info.num_nodes = 0;
4158 Rooms[i].bn_info.nodes = NULL;
4159 }
4160 }
4161 }
4162
4163 // Debug log the current sum
4164 mprintf(0, "End of load level checksum = %s\n", GetCurrentSumString());
4165 // Done
4166 return retval;
4167 }
4168
4169 #if (defined(EDITOR) || defined(NEWEDITOR))
4170
4171 #define cf_WriteVector(f, v) \
4172 do { \
4173 cf_WriteFloat((f), (v)->x); \
4174 cf_WriteFloat((f), (v)->y); \
4175 cf_WriteFloat((f), (v)->z); \
4176 } while (0)
4177 #define cf_WriteMatrix(f, m) \
4178 do { \
4179 cf_WriteVector((f), &(m)->rvec); \
4180 cf_WriteVector((f), &(m)->uvec); \
4181 cf_WriteVector((f), &(m)->fvec); \
4182 } while (0)
4183
4184 // writes an object
4185 // returns 1 if written ok
4186 int WriteObject(CFILE *ofile, object *objp) {
4187 int i, t, k;
4188
4189 cf_WriteByte(ofile, objp->type);
4190 cf_WriteShort(ofile, objp->id);
4191
4192 cf_WriteString(ofile, objp->name ? objp->name : "");
4193
4194 cf_WriteInt(ofile, objp->flags);
4195
4196 if (objp->type == OBJ_DOOR)
4197 cf_WriteShort(ofile, objp->shields);
4198
4199 cf_WriteInt(ofile, objp->roomnum);
4200 cf_WriteVector(ofile, &objp->pos);
4201 cf_WriteMatrix(ofile, &objp->orient);
4202
4203 cf_WriteByte(ofile, objp->contains_type);
4204 cf_WriteByte(ofile, objp->contains_id);
4205 cf_WriteByte(ofile, objp->contains_count);
4206
4207 cf_WriteFloat(ofile, objp->lifeleft);
4208
4209 ASSERT(objp->parent_handle == OBJECT_HANDLE_NONE); // not writing, so make sure none
4210
4211 // Write sound info if this is a soundsource object
4212 if (objp->control_type == CT_SOUNDSOURCE) {
4213 ASSERT(objp->type == OBJ_SOUNDSOURCE);
4214 cf_WriteString(ofile, (objp->ctype.soundsource_info.sound_index == -1)
4215 ? ""
4216 : Sounds[objp->ctype.soundsource_info.sound_index].name);
4217 cf_WriteFloat(ofile, objp->ctype.soundsource_info.volume);
4218 }
4219
4220 i = (objp->custom_default_script_name) ? strlen(objp->custom_default_script_name) : 0;
4221 cf_WriteByte(ofile, i);
4222 if (i > 0)
4223 cf_WriteBytes((uint8_t *)objp->custom_default_script_name, i, ofile);
4224
4225 i = (objp->custom_default_module_name) ? strlen(objp->custom_default_module_name) : 0;
4226 cf_WriteByte(ofile, i);
4227 if (i > 0)
4228 cf_WriteBytes((uint8_t *)objp->custom_default_module_name, i, ofile);
4229
4230 // If there is lightmap data for this object, write it out.
4231 if (objp->lighting_render_type == LRT_LIGHTMAPS) {
4232 if (objp->lm_object.used == 0) {
4233 mprintf(0, "Warning: Object %d is set for lightmaps but has no lightmap data!\n", objp - Objects);
4234 cf_WriteByte(ofile, 0);
4235 } else {
4236 cf_WriteByte(ofile, 1);
4237 cf_WriteByte(ofile, objp->lm_object.num_models);
4238 for (i = 0; i < objp->lm_object.num_models; i++) {
4239 cf_WriteShort(ofile, objp->lm_object.num_faces[i]);
4240 for (t = 0; t < objp->lm_object.num_faces[i]; t++) {
4241 lightmap_object_face *fp = &objp->lm_object.lightmap_faces[i][t];
4242 ASSERT(LightmapInfoRemap[fp->lmi_handle] != BAD_LMI_INDEX);
4243 cf_WriteShort(ofile, (uint16_t)LightmapInfoRemap[fp->lmi_handle]);
4244
4245 cf_WriteVector(ofile, &fp->rvec);
4246 cf_WriteVector(ofile, &fp->uvec);
4247
4248 cf_WriteByte(ofile, fp->num_verts);
4249
4250 for (k = 0; k < fp->num_verts; k++) {
4251 cf_WriteFloat(ofile, fp->u2[k]);
4252 cf_WriteFloat(ofile, fp->v2[k]);
4253 }
4254 }
4255 }
4256 }
4257 } else
4258 cf_WriteByte(ofile, 0);
4259
4260 return 1;
4261 }
4262
4263 // Writes a trigger
4264 // Returns: true if written ok, else false
4265 int WriteTrigger(CFILE *ofile, trigger *tp) {
4266 cf_WriteString(ofile, tp->name);
4267 cf_WriteShort(ofile, tp->roomnum);
4268 cf_WriteShort(ofile, tp->facenum);
4269 cf_WriteShort(ofile, tp->flags);
4270 cf_WriteShort(ofile, tp->activator);
4271
4272 return 1;
4273 }
4274
4275 // Writes a face to a disk file
4276 // Parameters: ofile - file to write to
4277 // fp - face to write
4278 // Returns: 1 if written ok, else 0
4279 int WriteFace(CFILE *ofile, face *fp) {
4280 int i;
4281
4282 // Write number of verts
4283 cf_WriteByte(ofile, fp->num_verts);
4284
4285 // Verts
4286 for (i = 0; i < fp->num_verts; i++)
4287 cf_WriteShort(ofile, fp->face_verts[i]);
4288
4289 // uvls
4290 fp->flags &= ~FF_VERTEX_ALPHA;
4291
4292 for (i = 0; i < fp->num_verts; i++) {
4293 cf_WriteFloat(ofile, fp->face_uvls[i].u);
4294 cf_WriteFloat(ofile, fp->face_uvls[i].v);
4295 cf_WriteByte(ofile, fp->face_uvls[i].alpha);
4296
4297 if (fp->face_uvls[i].alpha != 255)
4298 fp->flags |= FF_VERTEX_ALPHA;
4299 }
4300
4301 // Other stuff
4302 if ((fp->flags & FF_LIGHTMAP) &&
4303 (fp->lmi_handle == BAD_LMI_INDEX || LightmapInfoRemap[fp->lmi_handle] == BAD_LMI_INDEX)) {
4304 fp->flags &= ~FF_LIGHTMAP;
4305 mprintf(0, "Almost saved a bogus lightmap!\n");
4306 }
4307
4308 cf_WriteShort(ofile, fp->flags);
4309 cf_WriteByte(ofile, fp->portal_num);
4310 cf_WriteShort(ofile, fp->tmap);
4311
4312 if (fp->flags & FF_LIGHTMAP) {
4313 // Write UV2's
4314
4315 ASSERT(LightmapInfoRemap[fp->lmi_handle] != BAD_LMI_INDEX);
4316 cf_WriteShort(ofile, (uint16_t)LightmapInfoRemap[fp->lmi_handle]);
4317
4318 for (i = 0; i < fp->num_verts; i++) {
4319 cf_WriteFloat(ofile, fp->face_uvls[i].u2);
4320 cf_WriteFloat(ofile, fp->face_uvls[i].v2);
4321 }
4322 }
4323
4324 if (fp->light_multiple == 186) {
4325 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
4326 mprintf(0, "Bogus light multiple detected...bashing!\n");
4327 }
4328
4329 cf_WriteByte(ofile, fp->light_multiple);
4330
4331 if (fp->special_handle != BAD_SPECIAL_FACE_INDEX) {
4332 // Write out special face stuff
4333 uint8_t smooth = 0;
4334 cf_WriteByte(ofile, 1);
4335 cf_WriteByte(ofile, SpecialFaces[fp->special_handle].type);
4336 cf_WriteByte(ofile, 4);
4337
4338 if ((GameTextures[fp->tmap].flags & TF_SMOOTH_SPECULAR) &&
4339 (SpecialFaces[fp->special_handle].flags & SFF_SPEC_SMOOTH)) {
4340 smooth = 1;
4341 cf_WriteByte(ofile, 1);
4342 cf_WriteByte(ofile, fp->num_verts);
4343 } else {
4344 cf_WriteByte(ofile, 0);
4345 }
4346
4347 for (i = 0; i < 4; i++) {
4348 cf_WriteVector(ofile, &SpecialFaces[fp->special_handle].spec_instance[i].bright_center);
4349 cf_WriteShort(ofile, SpecialFaces[fp->special_handle].spec_instance[i].bright_color);
4350 }
4351 if (smooth) {
4352 for (i = 0; i < fp->num_verts; i++) {
4353 cf_WriteVector(ofile, &SpecialFaces[fp->special_handle].vertnorms[i]);
4354 }
4355 }
4356 } else
4357 cf_WriteByte(ofile, 0);
4358
4359 return 1;
4360 }
4361
4362 // Writes a portal to a disk file
4363 // Parameters: ofile - file to write to
4364 // pp - portal to write
4365 // Returns: 1 if written ok, else 0
4366 int WritePortal(CFILE *ofile, portal *pp) {
4367 cf_WriteInt(ofile, pp->flags);
4368
4369 cf_WriteShort(ofile, pp->portal_face);
4370
4371 cf_WriteInt(ofile, pp->croom);
4372 cf_WriteInt(ofile, pp->cportal);
4373 cf_WriteShort(ofile, pp->bnode_index);
4374
4375 cf_WriteVector(ofile, &pp->path_pnt);
4376
4377 cf_WriteInt(ofile, pp->combine_master);
4378
4379 return 1;
4380 }
4381
4382 // Writes a room to a disk file
4383 // Parameters: ofile - file to write to
4384 // rp - room to write
4385 // Returns: 1 if written ok, else 0
4386 int WriteRoom(CFILE *ofile, room *rp) {
4387 int i;
4388
4389 // Write counts
4390 cf_WriteInt(ofile, rp->num_verts);
4391 cf_WriteInt(ofile, rp->num_faces);
4392 cf_WriteInt(ofile, rp->num_portals);
4393
4394 // Write name
4395 cf_WriteString(ofile, rp->name ? rp->name : "");
4396
4397 // Write out the path point
4398 cf_WriteVector(ofile, &rp->path_pnt);
4399
4400 // Write verts
4401 for (i = 0; i < rp->num_verts; i++) {
4402 cf_WriteVector(ofile, &rp->verts[i]);
4403 }
4404
4405 // Write faces
4406 for (i = 0; i < rp->num_faces; i++)
4407 WriteFace(ofile, &rp->faces[i]);
4408
4409 // Write portals
4410 for (i = 0; i < rp->num_portals; i++)
4411 WritePortal(ofile, &rp->portals[i]);
4412
4413 // Write rest of data
4414 cf_WriteInt(ofile, rp->flags);
4415
4416 // Write pulse time
4417 cf_WriteByte(ofile, rp->pulse_time);
4418 cf_WriteByte(ofile, rp->pulse_offset);
4419
4420 // Write mirrored face
4421 cf_WriteShort(ofile, rp->mirror_face);
4422
4423 // Save doorway data if this room has a door
4424 if (rp->flags & RF_DOOR) {
4425 doorway *dp = rp->doorway_data;
4426
4427 cf_WriteByte(ofile, dp->flags);
4428 cf_WriteByte(ofile, dp->keys_needed);
4429 cf_WriteInt(ofile, dp->doornum);
4430 cf_WriteFloat(ofile, dp->position);
4431 }
4432
4433 if (!rp->volume_lights)
4434 cf_WriteByte(ofile, 0);
4435 else {
4436 cf_WriteByte(ofile, 1);
4437 int w = rp->volume_width;
4438 int h = rp->volume_height;
4439 int d = rp->volume_depth;
4440
4441 cf_WriteInt(ofile, w);
4442 cf_WriteInt(ofile, h);
4443 cf_WriteInt(ofile, d);
4444
4445 CheckToWriteCompressByte(ofile, rp->volume_lights, w * h * d);
4446 }
4447
4448 cf_WriteFloat(ofile, rp->fog_depth);
4449 cf_WriteFloat(ofile, rp->fog_r);
4450 cf_WriteFloat(ofile, rp->fog_g);
4451 cf_WriteFloat(ofile, rp->fog_b);
4452
4453 // Write ambient sound pattern name
4454 cf_WriteString(ofile, (rp->ambient_sound == -1) ? "" : AmbientSoundPatternName(rp->ambient_sound));
4455
4456 cf_WriteByte(ofile, (int8_t)rp->env_reverb);
4457
4458 // Write damage
4459 cf_WriteFloat(ofile, rp->damage);
4460 cf_WriteByte(ofile, rp->damage_type);
4461
4462 return 1;
4463 }
4464
4465 // Writes a chunk header. Writes chunk id & placeholder length. Returns chunk start pos
4466 int StartChunk(CFILE *ofile, char *chunk_name) {
4467 int chunk_start_pos;
4468
4469 cf_WriteBytes((uint8_t *)chunk_name, 4, ofile);
4470 chunk_start_pos = cftell(ofile);
4471 cf_WriteInt(ofile, 0); // placeholder for chunk len
4472
4473 return chunk_start_pos;
4474 }
4475
4476 // Fill in chunk length when done writing
4477 void EndChunk(CFILE *ofile, int chunk_start_pos) {
4478 int save_pos = cftell(ofile);
4479 int len = save_pos - chunk_start_pos;
4480
4481 // pad the chunk if necessary to make multiple of four bytes
4482 while (len & 3) {
4483 cf_WriteByte(ofile, 0);
4484 len++;
4485 save_pos++;
4486 }
4487
4488 // seek back to len field and fill in value
4489 cfseek(ofile, chunk_start_pos, SEEK_SET);
4490 cf_WriteInt(ofile, len); // write chunk length
4491
4492 // go back to end of file
4493 cfseek(ofile, save_pos, SEEK_SET);
4494 }
4495
4496 void WriteTerrainHeightChunk(CFILE *fp) {
4497 int i;
4498 uint8_t heightvals[TERRAIN_WIDTH * TERRAIN_DEPTH];
4499
4500 int start_pos;
4501 start_pos = StartChunk(fp, CHUNK_TERRAIN_HEIGHT);
4502
4503 for (i = 0; i < TERRAIN_WIDTH * TERRAIN_DEPTH; i++)
4504 heightvals[i] = Terrain_seg[i].ypos;
4505
4506 CheckToWriteCompressByte(fp, heightvals, TERRAIN_WIDTH * TERRAIN_DEPTH);
4507
4508 EndChunk(fp, start_pos);
4509 }
4510
4511 void WriteGamePathsChunk(CFILE *fp) {
4512 int start_pos;
4513 int i, j;
4514
4515 start_pos = StartChunk(fp, CHUNK_GAME_PATHS);
4516
4517 // Get the number of paths
4518 cf_WriteShort(fp, Num_game_paths);
4519
4520 for (i = 0; i < MAX_GAME_PATHS; i++) {
4521 if (GamePaths[i].used) {
4522 // Write out the path's info
4523 cf_WriteString(fp, GamePaths[i].name);
4524 cf_WriteInt(fp, GamePaths[i].num_nodes);
4525 cf_WriteByte(fp, GamePaths[i].flags);
4526
4527 for (j = 0; j < GamePaths[i].num_nodes; j++) {
4528 cf_WriteVector(fp, &GamePaths[i].pathnodes[j].pos);
4529 cf_WriteInt(fp, GamePaths[i].pathnodes[j].roomnum);
4530 cf_WriteInt(fp, GamePaths[i].pathnodes[j].flags);
4531 cf_WriteVector(fp, &GamePaths[i].pathnodes[j].fvec);
4532 cf_WriteVector(fp, &GamePaths[i].pathnodes[j].uvec);
4533 }
4534 }
4535 }
4536
4537 EndChunk(fp, start_pos);
4538 }
4539
4540 void WriteOverrideSoundChunk(CFILE *fp) {
4541 int start_pos;
4542 start_pos = StartChunk(fp, CHUNK_OVERRIDE_SOUNDS);
4543
4544 if (sound_override_force_field != -1) {
4545 cf_WriteString(fp, Sounds[sound_override_force_field].name);
4546 } else {
4547 cf_WriteString(fp, "");
4548 }
4549
4550 if (sound_override_glass_breaking != -1) {
4551 cf_WriteString(fp, Sounds[sound_override_glass_breaking].name);
4552 } else {
4553 cf_WriteString(fp, "");
4554 }
4555
4556 EndChunk(fp, start_pos);
4557 }
4558
4559 void WriteFFTMChunk(CFILE *fp) {
4560 int start_pos;
4561 start_pos = StartChunk(fp, CHUNK_FFT_MOD);
4562
4563 int i;
4564
4565 for (i = 0; i < MAX_FORCE_FIELD_BOUNCE_TEXTURES; i++) {
4566 if (force_field_bounce_texture[i] != -1) {
4567 cf_WriteString(fp, GameTextures[force_field_bounce_texture[i]].name);
4568 cf_WriteFloat(fp, force_field_bounce_multiplier[i]);
4569 } else {
4570 cf_WriteString(fp, "");
4571 }
4572 }
4573
4574 EndChunk(fp, start_pos);
4575 }
4576
4577 void WriteBNodeChunk(CFILE *fp) {
4578 int i;
4579 int j;
4580 int k;
4581
4582 int start_pos;
4583 start_pos = StartChunk(fp, CHUNK_BNODES);
4584
4585 cf_WriteShort(fp, Highest_room_index + 8);
4586
4587 for (i = 0; i <= Highest_room_index + 8; i++) {
4588 if ((i <= Highest_room_index && !Rooms[i].used)) {
4589 cf_WriteByte(fp, 0);
4590 } else {
4591 cf_WriteByte(fp, 1);
4592 bn_list *bnlist = BNode_GetBNListPtr(i, true);
4593
4594 cf_WriteShort(fp, bnlist->num_nodes);
4595 if (bnlist->num_nodes) {
4596 for (j = 0; j < bnlist->num_nodes; j++) {
4597 cf_WriteFloat(fp, bnlist->nodes[j].pos.x);
4598 cf_WriteFloat(fp, bnlist->nodes[j].pos.y);
4599 cf_WriteFloat(fp, bnlist->nodes[j].pos.z);
4600
4601 cf_WriteShort(fp, bnlist->nodes[j].num_edges);
4602 if (bnlist->nodes[j].num_edges) {
4603 for (k = 0; k < bnlist->nodes[j].num_edges; k++) {
4604 cf_WriteShort(fp, bnlist->nodes[j].edges[k].end_room);
4605 cf_WriteByte(fp, bnlist->nodes[j].edges[k].end_index);
4606
4607 cf_WriteShort(fp, bnlist->nodes[j].edges[k].flags);
4608 cf_WriteShort(fp, bnlist->nodes[j].edges[k].cost);
4609
4610 cf_WriteFloat(fp, bnlist->nodes[j].edges[k].max_rad);
4611 }
4612 }
4613 }
4614 }
4615 }
4616 }
4617 cf_WriteByte(fp, BNode_verified ? 1 : 0);
4618
4619 EndChunk(fp, start_pos);
4620 }
4621
4622 void WriteBOAChunk(CFILE *fp) {
4623 int start_pos;
4624 int i, j;
4625
4626 start_pos = StartChunk(fp, CHUNK_BOA);
4627
4628 cf_WriteInt(fp, BOA_mine_checksum);
4629 cf_WriteInt(fp, BOA_vis_checksum);
4630 cf_WriteInt(fp, Highest_room_index + 8);
4631 cf_WriteInt(fp, MAX_PATH_PORTALS);
4632
4633 for (i = 0; i <= Highest_room_index + 8; i++) {
4634 for (j = 0; j <= Highest_room_index + 8; j++) {
4635 cf_WriteShort(fp, BOA_Array[i][j]);
4636 }
4637 }
4638
4639 for (i = 0; i <= Highest_room_index + 8; i++) {
4640 for (j = 0; j < MAX_PATH_PORTALS; j++) {
4641 cf_WriteFloat(fp, BOA_cost_array[i][j]);
4642 }
4643 }
4644
4645 cf_WriteInt(fp, BOA_num_mines);
4646 cf_WriteInt(fp, BOA_num_terrain_regions);
4647
4648 for (i = 0; i < BOA_num_terrain_regions; i++) {
4649 cf_WriteInt(fp, BOA_num_connect[i]);
4650
4651 for (j = 0; j < BOA_num_connect[i]; j++) {
4652 cf_WriteInt(fp, BOA_connect[i][j].roomnum);
4653 cf_WriteInt(fp, BOA_connect[i][j].portal);
4654 }
4655 }
4656
4657 EndChunk(fp, start_pos);
4658 }
4659
4660 void WriteRoomAABBChunk(CFILE *fp) {
4661 int start_pos;
4662 int i;
4663
4664 start_pos = StartChunk(fp, CHUNK_ROOM_AABB);
4665
4666 cf_WriteInt(fp, Highest_room_index);
4667
4668 for (i = 0; i <= Highest_room_index; i++) {
4669 cf_WriteInt(fp, BOA_AABB_ROOM_checksum[i]);
4670 }
4671
4672 for (i = 0; i <= Highest_room_index; i++) {
4673 if (Rooms[i].used) {
4674 int j;
4675 int k;
4676
4677 ASSERT(Rooms[i].bbf_list && Rooms[i].bbf_list_min_xyz && Rooms[i].bbf_list_max_xyz && Rooms[i].bbf_list_sector);
4678
4679 cf_WriteInt(fp, 1); // used
4680
4681 cf_WriteInt(fp, Rooms[i].num_faces);
4682 for (j = 0; j < Rooms[i].num_faces; j++) {
4683 cf_WriteFloat(fp, Rooms[i].faces[j].min_xyz.x);
4684 cf_WriteFloat(fp, Rooms[i].faces[j].min_xyz.y);
4685 cf_WriteFloat(fp, Rooms[i].faces[j].min_xyz.z);
4686
4687 cf_WriteFloat(fp, Rooms[i].faces[j].max_xyz.x);
4688 cf_WriteFloat(fp, Rooms[i].faces[j].max_xyz.y);
4689 cf_WriteFloat(fp, Rooms[i].faces[j].max_xyz.z);
4690 }
4691
4692 cf_WriteInt(fp, BOA_AABB_ROOM_checksum[i]);
4693
4694 cf_WriteFloat(fp, Rooms[i].bbf_min_xyz.x);
4695 cf_WriteFloat(fp, Rooms[i].bbf_min_xyz.y);
4696 cf_WriteFloat(fp, Rooms[i].bbf_min_xyz.z);
4697
4698 cf_WriteFloat(fp, Rooms[i].bbf_max_xyz.x);
4699 cf_WriteFloat(fp, Rooms[i].bbf_max_xyz.y);
4700 cf_WriteFloat(fp, Rooms[i].bbf_max_xyz.z);
4701
4702 cf_WriteShort(fp, Rooms[i].num_bbf_regions);
4703 for (j = 0; j < Rooms[i].num_bbf_regions; j++) {
4704 cf_WriteShort(fp, Rooms[i].num_bbf[j]);
4705 }
4706
4707 for (j = 0; j < Rooms[i].num_bbf_regions; j++) {
4708 for (k = 0; k < Rooms[i].num_bbf[j]; k++) {
4709 cf_WriteShort(fp, Rooms[i].bbf_list[j][k]);
4710 }
4711
4712 cf_WriteFloat(fp, Rooms[i].bbf_list_min_xyz[j].x);
4713 cf_WriteFloat(fp, Rooms[i].bbf_list_min_xyz[j].y);
4714 cf_WriteFloat(fp, Rooms[i].bbf_list_min_xyz[j].z);
4715
4716 cf_WriteFloat(fp, Rooms[i].bbf_list_max_xyz[j].x);
4717 cf_WriteFloat(fp, Rooms[i].bbf_list_max_xyz[j].y);
4718 cf_WriteFloat(fp, Rooms[i].bbf_list_max_xyz[j].z);
4719
4720 cf_WriteByte(fp, Rooms[i].bbf_list_sector[j]);
4721 }
4722 } else {
4723 cf_WriteInt(fp, 0); // Not used
4724 }
4725 }
4726
4727 EndChunk(fp, start_pos);
4728 }
4729
4730 void WriteMatcenChunk(CFILE *fp) {
4731 int start_pos;
4732
4733 start_pos = StartChunk(fp, CHUNK_MATCEN_DATA);
4734
4735 cf_WriteInt(fp, Num_matcens);
4736
4737 int i;
4738
4739 for (i = 0; i < Num_matcens; i++) {
4740 ASSERT(Matcen[i]);
4741 Matcen[i]->Reset(); // Makes it like it hasn't run yet
4742 Matcen[i]->SaveData(fp);
4743 }
4744
4745 EndChunk(fp, start_pos);
4746 }
4747
4748 void WriteALifeChunk(CFILE *fp) {
4749 #ifndef NEWEDITOR
4750 int start_pos;
4751
4752 start_pos = StartChunk(fp, CHUNK_ALIFE_DATA);
4753
4754 a_life.SaveData(fp);
4755
4756 EndChunk(fp, start_pos);
4757 #endif
4758 }
4759
4760 void WriteLevelGoalsChunk(CFILE *fp) {
4761 int start_pos;
4762
4763 start_pos = StartChunk(fp, CHUNK_LEVEL_GOALS);
4764 Level_goals.SaveLevelGoalInfo(fp);
4765 EndChunk(fp, start_pos);
4766 }
4767
4768 void WriteTerrainSkyAndLightChunk(CFILE *fp) {
4769 int i;
4770 int max_sats;
4771
4772 max_sats = Terrain_sky.num_satellites;
4773
4774 int start_pos;
4775 start_pos = StartChunk(fp, CHUNK_TERRAIN_SKY);
4776
4777 cf_WriteFloat(fp, Terrain_sky.fog_scalar);
4778 cf_WriteFloat(fp, Terrain_sky.damage_per_second);
4779
4780 cf_WriteByte(fp, Terrain_sky.textured);
4781
4782 cf_WriteShort(fp, Terrain_sky.dome_texture);
4783
4784 cf_WriteInt(fp, Terrain_sky.sky_color);
4785 cf_WriteInt(fp, Terrain_sky.horizon_color);
4786 cf_WriteInt(fp, Terrain_sky.fog_color);
4787
4788 cf_WriteInt(fp, Terrain_sky.flags);
4789 cf_WriteFloat(fp, Terrain_sky.radius);
4790 cf_WriteFloat(fp, Terrain_sky.rotate_rate);
4791
4792 cf_WriteInt(fp, max_sats);
4793
4794 for (i = 0; i < max_sats; i++) {
4795 cf_WriteShort(fp, Terrain_sky.satellite_texture[i]);
4796 cf_WriteVector(fp, &Terrain_sky.satellite_vectors[i]);
4797 cf_WriteByte(fp, Terrain_sky.satellite_flags[i]);
4798 cf_WriteFloat(fp, Terrain_sky.satellite_size[i]);
4799
4800 cf_WriteFloat(fp, Terrain_sky.satellite_r[i]);
4801 cf_WriteFloat(fp, Terrain_sky.satellite_g[i]);
4802 cf_WriteFloat(fp, Terrain_sky.satellite_b[i]);
4803 }
4804
4805 // Write lighting
4806 uint8_t *byte_vals = (uint8_t *)mem_malloc(TERRAIN_DEPTH * TERRAIN_WIDTH);
4807
4808 // l
4809 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
4810 byte_vals[i] = Terrain_seg[i].l;
4811 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
4812
4813 // r
4814 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
4815 byte_vals[i] = Terrain_seg[i].r;
4816 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
4817
4818 // g
4819 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
4820 byte_vals[i] = Terrain_seg[i].g;
4821 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
4822
4823 // b
4824 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
4825 byte_vals[i] = Terrain_seg[i].b;
4826 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
4827
4828 // Terrain_dynamic_table
4829 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
4830 byte_vals[i] = Terrain_dynamic_table[i];
4831 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
4832
4833 // Terrain occlusion table
4834 cf_WriteInt(fp, Terrain_occlusion_checksum);
4835 for (i = 0; i < OCCLUSION_SIZE * OCCLUSION_SIZE * 32; i++) {
4836 int row = i / 32;
4837 int column = i % 32;
4838
4839 byte_vals[i] = Terrain_occlusion_map[row][column];
4840 }
4841 CheckToWriteCompressByte(fp, byte_vals, OCCLUSION_SIZE * OCCLUSION_SIZE * 32);
4842
4843 mem_free(byte_vals);
4844
4845 EndChunk(fp, start_pos);
4846 }
4847
4848 void WriteTerrainTmapChunk(CFILE *fp) {
4849 int i;
4850
4851 uint8_t *byte_vals = (uint8_t *)mem_malloc(TERRAIN_DEPTH * TERRAIN_WIDTH);
4852 uint16_t *short_vals = (uint16_t *)mem_malloc(2 * TERRAIN_DEPTH * TERRAIN_WIDTH);
4853
4854 int start_pos;
4855 start_pos = StartChunk(fp, CHUNK_TERRAIN_TMAPS_FLAGS);
4856
4857 // Tmap 1s
4858 for (i = 0; i < TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH; i++)
4859 short_vals[i] = Terrain_tex_seg[i].tex_index;
4860 CheckToWriteCompressShort(fp, short_vals, TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH);
4861
4862 // Rotators
4863 for (i = 0; i < TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH; i++)
4864 byte_vals[i] = Terrain_tex_seg[i].rotation;
4865 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_TEX_DEPTH * TERRAIN_TEX_WIDTH);
4866
4867 // flags
4868 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++)
4869 byte_vals[i] = Terrain_seg[i].flags;
4870 CheckToWriteCompressByte(fp, byte_vals, TERRAIN_DEPTH * TERRAIN_WIDTH);
4871
4872 mem_free(byte_vals);
4873 mem_free(short_vals);
4874
4875 EndChunk(fp, start_pos);
4876 }
4877
4878 void WriteTerrainChunks(CFILE *outfile) {
4879 WriteTerrainHeightChunk(outfile);
4880 WriteTerrainTmapChunk(outfile);
4881 WriteTerrainSkyAndLightChunk(outfile);
4882
4883 // Write out end chunk
4884 int start_pos = StartChunk(outfile, CHUNK_TERRAIN_END);
4885 EndChunk(outfile, start_pos);
4886 }
4887
4888 void WriteLightmapChunk(CFILE *fp) {
4889 int start_pos;
4890 int i;
4891 int lightmap_info_count = 0;
4892 int lightmap_count = 0;
4893
4894 uint16_t *lightmap_remap = (uint16_t *)mem_malloc(MAX_LIGHTMAPS * sizeof(uint16_t));
4895 uint8_t *lightmap_spoken_for = (uint8_t *)mem_malloc(MAX_LIGHTMAPS);
4896
4897 ASSERT(lightmap_remap);
4898 ASSERT(lightmap_spoken_for);
4899
4900 memset(lightmap_spoken_for, 0, MAX_LIGHTMAPS);
4901
4902 // Build a lightmap remap list
4903 for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) {
4904 if (LightmapInfo[i].used && LightmapInfo[i].type != LMI_DYNAMIC) {
4905 if (!lightmap_spoken_for[LightmapInfo[i].lm_handle]) {
4906 lightmap_spoken_for[LightmapInfo[i].lm_handle] = 1;
4907 lightmap_remap[LightmapInfo[i].lm_handle] = lightmap_count;
4908
4909 lightmap_count++;
4910 }
4911
4912 LightmapInfoRemap[i] = lightmap_info_count;
4913 lightmap_info_count++;
4914 } else
4915 LightmapInfoRemap[i] = BAD_LMI_INDEX;
4916 }
4917
4918 memset(lightmap_spoken_for, 0, MAX_LIGHTMAPS);
4919 start_pos = StartChunk(fp, CHUNK_NEW_LIGHTMAPS);
4920
4921 cf_WriteInt(fp, lightmap_count);
4922
4923 // Write out lightmap texture data
4924 for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) {
4925 if (LightmapInfo[i].used && LightmapInfo[i].type != LMI_DYNAMIC) {
4926 if (!lightmap_spoken_for[LightmapInfo[i].lm_handle]) {
4927 lightmap_spoken_for[LightmapInfo[i].lm_handle] = 1;
4928 int w = lm_w(LightmapInfo[i].lm_handle);
4929 int h = lm_h(LightmapInfo[i].lm_handle);
4930
4931 ASSERT(w >= 2 && w <= 128);
4932 ASSERT(h >= 2 && h <= 128);
4933
4934 cf_WriteShort(fp, w);
4935 cf_WriteShort(fp, h);
4936
4937 uint16_t *data = lm_data(LightmapInfo[i].lm_handle);
4938 ASSERT(data != NULL);
4939
4940 CheckToWriteCompressShort(fp, data, w * h);
4941 }
4942 }
4943 }
4944
4945 cf_WriteInt(fp, lightmap_info_count);
4946
4947 for (i = 0; i < MAX_LIGHTMAP_INFOS; i++) {
4948 if (LightmapInfo[i].used && LightmapInfo[i].type != LMI_DYNAMIC) {
4949 cf_WriteShort(fp, lightmap_remap[LightmapInfo[i].lm_handle]);
4950 int w = lmi_w(i);
4951 int h = lmi_h(i);
4952
4953 cf_WriteShort(fp, w);
4954 cf_WriteShort(fp, h);
4955
4956 cf_WriteByte(fp, LightmapInfo[i].type);
4957
4958 cf_WriteShort(fp, LightmapInfo[i].x1);
4959 cf_WriteShort(fp, LightmapInfo[i].y1);
4960
4961 cf_WriteByte(fp, LightmapInfo[i].xspacing);
4962 cf_WriteByte(fp, LightmapInfo[i].yspacing);
4963
4964 cf_WriteVector(fp, &LightmapInfo[i].upper_left);
4965 cf_WriteVector(fp, &LightmapInfo[i].normal);
4966 }
4967 }
4968
4969 mem_free(lightmap_remap);
4970 mem_free(lightmap_spoken_for);
4971
4972 EndChunk(fp, start_pos);
4973 }
4974
4975 // Macro to write out names of a certain data type
4976 #define WRITE_DATA_NAMES(max_items, item_array) \
4977 do { \
4978 highest_used_index = -1; /* Find index of highest used */ \
4979 for (i = 0; i < max_items; i++) \
4980 if (item_array[i].used) \
4981 highest_used_index = i; \
4982 cf_WriteInt(ofile, highest_used_index + 1); \
4983 for (i = 0; i <= highest_used_index; i++) \
4984 cf_WriteString(ofile, item_array[i].used ? item_array[i].name : ""); \
4985 } while (0)
4986
4987 // Macro to write out names of a certain data type
4988 #define WRITE_DATA_NAMES_GENERIC(max_items, item_array, chunk_name) \
4989 do { \
4990 int chunk_start_pos; \
4991 chunk_start_pos = StartChunk(ofile, chunk_name); \
4992 highest_used_index = -1; /* Find index of highest used */ \
4993 for (i = 0; i < max_items; i++) \
4994 if (item_array[i].type != OBJ_NONE) \
4995 highest_used_index = i; \
4996 cf_WriteInt(ofile, highest_used_index + 1); \
4997 for (i = 0; i <= highest_used_index; i++) \
4998 cf_WriteString(ofile, (item_array[i].type != OBJ_NONE) ? item_array[i].name : ""); \
4999 EndChunk(ofile, chunk_start_pos); \
5000 } while (0)
5001
5002 // Write the texture names for remapping when level is loaded
5003 void WriteTextureList(CFILE *ofile) {
5004 int i, highest_used_index;
5005
5006 WRITE_DATA_NAMES(MAX_TEXTURES, GameTextures);
5007 }
5008
5009 void WritePlayerStarts(CFILE *outfile) {
5010 int i;
5011
5012 cf_WriteShort(outfile, MAX_PLAYERS);
5013
5014 for (i = 0; i < MAX_PLAYERS; i++) {
5015 cf_WriteInt(outfile, Players[i].startpos_flags);
5016 }
5017 }
5018
5019 #include "editor/ebnode.h"
5020
5021 // Save a level file
5022 // Returns 1 if file saved ok, else 0
5023 int SaveLevel(char *filename, bool f_save_room_AABB) {
5024 CFILE *ofile;
5025 int i, highest_used_index;
5026 int chunk_start_pos;
5027 int count;
5028
5029 // Check for too many path portals
5030 for (int r = 0; r <= Highest_room_index; r++) {
5031 if (Rooms[r].num_portals > MAX_PATH_PORTALS)
5032 if (EditorMessageBox(MBOX_YESNO,
5033 "Room %d has %d portals, which is more than the %d portals that the path system allows."
5034 "\n\nDo you want to save anyway?",
5035 r, MAX_PATH_PORTALS) != IDYES)
5036 return 0;
5037 }
5038 #ifndef NEWEDITOR
5039 // Get the level ready to save
5040 ClearTransientObjects(1); // 1 means clear proximity bombs
5041 #endif
5042 // CompressMine(); //get rid of holes in room list
5043 MakeBOA();
5044 EBNode_VerifyGraph();
5045 ConsolidateMineMirrors();
5046
5047 if (f_save_room_AABB) {
5048 // Compute the bounding boxes
5049 ComputeAABB(true);
5050 }
5051
5052 // Find shells for all the rooms
5053 if (!FindArg("-noshells"))
5054 ComputeAllRoomShells();
5055
5056 ofile = cfopen(filename, "wb");
5057
5058 if (!ofile) {
5059 EditorMessageBox("Can't open file <%s> for writing: %s.", filename, sys_errlist[errno]);
5060 return 0;
5061 }
5062
5063 try { // catch cfile errors
5064
5065 // Write tag & version number
5066 cf_WriteBytes((uint8_t *)LEVEL_FILE_TAG, 4, ofile);
5067 cf_WriteInt(ofile, LEVEL_FILE_VERSION);
5068
5069 WriteGamePathsChunk(ofile);
5070
5071 // Write terrain sounds
5072 chunk_start_pos = StartChunk(ofile, CHUNK_TERRAIN_SOUND);
5073 int n_bands = 0, b;
5074 for (b = 0; b < NUM_TERRAIN_SOUND_BANDS; b++)
5075 if (Terrain_sound_bands[b].sound_index != -1)
5076 n_bands++;
5077 cf_WriteInt(ofile, n_bands);
5078 for (b = 0; b < NUM_TERRAIN_SOUND_BANDS; b++) {
5079 if (Terrain_sound_bands[b].sound_index != -1) {
5080 cf_WriteString(ofile, Sounds[Terrain_sound_bands[b].sound_index].name);
5081 cf_WriteByte(ofile, Terrain_sound_bands[b].low_alt);
5082 cf_WriteByte(ofile, Terrain_sound_bands[b].high_alt);
5083 cf_WriteFloat(ofile, Terrain_sound_bands[b].low_volume);
5084 cf_WriteFloat(ofile, Terrain_sound_bands[b].high_volume);
5085 }
5086 }
5087 EndChunk(ofile, chunk_start_pos);
5088
5089 // Write lightmaps
5090 // NOTE: This WriteLightmapChunk must appear before the object,room,or terrain chunks get
5091 // written out!!!!
5092 WriteLightmapChunk(ofile);
5093
5094 chunk_start_pos = StartChunk(ofile, CHUNK_PLAYER_STARTS);
5095 WritePlayerStarts(ofile);
5096 EndChunk(ofile, chunk_start_pos);
5097
5098 // Write the texture names for remapping when level is loaded
5099 chunk_start_pos = StartChunk(ofile, CHUNK_TEXTURE_NAMES);
5100 WriteTextureList(ofile);
5101 EndChunk(ofile, chunk_start_pos);
5102
5103 // Write the object names for remapping when level is loaded
5104 WRITE_DATA_NAMES_GENERIC(MAX_OBJECT_IDS, Object_info, CHUNK_GENERIC_NAMES);
5105
5106 // Write the door names for remapping when level is loaded
5107 chunk_start_pos = StartChunk(ofile, CHUNK_DOOR_NAMES);
5108 WRITE_DATA_NAMES(MAX_DOORS, Doors);
5109 EndChunk(ofile, chunk_start_pos);
5110
5111 // Write rooms
5112 chunk_start_pos = StartChunk(ofile, CHUNK_ROOMS);
5113
5114 // Count & write out number of rooms
5115
5116 // Count * write number of rooms, verts, faces, etc.
5117 room *rp;
5118 int nrooms = 0, nverts = 0, nfaces = 0, nfaceverts = 0, nportals = 0;
5119 extern int CountRoomFaceVerts(room * rp); // TEMP: move to room.h
5120 for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) // Count the number of faces, verts, etc. in the level
5121 if (rp->used) {
5122 nrooms++;
5123 nverts += rp->num_verts;
5124 nfaces += rp->num_faces;
5125 nfaceverts += CountRoomFaceVerts(rp);
5126 nportals += rp->num_portals;
5127 }
5128 cf_WriteInt(ofile, nrooms);
5129 cf_WriteInt(ofile, nverts);
5130 cf_WriteInt(ofile, nfaces);
5131 cf_WriteInt(ofile, nfaceverts);
5132 cf_WriteInt(ofile, nportals);
5133
5134 // Write out the rooms
5135 for (i = 0; i <= Highest_room_index; i++) {
5136 if (Rooms[i].used) {
5137 cf_WriteShort(ofile, i); // write out room number
5138 WriteRoom(ofile, &Rooms[i]);
5139 }
5140 }
5141
5142 // Done with rooms
5143 EndChunk(ofile, chunk_start_pos);
5144
5145 // Write room wind, if any rooms have wind
5146 for (i = nrooms = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) // Count the number of rooms with wind
5147 if ((rp->wind.x != 0.0) || (rp->wind.y != 0.0) || (rp->wind.z != 0.0))
5148 nrooms++;
5149 if (nrooms) {
5150 chunk_start_pos = StartChunk(ofile, CHUNK_ROOM_WIND);
5151 cf_WriteInt(ofile, nrooms);
5152 for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) { // write the wind values
5153 if ((rp->wind.x != 0.0) || (rp->wind.y != 0.0) || (rp->wind.z != 0.0)) {
5154 cf_WriteShort(ofile, i);
5155 cf_WriteVector(ofile, &rp->wind);
5156 }
5157 }
5158 EndChunk(ofile, chunk_start_pos);
5159 }
5160
5161 // Write terrain
5162 chunk_start_pos = StartChunk(ofile, CHUNK_TERRAIN);
5163 WriteTerrainChunks(ofile);
5164 EndChunk(ofile, chunk_start_pos);
5165
5166 // count the number of deleted object handles we need to write
5167 for (i = 0, count = 0; i < MAX_OBJECTS; i++)
5168 if ((Objects[i].type == OBJ_NONE) && ((Objects[i].handle & HANDLE_COUNT_MASK) != 0))
5169 count++;
5170
5171 // write object handles for deleted objects with non-zero handles
5172 chunk_start_pos = StartChunk(ofile, CHUNK_OBJECT_HANDLES);
5173 cf_WriteInt(ofile, count);
5174 for (i = 0; i < MAX_OBJECTS; i++)
5175 if ((Objects[i].type == OBJ_NONE) && ((Objects[i].handle & HANDLE_COUNT_MASK) != 0))
5176 cf_WriteInt(ofile, Objects[i].handle);
5177 EndChunk(ofile, chunk_start_pos);
5178
5179 // count the objects
5180 for (i = 0, count = 0; i <= Highest_object_index; i++)
5181 if ((Objects[i].type != OBJ_NONE) && (Objects[i].type != OBJ_ROOM))
5182 count++;
5183
5184 // write objects
5185 chunk_start_pos = StartChunk(ofile, CHUNK_OBJECTS);
5186 cf_WriteInt(ofile, count);
5187 for (i = 0; i <= Highest_object_index; i++) {
5188 if ((Objects[i].type != OBJ_NONE) && (Objects[i].type != OBJ_ROOM)) {
5189 cf_WriteInt(ofile, Objects[i].handle);
5190 WriteObject(ofile, &Objects[i]);
5191 }
5192 }
5193 EndChunk(ofile, chunk_start_pos);
5194
5195 // Write triggers
5196 chunk_start_pos = StartChunk(ofile, CHUNK_TRIGGERS);
5197 cf_WriteInt(ofile, Num_triggers);
5198 for (i = 0; i < Num_triggers; i++) {
5199 WriteTrigger(ofile, &Triggers[i]);
5200 }
5201 EndChunk(ofile, chunk_start_pos);
5202
5203 #ifndef NEWEDITOR
5204 // Save BSP info (if there is any)
5205 if (BSP_initted) {
5206 chunk_start_pos = StartChunk(ofile, CHUNK_NEW_BSP);
5207 cf_WriteInt(ofile, BSPChecksum);
5208
5209 SaveBSPNode(ofile, MineBSP.root);
5210
5211 EndChunk(ofile, chunk_start_pos);
5212 }
5213 #endif
5214
5215 // This has to be after all BOA related fields; so, a read level can build boa on the
5216 // spot if necessary.
5217 WriteBOAChunk(ofile);
5218
5219 if (BNode_allocated) {
5220 WriteBNodeChunk(ofile);
5221 }
5222
5223 #ifndef NEWEDITOR // for compatibility with Descent 3 v1.2 and earlier
5224 WriteOverrideSoundChunk(ofile);
5225 WriteFFTMChunk(ofile);
5226 #endif
5227
5228 if (f_save_room_AABB) {
5229 WriteRoomAABBChunk(ofile);
5230 }
5231
5232 // Matcen Information
5233 WriteMatcenChunk(ofile);
5234
5235 // Level Goal Information
5236 WriteLevelGoalsChunk(ofile);
5237
5238 // Ambient life Info
5239 WriteALifeChunk(ofile);
5240
5241 // Write level info
5242 chunk_start_pos = StartChunk(ofile, CHUNK_LEVEL_INFO);
5243 cf_WriteString(ofile, Level_info.name);
5244 cf_WriteString(ofile, Level_info.designer);
5245 cf_WriteString(ofile, Level_info.copyright);
5246 cf_WriteString(ofile, Level_info.notes);
5247 cf_WriteFloat(ofile, Gravity_strength);
5248 #ifndef NEWEDITOR // for compatibility with Descent 3 v1.2 and earlier
5249 cf_WriteInt(ofile, (int)Level_powerups_ignore_wind);
5250 #endif
5251 cf_WriteInt(ofile, (int)FVI_always_check_ceiling);
5252 cf_WriteFloat(ofile, Ceiling_height);
5253 EndChunk(ofile, chunk_start_pos);
5254
5255 //
5256 // Write editor info. All editor info should be at the end of the file
5257 //
5258
5259 // Start chunk
5260 chunk_start_pos = StartChunk(ofile, CHUNK_EDITOR_INFO);
5261
5262 // Save current and marked rooms
5263 cf_WriteShort(ofile, ROOMNUM(Curroomp));
5264 cf_WriteShort(ofile, Curface);
5265 cf_WriteShort(ofile, Curedge);
5266 cf_WriteShort(ofile, Curvert);
5267 cf_WriteShort(ofile, Markedroomp ? ROOMNUM(Markedroomp) : -1);
5268 cf_WriteShort(ofile, Markedface);
5269 cf_WriteShort(ofile, Markededge);
5270 cf_WriteShort(ofile, Markedvert);
5271
5272 // Save selected list
5273 cf_WriteInt(ofile, N_selected_rooms);
5274 for (i = 0; i < N_selected_rooms; i++)
5275 cf_WriteShort(ofile, Selected_rooms[i]); // save as shorts to save disk space
5276
5277 // Save several other variables
5278 cf_WriteInt(ofile, Cur_object_index);
5279 cf_WriteInt(ofile, Current_trigger);
5280 cf_WriteInt(ofile, Editor_view_mode);
5281 cf_WriteInt(ofile, Editor_viewer_id);
5282
5283 // Save wireframe view info
5284 cf_WriteVector(ofile, &Wireframe_view_mine.target);
5285 cf_WriteMatrix(ofile, &Wireframe_view_mine.orient);
5286 cf_WriteFloat(ofile, Wireframe_view_mine.dist);
5287
5288 // write out room multiplier
5289 for (i = 0; i < MAX_ROOMS; i++) {
5290 cf_WriteFloat(ofile, Room_multiplier[i]);
5291 cf_WriteFloat(ofile, Room_ambience_r[i]);
5292 cf_WriteFloat(ofile, Room_ambience_g[i]);
5293 cf_WriteFloat(ofile, Room_ambience_b[i]);
5294 }
5295
5296 // Write lighting spacing
5297 cf_WriteInt(ofile, LightSpacing);
5298 cf_WriteFloat(ofile, GlobalMultiplier);
5299 cf_WriteFloat(ofile, Ambient_red);
5300 cf_WriteFloat(ofile, Ambient_green);
5301 cf_WriteFloat(ofile, Ambient_blue);
5302 cf_WriteInt(ofile, rad_MaxStep);
5303
5304 // Close chunk
5305 EndChunk(ofile, chunk_start_pos);
5306
5307 } catch (cfile_error *cfe) {
5308 mprintf(0, "Error writing: file = <%s>, msg = \"%s\"\n", cfe->file->name, cfe->msg);
5309 ASSERT(cfe->read_write == CFE_WRITING);
5310 EditorMessageBox("Error writing file %s: %s", cfe->file->name, cfe->msg);
5311 cfclose(ofile);
5312 return 0;
5313 }
5314
5315 // Close the file
5316 cfclose(ofile);
5317
5318 return 1;
5319 }
5320
5321 #endif // ifdef EDITOR
5322
5323 #ifndef NEWEDITOR
5324
5325 int need_to_page_in = 0;
5326 int need_to_page_num = 0;
5327 void AlmostPageInGeneric(int id);
5328
5329 uint8_t texture_counted[MAX_BITMAPS];
5330 uint8_t sound_counted[MAX_SOUNDS];
5331 uint8_t poly_counted[MAX_POLY_MODELS];
5332
5333 void AlmostPageInLevelTexture(int id) {
5334 if (id == -1 || id == 0)
5335 return;
5336
5337 if ((texture_counted[id] == 0) && (GameBitmaps[id].flags & BF_NOT_RESIDENT)) {
5338 texture_counted[id] = 1;
5339 need_to_page_num++;
5340 /*
5341 //Get the size and add it to the count
5342 CFILE * infile = cfopen (GameBitmaps[id].name,"rb");
5343 //Get the size and add it to the count
5344 if(infile)
5345 {
5346 need_to_page_in += cfilelength(infile);
5347 cfclose(infile);
5348 }
5349 */
5350 if (GameTextures[id].flags & TF_DESTROYABLE && GameTextures[id].destroy_handle != -1)
5351 AlmostPageInLevelTexture(GameTextures[id].destroy_handle);
5352 }
5353 }
5354
5355 void AlmostPageInSound(int id) {
5356 if (id == -1)
5357 return;
5358
5359 // Check if the sample data is already loaded
5360 if (SoundFiles[id].sample_16bit != NULL || SoundFiles[id].sample_8bit != NULL || sound_counted[id])
5361 return;
5362 sound_counted[id] = 1;
5363 need_to_page_num++;
5364 /*
5365 //Check for space this sound takes up, and add it to our counter
5366 CFILE * infile = cfopen (SoundFiles[id].name,"rb");
5367 //Get the size and add it to the count
5368 if(infile)
5369 {
5370 need_to_page_in += cfilelength(infile);
5371 cfclose(infile);
5372 }
5373 */
5374 }
5375
5376 void AlmostPageInPolymodel(int num) {
5377 if ((poly_counted[num] == 0) && (Poly_models[num].flags & PMF_NOT_RESIDENT)) {
5378 poly_counted[num] = 1;
5379 need_to_page_num++;
5380 /*
5381 CFILE * infile = cfopen (Poly_models[num].name,"rb");
5382 //Get the size and add it to the count
5383 if(infile)
5384 {
5385 need_to_page_in += cfilelength(infile);
5386 cfclose(infile);
5387 }
5388 */
5389 }
5390 }
5391
5392 void AlmostPageInDoor(int id) {
5393 // Set sounds
5394 door *doorpointer = &Doors[id];
5395
5396 AlmostPageInPolymodel(doorpointer->model_handle);
5397
5398 poly_model *pm = &Poly_models[doorpointer->model_handle];
5399 for (int t = 0; t < pm->n_textures; t++)
5400 AlmostPageInLevelTexture(pm->textures[t]);
5401
5402 if (doorpointer->open_sound != -1 && doorpointer->open_sound != SOUND_NONE_INDEX)
5403 AlmostPageInSound(doorpointer->open_sound);
5404 if (doorpointer->close_sound != -1 && doorpointer->close_sound != SOUND_NONE_INDEX)
5405 AlmostPageInSound(doorpointer->close_sound);
5406 }
5407
5408 void AlmostPageInWeapon(int id) {
5409 weapon *weaponpointer = &Weapons[id];
5410
5411 if (id == -1)
5412 return;
5413
5414 int i;
5415
5416 if (!(weaponpointer->flags & (WF_IMAGE_BITMAP | WF_IMAGE_VCLIP))) {
5417 AlmostPageInPolymodel(weaponpointer->fire_image_handle);
5418
5419 poly_model *pm = &Poly_models[weaponpointer->fire_image_handle];
5420 for (int t = 0; t < pm->n_textures; t++)
5421 AlmostPageInLevelTexture(pm->textures[t]);
5422 }
5423
5424 // Load the various textures associated with this weapon
5425 if (weaponpointer->explode_image_handle != -1) {
5426 AlmostPageInLevelTexture(weaponpointer->explode_image_handle);
5427 }
5428
5429 if (weaponpointer->particle_handle != -1) {
5430 AlmostPageInLevelTexture(weaponpointer->particle_handle);
5431 }
5432
5433 if (weaponpointer->smoke_handle != -1) {
5434 AlmostPageInLevelTexture(weaponpointer->smoke_handle);
5435 }
5436
5437 if (weaponpointer->scorch_handle != -1) {
5438 AlmostPageInLevelTexture(weaponpointer->scorch_handle);
5439 }
5440
5441 if (weaponpointer->icon_handle != -1) {
5442 AlmostPageInLevelTexture(weaponpointer->icon_handle);
5443 }
5444
5445 // Try to load spawn weapons
5446 if (weaponpointer->spawn_handle != -1 && weaponpointer->spawn_count > 0 && weaponpointer->spawn_handle != id) {
5447 AlmostPageInWeapon(weaponpointer->spawn_handle);
5448 }
5449
5450 if (weaponpointer->alternate_spawn_handle != -1 && weaponpointer->spawn_count > 0 &&
5451 weaponpointer->alternate_spawn_handle != id) {
5452 AlmostPageInWeapon(weaponpointer->alternate_spawn_handle);
5453 }
5454
5455 if (weaponpointer->robot_spawn_handle != -1) {
5456 AlmostPageInGeneric(weaponpointer->robot_spawn_handle);
5457 }
5458
5459 // Try and load the various sounds
5460 for (i = 0; i < MAX_WEAPON_SOUNDS; i++) {
5461 if (weaponpointer->sounds[i] != SOUND_NONE_INDEX) {
5462 AlmostPageInSound(weaponpointer->sounds[i]);
5463 }
5464 }
5465 }
5466
5467 void AlmostPageInShip(int id) {
5468 int i, t;
5469
5470 ship *shippointer = &Ships[id];
5471
5472 // Page in all textures for this object
5473
5474 AlmostPageInPolymodel(shippointer->model_handle);
5475
5476 poly_model *pm = &Poly_models[shippointer->model_handle];
5477
5478 for (t = 0; t < pm->n_textures; t++)
5479 AlmostPageInLevelTexture(pm->textures[t]);
5480
5481 if (shippointer->med_render_handle != -1) {
5482 AlmostPageInPolymodel(shippointer->med_render_handle);
5483
5484 pm = &Poly_models[shippointer->med_render_handle];
5485 for (t = 0; t < pm->n_textures; t++)
5486 AlmostPageInLevelTexture(pm->textures[t]);
5487 }
5488
5489 if (shippointer->lo_render_handle != -1) {
5490 AlmostPageInPolymodel(shippointer->lo_render_handle);
5491
5492 pm = &Poly_models[shippointer->lo_render_handle];
5493 for (t = 0; t < pm->n_textures; t++)
5494 AlmostPageInLevelTexture(pm->textures[t]);
5495 }
5496
5497 if (shippointer->dying_model_handle != -1) {
5498 AlmostPageInPolymodel(shippointer->dying_model_handle);
5499
5500 pm = &Poly_models[shippointer->dying_model_handle];
5501 for (t = 0; t < pm->n_textures; t++)
5502 AlmostPageInLevelTexture(pm->textures[t]);
5503 }
5504
5505 // Try and load the various weapons
5506 int j;
5507 for (i = 0; i < MAX_PLAYER_WEAPONS; i++) {
5508 for (j = 0; j < MAX_WB_GUNPOINTS; j++) {
5509 if (shippointer->static_wb[i].gp_weapon_index[j] != LASER_INDEX) {
5510 AlmostPageInWeapon(shippointer->static_wb[i].gp_weapon_index[j]);
5511 }
5512 }
5513 }
5514
5515 // Try and load the various weapons
5516 for (i = 0; i < MAX_PLAYER_WEAPONS; i++) {
5517 for (j = 0; j < MAX_WB_FIRING_MASKS; j++) {
5518 if (shippointer->static_wb[i].fm_fire_sound_index[j] != SOUND_NONE_INDEX)
5519 AlmostPageInSound(shippointer->static_wb[i].fm_fire_sound_index[j]);
5520 }
5521 }
5522
5523 for (i = 0; i < MAX_PLAYER_WEAPONS; i++) {
5524 if (shippointer->firing_sound[i] != -1)
5525 AlmostPageInSound(shippointer->firing_sound[i]);
5526
5527 if (shippointer->firing_release_sound[i] != -1)
5528 AlmostPageInSound(shippointer->firing_release_sound[i]);
5529
5530 if (shippointer->spew_powerup[i] != -1)
5531 AlmostPageInGeneric(shippointer->spew_powerup[i]);
5532 }
5533 }
5534
5535 void AlmostPageInGeneric(int id) {
5536 int i, t;
5537
5538 if (id == -1)
5539 return;
5540
5541 object_info *objinfopointer = &Object_info[id];
5542
5543 // Page in all textures for this object
5544
5545 AlmostPageInPolymodel(objinfopointer->render_handle);
5546
5547 poly_model *pm = &Poly_models[objinfopointer->render_handle];
5548
5549 for (t = 0; t < pm->n_textures; t++)
5550 AlmostPageInLevelTexture(pm->textures[t]);
5551
5552 if (objinfopointer->med_render_handle != -1) {
5553 AlmostPageInPolymodel(objinfopointer->med_render_handle);
5554
5555 pm = &Poly_models[objinfopointer->med_render_handle];
5556 for (t = 0; t < pm->n_textures; t++)
5557 AlmostPageInLevelTexture(pm->textures[t]);
5558 }
5559
5560 if (objinfopointer->lo_render_handle != -1) {
5561 AlmostPageInPolymodel(objinfopointer->lo_render_handle);
5562
5563 pm = &Poly_models[objinfopointer->lo_render_handle];
5564 for (t = 0; t < pm->n_textures; t++)
5565 AlmostPageInLevelTexture(pm->textures[t]);
5566 }
5567
5568 // Process all sounds for this object
5569 for (i = 0; i < MAX_OBJ_SOUNDS; i++) {
5570 if (objinfopointer->sounds[i] != SOUND_NONE_INDEX) {
5571 AlmostPageInSound(objinfopointer->sounds[i]);
5572 }
5573 }
5574
5575 if (objinfopointer->ai_info) {
5576 for (i = 0; i < MAX_AI_SOUNDS; i++) {
5577 if (objinfopointer->ai_info->sound[i] != SOUND_NONE_INDEX) {
5578 AlmostPageInSound(objinfopointer->ai_info->sound[i]);
5579 }
5580 }
5581 }
5582
5583 // Try and load the various wb sounds
5584 int j;
5585 if (objinfopointer->static_wb) {
5586 for (i = 0; i < MAX_WBS_PER_OBJ; i++) {
5587 for (j = 0; j < MAX_WB_FIRING_MASKS; j++) {
5588 if (objinfopointer->static_wb[i].fm_fire_sound_index[j] != SOUND_NONE_INDEX) {
5589 AlmostPageInSound(objinfopointer->static_wb[i].fm_fire_sound_index[j]);
5590 }
5591 }
5592 }
5593 }
5594
5595 // Try and load the various wb sounds
5596 if (objinfopointer->anim) {
5597 for (i = 0; i < NUM_MOVEMENT_CLASSES; i++) {
5598 for (j = 0; j < NUM_ANIMS_PER_CLASS; j++) {
5599 if (objinfopointer->anim[i].elem[j].anim_sound_index != SOUND_NONE_INDEX) {
5600 AlmostPageInSound(objinfopointer->anim[i].elem[j].anim_sound_index);
5601 }
5602 }
5603 }
5604 }
5605
5606 // Load the spew types
5607 for (i = 0; i < MAX_DSPEW_TYPES; i++) {
5608 if (objinfopointer->dspew_number[i] > 0 && objinfopointer->dspew[i] != 0 && objinfopointer->dspew[i] != id) {
5609 AlmostPageInGeneric(objinfopointer->dspew[i]);
5610 }
5611 }
5612
5613 // Try and load the various weapons
5614
5615 if (objinfopointer->static_wb) {
5616 // Automatically include laser
5617 AlmostPageInWeapon(LASER_INDEX);
5618
5619 for (i = 0; i < MAX_WBS_PER_OBJ; i++) {
5620 for (j = 0; j < MAX_WB_GUNPOINTS; j++) {
5621 if (objinfopointer->static_wb[i].gp_weapon_index[j] != LASER_INDEX) {
5622 AlmostPageInWeapon(objinfopointer->static_wb[i].gp_weapon_index[j]);
5623 }
5624 }
5625 }
5626 }
5627 }
5628
5629 extern const char *Static_sound_names[];
5630 void AlmostPageInAllData() {
5631 int i;
5632
5633 AlmostPageInShip(Players[Player_num].ship_index);
5634 AlmostPageInLevelTexture(FindTextureName("LightFlareStar"));
5635 AlmostPageInLevelTexture(FindTextureName("LightFlare"));
5636
5637 // mprintf(0,"%d bytes to page in for the ship.\n",need_to_page_in);
5638 // Get static fireballs
5639 for (i = 0; i < NUM_FIREBALLS; i++) {
5640 char name[PAGENAME_LEN];
5641 strcpy(name, Fireballs[i].name);
5642
5643 name[strlen(name) - 4] = 0;
5644 int id = IGNORE_TABLE(FindTextureName(name));
5645 if (id != -1)
5646 AlmostPageInLevelTexture(id);
5647 }
5648 // mprintf(0,"%d bytes to page in for fireballs.\n",need_to_page_in);
5649 // Get static sounds
5650 for (i = 0; i < NUM_STATIC_SOUNDS; i++) {
5651 int sid = FindSoundName(IGNORE_TABLE(Static_sound_names[i]));
5652
5653 if (sid != -1)
5654 AlmostPageInSound(sid);
5655 }
5656 // mprintf(0,"%d bytes to page in for static sounds.\n",need_to_page_in);
5657 // First get textures
5658 for (i = 0; i <= Highest_room_index; i++) {
5659 if (!Rooms[i].used)
5660 continue;
5661
5662 room *rp = &Rooms[i];
5663 for (int t = 0; t < rp->num_faces; t++) {
5664 AlmostPageInLevelTexture(rp->faces[t].tmap);
5665 }
5666 }
5667 // mprintf(0,"%d bytes to page in for room textures.\n",need_to_page_in);
5668 // Touch all terrain textures
5669 for (i = 0; i < TERRAIN_TEX_WIDTH * TERRAIN_TEX_DEPTH; i++) {
5670 AlmostPageInLevelTexture(Terrain_tex_seg[i].tex_index);
5671 }
5672
5673 if (Terrain_sky.textured) {
5674 AlmostPageInLevelTexture(Terrain_sky.dome_texture);
5675 }
5676
5677 for (i = 0; i < Terrain_sky.num_satellites; i++)
5678 AlmostPageInLevelTexture(Terrain_sky.satellite_texture[i]);
5679 // mprintf(0,"%d bytes to page in for the terrain.\n",need_to_page_in);
5680 // Touch all objects
5681 for (i = 0; i <= Highest_object_index; i++) {
5682 object *obj = &Objects[i];
5683
5684 if (obj->type == OBJ_POWERUP || obj->type == OBJ_ROBOT || obj->type == OBJ_CLUTTER || obj->type == OBJ_BUILDING) {
5685 AlmostPageInGeneric(obj->id);
5686 continue;
5687 }
5688
5689 if (obj->type == OBJ_DOOR) {
5690 AlmostPageInDoor(obj->id);
5691 continue;
5692 }
5693 }
5694 // mprintf(0,"%d bytes to page in for objects.\n",need_to_page_in);
5695 }
5696
5697 // Go through all the data needing to be paged in, add it all up.
5698 int CountDataToPageIn() {
5699
5700 need_to_page_num = 0;
5701 need_to_page_in = 0;
5702
5703 int i;
5704
5705 for (i = 0; i < MAX_BITMAPS; i++) {
5706 texture_counted[i] = 0;
5707 }
5708 for (i = 0; i < MAX_SOUNDS; i++) {
5709 sound_counted[i] = 0;
5710 }
5711 for (i = 0; i < MAX_POLY_MODELS; i++) {
5712 poly_counted[i] = 0;
5713 }
5714
5715 AlmostPageInAllData();
5716
5717 return need_to_page_in;
5718 }
5719
5720 #endif
5721
5722 void Localization_SetLanguage(int type);
5723
5724 int Localization_GetLanguage(void);
5725
5726 char *LocalizeLevelName(char *level) {
5727 static char local_name[101];
5728
5729 char **english_names;
5730 int num_english_names;
5731
5732 char **local_names;
5733 int num_local_names;
5734
5735 local_name[0] = 0;
5736
5737 // Don't waste our time if we're already in english
5738 int save_lang = Localization_GetLanguage();
5739 if (LANGUAGE_ENGLISH == save_lang) {
5740 strcpy(local_name, level);
5741 return local_name;
5742 }
5743
5744 Localization_SetLanguage(LANGUAGE_ENGLISH);
5745 // Save the current language, then bash it to english
5746 if (!CreateStringTable("level_names.str", &english_names, &num_english_names)) {
5747 mprintf(0, "Couldn't open level_names stringtable!\n");
5748 Localization_SetLanguage(save_lang);
5749 strcpy(local_name, level);
5750 return local_name;
5751 }
5752
5753 // Restore the correct language
5754 Localization_SetLanguage(save_lang);
5755
5756 if (!CreateStringTable("level_names.str", &local_names, &num_local_names)) {
5757 mprintf(0, "Couldn't open level_names stringtable!\n");
5758 // destroy the english stringtable...
5759 DestroyStringTable(english_names, num_english_names);
5760
5761 strcpy(local_name, level);
5762 return local_name;
5763 }
5764
5765 // Now search for the correct level
5766 for (int i = 0; i < num_english_names; i++) {
5767 if (0 == stricmp(level, english_names[i])) {
5768 // Ok, we found a match. So return the local text.
5769 strcpy(local_name, local_names[i]);
5770 DestroyStringTable(english_names, num_english_names);
5771 DestroyStringTable(local_names, num_local_names);
5772 return local_name;
5773 }
5774 }
5775
5776 // We didn't find it, so just use the english
5777 strcpy(local_name, level);
5778 return local_name;
5779 }