Debian Codegraph @

editor/HView.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/HView.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:57:38 $
23 * $Author: kevinb $
24 *
25 * Code to implement view menu functions
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 24 4/30/99 11:18p Matt
30 * Limit viewer to terrain bounds when moving the viewer to an external
31 * room.
32 *
33 * 23 4/29/99 11:51p Matt
34 * Improved viewer move functions.
35 *
36 * 22 4/17/99 2:42p Matt
37 * Set Viewer_moved when a new viewer is selected.
38 *
39 * 21 2/28/99 8:11p Matt
40 * Fixed problems when at max viewers
41 *
42 * 20 1/15/99 7:52p Chris
43 * Updated ObjSetPos() to include a f_update_attach_children flag
44 *
45 * 19 5/12/98 2:49p Matt
46 * Link the room viewer to the terrain, since there's not always a room to
47 * put him in.
48 *
49 * 18 3/12/98 7:30p Chris
50 * Added ObjSetOrient
51 *
52 * 17 2/09/98 10:55a Matt
53 * When searching for rooms to put the viewer in, don't consider external
54 * rooms.
55 *
56 * 16 2/05/98 3:11p Matt
57 * Don't move the viewer to an external room.
58 *
59 * 15 2/05/98 2:57p Matt
60 * Changed code to use ObjSetPos()
61 *
62 * 14 2/04/98 6:23p Matt
63 * Changed object room number to indicate a terrain cell via a flag. Got
64 * rid of the object flag which used to indicate terrain.
65 *
66 * 13 1/29/98 5:50p Matt
67 * Changed old camera object type to be viewer object (for editor), and
68 * now camera objects will just be for game cameras.
69 *
70 * 12 1/21/98 1:08p Matt
71 * Made SetViewMode() not take new_viewer parm, since it's not needed
72 * after my revamp of the editor view system.
73 *
74 * 11 1/21/98 12:33p Matt
75 * Revamped viewer system
76 *
77 * 10 1/19/98 10:04a Matt
78 * Removed unused function
79 *
80 * 9 11/14/97 1:01p Matt
81 * When can't find camera, don't just make one in the center of room 0.
82 * Instead, find the first room that's used.
83 *
84 * 8 9/17/97 11:51a Samir
85 * BIG SEGMENT RIPOUT
86 *
87 * 7 8/25/97 2:50p Chris
88 * Improve AABB/object movement code.
89 *
90 * 6 8/04/97 7:39p Matt
91 * Pass new room number to ObjSetPos()
92 *
93 * 5 8/04/97 12:43p Matt
94 * Added SetViewerFromRoomFace()
95 *
96 * 4 7/22/97 7:08p Matt
97 * Cleaned up D3EditState, moving some vars in and some out, and renaming
98 * and changing a few others
99 *
100 * 4 4/01/97 11:00p Matt
101 * Changed editor to keep a viewer object (type camera) seperate from the
102 * player object. This camera, and not the player, is now moved by
103 * slewing, the C key, etc. When going into the game, the viewer position
104 * & orientation are copied to the player. When going back to the editor,
105 * the player position is copied to the viewer, and the player object is
106 * reset to its start location.
107 *
108 * 3 3/31/97 5:58p Matt
109 * Revamped mine update flags
110 *
111 * 2 3/17/97 2:26p Matt
112 * Added function to set player view from curseg/curside
113 *
114 * 1 3/17/97 1:47p Matt
115 *
116 * $NoKeywords: $
117 */
118
119 #include "mainfrm.h"
120 #include "HView.h"
121 #include "d3edit.h"
122 #include "terrain.h"
123 #include "erooms.h"
124 #include "editor.h"
125
126 #define FACE_VIEW_DIST 5.0
127
128 // Set the viewer in the specified room facing the specified face
129 // If room_center is true, put viewer at the center of the room facing the face
130 // If room_center is false, put the viewer directly in front of the selected face
131 // If the room is external, put the viewer a distance away from the room,
132 // facing either the center (if room_center is true) or the specified face
133 void SetViewerFromRoomFace(room *roomp, int facenum, bool room_center) {
134 vector vp, newpos;
135 matrix orient;
136 int roomnum;
137 bool outside_mine = 0;
138
139 ComputeCenterPointOnFace(&vp, roomp, facenum);
140 roomnum = ROOMNUM(roomp);
141
142 if (room_center) {
143
144 // Get position
145 ComputeRoomCenter(&newpos, roomp);
146
147 if (roomp->flags & RF_EXTERNAL) {
148 vector t;
149 float rad = ComputeRoomBoundingSphere(&t, roomp);
150
151 newpos.z -= rad * 1.5;
152
153 if (newpos.x < 1.0)
154 newpos.x = 1.0;
155 if (newpos.x > TERRAIN_WIDTH * TERRAIN_SIZE - 1.0)
156 newpos.x = TERRAIN_WIDTH * TERRAIN_SIZE - 1.0;
157 if (newpos.z < 1.0)
158 newpos.z = 1.0;
159 if (newpos.z > TERRAIN_DEPTH * TERRAIN_SIZE - 1.0)
160 newpos.z = TERRAIN_WIDTH * TERRAIN_SIZE - 1.0;
161
162 orient = Identity_matrix;
163
164 roomnum = GetTerrainRoomFromPos(&newpos);
165 } else {
166 // Get orientation
167 vp -= newpos; // vector from center of room to face
168 vm_VectorToMatrix(&orient, &vp, NULL, NULL);
169 }
170 } else {
171 face *fp = &roomp->faces[facenum];
172
173 newpos = vp + fp->normal * FACE_VIEW_DIST;
174
175 vector t = -fp->normal;
176 vm_VectorToMatrix(&orient, &t, NULL, NULL);
177
178 if (roomp->flags & RF_EXTERNAL) {
179 if (newpos.x < 1.0)
180 newpos.x = 1.0;
181 if (newpos.x > TERRAIN_WIDTH * TERRAIN_SIZE - 1.0)
182 newpos.x = TERRAIN_WIDTH * TERRAIN_SIZE - 1.0;
183 if (newpos.z < 1.0)
184 newpos.z = 1.0;
185 if (newpos.z > TERRAIN_DEPTH * TERRAIN_SIZE - 1.0)
186 newpos.z = TERRAIN_WIDTH * TERRAIN_SIZE - 1.0;
187 roomnum = GetTerrainRoomFromPos(&newpos);
188 } else {
189 int new_roomnum = FindPointRoom(&newpos);
190 if (new_roomnum == -1)
191 outside_mine = 1;
192 else
193 roomnum = new_roomnum;
194 }
195 }
196
197 // Reset viewer
198 if (Editor_view_mode == VM_ROOM) {
199 Viewer_object->pos = newpos;
200 Viewer_object->orient = orient;
201 } else
202 MoveViewer(&newpos, roomnum, &orient);
203
204 if (outside_mine)
205 Viewer_object->flags |= OF_OUTSIDE_MINE;
206
207 Viewer_moved = 1;
208 }
209
210 // max viewers for each type
211 #define MAX_VIEWERS 20
212
213 // The viewer in room mode has this ID
214 #define ROOM_VIEWER_ID MAX_VIEWERS
215
216 // Finds a specific viewer object it one exists
217 // Parameters: id - which viewer id
218 // Returns: object number of a viewer object, or -1 if none
219 int FindViewerObject(int id) {
220 object *objp;
221 int objnum = -1;
222
223 // trace through all objects looking for viewer
224 for (objnum = 0, objp = Objects; objnum <= Highest_object_index; objnum++, objp++)
225 if ((objp->type == OBJ_VIEWER) && (objp->id == id))
226 break;
227
228 if (objnum <= Highest_object_index) // found one
229 return objnum;
230 else
231 return -1;
232 }
233
234 // Finds a viewer object if one exists.
235 // Starts looking at the specified id and searches through all possible ids
236 // Parameters: id - which viewer id
237 // view_mode - if -1, find any viewer, else find one that matches view mode
238 // Returns: object number of a viewer object, or -1 if none
239 int FindNextViewerObject(int id, int view_mode) {
240 int i;
241 int objnum;
242 int terrain_flag;
243
244 if (id == -1)
245 id = 0;
246
247 ASSERT((id >= 0) && (id <= MAX_VIEWERS));
248
249 // Get flags
250 terrain_flag = (view_mode == VM_TERRAIN);
251
252 // Try all viewer id's, starting at the one passed in
253 for (i = 0; i < MAX_VIEWERS; i++) {
254 int check_id = ((id + i) % MAX_VIEWERS);
255
256 objnum = FindViewerObject(check_id);
257
258 if ((objnum != -1) && ((view_mode == -1) || ((OBJECT_OUTSIDE(&Objects[objnum]) != 0) == terrain_flag)))
259 break;
260 }
261
262 if (i == MAX_VIEWERS) // didn't find one
263 return -1;
264
265 return objnum;
266 }
267
268 // Creates a viewer object of the specified type
269 // Parameters: view_mode - mine,terrain,or room. See constants in 3dedit.h
270 // pos - initial position of this object
271 // roomnum - initial room/terrain cell of this object
272 // Returns: object number of the object created, or -1 if at max number of viewers of that type
273 int CreateViewerObject(int view_mode, vector *pos, int roomnum) {
274 int id;
275 object *objp;
276 int objnum = -1;
277
278 if (view_mode == VM_ROOM) {
279 id = ROOM_VIEWER_ID;
280
281 for (objnum = 0, objp = Objects; objnum <= Highest_object_index; objnum++, objp++)
282 if ((objp->type == OBJ_VIEWER) && (objp->id == id))
283 return -1; // this one already used
284 } else {
285 // for each id, loop through all objects to see if it's used
286 for (id = 0; id < MAX_VIEWERS; id++) {
287 for (objnum = 0, objp = Objects; objnum <= Highest_object_index; objnum++, objp++)
288 if ((objp->type == OBJ_VIEWER) && (objp->id == id))
289 break; // this one already used
290 if (objnum > Highest_object_index) // didn't find objec twith this id
291 break;
292 }
293
294 if (id == MAX_VIEWERS) // no unused viewer id's
295 return -1;
296 }
297
298 // Create the new object
299 objnum = ObjCreate(OBJ_VIEWER, id, roomnum, pos, NULL);
300
301 if (objnum == -1) {
302 Int3();
303 return -1;
304 }
305
306 return objnum;
307 }
308
309 // Returns the number (not the id) of the current viewer, in the range 0..MAX_VIEWERS
310 int GetViewerNum() {
311 ASSERT(Editor_view_mode != VM_ROOM);
312
313 return Viewer_object->id;
314 }
315
316 // Set the specified object to be the viewer
317 void SetViewer(int objnum) {
318 Viewer_object = &Objects[objnum];
319
320 if (Editor_view_mode != VM_ROOM)
321 Editor_viewer_id = Viewer_object->id;
322
323 if ((Editor_view_mode == VM_MINE) && OBJECT_OUTSIDE(Viewer_object))
324 SetViewMode(VM_TERRAIN);
325
326 if ((Editor_view_mode == VM_TERRAIN) && !(OBJECT_OUTSIDE(Viewer_object)))
327 SetViewMode(VM_MINE);
328
329 State_changed = Viewer_moved = 1;
330 }
331
332 // Creates a new viewer object. Copies position & orientation from the current viewer
333 void CreateNewViewer() {
334 int objnum;
335
336 objnum = CreateViewerObject(Editor_view_mode, &Viewer_object->pos, Viewer_object->roomnum);
337
338 if (objnum == -1) {
339 OutrageMessageBox("Cannot create new viewer: you already have the maximum number of viewers.");
340 return;
341 }
342
343 ObjSetOrient(&Objects[objnum], &Viewer_object->orient);
344
345 SetViewer(objnum);
346
347 EditorStatus("Viewer %d created.", GetViewerNum());
348
349 World_changed = 1;
350 }
351
352 // Select next viewer
353 void SelectNextViewer() {
354 int objnum;
355
356 if (Editor_view_mode == VM_ROOM) {
357 EditorStatus("There is only one viewer in Room View");
358 return;
359 }
360
361 ASSERT(Viewer_object != NULL);
362
363 objnum = FindNextViewerObject(Viewer_object->id + 1, -1);
364
365 ASSERT(objnum != -1);
366
367 if (objnum == OBJNUM(Viewer_object)) {
368 EditorStatus("No other viewers.");
369 return;
370 }
371
372 SetViewer(objnum);
373 EditorStatus("Viewer %d selected.", GetViewerNum());
374 }
375
376 // Deletes the current viewer object
377 void DeleteViewer() {
378 int objnum, old_num;
379
380 if (Viewer_object->id == ROOM_VIEWER_ID) {
381 OutrageMessageBox("You cannot delete the Room viewer.");
382 return;
383 }
384
385 objnum = FindNextViewerObject(Viewer_object->id + 1, -1);
386
387 ASSERT(objnum != -1);
388
389 if (objnum == OBJNUM(Viewer_object)) { // couldn't find another viewer
390 OutrageMessageBox("You cannot delete your only viewer.");
391 return;
392 }
393
394 old_num = GetViewerNum();
395
396 // Kill the old viewer
397 ObjDelete(OBJNUM(Viewer_object));
398
399 // Set the new viewer
400 SetViewer(objnum);
401
402 EditorStatus("Viewer %d deleted. Viewer %d selected.", old_num, GetViewerNum());
403
404 World_changed = 1;
405 }
406
407 // Sets the viewer object for the editor, creating if not already in mine
408 // Keeps seperate viewer objects for mine & terrain views
409 void SetEditorViewer() {
410 int objnum, roomnum;
411
412 // First, see if a camera object already exists in the level
413 if (Editor_view_mode == VM_ROOM)
414 objnum = FindViewerObject(ROOM_VIEWER_ID);
415 else
416 objnum = FindNextViewerObject(Editor_viewer_id, Editor_view_mode);
417
418 // If no viewer object, create one
419 if (objnum == -1) {
420 vector pos;
421
422 // get position for viewer
423 if (Editor_view_mode == VM_TERRAIN) { // if terrain, put viewer at center of world
424 pos.x = TERRAIN_SIZE * TERRAIN_WIDTH / 2;
425 pos.y = Terrain_seg[0].y + 30;
426 pos.z = TERRAIN_SIZE * TERRAIN_DEPTH / 2;
427 roomnum = MAKE_ROOMNUM(0); // any value ok, so long as it has terrain flag
428 } else if (Editor_view_mode == VM_MINE) { // if mine, put in center of any room
429 for (roomnum = 0; roomnum <= Highest_room_index; roomnum++)
430 if (Rooms[roomnum].used && !(Rooms[roomnum].flags & RF_EXTERNAL)) {
431 ComputeRoomCenter(&pos, &Rooms[roomnum]);
432 break;
433 }
434 ASSERT(roomnum <= Highest_room_index);
435 } else if (Editor_view_mode == VM_ROOM) { // if room, put at 0,0,0
436 vm_MakeZero(&pos);
437 roomnum = MAKE_ROOMNUM(0);
438 } else
439 Int3(); // unknown view mode
440
441 objnum = CreateViewerObject(Editor_view_mode, &pos, roomnum);
442
443 // If no free viewer slots, grab any viewer and move it
444 if (objnum == -1) {
445 ASSERT(Viewer_object != NULL);
446 if (Viewer_object->type == OBJ_VIEWER)
447 objnum = OBJNUM(Viewer_object);
448 else {
449 objnum = FindNextViewerObject(Editor_viewer_id, -1);
450 ASSERT(objnum != -1);
451 }
452 ObjSetPos(&Objects[objnum], &pos, roomnum, NULL, true);
453 }
454 }
455
456 ASSERT(objnum != -1);
457 SetViewer(objnum);
458 }
459
460 // Moves the room viewer to the origin, if there is a room viewer
461 void ResetRoomViewer() {
462 int objnum = FindViewerObject(ROOM_VIEWER_ID);
463
464 if (objnum != -1) {
465 vm_MakeZero(&Objects[objnum].pos);
466 ObjSetOrient(&Objects[objnum], &Identity_matrix);
467 }
468 }