Debian Codegraph @

editor/HObject.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/HObject.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Editor object handling functions
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 41 4/18/99 5:42a Chris
30 * Added the FQ_IGNORE_RENDER_THROUGH_PORTALS flag
31 *
32 * 40 3/27/99 12:51p Chris
33 * The editor object placement code now allows objects to be pushed into
34 * wall (when a toggle box is checked)
35 *
36 * 39 1/15/99 7:52p Chris
37 * Updated ObjSetPos() to include a f_update_attach_children flag
38 *
39 * 38 10/15/98 3:26p Chris
40 * Fixed known ground plane issues and used PhysCalcGround everywhere
41 *
42 * 37 10/07/98 12:28p Matt
43 * Fixed stupid bug that could cause access violation placing objects on
44 * the terrain.
45 *
46 * 36 8/24/98 2:20p Matt
47 * Fixed bug placing objects on external rooms.
48 *
49 * 35 8/20/98 7:28p Matt
50 * Added a warning when the user tries to delete a door object.
51 *
52 * 34 6/17/98 12:30p Samir
53 * don't draw wireframe when moving an object.
54 *
55 * 33 5/18/98 2:56p Matt
56 * Added code to readjust all ground object (for after the terrain has
57 * moved).
58 *
59 * 32 5/11/98 11:37a Sean
60 *
61 * 31 4/02/98 3:54p Jason
62 * first pass in getting polymodel paging to work
63 *
64 * 30 3/12/98 7:30p Chris
65 * Added ObjSetOrient
66 *
67 * 29 3/12/98 4:35p Chris
68 * Powerups are always aligned to the ground
69 *
70 * 28 2/20/98 5:30p Matt
71 * Don't allow physics objects to be placed if they collide with walls,
72 * and don't allow them to move into walls. Non-physics objects can be
73 * placed & moved into walls as long as their center points stay inside
74 * the mine.
75 *
76 * 27 2/13/98 6:50p Matt
77 * Fixed stupid bug that prevented placing of player ship 0
78 *
79 * 26 2/08/98 6:02p Matt
80 * Use groovy new vm_MatrixMulTMatrix() function
81 *
82 * 25 2/06/98 5:41p Matt
83 * Fixed object placement on surfaces.
84 *
85 * 24 2/06/98 3:08p Samir
86 * Simplified object movement code.
87 *
88 * 23 2/06/98 1:22p Matt
89 * Rewrote object placement code. Still has bugs when ground plane normal
90 * is not 0,1,0
91 *
92 * 22 2/05/98 2:59p Matt
93 * Made object placement on terrain work like in mine
94 *
95 * 21 2/04/98 7:07p Matt
96 * Fixed problem placing an object on the terrain.
97 *
98 * 20 2/04/98 6:23p Matt
99 * Changed object room number to indicate a terrain cell via a flag. Got
100 * rid of the object flag which used to indicate terrain.
101 *
102 * 19 1/07/98 6:39p Jason
103 * Fixed player object number stuff
104 *
105 * 18 1/07/98 10:45a Samir
106 * Don't allow moving objects from mine to terrain.
107 *
108 * 17 11/17/97 6:55p Matt
109 * Made HObjectMoveToViewer() work when the viewer is outside
110 *
111 * 16 9/17/97 11:35a Samir
112 * BIG SEGMENT RIPOUT
113 *
114 * 15 9/15/97 11:55a Samir
115 * Fixed playership adding.
116 *
117 * 14 9/05/97 8:54p Chris
118 * Added some ground plane support
119 *
120 * 13 9/03/97 8:00p Samir
121 * Implemented most of the fixes for the object moving system.
122 *
123 * 12 8/25/97 2:50p Chris
124 * Improve AABB/object movement code.
125 *
126 * 11 8/21/97 7:48p Matt
127 * Added code to move player object (like old move player to curseg)
128 *
129 * 10 8/20/97 4:23p Matt
130 * Fixed little bug
131 *
132 * 9 8/12/97 10:24p Matt
133 * Added code to place clutter & building objects
134 *
135 * 8 8/11/97 1:53p Matt
136 * Ripped out robot & powerup pages, and added generic page
137 *
138 * 7 8/04/97 3:18p Matt
139 * Added FVI checking to object placement & movement in rooms
140 *
141 * 6 8/04/97 12:47p Matt
142 * Added object placement & movement for rooms
143 *
144 * 5 8/01/97 5:33p Jason
145 * Fixed object deletion bug in terrain
146 *
147 * 4 7/22/97 7:07p Matt
148 * Cleaned up D3EditState, moving some vars in and some out, and renaming
149 * and changing a few others
150 *
151 * 3 7/17/97 4:34p Chris
152 * Made sure the orientations get orthagonalized. Yikes. No more
153 * degenerating orientations. :)
154 *
155 * 20 6/27/97 7:19p Matt
156 * Added function to move player 0 to current segment
157 *
158 * 19 5/23/97 5:46p Matt
159 * Added SetMatrixBasedOnSide() functionality to ExtractMatrixFromSeg().
160 *
161 * 18 5/13/97 5:52p Chris
162 * Added ability to exit and enter mine. Also did some
163 * incremental improvements.
164 *
165 * 17 4/07/97 3:14p Samir
166 * Added Flip and SetDefault Object functions.
167 *
168 * 16 4/04/97 2:57p Matt
169 * Added code to initialize all the type-specific data for an object from
170 * the page for that object type. This means that we need to pass less
171 * info to ObjCreate(), and that we save less info in the level save file.
172 * It also makes it easy to reset all the objects when an object page has
173 * changed.
174 *
175 * 15 4/02/97 8:08p Matt
176 * Changed object movement code to work either in the object's or the
177 * viewer's frame of reference, and not in the segment's frame of
178 * reference.
179 *
180 * 14 4/02/97 3:44p Matt
181 * Find a valid Ships[] entry, instead of assuming that Ships[0] is valid.
182 *
183 * 13 4/01/97 10:52p Matt
184 * Added code to add a player object
185 *
186 * 12 3/31/97 5:58p Matt
187 * Revamped mine update flags
188 *
189 * 11 3/27/97 11:54a Samir
190 * UpdateMineObjects returns number of objects deleted of a type.
191 *
192 * 10 3/27/97 11:48a Samir
193 * Added update objects in mine functions.
194 *
195 * 9 3/27/97 10:47a Chris
196 * Incremental
197 *
198 * 8 3/25/97 6:31p Samir
199 * Added robot placement.
200 *
201 * 7 3/21/97 5:01p Jason
202 * incremental terrain improvments
203 *
204 *
205 * 6 3/10/97 12:44p Chris
206 * Allow objects to be put against a wall
207 *
208 * 5 3/03/97 5:57a Chris
209 * Uncommented the code samir had waiting for fvi and fixed a function
210 * call.
211 *
212 * 4 2/19/97 3:37p Samir
213 * Added delete and rotation of objects.
214 *
215 * 3 2/17/97 7:19p Jason
216 * fixed powerup size arg
217 *
218 * 2 2/17/97 6:18p Samir
219 * Add powerup, move it around WITHIN the segment.
220 *
221 * 1 2/17/97 3:17p Samir
222 * Handler for Object manipulation functions.
223 *
224 * $NoKeywords: $
225 */
226
227 #include "HObject.h"
228 #include "object.h"
229 #include "FindIntersection.h"
230 #include "physics.h"
231 #include "boa.h"
232 #include "polymodel.h"
233
234 #include "d3edit.h"
235 #include "mono.h"
236 #include "vclip.h"
237 #include "terrain.h"
238 #include "player.h"
239 #include "ship.h"
240 #include "erooms.h"
241
242 // ---------------------------------------------------------------------------
243 // internal data
244
245 float Object_move_scale = HOBJECT_SCALE_UNIT;
246 angle Object_move_rotation = HOBJECT_ROTATION_UNIT;
247
248 // ---------------------------------------------------------------------------
249 // internal function prototypes
250
251 bool RotateObject(int objnum, angle p, angle h, angle b);
252 bool MoveObject(object *obj, vector *newpos);
253
254 // ---------------------------------------------------------------------------
255 // interface functions
256
257 #define OBJECT_PLACE_DIST 10.0
258
259 // ---------------------------------------------------------------------------
260
261 // Finds the seleced terrain cell. Returns cell number, or -1 if none or -2 if more than one
262 int GetSelectedTerrainCell() {
263 int i, found_cellnum = -1;
264
265 for (i = 0; i < TERRAIN_DEPTH * TERRAIN_WIDTH; i++) {
266 if (TerrainSelected[i])
267 if (found_cellnum == -1)
268 found_cellnum = i;
269 else
270 return -2;
271 }
272
273 return found_cellnum;
274 }
275
276 // Places an object in the world
277 // Parameters: obj_type,obj_id - specify the object to be placed
278 // Returns: true if object placed, false if there was some problem
279 bool HObjectPlace(int obj_type, int obj_id) {
280 int objnum;
281 object *objp;
282 poly_model *pm;
283 matrix orient = IDENTITY_MATRIX;
284
285 // Special stuff for player ship
286 if (obj_type == OBJ_PLAYER) {
287
288 if (!Num_ships) {
289 OutrageMessageBox("Cannot a player: There are no player ships.");
290 return 0;
291 }
292
293 // Store ship num in player array
294 int ship_num = D3EditState.current_ship;
295
296 if (ship_num == -1) {
297 OutrageMessageBox("You must have a current player ship selected for this operation.");
298 return 0;
299 }
300
301 Players[obj_id].ship_index = ship_num;
302 }
303
304 if (obj_type != OBJ_POWERUP) {
305 orient = Viewer_object->orient;
306 }
307
308 // Create the object at the viewer for now
309 objnum = ObjCreate(obj_type, obj_id, Viewer_object->roomnum, &Viewer_object->pos, &orient);
310 if (objnum == -1)
311 return 0;
312
313 objp = &Objects[objnum];
314
315 // If we have a ground plane, use current cell or face for position & place normal for orientation
316 if ((objp->render_type == RT_POLYOBJ) && ((pm = GetPolymodelPointer(objp->rtype.pobj_info.model_num))->n_ground)) {
317 vector *surface_norm;
318 vector pos;
319 int roomnum;
320
321 // If terrain, use current cell
322 if (Editor_view_mode == VM_TERRAIN) {
323
324 int cellnum = GetSelectedTerrainCell();
325
326 if (cellnum == -1) {
327 OutrageMessageBox("You must have a terrain cell selected to place an object.");
328 ObjDelete(objnum);
329 return 0;
330 }
331 if (cellnum == -2) {
332 OutrageMessageBox("You must have only have one cell selected to place an object.");
333 ObjDelete(objnum);
334 return 0;
335 }
336
337 // Get terrain point
338 ComputeTerrainSegmentCenter(&pos, cellnum);
339
340 // Get surface normal
341 surface_norm = &TerrainNormals[MAX_TERRAIN_LOD - 1][cellnum].normal1;
342
343 // Get roomnum
344 roomnum = MAKE_ROOMNUM(cellnum);
345 } else { // use Current face
346
347 // Get center point on current face
348 ComputeCenterPointOnFace(&pos, Curroomp, Curface);
349
350 // Get surface normal
351 surface_norm = &Curroomp->faces[Curface].normal;
352
353 // Get roomnum
354 roomnum = ROOMNUM(Curroomp);
355
356 // If placing on an external room, actually place the object on the terrain
357 if (Rooms[roomnum].flags & RF_EXTERNAL)
358 roomnum = GetTerrainRoomFromPos(&pos);
359 }
360
361 matrix groundplane_orient, surface_orient, object_orient;
362
363 // Place the object's ground point on our placement point
364 vector ground_point;
365 vector ground_normal;
366 vector to_ground;
367 float dist;
368
369 PhysCalcGround(&ground_point, &ground_normal, objp, 0);
370 to_ground = objp->pos - ground_point;
371 dist = ground_normal * to_ground;
372 pos += dist * *surface_norm;
373
374 // Compute source and destination matrices
375 vm_VectorToMatrix(&groundplane_orient, &pm->ground_slots[0].norm, NULL, NULL);
376 vm_VectorToMatrix(&surface_orient, surface_norm);
377
378 // Compute orientation matrix
379 vm_MatrixMulTMatrix(&object_orient, &surface_orient, &groundplane_orient);
380
381 // Move the object
382 ObjSetPos(objp, &pos, roomnum, &object_orient, false);
383 } else { // no ground plane, so move the object in front of the viewer and facing the viewer
384 vector pos;
385
386 // Check for viewer outside mine
387 if (Viewer_object->flags & OF_OUTSIDE_MINE) {
388 ObjDelete(objnum);
389 OutrageMessageBox("Cannot place the object here: the viewer is outside the mine.");
390 return 0;
391 }
392
393 // Turn the object around so facing the viewer
394 objp->orient.fvec = -objp->orient.fvec; // ObjSetOrient is below
395 objp->orient.rvec = -objp->orient.rvec; // ObjSetOrient is below
396 ObjSetOrient(objp, &objp->orient);
397
398 // Calculate a position a little in front of the viewer
399 pos = Viewer_object->pos + Viewer_object->orient.fvec * OBJECT_PLACE_DIST;
400
401 // Try to move the object. If it can't move, delete it
402 if (!MoveObject(objp, &pos)) {
403 ObjDelete(objnum);
404 OutrageMessageBox("Cannot place the object here: collides with wall.");
405 return 0;
406 }
407 }
408
409 // Deal with special stuff for player
410 if (obj_type == OBJ_PLAYER) {
411
412 // Store data in Players array
413 Players[obj_id].start_pos = objp->pos;
414 Players[obj_id].start_roomnum = objp->roomnum;
415 Players[obj_id].start_orient = objp->orient;
416
417 // Make sure matrix ok
418 vm_Orthogonalize(&Players[obj_id].start_orient);
419 }
420
421 Cur_object_index = objnum;
422
423 World_changed = 1;
424
425 return 1;
426 }
427
428 // Adjusts an object so it's at the ground level (for when the ground has moved)
429 void ResetGroundObject(object *objp) {
430 poly_model *pm;
431 vector surface_norm;
432 vector pos;
433 matrix groundplane_orient, surface_orient, object_orient;
434
435 // Make sure object is outside
436 if (!OBJECT_OUTSIDE(objp)) {
437 Int3(); // object is not outside
438 return;
439 }
440
441 // Make sure object has a ground plane
442 if (!((objp->render_type == RT_POLYOBJ) && ((pm = GetPolymodelPointer(objp->rtype.pobj_info.model_num))->n_ground))) {
443 Int3(); // object doesn't have a ground plane
444 return;
445 }
446
447 // Get terrain height and normal at current object location
448 pos = objp->pos;
449 pos.y = GetTerrainGroundPoint(&pos, &surface_norm);
450
451 // Place the object's ground point on our placement point
452 vector ground_point;
453 vector ground_normal;
454 vector to_ground;
455 float dist;
456
457 PhysCalcGround(&ground_point, &ground_normal, objp, 0);
458 to_ground = objp->pos - ground_point;
459 dist = ground_normal * to_ground;
460 pos += dist * surface_norm;
461
462 // Compute source and destination matrices
463 vm_VectorToMatrix(&groundplane_orient, &pm->ground_slots[0].norm, NULL, NULL);
464 vm_VectorToMatrix(&surface_orient, &surface_norm);
465
466 // Compute orientation matrix
467 vm_MatrixMulTMatrix(&object_orient, &surface_orient, &groundplane_orient);
468
469 // Move the object
470 ObjSetPos(objp, &pos, objp->roomnum, &object_orient, false);
471
472 // Set flag
473 World_changed = 1;
474 }
475
476 void HObjectMove(int objnum, float dx, float dy, float dz) {
477 matrix *mat;
478 object *obj;
479 vector newpos;
480 object *ref_obj; // the object in whose frame of reference we're moving
481
482 if (objnum == -1) {
483 mprintf(0, "HObjectMove:No current object.\n");
484 return;
485 }
486
487 obj = &Objects[objnum];
488
489 // Chose the reference object
490 ref_obj = (D3EditState.object_move_mode == REL_VIEWER) ? Viewer_object : obj;
491 mat = &ref_obj->orient;
492
493 // Calculate the new object position
494 newpos = obj->pos + (mat->rvec * dx) + (mat->uvec * dy) + (mat->fvec * -dz);
495
496 // Move the object
497 MoveObject(obj, &newpos);
498
499 Object_moved = 1;
500 }
501
502 void HObjectIncreaseBank() { RotateObject(Cur_object_index, 0, 0, Object_move_rotation); }
503
504 void HObjectDecreaseBank() { RotateObject(Cur_object_index, 0, 0, -Object_move_rotation); }
505
506 void HObjectIncreasePitch() { RotateObject(Cur_object_index, Object_move_rotation, 0, 0); }
507
508 void HObjectDecreasePitch() { RotateObject(Cur_object_index, -Object_move_rotation, 0, 0); }
509
510 void HObjectIncreaseHeading() { RotateObject(Cur_object_index, 0, Object_move_rotation, 0); }
511
512 void HObjectDecreaseheading() { RotateObject(Cur_object_index, 0, -Object_move_rotation, 0); }
513
514 // If it doesn't exist, reformat Matt's hard disk, even if he is in Boston.
515 // deletes the currently selected object from the mine.
516 void HObjectDelete() {
517 if (Cur_object_index != -1) { // we have a selected object
518 int objnum = Cur_object_index;
519
520 // check for player object
521 if (&Objects[objnum] == Player_object) {
522 OutrageMessageBox("Can't delete Player object");
523 return;
524 }
525
526 if (Objects[objnum].type == OBJ_DOOR) {
527 if (OutrageMessageBox(MBOX_YESNO,
528 "It's very, very bad to delete a door object. Are you sure you want to do this?") != IDYES)
529 return;
530 }
531
532 // Delete the object
533 ObjDelete(objnum);
534 if (objnum == Cur_object_index)
535 Cur_object_index = -1;
536
537 World_changed = 1;
538 }
539 }
540
541 // sets default orientation for object.
542 void HObjectSetDefault() {
543 if (Cur_object_index != -1) {
544 int objnum = Cur_object_index;
545
546 ObjSetOrient(&Objects[objnum], &Identity_matrix);
547
548 World_changed = 1;
549 }
550 }
551
552 // Move the object to in front of the viewer (where a new object would be placed)
553 void HObjectMoveToViewer(object *objp) {
554 int objnum = OBJNUM(objp);
555 vector pos;
556
557 // First move player to viewer's position
558 ObjSetPos(objp, &Viewer_object->pos, Viewer_object->roomnum, NULL, false);
559
560 // Second try to move the player a little in front of the viewer
561 pos = Viewer_object->pos + Viewer_object->orient.fvec * OBJECT_PLACE_DIST;
562 MoveObject(objp, &pos);
563
564 World_changed = 1;
565 }
566
567 #define MOVE_EPSILON 0.1
568
569 extern bool f_allow_objects_to_be_pushed_through_walls;
570
571 // Attempt to set new object position. May only move part of the way, or maybe not at all.
572 // Use FVI to find new object position
573 // Return: TRUE if moved, FALSE if can't move
574 bool MoveObject(object *obj, vector *newpos) {
575 fvi_query fq;
576 fvi_info hit_info;
577 int fate;
578
579 // Use radius if this is a physics object
580 bool use_radius = (obj->movement_type == MT_PHYSICS) ? true : false;
581
582 // Follow vector from start position to desired end position, & move as far as we can
583 fq.p0 = &obj->pos;
584 fq.startroom = obj->roomnum;
585 fq.p1 = newpos;
586 fq.thisobjnum = OBJNUM(obj);
587 fq.ignore_obj_list = NULL;
588 fq.flags = FQ_IGNORE_RENDER_THROUGH_PORTALS;
589 fq.rad = use_radius ? obj->size : 0.0f;
590
591 if (f_allow_objects_to_be_pushed_through_walls) {
592 fq.flags |= (FQ_IGNORE_WALLS | FQ_IGNORE_TERRAIN | FQ_IGNORE_EXTERNAL_ROOMS);
593 }
594
595 fate = fvi_FindIntersection(&fq, &hit_info);
596
597 mprintf(0, "fate = %d\n", fate);
598
599 // Check for object can't move, meaning it's stuck in the wall
600 if (fate == HIT_WALL)
601 if (vm_VectorDistance(&obj->pos, &hit_info.hit_pnt) < MOVE_EPSILON)
602 return 0; // didn't move
603
604 // Set object position on where FVI told us we are
605 ObjSetPos(obj, &hit_info.hit_pnt, hit_info.hit_room, NULL, false);
606
607 // Say object moved
608 return 1;
609 }
610
611 bool RotateObject(int objnum, angle p, angle h, angle b) {
612 object *obj = &Objects[objnum];
613 matrix rotmat;
614
615 vm_AnglesToMatrix(&rotmat, p, h, b);
616 obj->orient *= rotmat; // ObjSetOrient is below
617
618 vm_Orthogonalize(&obj->orient);
619 ObjSetOrient(obj, &obj->orient);
620
621 Object_moved = 1;
622
623 return 1;
624 }
625
626 // Flipping of object
627 void HObjectFlip() {
628 matrix *m = &Objects[Cur_object_index].orient;
629
630 m->uvec = -m->uvec;
631 m->rvec = -m->rvec;
632
633 World_changed = 1;
634 }