Debian Codegraph @

editor/HRoom.cpp

raw
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/HRoom.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Code to implement room functions
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 106 9/15/99 1:56p Matt
30 * Added the option to allow rooms or groups placed on the terrain to
31 * either align with the terrain or with gravity.
32 *
33 * 105 9/07/99 12:10p Matt
34 * Added a function to propagate a texture to all adjacent coplanar faces
35 * in a room.
36 *
37 * 104 8/30/99 1:00p Gwar
38 * use the "true" center of faces for determining attach points in room
39 * connecting functions
40 *
41 * 103 8/19/99 6:57p Gwar
42 * call ned_FindSharedEdge in CombineFaces to see if we can't combine
43 * because of a flipped normal
44 *
45 * 102 8/17/99 7:11p Gwar
46 *
47 * 101 8/17/99 12:08p Gwar
48 * don't set World_changed in functions that can be called from palette
49 * rooms
50 *
51 * 100 8/12/99 12:08a Gwar
52 * NEWEDITOR: texture, door and object memory management; and call SetPrim
53 * when changing Curroomp, which now force unmarks everything in current
54 * room view
55 *
56 * 99 7/04/99 4:55p Gwar
57 * changes for texture management in NEWEDITOR
58 *
59 * 98 5/08/99 6:39p Matt
60 * Added a function to delete a face and all faces connected to it.
61 *
62 * 97 5/04/99 12:32p Matt
63 * Fixed stupid bug and stupider bug.
64 *
65 * 96 4/30/99 6:52p Matt
66 * Added a function to merge an object's geometry into a room.
67 *
68 * 95 4/28/99 12:51a Matt
69 * Added some funtions to edit face geometry.
70 *
71 * 94 4/25/99 2:38p Gwar
72 * brought in HRoom.cpp and RoomUVs.cpp, and several misc game functions
73 * were added to globals.cpp to make it possible
74 *
75 * 93 4/19/99 12:10a Matt
76 * Added a menu item to delete a vertex from a face.
77 *
78 * 92 4/18/99 11:26p Matt
79 * Restored point-to-plane check to FixCracks(). This check is necessary
80 * because the point-to-edge check assumes that the points are all in the
81 * same plane, and will not work if they are not.
82 *
83 * 91 4/07/99 4:39p Matt
84 * In link to new external room, copy the textures from the old faces to
85 * the new faces.
86 *
87 * 90 4/07/99 1:50p Matt
88 * Took point-in-plane check out of fix level cracks, because it prevented
89 * tjoints from being fixed for some non-planar faces.
90 *
91 * 89 4/06/99 3:48p Matt
92 * Allow combining of rooms with rendered portals.
93 *
94 * 88 4/06/99 10:23a Matt
95 *
96 * 87 4/01/99 2:55p Matt
97 * Fixed triangulate face code to deal with colinear points
98 *
99 * 86 4/01/99 1:25p Matt
100 * Fixed bug in MatchPortalFaces when checking for match and a colinear
101 * point was found.
102 *
103 * 85 3/31/99 12:58p Matt
104 * Added snap-point-to-face
105 *
106 * 84 3/30/99 8:03p Matt
107 * Deleted commented-out code
108 *
109 * 83 3/30/99 8:01p Matt
110 * Took out normals-are-same check when combining adjacent faces. It was
111 * the wrong way to check the faces (the right way (in the D3 editor, that
112 * is) is to check for point in the plane) and we were only using it as a
113 * first-pass check anyway.
114 *
115 * 82 3/30/99 6:25p Matt
116 * Use new error system for combine faces function.
117 *
118 * 81 3/29/99 6:46p Matt
119 * When combining faces, use FaceIsPlanar() so the face will pass mine
120 * verification.
121 *
122 * 80 3/27/99 5:01p Matt
123 * Fixed stupid bug in build bridge code, that assigned UVs to all faces
124 * instead of one face, causing an Int3 sometimes for faces that hadn't
125 * yet had normals computed.
126 *
127 * 79 3/27/99 4:45p Matt
128 * Fixed a surprising fundamental flaw in the polygon clip code.
129 *
130 * 78 3/23/99 5:12p Matt
131 * Added function to combine rooms.
132 *
133 * 77 2/27/99 3:47p Mark
134 * Place room on terrain was improperly clearing out the placed door.
135 * (MattT on Mark's machine)
136 *
137 * 76 1/29/99 12:48p Matt
138 * Rewrote the doorway system
139 *
140 * 75 1/21/99 11:34a Matt
141 * Got rid of portal triggers. Since we don't have multi-face portals, a
142 * face trigger works fine for a portal. Also fixed a few editor/trigger
143 * bugs.
144 *
145 * 74 12/23/98 6:32p Matt
146 * Changed, slightly, the process of creating a new face
147 *
148 * 73 12/23/98 10:53a Matt
149 * Added functions to create a face
150 *
151 * 72 10/07/98 12:48p Matt
152 * Added some extra error checking to prevent doubly-formed portals.
153 *
154 * 71 10/03/98 8:31p Matt
155 * Added Join Rooms Exact function.
156 *
157 * 70 9/25/98 5:25p Matt
158 * Don't try to fix cracks in unused rooms.
159 *
160 * 69 9/24/98 5:00p Matt
161 * Improved error checking for running out of rooms.
162 *
163 * 68 9/16/98 10:02a Matt
164 * Fixed serval bugs, in JoinAllOverlappingFaces() and MatchPortalFaces().
165 *
166 * 67 9/11/98 4:46p Matt
167 * Don't allow combining of portal faces, pending code to do the combine
168 * correctly.
169 *
170 * 66 9/10/98 6:38p Matt
171 * Deal with error joining faces after smooth bridge.
172 *
173 * 65 9/09/98 4:07p Matt
174 * Added Smooth Bridge function
175 *
176 * 64 9/08/98 3:25p Matt
177 * Changed an error message.
178 *
179 * 63 9/08/98 12:05p Jason
180 * moved doorway.h out of room.h
181 *
182 * 62 9/07/98 10:58p Matt
183 * Added snap point to point.
184 *
185 * 61 9/07/98 8:14p Matt
186 * Don't delete colinear points (& thus form t-joints) when combining
187 * faces, and deal properly with combining faces that share multiple
188 * edges.
189 *
190 * 60 9/04/98 3:34p Matt
191 * Added groovy vertex snap code
192 *
193 * 59 9/04/98 12:29p Matt
194 * Added marked edge & vertex in the editor
195 *
196 * 58 9/04/98 11:34a Matt
197 * Fixed small bug in portal matching code, that caused one point on the
198 * portals not to be set exactly the same.
199 *
200 * 57 9/03/98 5:29p Matt
201 * Added code to fix cracks in levels.
202 *
203 * 56 9/02/98 5:57p Matt
204 * Added code to make faces match exactly when forming a portal. This
205 * should get rid of all remaining cracking problems.
206 *
207 * 55 9/02/98 4:07p Matt
208 * Fixed one kind of cracking: after clip, make sure old points that
209 * more-or-less line up are exactly the same.
210 *
211 * 54 9/02/98 2:29p Matt
212 * Added code to prevent t-joints from being formed when joining rooms
213 *
214 * 53 9/01/98 12:04p Matt
215 * Ripped out multi-face portal code
216 *
217 * 52 8/28/98 6:21p Matt
218 * Added a function to flip a face.
219 *
220 * 51 8/24/98 3:02p Matt
221 * Gave the user the option, when joining rooms, of extruding straight out
222 * or toward the center of the base face.
223 *
224 * 50 7/01/98 7:53p Matt
225 * Added code to allow forcing of join even when the two rooms stick
226 * through each other.
227 *
228 * 49 6/25/98 7:15p Matt
229 * Added a function to delete a pair of portals.
230 *
231 * 48 6/23/98 2:45p Matt
232 * Added option when propagating to all faces in a room to only do so to
233 * ones with the same texture.
234 *
235 * 47 6/08/98 12:28p Matt
236 * Added a function to triangulate a face
237 *
238 * 46 5/22/98 4:47p Matt
239 * Added menu item to propagate a texture to all the faces in a room.
240 *
241 * 45 4/27/98 6:41p Matt
242 * Added code to join all adjacent faces between two rooms
243 *
244 * 44 4/17/98 6:25p Matt
245 * In CheckFaceToPlane(), store the distance in a variable so it can be
246 * looked at in the debugger.
247 *
248 * 43 4/17/98 6:23p Matt
249 * Added code to make sure that the two faces of a portal have the same
250 * number of vertices, but commented it out for now. See the note in the
251 * code.
252 *
253 * 42 4/16/98 11:50a Matt
254 * Made snap code work for placed groups.
255 *
256 * 41 4/07/98 4:19p Matt
257 * Fixed (I hope) a problem joining faces that matched perfectly.
258 *
259 * 40 4/02/98 12:23p Jason
260 * trimmed some fat from our structures
261 *
262 * 39 4/01/98 5:55p Matt
263 * Partially re-wrote JoinRooms() to allow creation of adjacent portals.
264 *
265 * 38 2/18/98 2:26a Matt
266 * Fixed typo
267 *
268 * 37 2/16/98 2:53p Matt
269 * Deal with small floating-point precision problem.
270 *
271 * 36 2/16/98 1:27p Matt
272 * Added function to snap the placed room to a vertex/edge on the base
273 * room/face
274 *
275 * 35 2/11/98 12:38p Matt
276 * Added epsilon to point-to-plane check
277 *
278 * 34 2/08/98 6:02p Matt
279 * Use groovy new vm_MatrixMulTMatrix() function
280 *
281 * 33 2/04/98 6:23p Matt
282 * Changed object room number to indicate a terrain cell via a flag. Got
283 * rid of the object flag which used to indicate terrain.
284 *
285 * 32 1/30/98 1:27p Matt
286 * Orthogonalize after computing rotation matrix for placed rooms (fixed
287 * probem of bad matrix making rooms bigger).
288 *
289 * 31 1/21/98 12:40p Matt
290 * When attaching a room to the ground, delete the room's attach face
291 *
292 * 30 1/21/98 12:32p Matt
293 * Revamped viewer system
294 *
295 * 29 1/20/98 4:43p Matt
296 * Fixed up the bridge and join functions
297 *
298 * 28 1/15/98 7:34p Matt
299 * Revamped error checking when computing face normals
300 *
301 * 27 11/17/97 7:39p Sean
302 * Changed join epsilon, and took out a return from a bogus error message
303 * (Matt on Sean's machine)
304 *
305 * 26 11/06/97 6:47p Sean
306 * FROM JASON: Changed <Highest_room_index to <=
307 *
308 * 25 11/05/97 7:13p Matt
309 * Added join rooms function
310 *
311 * 24 10/03/97 3:38p Matt
312 * Added place and attach code for rooms on the terrain
313 *
314 * 23 10/01/97 7:51p Matt
315 * Added code for external rooms
316 *
317 * 22 9/29/97 12:09p Jason
318 * added functionality to doorway system
319 *
320 * 21 9/26/97 6:01p Matt
321 * Fixed delete room code so that Curroomp is never NULL
322 *
323 * 20 9/24/97 3:22p Matt
324 * Added Drop Room function
325 *
326 * 19 9/15/97 5:56p Matt
327 * Changed message (& fixed handling) when user tries to delete a room
328 * attached to the terrain.
329 *
330 * 18 9/12/97 9:01p Craig
331 * Fixed orientation of door object (Matt on Craig's machine)
332 *
333 * 17 9/12/97 5:38p Jason
334 * got doors working
335 *
336 * 16 9/11/97 5:38p Jason
337 * initial door coding for room engine
338 *
339 * 15 9/06/97 3:18p Matt
340 * Use LinkRoomsSimple() when adding, attaching, & bridging
341 * Reset Curportal when switch rooms
342 *
343 * 14 9/04/97 4:39p Matt
344 * Added includes needed as a result of removing includes from d3edit.h
345 *
346 * 13 9/02/97 2:26p Matt
347 * Made ComputePlacedRoomMatrix() deal w/ placed rooms or groups
348 * Made polygon clipper deal with points on the the edge of the clip
349 * polygon
350 *
351 * 12 8/29/97 5:43p Matt
352 * Moved some funcs to erooms.cpp, and fixed some stuff for selected rooms
353 *
354 * 11 8/27/97 10:30a Matt
355 * Fixed bogosity assigning UVs to faces in add room
356 *
357 * 10 8/22/97 9:30a Matt
358 * When adding a room, make the new room the current room, and make the
359 * face opposite the connection the current one. That way you can add
360 * several times in a row to create a long tunnel.
361 *
362 * 9 8/21/97 6:01p Matt
363 * Added combine faces & delete room functions
364 *
365 * 8 8/19/97 1:01p Matt
366 * Added error check for facing not overlapping when attaching rooms
367 *
368 * 7 8/18/97 6:59p Matt
369 * Implemented Place Room/Attach room system
370 *
371 * 6 8/05/97 10:47a Matt
372 * Added some error checking, and fixed a bug
373 *
374 * 5 8/04/97 7:38p Matt
375 * Don't add a room if there's already a room at the face
376 *
377 * 4 8/04/97 12:46p Matt
378 * Added BuildBridge() and SetMarkedRoom()
379 *
380 *
381 * 3 8/01/97 6:15p Matt
382 * Added code to attach rooms
383 *
384 * 2 7/22/97 10:33a Matt
385 * Added AddRoom()
386 *
387 * 1 7/21/97 4:48p Matt
388 *
389 * $NoKeywords: $
390 */
391
392 #include <stdlib.h>
393
394 #include "HRoom.h"
395
396 #ifndef NEWEDITOR
397 #include "d3edit.h"
398 #else
399 #include "..\neweditor\globals.h"
400 #include "..\neweditor\ned_geometry.h"
401 #endif
402
403 #include "erooms.h"
404 #include "RoomUVs.h"
405 #include "HView.h"
406 #include "SelectedRoom.h"
407 #include "group.h"
408 #include "door.h"
409 #include "doorway.h"
410 #include "terrain.h"
411 #include "HTexture.h"
412 #include "trigger.h"
413
414 // Make the Marked room/face the current room/face
415 void SetMarkedRoom() {
416 Markedroomp = Curroomp;
417 Markedface = Curface;
418 Markededge = Curedge;
419 Markedvert = Curvert;
420
421 State_changed = 1;
422
423 EditorStatus("Marked room:face set to %d:%d", ROOMNUM(Markedroomp), Markedface);
424 }
425
426 // Select next face on current room
427 void SelectNextFace() {
428 if (++Curface >= Curroomp->num_faces)
429 Curface = 0;
430 State_changed = 1;
431 }
432
433 // Select previous face on current room
434 void SelectPrevFace() {
435 if (--Curface < 0)
436 Curface = Curroomp->num_faces - 1;
437 State_changed = 1;
438 }
439
440 #define DEFAULT_ROOM_LENGTH 20.0
441
442 // Adds a room at the current room/face. The room is created by extuding out from the current face
443 void AddRoom() {
444 room *rp;
445 face *cfp;
446 int cnv, nfaces;
447 vector room_delta;
448 int i;
449
450 // Get values from current room & face
451 cfp = &Curroomp->faces[Curface]; // pointer to current face
452 cnv = cfp->num_verts; // number of verts in current face
453
454 // Check for portal already here
455 if (cfp->portal_num != -1) {
456 OutrageMessageBox("Can't add room: There is already a connection at the current room:face.");
457 return;
458 }
459
460 // Get values for new room
461 nfaces = cnv + 2;
462
463 // Get a pointer to our room
464 rp = CreateNewRoom(cnv * 2, nfaces, 0);
465 if (rp == NULL) {
466 OutrageMessageBox("Cannot add room: No free rooms.");
467 return;
468 }
469
470 // Compute delta vector
471 room_delta = cfp->normal * -DEFAULT_ROOM_LENGTH;
472
473 // Set the vertices for the room
474 for (i = 0; i < cnv; i++) {
475 rp->verts[i] = Curroomp->verts[cfp->face_verts[cnv - 1 - i]];
476 rp->verts[cnv + i] = rp->verts[i] + room_delta;
477 }
478
479 // Set the faces for the room
480 InitRoomFace(&rp->faces[0], cnv);
481 for (i = 0; i < cnv; i++)
482 rp->faces[0].face_verts[i] = i;
483
484 InitRoomFace(&rp->faces[1], cnv);
485 for (i = 0; i < cnv; i++)
486 rp->faces[1].face_verts[i] = cnv * 2 - 1 - i;
487
488 for (i = 0; i < nfaces - 2; i++) {
489 InitRoomFace(&rp->faces[i + 2], 4);
490 rp->faces[i + 2].face_verts[0] = i;
491 rp->faces[i + 2].face_verts[1] = i + cnv;
492 rp->faces[i + 2].face_verts[2] = ((i + 1) % cnv) + cnv;
493 rp->faces[i + 2].face_verts[3] = (i + 1) % cnv;
494 }
495
496 #ifndef NEWEDITOR
497 // Set normals & textures for each face
498 for (i = 0; i < nfaces; i++) {
499 if (!ComputeFaceNormal(rp, i))
500 Int3(); // Bad! Get Matt!
501
502 // Assign arbitrary texture map
503 rp->faces[i].tmap = i + 1;
504 }
505 #else
506 int texnum = Editor_state.GetCurrentTexture();
507
508 // Set normals & textures for each face
509 for (i = 0; i < nfaces; i++) {
510 if (!ComputeFaceNormal(rp, i))
511 Int3(); // Bad! Get Matt!
512
513 // Apply texture
514 HTextureApplyToRoomFace(rp, i, texnum);
515 }
516 #endif
517
518 // Set UVs for all the faces
519 AssignDefaultUVsToRoom(rp);
520
521 // Form the portals between the rooms
522 LinkRoomsSimple(Rooms, ROOMNUM(Curroomp), Curface, ROOMNUM(rp), 0);
523
524 // Make the new room the current room
525 #ifndef NEWEDITOR
526 Curroomp = rp;
527 Curface = 1;
528 Curedge = Curvert = 0;
529 Curportal = -1;
530 #else
531 theApp.m_pLevelWnd->SetPrim(rp, 1, -1, 0, 0);
532 #endif
533
534 // Set the flag
535 World_changed = 1;
536 }
537
538 // Computes the orientation matrix for the placed room
539 void ComputePlacedRoomMatrix() {
540 room *placedroomp;
541 int placedface;
542 matrix srcmat;
543 vector t;
544
545 if (Placed_room != -1) {
546 placedroomp = &Rooms[Placed_room];
547 placedface = Placed_room_face;
548 } else {
549 ASSERT(Placed_group != NULL);
550 placedroomp = &Placed_group->rooms[Placed_group->attachroom];
551 placedface = Placed_group->attachface;
552 }
553
554 // Compute source and destination matrices
555 t = -placedroomp->faces[placedface].normal;
556 vm_VectorToMatrix(&srcmat, &t, NULL, NULL);
557 vm_VectorAngleToMatrix(&Placed_room_orient, &Placed_room_orient.fvec, Placed_room_angle);
558
559 mprintf(0, "srcmat: %f %f %f\n", vm_GetMagnitude(&srcmat.fvec), vm_GetMagnitude(&srcmat.rvec),
560 vm_GetMagnitude(&srcmat.uvec));
561 mprintf(0, "orient: %f %f %f\n", vm_GetMagnitude(&Placed_room_orient.fvec), vm_GetMagnitude(&Placed_room_orient.rvec),
562 vm_GetMagnitude(&Placed_room_orient.uvec));
563
564 // Make sure the matrices are ok
565 vm_Orthogonalize(&srcmat);
566 vm_Orthogonalize(&Placed_room_orient);
567
568 // Compute matrix to rotate src -> dest
569 vm_MatrixMulTMatrix(&Placed_room_rotmat, &srcmat, &Placed_room_orient);
570
571 mprintf(0, "rotmat: %f %f %f\n", vm_GetMagnitude(&Placed_room_rotmat.fvec), vm_GetMagnitude(&Placed_room_rotmat.rvec),
572 vm_GetMagnitude(&Placed_room_rotmat.uvec));
573
574 // Make sure the matrix is ok
575 vm_Orthogonalize(&Placed_room_rotmat);
576 }
577
578 // Place a room for orientation before attachment
579 // Parameters: baseroomp - pointer to the room in the mine to which the new room will be attached
580 // baseface - the face on baseroomp to attach to
581 // placed_room - the number of the room to be attached
582 // placed_room_face the face on placed_room that's attached
583 void PlaceRoom(room *baseroomp, int baseface, int placed_room, int placed_room_face, int placed_room_door) {
584 // Clear the placed group if one exists
585 Placed_group = NULL;
586
587 ASSERT(baseroomp->faces[baseface].portal_num == -1);
588
589 room *placedroomp = &Rooms[placed_room];
590
591 // Set globals
592 Placed_room = placed_room;
593 Placed_room_face = placed_room_face;
594 Placed_room_orient.fvec = baseroomp->faces[baseface].normal;
595 Placed_room_angle = 0;
596 Placed_baseroomp = baseroomp;
597 Placed_baseface = baseface;
598 Placed_door = placed_room_door;
599
600 // Compute attach points on each face
601 #ifndef NEWEDITOR
602 ComputeCenterPointOnFace(&Placed_room_attachpoint, baseroomp, baseface);
603 ComputeCenterPointOnFace(&Placed_room_origin, placedroomp, placed_room_face);
604 #else
605 ComputeFaceBoundingCircle(&Placed_room_attachpoint, baseroomp, baseface);
606 ComputeFaceBoundingCircle(&Placed_room_origin, placedroomp, placed_room_face);
607 #endif
608
609 // Compute initial orientation matrix
610 ComputePlacedRoomMatrix();
611
612 // Set the flag
613 State_changed = 1;
614 }
615
616 // Lined up a placed room. Moves the placed room so the closest vert to basevert lines up exactly,
617 // and the edge from basevert to basevert+1 lines up with the corresponding edge on the placed room.
618 void SnapRoom(int basevert) {
619 vector basecenter, attcenter;
620 room *baseroomp, *attroomp;
621 face *bfp, *afp;
622 vector *bvp;
623 int baseface, attface;
624 vector baseedge, attedge;
625
626 // Get values for the attach room
627 if (Placed_room != -1) {
628 attroomp = &Rooms[Placed_room];
629 attface = Placed_room_face;
630 } else {
631 ASSERT(Placed_group != NULL);
632 attroomp = &Placed_group->rooms[Placed_group->attachroom];
633 attface = Placed_group->attachface;
634 }
635
636 // Set some vars
637 baseroomp = Placed_baseroomp;
638 baseface = Placed_baseface;
639 attcenter = Placed_room_origin;
640 basecenter = Placed_room_attachpoint;
641 bfp = &baseroomp->faces[baseface];
642 afp = &attroomp->faces[attface];
643 bvp = &baseroomp->verts[bfp->face_verts[basevert]];
644
645 // Find matching vert in attach face
646 float closest_dist = FLT_MAX;
647 int closest_v;
648 for (int v = 0; v < afp->num_verts; v++) {
649 vector rotvert;
650 float dist;
651
652 rotvert = ((attroomp->verts[afp->face_verts[v]] - attcenter) * Placed_room_rotmat) + basecenter;
653 dist = vm_VectorDistance(bvp, &rotvert);
654
655 if (dist < closest_dist) {
656 closest_dist = dist;
657 closest_v = v;
658 }
659 }
660
661 // Compute edge vector in base room
662 vm_GetNormalizedDir(&baseedge, &baseroomp->verts[bfp->face_verts[(basevert + 1) % bfp->num_verts]], bvp);
663
664 // Compute edge vector in attach room
665 vector v0 = ((attroomp->verts[afp->face_verts[closest_v]] - attcenter) * Placed_room_rotmat) + basecenter;
666 vector v1 = ((attroomp->verts[afp->face_verts[(closest_v + afp->num_verts - 1) % afp->num_verts]] - attcenter) *
667 Placed_room_rotmat) +
668 basecenter;
669 vm_GetNormalizedDir(&attedge, &v1, &v0);
670
671 // Get angle between two edge vectors
672 double dot = baseedge * attedge;
673 if (dot > 1.0)
674 dot = 1.0;
675 double ac = acos(dot);
676 float delta_ang = 32768.0 * ac / PI;
677
678 // Get sign of angle
679 vector checkv;
680 vm_CrossProduct(&checkv, &baseedge, &attedge);
681 if ((bfp->normal * checkv) > 0)
682 delta_ang = -delta_ang;
683
684 // Update placed room angle
685 Placed_room_angle += delta_ang;
686 while (Placed_room_angle < 0)
687 Placed_room_angle += 65536;
688
689 // Regenerate matrix
690 ComputePlacedRoomMatrix();
691
692 // Move the room to line up the points
693 v0 = ((attroomp->verts[afp->face_verts[closest_v]] - attcenter) * Placed_room_rotmat) + basecenter;
694 Placed_room_attachpoint += *bvp - v0;
695
696 // Update view
697 State_changed = 1;
698 }
699
700 // Attach an already-placed room
701 void AttachRoom() {
702 vector basecenter, attcenter;
703 room *baseroomp, *attroomp, *newroomp;
704 int baseface, attface;
705
706 ASSERT(Placed_room != -1);
707
708 // Set some vars
709 baseroomp = Placed_baseroomp;
710 baseface = Placed_baseface;
711 attroomp = &Rooms[Placed_room];
712 attface = Placed_room_face;
713 attcenter = Placed_room_origin;
714 basecenter = Placed_room_attachpoint;
715
716 // Get the new room
717 newroomp = CreateNewRoom(attroomp->num_verts, attroomp->num_faces, 0);
718 if (newroomp == NULL) {
719 OutrageMessageBox("Cannot attach room: No free rooms.");
720 return;
721 }
722
723 // Rotate verts, copying into new room
724 for (int i = 0; i < attroomp->num_verts; i++)
725 newroomp->verts[i] = ((attroomp->verts[i] - attcenter) * Placed_room_rotmat) + basecenter;
726
727 // Copy faces to new room
728 for (i = 0; i < attroomp->num_faces; i++) {
729 CopyFace(&newroomp->faces[i], &attroomp->faces[i]);
730 #ifdef NEWEDITOR
731 LevelTexIncrementTexture(attroomp->faces[i].tmap);
732 #endif
733 }
734
735 // Recompute normals for the faces
736 if (!ResetRoomFaceNormals(newroomp))
737 Int3(); // Get Matt
738
739 // Copy other values for this room
740 newroomp->flags = attroomp->flags;
741 newroomp->num_portals = 0;
742
743 // Check for terrain or mine room
744 if (baseroomp == NULL) { // a terrain room
745
746 // Flag this as an external room
747 newroomp->flags |= RF_EXTERNAL;
748
749 // Delete the attach face, which should now be facing the ground
750 DeleteRoomFace(newroomp, attface);
751 } else { // a mine room
752
753 // Clip the connecting faces against each other
754 if (!ClipFacePair(newroomp, attface, baseroomp, baseface)) {
755 OutrageMessageBox("Error making portal -- faces probably don't overlap.");
756 FreeRoom(newroomp);
757 return;
758 }
759
760 // Make the two faces match exactly
761 MatchPortalFaces(baseroomp, baseface, newroomp, attface);
762
763 // Create the portals between the rooms
764 LinkRoomsSimple(Rooms, ROOMNUM(baseroomp), baseface, ROOMNUM(newroomp), attface);
765
766 // If there is a door, place it!
767 if (Placed_door != -1) {
768 vector room_center;
769 matrix orient = ~Placed_room_rotmat;
770 vector doorcenter = {0, 0, 0};
771
772 FreeRoom(&Rooms[Placed_room]);
773
774 room_center = ((doorcenter - attcenter) * Placed_room_rotmat) + basecenter;
775
776 ObjCreate(OBJ_DOOR, Placed_door, newroomp - Rooms, &room_center, &orient);
777
778 doorway *dp = DoorwayAdd(newroomp, Placed_door);
779
780 #ifdef NEWEDITOR
781 LevelDoorIncrementDoorway(Placed_door);
782 #endif
783
784 Placed_door = -1;
785 }
786 }
787
788 // Un-place the now-attached room
789 Placed_room = -1;
790
791 // Flag the world as changed
792 World_changed = 1;
793 }
794
795 // structure for keeping track of vertices inserted in edges (from clipping)
796 struct edge_insert {
797 int v0, v1; // the edge that got the new vert
798 int new_v; // the new vertex
799 };
800
801 // List of vertices inserted in edges. Used only during clipping.
802 edge_insert Edge_inserts[MAX_VERTS_PER_FACE];
803 int Num_edge_inserts;
804
805 // Add an insert to the edge list
806 void AddEdgeInsert(int v0, int v1, int new_v) {
807 Edge_inserts[Num_edge_inserts].v0 = v0;
808 Edge_inserts[Num_edge_inserts].v1 = v1;
809 Edge_inserts[Num_edge_inserts].new_v = new_v;
810 Num_edge_inserts++;
811 }
812
813 // Clip a polygon against one edge of another polygon
814 // Fills inbuf and maybe outbuf with new polygons, and writes any new verts to the vertices array
815 // Parameters: nv - the number of verts in the polygon to be clipped
816 // vertnums - pointer to list of vertex numbers in the polygon
817 // vertices - list of vertices referred to in vertnums
818 // v0,v1 - the edge we're clipping against
819 // normal - the surface normal of the polygon
820 // inbuf - the clipped polygon is written to this buffer
821 // inv - the number of verts in inbuf is written here
822 // outbuf - the new polygon created by the part of the input polygon that was
823 //clipped away onv - the number of verys in outbuf num_vertices - pointer to the number of verts in the vertices array
824 void ClipAgainstEdge(int nv, int16_t *vertnums, vertex *vertices, int *num_vertices, vector *v0, vector *v1,
825 vector *normal, int16_t *inbuf, int *inv, int16_t *outbuf, int *onv) {
826 int i, prev, next, check;
827 int16_t *ip = inbuf, *op = outbuf;
828 vertex *curv, *prevv, *nextv;
829 int inside_points = 0, outside_points = 0; // real inside/outside points, distinct from edge points
830
831 for (i = 0, prev = nv - 1, next = 1; i < nv; i++) {
832
833 curv = &vertices[vertnums[i]];
834
835 // Find out where point lies
836 check = CheckPointAgainstEdge(&curv->vec, v0, v1, normal);
837 if (check == 0) { // Current vertex is on edge
838
839 // Add to both inside & outside lists
840 *op++ = vertnums[i];
841 *ip++ = vertnums[i];
842 } else if (check == -1) { // Current vertex is outside
843 int check2;
844
845 prevv = &vertices[vertnums[prev]];
846 nextv = &vertices[vertnums[next]];
847
848 // Clip edge w/ previous vertex
849 check2 = CheckPointAgainstEdge(&prevv->vec, v0, v1, normal);
850 if (check2 == 1) { // prev inside, so clip
851 ClipEdge(normal, prevv, curv, v0, v1, &vertices[*num_vertices]);
852 AddEdgeInsert(vertnums[prev], vertnums[i], *num_vertices);
853 *op++ = *ip++ = (*num_vertices)++;
854 }
855
856 // Add current vertex to outside polygon
857 *op++ = vertnums[i];
858 outside_points++;
859
860 // Clip edge w/ next vertex
861 check2 = CheckPointAgainstEdge(&nextv->vec, v0, v1, normal);
862 if (check2 == 1) { // next inside, so clip
863 ClipEdge(normal, curv, nextv, v0, v1, &vertices[*num_vertices]);
864 AddEdgeInsert(vertnums[i], vertnums[next], *num_vertices);
865 *op++ = *ip++ = (*num_vertices)++;
866 }
867 } else { // Current vertex is inside
868 ASSERT(check == 1);
869
870 // Add current vertex to inside polygon
871 *ip++ = vertnums[i];
872 inside_points++;
873 }
874
875 prev = i;
876 if (++next == nv)
877 next = 0;
878 }
879
880 // Set number of verts for return. If no real inside or outside points, then don't count edge points
881 *inv = inside_points ? (ip - inbuf) : 0;
882 *onv = outside_points ? (op - outbuf) : 0;
883 }
884
885 // Adds a new vertex to all instances of a given edge
886 void AddVertToAllEdges(room *rp, int v0, int v1, int new_v) {
887 face *fp;
888 int f, v;
889
890 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
891 for (v = 0; v < fp->num_verts; v++) {
892 if (((fp->face_verts[v] == v0) && (fp->face_verts[(v + 1) % fp->num_verts] == v1)) ||
893 ((fp->face_verts[v] == v1) && (fp->face_verts[(v + 1) % fp->num_verts] == v0)))
894 AddVertToFace(rp, f, new_v, v);
895 }
896 }
897 }
898
899 // Adds a new point to all instances of a given edge
900 // First adds the point to the room (or finds it there), then calls AddVertToAllEdges()
901 void AddPointToAllEdges(room *rp, int v0, int v1, vector *new_v) {
902 int newvertnum = RoomAddVertices(rp, 1);
903
904 rp->verts[newvertnum] = *new_v;
905
906 AddVertToAllEdges(rp, v0, v1, newvertnum);
907 }
908
909 // Clips a face against another. Produces one polygon that is the intersection of the two input
910 // faces, and zero or more extra polygons, which are parts of the input face outside the clipping face.
911 // The input face is replaced by the clipped face, and new faces (formed by the parts of the input
912 // face outside the clip-against face) are added to the end of the room's facelist.
913 // This routine assumes that part or all of the being-clipped face is inside the clip-against face
914 // Parameters: arp - the room with the face that is being changed
915 // afacenum - the face being changed
916 // brp - the room with the face we're clipping against
917 // bfacenum - the face we're clipping against
918 // Returns: true if the clip was ok, false if there was an error
919 bool ClipFace(room *arp, int afacenum, room *brp, int bfacenum) {
920 face *afp = &arp->faces[afacenum];
921 face *bfp = &brp->faces[bfacenum];
922 int edgenum;
923 int16_t vbuf0[MAX_VERTS_PER_FACE], vbuf1[MAX_VERTS_PER_FACE];
924 int16_t newface_verts[MAX_VERTS_PER_FACE][MAX_VERTS_PER_FACE];
925 int newface_nvs[MAX_VERTS_PER_FACE];
926 vertex newverts[MAX_VERTS_PER_FACE];
927 int newvertnums[MAX_VERTS_PER_FACE];
928 int num_newverts;
929 int num_newfaces = 0;
930 int16_t *src, *dest;
931 int nv;
932 int i;
933
934 // Init some stuff
935 nv = afp->num_verts;
936 src = vbuf0;
937 dest = vbuf1;
938
939 Num_edge_inserts = 0;
940
941 // copy our vertices into one buffer
942 for (i = 0; i < nv; i++) {
943 newverts[i].vec = arp->verts[afp->face_verts[i]];
944 newverts[i].uvl = afp->face_uvls[i];
945 newvertnums[i] = afp->face_verts[i];
946 src[i] = i;
947 }
948 num_newverts = nv;
949
950 // Clip our polygon against each edge
951 for (edgenum = 0; edgenum < bfp->num_verts; edgenum++) {
952 vector *v0, *v1;
953 int16_t *outbuf = newface_verts[num_newfaces];
954 int *onv = &newface_nvs[num_newfaces];
955
956 v0 = &brp->verts[bfp->face_verts[(bfp->num_verts - edgenum) % bfp->num_verts]];
957 v1 = &brp->verts[bfp->face_verts[bfp->num_verts - edgenum - 1]];
958
959 ClipAgainstEdge(nv, src, newverts, &num_newverts, v0, v1, &afp->normal, dest, &nv, outbuf, onv);
960
961 if (nv <= 2) // no new face -- faces must not overlap
962 return 0;
963
964 src = dest;
965 dest = (src == vbuf0) ? vbuf1 : vbuf0;
966
967 if (newface_nvs[num_newfaces]) // is there a new face?
968 num_newfaces++; //..yes, increment counter
969 }
970
971 // Now we have the clipped face and the other new faces
972 // Replace the old face, and add the new faces
973 int first_new_vert, first_new_face;
974 face *fp;
975
976 // Allocate space for the new verts
977 first_new_vert = RoomAddVertices(arp, num_newverts - afp->num_verts);
978
979 // Copy new vertices into room & get real vert numbers
980 for (i = 0; i < num_newverts - afp->num_verts; i++) {
981 arp->verts[first_new_vert + i] = newverts[afp->num_verts + i].vec;
982 newvertnums[afp->num_verts + i] = first_new_vert + i;
983 }
984
985 // Replace the input face with the clipped face
986 ReInitRoomFace(afp, nv);
987 for (i = 0; i < nv; i++) {
988 afp->face_verts[i] = newvertnums[src[i]];
989 afp->face_uvls[i] = newverts[src[i]].uvl;
990 }
991 if (!ComputeFaceNormal(arp, afacenum))
992 Int3(); // Get Matt
993
994 // Allocate space for the new faces
995 first_new_face = RoomAddFaces(arp, num_newfaces);
996
997 // Copy data for new faces (the outside faces)
998 for (i = 0; i < num_newfaces; i++) {
999 fp = &arp->faces[first_new_face + i];
1000 InitRoomFace(fp, newface_nvs[i]);
1001 for (int j = 0; j < newface_nvs[i]; j++) {
1002 fp->face_verts[j] = newvertnums[newface_verts[i][j]];
1003 fp->face_uvls[j] = newverts[newface_verts[i][j]].uvl;
1004 }
1005 if (!ComputeFaceNormal(arp, first_new_face + i))
1006 Int3(); // Get Matt
1007 #ifndef NEWEDITOR
1008 fp->tmap = arp->faces[afacenum].tmap;
1009 #else
1010 // Apply texture
1011 HTextureApplyToRoomFace(arp, first_new_face + i, arp->faces[afacenum].tmap);
1012 #endif
1013 CopyFaceFlags(fp, &arp->faces[afacenum]);
1014 }
1015
1016 // Add new verts to edges
1017 for (i = 0; i < Num_edge_inserts; i++) {
1018 int v0, v1, new_v;
1019 v0 = newvertnums[Edge_inserts[i].v0];
1020 v1 = newvertnums[Edge_inserts[i].v1];
1021 new_v = newvertnums[Edge_inserts[i].new_v];
1022
1023 AddVertToAllEdges(arp, v0, v1, new_v);
1024 }
1025
1026 // Done!
1027 return 1;
1028 }
1029
1030 // Adds a point to a face
1031 // Parameters: rp,facenum - the face we're adding the point to
1032 // vertindex - the position in the face's vertex list at which to add the point
1033 // vp - the point to add
1034 void AddPointToFace(room *rp, int facenum, int vertindex, vector *vp) {
1035 face *fp = &rp->faces[facenum];
1036 int old_verts[MAX_VERTS_PER_FACE];
1037 roomUVL old_uvls[MAX_VERTS_PER_FACE];
1038 int t;
1039
1040 // Add the new point to the room
1041 int newvertnum = RoomAddVertices(rp, 1);
1042 rp->verts[newvertnum] = *vp;
1043
1044 // Make copy of old verts
1045 for (t = 0; t < fp->num_verts; t++) {
1046 old_verts[t] = fp->face_verts[t];
1047 old_uvls[t] = fp->face_uvls[t];
1048 }
1049
1050 // Allocate for new verts
1051 ReInitRoomFace(fp, fp->num_verts + 1);
1052
1053 // Copy old verts
1054 for (t = 0; t < vertindex; t++) {
1055 fp->face_verts[t] = old_verts[t];
1056 fp->face_uvls[t] = old_uvls[t];
1057 }
1058 fp->face_verts[t++] = newvertnum;
1059 // fp0->face_uvls[t] = ??
1060 for (; t < fp->num_verts; t++) {
1061 fp->face_verts[t] = old_verts[t - 1];
1062 fp->face_uvls[t] = old_uvls[t - 1];
1063 }
1064 }
1065
1066 // Deletes a point from a face
1067 // Parameters: rp,facenum - the face we're deleting the point from
1068 // vertindex - the vertex to delete
1069 void DeletePointFromFace(room *rp, int facenum, int vertindex) {
1070 face *fp = &rp->faces[facenum];
1071 int old_verts[MAX_VERTS_PER_FACE];
1072 roomUVL old_uvls[MAX_VERTS_PER_FACE];
1073 int t;
1074
1075 // Make copy of old verts
1076 for (t = 0; t < fp->num_verts; t++) {
1077 old_verts[t] = fp->face_verts[t];
1078 old_uvls[t] = fp->face_uvls[t];
1079 }
1080
1081 // Allocate for new verts
1082 ReInitRoomFace(fp, fp->num_verts - 1);
1083
1084 // Copy old verts
1085 for (t = 0; t < vertindex; t++) {
1086 fp->face_verts[t] = old_verts[t];
1087 fp->face_uvls[t] = old_uvls[t];
1088 }
1089 for (; t < fp->num_verts; t++) {
1090 fp->face_verts[t] = old_verts[t + 1];
1091 fp->face_uvls[t] = old_uvls[t + 1];
1092 }
1093
1094 #ifndef NEWEDITOR
1095 World_changed = 1;
1096 #endif
1097 }
1098
1099 // Takes two faces which are going to be made into a portal and makes them match exactly
1100 // Alternatively, checks if the two faces can be matched
1101 // After this function the two faces will have exactly the same vertices
1102 // Parameters: rp0,facenum0 - one of the faces
1103 // rp1,facenum1 - the other face
1104 // check_only - if set, doesn't change anything; just checks the faces
1105 // Returns the number of points added to the faces
1106 // If just_checking set, returns true if faces match, else false
1107 int MatchPortalFaces(room *rp0, int facenum0, room *rp1, int facenum1, bool check_only) {
1108 face *fp0 = &rp0->faces[facenum0];
1109 face *fp1 = &rp1->faces[facenum1];
1110 vector *v0, *v1, *prev_v0, *prev_v1;
1111 int n0, n1, i, j, prev_vn0, prev_vn1, max_nv;
1112 int points_added = 0;
1113
1114 check_faces:;
1115
1116 // First, find one point in common
1117 for (i = 0; i < fp0->num_verts; i++) {
1118 for (j = 0; j < fp1->num_verts; j++)
1119 if (PointsAreSame(&rp0->verts[fp0->face_verts[i]], &rp1->verts[fp1->face_verts[j]]))
1120 break;
1121 if (j < fp1->num_verts)
1122 break;
1123 }
1124 if (i >= fp0->num_verts) {
1125 if (!check_only)
1126 Int3(); // Counldn't find common point! This is very bad. Get Matt.
1127 return 0; // no match
1128 }
1129
1130 prev_vn0 = fp0->face_verts[i];
1131 prev_vn1 = fp1->face_verts[j];
1132 prev_v0 = &rp0->verts[prev_vn0];
1133 prev_v1 = &rp1->verts[prev_vn1];
1134
1135 // Make starting points identical
1136 if (!check_only)
1137 *prev_v0 = *prev_v1;
1138
1139 // Use the larger number of vert
1140 max_nv = __max(fp0->num_verts, fp1->num_verts);
1141
1142 // Trace through faces, adding points where needed
1143 for (n0 = n1 = 1; n0 < max_nv && n1 < max_nv; n0++, n1++) {
1144 int vn0, vn1;
1145
1146 recheck:;
1147
1148 vn0 = fp0->face_verts[(i + n0) % fp0->num_verts];
1149 vn1 = fp1->face_verts[(j - n1 + fp1->num_verts) % fp1->num_verts];
1150
1151 v0 = &rp0->verts[vn0];
1152 v1 = &rp1->verts[vn1];
1153
1154 if (PointsAreSame(v0, v1)) { // Points are at least very close.
1155 if (!check_only)
1156 *v0 = *v1; // Make the points identical
1157 } else { // The points are not the same, so we check for an extra (colinear) point
1158
1159 float d0, d1;
1160
1161 // One of these points should lie along the edge of the other. Find which is which
1162 d0 = vm_VectorDistance(v0, prev_v0);
1163 d1 = vm_VectorDistance(v1, prev_v1);
1164
1165 if (d0 > d1) { // Point 1 is presumably on the edge prev_v0 -> v0
1166
1167 // Make sure the point is actually on the edge
1168 if (CheckPointAgainstEdge(v1, prev_v0, v0, &fp0->normal)) {
1169 if (check_only)
1170 return 0;
1171 Int3(); // point isn't on edge! Bad! Get Matt!
1172 } else {
1173 if (!check_only) {
1174
1175 // Add the point
1176 AddPointToAllEdges(rp0, prev_vn0, vn0, v1);
1177 points_added++;
1178 } else {
1179 n1++; // skip edge point & resume checking
1180 goto recheck;
1181 }
1182 }
1183 } else { // Point 0 is presumably on the edge prev_v1 -> v1
1184
1185 // Make sure the point is actually on the edge
1186 if (CheckPointAgainstEdge(v0, prev_v1, v1, &fp1->normal)) {
1187 if (check_only)
1188 return 0;
1189 Int3(); // point isn't on edge! Bad! Get Matt!
1190 } else {
1191
1192 if (!check_only) {
1193 // Add the point
1194 AddPointToAllEdges(rp1, prev_vn1, vn1, v0);
1195 points_added++;
1196 } else {
1197 n0++; // skip edge point & resume checking
1198 goto recheck;
1199 }
1200 }
1201 }
1202
1203 // Start check again
1204 if (!check_only)
1205 goto check_faces;
1206 }
1207
1208 prev_vn0 = vn0;
1209 prev_vn1 = vn1;
1210
1211 prev_v0 = v0;
1212 prev_v1 = v1;
1213 }
1214
1215 if (check_only)
1216 return 1; // no errors found, so faces ok
1217
1218 ASSERT(fp0->num_verts == fp1->num_verts); // Get Matt!
1219
1220 return points_added;
1221 }
1222
1223 // Clips a pair of faces against each other.
1224 // Produces one polygon in each face that is the intersection of the two input faces,
1225 // and zero or more extra polygons, which are parts of the input faces outside the clipping faces.
1226 // The input faces are replaced by the clipped faces, and new faces (formed by the parts of the input
1227 // face outside the clip-against face) are added to the end of the room's facelist.
1228 // This routine assumes that part or all of the being-clipped face is inside the clip-against face
1229 // Parameters: rp0,face0 - the first room:face
1230 // rp1,face1 - the second room:face
1231 // Returns: true if the clip was ok, false if there was an error
1232 bool ClipFacePair(room *rp0, int face0, room *rp1, int face1) {
1233 // Clip each face aginst the other
1234 return (ClipFace(rp0, face0, rp1, face1) && ClipFace(rp1, face1, rp0, face0));
1235 }
1236
1237 // Creates a new bridge room connecting two rooms
1238 // Parameters: attroomp,attface - one end of the new room
1239 // baseroom,baseface - the other end of the new room
1240 // The new room is created by extruding from attroom/attface
1241 void BuildBridge(room *attroomp, int attface, room *baseroomp, int baseface) {
1242 room *newroomp;
1243 face *afp, *bfp;
1244 int nv, nfaces;
1245 vector delta_vec, bc, ac;
1246 vector *basevert; // a vertex on the base face
1247 int i;
1248 float cos, dist;
1249
1250 // Check that the two faces don't already have portals
1251 if (attroomp->faces[attface].portal_num != -1) {
1252 OutrageMessageBox("Cannot build bridge: There is already a portal at %d:%d", ROOMNUM(attroomp), attface);
1253 return;
1254 }
1255 if (baseroomp->faces[baseface].portal_num != -1) {
1256 OutrageMessageBox("Cannot build bridge: There is already a portal at %d:%d", ROOMNUM(baseroomp), baseface);
1257 return;
1258 }
1259
1260 // Get values from current room & face
1261 afp = &attroomp->faces[attface]; // pointer to attach face
1262 bfp = &baseroomp->faces[baseface]; // pointer to base face
1263 nv = afp->num_verts; // number of verts in attach face
1264 nfaces = nv + 2; // number of faces in new room
1265
1266 // Make sure all points on each face are on the front of the other face
1267 vector tnorm = -afp->normal;
1268 int t = CheckFaceToPlane(baseroomp, baseface, &attroomp->verts[afp->face_verts[0]], &tnorm);
1269 if (t != -1) {
1270 OutrageMessageBox("Can't build bridge: Vertex %d on %d:%d is behind %d:%d", t, ROOMNUM(baseroomp), baseface,
1271 ROOMNUM(attroomp), attface);
1272 return;
1273 }
1274 tnorm = -bfp->normal;
1275 t = CheckFaceToPlane(attroomp, attface, &baseroomp->verts[bfp->face_verts[0]], &tnorm);
1276 if (t != -1) {
1277 OutrageMessageBox("Can't build bridge: Vertex %d on %d:%d is behind %d:%d", t, ROOMNUM(attroomp), attface,
1278 ROOMNUM(baseroomp), baseface);
1279 return;
1280 }
1281
1282 // Compute normalized vector from attach face to base face
1283 #ifndef NEWEDITOR
1284 ComputeCenterPointOnFace(&bc, baseroomp, baseface);
1285 ComputeCenterPointOnFace(&ac, attroomp, attface);
1286 #else
1287 ComputeFaceBoundingCircle(&bc, baseroomp, baseface);
1288 ComputeFaceBoundingCircle(&ac, attroomp, attface);
1289 #endif
1290 dist = vm_GetNormalizedDir(&delta_vec, &bc, &ac);
1291
1292 // Compute the cosine of the angle between our vector and the normal of the base face
1293 cos = delta_vec * bfp->normal;
1294
1295 // Get a pointer to a vertex on the base face
1296 basevert = &baseroomp->verts[bfp->face_verts[0]];
1297
1298 // Get a pointer to our room
1299 newroomp = CreateNewRoom(nv * 2, nfaces, 0);
1300 if (newroomp == NULL) {
1301 OutrageMessageBox("Cannot build bridge: No free rooms.");
1302 return;
1303 }
1304
1305 // Create new vertices in the plane of the base face
1306 for (i = 0; i < nv; i++) {
1307 float dist;
1308
1309 // Copy from att face to new room
1310 newroomp->verts[i] = attroomp->verts[afp->face_verts[nv - 1 - i]];
1311
1312 // Get distance from base face to this vertex
1313 dist = ((*basevert - newroomp->verts[i]) * bfp->normal) / cos;
1314
1315 // Get the new vert
1316 newroomp->verts[nv + i] = newroomp->verts[i] + (delta_vec * dist);
1317 }
1318
1319 // Set the faces for the room
1320 InitRoomFace(&newroomp->faces[0], nv);
1321 for (i = 0; i < nv; i++)
1322 newroomp->faces[0].face_verts[i] = i;
1323
1324 InitRoomFace(&newroomp->faces[1], nv);
1325 for (i = 0; i < nv; i++)
1326 newroomp->faces[1].face_verts[i] = nv * 2 - 1 - i;
1327
1328 for (i = 0; i < nfaces - 2; i++) {
1329 InitRoomFace(&newroomp->faces[i + 2], 4);
1330 newroomp->faces[i + 2].face_verts[0] = i;
1331 newroomp->faces[i + 2].face_verts[1] = i + nv;
1332 newroomp->faces[i + 2].face_verts[2] = ((i + 1) % nv) + nv;
1333 newroomp->faces[i + 2].face_verts[3] = (i + 1) % nv;
1334 }
1335
1336 // Set normals
1337 if (!ResetRoomFaceNormals(newroomp))
1338 Int3(); // Get Matt
1339
1340 // Set textures
1341 #ifndef NEWEDITOR
1342 for (i = 0; i < nfaces; i++)
1343 newroomp->faces[i].tmap = i + 1;
1344 #else
1345 int texnum = Editor_state.GetCurrentTexture();
1346 // Apply texture
1347 for (i = 0; i < nfaces; i++)
1348 HTextureApplyToRoomFace(newroomp, i, texnum);
1349 #endif
1350
1351 // Assign UVs
1352 AssignDefaultUVsToRoom(newroomp);
1353
1354 // Connect the new room to the attach room
1355 LinkRoomsSimple(Rooms, ROOMNUM(attroomp), attface, ROOMNUM(newroomp), 0);
1356
1357 // Clip the connecting faces against each other
1358 t = ClipFacePair(newroomp, 1, baseroomp, baseface);
1359 ASSERT(t != 0);
1360
1361 // Make the two faces match exactly
1362 MatchPortalFaces(baseroomp, baseface, newroomp, 1);
1363
1364 // Connect the new room to the base room
1365 LinkRoomsSimple(Rooms, ROOMNUM(baseroomp), baseface, ROOMNUM(newroomp), 1);
1366
1367 // Set Changed flag
1368 World_changed = 1;
1369 }
1370
1371 #define JOIN_EPSILON 0.01
1372
1373 // Connects two rooms by changing the shape of one room to attach to the other
1374 // Pretty similar to BuildBridge(), but doesn't create a new room
1375 // Parameters: attroomp,attface - one end of the new room
1376 // baseroom,baseface - the other end of the new room
1377 void JoinRooms(room *attroomp, int attface, room *baseroomp, int baseface) {
1378 face *afp, *bfp;
1379 int nv, nfaces, t;
1380 vector delta_vec;
1381 vector *basevert; // a vertex on the base face
1382 int i;
1383 float cos;
1384 vector save_verts[MAX_VERTS_PER_FACE]; // save verts for face in case we bail
1385
1386 // Check that the two faces don't already have portals
1387 if (attroomp->faces[attface].portal_num != -1) {
1388 OutrageMessageBox("Cannot join: There is already a portal at %d:%d", ROOMNUM(attroomp), attface);
1389 return;
1390 }
1391 if (baseroomp->faces[baseface].portal_num != -1) {
1392 OutrageMessageBox("Cannot join: There is already a portal at %d:%d", ROOMNUM(baseroomp), baseface);
1393 return;
1394 }
1395
1396 // Get values from current room & face
1397 afp = &attroomp->faces[attface]; // pointer to attach face
1398 bfp = &baseroomp->faces[baseface]; // pointer to base face
1399 nv = afp->num_verts; // number of verts in attach face
1400 nfaces = nv + 2; // number of faces in new room
1401
1402 // Get a pointer to a vertex on the base face
1403 basevert = &baseroomp->verts[bfp->face_verts[0]];
1404
1405 // Make sure all points on the attach face are on the front of the base face
1406 // We could probably take this check out and catch the problem when we're moving the points
1407 vector tnorm = -bfp->normal;
1408 t = CheckFaceToPlane(attroomp, attface, basevert, &tnorm);
1409 if (t != -1) {
1410 if (OutrageMessageBox(
1411 MBOX_YESNO,
1412 "One or more points on the attach face are behind the base face.\nDo you still want to join?") != IDYES) {
1413 EditorStatus("Join aborted.");
1414 return;
1415 }
1416 }
1417
1418 // Make a list of all verts in the attach room that are in a portal
1419 int portal_list[MAX_VERTS_PER_ROOM];
1420 int num_portal_verts;
1421 num_portal_verts = BuildListOfPortalVerts(attroomp, portal_list);
1422
1423 // Save old verts
1424 for (i = 0; i < nv; i++)
1425 save_verts[i] = attroomp->verts[afp->face_verts[i]];
1426
1427 int answer = OutrageMessageBox(MBOX_YESNO, "Do you want to connect to the center of the base face?\n\n"
1428 "Answer NO to extrude straight from the current face.");
1429
1430 // Compute normalized vector from attach face to base face.
1431 // This is the direction in which we will move the attach face
1432 if (answer == IDYES) { // extrude toward the center of the base face
1433 vector bc, ac;
1434 #ifndef NEWEDITOR
1435 ComputeCenterPointOnFace(&bc, baseroomp, baseface);
1436 ComputeCenterPointOnFace(&ac, attroomp, attface);
1437 #else
1438 ComputeFaceBoundingCircle(&bc, baseroomp, baseface);
1439 ComputeFaceBoundingCircle(&ac, attroomp, attface);
1440 #endif
1441 vm_GetNormalizedDir(&delta_vec, &bc, &ac);
1442 } else { // extrude straight from attach face
1443 delta_vec = afp->normal;
1444 }
1445
1446 // Compute the cosine of the angle between our vector and the normal of the base face
1447 cos = delta_vec * bfp->normal;
1448
1449 // If cos is zero, two center points are the same, so no move
1450 if (fabs(cos) < JOIN_EPSILON)
1451 goto no_move_points;
1452
1453 // Move the verts on the attach face to lie in the plane as the base face
1454 for (i = 0; i < nv; i++) {
1455 vector *vp = &attroomp->verts[afp->face_verts[i]];
1456 float dist;
1457
1458 // Get distance from base face to this vertex
1459 dist = ((*basevert - *vp) * bfp->normal) / cos;
1460
1461 // If we're moving the point, make sure it's not part of a portal
1462 if (fabs(dist) > POINT_TO_PLANE_EPSILON) {
1463
1464 // Look through list of portal verts for this vert
1465 for (t = 0; t < num_portal_verts; t++)
1466 if (portal_list[t] == afp->face_verts[i]) { // Vert is in a portal. Abort!
1467 OutrageMessageBox("Can't form joint because vertex %d on %d:%d is shared with a portal.\n\nTry doing the "
1468 "join the other way, or using the Bridge function.",
1469 i, ROOMNUM(attroomp), attface);
1470 goto abort_join;
1471 }
1472
1473 // Move the vert
1474 *vp += (delta_vec * dist);
1475 }
1476 }
1477
1478 // Recompute the normals in the room
1479 if (!ResetRoomFaceNormals(attroomp))
1480 Int3(); // Get Matt
1481
1482 // What about the uvs for the faces that changed?
1483
1484 no_move_points:;
1485
1486 // Clip the connecting faces against each other
1487 if (!ClipFacePair(attroomp, attface, baseroomp, baseface)) {
1488 OutrageMessageBox("Error making portal -- faces probably don't overlap.");
1489
1490 abort_join:;
1491
1492 // Restore old verts
1493 for (i = 0; i < nv; i++)
1494 attroomp->verts[afp->face_verts[i]] = save_verts[i];
1495
1496 // Recompute the normals in the room
1497 if (!ResetRoomFaceNormals(attroomp))
1498 Int3(); // Get Matt
1499
1500 return;
1501 }
1502
1503 // Make the two faces match exactly
1504 MatchPortalFaces(baseroomp, baseface, attroomp, attface);
1505
1506 // Create the portals between the rooms
1507 LinkRoomsSimple(Rooms, ROOMNUM(baseroomp), baseface, ROOMNUM(attroomp), attface);
1508
1509 // Set Changed flag
1510 World_changed = 1;
1511 }
1512
1513 // Connects two rooms if they already match up exactly
1514 // Parameters: attroomp,attface - one end of the new room
1515 // baseroom,baseface - the other end of the new room
1516 void JoinRoomsExact(room *attroomp, int attface, room *baseroomp, int baseface) {
1517 // Check that the two faces don't already have portals
1518 if (attroomp->faces[attface].portal_num != -1) {
1519 OutrageMessageBox("Cannot join: There is already a portal at %d:%d", ROOMNUM(attroomp), attface);
1520 return;
1521 }
1522 if (baseroomp->faces[baseface].portal_num != -1) {
1523 OutrageMessageBox("Cannot join: There is already a portal at %d:%d", ROOMNUM(baseroomp), baseface);
1524 return;
1525 }
1526
1527 // Check if we can do an exact match
1528 if (!MatchPortalFaces(baseroomp, baseface, attroomp, attface, 1)) {
1529 OutrageMessageBox("Cannot do exact join: These faces do not match");
1530 return;
1531 }
1532
1533 // Make the two faces match exactly
1534 MatchPortalFaces(baseroomp, baseface, attroomp, attface);
1535
1536 // Create the portals between the rooms
1537 LinkRoomsSimple(Rooms, ROOMNUM(baseroomp), baseface, ROOMNUM(attroomp), attface);
1538
1539 // Set Changed flag
1540 World_changed = 1;
1541 }
1542
1543 bool FaceIsPlanar(int nv, int16_t *face_verts, vector *normal, vector *verts);
1544
1545 // Combine two faces, if they can be combined
1546 // Parameters: rp - the room the faces are in
1547 // face0,face1 - the two faces
1548 // Returns: true if the faces were combined, else false
1549 // Note: The UV coordinates of the new face are derrived from face0
1550 bool CombineFaces(room *rp, int face0, int face1) {
1551 face *fp0 = &rp->faces[face0], *fp1 = &rp->faces[face1];
1552 int nv0 = fp0->num_verts, nv1 = fp1->num_verts;
1553 int v0, v1;
1554 int16_t vertlist[MAX_VERTS_PER_FACE];
1555 roomUVL uvllist[MAX_VERTS_PER_FACE];
1556 int i, nv;
1557 int first0, first1, n0, n1;
1558
1559 ASSERT(face0 != face1);
1560
1561 // Check for portals
1562 if ((fp0->portal_num != -1) || (fp1->portal_num != -1)) {
1563 #ifndef NEWEDITOR
1564 SetErrorMessage(
1565 "You cannot combine portal faces.\n\nMattT will add this functionality if you need it. Bug him about it.");
1566 #else
1567 SetErrorMessage("You cannot combine portal faces.");
1568 #endif
1569 return 0;
1570 }
1571
1572 // Make sure faces share an edge
1573 if (!FindSharedEdge(fp0, fp1, &v0, &v1)) {
1574 #ifdef NEWEDITOR
1575 if (ned_FindSharedEdge(fp0, fp1, &v0, &v1))
1576 SetErrorMessage("The face normals point in opposite directions.");
1577 else
1578 #endif
1579 SetErrorMessage("The faces do not share an edge.");
1580 return 0;
1581 }
1582
1583 // Get indices for first & last points in each face. One shared point is copied with each face.
1584 first0 = v0 + 1;
1585 n0 = nv0 - 1;
1586 first1 = v1 + 1;
1587 n1 = nv1 - 1;
1588
1589 // See if faces share additional edges
1590 while (1) { // check forward on face0, backward on face1
1591 int check0 = (first0 + 1) % nv0, check1 = (first1 + n1 - 1) % nv1;
1592
1593 if (fp0->face_verts[check0] != fp1->face_verts[check1])
1594 break;
1595
1596 first0++;
1597 n0--;
1598 n1--;
1599 }
1600 while (1) { // check forward on face1, backward on face0
1601 int check1 = (first1 + 1) % nv1, check0 = (first0 + n0 - 1) % nv0;
1602
1603 if (fp0->face_verts[check0] != fp1->face_verts[check1])
1604 break;
1605
1606 first1++;
1607 n1--;
1608 n0--;
1609 }
1610
1611 // Build list of verts for new face
1612 nv = 0;
1613 for (i = 0; i < n0; i++) { // Add points from first face
1614 vertlist[nv] = fp0->face_verts[(first0 + i) % nv0];
1615 uvllist[nv] = fp0->face_uvls[(first0 + i) % nv0];
1616 nv++;
1617 }
1618 for (i = 0; i < n1; i++) { // Add points from second face
1619 vertlist[nv] = fp1->face_verts[(first1 + i) % nv1];
1620 uvllist[nv] = fp1->face_uvls[(first1 + i) % nv1];
1621 nv++;
1622 }
1623
1624 // Calculate new normal
1625 vector new_normal;
1626 ComputeNormal(&new_normal, nv, vertlist, rp->verts);
1627
1628 // Check if new face is planar
1629 if (!FaceIsPlanar(nv, vertlist, &new_normal, rp->verts)) {
1630 SetErrorMessage("The new face would not be planar.");
1631 return 0;
1632 }
1633
1634 // Check if new face is convex
1635 int t;
1636 if ((t = CheckFaceConcavity(nv, vertlist, &new_normal, rp->verts)) != -1) {
1637 SetErrorMessage("The new face would be concave (at vertex %d).", vertlist[t]);
1638 return 0;
1639 }
1640
1641 // Reset face0 for new verts
1642 ReInitRoomFace(fp0, nv);
1643
1644 // Copy into new face
1645 for (i = 0; i < nv; i++) {
1646 fp0->face_verts[i] = vertlist[i];
1647 fp0->face_uvls[i] = uvllist[i];
1648 }
1649
1650 // Set new normal
1651 fp0->normal = new_normal;
1652
1653 // Reassign uvls in face
1654 AssignUVsToFace(rp, face0, &fp0->face_uvls[0], &fp0->face_uvls[1], 0, 1);
1655
1656 // Delete the old face
1657 DeleteRoomFace(rp, face1);
1658
1659 // Set flag
1660 World_changed = 1;
1661
1662 // Everything's ok
1663 return 1;
1664 }
1665
1666 // Deletes the given room from the mine
1667 void DeleteRoomFromMine(room *rp) {
1668 int i, p;
1669
1670 // See if this room is linked to the terrain
1671 for (p = 0; p < rp->num_portals; p++) {
1672 portal *pp = &rp->portals[p];
1673 if (pp->croom == -1) {
1674 OutrageMessageBox("You can't delete a room that attaches to the terrain. Detach the room before deleting.");
1675 return;
1676 }
1677 }
1678
1679 // Check if player or viewer object in this room
1680 for (i = rp->objects; i != -1; i = Objects[i].next) {
1681
1682 // Is this the player object?
1683 if (i == OBJNUM(Player_object)) {
1684 OutrageMessageBox("You can't delete the room with the player in it.");
1685 return;
1686 }
1687
1688 #ifndef NEWEDITOR
1689 // Is this the viewer object?
1690 if (i == OBJNUM(Viewer_object)) { // Try to find another viewer
1691 int objnum = FindNextViewerObject(Viewer_object->id + 1, -1);
1692 if (objnum == OBJNUM(Viewer_object)) {
1693 OutrageMessageBox("You can't delete the room with the only viewer in it.");
1694 return;
1695 } else
1696 SelectNextViewer();
1697 }
1698 #endif
1699 }
1700
1701 // count the number of rooms used. if only one, don't delete it.
1702 for (i = 0; i <= Highest_room_index; i++)
1703 if (Rooms[i].used && (i != ROOMNUM(rp)))
1704 break;
1705 if (i > Highest_room_index) {
1706 OutrageMessageBox("You can't delete the only room in the mine.");
1707 return;
1708 }
1709
1710 // If this is current room, set new current room
1711 if (rp == Curroomp) {
1712 room *newroomp = NULL;
1713
1714 // Look for connected room
1715 for (p = 0; p < rp->num_portals; p++)
1716 if (rp->portals[p].croom != -1) {
1717 newroomp = &Rooms[rp->portals[0].croom];
1718 break;
1719 }
1720
1721 // If didn't find connected room, look for any room
1722 if (newroomp == NULL) {
1723 for (i = 0; i <= Highest_room_index; i++) {
1724 if (Rooms[i].used && (i != ROOMNUM(rp))) {
1725 newroomp = &Rooms[i];
1726 break;
1727 }
1728 }
1729 }
1730
1731 ASSERT(newroomp != NULL);
1732
1733 #ifndef NEWEDITOR
1734 Curroomp = newroomp;
1735 Curface = Curedge = Curvert = 0;
1736 Curportal = -1;
1737 #else
1738 theApp.m_pLevelWnd->SetPrim(newroomp, 0, -1, 0, 0);
1739 #endif
1740
1741 EditorStatus("Current room set to %d.", ROOMNUM(Curroomp));
1742 }
1743
1744 // Delete connections
1745 while (rp->num_portals) {
1746 portal *pp = &rp->portals[0];
1747 ASSERT(pp->croom != -1);
1748 DeleteRoomPortal(&Rooms[pp->croom], pp->cportal);
1749 DeleteRoomPortal(rp, 0);
1750 }
1751
1752 // If this is marked room, clear marked room
1753 if (Markedroomp == rp)
1754 Markedroomp = NULL;
1755
1756 // Free the objects in this room
1757 for (i = rp->objects; i != -1; i = Objects[i].next) {
1758 #ifdef NEWEDITOR
1759 if (IS_GENERIC(Objects[i].type))
1760 LevelObjDecrementObject(Objects[i].id);
1761 else if (Objects[i].type == OBJ_DOOR) {
1762 ASSERT(rp->flags & RF_DOOR);
1763 LevelDoorDecrementDoorway(Objects[i].id);
1764 }
1765 #endif
1766 ObjDelete(i);
1767 }
1768
1769 #ifdef NEWEDITOR
1770 // Unmark the textures in this room
1771 for (i = 0; i < rp->num_faces; i++)
1772 LevelTexDecrementTexture(rp->faces[i].tmap);
1773 #endif
1774
1775 // Delete any triggers in this room
1776 face *fp;
1777 for (i = 0, fp = rp->faces; i < rp->num_faces; fp++, i++)
1778 if (fp->flags & FF_HAS_TRIGGER)
1779 DeleteTrigger(ROOMNUM(rp), i);
1780
1781 // Remove this room from the selected list (if there)
1782 RemoveRoomFromSelectedList(ROOMNUM(rp));
1783
1784 // Free up the room
1785 FreeRoom(rp);
1786 }
1787
1788 #define DROP_ROOM_OFFSET 20.0
1789
1790 // Places a room a short distance from the specified room & face
1791 // The new room is not attached to anything
1792 // Parameters: baseroomp,baseface - the new room is dropped off of this face
1793 // droproom_num - the room to be dropped
1794 void DropRoom(room *baseroomp, int baseface, int droproom_num) {
1795 int roomnum;
1796 room *newroomp;
1797 vector oldcenter, facecenter, newcenter, offset;
1798 float rad;
1799
1800 // Create a new room
1801 roomnum = GetFreeRoom(0);
1802 if (roomnum == -1) {
1803 OutrageMessageBox("Cannot drop room: No free rooms.");
1804 return;
1805 }
1806 newroomp = &Rooms[roomnum];
1807
1808 // Copy data to new room
1809 CopyRoom(newroomp, &Rooms[droproom_num]);
1810
1811 // Compute offset
1812 rad = ComputeRoomBoundingSphere(&oldcenter, newroomp);
1813 #ifndef NEWEDITOR
1814 ComputeCenterPointOnFace(&facecenter, baseroomp, baseface);
1815 #else
1816 ComputeFaceBoundingCircle(&facecenter, baseroomp, baseface);
1817 #endif
1818 newcenter = facecenter - (baseroomp->faces[baseface].normal * (rad + DROP_ROOM_OFFSET));
1819 offset = newcenter - oldcenter;
1820
1821 // Add in offset to all the points
1822 for (int i = 0; i < newroomp->num_verts; i++)
1823 newroomp->verts[i] += offset;
1824
1825 // Set current room to new room
1826 #ifndef NEWEDITOR
1827 Curroomp = newroomp;
1828 Curface = Curedge = Curvert = 0;
1829 Curportal = -1;
1830 #else
1831 theApp.m_pLevelWnd->SetPrim(newroomp, 0, -1, 0, 0);
1832 #endif
1833
1834 // We're done
1835 World_changed = 1;
1836 }
1837
1838 // Place a room on the terrain for orientation before attachment
1839 // Parameters: cellnum - the cell where the room is being placed
1840 // placed_room - the number of the room to be attached
1841 // placed_room_face the face on placed_room that's attached
1842 void PlaceExternalRoom(int cellnum, int placed_room, int placed_room_face, bool align_to_terrain) {
1843 room *placedroomp = &Rooms[placed_room];
1844
1845 // Clear the placed group if one exists
1846 Placed_group = NULL;
1847
1848 // Clear the placed door if one exists
1849 if (Placed_door != -1) {
1850 ASSERT(Placed_room != -1);
1851 FreeRoom(&Rooms[Placed_room]);
1852 Placed_door = Placed_door = -1;
1853 }
1854
1855 // Set globals
1856 Placed_room = placed_room;
1857 Placed_room_face = placed_room_face;
1858 Placed_room_angle = 0;
1859 Placed_baseroomp = NULL;
1860
1861 if (align_to_terrain)
1862 Placed_room_orient.fvec = TerrainNormals[MAX_TERRAIN_LOD - 1][cellnum].normal1;
1863 else
1864 Placed_room_orient.fvec = Identity_matrix.uvec;
1865
1866 // Compute attach point on terrain
1867 ComputeTerrainSegmentCenter(&Placed_room_attachpoint, cellnum);
1868
1869 // Compute attach points on each face
1870 #ifndef NEWEDITOR
1871 ComputeCenterPointOnFace(&Placed_room_origin, placedroomp, placed_room_face);
1872 #else
1873 ComputeFaceBoundingCircle(&Placed_room_origin, placedroomp, placed_room_face);
1874 #endif
1875
1876 // Compute initial orientation matrix
1877 ComputePlacedRoomMatrix();
1878
1879 // Set the flag
1880 State_changed = 1;
1881 }
1882
1883 // Find all the matching faces between two rooms and join them
1884 // Returns the number of new portals created
1885 int JoinAllMatchingFaces(room *rp0, room *rp1) {
1886 face *fp0, *fp1;
1887 int f0, f1;
1888 int join_count = 0;
1889
1890 // Check each face in room 0 against each in room 1
1891 for (f0 = 0, fp0 = rp0->faces; f0 < rp0->num_faces; f0++, fp0++) {
1892
1893 // Check if face 0 is already part of portal
1894 if (fp0->portal_num != -1)
1895 continue;
1896
1897 // Check current face against all faces in room 1
1898 for (f1 = 0, fp1 = rp1->faces; f1 < rp1->num_faces; f1++, fp1++) {
1899
1900 // Check if face 1 is already part of portal
1901 if (fp1->portal_num != -1)
1902 continue;
1903
1904 // First check if faces have same number of verts
1905 // This used to check if the normals were the same, but I decided that was bad. -MT, 3/30/99
1906 if ((fp0->num_verts == fp1->num_verts)) {
1907 int i, j, n;
1908
1909 // Find one point in common
1910 for (i = 0; i < fp0->num_verts; i++) {
1911 for (j = 0; j < fp1->num_verts; j++)
1912 if (PointsAreSame(&rp0->verts[fp0->face_verts[i]], &rp1->verts[fp1->face_verts[j]]))
1913 break;
1914 if (j < fp1->num_verts)
1915 break;
1916 }
1917 if (i >= fp0->num_verts) // Couldn't find a match
1918 continue; //..so go on to next face
1919
1920 // Trace through verts in faces, making sure they match
1921 for (n = 1; n < fp0->num_verts; n++) {
1922 vector *v0, *v1;
1923
1924 v0 = &rp0->verts[fp0->face_verts[(i + n) % fp0->num_verts]];
1925 v1 = &rp1->verts[fp1->face_verts[(j - n + fp1->num_verts) % fp1->num_verts]];
1926
1927 if (!PointsAreSame(v0, v1)) // Found mismatch
1928 break;
1929
1930 *v0 = *v1; // make points *exactly* the same
1931 }
1932 if (n < fp0->num_verts) // Found a mismatch
1933 continue; //..so go on to next face
1934
1935 // Found extact match, so join
1936 LinkRoomsSimple(Rooms, ROOMNUM(rp0), f0, ROOMNUM(rp1), f1);
1937
1938 // Increment count
1939 join_count++;
1940 }
1941 }
1942 }
1943
1944 return join_count;
1945 }
1946
1947 // Find all the overlapping faces between two rooms and join them
1948 // Returns the number of new portals created
1949 int JoinAllOverlappingFaces(room *rp0, room *rp1) {
1950 face *fp0, *fp1;
1951 int f0, f1;
1952 int join_count = 0;
1953
1954 // Check each face in room 0 against each in room 1
1955 for (f0 = 0, fp0 = rp0->faces; f0 < rp0->num_faces; f0++, fp0++) {
1956
1957 // Check if face 0 is already part of portal
1958 if (fp0->portal_num != -1)
1959 continue;
1960
1961 // Check current face against all faces in room 1
1962 for (f1 = 0, fp1 = rp1->faces; f1 < rp1->num_faces; f1++, fp1++) {
1963
1964 // Check if face 1 is already part of portal
1965 if (fp1->portal_num != -1)
1966 continue;
1967
1968 // This used to check if normals the same, but I decided that was bad. -MT, 3/30/99
1969 {
1970 int i;
1971 vector *planepoint, *norm;
1972
1973 // Normals are same, so check if points are all in the same plane
1974 planepoint = &rp1->verts[fp1->face_verts[0]];
1975 norm = &fp1->normal;
1976 for (i = 0; i < fp0->num_verts; i++)
1977 if (CheckPointToPlane(&rp0->verts[fp0->face_verts[i]], planepoint, norm))
1978 break;
1979 if (i < fp0->num_verts)
1980 continue;
1981
1982 planepoint = &rp0->verts[fp0->face_verts[0]];
1983 norm = &fp0->normal;
1984 for (i = 0; i < fp1->num_verts; i++)
1985 if (CheckPointToPlane(&rp1->verts[fp1->face_verts[i]], planepoint, norm))
1986 break;
1987 if (i < fp1->num_verts)
1988 continue;
1989
1990 // Clip the connecting faces against each other
1991 if (ClipFacePair(rp0, f0, rp1, f1)) {
1992
1993 // Make the two faces match exactly
1994 MatchPortalFaces(rp0, f0, rp1, f1);
1995
1996 // Create the portals between the rooms
1997 LinkRoomsSimple(Rooms, ROOMNUM(rp0), f0, ROOMNUM(rp1), f1);
1998
1999 // Reset face pointer, which may have been changed by the clip
2000 fp0 = &rp0->faces[f0];
2001
2002 // Increment count
2003 join_count++;
2004
2005 // Stop checking this face, since we've formed a portal for it
2006 break;
2007 } else { // it's kind of bad if the clip fails, but we have to live with it
2008
2009 // Reset face pointers, which may have been changed by the clip
2010 fp0 = &rp0->faces[f0];
2011 fp1 = &rp1->faces[f1];
2012 }
2013 }
2014 }
2015 }
2016
2017 return join_count;
2018 }
2019
2020 // Find all the adjacent faces between two rooms and join them
2021 void JoinAllAdjacentFaces(room *rp0, room *rp1) {
2022 int join_count;
2023 int answer;
2024
2025 answer = OutrageMessageBox(MBOX_YESNOCANCEL, "Do you want to include non-matching faces?\n\n"
2026 "Selecting YES means faces will be joined if they overlap.\n"
2027 "Selecting NO means faces much match exactly.");
2028
2029 if (answer == IDCANCEL)
2030 return;
2031
2032 if (answer == IDYES)
2033 join_count = JoinAllOverlappingFaces(rp0, rp1);
2034 else
2035 join_count = JoinAllMatchingFaces(rp0, rp1);
2036
2037 World_changed = 1;
2038
2039 OutrageMessageBox("%d new portals created.\n", join_count);
2040 }
2041
2042 int FindNeighbor(room *rp, int facenum, int edgenum) {
2043 face *fp = &rp->faces[facenum];
2044 int v0, v1, f, v;
2045
2046 v0 = fp->face_verts[edgenum];
2047 v1 = fp->face_verts[(edgenum + 1) % fp->num_verts];
2048
2049 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++)
2050 for (v = 0; v < fp->num_verts; v++)
2051 if ((fp->face_verts[v] == v1) && (fp->face_verts[(v + 1) % fp->num_verts] == v0))
2052 return f;
2053
2054 return -1;
2055 }
2056
2057 void PropagateFromFace(room *rp, int facenum, uint8_t *face_flags, bool matching_faces_only) {
2058 face *fp = &rp->faces[facenum];
2059
2060 face_flags[facenum] = 1;
2061
2062 for (int v = 0; v < fp->num_verts; v++) {
2063 int t;
2064
2065 t = FindNeighbor(rp, facenum, v);
2066
2067 if ((t != -1) && !face_flags[t])
2068 if (!matching_faces_only || (rp->faces[t].tmap == fp->tmap)) {
2069 HTexturePropagateToFace(rp, t, rp, facenum);
2070 PropagateFromFace(rp, t, face_flags, matching_faces_only);
2071 }
2072 }
2073 }
2074
2075 // Propagate a texture from one face to all the faces in the room
2076 // If matching_faces_only is set, only propagate if the face has the same texture
2077 void PropagateToAllFaces(room *rp, int facenum, bool matching_faces_only) {
2078 uint8_t face_flags[MAX_FACES_PER_ROOM];
2079
2080 // Clear flags
2081 for (int i = 0; i < rp->num_faces; i++)
2082 face_flags[i] = 0;
2083
2084 // Start recursive function
2085 PropagateFromFace(rp, facenum, face_flags, matching_faces_only);
2086
2087 // Set flag
2088 World_changed = 1;
2089 }
2090
2091 // Splits a face unto triangles, by fanning
2092 // Parameters: rp,facenum - the face to triangulate
2093 // vertnum - the vert that is the base of the fan
2094 void TriangulateFace(room *rp, int facenum, int vertnum) {
2095 face *fp = &rp->faces[facenum], *nfp;
2096 int num_new_faces, first_new_face, i;
2097
2098 if (fp->num_verts == 3) {
2099 OutrageMessageBox("Face %d has three verts and cannot be split.", facenum);
2100 return;
2101 }
2102
2103 if (fp->portal_num != -1) {
2104 OutrageMessageBox("You cannot split a face that's part of a portal.");
2105 return;
2106 }
2107
2108 if (fp->flags & (FF_HAS_TRIGGER + FF_FLOATING_TRIG)) {
2109 OutrageMessageBox("You cannot split a face that has a trigger.");
2110 return;
2111 }
2112
2113 // Add new faces
2114 num_new_faces = fp->num_verts - 2;
2115 first_new_face = RoomAddFaces(rp, num_new_faces);
2116
2117 // Reset pointer to our old face
2118 fp = &rp->faces[facenum];
2119
2120 // Set up each face
2121 for (i = 0, nfp = &rp->faces[first_new_face]; i < num_new_faces; i++, nfp++) {
2122 InitRoomFace(nfp, 3);
2123
2124 // Set verts & uvls for this face
2125 nfp->face_verts[0] = fp->face_verts[vertnum];
2126 nfp->face_uvls[0] = fp->face_uvls[vertnum];
2127 for (int v = 1; v < 3; v++) {
2128 nfp->face_verts[v] = fp->face_verts[(vertnum + i + v) % fp->num_verts];
2129 nfp->face_uvls[v] = fp->face_uvls[(vertnum + i + v) % fp->num_verts];
2130 }
2131
2132 // Copy other data for this face
2133 CopyFaceFlags(nfp, fp);
2134 #ifndef NEWEDITOR
2135 nfp->tmap = fp->tmap;
2136 #else
2137 // Apply texture
2138 HTextureApplyToRoomFace(rp, first_new_face + i, nfp->tmap);
2139 #endif
2140 nfp->light_multiple = fp->light_multiple;
2141
2142 // Compute the normal for this face
2143 ComputeFaceNormal(rp, first_new_face + i);
2144 }
2145
2146 // Delete original face
2147 DeleteRoomFace(rp, facenum);
2148
2149 // Check for degenerate faces (caused by colinear points) and merge with adjacent faces
2150 first_new_face--; // original face deleted, causing shift
2151 bool ok;
2152 do { // Check first face
2153 ok = ComputeFaceNormal(rp, first_new_face);
2154 if (!ok) {
2155 face *fp = &rp->faces[first_new_face];
2156 ASSERT(first_new_face < rp->num_faces - 1);
2157 AddVertToFace(rp, first_new_face, rp->faces[first_new_face + 1].face_verts[2], fp->num_verts - 1);
2158 rp->faces[first_new_face].face_uvls[rp->faces[first_new_face].num_verts - 1] =
2159 rp->faces[first_new_face + 1].face_uvls[2];
2160 DeleteRoomFace(rp, first_new_face + 1);
2161 num_new_faces--;
2162 }
2163 } while (!ok);
2164 do { // Check last face
2165 ok = ComputeFaceNormal(rp, rp->num_faces - 1);
2166 if (!ok) {
2167 face *fp = &rp->faces[rp->num_faces - 2];
2168 ASSERT(first_new_face < rp->num_faces - 1);
2169 AddVertToFace(rp, rp->num_faces - 1, rp->faces[rp->num_faces - 2].face_verts[1], 0);
2170 rp->faces[rp->num_faces - 1].face_uvls[1] = rp->faces[rp->num_faces - 2].face_uvls[1];
2171 DeleteRoomFace(rp, rp->num_faces - 2);
2172 num_new_faces--;
2173 }
2174 } while (!ok);
2175
2176 // We're done
2177 EditorStatus("Face %d replace by %d new faces", facenum, num_new_faces);
2178 #ifndef NEWEDITOR
2179 World_changed = 1;
2180 #endif
2181 }
2182
2183 // Deletes the connection between two rooms. Deletes both portals.
2184 void DeletePortalPair(room *rp, int portalnum) {
2185 int roomnum = ROOMNUM(rp);
2186 portal *pp = &rp->portals[portalnum];
2187 int croom = pp->croom, cportal = pp->cportal;
2188
2189 // Make sure the connecting portal points back to this portal
2190 if ((Rooms[croom].portals[cportal].croom != roomnum) || (Rooms[croom].portals[cportal].cportal != portalnum)) {
2191 OutrageMessageBox("Cannot delete this portal: the connecting portal does not point back at it.\n");
2192 return;
2193 }
2194
2195 // Check all the faces in this portal to make sure they point back to the portal
2196 if (rp->faces[pp->portal_face].portal_num != portalnum) {
2197 OutrageMessageBox("Cannot delete this portal: its face does not point back at it.");
2198 return;
2199 }
2200
2201 DeleteRoomPortal(rp, portalnum);
2202 DeleteRoomPortal(&Rooms[croom], cportal);
2203
2204 EditorStatus("Deleted room %d portal %d and room %d portal %d", roomnum, portalnum, croom, cportal);
2205
2206 World_changed = 1;
2207 }
2208
2209 // Flips a face
2210 void FlipFace(room *rp, int facenum) {
2211 face *fp = &rp->faces[facenum];
2212
2213 ASSERT(fp->portal_num == -1);
2214
2215 for (int i = 0; i < fp->num_verts / 2; i++) {
2216 int v = fp->face_verts[i];
2217 fp->face_verts[i] = fp->face_verts[fp->num_verts - 1 - i];
2218 fp->face_verts[fp->num_verts - 1 - i] = v;
2219
2220 roomUVL uvl = fp->face_uvls[i];
2221 fp->face_uvls[i] = fp->face_uvls[fp->num_verts - 1 - i];
2222 fp->face_uvls[fp->num_verts - 1 - i] = uvl;
2223 }
2224
2225 // Get new normal
2226 if (!ComputeFaceNormal(rp, facenum))
2227 Int3(); // Bad! Get Matt!
2228
2229 #ifndef NEWEDITOR
2230 World_changed = 1;
2231 EditorStatus("Room %d face %d flipped.", ROOMNUM(rp), facenum);
2232 #else
2233 if (ROOMNUM(rp) < MAX_ROOMS)
2234 EditorStatus("Room %d face %d flipped.", ROOMNUM(rp), facenum);
2235 else
2236 EditorStatus("Face %d flipped.", facenum);
2237 #endif
2238 }
2239
2240 // Attempt to fix the cracks in a level
2241 void FixCracks() {
2242 int r, p, f, f2, v, v2;
2243 room *rp;
2244 face *fp, *fp2;
2245 int possible_tjoints = 0, tjoints_fixed = 0;
2246 int points_added = 0;
2247 int points_are_same_count = 0;
2248
2249 if (OutrageMessageBox(MBOX_YESNO, "This function is somewhat dangerous.\n\n"
2250 "You should be sure to save your level beforehand, and you should check the "
2251 "results very carefully afterwards.\n\n"
2252 "Continue?") != IDYES)
2253 return;
2254
2255 // First fix gaps at portals
2256 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
2257 if (rp->used) {
2258 for (p = 0; p < rp->num_portals; p++) {
2259 portal *pp = &rp->portals[p];
2260 points_added +=
2261 MatchPortalFaces(rp, pp->portal_face, &Rooms[pp->croom], Rooms[pp->croom].portals[pp->cportal].portal_face);
2262 }
2263 }
2264 }
2265
2266 // Now search for and fix t-joints
2267 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
2268 if (rp->used)
2269 for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
2270 // if (fp->portal_num == -1)
2271 { // don't check portal faces
2272 recheck_face:;
2273 for (v = 0; v < fp->num_verts; v++) {
2274 int vv0 = fp->face_verts[v], vv1 = fp->face_verts[(v + 1) % fp->num_verts];
2275 for (f2 = 0, fp2 = rp->faces; f2 < rp->num_faces; f2++, fp2++) {
2276 if (f2 != f) {
2277 for (v2 = 0; v2 < fp2->num_verts; v2++) {
2278 int tt0 = fp2->face_verts[v2], tt1 = fp2->face_verts[(v2 + 1) % fp2->num_verts];
2279
2280 if ((vv0 == tt1) && (vv1 == tt0))
2281 break; // found one, so stop
2282 }
2283 if (v2 < fp2->num_verts)
2284 break;
2285 }
2286 }
2287 if (f2 == rp->num_faces) { // didn't find a match
2288 // mprintf(0,"Couldn't find shared edge for %d:%d edge %d\n",r,f,v);
2289 possible_tjoints++;
2290
2291 // Look for vert on this edge
2292 for (f2 = 0, fp2 = rp->faces; f2 < rp->num_faces; f2++, fp2++) {
2293 if (f2 != f) {
2294 for (v2 = 0; v2 < fp2->num_verts; v2++) {
2295 int tt0 = fp2->face_verts[v2], tt1 = fp2->face_verts[(v2 + 1) % fp2->num_verts];
2296
2297 if (vv0 == tt1) { // one point maches; check if next is on the edge
2298 // if (PointsAreColinear(&rp->verts[vv0],&rp->verts[tt0],&rp->verts[vv1])) {
2299
2300 if (PointsAreSame(&rp->verts[tt0], &rp->verts[vv0]))
2301 points_are_same_count++;
2302 if (PointsAreSame(&rp->verts[tt0], &rp->verts[vv1]))
2303 points_are_same_count++;
2304
2305 if (CheckPointToPlane(&rp->verts[tt0], &rp->verts[vv0], &fp->normal) == 0) {
2306 if (CheckPointAgainstEdge(&rp->verts[tt0], &rp->verts[vv0], &rp->verts[vv1], &fp->normal) ==
2307 0) {
2308 // make sure the new point is actually between the two edge points
2309 float edge_len = vm_VectorDistance(&rp->verts[vv1], &rp->verts[vv0]);
2310 float d0 = vm_VectorDistance(&rp->verts[tt0], &rp->verts[vv0]);
2311 float d1 = vm_VectorDistance(&rp->verts[tt0], &rp->verts[vv1]);
2312 if ((d0 < edge_len) && (d1 < edge_len)) {
2313 mprintf(0, "Adding %d to edge %d of %d:%d (from face %d)\n", tt0, v, r, f, f2);
2314 AddVertToFace(rp, f, tt0, v);
2315 tjoints_fixed++;
2316 goto recheck_face;
2317 }
2318 }
2319 }
2320 }
2321 }
2322 }
2323 }
2324 }
2325 }
2326 }
2327 }
2328 }
2329
2330 // Fix portals again because points may have been added when fixing t-joints
2331 for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
2332 if (rp->used) {
2333 for (p = 0; p < rp->num_portals; p++) {
2334 portal *pp = &rp->portals[p];
2335 points_added +=
2336 MatchPortalFaces(rp, pp->portal_face, &Rooms[pp->croom], Rooms[pp->croom].portals[pp->cportal].portal_face);
2337 }
2338 }
2339 }
2340
2341 mprintf(0, "%d possible t-joints\n", possible_tjoints);
2342 mprintf(0, "points_are_same_count = %d\n", points_are_same_count);
2343
2344 OutrageMessageBox("Level fix results:\n\n"
2345 " %d points added to portals\n"
2346 " %d T-joints fixed",
2347 points_added, tjoints_fixed);
2348
2349 World_changed = 1;
2350 }
2351
2352 // Variables for undoing a snap
2353 int Snap_roomnum = -1;
2354 int Snap_vertnum;
2355 vector Snap_old_point, Snap_new_point;
2356
2357 // Moves a vertex to lie on a specified edge
2358 void SnapPointToEdge(room *rp, int vertnum, vector *v0, vector *v1) {
2359 vector *vp = &rp->verts[vertnum];
2360 vector edge_vec, edge_normal;
2361 float d;
2362
2363 // Save info for undo
2364 Snap_roomnum = ROOMNUM(rp);
2365 Snap_vertnum = vertnum;
2366 Snap_old_point = *vp;
2367
2368 // Calculate new point
2369 edge_normal = edge_vec = *v1 - *v0;
2370 vm_NormalizeVector(&edge_normal);
2371 d = vm_DistToPlane(vp, &edge_normal, v0);
2372 *vp = *v0 + edge_normal * d;
2373
2374 // Set the point
2375 Snap_new_point = *vp;
2376
2377 // Done
2378 EditorStatus("Room %d point %d snapped.", Snap_roomnum, Snap_vertnum);
2379 World_changed = 1;
2380 }
2381
2382 // Moves a vertex to be conincident with another vertex
2383 void SnapPointToPoint(room *rp, int vertnum, room *snapto_rp, int snapto_vertnum) {
2384 vector *vp = &rp->verts[vertnum];
2385
2386 // Save info for undo
2387 Snap_roomnum = ROOMNUM(rp);
2388 Snap_vertnum = vertnum;
2389 Snap_old_point = *vp;
2390
2391 *vp = snapto_rp->verts[snapto_vertnum];
2392
2393 // Set the point
2394 Snap_new_point = *vp;
2395
2396 // Done
2397 EditorStatus("Room %d point %d snapped.", Snap_roomnum, Snap_vertnum);
2398 World_changed = 1;
2399 }
2400
2401 // Moves a vertex to lie on a specified plane
2402 void SnapPointToFace(room *rp, int vertnum, vector *v0, vector *normal) {
2403 vector *vp = &rp->verts[vertnum];
2404 float d;
2405
2406 // Save info for undo
2407 Snap_roomnum = ROOMNUM(rp);
2408 Snap_vertnum = vertnum;
2409 Snap_old_point = *vp;
2410
2411 // Calculate new point
2412 d = vm_DistToPlane(vp, normal, v0);
2413 *vp -= *normal * d;
2414
2415 // Set the point
2416 Snap_new_point = *vp;
2417
2418 // Done
2419 EditorStatus("Room %d point %d snapped.", Snap_roomnum, Snap_vertnum);
2420 World_changed = 1;
2421 }
2422
2423 // Undoes the most recently-performed snap
2424 void UndoSnap() {
2425 if (Snap_roomnum == -1)
2426 return;
2427
2428 if ((Snap_roomnum <= Highest_room_index) && Rooms[Snap_roomnum].used) {
2429 vector *vp = &Rooms[Snap_roomnum].verts[Snap_vertnum];
2430
2431 if ((vp->x == Snap_new_point.x) && (vp->y == Snap_new_point.y) && (vp->z == Snap_new_point.z)) {
2432 *vp = Snap_old_point;
2433 World_changed = 1;
2434 return;
2435 }
2436 }
2437
2438 OutrageMessageBox("Cannot Undo: The snapped point has changed since the snap.");
2439 Snap_roomnum = -1;
2440 }
2441
2442 // Gets the angle between two vectors in the range 0..1
2443 // Parameters: base - the start of both vectors
2444 // v0,v1 - the end points of the two vectors
2445 // normal - the surface normal of the face these verts lie in
2446 // Returns the cosine of the angle between <base,v0> and <base,v1>
2447 float GetAngle(vector *base, vector *v0, vector *v1, vector *normal) {
2448 vector edge0, edge1, cross;
2449 float dot;
2450
2451 vm_GetNormalizedDir(&edge0, v0, base);
2452 vm_GetNormalizedDir(&edge1, v1, base);
2453
2454 dot = edge0 * edge1;
2455 cross = edge0 ^ edge1;
2456
2457 if (cross * *normal > 0)
2458 return 0.25 - dot / 4.0;
2459 else
2460 return 0.75 + dot / 4.0;
2461 }
2462
2463 // Connects two rooms in a pleasing way
2464 void BuildSmoothBridge(room *rp0, int facenum0, room *rp1, int facenum1) {
2465 room *rp;
2466 int nv0, nv1, nverts, nfaces;
2467 int i;
2468 vector tverts[MAX_VERTS_PER_FACE];
2469
2470 // Get values from current faces
2471 face *fp0 = &rp0->faces[facenum0];
2472 face *fp1 = &rp1->faces[facenum1];
2473 nv0 = fp0->num_verts;
2474 nv1 = fp1->num_verts;
2475
2476 // Check for portal already here
2477 if (fp0->portal_num != -1) {
2478 OutrageMessageBox("Cannot build bridge: There is already a connection at %d:%d.", ROOMNUM(rp0), facenum0);
2479 return;
2480 }
2481 if (fp1->portal_num != -1) {
2482 OutrageMessageBox("Cannot build bridge: There is already a connection at %d:%d.", ROOMNUM(rp1), facenum1);
2483 return;
2484 }
2485
2486 // Get values for new room
2487 nverts = nv0 + nv1;
2488 nfaces = nverts + 2;
2489
2490 // Get a pointer to our room
2491 rp = CreateNewRoom(nverts, nfaces);
2492 if (rp == NULL) {
2493 OutrageMessageBox("Cannot build bridge: No free rooms.");
2494 return;
2495 }
2496
2497 // Set the vertices for the room
2498 for (i = 0; i < nv0; i++)
2499 rp->verts[i] = rp0->verts[fp0->face_verts[i]];
2500 for (i = 0; i < nv1; i++)
2501 rp->verts[nv0 + i] = rp1->verts[fp1->face_verts[nv1 - 1 - i]];
2502
2503 // Create the connecting faces
2504
2505 // Compute normalized vector from attach face to base face
2506 vector c0, c1, delta_vec;
2507 #ifndef NEWEDITOR
2508 ComputeCenterPointOnFace(&c0, rp0, facenum0);
2509 ComputeCenterPointOnFace(&c1, rp1, facenum1);
2510 #else
2511 ComputeFaceBoundingCircle(&c0, rp0, facenum0);
2512 ComputeFaceBoundingCircle(&c1, rp1, facenum1);
2513 #endif
2514 vm_GetNormalizedDir(&delta_vec, &c1, &c0);
2515
2516 // Compute the cosine of the angle between our vector and the normal of the base face
2517 float cos = delta_vec * fp1->normal;
2518
2519 // Get a pointer to a vertex on face1
2520 vector *vp = &rp->verts[nv0];
2521
2522 // Project the verts from end face0 into end face1
2523 for (i = 0; i < nv0; i++) {
2524
2525 // Get distance from base face to this vertex
2526 float dist = ((*vp - rp->verts[i]) * fp1->normal) / cos;
2527
2528 // Get the new vert
2529 tverts[i] = rp->verts[i] + (delta_vec * dist);
2530 }
2531
2532 // Find closest face0 point to point 0 of face1
2533 int smallest_i = -1;
2534 float smallest_angle = 1.0;
2535 vector tnorm = -fp1->normal;
2536 for (i = 0; i < nv0; i++) {
2537 float angle = GetAngle(&c1, &rp->verts[nv0], &tverts[i], &tnorm);
2538 if (angle > 0.5)
2539 angle = 1.0 - angle; // we want closest in either direction
2540 if (angle < smallest_angle) {
2541 smallest_i = i;
2542 smallest_angle = angle;
2543 }
2544 }
2545 ASSERT(smallest_i != -1);
2546
2547 // Generate new faces
2548 int prev1 = 0, // set up the previous edge
2549 prev0 = smallest_i;
2550 for (i = 0; i < nverts; i++) {
2551 float angle0, angle1;
2552
2553 face *fp = &rp->faces[i];
2554
2555 InitRoomFace(fp, 3);
2556
2557 fp->face_verts[0] = nv0 + prev1;
2558 fp->face_verts[1] = prev0;
2559
2560 if (i == nverts - 1) { // special case for last edge
2561 if ((rp->faces[0].face_verts[0] != fp->face_verts[0]) && (rp->faces[0].face_verts[0] != fp->face_verts[1]))
2562 fp->face_verts[2] = rp->faces[0].face_verts[0];
2563 else {
2564 ASSERT((rp->faces[0].face_verts[1] != fp->face_verts[0]) && (rp->faces[0].face_verts[0] != fp->face_verts[1]));
2565 fp->face_verts[2] = rp->faces[0].face_verts[1];
2566 }
2567 break;
2568 }
2569
2570 // Pick point for third vert
2571 angle1 = GetAngle(&c1, &rp->verts[nv0 + prev1], &rp->verts[nv0 + (prev1 + 1) % nv1], &tnorm);
2572 angle0 = GetAngle(&c1, &rp->verts[nv0 + prev1], &tverts[(prev0 + 1) % nv0], &tnorm);
2573
2574 if (angle1 < angle0) {
2575 prev1 = (prev1 + 1) % nv1;
2576 fp->face_verts[2] = nv0 + prev1;
2577 } else
2578 prev0 = fp->face_verts[2] = (prev0 + 1) % nv0;
2579 }
2580
2581 // Set the two end faces for the room
2582 InitRoomFace(&rp->faces[nfaces - 2], fp0->num_verts);
2583 for (i = 0; i < fp0->num_verts; i++)
2584 rp->faces[nfaces - 2].face_verts[i] = nv0 - 1 - i;
2585
2586 InitRoomFace(&rp->faces[nfaces - 1], fp1->num_verts);
2587 for (i = 0; i < fp1->num_verts; i++)
2588 rp->faces[nfaces - 1].face_verts[i] = fp0->num_verts + i;
2589
2590 // Set normals & textures for each face
2591 for (i = 0; i < nfaces; i++) {
2592 if (!ComputeFaceNormal(rp, i))
2593 Int3(); // Bad! Get Matt!
2594 }
2595
2596 // Attempt to combine faces
2597 for (i = 0; i < nfaces - 2; i++) {
2598 int t = (i + 1) % (nfaces - 2);
2599
2600 // Try compbining the faces
2601 if (CombineFaces(rp, i, t)) {
2602 i--; // check this face again
2603 nfaces--;
2604 }
2605 }
2606
2607 // Set textures for each face
2608 #ifndef NEWEDITOR
2609 for (i = 0; i < nfaces; i++)
2610 rp->faces[i].tmap = i + 1;
2611 #else
2612 int texnum = Editor_state.GetCurrentTexture();
2613 for (i = 0; i < nfaces; i++) {
2614 // Apply texture
2615 HTextureApplyToRoomFace(rp, i, texnum);
2616 }
2617 #endif
2618
2619 // Set UVs for all the faces
2620 AssignDefaultUVsToRoom(rp);
2621
2622 // Form the portals between the rooms
2623 LinkRoomsSimple(Rooms, ROOMNUM(rp0), facenum0, ROOMNUM(rp), nfaces - 2);
2624 LinkRoomsSimple(Rooms, ROOMNUM(rp1), facenum1, ROOMNUM(rp), nfaces - 1);
2625
2626 // Make the new room the current room
2627 #ifndef NEWEDITOR
2628 Curroomp = rp;
2629 Curface = nfaces - 2;
2630 Curedge = Curvert = 0;
2631 Curportal = -1;
2632 #else
2633 theApp.m_pLevelWnd->SetPrim(rp, nfaces - 2, -1, 0, 0);
2634 #endif
2635
2636 // Set the flag
2637 World_changed = 1;
2638 }
2639
2640 int16_t New_face_verts[MAX_VERTS_PER_FACE];
2641 int New_face_num_verts = -1;
2642 room *New_face_roomp;
2643
2644 void AddNewFaceVert() {
2645 if (Curroomp != New_face_roomp) {
2646 OutrageMessageBox("All points in the new face must be in the same room");
2647 return;
2648 }
2649
2650 if (New_face_num_verts == -1) {
2651 OutrageMessageBox("You must start a new face before adding to it");
2652 return;
2653 }
2654
2655 New_face_verts[New_face_num_verts++] = Curroomp->faces[Curface].face_verts[Curvert];
2656
2657 EditorStatus("Added %d as vert #%d in new face", Curroomp->faces[Curface].face_verts[Curvert], New_face_num_verts);
2658 }
2659
2660 void StartNewFace() {
2661 New_face_roomp = Curroomp;
2662 New_face_num_verts = 0;
2663
2664 AddNewFaceVert();
2665
2666 EditorStatus("Starting new face with vertex %d", Curroomp->faces[Curface].face_verts[Curvert]);
2667 }
2668
2669 void EndNewFace() {
2670 AddNewFaceVert();
2671
2672 if (New_face_num_verts == -1) {
2673 OutrageMessageBox("You must start a new face before ending it");
2674 return;
2675 }
2676
2677 if (New_face_num_verts < 3) {
2678 OutrageMessageBox("A face must have at least three vertices");
2679 return;
2680 }
2681
2682 int facenum;
2683
2684 facenum = RoomAddFaces(Curroomp, 1);
2685
2686 face *fp = &Curroomp->faces[facenum];
2687
2688 InitRoomFace(fp, New_face_num_verts);
2689
2690 for (int i = 0; i < New_face_num_verts; i++)
2691 fp->face_verts[i] = New_face_verts[i];
2692
2693 #ifndef NEWEDITOR
2694 fp->tmap = 0;
2695 #else
2696 int texnum = Editor_state.GetCurrentTexture();
2697 // Apply texture
2698 HTextureApplyToRoomFace(Curroomp, facenum, texnum);
2699 #endif
2700
2701 ComputeFaceNormal(Curroomp, facenum);
2702 AssignDefaultUVsToRoomFace(Curroomp, facenum);
2703
2704 EditorStatus("New face %d created", facenum);
2705
2706 New_face_num_verts = -1;
2707
2708 World_changed = 1;
2709 }
2710
2711 void ObjRelink(int objnum, int newroomnum);
2712 int RemoveDuplicatePoints(room *rp);
2713 trigger *FindTrigger(int roomnum, int facenum);
2714
2715 // Combine the two rooms. base_rp stays, att_rp goes away
2716 // Returns true if combine sucessful, false if can't join
2717 bool CombineRooms(room *base_rp, room *att_rp) {
2718 int i;
2719 face *fp;
2720 bool connected = 0, rendered_portals = 0;
2721
2722 // Make sure these rooms share a portal
2723 for (i = 0; i < base_rp->num_portals; i++) {
2724 if (base_rp->portals[i].croom == ROOMNUM(att_rp)) {
2725 connected = 1;
2726 if ((base_rp->faces[base_rp->portals[i].portal_face].flags & FF_HAS_TRIGGER) ||
2727 (att_rp->faces[att_rp->portals[base_rp->portals[i].cportal].portal_face].flags & FF_HAS_TRIGGER)) {
2728 OutrageMessageBox("Cannot combine rooms: a portal connecting them has a trigger.");
2729 return 0;
2730 }
2731 if ((base_rp->portals[i].flags & PF_RENDER_FACES) ||
2732 (att_rp->portals[base_rp->portals[i].cportal].flags & PF_RENDER_FACES)) {
2733 rendered_portals = 1;
2734 }
2735 }
2736 }
2737
2738 if (!connected) {
2739 OutrageMessageBox("Cannot combine rooms: the rooms are not connected.");
2740 return 0;
2741 }
2742
2743 if (rendered_portals) {
2744 if (OutrageMessageBox(
2745 MBOX_YESNO,
2746 "One or more of the portals connecting these rooms has the Render Faces flag set. "
2747 "The rendered face will remain in the room after combine.\n\nDo you still want to combine?") != IDYES)
2748 return 0;
2749 }
2750
2751 // Check for doors
2752 if (base_rp->doorway_data || att_rp->doorway_data) {
2753 OutrageMessageBox("Cannot combine rooms: one room is a door.");
2754 return 0;
2755 }
2756
2757 // Allocate new verts & faces
2758 int first_new_vert = RoomAddVertices(base_rp, att_rp->num_verts);
2759 int first_new_face = RoomAddFaces(base_rp, att_rp->num_faces);
2760
2761 // Copy verts to base room
2762 for (i = 0; i < att_rp->num_verts; i++)
2763 base_rp->verts[first_new_vert + i] = att_rp->verts[i];
2764
2765 // Copy faces to base room
2766 for (i = 0, fp = &base_rp->faces[first_new_face]; i < att_rp->num_faces; fp++, i++) {
2767
2768 // Copy the face
2769 CopyFace(fp, &att_rp->faces[i]);
2770 fp->flags = att_rp->faces[i].flags;
2771
2772 // Update the vertices
2773 for (int v = 0; v < fp->num_verts; v++)
2774 fp->face_verts[v] += first_new_vert;
2775
2776 // Move triggers from attach to base room
2777 if (att_rp->faces[i].flags & FF_HAS_TRIGGER) {
2778 trigger *tp = FindTrigger(ROOMNUM(att_rp), i);
2779 tp->roomnum = ROOMNUM(base_rp);
2780 tp->facenum += first_new_face;
2781 }
2782 }
2783
2784 // Deal with attach room's portals
2785 for (i = 0; i < att_rp->num_portals; i++) {
2786 int facenum, croom, cface, flags, cflags;
2787
2788 facenum = att_rp->portals[i].portal_face + first_new_face;
2789 flags = att_rp->portals[i].flags;
2790 croom = att_rp->portals[i].croom;
2791 cface = Rooms[croom].portals[att_rp->portals[i].cportal].portal_face;
2792 cflags = Rooms[croom].portals[att_rp->portals[i].cportal].flags;
2793
2794 // Delete old portal from attach room
2795 DeletePortalPair(att_rp, i);
2796
2797 // If this portal attaches to the base room, mark the portal faces for deletion
2798 if (croom == ROOMNUM(base_rp)) {
2799 if (!(flags & PF_RENDER_FACES))
2800 base_rp->faces[facenum].tmap = -1; //-1 means to delete the face
2801 if (!(cflags & PF_RENDER_FACES))
2802 base_rp->faces[cface].tmap = -1; //-1 means to delete the face
2803 } else { // Attaches to third room, so make attachment to base room
2804 LinkRooms(Rooms, ROOMNUM(base_rp), facenum, croom, cface);
2805 portal *pp = &base_rp->portals[base_rp->num_portals - 1];
2806 pp->flags |= (flags & (PF_RENDER_FACES + PF_RENDERED_FLYTHROUGH));
2807 Rooms[pp->croom].portals[pp->cportal].flags |= (cflags * (PF_RENDER_FACES + PF_RENDERED_FLYTHROUGH));
2808 }
2809
2810 i--; // adjust for new portal numbering
2811 }
2812
2813 // Delete obsolete faces
2814 for (i = 0; i < base_rp->num_faces; i++)
2815 if (base_rp->faces[i].tmap == -1) {
2816 DeleteRoomFace(base_rp, i);
2817 i--;
2818 }
2819
2820 // Link objects to new room
2821 while (att_rp->objects != -1)
2822 ObjRelink(att_rp->objects, ROOMNUM(base_rp));
2823
2824 // Delete the old room
2825 DeleteRoomFromMine(att_rp);
2826
2827 // Get rid of duplicate verts
2828 RemoveDuplicatePoints(base_rp);
2829
2830 // Done!
2831 World_changed = 1;
2832 return 1;
2833 }
2834
2835 // Creates a an external room and links the specified faces to it
2836 // Parameters: rp - the room to connect to the new room
2837 // nfaces - how many faces connect to the new room (this becomes the number of
2838 //portals) facenums - the list of faces to connect
2839 void LinkToExternalRoom(room *rp, int nfaces, int *facenums) {
2840 int vert_xlate[MAX_VERTS_PER_ROOM];
2841 int v, i, nverts = 0;
2842
2843 for (i = 0; i < rp->num_verts; i++)
2844 vert_xlate[i] = -1;
2845
2846 for (i = 0; i < nfaces; i++) {
2847 face *fp = &rp->faces[facenums[i]];
2848
2849 for (v = 0; v < fp->num_verts; v++) {
2850 int vertnum = fp->face_verts[v];
2851 if (vert_xlate[vertnum] == -1)
2852 vert_xlate[vertnum] = nverts++;
2853 }
2854 }
2855
2856 room *newrp = CreateNewRoom(nverts, nfaces);
2857
2858 newrp->flags |= RF_EXTERNAL;
2859
2860 for (i = 0; i < rp->num_verts; i++) {
2861 if (vert_xlate[i] != -1)
2862 newrp->verts[vert_xlate[i]] = rp->verts[i];
2863 }
2864
2865 for (i = 0; i < nfaces; i++) {
2866 face *newfp = &newrp->faces[i], *fp = &rp->faces[facenums[i]];
2867
2868 InitRoomFace(newfp, fp->num_verts);
2869
2870 for (v = 0; v < fp->num_verts; v++)
2871 newfp->face_verts[v] = vert_xlate[fp->face_verts[fp->num_verts - v - 1]];
2872
2873 #ifndef NEWEDITOR
2874 newfp->tmap = fp->tmap;
2875 #else
2876 // Apply texture
2877 HTextureApplyToRoomFace(newrp, i, newfp->tmap);
2878 #endif
2879
2880 if (!ComputeFaceNormal(newrp, i))
2881 Int3();
2882
2883 LinkRooms(Rooms, ROOMNUM(rp), facenums[i], ROOMNUM(newrp), i);
2884 }
2885
2886 AssignDefaultUVsToRoom(newrp);
2887
2888 World_changed = 1;
2889 }
2890
2891 extern bool Disable_editor_rendering;
2892
2893 // Splits a face into two pieces
2894 // Parameters: rp,facenum - the face to split
2895 // v0,v1 - the two verts that will form the new edge
2896 void SplitFace(room *rp, int facenum, int v0, int v1) {
2897 face *fp = &rp->faces[facenum], *nfp;
2898 int first_new_face, i;
2899
2900 if (v0 == v1) {
2901 OutrageMessageBox("You cannot split a face using the same point twice.");
2902 return;
2903 }
2904
2905 if (v1 < v0) {
2906 int t = v1;
2907 v1 = v0;
2908 v0 = t;
2909 }
2910
2911 if (v1 == (v0 + 1) % fp->num_verts) {
2912 OutrageMessageBox("You cannot split a face using two adjacent points.");
2913 return;
2914 }
2915
2916 if (fp->num_verts == 3) {
2917 OutrageMessageBox("Face %d has three verts and cannot be split.", facenum);
2918 return;
2919 }
2920
2921 if (fp->portal_num != -1) {
2922 OutrageMessageBox("You cannot split a face that's part of a portal.");
2923 return;
2924 }
2925
2926 if (fp->flags & (FF_HAS_TRIGGER + FF_FLOATING_TRIG)) {
2927 OutrageMessageBox("You cannot split a face that has a trigger.");
2928 return;
2929 }
2930
2931 Disable_editor_rendering = 1;
2932
2933 // Add new faces
2934 first_new_face = RoomAddFaces(rp, 2);
2935
2936 // Reset pointer to our old face
2937 fp = &rp->faces[facenum];
2938
2939 // Set up each face
2940 bool ok[2];
2941 for (i = 0, nfp = &rp->faces[first_new_face]; i < 2; i++, nfp++) {
2942 int nv, startv;
2943
2944 if (i == 0) {
2945 nv = v1 - v0 + 1;
2946 startv = v0;
2947 } else {
2948 nv = fp->num_verts - (v1 - v0) + 1;
2949 startv = v1;
2950 }
2951
2952 InitRoomFace(nfp, nv);
2953
2954 // Set verts & uvls for this face
2955 for (int v = 0; v < nv; v++) {
2956 nfp->face_verts[v] = fp->face_verts[(startv + v) % fp->num_verts];
2957 nfp->face_uvls[v] = fp->face_uvls[(startv + v) % fp->num_verts];
2958 }
2959
2960 // Copy other data for this face
2961 CopyFaceFlags(nfp, fp);
2962 #ifndef NEWEDITOR
2963 nfp->tmap = fp->tmap;
2964 #else
2965 // Apply texture
2966 HTextureApplyToRoomFace(rp, first_new_face + i, fp->tmap);
2967 #endif
2968 nfp->light_multiple = fp->light_multiple;
2969
2970 // Compute the normal for this face
2971 ok[i] = ComputeFaceNormal(rp, first_new_face + i);
2972 }
2973
2974 // Bail if either face is degenerate
2975 if (!ok[0] || !ok[1]) {
2976 OutrageMessageBox("Cannot form face here: one new face is degenerate.");
2977 DeleteRoomFace(rp, first_new_face);
2978 DeleteRoomFace(rp, first_new_face + 1);
2979 Disable_editor_rendering = 0;
2980 return;
2981 }
2982
2983 // Delete original face
2984 DeleteRoomFace(rp, facenum);
2985
2986 // We're done
2987 EditorStatus("Face %d replaced by new faces %d & %d", facenum, first_new_face - 1, first_new_face);
2988
2989 Disable_editor_rendering = 0;
2990
2991 #ifndef NEWEDITOR
2992 World_changed = 1;
2993 #endif
2994 }
2995
2996 // Adds a point to an edge
2997 void SplitEdge(room *rp, int facenum, int edgenum, float position) {
2998 face *fp = &rp->faces[facenum];
2999 int vert1 = (edgenum + 1) % fp->num_verts;
3000 vector *v0, *v1, newv;
3001
3002 v0 = &rp->verts[fp->face_verts[edgenum]];
3003 v1 = &rp->verts[fp->face_verts[vert1]];
3004
3005 newv = *v0 + (*v1 - *v0) * position;
3006
3007 AddPointToAllEdges(rp, fp->face_verts[edgenum], fp->face_verts[vert1], &newv);
3008
3009 World_changed = 1;
3010 }
3011
3012 #define MIN_NORMAL_MAG 0.035
3013
3014 int FindConnectedFace(room *rp, int facenum, int edgenum, int startface);
3015
3016 // Deletes a vertex from an face
3017 // Only deletes if the two adjacent edges are colinear, and there isn't a T-joint at this point
3018 void DeleteVert(room *rp, int facenum, int vertnum) {
3019 face *fp = &rp->faces[facenum];
3020 vector normal;
3021 float mag;
3022 int f0, f1;
3023 int prev_vertnum = (vertnum + fp->num_verts - 1) % fp->num_verts;
3024
3025 mag = vm_GetNormal(&normal, &rp->verts[fp->face_verts[prev_vertnum]], &rp->verts[fp->face_verts[vertnum]],
3026 &rp->verts[fp->face_verts[(vertnum + 1) % fp->num_verts]]);
3027
3028 if (mag >= MIN_NORMAL_MAG) {
3029 OutrageMessageBox("Can't remove point: edges aren't colinear.");
3030 return;
3031 }
3032
3033 // Make sure there isn't a t-joint at this face
3034 f0 = FindConnectedFace(rp, facenum, prev_vertnum, 0);
3035 f1 = FindConnectedFace(rp, facenum, vertnum, 0);
3036 if ((f0 != -1) && (f1 != -1) && (f0 != f1)) {
3037 OutrageMessageBox("Can't delete vertex: apparent T-joint with faces %d & %d\n", f0, f1);
3038 return;
3039 }
3040
3041 // Delete the point on our passed face
3042 DeletePointFromFace(rp, facenum, vertnum);
3043
3044 // Delete the point on the connected face
3045 face *fp2 = &rp->faces[f0];
3046 for (int v2 = 0; v2 < fp2->num_verts; v2++)
3047 if (fp2->face_verts[v2] == fp->face_verts[vertnum])
3048 break;
3049 ASSERT(v2 < fp2->num_verts);
3050 DeletePointFromFace(rp, f0, v2);
3051
3052 #ifndef NEWEDITOR
3053 World_changed = 1;
3054 #endif
3055 }
3056
3057 // Moves a vertex along its two adjacent edges
3058 // Only moves if the two adjacent edges are colinear
3059 void MoveVert(room *rp, int facenum, int vertnum, float new_position) {
3060 face *fp = &rp->faces[facenum];
3061 vector normal;
3062 float mag;
3063 int prev_vertnum = (vertnum + fp->num_verts - 1) % fp->num_verts;
3064 int next_vertnum = (vertnum + 1) % fp->num_verts;
3065 vector *v0, *v1, newv;
3066
3067 mag = vm_GetNormal(&normal, &rp->verts[fp->face_verts[prev_vertnum]], &rp->verts[fp->face_verts[vertnum]],
3068 &rp->verts[fp->face_verts[next_vertnum]]);
3069
3070 if (mag >= MIN_NORMAL_MAG) {
3071 OutrageMessageBox("Can't move point: edges aren't colinear.");
3072 return;
3073 }
3074
3075 v0 = &rp->verts[fp->face_verts[prev_vertnum]];
3076 v1 = &rp->verts[fp->face_verts[next_vertnum]];
3077
3078 newv = *v0 + (*v1 - *v0) * new_position;
3079
3080 rp->verts[fp->face_verts[vertnum]] = newv;
3081
3082 World_changed = 1;
3083 }
3084
3085 #include "polymodel.h"
3086
3087 // Incorporates the geometry from the specified object into the specified room. Deletes the object.
3088 // Returns true if worked, false if some error
3089 bool MergeObjectIntoRoom(room *rp, int objnum) {
3090 object *objp = &Objects[objnum];
3091 ASSERT(objp->render_type == RT_POLYOBJ);
3092
3093 poly_model *pm = GetPolymodelPointer(objp->rtype.pobj_info.model_num);
3094
3095 if (pm->n_models > 1) {
3096 #ifndef NEWEDITOR
3097 OutrageMessageBox("Cannot merge: Only supported for objects with one submodel.\n\nIf you really need this "
3098 "functions for objects with multiple submodles, talk to MattT.");
3099 #else
3100 OutrageMessageBox("Cannot merge: Only supported for objects with one submodel.");
3101 #endif
3102 return 0;
3103 }
3104 if (!pm->n_ground) {
3105 OutrageMessageBox("Cannot merge: The object must have a ground plane to merge.");
3106 return 0;
3107 }
3108 if (OBJECT_OUTSIDE(objp)) {
3109 OutrageMessageBox("Cannot merge: The object is outside.");
3110 return 0;
3111 }
3112 if (objp->roomnum != ROOMNUM(rp)) {
3113 OutrageMessageBox("Cannot merge: The object is not in the specified room.");
3114 return 0;
3115 }
3116
3117 // Get points to model data
3118 bsp_info *sm = &pm->submodel[0];
3119
3120 // Add verts to room
3121 int first_new_vert = RoomAddVertices(rp, sm->nverts);
3122
3123 // Rotate the points and copy them to the room
3124 matrix rotmat = ~objp->orient;
3125 for (int i = 0; i < sm->nverts; i++) {
3126 rp->verts[first_new_vert + i] = objp->pos + sm->verts[i] * rotmat;
3127 }
3128
3129 // Add faces to the room
3130 int first_new_face = RoomAddFaces(rp, sm->num_faces);
3131
3132 // Copy the faces
3133 for (i = 0; i < sm->num_faces; i++) {
3134 polyface *pfp = &sm->faces[i];
3135 face *fp = &rp->faces[first_new_face + i];
3136
3137 InitRoomFace(fp, pfp->nverts);
3138
3139 for (int v = 0; v < fp->num_verts; v++) {
3140 fp->face_verts[v] = first_new_vert + pfp->vertnums[v];
3141 fp->face_uvls[v].u = pfp->u[v];
3142 fp->face_uvls[v].v = pfp->v[v];
3143 }
3144
3145 #ifndef NEWEDITOR
3146 fp->tmap = pm->textures[pfp->texnum];
3147 #else
3148 // Apply texture
3149 HTextureApplyToRoomFace(rp, first_new_face + i, pm->textures[pfp->texnum]);
3150 #endif
3151
3152 if (!ComputeFaceNormal(rp, first_new_face + i))
3153 Int3(); // Bad! Get Matt!
3154 }
3155
3156 // Kill the object
3157 ObjDelete(objnum);
3158
3159 // Done!
3160 World_changed = 1;
3161 return 1;
3162 }
3163
3164 void MarkFaceForDeletion(room *rp, int facenum) {
3165 face *fp = &rp->faces[facenum];
3166
3167 // Mark this face
3168 fp->tmap = -1;
3169
3170 // Find connect faces
3171 for (int e = 0; e < fp->num_verts; e++) {
3172 int t = -1;
3173
3174 while ((t = FindConnectedFace(rp, facenum, e, t + 1)) != -1) {
3175 if (rp->faces[t].tmap != -1)
3176 MarkFaceForDeletion(rp, t);
3177 }
3178 }
3179 }
3180
3181 // Delete all the faces connected to the specified face
3182 void DeleteAllConnectedFaces(room *rp, int facenum) {
3183 // Make sure no faces using tmap of -1
3184 for (int f = 0; f < rp->num_faces; f++)
3185 ASSERT(rp->faces[f].tmap != -1);
3186
3187 // Do recursive marking
3188 MarkFaceForDeletion(rp, facenum);
3189
3190 // Now delete the faces
3191 for (f = 0; f < rp->num_faces;) {
3192
3193 if (rp->faces[f].tmap == -1)
3194 DeleteRoomFace(rp, f);
3195 else
3196 f++;
3197 }
3198
3199 // Done
3200 World_changed = 1;
3201 }
3202
3203 // returns the number of faces changed
3204 int PropagateToConnectedFacesSub(room *rp, int facenum, bool *face_checked) {
3205 int n_changed = 0;
3206
3207 face_checked[facenum] = 1;
3208
3209 face *fp = &rp->faces[facenum];
3210
3211 for (int e = 0; e < fp->num_verts; e++) {
3212 int t = -1;
3213
3214 do {
3215
3216 t = FindConnectedFace(rp, facenum, e, t + 1);
3217
3218 if ((t != -1) && !face_checked[t]) {
3219
3220 if (NormalsAreSame(&fp->normal, &rp->faces[t].normal)) {
3221
3222 HTexturePropagateToFace(rp, t, rp, facenum);
3223
3224 n_changed++;
3225
3226 n_changed += PropagateToConnectedFacesSub(rp, t, face_checked);
3227 }
3228 }
3229
3230 } while (t != -1);
3231 }
3232
3233 return n_changed;
3234 }
3235
3236 void PropagateToConnectedFaces(room *rp, int facenum) {
3237 bool face_checked[MAX_FACES_PER_ROOM];
3238 int f, n_changed;
3239
3240 for (f = 0; f < rp->num_faces; f++)
3241 face_checked[f] = 0;
3242
3243 face_checked[facenum] = 1;
3244
3245 n_changed = PropagateToConnectedFacesSub(rp, facenum, face_checked);
3246
3247 EditorStatus("%d faces were retextured.", n_changed);
3248
3249 if (n_changed)
3250 World_changed = 1;
3251 }