Debian Codegraph @

common/include/editor/editor.h

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Header for editor functions, data strcutures, etc.
23 *
24 */
25
26 #pragma once
27
28 #include <array>
29 #include "dxxsconf.h"
30 #include "dsx-ns.h"
31 #include "vecmat.h"
32 #include "ui.h"
33 #include "fmtcheck.h"
34
35 #include "fwd-window.h"
36 #include "fwd-segment.h"
37 #include "objnum.h"
38 #ifdef DXX_BUILD_DESCENT
39 #include "robot.h"
40 #endif
41 #include "physfsx.h"
42
43 /*
44 * Constants
45 *
46 */
47
48 #define ED_SCREEN_W 800 //width of editor screen
49 #define ED_SCREEN_H 600 //height of editor screen
50
51 #define MENUBAR_H 16
52
53 #define GAMEVIEW_X 1 //where the 320x200 game window goes
54 #define GAMEVIEW_Y 1+MENUBAR_H
55 #define GAMEVIEW_W 320
56 #define GAMEVIEW_H 200
57
58 #define STATUS_X 0
59 #define STATUS_H 18
60 #define STATUS_Y (ED_SCREEN_H-STATUS_H)
61 #define STATUS_W ED_SCREEN_W
62
63 #define LVIEW_X 1 //large view
64 #define LVIEW_Y (GAMEVIEW_Y+GAMEVIEW_H+2)
65 #define LVIEW_W 447
66 #define LVIEW_H (STATUS_Y-LVIEW_Y-2)
67
68 #define TMAPBOX_X (LVIEW_X+LVIEW_W+4) //location of first one
69 #define TMAPBOX_Y (LVIEW_Y+2)
70 #define TMAPBOX_W 64
71 #define TMAPBOX_H 64
72
73 #define TMAPCURBOX_X (TMAPBOX_X + 4*(TMAPBOX_W + 3))
74 #define TMAPCURBOX_Y (TMAPBOX_Y + TMAPBOX_H)
75
76 #define OBJCURBOX_X (TMAPCURBOX_X)
77 #define OBJCURBOX_Y (TMAPCURBOX_Y + 3*(TMAPBOX_H + 2) -40)
78
79 #define PAD_X (GAMEVIEW_X + GAMEVIEW_W + 16)
80 #define PAD_Y (GAMEVIEW_Y + 4)
81
82 #define SMALLVIEW_W 173 //width of small view windows
83 #define SMALLVIEW_H 148 //height of small view windows
84
85 #define TVIEW_X (LVIEW_X+LVIEW_W+2) //top view
86 #define TVIEW_Y LVIEW_Y
87 #define TVIEW_W SMALLVIEW_W
88 #define TVIEW_H SMALLVIEW_H
89
90 #define FVIEW_X TVIEW_X //front view
91 #define FVIEW_Y (TVIEW_Y+SMALLVIEW_H+2)
92 #define FVIEW_W SMALLVIEW_W
93 #define FVIEW_H SMALLVIEW_H
94
95 #define RVIEW_X (TVIEW_X+SMALLVIEW_W+2) //right view
96 #define RVIEW_Y FVIEW_Y
97 #define RVIEW_W SMALLVIEW_W
98 #define RVIEW_H SMALLVIEW_H
99
100 #define GVIEW_X RVIEW_X //group view
101 #define GVIEW_Y TVIEW_Y
102 #define GVIEW_W SMALLVIEW_W
103 #define GVIEW_H SMALLVIEW_H
104
105 //there were color constants here, but I moved them to meddraw.c - Matt
106
107 #define SEGMOVE_PAD_ID 0
108 #define SEGSIZE_PAD_ID 1
109 #define CURVE_PAD_ID 2
110 #define TEXTURE_PAD_ID 3
111 #define OBJECT_PAD_ID 4
112 #define OBJMOV_PAD_ID 5
113 #define GROUP_PAD_ID 6
114 #define LIGHTING_PAD_ID 7
115 #define TEST_PAD_ID 8
116 #define MAX_PAD_ID 8
117
118 /*
119 * Strucures
120 *
121 */
122
123 #define VF_ANGLES 0
124 #define VF_MATRIX 1
125
126 // Default size of a segment
127 #define DEFAULT_X_SIZE F1_0*20
128 #define DEFAULT_Y_SIZE F1_0*20
129 #define DEFAULT_Z_SIZE F1_0*20
130
131 // Scale factor from 3d units (integer portion) to uv coordinates (integer portion)
132 #define VMAG (F1_0 / (DEFAULT_X_SIZE/F1_0))
133 #define UMAG VMAG // unused
134
135 // Number of segments which can be found (size of Found_segs[])
136
137 #define MAX_GROUPS 10
138 #define ROT_GROUP MAX_GROUPS
139
140 // Modes for segment sizing
141 #define SEGSIZEMODE_FREE 1
142 #define SEGSIZEMODE_ALL 2
143 #define SEGSIZEMODE_CURSIDE 3
144 #define SEGSIZEMODE_EDGE 4
145 #define SEGSIZEMODE_VERTEX 5
146
147 #define SEGSIZEMODE_MIN SEGSIZEMODE_FREE
148 #define SEGSIZEMODE_MAX SEGSIZEMODE_VERTEX
149
150 //defines a view for an editor window
151 struct editor_view
152 {
153 short ev_num; //each view has it's own number
154 short ev_changed; //set to true if view changed
155 grs_canvas *ev_canv; //points to this window's canvas
156 fix ev_dist; //the distance from the view point
157 vms_matrix ev_matrix; //the view matrix
158 fix ev_zoom; //zoom for this window
159 };
160
161 enum class editor_gamestate : uint8_t
162 {
163 none, // editing a level, not a gamestate
164 unsaved, // just pressed delete-e from a game
165 saved // saved state. Do we want to restore it when launching editor from menu? Ask user.
166 };
167
168 /*
169 * Global variables
170 *
171 */
172
173 extern std::array<editor_view *, 1> Views;
174 extern int Large_view_index;
175 extern std::unique_ptr<UI_GADGET_USERBOX> LargeViewBox, GameViewBox, GroupViewBox;
176 extern int Found_seg_index; // Index in Found_segs corresponding to Cursegp
177 extern editor_gamestate gamestate;
178
179 extern vms_vector Ed_view_target; // what editor is looking at
180
181 extern class window *Pad_info; // Keypad text
182
183 extern int Show_axes_flag; // 0 = don't show, !0 = do show coordinate axes in *Cursegp orientation
184
185 namespace dcx {
186 extern grs_font_ptr editor_font;
187 extern int Autosave_count; // Current counter for which autosave mine we are "on"
188 extern int Autosave_flag; // Whether or not Autosave is on.
189 extern struct tm Editor_time_of_day;
190
191 using mine_filename_type = std::array<char, PATH_MAX>;
192 extern mine_filename_type mine_filename;
193 }
194
195 extern int SegSizeMode; // Mode = 0/1 = not/is legal to move bound vertices,
196
197 #ifdef DXX_BUILD_DESCENT
198 namespace dsx {
199 void init_editor(void);
200 // Initialize all vertices to inactive status.
201 extern void init_all_vertices(void);
202 }
203 #endif
204
205 void med_combine_duplicate_vertices(enumerated_array<uint8_t, MAX_VERTICES, vertnum_t> &);
206
207 #ifdef DXX_BUILD_DESCENT
208 namespace dsx {
209 // Attach side newside of newseg to side destside of destseg.
210 // Copies *newseg into global array Segments, increments Num_segments.
211 // Forms a weld between the two segments by making the new segment fit to the old segment.
212 // Updates number of faces per side if necessitated by new vertex coordinates.
213 // Return value:
214 // 0 = successful attach
215 // 1 = No room in Segments[].
216 // 2 = No room in Vertices[].
217 int med_attach_segment(vmsegptridx_t destseg, csmusegment newseg, sidenum_t destside, sidenum_t newside);
218
219 // Delete a segment.
220 // Deletes a segment from the global array Segments.
221 // Updates Cursegp to be the segment to which the deleted segment was connected. If there is
222 // more than one connected segment, the new Cursegp will be the segment with the highest index
223 // of connection in the deleted segment (highest index = front)
224 // Return value:
225 // 0 = successful deletion
226 // 1 = unable to delete
227 int med_delete_segment(vmsegptridx_t sp);
228
229 // Rotate the segment *seg by the pitch, bank, heading defined by *rot, destructively
230 // modifying its four free vertices in the global array Vertices.
231 // It is illegal to rotate a segment which has MAX_SIDES_PER_SEGMENT != 1.
232 // Pitch, bank, heading are about the point which is the average of the four points
233 // forming the side of connection.
234 // Return value:
235 // 0 = successful rotation
236 // 1 = MAX_SIDES_PER_SEGMENT makes rotation illegal (connected to 0 or 2+ segments)
237 // 2 = Rotation causes degeneracy, such as self-intersecting segment.
238 int med_rotate_segment(vmsegptridx_t seg, const vms_matrix &rotmat);
239
240 // Create a wall which can be removed.
241 // Creates wall at sp->sides[side], making it part of segment sp
242 // Removable walls must be placed between two connected segments. You should add the removable
243 // wall on both sides. In fact, you really must.
244 void create_removable_wall(fvcvertptr &vcvertptr, vmsegptridx_t sp, sidenum_t side, texture1_value tmap_num);
245 }
246 #endif
247
248 // Saves mine contained in Segments[] and Vertices[].
249 // Num_segments = number of segments in mine.
250 // Num_vertices = number of vertices in mine.
251 // Cursegp = current segment.
252 // Saves Num_segments, and index of current segment (which is Cursegp - Segments), which will be converted to a pointer
253 // and written to Cursegp in med_load_mine.
254 // Returns:
255 // 0 = successfully saved.
256 // 1 = unable to save.
257 int med_save_mine(const mine_filename_type &name);
258
259 // Updates the screen... (I put the prototype here for curves.c)
260 extern int medlisp_update_screen();
261
262 // Returns 0 if no error, 1 if error, whatever that might be.
263 // Sets globals:
264 // Num_segments
265 // Num_vertices
266 // Cursegp = pointer to only segment.
267 #ifdef DXX_BUILD_DESCENT
268 namespace dsx {
269 extern int create_new_mine(void);
270
271 }
272 #endif
273 #ifdef DXX_BUILD_DESCENT
274 namespace dsx {
275 // Create a segment given center, dimensions, rotation matrix.
276 // Note that the created segment will always have planar sides and rectangular cross sections.
277 // It will be created with walls on all sides, ie not connected to anything.
278 void med_create_segment(vmsegptridx_t sp,fix cx, fix cy, fix cz, fix length, fix width, fix height, const vms_matrix &mp);
279
280 // Create New_segment with sizes found in *scale.
281 void med_create_new_segment(const vms_vector &scale);
282
283 // Create New_segment with sizes found in Cursegp.
284 extern void med_create_new_segment_from_cursegp(void);
285
286 // Create a new segment and use it to form a bridge between two existing segments.
287 // Specify two segment:side pairs. If either segment:side is not open (ie, segment->children[side] != -1)
288 // then it is not legal to form the brider.
289 // Return:
290 // 0 bridge segment formed
291 // 1 unable to form bridge because one (or both) of the sides is not open.
292 // Note that no new vertices are created by this process.
293 int med_form_bridge_segment(vmsegptridx_t seg1, sidenum_t side1, vmsegptridx_t seg2, sidenum_t side2);
294 }
295 #endif
296
297 // Compress mine at Segments and Vertices by squeezing out all holes.
298 // If no holes (ie, an unused segment followed by a used segment), then no action.
299 // If Cursegp or Markedsegp is a segment which gets moved to fill in a hole, then
300 // they are properly updated.
301 extern void med_compress_mine(void);
302
303 void update_matrix_based_on_side(vms_matrix &rotmat, sidenum_t destside);
304
305 // Curves stuff.
306
307 #define ACCURACY 0.1*F1_0
308
309 struct vms_equation
310 {
311 union {
312 struct {fix x3, x2, x1, x0, y3, y2, y1, y0, z3, z2, z1, z0;} n;
313 std::array<std::array<fix, 4>, 3> xyz;
314 };
315 };
316
317 // Q(t) = (2t^3 - 3t^2 + 1) p1 + (-2t^3 + 3t^2) p4 + (t^3 - 2t^2 + t) r1 + (t^3 - t^2 ) r4
318
319 extern vms_vector evaluate_curve(vms_equation *coeffs, int degree, fix t);
320
321 fix curve_dist(vms_equation *coeffs, int degree, fix t0, const vms_vector &p0, fix dist);
322
323 extern void plot_parametric(vms_equation *coeffs, fix min_t, fix max_t, fix del_t);
324
325 // Curve generation routine.
326 // Returns 1 if curve is generated.
327 // Returns 0 if no curve.
328 extern int generate_curve( fix r1scale, fix r4scale );
329
330 // Deletes existing curve generated in generate_curve().
331 extern void delete_curve();
332
333 #ifdef DXX_BUILD_DESCENT
334 namespace dsx {
335
336 // Assign default u,v coordinates to all sides of a segment.
337 // This routine should only be used for segments which are not connected to anything else,
338 // ie the segment created at mine creation.
339 void assign_default_uvs_to_segment(vmsegptridx_t segp);
340 void assign_default_uvs_to_side(vmsegptridx_t segp, sidenum_t side);
341
342 // Create coordinate axes in orientation of specified segment, stores vertices at *vp.
343 void create_coordinate_axes_from_segment(const shared_segment &sp, std::array<vertnum_t, 16> &vertnums);
344
345 // Set Vertex_active to number of occurrences of each vertex.
346 // Set Num_vertices.
347 extern void set_vertex_counts(void);
348
349 // Modify seg2 to share side2 with seg1:side1. This forms a connection between
350 // two segments without creating a new segment. It modifies seg2 by sharing
351 // vertices from seg1. seg1 is not modified. Four vertices from seg2 are
352 // deleted.
353 // If the four vertices forming side2 in seg2 are not free, the joint is not formed.
354 // Return code:
355 // 0 joint formed
356 // 1 unable to form joint because one or more vertices of side2 is not free
357 // 2 unable to form joint because side1 is already used
358 int med_form_joint(vmsegptridx_t seg1, sidenum_t side1, vmsegptridx_t seg2, sidenum_t side2);
359 }
360
361 // The current texture... use by saying something=bm_lock_bitmap(CurrentTexture)
362 extern texture_index CurrentTexture;
363
364 namespace dsx {
365 void med_propagate_tmaps_to_segments(vcsegptridx_t base_seg,vmsegptridx_t con_seg, int uv_only_flag);
366 void med_propagate_tmaps_to_back_side(vmsegptridx_t base_seg, sidenum_t back_side, int uv_only_flag);
367
368 // Find segment adjacent to sp:side.
369 // Adjacent means a segment which shares all four vertices.
370 // If found, return a pair containing the found segment and side.
371 // If not found, return an empty optional.
372 std::optional<std::pair<vmsegptridx_t, sidenum_t>> med_find_adjacent_segment_side(vmsegptridx_t sp, sidenum_t side);
373
374 // Finds the closest segment and side to sp:side.
375 std::optional<std::pair<vmsegptridx_t, sidenum_t>> med_find_closest_threshold_segment_side(vmsegptridx_t sp, sidenum_t side, fix threshold);
376
377 // Select previous segment.
378 // If there is a connection on the side opposite to the current side, then choose that segment.
379 // If there is no connecting segment on the opposite face, try any segment.
380
381 // Select next segment.
382 // If there is a connection on the current side, then choose that segment.
383 // If there is no connecting segment on the current side, try any segment.
384
385 // Copy texture maps in newseg to nsp.
386 void copy_uvs_seg_to_seg(unique_segment &destseg, const unique_segment &srcseg);
387
388 // Return true if segment is concave.
389
390 // Return N_found_segs = number of concave segments in mine.
391 // Segment ids stored at Found_segs
392 extern void find_concave_segs(void);
393
394 // High level call. Check for concave segments, print warning message (using editor_status)
395 // if any concave segments.
396 // Calls find_concave_segs, therefore N_found_segs gets set, and Found_segs filled in.
397 extern void warn_if_concave_segments(void);
398
399 // Warn if segment s is concave.
400 void warn_if_concave_segment(vmsegptridx_t s);
401 }
402
403 // Add a vertex to the vertex list.
404 vertnum_t med_add_vertex(const vertex &vp);
405
406 // Add a vertex to the vertex list which may be identical to another vertex (in terms of coordinates).
407 // Don't scan list, looking for presence of a vertex with same coords, add this one.
408 vertnum_t med_create_duplicate_vertex(const vertex &vp);
409
410 namespace dsx {
411 // Create a new segment, duplicating exactly, including vertex ids and children, the passed segment.
412 segnum_t med_create_duplicate_segment(segment_array &, const segment &sp);
413
414 // Returns the index of a free segment.
415 // Scans the Segments array.
416 segnum_t get_free_segment_number(segment_array &);
417 }
418 #endif
419
420 // Diagnostic message.
421 #define diagnostic_message editor_status
422 #define diagnostic_message_fmt editor_status_fmt
423
424 // Editor status message.
425 void editor_status_fmt(const char *) = delete;
426 extern void editor_status_fmt(const char *format, ... ) dxx_compiler_attribute_format_printf(1, 2);
427
428 // Variables in editor.c that the k*.c files need
429
430 #define UF_NONE 0x000 //nothing has changed
431 #define UF_WORLD_CHANGED 0x001 //something added or deleted
432 #define UF_VIEWPOINT_MOVED 0x002 //what we're watching has moved
433
434 #define UF_GAME_VIEW_CHANGED 0x004 //the game window changed
435 #define UF_ED_STATE_CHANGED 0x008 //something like curside,curseg changed
436
437 #define UF_ALL 0xffffffff //all flags
438
439 extern uint Update_flags;
440 extern int Funky_chase_mode;
441 extern vms_angvec Seg_orientation;
442 extern int mine_changed;
443 extern int ModeFlag;
444 extern editor_view *current_view;
445
446 //the view for the different windows
447 extern editor_view LargeView;
448 extern editor_view TopView;
449 extern editor_view FrontView;
450 extern editor_view RightView;
451
452 extern int SafetyCheck();
453 #ifdef DXX_BUILD_DESCENT
454 namespace dsx {
455 int save_mine_data_compiled(PHYSFS_File *SaveFile);
456
457 }
458 #endif
459 void editor_status( const char *text);
460
461 extern int MacroNumEvents;
462 extern int MacroStatus;
463
464 extern int Lock_view_to_cursegp; // !0 means whenever cursegp changes, view it
465
466 // eglobal.c
467 extern int Num_tilings; // number of tilings/wall
468 extern int Degenerate_segment_found;
469
470 namespace dcx {
471
472 #ifdef DXX_BUILD_DESCENT
473 [[nodiscard]]
474 vms_matrix med_extract_matrix_from_segment(const shared_segment &sp);
475
476 // Returns true if vertex vi is contained in exactly one segment, else returns false.
477 int is_free_vertex(const fvcsegptr &vcsegptr, vertnum_t vi);
478 #endif
479
480 // Initializes autosave system.
481 // Sets global Autosave_count to 0.
482 extern void init_autosave(void);
483
484 // Closes autosave system.
485 // Deletes all autosaved files.
486 extern void close_autosave(void);
487
488 // Saves current mine to name.miX where name = suffix of mine name and X = Autosave_count.
489 // For example, if name = "cookie.min", and Autosave_count = 3, then writes "cookie.mi3".
490 // Increments Autosave_count, wrapping from 9 to 0.
491 // (If there is no current mine name, assume "temp.min")
492 // Call med_save_mine to save the mine.
493 void autosave_mine(const std::array<char, PATH_MAX> &name);
494
495 // Timed autosave
496 void TimedAutosave(const std::array<char, PATH_MAX> &name);
497 extern void set_editor_time_of_day();
498
499 // Undo function
500 extern int undo(void);
501 extern std::array<const char *, 10> undo_status;
502
503 #ifdef DXX_BUILD_DESCENT
504 [[nodiscard]]
505 vms_vector med_extract_right_vector_from_segment_side(const shared_segment &sp, sidenum_t sidenum);
506 [[nodiscard]]
507 vms_vector med_extract_up_vector_from_segment_side(const shared_segment &sp, sidenum_t sidenum);
508 #endif
509 }
510
511 // group.c
512 int RotateSegmentNew(vms_angvec *pbh);
513 int rotate_segment_new(const vms_angvec &pbh);
514
515 // The current object type and id declared in eglobal.c
516 extern short Cur_object_type;
517 extern short Cur_object_id;
518
519 // From med.c
520 extern int DisplayCurrentRobotType(void);
521 extern objnum_t Cur_object_index;
522
523 extern int render_3d_in_big_window;
524 extern void move_object_to_mouse_click(void);
525
526 //these are instances of canvases, pointed to by variables below
527 extern grs_subcanvas _canv_editor_game; //the game on the editor screen
528
529 //these are pointers to our canvases
530 extern grs_canvas *Canv_editor; //the editor screen
531 extern grs_subcanvas *const Canv_editor_game; //the game on the editor screen
532
533 struct editor_dialog final : UI_DIALOG
534 {
535 editor_dialog(short x, short y, const short w, const short h, const enum dialog_flags flags, std::array<RAIIPHYSFS_ComputedPathMount, 4> &&search_path_editor);
536 std::array<std::unique_ptr<UI_GADGET_BUTTON>, 9> pad_goto;
537 std::unique_ptr<UI_GADGET_BUTTON>
538 pad_prev,
539 pad_next;
540 std::array<RAIIPHYSFS_ComputedPathMount, 4> search_path_editor;
541 virtual window_event_result callback_handler(const d_event &) override;
542 };
543
544 extern editor_dialog *EditorWindow;
545
546 [[nodiscard]]
547 vms_vector med_point_2_vec(grs_canvas *canv, short sx, short sy);
548
549 //shutdown ui on the editor screen
550 void close_editor_screen(void);
551
552 #ifdef DXX_BUILD_DESCENT
553 namespace dsx {
554 // From eobject.c
555 int place_object(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_polygon_model_state &LevelSharedPolygonModelState, const d_robot_info_array &Robot_info, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, vmsegptridx_t segp, const vms_vector &object_pos, short object_type, uint8_t object_id);
556
557 }
558 #endif
559
560 // In medmisc.c
561 extern void draw_world_from_game(void);
562
563 // In medrobot.c
564 extern void close_all_windows(void);
565
566 // In seguvs.c
567
568 // Amount to stretch a texture map by.
569 // The two different ones are for the two dimensions of a texture map.
570 extern fix Stretch_scale_x, Stretch_scale_y;