Debian Codegraph @

similar/editor/medwall.cpp

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-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Created from version 1.11 of main\wall.c
23 *
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <math.h>
29 #include <string.h>
30 #include "wall.h"
31 #include "editor/medwall.h"
32 #include "inferno.h"
33 #include "editor/editor.h"
34 #include "editor/esegment.h"
35 #include "segment.h"
36 #include "dxxerror.h"
37 #include "event.h"
38 #include "game.h"
39 #include "gameseg.h"
40 #include "textures.h"
41 #include "switch.h"
42 #include "editor/eswitch.h"
43 #include "texmerge.h"
44 #include "timer.h"
45 #include "cntrlcen.h"
46 #include "key.h"
47 #include "centers.h"
48 #include "piggy.h"
49 #include "kdefs.h"
50 #include "u_mem.h"
51 #include "d_enumerate.h"
52
53 #include "compiler-range_for.h"
54 #include "d_bitset.h"
55 #include "d_range.h"
56 #include "partial_range.h"
57 #include "d_underlying_value.h"
58 #include "d_zip.h"
59 #include <memory>
60 #include <utility>
61
62 //-------------------------------------------------------------------------
63 // Variables for this module...
64 //-------------------------------------------------------------------------
65
66 namespace {
67
68 struct wall_dialog : UI_DIALOG
69 {
70 using UI_DIALOG::UI_DIALOG;
71 std::unique_ptr<UI_GADGET_USERBOX> wallViewBox;
72 std::unique_ptr<UI_GADGET_BUTTON> quitButton, prev_wall, next_wall, blastable, door, illusory, closed_wall, goto_prev_wall, goto_next_wall, remove, bind_trigger, bind_control;
73 std::array<std::unique_ptr<UI_GADGET_CHECKBOX>, 3> doorFlag;
74 std::array<std::unique_ptr<UI_GADGET_RADIO>, 4> keyFlag;
75 wallnum_t old_wall_num = wall_none; // Set to some dummy value so everything works ok on the first frame.
76 fix64 time;
77 int framenum{0};
78 virtual window_event_result callback_handler(const d_event &) override;
79 };
80
81 static wall_dialog *MainWindow;
82 static int Current_door_type=1;
83
84 struct count_wall
85 {
86 wallnum_t wallnum;
87 segnum_t segnum;
88 sidenum_t sidenum;
89 };
90
91 static unsigned predicate_find_nonblastable_wall(const wclip &w)
92 {
93 if (w.num_frames == wclip_frames_none)
94 return 0;
95 return !(w.flags & WCF_BLASTABLE);
96 }
97
98 static unsigned predicate_find_blastable_wall(const wclip &w)
99 {
100 if (w.num_frames == wclip_frames_none)
101 return 0;
102 return w.flags & WCF_BLASTABLE;
103 }
104
105 static wallnum_t allocate_wall(wall_array &Walls)
106 {
107 using T = typename std::underlying_type<wallnum_t>::type;
108 const auto current_count = Walls.get_count();
109 const wallnum_t r{static_cast<T>(current_count)};
110 static_assert(MAX_WALLS <= std::numeric_limits<T>::max());
111 Walls.set_count(current_count + 1);
112 return r;
113 }
114
115 //---------------------------------------------------------------------
116 // Add a wall (removable 2 sided)
117 static int add_wall(fvcvertptr &vcvertptr, wall_array &Walls, const vmsegptridx_t seg, const sidenum_t side)
118 {
119 if (Walls.get_count() < MAX_WALLS-2)
120 if (IS_CHILD(seg->children[side])) {
121 shared_segment &sseg = seg;
122 auto &side0 = sseg.sides[side];
123 if (side0.wall_num == wall_none) {
124 side0.wall_num = allocate_wall(Walls);
125 }
126
127 const auto &&csegp = seg.absolute_sibling(seg->children[side]);
128 auto Connectside = find_connect_side(seg, csegp);
129
130 shared_segment &scseg = csegp;
131 auto &side1 = scseg.sides[Connectside];
132 if (side1.wall_num == wall_none) {
133 side1.wall_num = allocate_wall(Walls);
134 }
135
136 const auto t1 = build_texture1_value(CurrentTexture);
137 create_removable_wall(vcvertptr, seg, side, t1);
138 create_removable_wall(vcvertptr, csegp, Connectside, t1);
139
140 return 1;
141 }
142
143 return 0;
144 }
145
146 static int wall_assign_door(int door_type)
147 {
148 shared_segment &sseg = Cursegp;
149 unique_segment &useg = Cursegp;
150 if (sseg.sides[Curside].wall_num == wall_none) {
151 editor_status("Cannot assign door. No wall at Curside.");
152 return 0;
153 }
154
155 auto &Walls = LevelUniqueWallSubsystemState.Walls;
156 auto &vmwallptr = Walls.vmptr;
157 auto &wall0 = *vmwallptr(sseg.sides[Curside].wall_num);
158 if (wall0.type != WALL_DOOR && wall0.type != WALL_BLASTABLE)
159 {
160 editor_status("Cannot assign door. No door at Curside.");
161 return 0;
162 }
163
164 Current_door_type = door_type;
165
166 auto &csegp = *vmsegptr(Cursegp->children[Curside]);
167 auto Connectside = find_connect_side(Cursegp, csegp);
168
169 wall0.clip_num = door_type;
170 shared_segment &scseg = csegp;
171 unique_segment &ucseg = csegp;
172 vmwallptr(scseg.sides[Connectside].wall_num)->clip_num = door_type;
173
174 auto &wa = GameSharedState.WallAnims[door_type];
175 auto &side0 = useg.sides[Curside];
176 auto &side1 = ucseg.sides[Connectside];
177 if (wa.flags & WCF_TMAP1) {
178 const auto t1 = build_texture1_value(wa.frames[0]);
179 side0.tmap_num = t1;
180 side1.tmap_num = t1;
181 side0.tmap_num2 = texture2_value::None;
182 side1.tmap_num2 = texture2_value::None;
183 }
184 else {
185 side0.tmap_num2 = side1.tmap_num2 = texture2_value{wa.frames[0]};
186 }
187
188 Update_flags |= UF_WORLD_CHANGED;
189 return 1;
190 }
191
192 static int wall_add_to_side(fvcvertptr &vcvertptr, wall_array &Walls, const vmsegptridx_t segp, sidenum_t side, unsigned type);
193
194 }
195
196 int wall_add_blastable()
197 {
198 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
199 auto &Vertices = LevelSharedVertexState.get_vertices();
200 auto &vcvertptr = Vertices.vcptr;
201 auto &Walls = LevelUniqueWallSubsystemState.Walls;
202 return wall_add_to_side(vcvertptr, Walls, Cursegp, Curside, WALL_BLASTABLE);
203 }
204
205 int wall_add_door()
206 {
207 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
208 auto &Vertices = LevelSharedVertexState.get_vertices();
209 auto &vcvertptr = Vertices.vcptr;
210 auto &Walls = LevelUniqueWallSubsystemState.Walls;
211 return wall_add_to_side(vcvertptr, Walls, Cursegp, Curside, WALL_DOOR);
212 }
213
214 int wall_add_closed_wall()
215 {
216 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
217 auto &Vertices = LevelSharedVertexState.get_vertices();
218 auto &vcvertptr = Vertices.vcptr;
219 auto &Walls = LevelUniqueWallSubsystemState.Walls;
220 return wall_add_to_side(vcvertptr, Walls, Cursegp, Curside, WALL_CLOSED);
221 }
222
223 int wall_add_external_wall()
224 {
225 if (Cursegp->children[Curside] == segment_exit)
226 {
227 editor_status( "Wall is already external!" );
228 return 1;
229 }
230
231 if (IS_CHILD(Cursegp->children[Curside])) {
232 editor_status( "Cannot add external wall here - seg has children" );
233 return 0;
234 }
235 Cursegp->children[Curside] = segment_exit;
236 return 1;
237 }
238
239 int wall_add_illusion()
240 {
241 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
242 auto &Vertices = LevelSharedVertexState.get_vertices();
243 auto &vcvertptr = Vertices.vcptr;
244 auto &Walls = LevelUniqueWallSubsystemState.Walls;
245 return wall_add_to_side(vcvertptr, Walls, Cursegp, Curside, WALL_ILLUSION);
246 }
247
248 namespace {
249
250 static int GotoPrevWall()
251 {
252 shared_segment &sseg = Cursegp;
253 auto &side = sseg.sides[Curside];
254 auto &Walls = LevelUniqueWallSubsystemState.Walls;
255 auto &vcwallptr = Walls.vcptr;
256 const auto wall_count = Walls.get_count();
257 if (!wall_count)
258 /* No walls exist; nothing to do. */
259 return 0;
260 const auto current_wall = side.wall_num == wall_none
261 ? wall_count
262 : underlying_value(side.wall_num);
263 const auto previous_wall = static_cast<wallnum_t>((current_wall ? current_wall : wall_count) - 1u);
264 auto &w = *vcwallptr(previous_wall);
265 if (w.segnum == segment_none)
266 {
267 return 0;
268 }
269
270 if (w.sidenum == side_none)
271 {
272 return 0;
273 }
274
275 Cursegp = imsegptridx(w.segnum);
276 Curside = w.sidenum;
277
278 return 1;
279 }
280
281 static int GotoNextWall() {
282 shared_segment &sseg = Cursegp;
283 auto &side = sseg.sides[Curside];
284 auto &Walls = LevelUniqueWallSubsystemState.Walls;
285 auto &vcwallptr = Walls.vcptr;
286
287 const auto wall_count = Walls.get_count();
288 if (!wall_count)
289 /* No walls exist; nothing to do. */
290 return 0;
291 const auto current_wall = side.wall_num; // It's ok to be -1 because it will immediately become 0
292 using T = typename std::underlying_type<wallnum_t>::type;
293 const wallnum_t next_wall = (current_wall == wall_none || static_cast<T>(current_wall) >= wall_count)
294 ? wallnum_t{0}
295 : static_cast<wallnum_t>(static_cast<T>(current_wall) + 1u);
296 auto &w = *vcwallptr(next_wall);
297 if (w.segnum == segment_none)
298 {
299 return 0;
300 }
301
302 if (w.sidenum == side_none)
303 {
304 return 0;
305 }
306
307 Cursegp = imsegptridx(w.segnum);
308 Curside = w.sidenum;
309
310 return 1;
311 }
312
313 template <typename I>
314 I wraparound_find_if(const I begin, const I start, const I end, auto &&predicate)
315 {
316 for (I iter = start;;)
317 {
318 ++ iter;
319 if (iter == end)
320 iter = begin;
321 if (iter == start)
322 return iter;
323 if (predicate(*iter))
324 return iter;
325 }
326 }
327
328 /*
329 * Given a range defined by [`begin`, `end`), a starting point `start`
330 * that is within that range, and a predicate `predicate`, examine each
331 * element in the range (`start`, `begin`]. If `predicate(*iter)`
332 * returns true, return `iter`. Otherwise, perform the same search on
333 * the range (`end`, `start`). If traversal reaches `start` without
334 * finding such an element, return `start` without calling
335 * `predicate(*start)`.
336 */
337 template <typename I>
338 I wraparound_backward_find_if(const I begin, const I start, const I end, auto &&predicate)
339 {
340 for (I iter = start;;)
341 {
342 if (iter == begin)
343 iter = end;
344 -- iter;
345 if (iter == start)
346 return iter;
347 if (predicate(*iter))
348 return iter;
349 }
350 }
351
352 static int PrevWall() {
353 int wall_type;
354 const auto cur_wall_num = Cursegp->shared_segment::sides[Curside].wall_num;
355 if (cur_wall_num == wall_none)
356 {
357 editor_status("Cannot assign new wall. No wall on curside.");
358 return 0;
359 }
360
361 auto &Walls = LevelUniqueWallSubsystemState.Walls;
362 auto &vcwallptr = Walls.vcptr;
363 auto &w = *vcwallptr(cur_wall_num);
364 wall_type = w.clip_num;
365 auto &WallAnims = GameSharedState.WallAnims;
366
367 const auto b = WallAnims.begin();
368 const auto s = std::next(b, wall_type);
369 const auto e = std::next(b, Num_wall_anims);
370 if (w.type == WALL_DOOR)
371 {
372 auto iter = wraparound_backward_find_if(b, s, e, predicate_find_nonblastable_wall);
373 if (iter == s)
374 throw std::runtime_error("Cannot find clip for door.");
375 wall_type = std::distance(b, iter);
376 }
377 else if (w.type == WALL_BLASTABLE)
378 {
379 auto iter = wraparound_backward_find_if(b, s, e, predicate_find_blastable_wall);
380 if (iter == s)
381 throw std::runtime_error("Cannot find clip for blastable wall.");
382 wall_type = std::distance(b, iter);
383 }
384
385 wall_assign_door(wall_type);
386
387 Update_flags |= UF_WORLD_CHANGED;
388 return 1;
389 }
390
391 static int NextWall() {
392 int wall_type;
393 const auto cur_wall_num = Cursegp->shared_segment::sides[Curside].wall_num;
394 if (cur_wall_num == wall_none)
395 {
396 editor_status("Cannot assign new wall. No wall on curside.");
397 return 0;
398 }
399
400 auto &Walls = LevelUniqueWallSubsystemState.Walls;
401 auto &WallAnims = GameSharedState.WallAnims;
402 auto &vcwallptr = Walls.vcptr;
403 auto &w = *vcwallptr(cur_wall_num);
404 wall_type = w.clip_num;
405
406 const auto b = WallAnims.begin();
407 const auto s = std::next(b, wall_type);
408 const auto e = std::next(b, Num_wall_anims);
409 if (w.type == WALL_DOOR)
410 {
411 auto iter = wraparound_find_if(b, s, e, predicate_find_nonblastable_wall);
412 if (iter == s)
413 throw std::runtime_error("Cannot find clip for door.");
414 wall_type = std::distance(b, iter);
415 }
416 else if (w.type == WALL_BLASTABLE)
417 {
418 auto iter = wraparound_find_if(b, s, e, predicate_find_blastable_wall);
419 if (iter == s)
420 throw std::runtime_error("Cannot find clip for blastable wall.");
421 wall_type = std::distance(b, iter);
422 }
423
424 wall_assign_door(wall_type);
425
426 Update_flags |= UF_WORLD_CHANGED;
427 return 1;
428 }
429
430 }
431
432 //-------------------------------------------------------------------------
433 // Called from the editor... does one instance of the wall dialog box
434 //-------------------------------------------------------------------------
435 int do_wall_dialog()
436 {
437 // Only open 1 instance of this window...
438 if ( MainWindow != NULL ) return 0;
439
440 // Close other windows.
441 close_all_windows();
442
443 // Open a window with a quit button
444 MainWindow = window_create<wall_dialog>(TMAPBOX_X + 20, TMAPBOX_Y + 20, 765 - TMAPBOX_X, 545 - TMAPBOX_Y, DF_DIALOG);
445 return 1;
446 }
447
448 static window_event_result wall_dialog_created(wall_dialog *const wd)
449 {
450 wd->quitButton = ui_add_gadget_button(*wd, 20, 252, 48, 40, "Done", nullptr);
451 // These are the checkboxes for each door flag.
452 int i{80};
453 wd->doorFlag[0] = ui_add_gadget_checkbox(*wd, 22, i, 16, 16, 0, "Locked"); i += 24;
454 wd->doorFlag[1] = ui_add_gadget_checkbox(*wd, 22, i, 16, 16, 0, "Auto"); i += 24;
455 wd->doorFlag[2] = ui_add_gadget_checkbox(*wd, 22, i, 16, 16, 0, "Illusion OFF"); i += 24;
456 wd->keyFlag[0] = ui_add_gadget_radio(*wd, 22, i, 16, 16, 0, "NONE"); i += 24;
457 wd->keyFlag[1] = ui_add_gadget_radio(*wd, 22, i, 16, 16, 0, "Blue"); i += 24;
458 wd->keyFlag[2] = ui_add_gadget_radio(*wd, 22, i, 16, 16, 0, "Red"); i += 24;
459 wd->keyFlag[3] = ui_add_gadget_radio(*wd, 22, i, 16, 16, 0, "Yellow"); i += 24;
460 // The little box the wall will appear in.
461 wd->wallViewBox = ui_add_gadget_userbox(*wd, 155, 5, 64, 64);
462 // A bunch of buttons...
463 i = 80;
464 wd->prev_wall = ui_add_gadget_button(*wd, 155, i, 70, 22, "<< Clip", PrevWall);
465 wd->next_wall = ui_add_gadget_button(*wd, 155+70, i, 70, 22, "Clip >>", NextWall);i += 25;
466 wd->blastable = ui_add_gadget_button(*wd, 155, i, 140, 22, "Add Blastable", wall_add_blastable); i += 25;
467 wd->door = ui_add_gadget_button(*wd, 155, i, 140, 22, "Add Door", wall_add_door ); i += 25;
468 wd->illusory = ui_add_gadget_button(*wd, 155, i, 140, 22, "Add Illusory", wall_add_illusion); i += 25;
469 wd->closed_wall = ui_add_gadget_button(*wd, 155, i, 140, 22, "Add Closed Wall", wall_add_closed_wall); i+=25;
470 wd->goto_prev_wall = ui_add_gadget_button(*wd, 155, i, 70, 22, "<< Prev", GotoPrevWall);
471 wd->goto_next_wall = ui_add_gadget_button(*wd, 155+70, i, 70, 22, "Next >>", GotoNextWall);i += 25;
472 wd->remove = ui_add_gadget_button(*wd, 155, i, 140, 22, "Remove Wall", wall_remove); i += 25;
473 wd->bind_trigger = ui_add_gadget_button(*wd, 155, i, 140, 22, "Bind to Trigger", bind_wall_to_trigger); i += 25;
474 wd->bind_control = ui_add_gadget_button(*wd, 155, i, 140, 22, "Bind to Control", bind_wall_to_control_center); i+=25;
475
476 return window_event_result::handled;
477 }
478
479 void close_wall_window()
480 {
481 if (MainWindow)
482 ui_close_dialog(*std::exchange(MainWindow, nullptr));
483 }
484
485 window_event_result wall_dialog::callback_handler(const d_event &event)
486 {
487 switch(event.type)
488 {
489 case event_type::window_created:
490 return wall_dialog_created(this);
491 case event_type::window_close:
492 MainWindow = nullptr;
493 return window_event_result::ignored;
494 default:
495 break;
496 }
497 fix DeltaTime;
498 fix64 Temp;
499 int keypress{0};
500 window_event_result rval = window_event_result::ignored;
501 if (event.type == event_type::key_command)
502 keypress = event_key_get(event);
503 Assert(MainWindow != NULL);
504
505 //------------------------------------------------------------
506 // Call the ui code..
507 //------------------------------------------------------------
508 ui_button_any_drawn = 0;
509
510 auto &Walls = LevelUniqueWallSubsystemState.Walls;
511 auto &WallAnims = GameSharedState.WallAnims;
512 auto &imwallptridx = Walls.imptridx;
513 const auto &&w = imwallptridx(Cursegp->shared_segment::sides[Curside].wall_num);
514 //------------------------------------------------------------
515 // If we change walls, we need to reset the ui code for all
516 // of the checkboxes that control the wall flags.
517 //------------------------------------------------------------
518 if (w)
519 {
520 if (old_wall_num != w)
521 {
522 auto &current_wall = w.get_checked_reference();
523 const auto wall_flags = current_wall.flags;
524 const auto wall_keys = current_wall.keys;
525 ui_checkbox_check(doorFlag[0].get(), wall_flags & wall_flag::door_locked);
526 ui_checkbox_check(doorFlag[1].get(), wall_flags & wall_flag::door_auto);
527 ui_checkbox_check(doorFlag[2].get(), wall_flags & wall_flag::illusion_off);
528
529 ui_radio_set_value(*keyFlag[0], wall_keys & wall_key::none);
530 ui_radio_set_value(*keyFlag[1], wall_keys & wall_key::blue);
531 ui_radio_set_value(*keyFlag[2], wall_keys & wall_key::red);
532 ui_radio_set_value(*keyFlag[3], wall_keys & wall_key::gold);
533 }
534 }
535
536 //------------------------------------------------------------
537 // If any of the checkboxes that control the wallflags are set, then
538 // update the corresponding wall flag.
539 //------------------------------------------------------------
540
541 if (const auto wp = w.get_unchecked_pointer(); wp && wp->type == WALL_DOOR)
542 {
543 if (GADGET_PRESSED(doorFlag[0].get()))
544 {
545 if (doorFlag[0]->flag == 1)
546 wp->flags |= wall_flag::door_locked;
547 else
548 wp->flags &= ~wall_flag::door_locked;
549 rval = window_event_result::handled;
550 }
551 else if (GADGET_PRESSED(doorFlag[1].get()))
552 {
553 if (doorFlag[1]->flag == 1)
554 wp->flags |= wall_flag::door_auto;
555 else
556 wp->flags &= ~wall_flag::door_auto;
557 rval = window_event_result::handled;
558 }
559
560 //------------------------------------------------------------
561 // If any of the radio buttons that control the mode are set, then
562 // update the corresponding key.
563 //------------------------------------------------------------
564 range_for (const int i, xrange(4u)) {
565 if (GADGET_PRESSED(keyFlag[i].get()))
566 {
567 wp->keys = static_cast<wall_key>(1 << i);
568 rval = window_event_result::handled;
569 }
570 }
571 } else {
572 range_for (auto &i, partial_const_range(doorFlag, 2u))
573 ui_checkbox_check(i.get(), 0);
574 range_for (auto &i, keyFlag)
575 ui_radio_set_value(*i, 0);
576 }
577
578 if (const auto wp = w.get_unchecked_pointer(); wp && wp->type == WALL_ILLUSION)
579 {
580 if (GADGET_PRESSED(doorFlag[2].get()))
581 {
582 auto &wf = wp->flags;
583 if (doorFlag[2]->flag == 1)
584 wf |= wall_flag::illusion_off;
585 else
586 wf &= ~wall_flag::illusion_off;
587 rval = window_event_result::handled;
588 }
589 } else
590 doorFlag[2]->flag = 0; // Tells ui that this button isn't checked
591
592 //------------------------------------------------------------
593 // Draw the wall in the little 64x64 box
594 //------------------------------------------------------------
595 if (event.type == event_type::ui_dialog_draw)
596 {
597 // A simple frame time counter for animating the walls...
598 Temp = timer_query();
599 DeltaTime = Temp - time;
600
601 gr_set_current_canvas(wallViewBox->canvas);
602 if (const auto wp = w.get_unchecked_pointer())
603 {
604 const auto type = wp->type;
605 if ((type == WALL_DOOR) || (type == WALL_BLASTABLE)) {
606 if (DeltaTime > ((F1_0*200)/1000)) {
607 framenum++;
608 time = Temp;
609 }
610 auto &wa = WallAnims[wp->clip_num];
611 if (framenum >= wa.num_frames)
612 framenum=0;
613 const auto frame = wa.frames[framenum];
614 const auto texture = Textures[frame];
615 PIGGY_PAGE_IN(texture);
616 gr_ubitmap(*grd_curcanv, GameBitmaps[texture]);
617 } else {
618 if (type == WALL_OPEN)
619 gr_clear_canvas(*grd_curcanv, CBLACK);
620 else {
621 auto &curside = Cursegp->unique_segment::sides[Curside];
622 const auto tmap_num = curside.tmap_num;
623 if (curside.tmap_num2 != texture2_value::None)
624 gr_ubitmap(*grd_curcanv, texmerge_get_cached_bitmap(tmap_num, curside.tmap_num2));
625 else {
626 const auto texture1 = Textures[get_texture_index(tmap_num)];
627 PIGGY_PAGE_IN(texture1);
628 gr_ubitmap(*grd_curcanv, GameBitmaps[texture1]);
629 }
630 }
631 }
632 } else
633 gr_clear_canvas(*grd_curcanv, CGREY);
634 }
635
636 //------------------------------------------------------------
637 // If anything changes in the ui system, redraw all the text that
638 // identifies this wall.
639 //------------------------------------------------------------
640 if (event.type == event_type::ui_dialog_draw)
641 {
642 if (const auto wp = w.get_unchecked_pointer())
643 {
644 ui_dprintf_at( MainWindow, 12, 6, "Wall: %hu ", underlying_value(wallnum_t{w}));
645 switch (wp->type)
646 {
647 case WALL_NORMAL:
648 ui_dputs_at(MainWindow, 12, 23, " Type: Normal");
649 break;
650 case WALL_BLASTABLE:
651 ui_dputs_at(MainWindow, 12, 23, " Type: Blastable");
652 break;
653 case WALL_DOOR:
654 ui_dputs_at(MainWindow, 12, 23, " Type: Door ");
655 ui_dputs_at( MainWindow, 223, 6, &WallAnims[wp->clip_num].filename[0]);
656 break;
657 case WALL_ILLUSION:
658 ui_dputs_at(MainWindow, 12, 23, " Type: Illusion ");
659 break;
660 case WALL_OPEN:
661 ui_dputs_at(MainWindow, 12, 23, " Type: Open ");
662 break;
663 case WALL_CLOSED:
664 ui_dputs_at(MainWindow, 12, 23, " Type: Closed ");
665 break;
666 default:
667 ui_dputs_at(MainWindow, 12, 23, " Type: Unknown ");
668 break;
669 }
670 if (wp->type != WALL_DOOR)
671 ui_dputs_at(MainWindow, 223, 6, " ");
672
673 ui_dprintf_at( MainWindow, 12, 40, " Clip: %d ", wp->clip_num );
674 ui_dprintf_at(MainWindow, 12, 57, " Trigger: %d ", underlying_value(wp->trigger));
675 } else {
676 ui_dputs_at(MainWindow, 12, 6, "Wall: none ");
677 ui_dputs_at(MainWindow, 12, 23, " Type: none ");
678 ui_dputs_at(MainWindow, 12, 40, " Clip: none ");
679 ui_dputs_at(MainWindow, 12, 57, " Trigger: none ");
680 }
681 }
682
683 if (ui_button_any_drawn || old_wall_num != w)
684 Update_flags |= UF_WORLD_CHANGED;
685 if (GADGET_PRESSED(quitButton.get()) || keypress == KEY_ESC)
686 {
687 return window_event_result::close;
688 }
689
690 old_wall_num = w;
691 return rval;
692 }
693
694
695 //---------------------------------------------------------------------
696
697 // Restore all walls to original status (closed doors, repaired walls)
698 int wall_restore_all()
699 {
700 auto &Walls = LevelUniqueWallSubsystemState.Walls;
701 auto &WallAnims = GameSharedState.WallAnims;
702 auto &vcwallptr = Walls.vcptr;
703 auto &vmwallptr = Walls.vmptr;
704 for (auto &w : vmwallptr)
705 {
706 if (w.flags & wall_flag::blasted)
707 {
708 w.hps = WALL_HPS;
709 }
710 w.flags &= ~(wall_flag::blasted | wall_flag::door_opened | wall_flag::exploding);
711 }
712
713 auto &ActiveDoors = LevelUniqueWallSubsystemState.ActiveDoors;
714 for (auto &i : ActiveDoors.vmptr)
715 wall_close_door_ref(Segments.vmptridx, Walls, WallAnims, i);
716
717 for (csmusegment i : vmsegptr)
718 for (auto &&[ss, us] : zip(i.s.sides, i.u.sides))
719 {
720 const auto wall_num = ss.wall_num;
721 if (wall_num != wall_none)
722 {
723 auto &w = *vcwallptr(wall_num);
724 if (w.type == WALL_BLASTABLE || w.type == WALL_DOOR)
725 us.tmap_num2 = texture2_value{WallAnims[w.clip_num].frames[0]};
726 }
727 }
728
729 #if DXX_BUILD_DESCENT == 2
730 auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
731 auto &vmtrgptr = Triggers.vmptr;
732 for (auto &i : vmtrgptr)
733 i.flags &= ~trigger_behavior_flags::disabled;
734 #endif
735 Update_flags |= UF_GAME_VIEW_CHANGED;
736
737 return 1;
738 }
739
740 //---------------------------------------------------------------------
741 // Remove a specific side.
742 int wall_remove_side(const vmsegptridx_t seg, const sidenum_t side)
743 {
744 const shared_segment &sseg = seg;
745 const auto seg_child = sseg.children[side];
746 if (IS_CHILD(seg_child) && sseg.sides[side].wall_num != wall_none)
747 {
748 shared_segment &csegp = *vmsegptr(seg_child);
749 const auto Connectside = find_connect_side(seg, csegp);
750
751 remove_trigger(seg, side);
752 remove_trigger(csegp, Connectside);
753
754 // Remove walls 'wall_num' and connecting side 'wall_num'
755 // from Walls array.
756 const auto wall0 = seg->shared_segment::sides[side].wall_num;
757 const auto wall1 = csegp.sides[Connectside].wall_num;
758 const auto lower_wallnum = (wall0 < wall1) ? wall0 : wall1;
759
760 auto &Walls = LevelUniqueWallSubsystemState.Walls;
761 auto &vcwallptr = Walls.vcptr;
762 auto &vmwallptr = Walls.vmptr;
763 {
764 const auto linked_wall = vcwallptr(lower_wallnum)->linked_wall;
765 if (linked_wall != wall_none)
766 vmwallptr(linked_wall)->linked_wall = wall_none;
767 }
768 const wallnum_t upper_wallnum = static_cast<wallnum_t>(static_cast<unsigned>(lower_wallnum) + 1u);
769 {
770 const auto linked_wall = vcwallptr(upper_wallnum)->linked_wall;
771 if (linked_wall != wall_none)
772 vmwallptr(linked_wall)->linked_wall = wall_none;
773 }
774
775 {
776 const std::size_t num_walls{Walls.get_count()};
777 auto &&sr = partial_const_range(Walls, static_cast<unsigned>(lower_wallnum) + 2, num_walls);
778 std::move(sr.begin(), sr.end(), partial_range(Walls, static_cast<unsigned>(lower_wallnum), num_walls - 2).begin());
779 Walls.set_count(num_walls - 2);
780 }
781
782 for (auto &seg : vmsegptr)
783 {
784 if (seg.segnum != segment_none)
785 for (auto &w : seg.shared_segment::sides)
786 if (w.wall_num != wall_none && w.wall_num > upper_wallnum)
787 w.wall_num = static_cast<wallnum_t>(static_cast<unsigned>(w.wall_num) - 2u);
788 }
789
790 // Destroy any links to the deleted wall.
791 auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
792 auto &vmtrgptr = Triggers.vmptr;
793 for (auto &t : vmtrgptr)
794 {
795 for (int l=0;l < t.num_links;l++)
796 if (t.seg[l] == seg && t.side[l] == side) {
797 for (int t1=0;t1 < t.num_links-1;t1++) {
798 t.seg[t1] = t.seg[t1+1];
799 t.side[t1] = t.side[t1+1];
800 }
801 t.num_links--;
802 }
803 }
804
805 // Destroy control center links as well.
806 for (int l=0;l<ControlCenterTriggers.num_links;l++)
807 if (ControlCenterTriggers.seg[l] == seg && ControlCenterTriggers.side[l] == side) {
808 for (int t1=0;t1<ControlCenterTriggers.num_links-1;t1++) {
809 ControlCenterTriggers.seg[t1] = ControlCenterTriggers.seg[t1+1];
810 ControlCenterTriggers.side[t1] = ControlCenterTriggers.side[t1+1];
811 }
812 ControlCenterTriggers.num_links--;
813 }
814
815 seg->shared_segment::sides[side].wall_num = wall_none;
816 csegp.sides[Connectside].wall_num = wall_none;
817
818 Update_flags |= UF_WORLD_CHANGED;
819 return 1;
820 }
821
822 editor_status( "Can't remove wall. No wall present.");
823 return 0;
824 }
825
826 //---------------------------------------------------------------------
827 // Remove a special wall.
828 int wall_remove()
829 {
830 return wall_remove_side(Cursegp, Curside);
831 }
832
833 namespace {
834
835 //---------------------------------------------------------------------
836 // Add a wall to curside
837 static int wall_add_to_side(fvcvertptr &vcvertptr, wall_array &Walls, const vmsegptridx_t segp, const sidenum_t side, const unsigned type)
838 {
839 if (add_wall(vcvertptr, Walls, segp, side)) {
840 const auto &&csegp = segp.absolute_sibling(segp->children[side]);
841 auto connectside = find_connect_side(segp, csegp);
842
843 auto &vmwallptr = Walls.vmptr;
844 auto &w0 = *vmwallptr(segp->shared_segment::sides[side].wall_num);
845 auto &w1 = *vmwallptr(csegp->shared_segment::sides[connectside].wall_num);
846 w0.segnum = segp;
847 w1.segnum = csegp;
848
849 w0.sidenum = side;
850 w1.sidenum = connectside;
851
852 w0.flags = {};
853 w1.flags = {};
854
855 w0.type = type;
856 w1.type = type;
857
858 w0.clip_num = -1;
859 w1.clip_num = -1;
860
861 w0.keys = wall_key::none;
862 w1.keys = wall_key::none;
863
864 if (type == WALL_BLASTABLE) {
865 w0.hps = WALL_HPS;
866 w1.hps = WALL_HPS;
867 }
868
869 if (type != WALL_DOOR) {
870 segp->unique_segment::sides[side].tmap_num2 = texture2_value::None;
871 csegp->unique_segment::sides[connectside].tmap_num2 = texture2_value::None;
872 }
873
874 if (type == WALL_DOOR) {
875 w0.flags |= wall_flag::door_auto;
876 w1.flags |= wall_flag::door_auto;
877
878 w0.clip_num = Current_door_type;
879 w1.clip_num = Current_door_type;
880 }
881
882 //Update_flags |= UF_WORLD_CHANGED;
883 //return 1;
884
885 // return NextWall(); //assign a clip num
886 return wall_assign_door(Current_door_type);
887
888 } else {
889 editor_status( "Cannot add wall here, no children" );
890 return 0;
891 }
892 }
893
894 }
895
896 //---------------------------------------------------------------------
897 // Add a wall to markedside
898 int wall_add_to_markedside(fvcvertptr &vcvertptr, wall_array &Walls, const int8_t type)
899 {
900 if (add_wall(vcvertptr, Walls, Markedsegp, Markedside)) {
901 const auto &&csegp = vmsegptridx(Markedsegp->children[Markedside]);
902 auto Connectside = find_connect_side(Markedsegp, csegp);
903
904 const auto wall_num = Markedsegp->shared_segment::sides[Markedside].wall_num;
905 const auto cwall_num = csegp->shared_segment::sides[Connectside].wall_num;
906 auto &vmwallptr = Walls.vmptr;
907 auto &w0 = *vmwallptr(wall_num);
908 auto &w1 = *vmwallptr(cwall_num);
909
910 w0.segnum = Markedsegp;
911 w1.segnum = csegp;
912
913 w0.sidenum = Markedside;
914 w1.sidenum = Connectside;
915
916 w0.flags = {};
917 w1.flags = {};
918
919 w0.type = type;
920 w1.type = type;
921
922 w0.trigger = trigger_none;
923 w1.trigger = trigger_none;
924
925 w0.clip_num = -1;
926 w1.clip_num = -1;
927
928 w0.keys = wall_key::none;
929 w1.keys = wall_key::none;
930
931 if (type == WALL_BLASTABLE) {
932 w0.hps = WALL_HPS;
933 w1.hps = WALL_HPS;
934
935 w0.clip_num = 0;
936 w1.clip_num = 0;
937 }
938
939 if (type != WALL_DOOR) {
940 Markedsegp->unique_segment::sides[Markedside].tmap_num2 = texture2_value::None;
941 csegp->unique_segment::sides[Connectside].tmap_num2 = texture2_value::None;
942 }
943
944 Update_flags |= UF_WORLD_CHANGED;
945 return 1;
946 } else {
947 editor_status( "Cannot add wall here, no children" );
948 return 0;
949 }
950 }
951
952 int bind_wall_to_control_center() {
953
954 int link_num;
955 if (Cursegp->shared_segment::sides[Curside].wall_num == wall_none) {
956 editor_status("No wall at Curside.");
957 return 0;
958 }
959
960 link_num = ControlCenterTriggers.num_links;
961 for (int i=0;i<link_num;i++)
962 if (Cursegp == ControlCenterTriggers.seg[i] && Curside == ControlCenterTriggers.side[i])
963 {
964 editor_status("Curside already bound to Control Center.");
965 return 0;
966 }
967
968 // Error checking completed, actual binding begins
969 ControlCenterTriggers.seg[link_num] = Cursegp;
970 ControlCenterTriggers.side[link_num] = Curside;
971 ControlCenterTriggers.num_links++;
972
973 editor_status("Wall linked to control center");
974
975 return 1;
976 }
977
978 //link two doors, curseg/curside and markedseg/markedside
979 int wall_link_doors()
980 {
981 const auto cwall_num = Cursegp->shared_segment::sides[Curside].wall_num;
982 auto &Walls = LevelUniqueWallSubsystemState.Walls;
983 auto &imwallptr = Walls.imptr;
984 const auto &&w1 = imwallptr(cwall_num);
985
986 if (!w1 || w1->type != WALL_DOOR) {
987 editor_status("Curseg/curside is not a door");
988 return 0;
989 }
990
991 if (!Markedsegp) {
992 editor_status("No marked side.");
993 return 0;
994 }
995
996 const auto mwall_num = Markedsegp->shared_segment::sides[Markedside].wall_num;
997 const auto &&w2 = imwallptr(mwall_num);
998
999 if (!w2 || w2->type != WALL_DOOR) {
1000 editor_status("Markedseg/markedside is not a door");
1001 return 0;
1002 }
1003
1004 if (w1->linked_wall != wall_none)
1005 editor_status("Curseg/curside is already linked");
1006
1007 if (w2->linked_wall != wall_none)
1008 editor_status("Markedseg/markedside is already linked");
1009
1010 w1->linked_wall = Markedsegp->shared_segment::sides[Markedside].wall_num;
1011 w2->linked_wall = Cursegp->shared_segment::sides[Curside].wall_num;
1012
1013 return 1;
1014 }
1015
1016 int wall_unlink_door()
1017 {
1018 const auto cwall_num = Cursegp->shared_segment::sides[Curside].wall_num;
1019 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1020 auto &imwallptr = Walls.imptr;
1021 const auto &&w1 = imwallptr(cwall_num);
1022
1023 if (!w1 || w1->type != WALL_DOOR) {
1024 editor_status("Curseg/curside is not a door");
1025 return 0;
1026 }
1027
1028 if (w1->linked_wall == wall_none)
1029 {
1030 editor_status("Curseg/curside is not linked");
1031 return 0;
1032 }
1033
1034 auto &vmwallptr = Walls.vmptr;
1035 auto &w2 = *vmwallptr(w1->linked_wall);
1036 Assert(w2.linked_wall == cwall_num);
1037
1038 w2.linked_wall = wall_none;
1039 w1->linked_wall = wall_none;
1040
1041 return 1;
1042
1043 }
1044
1045 int check_walls()
1046 {
1047 auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters;
1048 std::array<count_wall, MAX_WALLS> CountedWalls;
1049
1050 unsigned wall_count{0};
1051 range_for (const auto &&segp, vmsegptridx)
1052 {
1053 if (segp->segnum != segment_none) {
1054 // Check fuelcenters
1055 const auto matcen_num = segp->matcen_num;
1056 if (matcen_num == materialization_center_number{0u})
1057 if (RobotCenters[matcen_num].segnum != segp) {
1058 segp->matcen_num = materialization_center_number::None;
1059 }
1060
1061 if (matcen_num != materialization_center_number::None)
1062 RobotCenters[matcen_num].segnum = segp;
1063
1064 for (auto &&[idx, s] : enumerate(segp->shared_segment::sides))
1065 {
1066 if (s.wall_num != wall_none) {
1067 CountedWalls[wall_count].wallnum = s.wall_num;
1068 CountedWalls[wall_count].segnum = segp;
1069 CountedWalls[wall_count].sidenum = idx;
1070 wall_count++;
1071 }
1072 }
1073 }
1074 }
1075
1076 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1077 if (wall_count != Walls.get_count()) {
1078 if (ui_messagebox(-2, -2, 2, "Num_walls is bogus\nDo you wish to correct it?\n", "Yes", "No") == 1)
1079 {
1080 Walls.set_count(wall_count);
1081 editor_status_fmt("Num_walls set to %d\n", Walls.get_count());
1082 }
1083 }
1084
1085 // Check validity of Walls array.
1086 auto &vmwallptr = Walls.vmptr;
1087 range_for (auto &cw, partial_const_range(CountedWalls, Walls.get_count()))
1088 {
1089 auto &w = *vmwallptr(cw.wallnum);
1090 if (w.segnum != cw.segnum || w.sidenum != cw.sidenum)
1091 {
1092 if (ui_messagebox( -2, -2, 2, "Unmatched wall detected\nDo you wish to correct it?\n", "Yes", "No") == 1)
1093 {
1094 w.segnum = cw.segnum;
1095 w.sidenum = cw.sidenum;
1096 }
1097 }
1098 }
1099
1100 const auto &&used_walls = partial_const_range(Walls, wall_count);
1101 const auto predicate = [](const wall &w) {
1102 return w.trigger != trigger_none;
1103 };
1104 unsigned trigger_count = std::count_if(used_walls.begin(), used_walls.end(), predicate);
1105
1106 auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
1107 if (trigger_count != Triggers.get_count()) {
1108 if (ui_messagebox(-2, -2, 2, "Num_triggers is bogus\nDo you wish to correct it?\n", "Yes", "No") == 1)
1109 {
1110 Triggers.set_count(trigger_count);
1111 editor_status_fmt("Num_triggers set to %d\n", Triggers.get_count());
1112 }
1113 }
1114
1115 return 1;
1116
1117 }
1118
1119
1120 int delete_all_walls()
1121 {
1122 if (ui_messagebox(-2, -2, 2, "Are you sure that walls are hosed so\n badly that you want them ALL GONE!?\n", "YES!", "No") == 1)
1123 {
1124 range_for (shared_segment &segp, vmsegptr)
1125 {
1126 range_for (auto &side, segp.sides)
1127 side.wall_num = wall_none;
1128 }
1129 auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
1130 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1131 Walls.set_count(0);
1132 Triggers.set_count(0);
1133
1134 return 1;
1135 }
1136
1137 return 0;
1138 }
1139
1140 namespace {
1141
1142 // ------------------------------------------------------------------------------------------------
1143 static void copy_old_wall_data_to_new(wallnum_t owall, wallnum_t nwall)
1144 {
1145 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1146 auto &o = *Walls.vcptr(owall);
1147 auto &n = *Walls.vmptr(nwall);
1148 n.flags = o.flags;
1149 n.type = o.type;
1150 n.clip_num = o.clip_num;
1151 n.keys = o.keys;
1152 n.hps = o.hps;
1153 n.state = o.state;
1154 n.linked_wall = wall_none;
1155
1156 n.trigger = trigger_none;
1157 if (o.trigger != trigger_none)
1158 {
1159 editor_status("Warning: Trigger not copied in group copy.");
1160 }
1161 }
1162
1163 }
1164
1165 // ------------------------------------------------------------------------------------------------
1166 void copy_group_walls(int old_group, int new_group)
1167 {
1168 group::segment_array_type_t::const_iterator bn = GroupList[new_group].segments.begin();
1169
1170 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1171 auto &vmwallptr = Walls.vmptr;
1172 range_for (const auto old_seg, GroupList[old_group].segments)
1173 {
1174 const auto new_seg = *bn++;
1175 auto &os = vcsegptr(old_seg)->shared_segment::sides;
1176 auto &ns = vmsegptr(new_seg)->shared_segment::sides;
1177 for (const auto j : MAX_SIDES_PER_SEGMENT)
1178 {
1179 if (os[j].wall_num != wall_none) {
1180 const auto nw = static_cast<wallnum_t>(Walls.get_count());
1181 ns[j].wall_num = nw;
1182 copy_old_wall_data_to_new(os[j].wall_num, nw);
1183 auto &w = *vmwallptr(nw);
1184 w.segnum = new_seg;
1185 w.sidenum = j;
1186 Walls.set_count(Walls.get_count() + 1);
1187 Assert(Walls.get_count() < MAX_WALLS);
1188 }
1189 }
1190 }
1191 }
1192
1193 static int Validate_walls=1;
1194
1195 // --------------------------------------------------------------------------------------------------------
1196 // This function should be in medwall.c.
1197 // Make sure all wall/segment connections are valid.
1198 void check_wall_validity(void)
1199 {
1200 if (!Validate_walls)
1201 return;
1202
1203 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1204 auto &vcwallptr = Walls.vcptr;
1205 for (auto &&wi : Walls.vcptridx)
1206 {
1207 auto &w{*wi};
1208 const segnum_t segnum{w.segnum};
1209 const auto sidenum{w.sidenum};
1210
1211 if (vcsegptr(segnum)->shared_segment::sides[sidenum].wall_num != wi)
1212 {
1213 if (!Validate_walls)
1214 return;
1215 Int3(); // Error! Your mine has been invalidated!
1216 // Do not continue! Do not save!
1217 // Remember your last action and Contact Mike!
1218 // To continue, set the variable Validate_walls to 1 by doing:
1219 // /Validate_walls = 1
1220 // Then do the usual /eip++;g
1221
1222 }
1223 }
1224
1225 enumerated_bitset<MAX_WALLS, wallnum_t> wall_flags{};
1226
1227 range_for (const auto &&segp, vmsegptridx)
1228 {
1229 if (segp->segnum != segment_none)
1230 for (const auto j : MAX_SIDES_PER_SEGMENT)
1231 {
1232 // Check walls
1233 auto wall_num = segp->shared_segment::sides[j].wall_num;
1234 if (wall_num != wall_none) {
1235 auto &&wf = wall_flags[wall_num];
1236 if (wf)
1237 {
1238 if (!Validate_walls)
1239 return;
1240 Int3(); // Error! Your mine has been invalidated!
1241 // Do not continue! Do not save!
1242 // Remember your last action and Contact Mike!
1243 // To continue, set the variable Validate_walls to 1 by doing:
1244 // /Validate_walls = 1
1245 // Then do the usual /eip++;g
1246 }
1247
1248 auto &w = *vcwallptr(wall_num);
1249 if (w.segnum != segp || w.sidenum != j)
1250 {
1251 if (!Validate_walls)
1252 return;
1253 Int3(); // Error! Your mine has been invalidated!
1254 // Do not continue! Do not save!
1255 // Remember your last action and Contact Mike!
1256 // To continue, set the variable Validate_walls to 1 by doing:
1257 // /Validate_walls = 1
1258 // Then do the usual /eip++;g
1259 }
1260 wf = true;
1261 }
1262 }
1263 }
1264 }
1265