Debian Codegraph @

editor/Erooms.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/editor/Erooms.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Functions to create, delete, etc. rooms
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 167 10/14/99 4:21p Matt
30 * Added code to remove duplicate faces from a room.
31 *
32 * 166 9/30/99 5:02p Matt
33 * Automatically normalize face UV values when a level is verified.
34 *
35 * 165 9/07/99 4:36p Gwar
36 * disabled deletion of only the face verts of a face in DeleteRoomFace
37 * (it now deletes all unused verts, like before)
38 *
39 * 164 8/30/99 1:50p Gwar
40 * stupid me forgot a semicolon
41 *
42 * 163 8/30/99 1:23p Gwar
43 * In DeleteRoomFace, added a way to delete only verts that were in the
44 * deleted face
45 *
46 * 162 8/17/99 6:11p Gwar
47 * handle NEWEDITOR texture management in CopyRoom
48 *
49 * 161 8/15/99 3:10a Gwar
50 * decrement Curface in DeleteRoomFace if Curface is the last face in the
51 * room
52 *
53 * 160 8/13/99 4:52p Gwar
54 * fixed a stupid bug from my last checkin
55 *
56 * 159 8/13/99 4:43p Gwar
57 * decrement Markedface in DeleteRoomFace if Markedface is the last face
58 * in the room
59 *
60 * 158 7/17/99 1:36a Gwar
61 * fixed a string formatting bug in a call to OutrageMessageBox
62 *
63 * 157 7/14/99 9:07a Gwar
64 * added a parameter to AllocLoadRoom
65 *
66 * 156 7/06/99 10:22a Gwar
67 * removed and #ifdef NEWEDITOR for this build...
68 *
69 * 155 7/04/99 4:55p Gwar
70 * changes for texture management in NEWEDITOR
71 *
72 * 154 7/03/99 5:50p Gwar
73 * minor changes for NEWEDITOR
74 *
75 * 153 7/02/99 2:17p Gwar
76 * added a parameter to AllocLoadRoom to disable centering on origin
77 *
78 * 152 5/05/99 5:03a Gwar
79 * renamed ned_GameTextures array to GameTextures in new editor to make
80 * game code happy; 3D texture view still does not display textures
81 *
82 * 151 5/05/99 3:02a Gwar
83 * added a parameter to DeleteRoomFace (by permission)
84 *
85 * 150 4/30/99 2:21p Jason
86 * changes to verify level completeness
87 *
88 * 149 4/27/99 3:36p Jason
89 * fixed terrain occlusion bug
90 *
91 * 148 4/26/99 11:11a Chris
92 * Updated Bnode system
93 *
94 * 147 4/25/99 10:16p Matt
95 * Added soundsource check to verify mine.
96 *
97 * 146 4/25/99 9:02p Chris
98 * Improving the Bnode system
99 *
100 * 145 4/21/99 10:20p Matt
101 * Don't compute shells for external rooms in mine verify.
102 *
103 * 144 4/21/99 5:15a Gwar
104 * added support for paletted rooms (ORFs) into new editor
105 *
106 * 143 4/20/99 8:19p Matt
107 * Added an assert.
108 *
109 * 142 4/15/99 3:10p Matt
110 * Took out Int3() in FindSharedEdge(), and took out some mrpintf()s in
111 * the shell calculation.
112 *
113 * 141 4/12/99 10:43a Jason
114 * checked in so code would compile
115 *
116 * 140 4/10/99 5:38p Matt
117 * Commented out some code that wouldn't compile.
118 *
119 * 139 4/08/99 5:46p Jason
120 * added texture finding option
121 *
122 * 138 4/08/99 3:15p Matt
123 * Don't compute a shell for external rooms.
124 *
125 * 137 4/08/99 12:45p Jason
126 * don't count lights or non renderable portal faces in the level
127 *
128 * 136 4/07/99 2:22p Jason
129 * warned designers if they have over 60 128s in their level
130 *
131 * 135 4/07/99 12:43p Jason
132 * added texture counting to verify level
133 *
134 * 134 4/07/99 11:24a Jason
135 * verify level now checks terrain occlusion and boa
136 *
137 * 133 4/06/99 5:10p Matt
138 * Base room shell on all portals in the room, not just the first one.
139 *
140 * 132 4/05/99 11:17a Matt
141 * Added code to list all the objects in the levels.
142 *
143 * 131 4/01/99 9:42a Matt
144 * Added code to compute room shells, and made verify level do it.
145 *
146 * 130 3/31/99 11:34a Matt
147 * Got rid of unused forcefield checkbox on room tab
148 *
149 * 129 3/31/99 11:33a Matt
150 * Make note of non-planar faces that are portals.
151 *
152 * 128 3/30/99 7:57p Matt
153 * Added prototype and moved a function to be inline in the header.
154 *
155 * 127 3/30/99 7:20p Matt
156 * Deleted PointsAreColinear(), since it was never used and worked the
157 * wrong way.
158 *
159 * 126 3/29/99 6:45p Matt
160 * Changed concavity tolerance from 0.005 to 0.05.
161 * Added verify code to check for concave faces and degenerate faces.
162 * Added code to fix degenerate faces.
163 *
164 * 125 3/24/99 5:53p Jason
165 * added per room ambience settings
166 *
167 * 124 3/23/99 1:41p Jason
168 * fixed concave normal bug
169 *
170 * 123 3/11/99 5:12p Matt
171 * Added some error messages for the new verify level checks
172 *
173 * 122 3/11/99 4:58p Matt
174 * Fixed extra-stupid malloc bug
175 *
176 * 121 3/11/99 3:20p Matt
177 * Added checks for T-joints and mismatched portals to verify level.
178 *
179 * 120 2/19/99 1:24p Matt
180 * Moved ComputerCenterRoomOnFace() from editor to main
181 *
182 * 119 2/18/99 12:32p Jason
183 * added room multiplier
184 *
185 * 118 2/11/99 1:44p Matt
186 * Fix up triggers when deleting a face
187 *
188 * 117 2/03/99 1:10p Matt
189 * Changed the paletted room current faces to be stored in a seperate
190 * array, instead of in the room structure.
191 *
192 * 116 1/29/99 12:48p Matt
193 * Rewrote the doorway system
194 *
195 * 115 1/25/99 7:39p Luke
196 * Added comment to tell me when ROOMFILE_VERSION changes (so I can change
197 * RoomView)
198 *
199 * 114 1/21/99 11:34a Matt
200 * Got rid of portal triggers. Since we don't have multi-face portals, a
201 * face trigger works fine for a portal. Also fixed a few editor/trigger
202 * bugs.
203 *
204 * 113 12/22/98 2:03p Matt
205 * Added room names, and made rooms not compress so that room numbers are
206 * suitable for persistant uses.
207 *
208 * 112 12/09/98 3:12p Jason
209 * save/load light multiples with faces
210 *
211 * 111 12/07/98 12:00p Sean
212 * Adjusted co-planar epsilon
213 *
214 * 110 11/17/98 5:58p Sean
215 * Changed concavity tolerance from 0.0001 to 0.0005 (MattT on Sean's
216 * machine)
217 *
218 * 109 11/03/98 6:20p Matt
219 * Changed the concavity check to not be dependent on the length of the
220 * edges being checked.
221 *
222 * 108 11/02/98 6:15p Chris
223 * Room AABBs get saved with the level and the sort_face and dec_sort_face
224 * list s have been removed
225 *
226 * 107 11/01/98 1:58a Jeff
227 * converted the vsprintf calls to use the Pvsprintf, which is a safe
228 * vsprintf, no buffer overflows allowed
229 *
230 * 106 10/08/98 8:16p Matt
231 * Made AssignDefaultUVsToRoom() call AssignDefaultUVsToRoomFace()
232 *
233 * 105 10/08/98 4:23p Kevin
234 * Changed code to comply with memory library usage. Always use mem_malloc
235 * , mem_free and mem_strdup
236 *
237 * 104 10/07/98 12:48p Matt
238 * Added some extra error checking to prevent doubly-formed portals.
239 *
240 * 103 10/03/98 8:05p Matt
241 * Changed the way CheckIfFaceIsPlanar() works (& renamed it to
242 * FaceIsPlanar()) and added planar check to VerifyMine() & VerifyRoom()
243 *
244 * 102 9/29/98 4:00p Matt
245 * Fix selected room list when compressing the mine.
246 *
247 * 101 9/28/98 10:55a Jason
248 * fixed some bugs that VC6 caught
249 *
250 * 100 9/24/98 5:00p Matt
251 * Improved error checking for running out of rooms.
252 *
253 * 99 9/14/98 12:18p Matt
254 * Took out mprintf() that was slowing down room importing.
255 *
256 * 98 9/10/98 6:37p Matt
257 * Increased concavity check tolerance (from 0.002 to 0.003) to fix
258 * problem forming smooth bridge on Dan's level 9.
259 *
260 * 97 9/09/98 9:10p Matt
261 * In verify & fix functions, don't process non-used rooms.
262 *
263 * 96 9/08/98 6:12p Matt
264 * Added code to check for and fix duplicate and unused points.
265 *
266 * 95 9/08/98 12:54p Matt
267 * Changed CONCAVITY_TOLERANCE from 0.0000000001 to 0.002 to fix a problem
268 * Sean was having joining faces.
269 *
270 * 94 9/08/98 12:53p Matt
271 * Added duplicate point check to level verify
272 *
273 * 93 9/08/98 12:05p Jason
274 * moved doorway.h out of room.h
275 *
276 * 92 9/03/98 1:23p Matt
277 * Made AddVertToFace() compute alpha, u2, & v2 values for the new point.
278 *
279 * 91 9/02/98 5:56p Matt
280 * Changed comment
281 *
282 * 90 9/02/98 4:20p Matt
283 * Cleaned up some linking code now that we don't have multi-face portals.
284 *
285 * 89 9/02/98 2:28p Matt
286 * New function to add a vertex to a face.
287 *
288 * 88 9/01/98 12:04p Matt
289 * Ripped out multi-face portal code
290 *
291 * 87 8/31/98 4:37p Matt
292 * Added improved room & mine error checking.
293 *
294 * 86 8/14/98 4:46p Matt
295 * Fix doorways when compressing the mine
296 *
297 * 85 8/11/98 12:46p Matt
298 * Temporary fix for forming portals between faces with different numbers
299 * of vertices.
300 *
301 * 84 7/21/98 4:25p Chris
302 * Fixed a bug with the new AABB stuff
303 *
304 * 83 7/21/98 2:14p Chris
305 * Some FVI speedups - not done
306 *
307 * 82 7/17/98 9:56a Chris
308 * Intermediate checkin
309 *
310 * 81 7/16/98 8:29p Chris
311 * Partial implementation of the new collide code
312 *
313 * 80 6/26/98 12:30p Matt
314 * In PointsAreSame(), check for the middle point being the same as either
315 * of the end points.
316 *
317 * 79 6/25/98 7:15p Matt
318 * Added code to check for errors in portal linkage
319 *
320 * 78 6/19/98 12:22p Jason
321 * fixed special faces bug
322 *
323 * 77 6/08/98 12:27p Matt
324 * Added function to copy from one face to another the flags that should
325 * be inherited.
326 *
327 * 76 6/02/98 6:03p Jason
328 * added specular lightmaps
329 *
330 * 75 5/25/98 3:46p Jason
331 * added better light glows
332 *
333 * 74 5/22/98 3:28p Jason
334 * added specular lighting
335 *
336 * 73 5/04/98 12:27p Matt
337 * Fixed misspelling
338 *
339 * 72 4/27/98 6:41p Matt
340 * Cleaned up epsilons some more, and moved some code here from hroom.cpp.
341 *
342 * 71 4/21/98 4:05p Jason
343 * made copyface take into accound light multiples
344 *
345 * 70 4/21/98 2:44p Matt
346 * Added code to check a level, but haven't finished or made it available
347 * yet.
348 *
349 * 69 4/16/98 3:33p Matt
350 * Cleaned up epsilon values, again.
351 *
352 * 68 4/16/98 12:59p Matt
353 * Changed point-to-edge epsilon, and took out Int3s's
354 *
355 * 67 4/16/98 10:41a Matt
356 * Added FindSharedEdgeAcrossRooms()
357 *
358 * 66 4/15/98 4:57p Matt
359 * Fixed massive stupidity in the way I computed if a point was outside an
360 * edge for polygon:polygon clipping.
361 *
362 * 65 4/02/98 12:23p Jason
363 * trimmed some fat from our structures
364 *
365 * 64 3/31/98 3:49p Jason
366 * added memory lib
367 *
368 * 63 3/18/98 4:31p Chris
369 * Speed up fvi and fixed some bugs
370 *
371 * 62 3/16/98 5:50p Chris
372 * Added sorted face lists for fvi
373 *
374 * 61 2/24/98 1:54p Jason
375 * added much stricter concavity testing
376 *
377 * 60 2/23/98 4:25p Jason
378 * got rid of stupid compiler warning
379 *
380 * 59 2/22/98 3:02p Jason
381 * changed concavity tolerance in CheckIfFaceIsPlanar
382 *
383 * 58 2/19/98 2:20p Jason
384 * fixed lightmap combining to eliminate tjoint problems
385 *
386 * 57 2/16/98 1:07p Matt
387 * Changed assert to int3
388 *
389 * 56 2/12/98 9:10p Matt
390 * Adjusted epsilon when clipping rooms.
391 *
392 * 55 2/11/98 4:03p Matt
393 * When loading a room, center it at the origin
394 *
395 * 54 2/11/98 12:38p Matt
396 * Fixed small bug in DeleteRoomVert()
397 *
398 * 53 2/10/98 3:50p Jason
399 * added pulsing walls
400 *
401 * 52 2/08/98 6:03p Matt
402 * When copying a face, don't copy the has_trigger flag
403 *
404 * 51 1/20/98 4:05p Matt
405 * Move BuildListOfPortalVerts() from roomkeypaddialog.cpp to erooms.cpp
406 *
407 * 50 1/16/98 11:53a Matt
408 * Relaxed low-precision normal check, and added mprintfs with info on
409 * problem normals
410 *
411 * 49 1/15/98 7:34p Matt
412 * Revamped error checking when computing face normals
413 *
414 * 48 1/12/98 5:05p Luke
415 * Code was using wrong constant when searching palette rooms (MattT on
416 * Luke's machine)
417 *
418 * 47 12/30/97 3:33p Jason
419 * fixed stupid variable bug
420 *
421 * 46 11/24/97 1:30a Jason
422 * first attempt at adding shadows
423 *
424 * 45 11/17/97 7:37p Sean
425 * Increased point edge epsilon (Matt on Sean's machine)
426 *
427 * 44 11/11/97 6:45p Matt
428 * Adjusted epsilon value for edge clip
429 *
430 * 43 11/04/97 7:30p Matt
431 * Dealt with yet more precision problems in room clip
432 *
433 * 42 10/30/97 6:31p Jason
434 * fixed potential lightmap bugs
435 *
436 * 41 10/29/97 12:36p Matt
437 * Dealt with some precision problems in clipper
438 *
439 * 40 10/13/97 5:08p Matt
440 * Moved ComputeRoomBoundingSphere() & CreateRoomObjects() from editor to
441 * main
442 *
443 *
444 * 39 10/01/97 7:51p Matt
445 * Added code for external rooms
446 *
447 * 38 9/24/97 3:20p Matt
448 * Added ComputeRoomMinMax()
449 *
450 * 37 9/19/97 2:52p Jason
451 * changes to fix lightmap seam problem
452 *
453 * 36 9/17/97 11:52a Samir
454 * BIG SEGMENT RIPOUT
455 *
456 * 35 9/16/97 4:27p Matt
457 * Got rid of static_light & changed fields in the room struct
458 *
459 * 34 9/15/97 2:21p Matt
460 * Remap triggers when compress mine. (Also deleted some segment engine
461 * code.)
462 *
463 * 33 9/15/97 11:03a Jason
464 * fixed bug with alpha
465 *
466 * 32 9/12/97 2:27p Matt
467 * Added code to delete any unused vertices when deleting a face
468 *
469 * 31 9/10/97 3:02p Matt
470 * Moved GetIJ() from erooms.cpp to room.cpp
471 *
472 * 30 9/09/97 10:25a Matt
473 * Clip alpha values when clipping edge
474 *
475 * 29 9/03/97 2:00p Jason
476 * made LIGHTMAP_SPACING #define for controlling spacing of lightmap
477 * elements
478 *
479 * 28 9/02/97 5:17p Jason
480 * changes for dynamic lighting
481 *
482 * 27 9/02/97 2:28p Matt
483 * Changed PointOutsideEdge() to the more generally-useful
484 * CheckPointAgainstEdge()
485 * Made CopyFace() clear the portal number
486 *
487 * 26 9/02/97 11:47a Jason
488 * Got alpha per vertex working
489 *
490 * 25 9/02/97 11:01a Matt
491 * Moved assert()
492 *
493 * 24 8/29/97 5:44p Matt
494 * Moved a couple functions from hroom.cpp, and added a couple more useful
495 * functions.
496 *
497 * 23 8/28/97 12:31p Jason
498 * added hi-res lightmaps for radiosity
499 *
500 * 22 8/22/97 1:02p Matt
501 * Got CompressMine() working for rooms
502 *
503 * 21 8/21/97 5:56p Matt
504 * Added a bunch of useful functions, and make CheckFaceConcavity() more
505 * generally useful.
506 *
507 * 20 8/18/97 11:47a Sean
508 * FROM JASON: Recompute normals on LoadRoom
509 *
510 * 19 8/04/97 6:46p Jason
511 * added code for a lightmap system
512 *
513 * 18 8/01/97 6:11p Matt
514 * Added several functions that I needed for attach room code
515 *
516 * 17 8/01/97 4:41p Jason
517 * made FixConcaveFaces reset all room normals as an extra precaution
518 *
519 * 16 8/01/97 12:50p Jason
520 * added code to support scaling of rooms/faces/edges
521 *
522 * 15 7/31/97 3:31p Jason
523 * added functions to rotate portal rooms
524 *
525 * 14 7/29/97 1:54p Matt
526 * Added some generally useful room/face functions
527 *
528 * 13 7/21/97 12:14p Matt
529 * Fixed stupid bug in concavity check
530 *
531 * 12 7/18/97 5:36p Jason
532 * save changed paletted rooms on exit
533 *
534 * 11 7/18/97 4:37p Matt
535 * Added function CheckFaceConcavity()
536 *
537 *
538 *
539 * $NoKeywords: $
540 */
541
542 #include <cstdarg>
543 #include <cstdio>
544
545 #include <string.h>
546
547 #ifndef NEWEDITOR
548 #include "d3edit.h"
549 #else
550 #include "..\neweditor\stdafx.h"
551 #include "..\neweditor\neweditor.h"
552 #include "..\neweditor\globals.h"
553 #include "..\neweditor\ned_geometry.h"
554 #endif
555
556 #include "erooms.h"
557 #include "room.h"
558 #include "gametexture.h"
559 #include "terrain.h"
560 #include "special_face.h"
561 #include "lighting.h"
562 #include "trigger.h"
563 #include "mem.h"
564 #include "doorway.h"
565
566 #ifndef NEWEDITOR
567 #include "editor_lighting.h"
568 #endif
569
570 #include "boa.h"
571 #include "bnode.h"
572
573 // List of current faces for the palette rooms
574 int Current_faces[MAX_PALETTE_ROOMS];
575
576 // Returns a free room number, & marks it no longer free. Returns -1 if none free.
577 // If palette_room is true, allocate out of the part of the array for the room palette
578 int GetFreeRoom(bool palette_room) {
579 int roomnum;
580 int start, end;
581
582 int old_hri = Highest_room_index;
583
584 if (palette_room) {
585 start = FIRST_PALETTE_ROOM;
586 end = start + MAX_PALETTE_ROOMS;
587 } else {
588 start = 0;
589 end = MAX_ROOMS;
590 }
591
592 for (roomnum = start; roomnum < end; roomnum++)
593 if (!Rooms[roomnum].used)
594 break;
595
596 if (roomnum == end) // couldn't find a free room
597 return -1;
598
599 if (!palette_room) {
600 if (roomnum > Highest_room_index)
601 Highest_room_index = roomnum;
602 } else
603 Current_faces[roomnum - FIRST_PALETTE_ROOM] = 0;
604
605 BNode_RemapTerrainRooms(old_hri, Highest_room_index);
606
607 return roomnum;
608 }
609
610 // Allocates a room & initializes it.
611 // Memory is allocated for faces & verts arrays, but the elements are *not* initialized
612 // The number of portals is set to zero.
613 // If palette_room is true, allocate out of the part of the array for the room palette
614 // Returns: pointer to new room, or NULL if no free rooms
615 room *CreateNewRoom(int nverts, int nfaces, bool palette_room) {
616 int roomnum;
617 room *rp;
618
619 // Get a free room
620 roomnum = GetFreeRoom(palette_room);
621
622 if (roomnum == -1)
623 return NULL;
624
625 rp = &Rooms[roomnum];
626
627 // Initalize room, allocating memory
628 InitRoom(rp, nverts, nfaces, 0);
629
630 return rp;
631 }
632
633 // Find the uv associated with a face vertex
634 // This routine works by projecting the face on to its own normals plane
635 // and just treating the face like a 2d surface
636
637 // Important - vertnum is the index into the face_verts[] array in the face structure,
638 // not an index into the verts[] array of the room structure
639 void GetUVLForRoomPoint(int roomnum, int facenum, int vertnum, roomUVL *uvl) {
640 matrix face_matrix, trans_matrix;
641 vector fvec;
642 vector avg_vert;
643 vector verts[MAX_VERTS_PER_FACE];
644 vector rot_vert;
645
646 ASSERT(Rooms[roomnum].used);
647 ASSERT(Rooms[roomnum].faces[facenum].num_verts >= 3);
648
649 // find the center point of this face
650 vm_MakeZero(&avg_vert);
651 for (int i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++)
652 avg_vert += Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
653
654 avg_vert /= i;
655
656 // Make the orientation matrix
657 // Reverse the normal because we're looking "at" the face, not from it
658 fvec = -Rooms[roomnum].faces[facenum].normal;
659
660 vm_VectorToMatrix(&face_matrix, &fvec, NULL, NULL);
661 // Make the transformation matrix
662
663 angvec avec;
664 vm_ExtractAnglesFromMatrix(&avec, &face_matrix);
665 vm_AnglesToMatrix(&trans_matrix, avec.p, avec.h, avec.b);
666
667 // Rotate all the points
668 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
669 vector vert = Rooms[roomnum].verts[Rooms[roomnum].faces[facenum].face_verts[i]];
670
671 vert -= avg_vert;
672 vm_MatrixMulVector(&rot_vert, &vert, &trans_matrix);
673
674 verts[i] = rot_vert;
675 }
676
677 // Find left most point
678 int leftmost_point = -1;
679 float leftmost_x = 900000.00f; // a big number
680
681 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
682 if (verts[i].x < leftmost_x) {
683 leftmost_point = i;
684 leftmost_x = verts[i].x;
685 }
686 }
687
688 ASSERT(leftmost_point != -1);
689
690 // Find top most point
691 int topmost_point = -1;
692 float topmost_y = -900000.0f; // a big number
693
694 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
695 if (verts[i].y > topmost_y) {
696 topmost_point = i;
697 topmost_y = verts[i].y;
698 }
699 }
700
701 ASSERT(topmost_point != -1);
702
703 // Find right most point
704 int rightmost_point = -1;
705 float rightmost_x = -900000.00f; // a big number
706
707 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
708 if (verts[i].x > rightmost_x) {
709 rightmost_point = i;
710 rightmost_x = verts[i].x;
711 }
712 }
713
714 ASSERT(rightmost_point != -1);
715
716 // Find bottom most point
717 int bottommost_point = -1;
718 float bottommost_y = 900000.0f; // a big number
719
720 for (i = 0; i < Rooms[roomnum].faces[facenum].num_verts; i++) {
721 if (verts[i].y < bottommost_y) {
722 bottommost_point = i;
723 bottommost_y = verts[i].y;
724 }
725 }
726
727 ASSERT(bottommost_point != -1);
728
729 // now set the base vertex, which is where we base uv 0,0 on
730
731 vector base_vector;
732
733 base_vector.x = verts[leftmost_point].x;
734 base_vector.y = verts[topmost_point].y;
735 base_vector.z = 0;
736
737 // now actually find the uv of our specified point
738
739 uvl->u = (verts[vertnum].x - base_vector.x) / 20.0;
740 uvl->v = (base_vector.y - verts[vertnum].y) / 20.0;
741 }
742
743 #define DEFAULT_ALPHA 255
744
745 // Goes through each face of the passed room and sets the default uvs
746 void AssignDefaultUVsToRoom(room *rp) {
747 ASSERT(rp->used >= 1);
748
749 for (int i = 0; i < rp->num_faces; i++)
750 AssignDefaultUVsToRoomFace(rp, i);
751 }
752
753 // Sets the default UVS for a room face
754 void AssignDefaultUVsToRoomFace(room *rp, int facenum) {
755 ASSERT(rp->used >= 1);
756 ASSERT(facenum < rp->num_faces);
757
758 int t;
759
760 for (t = 0; t < rp->faces[facenum].num_verts; t++) {
761 GetUVLForRoomPoint(ROOMNUM(rp), facenum, t, &rp->faces[facenum].face_uvls[t]);
762 rp->faces[facenum].face_uvls[t].alpha = DEFAULT_ALPHA;
763 }
764 }
765
766 // Searches thru all rooms for a specific name, returns -1 if not found
767 // or index of room with name
768 int FindRoomName(char *name) {
769 int i;
770
771 ASSERT(name != NULL);
772
773 for (i = FIRST_PALETTE_ROOM; i < FIRST_PALETTE_ROOM + MAX_PALETTE_ROOMS; i++)
774 if (Rooms[i].used && Rooms[i].name && !stricmp(name, Rooms[i].name))
775 return i;
776
777 return -1;
778 }
779
780 #define ROOM_HEADER_CHUNK 0
781 #define ROOM_VERTEX_CHUNK 1
782 #define ROOM_FACES_CHUNK 2
783 #define ROOM_END_CHUNK 3
784 #define ROOM_TEXTURE_CHUNK 4
785 #define ROOM_NEW_HEADER_CHUNK 5
786
787 #define ROOMFILE_VERSION 4 // Please tell Luke if you change this (and why) -- THANKS!!
788
789 // saves a room in our ORF (Outrage room file) format
790 void SaveRoom(int n, char *filename) {
791 CFILE *outfile;
792 int headsize, savepos, vertsize, facesize, texsize;
793 int highest_index = 0;
794 int16_t Room_to_texture[MAX_TEXTURES];
795 int t, found_it = 0;
796
797 // Make sure its in use!
798 ASSERT(Rooms[n].used);
799
800 outfile = (CFILE *)cfopen(filename, "wb");
801 if (!outfile) {
802 mprintf(0, "Couldn't save room %s!\n", filename);
803 Int3();
804 return;
805 }
806
807 // write out header info
808 cf_WriteInt(outfile, ROOM_NEW_HEADER_CHUNK);
809 headsize = cftell(outfile);
810 cf_WriteInt(outfile, -1);
811
812 cf_WriteInt(outfile, ROOMFILE_VERSION);
813 cf_WriteInt(outfile, Rooms[n].num_verts);
814 cf_WriteInt(outfile, Rooms[n].num_faces);
815
816 savepos = cftell(outfile);
817 cfseek(outfile, headsize, SEEK_SET);
818 cf_WriteInt(outfile, (savepos - headsize) - 4);
819 cfseek(outfile, savepos, SEEK_SET);
820
821 // write out vertex info
822 cf_WriteInt(outfile, ROOM_VERTEX_CHUNK);
823 vertsize = cftell(outfile);
824 cf_WriteInt(outfile, -1);
825
826 for (int i = 0; i < Rooms[n].num_verts; i++) {
827 cf_WriteFloat(outfile, Rooms[n].verts[i].x);
828 cf_WriteFloat(outfile, Rooms[n].verts[i].y);
829 cf_WriteFloat(outfile, Rooms[n].verts[i].z);
830 }
831
832 savepos = cftell(outfile);
833 cfseek(outfile, vertsize, SEEK_SET);
834 cf_WriteInt(outfile, (savepos - vertsize) - 4);
835 cfseek(outfile, savepos, SEEK_SET);
836
837 // figure out correct texture ordering
838 for (i = 0; i < Rooms[n].num_faces; i++) {
839 int16_t index = Rooms[n].faces[i].tmap;
840
841 for (found_it = 0, t = 0; t < highest_index; t++) {
842 if (Room_to_texture[t] == index) {
843 // This texture is already there
844 found_it = 1;
845 break;
846 }
847 }
848 if (found_it == 0) {
849 // Add this index to our list of textures
850 Room_to_texture[highest_index] = index;
851 highest_index++;
852 ASSERT(highest_index < MAX_TEXTURES);
853 }
854 }
855
856 // write out texture info
857 cf_WriteInt(outfile, ROOM_TEXTURE_CHUNK);
858 texsize = cftell(outfile);
859 cf_WriteInt(outfile, -1);
860
861 // Write out how many different textures there are and then write their names
862 cf_WriteInt(outfile, highest_index);
863
864 for (i = 0; i < highest_index; i++) {
865 int index = Room_to_texture[i];
866 cf_WriteString(outfile, GameTextures[index].used ? GameTextures[index].name : "");
867 }
868
869 savepos = cftell(outfile);
870 cfseek(outfile, texsize, SEEK_SET);
871 cf_WriteInt(outfile, (savepos - texsize) - 4);
872 cfseek(outfile, savepos, SEEK_SET);
873
874 // write out face info
875 cf_WriteInt(outfile, ROOM_FACES_CHUNK);
876 facesize = cftell(outfile);
877 cf_WriteInt(outfile, -1);
878
879 for (i = 0; i < Rooms[n].num_faces; i++) {
880 cf_WriteByte(outfile, Rooms[n].faces[i].light_multiple);
881 cf_WriteInt(outfile, Rooms[n].faces[i].num_verts);
882
883 cf_WriteFloat(outfile, Rooms[n].faces[i].normal.x);
884 cf_WriteFloat(outfile, Rooms[n].faces[i].normal.y);
885 cf_WriteFloat(outfile, Rooms[n].faces[i].normal.z);
886
887 // Search through our texture list and write out that index
888 for (t = 0; t < highest_index; t++) {
889 if (Rooms[n].faces[i].tmap == Room_to_texture[t]) {
890 cf_WriteShort(outfile, t);
891 t = highest_index; // stupid way to break out
892 }
893 }
894
895 for (t = 0; t < Rooms[n].faces[i].num_verts; t++) {
896 cf_WriteShort(outfile, Rooms[n].faces[i].face_verts[t]);
897 cf_WriteFloat(outfile, Rooms[n].faces[i].face_uvls[t].u);
898 cf_WriteFloat(outfile, Rooms[n].faces[i].face_uvls[t].v);
899 cf_WriteFloat(outfile, 0);
900 cf_WriteFloat(outfile, 0);
901 cf_WriteFloat(outfile, 0);
902 cf_WriteFloat(outfile, 0);
903 cf_WriteFloat(outfile, Ubyte_to_float[Rooms[n].faces[i].face_uvls[t].alpha]);
904 }
905 }
906
907 savepos = cftell(outfile);
908 cfseek(outfile, facesize, SEEK_SET);
909 cf_WriteInt(outfile, (savepos - facesize) - 4);
910 cfseek(outfile, savepos, SEEK_SET);
911
912 cf_WriteInt(outfile, ROOM_END_CHUNK);
913 cf_WriteInt(outfile, 4);
914
915 cfclose(outfile);
916
917 mprintf(0, "Room file %s saved.\n", filename);
918 }
919
920 // Allocates a room and then tries to load it
921 // Returns index into Rooms[] array on success
922 // -1 on fail
923 int AllocLoadRoom(char *filename, bool bCenter, bool palette_room) {
924 CFILE *infile;
925 int done = 0, initted = 0;
926 int command, len, room_num = -1, i;
927 room *rp;
928 char texture_names[MAX_TEXTURES][PAGENAME_LEN];
929 int highest_index;
930 int16_t tex_index;
931 int room_version = 0;
932
933 infile = (CFILE *)cfopen(filename, "rb");
934 if (!infile) {
935 Int3(); // hey, couldn't load this room!
936 return -1;
937 }
938
939 while (!done) {
940 command = cf_ReadInt(infile);
941 len = cf_ReadInt(infile);
942
943 switch (command) {
944 // Read room header stuff
945 case ROOM_HEADER_CHUNK:
946 case ROOM_NEW_HEADER_CHUNK:
947 int num_verts, num_faces;
948
949 if (command == ROOM_NEW_HEADER_CHUNK)
950 room_version = cf_ReadInt(infile);
951 num_verts = cf_ReadInt(infile);
952 num_faces = cf_ReadInt(infile);
953
954 // room_num=AllocRoom (num_verts,num_faces);
955 rp = CreateNewRoom(num_verts, num_faces, palette_room);
956
957 if (rp == NULL) {
958 done = 1;
959 continue;
960 }
961
962 initted = 1;
963 break;
964
965 case ROOM_VERTEX_CHUNK:
966 if (!initted) {
967 Int3(); // bad format info, get jason
968 done = 1;
969 break;
970 }
971 for (i = 0; i < rp->num_verts; i++) {
972 rp->verts[i].x = cf_ReadFloat(infile);
973 rp->verts[i].y = cf_ReadFloat(infile);
974 rp->verts[i].z = cf_ReadFloat(infile);
975 }
976 break;
977 case ROOM_TEXTURE_CHUNK:
978 if (!initted) {
979 Int3(); // bad format info, get jason
980 done = 1;
981 break;
982 }
983 highest_index = cf_ReadInt(infile);
984 for (i = 0; i < highest_index; i++)
985 cf_ReadString(texture_names[i], PAGENAME_LEN, infile); // get old name
986
987 break;
988
989 case ROOM_FACES_CHUNK:
990 if (!initted) {
991 Int3(); // bad format info, get jason
992 done = 1;
993 break;
994 }
995
996 for (i = 0; i < rp->num_faces; i++) {
997 uint8_t light_multiple = 4;
998 if (room_version >= 4)
999 light_multiple = cf_ReadByte(infile);
1000
1001 int nverts = cf_ReadInt(infile);
1002
1003 // rp->faces[i].num_verts=cf_ReadInt (infile);
1004 // rp->faces[i].face_verts=(int16_t *)mem_malloc (sizeof(int16_t)*rp->faces[i].num_verts);
1005 // rp->faces[i].face_uvls=(g3UVL *)mem_malloc (sizeof(g3UVL)*rp->faces[i].num_verts);
1006
1007 InitRoomFace(&rp->faces[i], nverts);
1008
1009 ASSERT(rp->faces[i].face_verts);
1010 ASSERT(rp->faces[i].face_uvls);
1011
1012 rp->faces[i].normal.x = cf_ReadFloat(infile);
1013 rp->faces[i].normal.y = cf_ReadFloat(infile);
1014 rp->faces[i].normal.z = cf_ReadFloat(infile);
1015 rp->faces[i].light_multiple = light_multiple;
1016
1017 tex_index = cf_ReadShort(infile);
1018 tex_index = FindTextureName(texture_names[tex_index]);
1019
1020 if (tex_index == -1) // If this texture doesn't exist, bash to error texture
1021 rp->faces[i].tmap = 0;
1022 else
1023 rp->faces[i].tmap = tex_index;
1024
1025 for (int t = 0; t < rp->faces[i].num_verts; t++) {
1026 rp->faces[i].face_verts[t] = cf_ReadShort(infile);
1027 rp->faces[i].face_uvls[t].u = cf_ReadFloat(infile);
1028 rp->faces[i].face_uvls[t].v = cf_ReadFloat(infile);
1029 cf_ReadFloat(infile);
1030 cf_ReadFloat(infile);
1031 cf_ReadFloat(infile);
1032 cf_ReadFloat(infile);
1033 if (room_version >= 1)
1034 rp->faces[i].face_uvls[t].alpha = Float_to_ubyte(cf_ReadFloat(infile));
1035
1036 if (room_version <= 1)
1037 rp->faces[i].face_uvls[t].alpha = 255;
1038 }
1039 }
1040 break;
1041 case ROOM_END_CHUNK:
1042 done = 1;
1043 break;
1044 default:
1045 // skip the ones we don't know
1046 for (i = 0; i < len; i++)
1047 cf_ReadByte(infile);
1048 break;
1049 }
1050 }
1051
1052 cfclose(infile);
1053
1054 if (!ResetRoomFaceNormals(rp))
1055 OutrageMessageBox("Warning: This room has faces with bad normals.\n\n"
1056 "It is recommended that you fix this room before using it.");
1057
1058 // Center the room
1059 if (bCenter) {
1060 vector center;
1061 #ifndef NEWEDITOR
1062 ComputeRoomCenter(&center, rp);
1063 #else
1064 ComputeRoomBoundingSphere(&center, rp);
1065 #endif
1066 for (int v = 0; v < rp->num_verts; v++)
1067 rp->verts[v] -= center;
1068 }
1069
1070 // We're done
1071 return ROOMNUM(rp);
1072 }
1073
1074 // Gets next palette room (from n) that has actually been alloced
1075 int GetNextRoom(int n) {
1076 int i;
1077
1078 ASSERT((n == -1) || ((n >= FIRST_PALETTE_ROOM) && n < FIRST_PALETTE_ROOM + MAX_PALETTE_ROOMS));
1079
1080 if (n == -1) // start at beginning
1081 n = FIRST_PALETTE_ROOM - 1;
1082
1083 for (i = n + 1; i < FIRST_PALETTE_ROOM + MAX_PALETTE_ROOMS; i++)
1084 if (Rooms[i].used)
1085 return i;
1086
1087 for (i = FIRST_PALETTE_ROOM; i <= n; i++)
1088 if (Rooms[i].used)
1089 return i;
1090
1091 // no room found
1092
1093 return -1;
1094 }
1095
1096 void GetIJ(const vector *normal, int *ii, int *jj);
1097
1098 // How much slack to give to give the concavity test
1099 #define CONCAVITY_TOLERANCE 0.05
1100
1101 // Deterimines whether a face is concave or convex
1102 // Parameters: num_verts - the number of vertices in the face to be tested
1103 // face_verts - list of vertex numbers in this face
1104 // normal - the surface normal of this face
1105 // verts - array of vertices into which face_verts elements index
1106 // Returns: If the face is concave, returns the number of the vertex that makes the concavity.
1107 // If the face is convex, returns -1
1108 // NOTE: A face could have multiple concavities, and this will only find the one with the
1109 // lowest-numbered vertex
1110 int CheckFaceConcavity(int num_verts, int16_t *face_verts, vector *normal, vector *verts) {
1111 int ii, jj;
1112 float i0, j0, i1, j1;
1113 float *v0, *v1;
1114 int vn;
1115
1116 // Get the vertices for projection
1117 GetIJ(normal, &ii, &jj);
1118
1119 // Get vector from last vertex to first vertex
1120 v0 = (float *)&verts[face_verts[num_verts - 1]];
1121 v1 = (float *)&verts[face_verts[0]];
1122 i1 = v1[ii] - v0[ii];
1123 j1 = v1[jj] - v0[jj];
1124
1125 // Go through each vert and check for concavity
1126 for (vn = 0; vn < num_verts; vn++) {
1127 float dot;
1128
1129 // Copy previous vertex & edge values
1130 v0 = v1;
1131 i0 = i1;
1132 j0 = j1;
1133
1134 // Compute new values
1135 v1 = (float *)&verts[face_verts[(vn + 1) % num_verts]];
1136 i1 = v1[ii] - v0[ii];
1137 j1 = v1[jj] - v0[jj];
1138
1139 // Now we have two vectors, <i0,j0> and <i1,j1>. Let's see if we have a concavity
1140 dot = (-j0 * i1) + (i0 * j1);
1141 dot /= sqrt(i0 * i0 + j0 * j0) * sqrt(i1 * i1 + j1 * j1);
1142
1143 if (dot > CONCAVITY_TOLERANCE) { // we have a concavity!
1144 // mprintf(0,"Concavity check dot = %f\n",dot);
1145 return vn; //..so return this vertex number
1146 }
1147 }
1148
1149 // No concavity found
1150 return -1;
1151 }
1152
1153 // Recompute the surface normals for all the faces in a room
1154 // Parameters: rp - pointer to the room
1155 // Returns: true if normals computed ok, false if some normals were bad
1156 bool ResetRoomFaceNormals(room *rp) {
1157 int bad_normals = 0;
1158
1159 for (int i = 0; i < rp->num_faces; i++)
1160 if (!ComputeFaceNormal(rp, i))
1161 bad_normals++;
1162
1163 if (bad_normals > 0) {
1164 mprintf(1, "Warning: Room %d has %d bad or low-precision normals\n", ROOMNUM(rp), bad_normals);
1165 return 0;
1166 } else
1167 return 1;
1168 }
1169
1170 // Copies the contents of one face to another. Sets the portal num in the new face to -1.
1171 // Parameters: dfp - pointer to the destination face. This face should be uninitialized.
1172 // sfp - pointer to the source face
1173 void CopyFace(face *dfp, face *sfp) {
1174 InitRoomFace(dfp, sfp->num_verts);
1175
1176 dfp->flags = sfp->flags;
1177 dfp->portal_num = -1;
1178 dfp->normal = sfp->normal;
1179 dfp->tmap = sfp->tmap;
1180 dfp->light_multiple = sfp->light_multiple;
1181
1182 // Clear the flags that we don't want transferred over
1183 dfp->flags &= ~FF_LIGHTMAP;
1184 dfp->flags &= ~FF_HAS_TRIGGER;
1185
1186 // Copy vertices and uvls
1187 for (int i = 0; i < sfp->num_verts; i++) {
1188 dfp->face_verts[i] = sfp->face_verts[i];
1189 dfp->face_uvls[i] = sfp->face_uvls[i];
1190 }
1191 }
1192
1193 // Checks to see if a face is planar.
1194 // See if all the points are within a certain distance of an average point
1195 // Returns 1 if face is planar, 0 if not
1196 bool FaceIsPlanar(int nv, int16_t *face_verts, vector *normal, vector *verts) {
1197 // Triangles are always planar
1198 if (nv == 3)
1199 return 1;
1200
1201 // Get average distance from origin for points on this face
1202 float average_d = 0;
1203 for (int v = 0; v < nv; v++)
1204 average_d += verts[face_verts[v]] * *normal;
1205 average_d /= nv;
1206
1207 // Look for points too far from the average
1208 float d;
1209 for (v = 0; v < nv; v++) {
1210 d = verts[face_verts[v]] * *normal;
1211 if (fabs(d - average_d) > POINT_TO_PLANE_EPSILON)
1212 return 0;
1213 }
1214
1215 // Didn't find anything wrong, so face is planar
1216 return 1;
1217 }
1218
1219 // Fixes all the concave/nonplanar faces of facelist of room rp
1220 void FixConcaveFaces(room *rp, int *facelist, int facecount) {
1221 int i, t, k;
1222 face *newfaces;
1223
1224 for (i = 0; i < facecount; i++) {
1225 // If this face is concave, build a triangle strip out of the concave face
1226 if (!FaceIsPlanar(rp, facelist[i])) {
1227 int concave_verts[MAX_VERTS_PER_FACE];
1228
1229 int old_num_faces = rp->num_faces;
1230 int concave_count = rp->faces[facelist[i]].num_verts;
1231 int old_tmap = rp->faces[facelist[i]].tmap;
1232 int num_new_faces = concave_count - 3;
1233 ASSERT(num_new_faces > 0);
1234
1235 mprintf(0, "Creating %d new faces from face %d!\n", num_new_faces, facelist[i]);
1236
1237 // copy the concave vert indices for later use
1238 for (t = 0; t < concave_count; t++)
1239 concave_verts[t] = rp->faces[facelist[i]].face_verts[t];
1240
1241 // Allocate memory for our new faces
1242 int nfaces = rp->num_faces + num_new_faces;
1243
1244 newfaces = (face *)mem_malloc(nfaces * sizeof(face));
1245 ASSERT(newfaces != NULL);
1246
1247 // Copy all the faces into our new array
1248 for (t = 0; t < rp->num_faces; t++) {
1249
1250 if (t != facelist[i]) {
1251 int nverts = rp->faces[t].num_verts;
1252
1253 newfaces[t].face_verts = (int16_t *)mem_malloc(nverts * sizeof(int16_t));
1254 ASSERT(newfaces[t].face_verts != NULL);
1255 newfaces[t].face_uvls = (roomUVL *)mem_malloc(nverts * sizeof(roomUVL));
1256 ASSERT(newfaces[t].face_uvls != NULL);
1257
1258 newfaces[t].normal = rp->faces[t].normal;
1259 newfaces[t].tmap = rp->faces[t].tmap;
1260 newfaces[t].flags = rp->faces[t].flags;
1261 newfaces[t].portal_num = rp->faces[t].portal_num;
1262 newfaces[t].num_verts = rp->faces[t].num_verts;
1263 newfaces[t].special_handle = BAD_SPECIAL_FACE_INDEX;
1264
1265 for (k = 0; k < nverts; k++) {
1266 newfaces[t].face_verts[k] = rp->faces[t].face_verts[k];
1267 newfaces[t].face_uvls[k] = rp->faces[t].face_uvls[k];
1268 }
1269 } else // special case the concave face into a triangle
1270 {
1271 int nverts = 3;
1272
1273 newfaces[t].face_verts = (int16_t *)mem_malloc(nverts * sizeof(int16_t));
1274 ASSERT(newfaces[t].face_verts != NULL);
1275 newfaces[t].face_uvls = (roomUVL *)mem_malloc(nverts * sizeof(roomUVL));
1276 ASSERT(newfaces[t].face_uvls != NULL);
1277
1278 newfaces[t].tmap = rp->faces[t].tmap;
1279 newfaces[t].flags = rp->faces[t].flags;
1280 newfaces[t].portal_num = rp->faces[t].portal_num;
1281 newfaces[t].num_verts = 3;
1282 newfaces[t].special_handle = BAD_SPECIAL_FACE_INDEX;
1283
1284 for (k = 0; k < nverts; k++) {
1285 newfaces[t].face_verts[k] = rp->faces[t].face_verts[k];
1286 newfaces[t].face_uvls[k] = rp->faces[t].face_uvls[k];
1287 }
1288
1289 // Get new normal
1290 if (!ComputeFaceNormal(rp, t))
1291 Int3(); // Bad normal: get Matt or Jason, or ignore.
1292
1293 newfaces[t].normal = rp->faces[t].normal;
1294 }
1295 }
1296
1297 // Free up the old list of faces
1298 mem_free(rp->faces);
1299 rp->faces = newfaces;
1300 rp->num_faces = nfaces;
1301
1302 if (rp->num_bbf_regions) {
1303 for (i = 0; i < rp->num_bbf_regions; i++) {
1304 mem_free(rp->bbf_list[i]);
1305 }
1306 mem_free(rp->bbf_list);
1307 mem_free(rp->num_bbf);
1308 mem_free(rp->bbf_list_min_xyz);
1309 mem_free(rp->bbf_list_max_xyz);
1310 mem_free(rp->bbf_list_sector);
1311 rp->num_bbf_regions = 0;
1312 }
1313
1314 // Now build the remaining triangles of our concave face
1315 for (t = 0; t < num_new_faces; t++)
1316 InitRoomFace(&rp->faces[old_num_faces + t], 3);
1317
1318 for (t = 0; t < concave_count - 3; t++) {
1319 rp->faces[old_num_faces + t].face_verts[0] = concave_verts[0];
1320 rp->faces[old_num_faces + t].face_verts[1] = concave_verts[2 + t];
1321 rp->faces[old_num_faces + t].face_verts[2] = concave_verts[3 + t];
1322 rp->faces[old_num_faces + t].tmap = old_tmap;
1323
1324 if (!ComputeFaceNormal(rp, old_num_faces + t))
1325 Int3(); // Bad normal: get Matt or Jason, or ignore.
1326
1327 AssignDefaultUVsToRoomFace(rp, old_num_faces + t);
1328 }
1329 }
1330 }
1331
1332 if (!ResetRoomFaceNormals(rp))
1333 Int3(); // Get Matt or Jason
1334 }
1335
1336 // Changes the number of verts in a face. Frees and reallocates the face_verts & face_uvls arrays.
1337 // Leaves all other fields the same
1338 void ReInitRoomFace(face *fp, int nverts) {
1339 ASSERT(nverts != 0);
1340
1341 fp->num_verts = nverts;
1342
1343 mem_free(fp->face_verts);
1344 mem_free(fp->face_uvls);
1345
1346 fp->face_verts = (int16_t *)mem_malloc(nverts * sizeof(*fp->face_verts));
1347 ASSERT(fp->face_verts != NULL);
1348 fp->face_uvls = (roomUVL *)mem_malloc(nverts * sizeof(*fp->face_uvls));
1349 ASSERT(fp->face_uvls != NULL);
1350 }
1351
1352 // Determines if two points are close enough together to be considered the same
1353 // Parameters: v0,v1 - the two points
1354 // Returns: true if the points are the same or very close; else false
1355 bool PointsAreSame(vector *v0, vector *v1) {
1356 float d = vm_VectorDistance(v0, v1);
1357
1358 return (d < POINT_TO_POINT_EPSILON);
1359 }
1360
1361 // Check to see if a point is in, in front of, or behind a plane
1362 // Parameters: checkpoint - the point to check
1363 // planepoint,normal - the plane we're checking against
1364 // Returns: 0 if on the plane, -1 if behind, 1 if in front
1365 int CheckPointToPlane(vector *checkpoint, vector *planepoint, vector *normal) {
1366 float d = (*checkpoint - *planepoint) * *normal;
1367
1368 if (d < -POINT_TO_PLANE_EPSILON)
1369 return -1;
1370 else if (d > POINT_TO_PLANE_EPSILON)
1371 return 1;
1372 else
1373 return 0;
1374 }
1375
1376 // Check to see if all the points on a face are in front of a plane
1377 // Parameters: rp,facenum - the face to check
1378 // planepoint,normal - define the plane we're checking against
1379 // Returns: the number of the first point found on the back of the plane, or -1 of all on front
1380 int CheckFaceToPlane(room *rp, int facenum, vector *planepoint, vector *normal) {
1381 face *fp = &rp->faces[facenum];
1382
1383 for (int i = 0; i < fp->num_verts; i++)
1384 if (CheckPointToPlane(&rp->verts[fp->face_verts[i]], planepoint, normal) < 0)
1385 return i;
1386
1387 return -1; // no points found behind the plane
1388 }
1389
1390 // Create space for additional vertices in a room.
1391 // Allocates a new array of vertices, copies from the old list, and frees the old list
1392 // The new vertices are at the end of the list, so none of the old vertices change number
1393 // Parameters: rp - the room
1394 // n_new_verts - how many vertices are being added to the room
1395 // Returns: the number of the first new vertex
1396 int RoomAddVertices(room *rp, int num_new_verts) {
1397 if (num_new_verts == 0)
1398 return 0;
1399
1400 vector *newverts = (vector *)mem_malloc((rp->num_verts + num_new_verts) * sizeof(*newverts));
1401
1402 ASSERT(newverts != NULL);
1403
1404 for (int i = 0; i < rp->num_verts; i++) {
1405 newverts[i] = rp->verts[i];
1406 }
1407
1408 mem_free(rp->verts);
1409
1410 rp->verts = newverts;
1411
1412 rp->num_verts += num_new_verts;
1413
1414 return (rp->num_verts - num_new_verts);
1415 }
1416
1417 // Create space for additional faces in a room.
1418 // Allocates a new faces array, copies from the old list, and frees the old list
1419 // The new faces are at the end of the list, so none of the old faces change number
1420 // Parameters: rp - the room
1421 // num_new_faces - how many faces are being added to the room
1422 // Returns: the number of the first new face
1423 int RoomAddFaces(room *rp, int num_new_faces) {
1424 int i;
1425
1426 if (num_new_faces == 0)
1427 return 0;
1428
1429 face *newfaces = (face *)mem_malloc((rp->num_faces + num_new_faces) * sizeof(*newfaces));
1430
1431 ASSERT(newfaces != NULL);
1432
1433 for (i = 0; i < rp->num_faces; i++)
1434 newfaces[i] = rp->faces[i];
1435
1436 mem_free(rp->faces);
1437
1438 rp->faces = newfaces;
1439 rp->num_faces += num_new_faces;
1440
1441 if (rp->num_bbf_regions) {
1442 for (i = 0; i < rp->num_bbf_regions; i++) {
1443 mem_free(rp->bbf_list[i]);
1444 }
1445 mem_free(rp->bbf_list);
1446 mem_free(rp->num_bbf);
1447 mem_free(rp->bbf_list_min_xyz);
1448 mem_free(rp->bbf_list_max_xyz);
1449 mem_free(rp->bbf_list_sector);
1450
1451 rp->num_bbf_regions = 0;
1452 }
1453
1454 return (rp->num_faces - num_new_faces);
1455 }
1456
1457 // Check if a point is inside, outside, or on an edge of a polygon
1458 // Parameters: checkv - the point to be checked
1459 // v1,v0 - the edge to check against. Two sequential verts in a clockwise polygon.
1460 // normal - the surface normal of the polygon
1461 // Returns: 1 if the point in inside the edge
1462 // 0 if the point is on the edge
1463 // -1 if the point is outside the edge
1464 int CheckPointAgainstEdge(vector *checkv, vector *v0, vector *v1, vector *normal) {
1465 int ii, jj;
1466 float edge_i, edge_j, check_i, check_j;
1467 float *vv0, *vv1, *checkvv;
1468 float edge_mag, dot;
1469
1470 // Get the vertices for projection
1471 GetIJ(normal, &ii, &jj);
1472
1473 // Get pointers to elements of our vectors
1474 vv0 = (float *)v0;
1475 vv1 = (float *)v1;
1476 checkvv = (float *)checkv;
1477
1478 // Get 2d vector for edge
1479 edge_i = vv1[ii] - vv0[ii];
1480 edge_j = vv1[jj] - vv0[jj];
1481 edge_mag = sqrt(edge_i * edge_i + edge_j * edge_j);
1482
1483 // Get 2d vector for check point
1484 check_i = checkvv[ii] - vv0[ii];
1485 check_j = checkvv[jj] - vv0[jj];
1486
1487 // Now do the dot product to see if the check point is on the front
1488 dot = ((-edge_j * check_i) + (edge_i * check_j)) / edge_mag;
1489
1490 // Check dot value and return appropriate code
1491 if (dot > POINT_TO_EDGE_EPSILON)
1492 return -1;
1493 else if (dot < -POINT_TO_EDGE_EPSILON)
1494 return 1;
1495 else
1496 return 0;
1497 }
1498
1499 // Clips on edge of a polygon against another edge
1500 // Parameters: normal - defines the plane in which these edgs lie
1501 // v0,v1 - the edge to be clipped
1502 // v2,v3 - is the edge clipped against
1503 // newv - filled in with the intersection point
1504 void ClipEdge(vector *normal, vertex *v0, vertex *v1, vector *v2, vector *v3, vertex *newv) {
1505 float *vv0, *vv1, *vv2, *vv3;
1506 float k;
1507 int ii, jj;
1508
1509 // Get the vertices for projection
1510 GetIJ(normal, &ii, &jj);
1511
1512 // Get pointers to elements of our vectors
1513 vv0 = (float *)&v0->vec;
1514 vv1 = (float *)&v1->vec;
1515 vv2 = (float *)v2;
1516 vv3 = (float *)v3;
1517
1518 k = ((vv2[jj] - vv0[jj]) * (vv3[ii] - vv2[ii]) - (vv2[ii] - vv0[ii]) * (vv3[jj] - vv2[jj])) /
1519 ((vv1[jj] - vv0[jj]) * (vv3[ii] - vv2[ii]) - (vv1[ii] - vv0[ii]) * (vv3[jj] - vv2[jj]));
1520
1521 // Deal w/ precision problems
1522 if (k < 0.0) {
1523 ASSERT((vm_VectorDistance(&v1->vec, &v0->vec) * -k) < POINT_TO_EDGE_EPSILON);
1524 k = 0.0;
1525 }
1526 if (k > 1.0) {
1527 ASSERT((vm_VectorDistance(&v1->vec, &v0->vec) * (k - 1.0)) < POINT_TO_EDGE_EPSILON);
1528 k = 1.0;
1529 }
1530
1531 // Check for valid values of k
1532 ASSERT((k >= 0) && (k <= 1.0));
1533
1534 newv->vec = v0->vec + (v1->vec - v0->vec) * k;
1535
1536 newv->uvl.u = v0->uvl.u + (v1->uvl.u - v0->uvl.u) * k;
1537 newv->uvl.v = v0->uvl.v + (v1->uvl.v - v0->uvl.v) * k;
1538 newv->uvl.alpha = v0->uvl.alpha + ((v1->uvl.alpha - v0->uvl.alpha) * k);
1539 }
1540
1541 // Finds a shared edge, if one exists, between two faces in the same room
1542 // Parameters: fp0,fp1 - pointers to the two faces
1543 // vn0,vn1 - filled in with the vertex numbers of the edge. These vert numbers are
1544 // relative to their own faces. The shared edge is
1545 //verts <vn0,vn0+1> on face 0, and <vn1+1,vn1> on face 1 Returns: true if a shared edge was found, else
1546 // false
1547 bool FindSharedEdge(face *fp0, face *fp1, int *vn0, int *vn1) {
1548 int i, j, a0, b0, a1, b1;
1549
1550 // Go through each edge in first face
1551 for (i = 0; i < fp0->num_verts; i++) {
1552
1553 // Get edge verts - <a0,b0> is edge on first face
1554 a0 = fp0->face_verts[i];
1555 b0 = fp0->face_verts[(i + 1) % fp0->num_verts];
1556
1557 // Check against second face
1558 for (j = 0; j < fp1->num_verts; j++) {
1559
1560 // Get edge verts - <a1,b1> is edge on second face
1561 a1 = fp1->face_verts[j];
1562 b1 = fp1->face_verts[(j + 1) % fp1->num_verts];
1563
1564 //@@if ((a0==a1) && (b0==b1))
1565 //@@ Int3(); //If you hit this, you probably have a duplicate
1566 //or overlapping face
1567
1568 if ((a0 == b1) && (b0 == a1)) { // found match!
1569 *vn0 = i;
1570 *vn1 = j;
1571 return 1;
1572 }
1573 }
1574 }
1575
1576 // Didn't find an edge, so return error
1577 return 0;
1578 }
1579
1580 // Finds a shared edge, if one exists, between two faces in different rooms
1581 // Parameters: rp0,rp1 - pointers to the two rooms
1582 // face0,face1 - the face numbers in rp0 & rp1, respectively
1583 // vn0,vn1 - filled in with the vertex numbers of the edge. These vert numbers are
1584 // relative to their own faces. The shared edge is
1585 //verts <vn0,vn0+1> on face 0, and <vn1+1,vn1> on face 1 Returns: true if a shared edge was found, else
1586 // false
1587 bool FindSharedEdgeAcrossRooms(room *rp0, int face0, room *rp1, int face1, int *vn0, int *vn1) {
1588 int i, j;
1589 vector *va0, *vb0, *va1, *vb1;
1590 face *fp0, *fp1;
1591
1592 // Get pointers to the two faces
1593 fp0 = &rp0->faces[face0];
1594 fp1 = &rp1->faces[face1];
1595
1596 // Go through each edge in first face
1597 for (i = 0; i < fp0->num_verts; i++) {
1598
1599 // Get edge verts - <va0,vb0> is edge on first face
1600 va0 = &rp0->verts[fp0->face_verts[i]];
1601 vb0 = &rp0->verts[fp0->face_verts[(i + 1) % fp0->num_verts]];
1602
1603 // Check against second face
1604 for (j = 0; j < fp1->num_verts; j++) {
1605
1606 // Get edge verts - <va1,vb1> is edge on second face
1607 va1 = &rp1->verts[fp1->face_verts[j]];
1608 vb1 = &rp1->verts[fp1->face_verts[(j + 1) % fp1->num_verts]];
1609
1610 if (PointsAreSame(va0, va1) && PointsAreSame(vb0, vb1)) // this shouldn't happen
1611 Int3();
1612
1613 if (PointsAreSame(va0, vb1) && PointsAreSame(vb0, va1)) { // found match!
1614 *vn0 = i;
1615 *vn1 = j;
1616 return 1;
1617 }
1618 }
1619 }
1620
1621 // Didn't find an edge, so return error
1622 return 0;
1623 }
1624
1625 // Delete a vertex from a room. Assumes the vertex is unused.
1626 // Parameters: rp - pointer to room
1627 // vertnum - the vertex to delete
1628 void DeleteRoomVert(room *rp, int vertnum) {
1629 int f, v;
1630 face *fp;
1631 vector *newverts;
1632
1633 // Remap vertices in faces
1634 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
1635 for (v = 0; v < fp->num_verts; v++)
1636 if (fp->face_verts[v] == vertnum)
1637 Int3(); // THIS IS VERY BAD! DELETING A VERTEX STILL IN USE!
1638 else if (fp->face_verts[v] > vertnum)
1639 fp->face_verts[v]--;
1640
1641 // malloc new list
1642 newverts = (vector *)mem_malloc(sizeof(*newverts) * (rp->num_verts - 1));
1643 ASSERT(newverts != NULL);
1644
1645 // Copy verts to new list
1646 for (v = 0; v < vertnum; v++) {
1647 newverts[v] = rp->verts[v];
1648 }
1649
1650 for (; v < rp->num_verts - 1; v++) {
1651 newverts[v] = rp->verts[v + 1];
1652 }
1653
1654 // Delete old list
1655 mem_free(rp->verts);
1656
1657 // Use new list
1658 rp->verts = newverts;
1659
1660 // Update count
1661 rp->num_verts--;
1662 }
1663
1664 // Check the vertices in a room & remove unused ones
1665 // Parameters: rp - the room to check
1666 // Returns: the number of vertices removed
1667 int DeleteUnusedRoomVerts(room *rp) {
1668 bool vert_used[MAX_VERTS_PER_ROOM];
1669 int f, v;
1670 face *fp;
1671 int n_deleted = 0;
1672 ;
1673
1674 // Init all the flags to unused
1675 for (v = 0; v < rp->num_verts; v++)
1676 vert_used[v] = 0;
1677
1678 // Go through all the faces & flag the used verts
1679 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
1680 for (v = 0; v < fp->num_verts; v++)
1681 vert_used[fp->face_verts[v]] = 1;
1682
1683 // Now delete the unused verts
1684 for (v = rp->num_verts - 1; v >= 0; v--)
1685 if (!vert_used[v]) {
1686 DeleteRoomVert(rp, v);
1687 n_deleted++;
1688 }
1689
1690 // Done
1691 return n_deleted;
1692 }
1693
1694 // Delete a face from a room
1695 // Parameters: rp - the room the face is in
1696 // facenum - the face to be deleted
1697 void DeleteRoomFace(room *rp, int facenum, bool delete_unused_verts) {
1698 int p, f, i, t;
1699 face *newfaces;
1700
1701 // Check for trigger on this face
1702 if (rp->faces[facenum].flags & FF_HAS_TRIGGER)
1703 DeleteTrigger(ROOMNUM(rp), facenum);
1704
1705 // Adjust face numbers in portals
1706 for (p = 0; p < rp->num_portals; p++) {
1707 portal *pp = &rp->portals[p];
1708 ASSERT(pp->portal_face != facenum);
1709 if (pp->portal_face > facenum)
1710 pp->portal_face--;
1711 }
1712
1713 // Adjust face numbers in triggers
1714 for (t = 0; t < Num_triggers; t++) {
1715 trigger *tp = &Triggers[t];
1716 if (tp->roomnum == ROOMNUM(rp)) {
1717 ASSERT(tp->facenum != facenum);
1718 if (tp->facenum > facenum)
1719 tp->facenum--;
1720 }
1721 }
1722
1723 // Allocate new face list
1724 newfaces = (face *)mem_malloc(sizeof(*newfaces) * (rp->num_faces - 1));
1725 ASSERT(newfaces != NULL);
1726
1727 // Copy faces over
1728 for (f = 0; f < facenum; f++)
1729 newfaces[f] = rp->faces[f];
1730 for (f++; f < rp->num_faces; f++)
1731 newfaces[f - 1] = rp->faces[f];
1732
1733 #ifdef NEWEDITOR
1734 // Decrement texture usage
1735 if (ROOMNUM(rp) < MAX_ROOMS) {
1736 LevelTexDecrementTexture(rp->faces[facenum].tmap);
1737 //@@RoomTexDecrementTexture(rp->faces[facenum].tmap,m_Textures_in_use,false);
1738 } else {
1739 // TODO: for room tab, replace second line with first
1740 ned_MarkTextureInUse(rp->faces[facenum].tmap, false);
1741 //@@RoomTexDecrementTexture(rp->faces[facenum].tmap,m_Textures_in_use);
1742 }
1743 #endif
1744
1745 // Free deleted face memeory
1746 FreeRoomFace(&rp->faces[facenum]);
1747
1748 // Free old face list
1749 mem_free(rp->faces);
1750
1751 // Use new face list
1752 rp->faces = newfaces;
1753 rp->num_faces--;
1754
1755 // Adjust the current and marked faces if necessary
1756 #ifdef NEWEDITOR
1757 if (theApp.m_pLevelWnd != NULL)
1758 #endif
1759 if (rp == Curroomp) {
1760 if (Curface == rp->num_faces)
1761 Curface = rp->num_faces - 1;
1762 else if (Curface > rp->num_faces)
1763 Int3(); // this shouldn't be
1764 if (Markedface == rp->num_faces)
1765 Markedface = rp->num_faces - 1;
1766 else if (Markedface > rp->num_faces)
1767 Int3(); // this shouldn't be
1768 }
1769
1770 if (rp->num_bbf_regions) {
1771 for (i = 0; i < rp->num_bbf_regions; i++) {
1772 mem_free(rp->bbf_list[i]);
1773 }
1774 mem_free(rp->bbf_list);
1775 mem_free(rp->num_bbf);
1776 mem_free(rp->bbf_list_min_xyz);
1777 mem_free(rp->bbf_list_max_xyz);
1778 mem_free(rp->bbf_list_sector);
1779
1780 rp->num_bbf_regions = 0;
1781 }
1782
1783 // Get rid of any now-unused verts
1784 if (delete_unused_verts)
1785 DeleteUnusedRoomVerts(rp);
1786 }
1787
1788 // Deletes a portal from a room. Does not delete this portal this connects to
1789 // Parameters: rp - the room the portal is in
1790 // portalnum - the portal to be deleted
1791 void DeleteRoomPortal(room *rp, int portalnum) {
1792 portal *pp = &rp->portals[portalnum];
1793 face *fp = &rp->faces[pp->portal_face];
1794 portal *newportals;
1795
1796 // Clear the portal field of the portal's face
1797 ASSERT(fp->portal_num == portalnum);
1798 fp->portal_num = -1;
1799
1800 // Renumber all portals that come after this one
1801 for (int p = portalnum + 1; p < rp->num_portals; p++) {
1802 portal *tp = &rp->portals[p];
1803
1804 // Renumber the face in this portal
1805 ASSERT(rp->faces[tp->portal_face].portal_num == p);
1806 rp->faces[tp->portal_face].portal_num--;
1807
1808 // Renumber the link back to this portal
1809 if (tp->croom != -1)
1810 Rooms[tp->croom].portals[tp->cportal].cportal--;
1811 }
1812
1813 // Alloc new portal list
1814 if (rp->num_portals == 1)
1815 newportals = NULL;
1816 else {
1817 newportals = (portal *)mem_malloc(sizeof(*newportals) * (rp->num_portals - 1));
1818 ASSERT(newportals != NULL);
1819 }
1820
1821 // Copy portals over
1822 for (p = 0; p < portalnum; p++)
1823 newportals[p] = rp->portals[p];
1824 for (p++; p < rp->num_portals; p++)
1825 newportals[p - 1] = rp->portals[p];
1826
1827 // Free old portal list
1828 mem_free(rp->portals);
1829
1830 // Use the new list
1831 rp->portals = newportals;
1832 rp->num_portals--;
1833 }
1834
1835 #if 0
1836 //Remove holes in the room list
1837 void CompressRooms(void)
1838 {
1839 int hole,t,high_room;
1840
1841 t = Highest_room_index;
1842 high_room=Highest_room_index;
1843
1844
1845 for (hole=0; hole < t; hole++)
1846 if (! Rooms[hole].used) {
1847
1848 // found an unused t which is a hole if a used t follows (not necessarily immediately) it.
1849 while (! Rooms[t].used)
1850 t--;
1851
1852 if (t > hole) {
1853 room *rp;
1854 int objnum;
1855
1856 // Ok, hole is the index of a hole, t is the index of a t which follows it.
1857 // Copy t into hole, update pointers to it, update Curroomp, Markedroomp if necessary.
1858 Rooms[hole] = Rooms[t];
1859 Rooms[t].used = 0;
1860
1861 if (Curroomp == &Rooms[t])
1862 Curroomp = &Rooms[hole];
1863
1864 if (Markedroomp == &Rooms[t])
1865 Markedroomp = &Rooms[hole];
1866
1867 //Fix connections
1868 rp = &Rooms[hole];
1869 for (int p=0;p<rp->num_portals;p++) {
1870 portal *pp = &rp->portals[p];
1871
1872 if (pp->croom == -1) { //a terrain connection
1873 for (int l=0;l<MAX_LINK_TILES;l++)
1874 if (Link_tile[l].terrain_seg != -1)
1875 if (Link_tile[l].mine_seg == t) {
1876 Link_tile[l].mine_seg = hole;
1877 break;
1878 }
1879 ASSERT(l < MAX_LINK_TILES);
1880 }
1881 else {
1882 ASSERT(Rooms[pp->croom].portals[pp->cportal].croom == t);
1883 Rooms[pp->croom].portals[pp->cportal].croom = hole;
1884 }
1885 }
1886
1887 //Update object room pointers
1888 for (objnum = rp->objects; objnum != -1; objnum = Objects[objnum].next) {
1889 ASSERT(Objects[objnum].roomnum == t);
1890 Objects[objnum].roomnum = hole;
1891 }
1892
1893 //Fix triggers
1894 for (p=0;p<Num_triggers;p++)
1895 if (Triggers[p].roomnum == t)
1896 Triggers[p].roomnum = hole;
1897
1898 //Fix selected list
1899 for (int i=0;i<N_selected_rooms;i++)
1900 if (Selected_rooms[i] == t)
1901 Selected_rooms[i] = hole;
1902
1903 t--;
1904 } // end if (t > hole)
1905 } // end if
1906
1907 while (! Rooms[Highest_room_index].used)
1908 Highest_room_index--;
1909 }
1910
1911 // Compress mine by getting rid of holes the the room array
1912 void CompressMine()
1913 {
1914 //if (Do_duplicate_vertex_check) {
1915 // med_combine_duplicate_vertices(Vertex_active);
1916 // Do_duplicate_vertex_check = 0;
1917 //}
1918
1919 CompressRooms();
1920 }
1921 #endif
1922
1923 // Copies the contents of one face to another.
1924 // Parameters: dfp - pointer to the destination face. This face should be uninitialized.
1925 // sfp - pointer to the source face
1926 void CopyPortal(face *dfp, face *sfp) {
1927 InitRoomFace(dfp, sfp->num_verts);
1928
1929 dfp->flags = sfp->flags;
1930 dfp->portal_num = sfp->portal_num;
1931 dfp->normal = sfp->normal;
1932 dfp->tmap = sfp->tmap;
1933
1934 for (int i = 0; i < sfp->num_verts; i++) {
1935 dfp->face_verts[i] = sfp->face_verts[i];
1936 dfp->face_uvls[i] = sfp->face_uvls[i];
1937 }
1938 }
1939
1940 // Copies the data from one room into another
1941 // Note: Portals are not copied, and the new room will have zero portals
1942 // Parameters: destp - the destination room of the copy
1943 // srcp - the source room for the copy
1944 void CopyRoom(room *destp, room *srcp) {
1945 // Initialize the new room
1946 InitRoom(destp, srcp->num_verts, srcp->num_faces, 0);
1947
1948 // Copy over the faces
1949 for (int i = 0; i < destp->num_faces; i++) {
1950 CopyFace(&destp->faces[i], &srcp->faces[i]);
1951 #ifdef NEWEDITOR
1952 LevelTexIncrementTexture(srcp->faces[i].tmap);
1953 #endif
1954 }
1955
1956 // Copy over the verts
1957 for (i = 0; i < destp->num_verts; i++)
1958 destp->verts[i] = srcp->verts[i];
1959
1960 // Copy doorway info
1961 if (srcp->doorway_data) {
1962 destp->doorway_data = (doorway *)mem_malloc(sizeof(*destp->doorway_data));
1963 *destp->doorway_data = *srcp->doorway_data;
1964 }
1965
1966 // Copy over the rest of the data
1967 destp->flags = srcp->flags;
1968
1969 // Copy over room lighting
1970 Room_multiplier[destp - Rooms] = Room_multiplier[srcp - Rooms];
1971 Room_ambience_r[destp - Rooms] = Room_ambience_r[srcp - Rooms];
1972 Room_ambience_g[destp - Rooms] = Room_ambience_g[srcp - Rooms];
1973 Room_ambience_b[destp - Rooms] = Room_ambience_b[srcp - Rooms];
1974 }
1975
1976 // Adds a new portal for this room. Returns the portal number.
1977 // Initializes the flags
1978 int AddPortal(room *rp) {
1979 portal *newlist;
1980 int i;
1981
1982 newlist = (portal *)mem_malloc(sizeof(*newlist) * (rp->num_portals + 1));
1983
1984 // Copy from old list to new list, and free old list
1985 if (rp->num_portals) {
1986
1987 for (i = 0; i < rp->num_portals; i++)
1988 newlist[i] = rp->portals[i];
1989
1990 mem_free(rp->portals);
1991 }
1992
1993 // Point at new list
1994 rp->portals = newlist;
1995
1996 // Init flags
1997 rp->portals[rp->num_portals].flags = 0;
1998 rp->portals[rp->num_portals].bnode_index = -1;
1999
2000 return rp->num_portals++;
2001 }
2002
2003 // Links two rooms, creating portals in each room
2004 // Parameters: roomlist - pointer to the array of rooms
2005 // room0,face0 - the room & face numbers of the first room
2006 // room1,face1 - the room & face numbers of the second room
2007 void LinkRooms(room *roomlist, int room0, int face0, int room1, int face1) {
2008 room *rp0, *rp1;
2009 int pn0, pn1;
2010 int nv0, nv1;
2011
2012 // Set some vars
2013 rp0 = &roomlist[room0];
2014 rp1 = &roomlist[room1];
2015 nv0 = rp0->faces[face0].num_verts;
2016 nv1 = rp1->faces[face1].num_verts;
2017
2018 // Make sure no portals already
2019 ASSERT(rp0->faces[face0].portal_num == -1);
2020 ASSERT(rp1->faces[face1].portal_num == -1);
2021
2022 // Check for match
2023 if (nv0 != nv1)
2024 Int3(); // You must get Matt if you hit this!
2025
2026 // Create portals
2027 pn0 = AddPortal(rp0);
2028 pn1 = AddPortal(rp1);
2029
2030 // Link portals
2031 rp0->portals[pn0].croom = room1;
2032 rp0->portals[pn0].cportal = pn1;
2033 rp1->portals[pn1].croom = room0;
2034 rp1->portals[pn1].cportal = pn0;
2035
2036 // Add faces to portals
2037 rp0->portals[pn0].portal_face = face0;
2038 rp1->portals[pn1].portal_face = face1;
2039
2040 // Mark faces as being part of portals
2041 rp0->faces[face0].portal_num = pn0;
2042 rp1->faces[face1].portal_num = pn1;
2043 }
2044
2045 // Finds the min and max x,y,z values of the vertices in a room
2046 // Parameters: min,max - filled in with the minimum and maximum x,y, & z values, respectively
2047 // rp = the room
2048 void ComputeRoomMinMax(vector *min, vector *max, room *rp) {
2049 min->x = min->y = min->z = FLT_MAX;
2050 max->x = max->y = max->z = -FLT_MAX;
2051
2052 for (int i = 0; i < rp->num_verts; i++) {
2053 if (rp->verts[i].x > max->x)
2054 max->x = rp->verts[i].x;
2055 if (rp->verts[i].y > max->y)
2056 max->y = rp->verts[i].y;
2057 if (rp->verts[i].z > max->z)
2058 max->z = rp->verts[i].z;
2059
2060 if (rp->verts[i].x < min->x)
2061 min->x = rp->verts[i].x;
2062 if (rp->verts[i].y < min->y)
2063 min->y = rp->verts[i].y;
2064 if (rp->verts[i].z < min->z)
2065 min->z = rp->verts[i].z;
2066 }
2067 }
2068
2069 // Builds a list of all the vertices in a room that are part of a portal
2070 // Parameters: rp - the room to check
2071 // list - filled in with the list of vert numbers. List should be
2072 //MAX_VERTS_PER_ROOM big Returns: the number of verts in the list
2073 int BuildListOfPortalVerts(room *rp, int *list) {
2074 int i, t, j;
2075 int count = 0;
2076
2077 for (i = 0; i < rp->num_portals; i++) {
2078 face *fp = &rp->faces[rp->portals[i].portal_face];
2079
2080 for (t = 0; t < fp->num_verts; t++) {
2081 int v = fp->face_verts[t];
2082
2083 for (j = 0; j < count; j++)
2084 if (list[j] == v)
2085 break;
2086
2087 if (j == count)
2088 list[count++] = v;
2089 }
2090 }
2091
2092 return count;
2093 }
2094
2095 #define BUF_LEN 100000
2096
2097 char error_buf[BUF_LEN];
2098 int error_buf_offset;
2099
2100 // Log an error in the room check process
2101 void CheckError(const char *str, ...) {
2102 std::va_list arglist;
2103 int nchars;
2104
2105 va_start(arglist, str);
2106 nchars = std::vsnprintf(error_buf + error_buf_offset, BUF_LEN - error_buf_offset, str, arglist);
2107 va_end(arglist);
2108
2109 error_buf_offset += strlen(error_buf + error_buf_offset);
2110
2111 if (error_buf_offset >= BUF_LEN)
2112 OutrageMessageBox("There has been a text buffer overflow in CheckError().");
2113 }
2114
2115 // Checks the normals in a room
2116 // Returns the number of bad normals
2117 int CheckNormals(room *rp) {
2118 int errors = 0;
2119
2120 for (int f = 0; f < rp->num_faces; f++)
2121 if (!ComputeFaceNormal(rp, f)) {
2122 CheckError("Room %3d face %3d has a bad or low-precision normal\n", ROOMNUM(rp), f);
2123 errors++;
2124 }
2125
2126 return errors;
2127 }
2128
2129 // Checks for concave faces
2130 // Returns the number of concave faces
2131 int CheckConcaveFaces(room *rp) {
2132 int f;
2133 face *fp;
2134 int errors = 0;
2135
2136 // Go through all the faces
2137 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
2138
2139 int vert = CheckFaceConcavity(fp->num_verts, fp->face_verts, &fp->normal, rp->verts);
2140 if (vert != -1) {
2141 CheckError("Room %3d face %3d is concave at vertex %d\n", ROOMNUM(rp), f, vert);
2142 errors++;
2143 }
2144 }
2145
2146 return errors;
2147 }
2148
2149 int CheckDegenerateFaces(room *rp) {
2150 int errors = 0;
2151 int f, v;
2152 face *fp;
2153
2154 // Go through all the faces
2155 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
2156 for (v = 0; v < fp->num_verts; v++)
2157 if (fp->face_verts[v] == fp->face_verts[(v + 2) % fp->num_verts]) {
2158 CheckError("Room %3d face %3d is degenerate\n", ROOMNUM(rp), f);
2159 errors++;
2160 break;
2161 }
2162 }
2163
2164 return errors;
2165 }
2166
2167 // Make sure the two faces of a portal match
2168 // Returns true if the portal is ok
2169 bool CheckPortal(room *rp0, int p0) {
2170 int i, j, p1;
2171 room *rp1;
2172 portal *pp0, *pp1;
2173 face *fp0, *fp1;
2174
2175 // Get info
2176 pp0 = &rp0->portals[p0];
2177 rp1 = &Rooms[pp0->croom];
2178 p1 = pp0->cportal;
2179 pp1 = &rp1->portals[p1];
2180 fp0 = &rp0->faces[pp0->portal_face];
2181 fp1 = &rp1->faces[pp1->portal_face];
2182
2183 // Make sure faces have the same number of verts
2184 if (fp0->num_verts != fp1->num_verts) {
2185 CheckError("Room %d portal %d has a different number of vertices (%d) than room %d portal %d (%d)\n", ROOMNUM(rp0),
2186 p0, fp0->num_verts, ROOMNUM(rp1), p1, fp1->num_verts);
2187 return 0;
2188 }
2189
2190 // Find one point in common
2191 for (i = 0; i < fp0->num_verts; i++) {
2192 for (j = 0; j < fp1->num_verts; j++)
2193 if (PointsAreSame(&rp0->verts[fp0->face_verts[i]], &rp1->verts[fp1->face_verts[j]]))
2194 break;
2195 if (j < fp1->num_verts)
2196 break;
2197 }
2198 if (i >= fp0->num_verts) {
2199 CheckError("Cannot find a single matching point between room %d portal %d and room %d portal %d\n", ROOMNUM(rp0),
2200 p0, ROOMNUM(rp1), p1);
2201 return 0;
2202 }
2203
2204 vector *v0, *v1, *prev0, *prev1;
2205 int n;
2206
2207 // Trace through verts in faces, making sure they match
2208 prev0 = &rp0->verts[fp0->face_verts[i]];
2209 prev1 = &rp1->verts[fp1->face_verts[j]];
2210 for (n = 1; n < fp0->num_verts; n++) {
2211
2212 v0 = &rp0->verts[fp0->face_verts[(i + n) % fp0->num_verts]];
2213 v1 = &rp1->verts[fp1->face_verts[(j - n + fp1->num_verts) % fp1->num_verts]];
2214
2215 if (!PointsAreSame(v0, v1)) {
2216 CheckError("Vertex %d in room %d portal %d does not match vertex %d in room %d portal %d\n",
2217 (i + n) % fp0->num_verts, ROOMNUM(rp0), p0, (j - n + fp1->num_verts) % fp1->num_verts, ROOMNUM(rp1),
2218 p1);
2219 return 0;
2220 }
2221 }
2222
2223 return 1;
2224 }
2225
2226 extern void DumpTextToClipboard(char *text);
2227
2228 // Checks for bad portal connections
2229 // Returns the number of bad portals
2230 int CheckPortals(room *rp) {
2231 portal *pp;
2232 int p;
2233 int r = ROOMNUM(rp);
2234 int errors = 0;
2235
2236 for (p = 0, pp = rp->portals; p < rp->num_portals; pp++, p++) {
2237
2238 // Make sure the connecting portal points back to this portal
2239 if ((Rooms[pp->croom].portals[pp->cportal].croom != r) || (Rooms[pp->croom].portals[pp->cportal].cportal != p)) {
2240 CheckError("Room %d, portal %d points at room %d portal %d, but the latter points at room %d portal %d\n", r, p,
2241 pp->croom, pp->cportal, Rooms[pp->croom].portals[pp->cportal].croom,
2242 Rooms[pp->croom].portals[pp->cportal].cportal);
2243 errors++;
2244 }
2245
2246 // Check to make sure the face in this portal points back to it
2247 if (rp->faces[pp->portal_face].portal_num != p) {
2248 CheckError("Room %d portal %d contains face %d, but that face points at portal %d\n", r, p, pp->portal_face,
2249 rp->faces[pp->portal_face].portal_num);
2250 errors++;
2251 }
2252 }
2253
2254 return errors;
2255 }
2256
2257 // Checks if the given face is a duplicate of another face
2258 // Returns the face number of which this is a duplicate, or -1 if not a duplicate
2259 int CheckForDuplicateFace(room *rp, int facenum) {
2260 face *fp0 = &rp->faces[facenum];
2261 int r = ROOMNUM(rp);
2262
2263 for (int j = 0; j < facenum; j++) {
2264 face *fp1 = &rp->faces[j];
2265
2266 if (fp0->num_verts == fp1->num_verts)
2267 for (int v = 0; v < fp1->num_verts; v++) // look for a shared vert
2268 if (fp1->face_verts[v] == fp0->face_verts[0]) {
2269 for (int t = 0; t < fp0->num_verts; t++)
2270 if (fp0->face_verts[t] != fp1->face_verts[(v + t) % fp1->num_verts])
2271 break;
2272 if (t == fp0->num_verts) {
2273 return j;
2274 }
2275 break;
2276 }
2277 }
2278
2279 // Didn't find a duplicate
2280 return -1;
2281 }
2282
2283 // Removes all the duplicate faces in a room
2284 void RemoveDuplicateFaces(room *rp) {
2285 int r = ROOMNUM(rp);
2286 int removed = 0;
2287
2288 for (int i = 0; i < rp->num_faces; i++) {
2289 int dup = CheckForDuplicateFace(rp, i);
2290
2291 if (dup != -1) {
2292 DeleteRoomFace(rp, i);
2293 mprintf(0, "Removed face %d (duplicate of face %d) from room %d\n", i, dup, r);
2294 removed++;
2295 i--;
2296 }
2297 }
2298
2299 if (removed) {
2300 OutrageMessageBox("%d duplicate faces have been removed from room %d.", removed, r);
2301 World_changed = 1;
2302 } else
2303 OutrageMessageBox("There are no duplicate faces in room %d.", r);
2304 }
2305
2306 // Checks for duplicate faces
2307 // Returns the number of duplicate faces
2308 int CheckDuplicateFaces(room *rp) {
2309 int r = ROOMNUM(rp);
2310 int errors = 0;
2311
2312 for (int i = 0; i < rp->num_faces; i++) {
2313 int dup = CheckForDuplicateFace(rp, i);
2314
2315 if (dup != -1) {
2316 CheckError("Room %d: face %d is the same as face %d\n", r, i, dup);
2317 errors++;
2318 }
2319 }
2320
2321 return errors;
2322 }
2323
2324 // Checks for non-planar faces
2325 // Returns the number of non-planar faces
2326 int CheckNonPlanarFaces(room *rp) {
2327 int r = ROOMNUM(rp);
2328 int errors = 0;
2329
2330 for (int f = 0; f < rp->num_faces; f++)
2331 if (!FaceIsPlanar(rp, f)) {
2332 if (rp->faces[f].portal_num != -1)
2333 CheckError("Room %d: face %d is not planar. FACE IS PORTAL!\n", r, f);
2334 else
2335 CheckError("Room %d: face %d is not planar\n", r, f);
2336 errors++;
2337 }
2338
2339 return errors;
2340 }
2341
2342 // Checks for duplicate points
2343 // Returns the number of duplicate points
2344 int CheckDuplicatePoints(room *rp) {
2345 int r = ROOMNUM(rp);
2346 int errors = 0;
2347
2348 for (int i = 0; i < rp->num_verts; i++)
2349 for (int j = 0; j < i; j++)
2350 if (PointsAreSame(&rp->verts[i], &rp->verts[j])) {
2351 float d = vm_VectorDistance(&rp->verts[i], &rp->verts[j]);
2352 CheckError("Room %d: vert %3d is the same as vert %3d (d=%.2f)\n", r, i, j, d);
2353 errors++;
2354 }
2355
2356 return errors;
2357 }
2358
2359 // Find any unused points in a room
2360 int CheckUnusedPoints(room *rp) {
2361 int r = ROOMNUM(rp);
2362 int errors = 0;
2363 bool vert_used[MAX_VERTS_PER_ROOM];
2364 int f, v;
2365 face *fp;
2366
2367 // Init all the flags to unused
2368 for (v = 0; v < rp->num_verts; v++)
2369 vert_used[v] = 0;
2370
2371 // Go through all the faces & flag the used verts
2372 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
2373 for (v = 0; v < fp->num_verts; v++)
2374 vert_used[fp->face_verts[v]] = 1;
2375
2376 // Now delete the unused verts
2377 for (v = 0; v < rp->num_verts; v++)
2378 if (!vert_used[v]) {
2379 CheckError("Room %d: point %d is unused\n", r, v);
2380 errors++;
2381 }
2382
2383 // Done
2384 return errors;
2385 }
2386
2387 // Checks for duplicate points in faces
2388 // Returns the number of duplicate face points
2389 int CheckDuplicateFacePoints(room *rp) {
2390 int r = ROOMNUM(rp);
2391 int f, v, errors = 0;
2392 face *fp;
2393
2394 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
2395 for (v = 0; v < fp->num_verts; v++)
2396 if (fp->face_verts[v] == fp->face_verts[(v + 1) % fp->num_verts]) {
2397 CheckError("Room %d, face %d: vert #%d (%d) is the same as #%d\n", r, f, (v + 1) % fp->num_verts,
2398 fp->face_verts[v], v);
2399 errors++;
2400 }
2401
2402 return errors;
2403 }
2404
2405 // Returns true if faces match exactly, else false
2406 int CheckPortalFaces(room *rp0, int facenum0, room *rp1, int facenum1) {
2407 face *fp0 = &rp0->faces[facenum0];
2408 face *fp1 = &rp1->faces[facenum1];
2409 vector *v0, *v1, *prev_v0, *prev_v1;
2410 int n, i, j, prev_vn0, prev_vn1;
2411 int points_added = 0;
2412
2413 // First, find one point in common
2414 for (i = 0; i < fp0->num_verts; i++) {
2415 for (j = 0; j < fp1->num_verts; j++)
2416 if (PointsAreSame(&rp0->verts[fp0->face_verts[i]], &rp1->verts[fp1->face_verts[j]]))
2417 break;
2418 if (j < fp1->num_verts)
2419 break;
2420 }
2421 if (i >= fp0->num_verts)
2422 return 0;
2423
2424 prev_vn0 = fp0->face_verts[i];
2425 prev_vn1 = fp1->face_verts[j];
2426 prev_v0 = &rp0->verts[prev_vn0];
2427 prev_v1 = &rp1->verts[prev_vn1];
2428
2429 if (fp0->num_verts != fp1->num_verts) {
2430 CheckError("Room %d: portal face %d does not match %d:%d\n", ROOMNUM(rp0), facenum0, ROOMNUM(rp1), facenum1);
2431 return 0;
2432 }
2433
2434 // Trace through faces, adding points where needed
2435 for (n = 1; n < fp0->num_verts; n++) {
2436 int vn0, vn1;
2437
2438 vn0 = fp0->face_verts[(i + n) % fp0->num_verts];
2439 vn1 = fp1->face_verts[(j - n + fp1->num_verts) % fp1->num_verts];
2440
2441 v0 = &rp0->verts[vn0];
2442 v1 = &rp1->verts[vn1];
2443
2444 if (!PointsAreSame(v0, v1)) { // Points are at least very close.
2445 CheckError("Room %d: portal face %d does not match %d:%d\n", ROOMNUM(rp0), facenum0, ROOMNUM(rp1), facenum1);
2446 return 0;
2447 }
2448
2449 prev_vn0 = vn0;
2450 prev_vn1 = vn1;
2451
2452 prev_v0 = v0;
2453 prev_v1 = v1;
2454 }
2455
2456 return 1; // no errors found, so faces ok
2457 }
2458
2459 int CheckRoomPortalFaces(room *rp) {
2460 int errors = 0;
2461
2462 for (int p = 0; p < rp->num_portals; p++) {
2463 portal *pp = &rp->portals[p];
2464 if (!CheckPortalFaces(rp, pp->portal_face, &Rooms[pp->croom], Rooms[pp->croom].portals[pp->cportal].portal_face))
2465 errors++;
2466 }
2467
2468 return errors;
2469 }
2470
2471 // Find t-joints in this room
2472 int FindTJoints(room *rp) {
2473 int f, f2, v, v2;
2474 face *fp, *fp2;
2475 int tjoints = 0;
2476 int points_added = 0;
2477
2478 // Now search for and fix t-joints
2479 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
2480 for (v = 0; v < fp->num_verts; v++) {
2481 int vv0 = fp->face_verts[v], vv1 = fp->face_verts[(v + 1) % fp->num_verts];
2482 for (f2 = 0, fp2 = rp->faces; f2 < rp->num_faces; f2++, fp2++) {
2483 if (f2 != f) {
2484 for (v2 = 0; v2 < fp2->num_verts; v2++) {
2485 int tt0 = fp2->face_verts[v2], tt1 = fp2->face_verts[(v2 + 1) % fp2->num_verts];
2486
2487 if ((vv0 == tt1) && (vv1 == tt0))
2488 break; // found one, so stop
2489 }
2490 if (v2 < fp2->num_verts)
2491 break;
2492 }
2493 }
2494 if (f2 == rp->num_faces) { // didn't find a match
2495
2496 // Look for vert on this edge
2497 for (f2 = 0, fp2 = rp->faces; f2 < rp->num_faces; f2++, fp2++) {
2498 if (f2 != f) {
2499 for (v2 = 0; v2 < fp2->num_verts; v2++) {
2500 int tt0 = fp2->face_verts[v2], tt1 = fp2->face_verts[(v2 + 1) % fp2->num_verts];
2501
2502 if (vv0 == tt1) { // one point maches; check if next is on the edge
2503 if (CheckPointToPlane(&rp->verts[tt0], &rp->verts[vv0], &fp->normal) == 0) {
2504 if (CheckPointAgainstEdge(&rp->verts[tt0], &rp->verts[vv0], &rp->verts[vv1], &fp->normal) == 0) {
2505 // make sure the new point is actually between the two edge points
2506 float edge_len = vm_VectorDistance(&rp->verts[vv1], &rp->verts[vv0]);
2507 float d0 = vm_VectorDistance(&rp->verts[tt0], &rp->verts[vv0]);
2508 float d1 = vm_VectorDistance(&rp->verts[tt0], &rp->verts[vv1]);
2509 if ((d0 < edge_len) && (d1 < edge_len)) {
2510 CheckError("Room %d: face %d edge %d has a T-joint\n", ROOMNUM(rp), f, v);
2511 tjoints++;
2512 goto next_face;
2513 }
2514 }
2515 }
2516 }
2517 }
2518 }
2519 }
2520 }
2521 }
2522 next_face:;
2523 }
2524
2525 return tjoints;
2526 }
2527
2528 // Counts the number of unique textures in a level, plus gives names of textures used
2529 void CountUniqueTextures() {
2530 uint16_t *texture_tracking = (uint16_t *)mem_malloc(MAX_TEXTURES * 2);
2531 ASSERT(texture_tracking);
2532 memset(texture_tracking, 0, MAX_TEXTURES * 2);
2533
2534 for (int i = 0; i <= Highest_room_index; i++) {
2535 room *rp = &Rooms[i];
2536 if (rp->used == 0)
2537 continue;
2538
2539 for (int t = 0; t < rp->num_faces; t++) {
2540 face *fp = &rp->faces[t];
2541
2542 if (fp->portal_num != -1) {
2543 if (!(rp->portals[fp->portal_num].flags & PF_RENDER_FACES))
2544 continue;
2545 }
2546
2547 if (fp->tmap != -1 && !(GameTextures[fp->tmap].flags & (TF_PROCEDURAL | TF_TEXTURE_64 | TF_TEXTURE_32))) {
2548 texture_tracking[fp->tmap]++;
2549 }
2550 }
2551 }
2552
2553 // Now count totals
2554 int total = 0, total_with_lights = 0;
2555 for (i = 0; i < MAX_TEXTURES; i++) {
2556 if (texture_tracking[i]) {
2557 if (!(GameTextures[i].flags & TF_LIGHT)) {
2558 total++;
2559 total_with_lights++;
2560 } else
2561 total_with_lights++;
2562 }
2563 }
2564
2565 CheckError("There are %d unique 128x128 textures (excluding lights) in this level:\n", total);
2566 CheckError("There are %d unique 128x128 textures (including lights) in this level:\n", total_with_lights);
2567
2568 if (total > 60)
2569 CheckError("ERROR: YOU HAVE MORE THAT 60 128x128 TEXTURES...YOU *MUST* FIX THIS!\n");
2570
2571 for (i = 0; i < MAX_TEXTURES; i++) {
2572 if (texture_tracking[i] && !(GameTextures[i].flags & TF_LIGHT)) {
2573 CheckError("%d : %s %s bmp=%s\n", texture_tracking[i], GameTextures[i].name,
2574 (GameTextures[i].flags & TF_ANIMATED) ? "(Animated)" : "",
2575 (GameTextures[i].flags & TF_ANIMATED) ? "" : GameBitmaps[GameTextures[i].bm_handle].name);
2576 }
2577 }
2578
2579 for (i = 0; i < MAX_TEXTURES; i++) {
2580 if (texture_tracking[i] && (GameTextures[i].flags & TF_LIGHT)) {
2581 CheckError("%d : %s %s %s bmp=%s\n", texture_tracking[i], GameTextures[i].name,
2582 (GameTextures[i].flags & TF_ANIMATED) ? "(Animated)" : "",
2583 (GameTextures[i].flags & TF_LIGHT) ? "(Light)" : "",
2584 (GameTextures[i].flags & TF_ANIMATED) ? "" : GameBitmaps[GameTextures[i].bm_handle].name);
2585 }
2586 }
2587
2588 mem_free(texture_tracking);
2589 }
2590
2591 // Returns the number of soundsource objects that don't have a sound attached
2592 int CheckSoundsourceObjects() {
2593 int objnum, total = 0, named = 0, quiet = 0, quiet_unnamed = 0;
2594 object *objp;
2595
2596 for (objnum = 0, objp = Objects; objnum <= Highest_object_index; objnum++, objp++) {
2597 if (objp->type == OBJ_SOUNDSOURCE) {
2598 ASSERT(objp->control_type == CT_SOUNDSOURCE);
2599 total++;
2600 if (objp->name)
2601 named++;
2602 if (objp->ctype.soundsource_info.sound_index == -1) {
2603 CheckError("Soundsource object %d (\"%s\") has no sound specified.\n", objnum,
2604 objp->name ? objp->name : "<no name>");
2605 quiet++;
2606 if (!objp->name)
2607 quiet_unnamed++;
2608 }
2609 }
2610 }
2611
2612 CheckError(
2613 "This level has %d soundsource objects; %d have names, %d don't have sounds, and %d have no sounds & no name.\n",
2614 total, named, quiet, quiet_unnamed);
2615
2616 return quiet;
2617 }
2618
2619 int BOAGetMineChecksum();
2620
2621 // Get rid of large whole parts of face UV coordinates
2622 void NormalizeFaceUVs() {
2623 int r;
2624 room *rp;
2625
2626 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
2627 if (rp->used) {
2628 int f;
2629 face *fp;
2630
2631 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
2632 float base_u = floor(fp->face_uvls[0].u);
2633 float base_v = floor(fp->face_uvls[0].v);
2634
2635 for (int v = 0; v < fp->num_verts; v++) {
2636 fp->face_uvls[v].u -= base_u;
2637 fp->face_uvls[v].v -= base_v;
2638 }
2639 }
2640 }
2641 }
2642 }
2643
2644 // Test the mine for validity
2645 void VerifyMine() {
2646 int r;
2647 room *rp;
2648 int errors = 0, bad_normals = 0, concave_faces = 0, degenerate_faces = 0, bad_portals = 0, duplicate_faces = 0,
2649 duplicate_points = 0, duplicate_face_points = 0, unused_points = 0, nonplanar_faces = 0, mismatched_portals = 0,
2650 tjoints = 0, bad_shells = 0, quiet_soundsource_objects = 0;
2651
2652 error_buf_offset = 0;
2653
2654 // Normalize all the UV coordinates in the level
2655 NormalizeFaceUVs();
2656
2657 // Check normals, portals, & duplicate faces
2658 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++)
2659 if (rp->used) {
2660
2661 bad_normals += CheckNormals(rp);
2662 errors += bad_normals;
2663
2664 concave_faces += CheckConcaveFaces(rp);
2665 errors += concave_faces;
2666
2667 degenerate_faces += CheckDegenerateFaces(rp);
2668 errors += degenerate_faces;
2669
2670 bad_portals += CheckPortals(rp);
2671 errors += bad_portals;
2672
2673 duplicate_faces += CheckDuplicateFaces(rp);
2674 errors += duplicate_faces;
2675
2676 nonplanar_faces += CheckNonPlanarFaces(rp);
2677 errors += nonplanar_faces;
2678
2679 duplicate_points += CheckDuplicatePoints(rp);
2680 errors += duplicate_points;
2681
2682 duplicate_face_points += CheckDuplicateFacePoints(rp);
2683 errors += duplicate_face_points;
2684
2685 unused_points += CheckUnusedPoints(rp);
2686 errors += unused_points;
2687
2688 mismatched_portals += CheckRoomPortalFaces(rp);
2689 errors += mismatched_portals;
2690
2691 tjoints += FindTJoints(rp);
2692 errors += tjoints;
2693
2694 if ((rp->num_portals > 0) && !(rp->flags & RF_EXTERNAL)) {
2695 int shell_errors = ComputeRoomShell(rp);
2696 if (shell_errors) {
2697 bad_shells++;
2698 errors++;
2699 }
2700 }
2701 }
2702
2703 quiet_soundsource_objects = CheckSoundsourceObjects();
2704 errors += quiet_soundsource_objects;
2705
2706 CountUniqueTextures();
2707
2708 bool terrain_occluded = false;
2709 bool boa_ran = false;
2710 bool terrain_volume = false;
2711
2712 if (Terrain_occlusion_checksum == (Terrain_checksum + 1))
2713 terrain_occluded = true;
2714 else
2715 errors = 1;
2716
2717 if (BOAGetMineChecksum() == BOA_vis_checksum)
2718 boa_ran = true;
2719 else
2720 errors = true;
2721
2722 // Check to see if dynamic terrain lighting is calculated
2723 for (int i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH && !terrain_volume; i++) {
2724 if (Terrain_dynamic_table[i] != 255)
2725 terrain_volume = true;
2726 }
2727
2728 if (!terrain_volume)
2729 errors = true;
2730
2731 // Show message if errors
2732 if (errors) {
2733 mprintf(0, "Error buf size = %d\n", strlen(error_buf));
2734 DumpTextToClipboard(error_buf);
2735 OutrageMessageBox("Mine check results:\n"
2736 "\n"
2737 " Bad portals:\t\t%d\n"
2738 " Bad Normals:\t\t%d\n"
2739 " Concave faces:\t\t%d\n"
2740 " Degenerate faces:\t\t%d\n"
2741 " Duplicate faces:\t\t%d\n"
2742 " Non-planar faces:\t\t%d\n"
2743 " Duplicate points:\t\t%d\n"
2744 " Duplicate face points:\t%d\n"
2745 " Unused points:\t\t%d\n"
2746 " Unmatched portals:\t\t%d\n"
2747 " T-Joints:\t\t\t%d\n"
2748 " Bad shells:\t\t%d\n"
2749 " Quiet soundsource objs:\t%d\n"
2750 " Terrain occlusion:\t\t%s\n"
2751 " BOA:\t\t\t%s\n"
2752 " Terrain volume lighting:\t\t\t%s\n"
2753 "\n"
2754 "\n"
2755 "For detailed info, see clipboard.",
2756 bad_portals, bad_normals, concave_faces, degenerate_faces, duplicate_faces, nonplanar_faces,
2757 duplicate_points, duplicate_face_points, unused_points, mismatched_portals, tjoints, bad_shells,
2758 quiet_soundsource_objects, terrain_occluded ? "Valid" : "NOT VALID",
2759 boa_ran ? "Valid" : "NOT VALID", terrain_volume ? "Valid" : "NOT VALID");
2760 } else {
2761 OutrageMessageBox("Mine has no errors, but check clipboard for texture counts.");
2762 }
2763 }
2764
2765 // Does CheckMine() stuff on one room only
2766 void VerifyRoom(room *rp) {
2767 int errors, bad_normals, concave_faces, duplicate_faces, duplicate_points, duplicate_face_points, unused_points,
2768 nonplanar_faces;
2769
2770 // Reset error buffer
2771 error_buf_offset = 0;
2772
2773 errors = bad_normals = CheckNormals(rp);
2774
2775 concave_faces = CheckConcaveFaces(rp);
2776 errors += concave_faces;
2777
2778 duplicate_faces = CheckDuplicateFaces(rp);
2779 errors += duplicate_faces;
2780
2781 nonplanar_faces = CheckNonPlanarFaces(rp);
2782 errors += nonplanar_faces;
2783
2784 duplicate_points = CheckDuplicatePoints(rp);
2785 errors += duplicate_points;
2786
2787 duplicate_face_points = CheckDuplicateFacePoints(rp);
2788 errors += duplicate_face_points;
2789
2790 unused_points = CheckUnusedPoints(rp);
2791 errors += unused_points;
2792
2793 // Show message if errors
2794 if (errors) {
2795 DumpTextToClipboard(error_buf);
2796 OutrageMessageBox("Room check results:\n"
2797 "\n"
2798 " Bad Normals:\t\t%d\n"
2799 " Concave faces:\t\t%d\n"
2800 " Duplicate faces:\t\t%d\n"
2801 " Non-planar faces:\t\t%d\n"
2802 " Duplicate points:\t\t%d\n"
2803 " Duplicate face points:\t%d\n"
2804 " Unused points:\t\t%d\n"
2805 "\n"
2806 "\n"
2807 "For detailed info, see clipboard.",
2808 bad_normals, concave_faces, duplicate_faces, nonplanar_faces, duplicate_points,
2809 duplicate_face_points, unused_points);
2810 } else
2811 OutrageMessageBox("Room has no errors.");
2812 }
2813
2814 // Copy the flags from one face to another derrived from the first (by clipping or splitting)
2815 // Only those flags which are safe to copy are copied
2816 // Parameters: dfp - pointer to the destination face
2817 // sfp - pointer to the source face
2818 void CopyFaceFlags(face *dfp, face *sfp) {
2819 dfp->flags = 0;
2820
2821 //@@if (sfp->flags & FF_FORCEFIELD)
2822 //@@ dfp->flags |= FF_FORCEFIELD;
2823
2824 if (sfp->flags & FF_GOALFACE)
2825 dfp->flags |= FF_GOALFACE;
2826 }
2827
2828 // Inserts a vertex into a face
2829 // Parameters: rp - the room for this face
2830 // facenum - the face to which to add the vertex
2831 // new_v - the number of the vertex to add
2832 // after_v - the new vert is added *after* this vert (index into face verts)
2833 void AddVertToFace(room *rp, int facenum, int new_v, int after_v) {
2834 face *fp = &rp->faces[facenum];
2835 int old_verts[MAX_VERTS_PER_FACE];
2836 roomUVL old_uvls[MAX_VERTS_PER_FACE];
2837 int t;
2838
2839 // Make copy of old verts
2840 for (t = 0; t < fp->num_verts; t++) {
2841 old_verts[t] = fp->face_verts[t];
2842 old_uvls[t] = fp->face_uvls[t];
2843 }
2844
2845 // Allocate for new verts
2846 ReInitRoomFace(fp, fp->num_verts + 1);
2847
2848 // Copy old verts
2849 for (t = 0; t <= after_v; t++) {
2850 fp->face_verts[t] = old_verts[t];
2851 fp->face_uvls[t] = old_uvls[t];
2852 }
2853
2854 // Insert new vert
2855 fp->face_verts[t++] = new_v;
2856
2857 // Copy rest of new verts
2858 for (; t < fp->num_verts; t++) {
2859 fp->face_verts[t] = old_verts[t - 1];
2860 fp->face_uvls[t] = old_uvls[t - 1];
2861 }
2862
2863 // Compute the uv values for the new vert
2864 vector *v0 = &rp->verts[fp->face_verts[after_v]], *v1 = &rp->verts[fp->face_verts[(after_v + 2) % fp->num_verts]],
2865 *vn = &rp->verts[fp->face_verts[after_v + 1]];
2866 roomUVL *uv0 = &fp->face_uvls[after_v], *uv1 = &fp->face_uvls[(after_v + 2) % fp->num_verts];
2867
2868 float k = vm_VectorDistance(vn, v0) / vm_VectorDistance(v1, v0);
2869
2870 fp->face_uvls[after_v + 1].u = uv0->u + k * (uv1->u - uv0->u);
2871 fp->face_uvls[after_v + 1].v = uv0->v + k * (uv1->v - uv0->v);
2872 fp->face_uvls[after_v + 1].u2 = uv0->u2 + k * (uv1->u2 - uv0->u2);
2873 fp->face_uvls[after_v + 1].v2 = uv0->v2 + k * (uv1->v2 - uv0->v2);
2874 fp->face_uvls[after_v + 1].alpha = uv0->alpha + k * (uv1->alpha - uv0->alpha);
2875 }
2876
2877 // Removes the duplicate points in a room
2878 // Returns the number removed
2879 int RemoveDuplicatePoints(room *rp) {
2880 face *fp;
2881 int f, v;
2882 int n_fixed = 0;
2883
2884 for (int i = 0; i < rp->num_verts; i++)
2885 for (int j = 0; j < i; j++)
2886 if (PointsAreSame(&rp->verts[i], &rp->verts[j])) {
2887
2888 // Replace the higher-numbered point with the lower-numbered in all the faces in this room
2889 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
2890 for (v = 0; v < fp->num_verts; v++)
2891 if (fp->face_verts[v] == i)
2892 fp->face_verts[v] = j;
2893
2894 // Delete the now-unused vert
2895 DeleteRoomVert(rp, i);
2896
2897 n_fixed++;
2898
2899 i--; // back up, since the point we're checking is now gone
2900 break; // don't keep checking for duplicates
2901 }
2902
2903 return n_fixed;
2904 }
2905
2906 // Remove duplicate points in faces
2907 // Returns the number of duplicate face points removed
2908 int RemoveDuplicateFacePoints(room *rp) {
2909 int r = ROOMNUM(rp);
2910 int f, v, n_fixed = 0;
2911 face *fp;
2912
2913 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
2914 for (v = 0; v < fp->num_verts; v++)
2915 if (fp->face_verts[v] == fp->face_verts[(v + 1) % fp->num_verts]) {
2916 int new_verts[MAX_VERTS_PER_FACE];
2917 roomUVL new_uvls[MAX_VERTS_PER_FACE];
2918
2919 for (int t = 0; t < v; t++) {
2920 new_verts[t] = fp->face_verts[t];
2921 new_uvls[t] = fp->face_uvls[t];
2922 }
2923 for (; t < fp->num_verts - 1; t++) {
2924 new_verts[t] = fp->face_verts[t + 1];
2925 new_uvls[t] = fp->face_uvls[t + 1];
2926 }
2927
2928 ReInitRoomFace(fp, fp->num_verts - 1);
2929
2930 for (t = 0; t < fp->num_verts; t++) {
2931 fp->face_verts[t] = new_verts[t];
2932 fp->face_uvls[t] = new_uvls[t];
2933 }
2934
2935 n_fixed++;
2936 f--;
2937 fp--; // recheck this face
2938 break;
2939 }
2940
2941 return n_fixed;
2942 }
2943
2944 // Removes all the duplicate points in a level
2945 void RemoveAllDuplicateAndUnusedPoints() {
2946 int r;
2947 room *rp;
2948 int n_unused = 0, n_duplicate = 0, n_duplicate_face = 0;
2949
2950 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++)
2951 if (rp->used) {
2952 n_unused += DeleteUnusedRoomVerts(rp);
2953 n_duplicate += RemoveDuplicatePoints(rp);
2954 n_duplicate_face += RemoveDuplicateFacePoints(rp);
2955 }
2956
2957 OutrageMessageBox(" %d Unused points deleted\n"
2958 " %d Duplicate points deleted\n"
2959 " %d Duplicate face points deleted\n",
2960 n_unused, n_duplicate, n_duplicate_face);
2961
2962 if (n_unused || n_duplicate || n_duplicate_face)
2963 World_changed = 1;
2964 }
2965
2966 // Returns the number fixed
2967 void FixDegenerateFaces() {
2968 int fixed = 0, deleted = 0;
2969 int r, f, v;
2970 face *fp;
2971 room *rp;
2972
2973 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
2974 if (rp->used) {
2975 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
2976 bool face_fixed = 0;
2977 recheck_face:;
2978 if (fp->num_verts < 3) {
2979 DeleteRoomFace(rp, f);
2980 deleted++;
2981 face_fixed = 0; // deleted overrides fixed
2982 f--;
2983 fp = &rp->faces[f];
2984 } else {
2985 for (v = 0; v < fp->num_verts; v++) {
2986 if (fp->face_verts[v] == fp->face_verts[(v + 2) % fp->num_verts]) {
2987 int16_t tverts[MAX_VERTS_PER_FACE];
2988 roomUVL tuvls[MAX_VERTS_PER_FACE];
2989
2990 for (int i = 0; i < fp->num_verts - 2; i++) {
2991 tverts[i] = fp->face_verts[(v + 2 + i) % fp->num_verts];
2992 tuvls[i] = fp->face_uvls[(v + 2 + i) % fp->num_verts];
2993 }
2994 for (i = 0; i < fp->num_verts - 2; i++) {
2995 fp->face_verts[i] = tverts[i];
2996 fp->face_uvls[i] = tuvls[i];
2997 }
2998 fp->num_verts -= 2;
2999 face_fixed = 1;
3000 goto recheck_face;
3001 }
3002 }
3003 }
3004 if (face_fixed)
3005 fixed++;
3006 }
3007 }
3008 }
3009
3010 OutrageMessageBox("%d Degenerate faces fixed\n%d Degenerate faces deleted", fixed, deleted);
3011
3012 if (fixed || deleted)
3013 World_changed = 1;
3014 }
3015
3016 // Finda a face that connects to the specified face
3017 // Starts looking at startface
3018 // Returns the attached facenum, or -1 if found none
3019 int FindConnectedFace(room *rp, int facenum, int edgenum, int startface) {
3020 face *fp0 = &rp->faces[facenum], *fp1;
3021 int a0, b0, a1, b1;
3022 int f;
3023
3024 // Get edge verts - <a0,b0> is edge on first face
3025 a0 = fp0->face_verts[edgenum];
3026 b0 = fp0->face_verts[(edgenum + 1) % fp0->num_verts];
3027
3028 for (f = startface, fp1 = &rp->faces[startface]; f < rp->num_faces; f++, fp1++) {
3029
3030 if (f == facenum)
3031 continue;
3032
3033 for (int e = 0; e < fp1->num_verts; e++) {
3034
3035 // Get edge verts - <a1,b1> is edge on second face
3036 a1 = fp1->face_verts[e];
3037 b1 = fp1->face_verts[(e + 1) % fp1->num_verts];
3038
3039 //@@if ((a0==a1) && (b0==b1))
3040 //@@ Int3(); //If you hit this, you probably have a duplicate
3041 //or overlapping face
3042
3043 if ((a0 == b1) && (b0 == a1)) { // found match!
3044 return f;
3045 }
3046 }
3047 }
3048
3049 return -1; // no match
3050 }
3051
3052 // Shell flags
3053 #define SHELL_NONE 0
3054 #define SHELL_UNCHECKED 1
3055 #define SHELL_CLOSED 2
3056 #define SHELL_ERROR 3
3057
3058 // Finds the shell for the specified room. If the shell is found with no errors, sets
3059 // the non-shell flag for those faces not in the shell. If there are errors finding the
3060 // shell, all faces have the non-shell flag cleared.
3061 // Assumes all portals are part of the shell and starts checking from those faces
3062 // Returns the number of shell errors (unconnected edges) in the room.
3063 // Writes errors to the error buffer
3064 int ComputeRoomShell(room *rp) {
3065 uint8_t shell_flags[MAX_FACES_PER_ROOM];
3066 bool done = 0;
3067 int errors = 0;
3068 int f;
3069
3070 for (f = 0; f < rp->num_faces; f++)
3071 shell_flags[f] = SHELL_NONE;
3072
3073 // Start with the portal faces
3074 ASSERT(rp->num_portals > 0);
3075 for (int p = 0; p < rp->num_portals; p++)
3076 shell_flags[rp->portals[p].portal_face] = SHELL_UNCHECKED;
3077
3078 // Check all the unchecked faces
3079 while (!done) {
3080
3081 for (f = 0; f < rp->num_faces; f++) {
3082
3083 if (shell_flags[f] == SHELL_UNCHECKED) {
3084 face *fp = &rp->faces[f];
3085
3086 for (int e = 0; e < fp->num_verts; e++) {
3087
3088 int t = FindConnectedFace(rp, f, e, 0);
3089
3090 if (t != -1) {
3091 int t2;
3092
3093 // See if any additional faces
3094 t2 = FindConnectedFace(rp, f, e, t + 1);
3095 if (t2 != -1) {
3096 // mprintf(0,"Room %d face %d: Found second connection for edge %d (face %d)\n",ROOMNUM(rp),f,e,t);
3097 // CheckError("Room %d face %d: Found second connection for edge %d (face %d)\n",ROOMNUM(rp),f,e,t);
3098 } else { // No double-edge, so add connected face
3099
3100 if (shell_flags[t] == SHELL_NONE)
3101 shell_flags[t] = SHELL_UNCHECKED;
3102 }
3103 } else {
3104 // mprintf(0,"Room %d face %d: No connection for edge %d\n",ROOMNUM(rp),f,e);
3105 CheckError("Room %d face %d: No connection for edge %d\n", ROOMNUM(rp), f, e);
3106 shell_flags[f] = SHELL_ERROR;
3107 errors++;
3108 }
3109 }
3110
3111 if (shell_flags[f] == SHELL_UNCHECKED)
3112 shell_flags[f] = SHELL_CLOSED;
3113
3114 break;
3115 }
3116 }
3117
3118 done = (f == rp->num_faces);
3119 }
3120
3121 // Clear flags
3122 for (f = 0; f < rp->num_faces; f++) {
3123 ASSERT(shell_flags[f] != SHELL_UNCHECKED);
3124 rp->faces[f].flags &= ~FF_NOT_SHELL;
3125 }
3126
3127 // If no errors, set flags for room
3128 if (errors == 0) {
3129 for (f = 0; f < rp->num_faces; f++) {
3130 if (shell_flags[f] == SHELL_NONE)
3131 rp->faces[f].flags |= FF_NOT_SHELL;
3132 }
3133 }
3134
3135 return errors;
3136 }
3137
3138 // Finds shells for all rooms.
3139 // Returns the number of rooms with bad shells
3140 int ComputeAllRoomShells() {
3141 int r, bad_shells = 0;
3142 room *rp;
3143
3144 error_buf_offset = 0;
3145
3146 mprintf(0, "Computing room shells...");
3147
3148 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
3149 if (rp->used && (rp->num_portals > 0) && !(rp->flags & RF_EXTERNAL)) {
3150 int errors = ComputeRoomShell(rp);
3151 if (errors)
3152 bad_shells++;
3153 }
3154 }
3155
3156 mprintf(0, "Done\n");
3157
3158 mprintf(0, "Error buf size = %d\n", strlen(error_buf));
3159
3160 DumpTextToClipboard(error_buf);
3161
3162 return bad_shells;
3163 }
3164
3165 #include "objinfo.h"
3166 #include "mission.h"
3167
3168 void ListObjectsInLevel(int *object_counts) {
3169 int i;
3170 object *objp;
3171
3172 for (i = 0; i < MAX_OBJECT_IDS; i++)
3173 object_counts[i] = 0;
3174
3175 for (i = 0, objp = Objects; i <= Highest_object_index; i++, objp++) {
3176 if (IS_GENERIC(objp->type))
3177 object_counts[objp->id]++;
3178 }
3179
3180 CheckError("\n Robots:\n");
3181 for (i = 0; i < MAX_OBJECT_IDS; i++) {
3182 if (object_counts[i] && (Object_info[i].type == OBJ_ROBOT))
3183 CheckError(" %2d\t%s\n", object_counts[i], Object_info[i].name);
3184 }
3185
3186 CheckError("\n Powerups:\n");
3187 for (i = 0; i < MAX_OBJECT_IDS; i++) {
3188 if (object_counts[i] && (Object_info[i].type == OBJ_POWERUP))
3189 CheckError(" %2d\t%s\n", object_counts[i], Object_info[i].name);
3190 }
3191
3192 CheckError("\n Buildings:\n");
3193 for (i = 0; i < MAX_OBJECT_IDS; i++) {
3194 if (object_counts[i] && (Object_info[i].type == OBJ_BUILDING))
3195 CheckError(" %2d\t%s\n", object_counts[i], Object_info[i].name);
3196 }
3197
3198 CheckError("\n Clutter:\n");
3199 for (i = 0; i < MAX_OBJECT_IDS; i++) {
3200 if (object_counts[i] && (Object_info[i].type == OBJ_CLUTTER))
3201 CheckError(" %2d\t%s\n", object_counts[i], Object_info[i].name);
3202 }
3203 }
3204
3205 #include "loadlevel.h"
3206
3207 extern bool Disable_editor_rendering;
3208
3209 #define NUM_LEVELS 15
3210
3211 void ListObjectsInAllLevels() {
3212 int object_counts[NUM_LEVELS + 1][MAX_OBJECT_IDS];
3213 char levelname[25];
3214 int i, l;
3215
3216 error_buf_offset = 0;
3217
3218 // Clear totals
3219 for (i = 0; i < MAX_OBJECT_IDS; i++)
3220 object_counts[0][i] = 0;
3221
3222 // Get totals for each level
3223 for (l = 1; l < NUM_LEVELS + 1; l++) {
3224
3225 sprintf(levelname, "Level%d.d3l", l);
3226
3227 LoadLevel(levelname);
3228
3229 CheckError("\n\nLevel: %s\n", levelname);
3230
3231 ListObjectsInLevel(&object_counts[l][0]);
3232
3233 // Accumulate totals
3234 for (i = 0; i < MAX_OBJECT_IDS; i++)
3235 object_counts[0][i] += object_counts[l][i];
3236 }
3237
3238 // Print headers
3239 CheckError("\n\nName\tType\tTotal");
3240 for (l = 1; l < NUM_LEVELS + 1; l++)
3241 CheckError("\t%d", l + 1);
3242
3243 // Print table
3244 for (i = 0; i < MAX_OBJECT_IDS; i++) {
3245 if (Object_info[i].type != OBJ_NONE) {
3246 CheckError("\n%s\t%c", Object_info[i].name, Object_type_names[Object_info[i].type][0]);
3247 for (int l = 0; l < NUM_LEVELS + 1; l++) {
3248 CheckError("\t%d", object_counts[l][i]);
3249 }
3250 }
3251 }
3252
3253 DumpTextToClipboard(error_buf);
3254 }