Debian Codegraph @

editor/Group.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/Group.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Functions for copying, pasting, etc. of groups
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 35 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 * 34 8/30/99 1:01p Gwar
34 * use the "true" center of faces for determining place position of a
35 * group
36 *
37 * 33 8/17/99 6:12p Gwar
38 * handle NEWEDITOR generic object management in AttachGroup
39 *
40 * 32 8/17/99 6:46a Gwar
41 * added group support to NEWEDITOR
42 *
43 * 31 5/08/99 1:39a Matt
44 * Added a function to delete all objects of a certain type, and support
45 * for placing and attaching groups to the terrain.
46 *
47 * 30 4/21/99 10:19p Matt
48 * DeleteGroup() wasn't setting the world changed flag
49 *
50 * 29 3/15/99 1:29p Matt
51 * Added objects & triggers to save/load for groups.
52 *
53 * 28 1/29/99 12:48p Matt
54 * Rewrote the doorway system
55 *
56 * 27 1/26/99 6:02p Matt
57 * Fixed (I hope) a problem with pasted object having the wrong
58 * orientation.
59 *
60 * 26 1/21/99 11:15p Jeff
61 * pulled out some structs and defines from header files and moved them
62 * into seperate header files so that multiplayer dlls don't require major
63 * game headers, just those new headers. Side effect is a shorter build
64 * time. Also cleaned up some header file #includes that weren't needed.
65 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
66 * manage.h and multi.h
67 *
68 * 25 1/21/99 11:34a Matt
69 * Got rid of portal triggers. Since we don't have multi-face portals, a
70 * face trigger works fine for a portal. Also fixed a few editor/trigger
71 * bugs.
72 *
73 * 24 1/14/99 11:06a Matt
74 * Added names to triggers
75 *
76 * 23 1/08/99 2:56p Samir
77 * Ripped out OSIRIS1.
78 *
79 * 22 12/01/98 11:17p Matt
80 * Made copy/paste work with doors.
81 *
82 * 21 10/08/98 4:23p Kevin
83 * Changed code to comply with memory library usage. Always use mem_malloc
84 * , mem_free and mem_strdup
85 *
86 * 20 9/24/98 5:00p Matt
87 * Improved error checking for running out of rooms.
88 *
89 * 19 9/02/98 5:57p Matt
90 * Added code to make faces match exactly when forming a portal. This
91 * should get rid of all remaining cracking problems.
92 *
93 * 18 9/01/98 12:04p Matt
94 * Ripped out multi-face portal code
95 *
96 * 17 8/15/98 10:50p Matt
97 * When copying a portal, copy its flags
98 *
99 * 16 6/23/98 2:43p Matt
100 * Changed calls to OutrageMessageBox() & Debug_MessageBox() to deal with
101 * int return value (instead of bool).
102 *
103 * 15 4/21/98 2:43p Matt
104 * Added system to disable render updates while deleting large numbers of
105 * rooms, since the mine is not stable until this operation is done.
106 *
107 * 14 2/09/98 1:38p Matt
108 * When pasting a group, ask the user if he also wants to paste objects
109 * and triggers
110 *
111 * 13 2/04/98 6:23p Matt
112 * Changed object room number to indicate a terrain cell via a flag. Got
113 * rid of the object flag which used to indicate terrain.
114 *
115 * 12 1/29/98 5:50p Matt
116 * Changed old camera object type to be viewer object (for editor), and
117 * now camera objects will just be for game cameras.
118 *
119 * 11 1/20/98 4:12p Samir
120 * New script housekeeping system.
121 *
122 * 10 1/15/98 7:34p Matt
123 * Revamped error checking when computing face normals
124 *
125 * 9 12/23/97 11:06a Samir
126 * Added pserror.h
127 *
128 * 8 10/13/97 6:20p Matt
129 * Fixed stupid bug copying objects in group
130 *
131 * 7 10/03/97 3:37p Matt
132 * Fixed bug copying multiple objects in a room, and changed code to not
133 * copy viewer or player objects
134 *
135 * 6 9/24/97 6:18p Samir
136 * Use script names instead of script id values to identify scripts.
137 *
138 * 5 9/22/97 2:31p Matt
139 * Made group code copy & paste objects & triggers
140 *
141 * 4 9/08/97 4:05p Samir
142 * Fixed some warnings and took out extranneous includes to windows.h in
143 * important headers.
144 *
145 * 3 9/02/97 6:42p Matt
146 * Got paste & group save/load working
147 *
148 * 2 8/29/97 5:45p Matt
149 * Converted group code to work with rooms (unfinished)
150 *
151 * 11 6/16/97 6:40p Matt
152 * Fixed bug caused when Selected_segments[] was changed while
153 * DeleteGroup() was indexing through it.
154 *
155 * 10 6/12/97 7:17p Matt
156 * Deleted now-unused RotateWorld() function
157 *
158 * 9 5/23/97 5:46p Matt
159 * Added SetMatrixBasedOnSide() functionality to ExtractMatrixFromSeg().
160 *
161 * 8 5/13/97 11:58a Matt
162 * Made group file code save & load the level file version number to use
163 * when reading segments
164 *
165 * 7 5/08/97 5:07p Matt
166 * When rotating world, rotate the editor's wireframe window view vars
167 *
168 * 6 5/08/97 4:43p Matt
169 * Fixed bug when rotating objects in RotateWorld()
170 *
171 * 5 5/06/97 2:26p Jason
172 * fixed bug in rotate world
173 *
174 * 4 5/05/97 3:57p Matt
175 * Added code to rotate the mine (& the objects in it) to join with the
176 * terrain.
177 *
178 * 3 3/31/97 5:58p Matt
179 * Revamped mine update flags
180 *
181 * 2 3/12/97 3:25p Matt
182 * Added funcs for cut, copy, paste, & delete, and to save and load
183 * groups.
184 *
185 * 1 3/11/97 9:34p Matt
186 *
187 * $NoKeywords: $
188 */
189
190 #include "group.h"
191
192 #ifndef NEWEDITOR
193 #include "d3edit.h"
194 #else
195 #include "globals.h"
196 #endif
197
198 #include "selectedroom.h"
199 #include "room.h"
200 #include "hroom.h"
201 #include "erooms.h"
202 #include "loadlevel.h"
203
204 #ifndef NEWEDITOR
205 #include "moveworld.h"
206 #endif
207
208 #include "object.h"
209 #include "trigger.h"
210 #include "pserror.h"
211 #include "doorway.h"
212 #include "mem.h"
213 #include <string.h>
214 #include "door.h"
215
216 // Free a group.
217 // Parameters: g - the group to be freed
218 void FreeGroup(group *g) {
219 for (int r = 0; r < g->nrooms; r++)
220 FreeRoom(&g->rooms[r]);
221
222 mem_free(g->rooms);
223
224 if (g->nobjects)
225 mem_free(g->objects);
226
227 if (g->ntriggers)
228 mem_free(g->triggers);
229
230 mem_free(g);
231 }
232
233 // Copy the given list of rooms to a group
234 // Parameters: nrooms - the number of rooms in list
235 // roomnums - pointer to list of room numbers
236 // attachroom, attachface - where group attaches when pasted
237 // Returns: pointer to group
238 group *CopyGroup(int nrooms, int *roomnums, int attachroom, int attachface) {
239 group *g;
240 int r;
241 int room_xlate[MAX_ROOMS];
242 int n_objects = 0, n_triggers = 0, n_doors = 0;
243
244 // Allocate group & room arrays
245 g = (group *)mem_malloc(sizeof(*g));
246 ASSERT(g != NULL);
247 g->nrooms = nrooms;
248 g->rooms = (room *)mem_malloc(sizeof(*g->rooms) * nrooms);
249 ASSERT(g->rooms != NULL);
250
251 // Initialize xlate list
252 for (r = 0; r < MAX_ROOMS; r++)
253 room_xlate[r] = -1;
254
255 // Copy the rooms & build xlate list. Count objects while we're at it
256 for (r = 0; r < nrooms; r++) {
257
258 CopyRoom(&g->rooms[r], &Rooms[roomnums[r]]);
259 room_xlate[roomnums[r]] = r;
260
261 for (int o = Rooms[roomnums[r]].objects; o != -1; o = Objects[o].next)
262 if ((Objects[o].type != OBJ_VIEWER) && (Objects[o].type != OBJ_CAMERA) && (Objects[o].type != OBJ_PLAYER)) {
263 n_objects++;
264 if (Objects[o].type == OBJ_DOOR)
265 n_doors++;
266 }
267 }
268
269 // Add portals for rooms in the group
270 for (r = 0; r < nrooms; r++)
271 for (int p = 0; p < Rooms[roomnums[r]].num_portals; p++) {
272 portal *pp = &Rooms[roomnums[r]].portals[p];
273 if ((pp->croom != -1) && IsRoomSelected(pp->croom)) {
274
275 // link the two rooms
276 if (r < room_xlate[pp->croom]) // only link lower to higher, so we don't double link
277 LinkRoomsSimple(g->rooms, r, pp->portal_face, room_xlate[pp->croom],
278 Rooms[pp->croom].portals[pp->cportal].portal_face);
279
280 // Copy the portal flags
281 int new_portal_num = g->rooms[r].faces[pp->portal_face].portal_num;
282 g->rooms[r].portals[new_portal_num].flags = pp->flags;
283 }
284 }
285
286 // Fill in attach fields
287 g->attachroom = room_xlate[attachroom];
288 g->attachface = attachface;
289
290 // Copy objects
291 g->nobjects = n_objects;
292 g->ndoors = n_doors;
293 if (n_objects == 0)
294 g->objects = NULL;
295 else {
296 int objnum = 0;
297
298 // Get memory for objects
299 g->objects = (object *)mem_malloc(sizeof(*g->objects) * n_objects);
300
301 // Go through list of rooms & copy the objects
302 for (r = 0; r < nrooms; r++) {
303 for (int o = Rooms[roomnums[r]].objects; o != -1; o = Objects[o].next) {
304 object *dest = &g->objects[objnum], *src = &Objects[o];
305
306 if ((src->type == OBJ_VIEWER) || (src->type == OBJ_CAMERA) || (src->type == OBJ_PLAYER))
307 continue; // don't copy cameras
308
309 // Copy some selected data
310 dest->type = src->type;
311 dest->id = src->id;
312 dest->flags = src->flags;
313 dest->pos = src->pos;
314 dest->orient = src->orient;
315 dest->lifeleft = src->lifeleft;
316
317 dest->contains_type = src->contains_type;
318 dest->contains_id = src->contains_id;
319 dest->contains_count = src->contains_count;
320
321 if (src->control_type == CT_POWERUP)
322 dest->ctype.powerup_info.count = src->ctype.powerup_info.count;
323
324 if (src->type == OBJ_DOOR)
325 dest->shields = src->shields;
326
327 dest->roomnum = r;
328
329 objnum++;
330 }
331 }
332 }
333
334 // Count the triggers in the group
335 for (int t = 0; t < Num_triggers; t++)
336 if (room_xlate[Triggers[t].roomnum] != -1)
337 n_triggers++;
338
339 // Copy triggers
340 g->ntriggers = n_triggers;
341 if (n_triggers == 0)
342 g->triggers = NULL;
343 else {
344 int trignum = 0;
345
346 // Get memory for triggers
347 g->triggers = (trigger *)mem_malloc(sizeof(*g->triggers) * n_triggers);
348
349 for (t = 0; t < Num_triggers; t++)
350 if (room_xlate[Triggers[t].roomnum] != -1) {
351 g->triggers[trignum] = Triggers[t];
352 g->triggers[trignum].roomnum = room_xlate[Triggers[t].roomnum];
353 trignum++;
354 }
355 }
356
357 // Return the new group
358 return g;
359 }
360
361 extern bool Disable_editor_rendering;
362
363 // Delete the given list of rooms
364 // Parameters: nrooms - the number of rooms in list
365 // roomnums - pointer to list of room numbers
366 void DeleteGroup(int nrooms, int *roomnums) {
367 int i;
368 int *troomnums;
369 bool save_disable_flag = Disable_editor_rendering;
370
371 Disable_editor_rendering = 1;
372
373 // Make copy of list of rooms to delete. We must do this because roomnums may be a pointer
374 // to the global Selected_list, the contents of which may be changed by DeleteRoomFromMine()
375 troomnums = (int *)mem_malloc(sizeof(*troomnums) * nrooms);
376 for (i = 0; i < nrooms; i++)
377 troomnums[i] = roomnums[i];
378
379 // Now delete the rooms
380 for (i = 0; i < nrooms; i++)
381 DeleteRoomFromMine(&Rooms[troomnums[i]]);
382
383 // Free up our array
384 mem_free(troomnums);
385
386 // Regenerate external object rooms
387 CreateRoomObjects();
388
389 // Restore old flag
390 Disable_editor_rendering = save_disable_flag;
391
392 // We're done! Update flags and leave
393 World_changed = 1;
394 }
395
396 // Place the given group at the specified room face
397 // The function merely causes the group to be drawn in the editor, allowing the user to line it up
398 // before attaching it. The function AttachGroup() must be called to do the actual attachment.
399 // Parameters: destroomp, destside - where to place the group
400 // g - the group to place
401 void PlaceGroup(room *destroomp, int destface, group *g) {
402 // Clear the placed room, if one exists
403 Placed_room = -1;
404
405 ASSERT(destroomp->faces[destface].portal_num == -1);
406
407 // Set globals
408 Placed_group = g;
409 Placed_room_orient.fvec = destroomp->faces[destface].normal;
410 Placed_room_angle = 0;
411 Placed_baseroomp = destroomp;
412 Placed_baseface = destface;
413
414 // Compute attach points on each face
415 #ifndef NEWEDITOR
416 ComputeCenterPointOnFace(&Placed_room_attachpoint, destroomp, destface);
417 ComputeCenterPointOnFace(&Placed_room_origin, &g->rooms[g->attachroom], g->attachface);
418 #else
419 float ComputeFaceBoundingCircle(vector * center, room * rp, int facenum);
420 ComputeFaceBoundingCircle(&Placed_room_attachpoint, destroomp, destface);
421 ComputeFaceBoundingCircle(&Placed_room_origin, &g->rooms[g->attachroom], g->attachface);
422 #endif
423
424 // Compute initial orientation matrix
425 ComputePlacedRoomMatrix();
426
427 // Set the flag
428 State_changed = 1;
429 }
430
431 #include "terrain.h"
432
433 // Place the given group at the specified terrain cell
434 // The function merely causes the group to be drawn in the editor, allowing the user to line it up
435 // before attaching it. The function AttachGroup() must be called to do the actual attachment.
436 // Parameters: cellnum - where to place the group
437 // g - the group to place
438 void PlaceGroupTerrain(int cellnum, group *g, bool align_to_terrain) {
439 // Clear the placed room, if one exists
440 Placed_room = -1;
441
442 // Set globals
443 Placed_group = g;
444 Placed_room_angle = 0;
445 Placed_baseroomp = NULL;
446
447 if (align_to_terrain)
448 Placed_room_orient.fvec = TerrainNormals[MAX_TERRAIN_LOD - 1][cellnum].normal1;
449 else
450 Placed_room_orient.fvec = Identity_matrix.uvec;
451
452 // Compute attach point on terrain
453 ComputeTerrainSegmentCenter(&Placed_room_attachpoint, cellnum);
454
455 // Compute attach points on face
456 ComputeCenterPointOnFace(&Placed_room_origin, &g->rooms[g->attachroom], g->attachface);
457
458 // Compute initial orientation matrix
459 ComputePlacedRoomMatrix();
460
461 // Set the flag
462 State_changed = 1;
463 }
464
465 // Attach the already-placed group
466 void AttachGroup() {
467 vector basecenter, attcenter;
468 room *baseroomp, *attroomp;
469 int baseface, attface;
470 int room_xlate[MAX_ROOMS];
471 int r;
472
473 ASSERT(Placed_group != NULL);
474
475 // Set some vars
476 baseroomp = Placed_baseroomp;
477 baseface = Placed_baseface;
478 attroomp = &Placed_group->rooms[Placed_group->attachroom];
479 attface = Placed_group->attachface;
480 attcenter = Placed_room_origin;
481 basecenter = Placed_room_attachpoint;
482
483 // Copy the rooms into the main room list, build the xlate table, and rotate the verts
484 for (r = 0; r < Placed_group->nrooms; r++) {
485 int roomnum;
486 room *newroomp;
487
488 // Get a new room & copy into it
489 roomnum = GetFreeRoom(0);
490 if (roomnum == -1) {
491 OutrageMessageBox("Cannot attach group: Not enough free rooms.");
492 for (int t = 0; t < r; t++) // delete the rooms we just created
493 DeleteRoomFromMine(&Rooms[room_xlate[t]]);
494 return;
495 }
496 newroomp = &Rooms[roomnum];
497 CopyRoom(newroomp, &Placed_group->rooms[r]);
498 room_xlate[r] = roomnum;
499
500 // Rotate verts in the new rooms
501 for (int i = 0; i < newroomp->num_verts; i++)
502 newroomp->verts[i] = ((newroomp->verts[i] - attcenter) * Placed_room_rotmat) + basecenter;
503
504 // Recompute normals for the faces
505 if (!ResetRoomFaceNormals(newroomp))
506 Int3(); // Get Matt
507 }
508
509 // Add portals for new rooms
510 for (r = 0; r < Placed_group->nrooms; r++)
511 for (int p = 0; p < Placed_group->rooms[r].num_portals; p++) {
512 portal *pp = &Placed_group->rooms[r].portals[p];
513 if (r < pp->croom) // only link lower to higher, so we don't double link
514 LinkRoomsSimple(Rooms, room_xlate[r], pp->portal_face, room_xlate[pp->croom],
515 Placed_group->rooms[pp->croom].portals[pp->cportal].portal_face);
516
517 // Copy the portal flags
518 int new_portal_num = Rooms[room_xlate[r]].faces[pp->portal_face].portal_num;
519 Rooms[room_xlate[r]].portals[new_portal_num].flags = pp->flags;
520 }
521
522 // Get pointer to the placed attachroom
523 room *newroomp = &Rooms[room_xlate[Placed_group->attachroom]];
524
525 if (baseroomp) {
526 // Clip the connecting faces against each other
527 if (!ClipFacePair(newroomp, attface, baseroomp, baseface)) {
528 OutrageMessageBox("Error making portal -- faces probably don't overlap.");
529 for (r = 0; r < Placed_group->nrooms; r++)
530 FreeRoom(&Rooms[room_xlate[r]]);
531 return;
532 }
533
534 // Make the two faces match exactly
535 MatchPortalFaces(baseroomp, baseface, newroomp, attface);
536
537 // Link the attach room to the base room
538 LinkRoomsSimple(Rooms, ROOMNUM(baseroomp), baseface, ROOMNUM(newroomp), attface);
539 }
540
541 // Paste objects
542 if (Placed_group->nobjects) {
543 bool place_all = 0;
544
545 // Ask if the user wants the objects pasted
546 if (Placed_group->nobjects > Placed_group->ndoors)
547 place_all =
548 (OutrageMessageBox(MBOX_YESNO,
549 "The group you are pasting has %d (non-door) object(s). Do you wish to paste it/them?",
550 Placed_group->nobjects - Placed_group->ndoors) == IDYES);
551
552 // Add the objects (either just doors or all objects)
553 for (int o = 0; o < Placed_group->nobjects; o++) {
554 object *src = &Placed_group->objects[o], *dest;
555
556 if (place_all || (src->type == OBJ_DOOR)) {
557 vector pos;
558 matrix orient;
559 int objnum;
560
561 pos = ((src->pos - attcenter) * Placed_room_rotmat) + basecenter;
562 orient = ~Placed_room_rotmat * src->orient;
563
564 objnum = ObjCreate(src->type, src->id, room_xlate[src->roomnum], &pos, &orient);
565
566 #ifdef NEWEDITOR
567 if (IS_GENERIC(src->type))
568 LevelObjIncrementObject(src->id);
569 #endif
570
571 dest = &Objects[objnum];
572
573 dest->flags = src->flags;
574 dest->lifeleft = src->lifeleft;
575
576 dest->contains_type = src->contains_type;
577 dest->contains_id = src->contains_id;
578 dest->contains_count = src->contains_count;
579
580 if (dest->control_type == CT_POWERUP)
581 dest->ctype.powerup_info.count = src->ctype.powerup_info.count;
582
583 if (src->type == OBJ_DOOR)
584 dest->shields = src->shields;
585 }
586 }
587 }
588
589 // Paste triggers
590 if (Placed_group->ntriggers) {
591
592 // Ask if the user wants the triggers
593 if (OutrageMessageBox(MBOX_YESNO, "The group you are pasting has %d trigger(s). Do you wish to paste it/them?",
594 Placed_group->ntriggers) == IDYES) {
595
596 // Add the triggers
597 for (int t = 0; t < Placed_group->ntriggers; t++) {
598 trigger *tp = &Placed_group->triggers[t];
599 int trignum;
600
601 trignum = AddTrigger("", room_xlate[tp->roomnum], tp->facenum, tp->activator, NULL);
602 ASSERT(trignum != -1);
603
604 Triggers[trignum].flags = tp->flags;
605 }
606 } else { // Triggers not being pasted, so must delete any floating trigger faces
607 for (r = 0; r < Placed_group->nrooms; r++) {
608 room *rp = &Rooms[room_xlate[r]];
609 for (int f = 0; f < rp->num_faces; f++) {
610 if (rp->faces[f].flags & FF_FLOATING_TRIG)
611 DeleteRoomFace(rp, f);
612 }
613 }
614 }
615 }
616
617 // Un-place the now-attached group
618 Placed_group = NULL;
619
620 // We're done! Update flags and leave
621 World_changed = 1;
622 }
623
624 #define GROUP_FILE_TAG "D3GP"
625 #define GROUP_FILE_VERSION 3
626 // Version number changes:
627 // 0 -> 1 Save the level file version to use when reading objects, rooms, etc.
628 // 1 -> 2 Now works for room engine
629 // 2 -> 3 Save objects, doors, & triggers
630
631 #define cf_ReadVector(f, v) \
632 do { \
633 (v)->x = cf_ReadFloat(f); \
634 (v)->y = cf_ReadFloat(f); \
635 (v)->z = cf_ReadFloat(f); \
636 } while (0)
637 #define cf_WriteVector(f, v) \
638 do { \
639 cf_WriteFloat((f), (v)->x); \
640 cf_WriteFloat((f), (v)->y); \
641 cf_WriteFloat((f), (v)->z); \
642 } while (0)
643 #define cf_WriteMatrix(f, m) \
644 do { \
645 cf_WriteVector((f), &(m)->rvec); \
646 cf_WriteVector((f), &(m)->uvec); \
647 cf_WriteVector((f), &(m)->fvec); \
648 } while (0)
649 #define cf_ReadMatrix(f, m) \
650 do { \
651 cf_ReadVector((f), &(m)->rvec); \
652 cf_ReadVector((f), &(m)->uvec); \
653 cf_ReadVector((f), &(m)->fvec); \
654 } while (0)
655
656 // Saves an object for a group
657 void WriteGroupObject(CFILE *ofile, object *objp) {
658 cf_WriteByte(ofile, objp->type);
659 cf_WriteShort(ofile, objp->id);
660 cf_WriteInt(ofile, objp->flags);
661
662 if (objp->type == OBJ_DOOR)
663 cf_WriteShort(ofile, objp->shields);
664
665 cf_WriteInt(ofile, objp->roomnum);
666 cf_WriteVector(ofile, &objp->pos);
667 cf_WriteMatrix(ofile, &objp->orient);
668
669 cf_WriteByte(ofile, objp->contains_type);
670 cf_WriteByte(ofile, objp->contains_id);
671 cf_WriteByte(ofile, objp->contains_count);
672
673 cf_WriteFloat(ofile, objp->lifeleft);
674 }
675
676 void WriteGroupTrigger(CFILE *ofile, trigger *tp) {
677 cf_WriteShort(ofile, tp->roomnum);
678 cf_WriteShort(ofile, tp->facenum);
679 cf_WriteShort(ofile, tp->flags);
680 cf_WriteShort(ofile, tp->activator);
681 }
682
683 // Saves a group to disk in the given filename
684 void SaveGroup(char *filename, group *g) {
685 CFILE *ifile;
686 int i;
687
688 ifile = cfopen(filename, "wb");
689
690 if (!ifile)
691 return;
692
693 // Write tag & version number
694 cf_WriteBytes((uint8_t *)GROUP_FILE_TAG, 4, ifile);
695 cf_WriteInt(ifile, GROUP_FILE_VERSION);
696 cf_WriteInt(ifile, LEVEL_FILE_VERSION);
697
698 // Write the current list of textures
699 WriteTextureList(ifile);
700
701 // Write group info
702 cf_WriteInt(ifile, g->nrooms);
703 cf_WriteInt(ifile, g->attachroom);
704 cf_WriteInt(ifile, g->attachface);
705 cf_WriteInt(ifile, g->nobjects);
706 cf_WriteInt(ifile, g->ndoors);
707 cf_WriteInt(ifile, g->ntriggers);
708
709 // Write rooms
710 for (i = 0; i < g->nrooms; i++)
711 WriteRoom(ifile, &g->rooms[i]);
712
713 // Write objects
714 for (i = 0; i < g->nobjects; i++)
715 WriteGroupObject(ifile, &g->objects[i]);
716
717 // Write triggers
718 for (i = 0; i < g->ntriggers; i++)
719 WriteGroupTrigger(ifile, &g->triggers[i]);
720
721 // Close the file
722 cfclose(ifile);
723 }
724
725 // Reads an object for a group
726 void ReadGroupObject(CFILE *ifile, object *objp) {
727 objp->type = cf_ReadByte(ifile);
728 objp->id = cf_ReadShort(ifile);
729 objp->flags = cf_ReadInt(ifile);
730
731 if (objp->type == OBJ_DOOR)
732 objp->shields = cf_ReadShort(ifile);
733
734 objp->roomnum = cf_ReadInt(ifile);
735 cf_ReadVector(ifile, &objp->pos);
736 cf_ReadMatrix(ifile, &objp->orient);
737
738 objp->contains_type = cf_ReadByte(ifile);
739 objp->contains_id = cf_ReadByte(ifile);
740 objp->contains_count = cf_ReadByte(ifile);
741
742 objp->lifeleft = cf_ReadFloat(ifile);
743 }
744
745 void ReadGroupTrigger(CFILE *ifile, trigger *tp) {
746 tp->roomnum = cf_ReadShort(ifile);
747 tp->facenum = cf_ReadShort(ifile);
748 tp->flags = cf_ReadShort(ifile);
749 tp->activator = cf_ReadShort(ifile);
750 }
751
752 // Loads a group from disk
753 group *LoadGroup(char *filename) {
754 CFILE *ifile;
755 char tag[4];
756 int version, level_version;
757 group *g;
758 int i;
759
760 ifile = cfopen(filename, "rb");
761
762 if (!ifile)
763 return NULL;
764
765 // Read & check tag
766 cf_ReadBytes((uint8_t *)tag, 4, ifile);
767 if (strncmp(tag, GROUP_FILE_TAG, 4)) {
768 cfclose(ifile);
769 return NULL;
770 }
771
772 // Read & check version number
773 version = cf_ReadInt(ifile);
774 if (version > GROUP_FILE_VERSION) {
775 cfclose(ifile);
776 return NULL;
777 }
778
779 // Check for out-of-date groups
780 if (version < 2) {
781 OutrageMessageBox("Sorry - segment groups no longer supported.");
782 cfclose(ifile);
783 return NULL;
784 }
785
786 // Read & check level file version number
787 level_version = cf_ReadInt(ifile);
788 if (level_version > LEVEL_FILE_VERSION) {
789 cfclose(ifile);
790 return NULL;
791 }
792
793 // Read the textures & build xlate table
794 ReadTextureList(ifile);
795
796 // Allocate group
797 g = (group *)mem_malloc(sizeof(*g));
798
799 // Read group info
800 g->nrooms = cf_ReadInt(ifile);
801 g->attachroom = cf_ReadInt(ifile);
802 g->attachface = cf_ReadInt(ifile);
803
804 // Read object, door, & trigger info
805 if (version >= 3) {
806 g->nobjects = cf_ReadInt(ifile);
807 g->ndoors = cf_ReadInt(ifile);
808 g->ntriggers = cf_ReadInt(ifile);
809 } else
810 g->nobjects = g->ndoors = g->ntriggers = 0;
811
812 // Allocate room and vertex arrays
813 g->rooms = (room *)mem_malloc(sizeof(*g->rooms) * g->nrooms);
814
815 // Read rooms
816 for (i = 0; i < g->nrooms; i++) {
817 g->rooms[i].used = 0; // ReadRoom() wants the used flag to be clear
818 ReadRoom(ifile, &g->rooms[i], level_version);
819 }
820
821 // Read objects & triggers
822 g->triggers = NULL;
823 g->objects = NULL;
824 if (version >= 3) {
825
826 // Allocate objects array
827 if (g->nobjects)
828 g->objects = (object *)mem_malloc(sizeof(*g->objects) * g->nobjects);
829
830 // Allocate triggers array
831 if (g->ntriggers)
832 g->triggers = (trigger *)mem_malloc(sizeof(*g->triggers) * g->ntriggers);
833
834 // Read objects
835 for (i = 0; i < g->nobjects; i++)
836 ReadGroupObject(ifile, &g->objects[i]);
837
838 // Read triggers
839 for (i = 0; i < g->ntriggers; i++)
840 ReadGroupTrigger(ifile, &g->triggers[i]);
841 }
842
843 // Close the file
844 cfclose(ifile);
845
846 return g;
847 }