Debian Codegraph @

similar/editor/meddraw.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 * Med drawing functions.
23 *
24 */
25
26 #include <algorithm>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include "inferno.h"
32 #include "segment.h"
33 #include "segpoint.h"
34 #include "gameseg.h"
35 #include "common/3d/globvars.h"
36 #include "gr.h"
37 #include "ui.h"
38 #include "editor/editor.h"
39 #include "editor/esegment.h"
40 #include "wall.h"
41 #include "switch.h"
42 #include "dxxerror.h"
43 #include "u_mem.h"
44 #include "render.h"
45 #include "func.h"
46 #include "textures.h"
47 #include "object.h"
48 #include "meddraw.h"
49 #include "d_enumerate.h"
50 #include "d_levelstate.h"
51 #include "d_range.h"
52 #include "compiler-range_for.h"
53 #include "partial_range.h"
54 #include "segiter.h"
55 #include "d_zip.h"
56
57 #if DXX_USE_OGL
58 #include "ogl_init.h"
59 #endif
60
61 using std::min;
62
63 // Colors used in editor for indicating various kinds of segments.
64 #define SELECT_COLOR BM_XRGB( 63/2 , 41/2 , 0/2)
65 #define FOUND_COLOR BM_XRGB( 0/2 , 30/2 , 45/2)
66 #define WARNING_COLOR BM_XRGB( 63/2 , 0/2 , 0/2)
67 #define AXIS_COLOR BM_XRGB( 63/2 , 0/2 , 63/2)
68 #define PLAINSEG_COLOR BM_XRGB( 45/2 , 45/2 , 45/2)
69 #define MARKEDSEG_COLOR BM_XRGB( 0/2 , 63/2 , 0/2)
70 #define MARKEDSIDE_COLOR BM_XRGB( 0/2 , 63/2 , 63/2)
71 #define CURSEG_COLOR BM_XRGB( 63/2 , 63/2 , 63/2)
72 #define CURSIDE_COLOR BM_XRGB( 63/2 , 63/2 , 0/2)
73 #define CUREDGE_COLOR BM_XRGB( 0 , 63/2 , 0 )
74 #define GROUPSEG_COLOR BM_XRGB( 0/2 , 0/2 , 63/2)
75 #define GROUPSIDE_COLOR BM_XRGB( 63/2 , 0/2 , 45/2)
76 #define GROUP_COLOR BM_XRGB( 0/2 , 45/2 , 0/2)
77 #define ROBOT_COLOR BM_XRGB( 31 , 0 , 0 )
78 #define PLAYER_COLOR BM_XRGB( 0 , 0 , 31 )
79
80 constexpr std::integral_constant<unsigned, MAX_VERTICES * 4> MAX_EDGES{};
81
82 namespace {
83
84 enum class packed_edge : uint8_t
85 {
86 };
87
88 static int Search_mode=0; //if true, searching for segments at given x,y
89 static int Search_x,Search_y;
90 static int Automap_test=0; // Set to 1 to show wireframe in automap mode.
91
92 static void draw_seg_objects(grs_canvas &canvas, const unique_segment &seg)
93 {
94 auto &Objects = LevelUniqueObjectState.Objects;
95 auto &vcobjptridx = Objects.vcptridx;
96 const g3_instance_context viewer_context{View_matrix, View_position};
97 range_for (const auto obj, objects_in(seg, vcobjptridx, vcsegptr))
98 {
99 const uint8_t color = (obj->type == OBJ_PLAYER && static_cast<icobjptridx_t::index_type>(obj) > 0)
100 ? BM_XRGB(0, 25, 0)
101 : (obj == ConsoleObject
102 ? PLAYER_COLOR
103 : ROBOT_COLOR
104 );
105 g3_draw_sphere(canvas, /* sphere_point = */ g3_draw_sphere_point{viewer_context, obj->pos}, obj->size, color);
106 }
107 }
108
109 static void draw_line(const g3_draw_line_context &context, const vertnum_t pnum0, const vertnum_t pnum1)
110 {
111 g3_draw_line(context, Segment_points[pnum0], Segment_points[pnum1]);
112 }
113
114 // ----------------------------------------------------------------------------
115 static void draw_segment(const g3_draw_line_context &context, const shared_segment &seg)
116 {
117 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
118 auto &Vertices = LevelSharedVertexState.get_vertices();
119 if (seg.segnum == segment_none) //this segment doesn't exitst
120 return;
121
122 auto &svp = seg.verts;
123 auto &vcvertptr = Vertices.vcptr;
124 if (rotate_list(vcvertptr, svp).uand == clipping_code::None)
125 { //all off screen?
126 for (const uint8_t i : xrange(4u))
127 draw_line(context, svp[(segment_relative_vertnum{i})], svp[(segment_relative_vertnum{static_cast<uint8_t>(i + 4)})]);
128
129 for (const uint8_t i : xrange(3u))
130 {
131 draw_line(context, svp[(segment_relative_vertnum{i})], svp[(segment_relative_vertnum{static_cast<uint8_t>(i + 1)})]);
132 draw_line(context, svp[(segment_relative_vertnum{static_cast<uint8_t>(i + 4)})], svp[(segment_relative_vertnum{static_cast<uint8_t>(i + 4 + 1)})]);
133 }
134
135 draw_line(context, svp[segment_relative_vertnum::_0], svp[segment_relative_vertnum::_3]);
136 draw_line(context, svp[segment_relative_vertnum::_4], svp[segment_relative_vertnum::_7]);
137 }
138 }
139
140 //for looking for segment under a mouse click
141 static void check_segment(const vmsegptridx_t seg)
142 {
143 auto &svp = seg->verts;
144 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
145 auto &Vertices = LevelSharedVertexState.get_vertices();
146 auto &vcvertptr = Vertices.vcptr;
147 if (rotate_list(vcvertptr, svp).uand == clipping_code::None)
148 { //all off screen?
149 #if DXX_USE_OGL
150 g3_end_frame();
151 #endif
152 {
153 uint8_t color{0};
154 gr_pixel(grd_curcanv->cv_bitmap, Search_x, Search_y, color); //set our search pixel to color zero
155 }
156 #if DXX_USE_OGL
157 g3_start_frame(*grd_curcanv);
158 #endif
159 {
160 const uint8_t color = 1;
161 //and render in color one
162
163 range_for (auto &fn, Side_to_verts)
164 {
165 std::array<g3_draw_tmap_point *, 3> vert_list{{
166 &Segment_points[seg->verts[fn[side_relative_vertnum::_0]]],
167 &Segment_points[seg->verts[fn[side_relative_vertnum::_1]]],
168 &Segment_points[seg->verts[fn[side_relative_vertnum::_2]]],
169 }};
170 g3_check_and_draw_poly(*grd_curcanv, vert_list, color);
171
172 vert_list[1] = vert_list[2];
173 vert_list[2] = &Segment_points[seg->verts[fn[side_relative_vertnum::_3]]];
174 g3_check_and_draw_poly(*grd_curcanv, vert_list, color);
175 }
176 }
177
178 if (gr_ugpixel(grd_curcanv->cv_bitmap,Search_x,Search_y) == 1)
179 {
180 Found_segs.emplace_back(seg);
181 }
182 }
183 }
184
185 // ----------------------------------------------------------------------------
186 static void draw_seg_side(const shared_segment &seg, const sidenum_t side, const color_palette_index color)
187 {
188 auto &svp = seg.verts;
189 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
190 auto &Vertices = LevelSharedVertexState.get_vertices();
191 auto &vcvertptr = Vertices.vcptr;
192 if (rotate_list(vcvertptr, svp).uand == clipping_code::None)
193 { //all off screen?
194 auto &stv = Side_to_verts[side];
195 g3_draw_line_context context{*grd_curcanv, color};
196 for (const auto i : MAX_VERTICES_PER_SIDE)
197 draw_line(context, svp[stv[i]], svp[stv[next_side_vertex(i)]]);
198 }
199 }
200
201 static void draw_side_edge(const shared_segment &seg, const sidenum_t side, const side_relative_vertnum edge, const color_palette_index color)
202 {
203 auto &svp = seg.verts;
204 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
205 auto &Vertices = LevelSharedVertexState.get_vertices();
206 auto &vcvertptr = Vertices.vcptr;
207 if (rotate_list(vcvertptr, svp).uand == clipping_code::None) //on screen?
208 {
209 auto &stv = Side_to_verts[side];
210 draw_line(g3_draw_line_context{*grd_curcanv, color}, svp[stv[edge]], svp[stv[next_side_vertex(edge)]]);
211 }
212 }
213
214 __attribute__((used))
215 int Show_triangulations{0};
216
217 //edge types - lower number types have precedence
218 #define ET_FACING 0 //this edge on a facing face
219 #define ET_NOTFACING 1 //this edge on a non-facing face
220 #define ET_NOTUSED 2 //no face uses this edge
221 #define ET_NOTEXTANT 3 //would exist if side were triangulated
222
223 #define ET_EMPTY 255 //this entry in array is empty
224
225 static
226 #if DXX_BUILD_DESCENT == 1
227 constexpr
228 #endif
229 std::array<color_palette_index, 3> edge_colors{{54, 59, 64}};
230
231 struct seg_edge
232 {
233 vertnum_t v0, v1;
234 ushort type;
235 ubyte face_count, backface_count;
236 };
237
238 static std::array<seg_edge, MAX_EDGES> edge_list;
239 static std::array<int, MAX_EDGES> used_list; //which entries in edge_list have been used
240 static int n_used;
241
242 static unsigned edge_list_size; //set each frame
243
244 static constexpr packed_edge pack_edge(const unsigned a, const unsigned b)
245 {
246 return static_cast<packed_edge>((a << 3) | b);
247 }
248
249 //define edge numberings
250 constexpr std::array<packed_edge, 24> edges = {{
251 pack_edge(0, 1), // edge 0
252 pack_edge(0, 3), // edge 1
253 pack_edge(0, 4), // edge 2
254 pack_edge(1, 2), // edge 3
255 pack_edge(1, 5), // edge 4
256 pack_edge(2, 3), // edge 5
257 pack_edge(2, 6), // edge 6
258 pack_edge(3, 7), // edge 7
259 pack_edge(4, 5), // edge 8
260 pack_edge(4, 7), // edge 9
261 pack_edge(5, 6), // edge 10
262 pack_edge(6, 7), // edge 11
263
264 pack_edge(0, 5), // right cross
265 pack_edge(0, 7), // top cross
266 pack_edge(1, 3), // front cross
267 pack_edge(2, 5), // bottom cross
268 pack_edge(2, 7), // left cross
269 pack_edge(4, 6), // back cross
270
271 //crosses going the other way
272
273 pack_edge(1, 4), // other right cross
274 pack_edge(3, 4), // other top cross
275 pack_edge(0, 2), // other front cross
276 pack_edge(1, 6), // other bottom cross
277 pack_edge(3, 6), // other left cross
278 pack_edge(5, 7), // other back cross
279 }};
280
281 #define N_NORMAL_EDGES 12 //the normal edges of a box
282 #define N_EXTRA_EDGES 12 //ones created by triangulation
283 #define N_EDGES_PER_SEGMENT (N_NORMAL_EDGES+N_EXTRA_EDGES)
284
285 //given two vertex numbers on a segment (range 0..7), tell what edge number it is
286 static std::size_t find_edge_num(const segment_relative_vertnum ev0, const segment_relative_vertnum ev1)
287 {
288 const auto &&[v0, v1] = std::minmax(ev0, ev1);
289 const auto vv = pack_edge(static_cast<uint8_t>(v0), static_cast<uint8_t>(v1));
290 const auto iter = std::find(edges.begin(), edges.end(), vv);
291 return std::distance(edges.begin(), iter);
292 }
293
294 //finds edge, filling in edge_ptr. if found old edge, returns index, else return -1
295 static std::pair<seg_edge &, std::size_t> find_edge(const vertnum_t v0, const vertnum_t v1)
296 {
297 const auto &&hash_object = std::hash<vertnum_t>{};
298 const auto initial_hash_slot = (hash_object(v0) ^ (hash_object(v1) << 10)) % edge_list_size;
299
300 for (auto current_hash_slot = initial_hash_slot;;)
301 {
302 auto &e = edge_list[current_hash_slot];
303 if (e.type == ET_EMPTY)
304 return {e, UINT32_MAX};
305 else if (e.v0 == v0 && e.v1 == v1)
306 return {e, current_hash_slot};
307 else {
308 if (++ current_hash_slot == edge_list_size)
309 current_hash_slot = 0;
310 if (current_hash_slot == initial_hash_slot)
311 throw std::runtime_error("edge list full: search wrapped without finding a free slot");
312 }
313 }
314 }
315
316 //adds an edge to the edge list
317 static void add_edge(const vertnum_t ev0, const vertnum_t ev1, const uint8_t type)
318 {
319 const auto &&[v0, v1] = std::minmax(ev0, ev1);
320 auto &&[e, current_hash_slot] = find_edge(v0, v1);
321
322 if (current_hash_slot == UINT32_MAX)
323 {
324 e.v0 = v0;
325 e.v1 = v1;
326 e.type = type;
327 used_list[n_used] = &e - edge_list.begin();
328 if (type == ET_FACING)
329 e.face_count++;
330 else if (type == ET_NOTFACING)
331 e.backface_count++;
332 n_used++;
333 } else {
334 if (e.type > type)
335 e.type = type;
336 if (type == ET_FACING)
337 e.face_count++;
338 else if (type == ET_NOTFACING)
339 e.backface_count++;
340 }
341 }
342
343 //adds a segment's edges to the edge list
344 static void add_edges(const shared_segment &seg)
345 {
346 auto &svp = seg.verts;
347 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
348 auto &Vertices = LevelSharedVertexState.get_vertices();
349 auto &vcvertptr = Vertices.vcptr;
350 if (rotate_list(vcvertptr, svp).uand == clipping_code::None)
351 { //all off screen?
352 int i,fn,vn;
353 int flag;
354 std::array<uint8_t, std::size(edges)> edge_flags;
355 for (i=0;i<N_NORMAL_EDGES;i++) edge_flags[i]=ET_NOTUSED;
356 for (;i<N_EDGES_PER_SEGMENT;i++) edge_flags[i]=ET_NOTEXTANT;
357
358 for (auto &&[idx, sidep] : enumerate(seg.sides))
359 {
360 const auto &&vertex_list = create_all_vertex_lists(seg, sidep, idx);
361 const unsigned num_vertices{vertex_list.side_type == vertex_array_side_type::quad ? 4u : 3u};
362
363 const unsigned num_faces{vertex_list.side_type == vertex_array_side_type::quad ? 1u : 2u};
364 for (fn=0; fn<num_faces; fn++) {
365 //Note: normal check appears to be the wrong way since the normals points in, but we're looking from the outside
366 if (g3_check_normal_facing(vcvertptr(seg.verts[vertex_list[fn*3]]), sidep.normals[fn]))
367 flag = ET_NOTFACING;
368 else
369 flag = ET_FACING;
370
371 auto v0 = &vertex_list[fn*3];
372 for (vn=0; vn<num_vertices-1; vn++) {
373 if (const auto en = find_edge_num(*v0, *(v0 + 1)); en != edges.size())
374 if (flag < edge_flags[en]) edge_flags[en] = flag;
375
376 v0++;
377 }
378 if (const auto en = find_edge_num(*v0, vertex_list[fn * 3]); en != edges.size())
379 if (flag < edge_flags[en]) edge_flags[en] = flag;
380 }
381 }
382
383 for (i=0; i<N_EDGES_PER_SEGMENT; i++)
384 if (i<N_NORMAL_EDGES || (edge_flags[i]!=ET_NOTEXTANT && Show_triangulations))
385 {
386 const uint8_t e0 = static_cast<uint8_t>(edges[i]) >> 3;
387 const uint8_t e1 = static_cast<uint8_t>(edges[i]) & 7;
388 add_edge(seg.verts[(segment_relative_vertnum{e0})], seg.verts[(segment_relative_vertnum{e1})], edge_flags[i]);
389 }
390 }
391 }
392
393 // ----------------------------------------------------------------------------
394 static void draw_trigger_side(const shared_segment &seg, const sidenum_t side, const color_palette_index color)
395 {
396 auto &svp = seg.verts;
397 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
398 auto &Vertices = LevelSharedVertexState.get_vertices();
399 auto &vcvertptr = Vertices.vcptr;
400 if (rotate_list(vcvertptr, svp).uand == clipping_code::None)
401 { //all off screen?
402 // Draw diagonals
403 auto &stv = Side_to_verts[side];
404 draw_line(g3_draw_line_context{*grd_curcanv, color}, svp[stv[side_relative_vertnum::_0]], svp[stv[side_relative_vertnum::_2]]);
405 }
406 }
407
408 // ----------------------------------------------------------------------------
409 static void draw_wall_side(const shared_segment &seg, const sidenum_t side, const color_palette_index color)
410 {
411 auto &svp = seg.verts;
412 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
413 auto &Vertices = LevelSharedVertexState.get_vertices();
414 auto &vcvertptr = Vertices.vcptr;
415 if (rotate_list(vcvertptr, svp).uand == clipping_code::None)
416 { //all off screen?
417 // Draw diagonals
418 g3_draw_line_context context{*grd_curcanv, color};
419 auto &stv = Side_to_verts[side];
420 draw_line(context, svp[stv[side_relative_vertnum::_0]], svp[stv[side_relative_vertnum::_2]]);
421 draw_line(context, svp[stv[side_relative_vertnum::_1]], svp[stv[side_relative_vertnum::_3]]);
422 }
423 }
424
425 #define WALL_BLASTABLE_COLOR rgb_t{31, 0, 0} // RED
426 #define WALL_DOOR_COLOR rgb_t{0, 0, 31} // DARK BLUE
427 #define WALL_DOOR_LOCKED_COLOR rgb_t{0, 0, 63} // BLUE
428 #define WALL_AUTO_DOOR_COLOR rgb_t{0, 31, 0} // DARK GREEN
429 #define WALL_AUTO_DOOR_LOCKED_COLOR rgb_t{0, 63, 0} // GREEN
430 #define WALL_ILLUSION_COLOR rgb_t{63, 0, 63} // PURPLE
431
432 #define TRIGGER_COLOR BM_XRGB( 63/2 , 63/2 , 0/2) // YELLOW
433
434 // ----------------------------------------------------------------------------------------------------------------
435 // Draws special walls (for now these are just removable walls.)
436 static void draw_special_wall(const shared_segment &seg, const sidenum_t side)
437 {
438 auto &Walls = LevelUniqueWallSubsystemState.Walls;
439 auto &vcwallptr = Walls.vcptr;
440 auto &w = *vcwallptr(seg.sides[side].wall_num);
441 const auto get_color = [=]() {
442 const auto type{w.type};
443 if (type != WALL_OPEN)
444 {
445 const auto flags{w.flags};
446 if (flags & wall_flag::door_locked)
447 return (flags & wall_flag::door_auto) ? WALL_AUTO_DOOR_LOCKED_COLOR : WALL_DOOR_LOCKED_COLOR;
448 if (flags & wall_flag::door_auto)
449 return WALL_AUTO_DOOR_COLOR;
450 if (type == WALL_BLASTABLE)
451 return WALL_BLASTABLE_COLOR;
452 if (type == WALL_DOOR)
453 return WALL_DOOR_COLOR;
454 if (type == WALL_ILLUSION)
455 return WALL_ILLUSION_COLOR;
456 }
457 return rgb_t{45, 45, 45};
458 };
459 const auto color = get_color();
460 draw_wall_side(seg,side, gr_find_closest_color(color.r, color.g, color.b));
461
462 if (w.trigger != trigger_none)
463 {
464 draw_trigger_side(seg,side, TRIGGER_COLOR);
465 }
466 }
467
468
469 // ----------------------------------------------------------------------------------------------------------------
470 // Recursively parse mine structure, drawing segments.
471 static void draw_mine_sub(const vmsegptridx_t segnum,int depth, visited_segment_bitarray_t &visited)
472 {
473 if (visited[segnum]) return; // If segment already drawn, return.
474 visited[segnum] = true; // Say that this segment has been drawn.
475 auto mine_ptr = segnum;
476 // If this segment is active, process it, else skip it.
477
478 if (mine_ptr->segnum != segment_none) {
479
480 if (Search_mode) check_segment(mine_ptr);
481 else add_edges(mine_ptr); //add this segments edges to list
482
483 if (depth != 0) {
484 const shared_segment &sseg = *mine_ptr;
485 for (const auto &&[idx, child_segnum, sside] : enumerate(zip(sseg.children, sseg.sides)))
486 {
487 if (IS_CHILD(child_segnum))
488 {
489 if (sside.wall_num != wall_none)
490 draw_special_wall(mine_ptr, static_cast<sidenum_t>(idx));
491 draw_mine_sub(segnum.absolute_sibling(child_segnum), depth-1, visited);
492 }
493 }
494 }
495 }
496 }
497
498 static void draw_mine_edges(int automap_flag)
499 {
500 int i,type;
501 seg_edge *e;
502
503 for (type=ET_NOTUSED;type>=ET_FACING;type--) {
504 const auto color = edge_colors[type];
505 g3_draw_line_context context{*grd_curcanv, color};
506 for (i=0;i<n_used;i++) {
507 e = &edge_list[used_list[i]];
508 if (e->type == type)
509 if ((!automap_flag) || (e->face_count == 1))
510 draw_line(context, e->v0, e->v1);
511 }
512 }
513 }
514
515 static void clear_edge_list()
516 {
517 range_for (auto &i, partial_range(edge_list, edge_list_size))
518 {
519 i.type = ET_EMPTY;
520 i.face_count = 0;
521 i.backface_count = 0;
522 }
523 }
524
525 //draws an entire mine
526 static void draw_mine(const vmsegptridx_t mine_ptr,int depth)
527 {
528 visited_segment_bitarray_t visited;
529
530 edge_list_size = min(LevelSharedSegmentState.Num_segments * 12, MAX_EDGES.value); //make maybe smaller than max
531
532 // clear edge list
533 clear_edge_list();
534
535 n_used = 0;
536
537 draw_mine_sub(mine_ptr,depth, visited);
538
539 draw_mine_edges(0);
540
541 }
542
543 // -----------------------------------------------------------------------------
544 // Draw all segments, ignoring connectivity.
545 // A segment is drawn if its segnum != -1.
546 static void draw_mine_all(int automap_flag)
547 {
548 edge_list_size = min(LevelSharedSegmentState.Num_segments * 12, MAX_EDGES.value); //make maybe smaller than max
549
550 // clear edge list
551 clear_edge_list();
552
553 n_used = 0;
554
555 range_for (const auto &&segp, vmsegptridx)
556 {
557 if (segp->segnum != segment_none)
558 {
559 for (auto &&[idx, value] : enumerate(segp->shared_segment::sides))
560 if (value.wall_num != wall_none)
561 draw_special_wall(segp, idx);
562 if (Search_mode)
563 check_segment(segp);
564 else {
565 add_edges(segp);
566 draw_seg_objects(*grd_curcanv, segp);
567 }
568 }
569 }
570
571 draw_mine_edges(automap_flag);
572
573 }
574
575 static void draw_listed_segments(g3_draw_line_context &context, count_segment_array_t &s)
576 {
577 range_for (const auto &ss, s)
578 {
579 const auto &&segp = vcsegptr(ss);
580 if (segp->segnum != segment_none)
581 draw_segment(context, segp);
582 }
583 }
584
585 static void draw_selected_segments(void)
586 {
587 g3_draw_line_context context{*grd_curcanv, SELECT_COLOR};
588 draw_listed_segments(context, Selected_segs);
589 }
590
591 static void draw_found_segments(void)
592 {
593 g3_draw_line_context context{*grd_curcanv, FOUND_COLOR};
594 draw_listed_segments(context, Found_segs);
595 }
596
597 static void draw_warning_segments(void)
598 {
599 g3_draw_line_context context{*grd_curcanv, WARNING_COLOR};
600 draw_listed_segments(context, Warning_segs);
601 }
602
603 static void draw_group_segments(void)
604 {
605 if (current_group > -1)
606 {
607 g3_draw_line_context context{*grd_curcanv, GROUP_COLOR};
608 draw_listed_segments(context, GroupList[current_group].segments);
609 }
610 }
611
612
613 static void draw_special_segments(void)
614 {
615 // Highlight matcens, fuelcens, etc.
616 for (auto &seg : vcsegptr)
617 {
618 if (seg.segnum != segment_none)
619 {
620 unsigned r, g, b;
621 switch (seg.special)
622 {
623 case segment_special::fuelcen:
624 r = 29 * 2, g = 27 * 2, b = 13 * 2;
625 break;
626 case segment_special::controlcen:
627 r = 29 * 2, g = 0, b = 0;
628 break;
629 case segment_special::robotmaker:
630 r = 29 * 2, g = 0, b = 31 * 2;
631 break;
632 default:
633 continue;
634 }
635 draw_segment(g3_draw_line_context{*grd_curcanv, /* color = */ gr_find_closest_color(r, g, b)}, seg);
636 }
637 }
638 }
639
640
641 //find a free vertex. returns the vertex number
642 static vertnum_t alloc_vert()
643 {
644 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
645
646 const auto Num_vertices = LevelSharedVertexState.Num_vertices;
647 assert(Num_vertices < MAX_SEGMENT_VERTICES);
648
649 auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
650 vertnum_t vn{};
651 for (; static_cast<unsigned>(vn) < Num_vertices && Vertex_active[vn]; ++vn)
652 {
653 }
654
655 Vertex_active[vn] = 1;
656
657 ++LevelSharedVertexState.Num_vertices;
658
659 return vn;
660 }
661
662 //frees a vertex
663 static void free_vert(vertnum_t vert_num)
664 {
665 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
666 auto &Vertex_active = LevelSharedVertexState.get_vertex_active();
667 Vertex_active[vert_num] = 0;
668 --LevelSharedVertexState.Num_vertices;
669 }
670
671 // -----------------------------------------------------------------------------
672 static void draw_coordinate_axes(void)
673 {
674 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
675 auto &Vertices = LevelSharedVertexState.get_vertices();
676 std::array<vertnum_t, 16> Axes_verts;
677 vms_vector tvec;
678
679 range_for (auto &i, Axes_verts)
680 i = alloc_vert();
681
682 create_coordinate_axes_from_segment(Cursegp,Axes_verts);
683
684 auto &vcvertptr = Vertices.vcptr;
685 auto &vmvertptr = Vertices.vmptr;
686 const auto &&av0 = vcvertptr(Axes_verts[0]);
687 const auto &&av1 = vcvertptr(Axes_verts[1]);
688 const auto &&av2 = vcvertptr(Axes_verts[2]);
689 const auto &&av3 = vcvertptr(Axes_verts[3]);
690 const auto &&av4 = vmvertptr(Axes_verts[4]);
691 const auto &&av6 = vmvertptr(Axes_verts[6]);
692 const auto &&av9 = vmvertptr(Axes_verts[9]);
693 const auto &&av10 = vmvertptr(Axes_verts[10]);
694 const auto &&av11 = vmvertptr(Axes_verts[11]);
695 const auto &&av12 = vmvertptr(Axes_verts[12]);
696 const auto &&av14 = vmvertptr(Axes_verts[14]);
697 const auto &&xvec{vm_vec_build_sub(av1, av0)};
698 const auto &&yvec{vm_vec_build_sub(av2, av0)};
699 const auto &&zvec{vm_vec_build_sub(av3, av0)};
700
701 // Create the letter X
702 tvec = xvec;
703 vm_vec_add(av4, av1, vm_vec_scale(tvec, F1_0 / 16));
704 tvec = yvec;
705 vm_vec_add2(av4, vm_vec_scale(tvec, F1_0 / 8));
706 vm_vec_sub(av6, av4, vm_vec_scale(tvec, F2_0));
707 tvec = xvec;
708 vm_vec_scale(tvec, F1_0 / 8);
709 vm_vec_add(vmvertptr(Axes_verts[7]), av4, tvec);
710 vm_vec_add(vmvertptr(Axes_verts[5]), av6, tvec);
711
712 // Create the letter Y
713 tvec = yvec;
714 vm_vec_add(av11, av2, vm_vec_scale(tvec, F1_0 / 16));
715 vm_vec_add(vmvertptr(Axes_verts[8]), av11, tvec);
716 vm_vec_add(av9, av11, vm_vec_scale(tvec, F1_0 * 2));
717 vm_vec_add(av10, av11, tvec);
718 tvec = xvec;
719 vm_vec_scale(tvec, F1_0 / 16);
720 vm_vec_sub2(av9, tvec);
721 vm_vec_add2(av10, tvec);
722
723 // Create the letter Z
724 tvec = zvec;
725 vm_vec_add(av12, av3, vm_vec_scale(tvec, F1_0 / 16));
726 tvec = yvec;
727 vm_vec_add2(av12, vm_vec_scale(tvec, F1_0 / 8));
728 vm_vec_sub(av14, av12, vm_vec_scale(tvec, F2_0));
729 tvec = zvec;
730 vm_vec_scale(tvec, F1_0 / 8);
731 vm_vec_add(vmvertptr(Axes_verts[13]), av12, tvec);
732 vm_vec_add(vmvertptr(Axes_verts[15]), av14, tvec);
733
734 rotate_list(vcvertptr, Axes_verts);
735
736 const color_palette_index color = AXIS_COLOR;
737 g3_draw_line_context context{*grd_curcanv, color};
738
739 draw_line(context, Axes_verts[0], Axes_verts[1]);
740 draw_line(context, Axes_verts[0], Axes_verts[2]);
741 draw_line(context, Axes_verts[0], Axes_verts[3]);
742
743 // draw the letter X
744 draw_line(context, Axes_verts[4], Axes_verts[5]);
745 draw_line(context, Axes_verts[6], Axes_verts[7]);
746
747 // draw the letter Y
748 draw_line(context, Axes_verts[8], Axes_verts[9]);
749 draw_line(context, Axes_verts[8], Axes_verts[10]);
750 draw_line(context, Axes_verts[8], Axes_verts[11]);
751
752 // draw the letter Z
753 draw_line(context, Axes_verts[12], Axes_verts[13]);
754 draw_line(context, Axes_verts[13], Axes_verts[14]);
755 draw_line(context, Axes_verts[14], Axes_verts[15]);
756
757 range_for (auto &i, Axes_verts)
758 free_vert(i);
759 }
760
761 }
762
763 void draw_world(grs_canvas *screen_canvas,editor_view *v,const vmsegptridx_t mine_ptr,int depth)
764 {
765 vms_vector viewer_position;
766
767 gr_set_current_canvas(screen_canvas);
768
769 viewer_position = v->ev_matrix.fvec;
770 vm_vec_scale(viewer_position,-v->ev_dist);
771
772 vm_vec_add2(viewer_position,Ed_view_target);
773
774 gr_clear_canvas(*grd_curcanv, 0);
775 g3_start_frame(*grd_curcanv);
776 g3_set_view_matrix(viewer_position,v->ev_matrix,v->ev_zoom);
777
778 render_start_frame();
779
780 // Draw all segments or only connected segments.
781 // We might want to draw all segments if we have broken the mine into pieces.
782 if (Draw_all_segments)
783 draw_mine_all(Automap_test);
784 else
785 draw_mine(mine_ptr,depth);
786
787 // Draw the found segments
788 if (!Automap_test) {
789 draw_warning_segments();
790 draw_group_segments();
791 draw_found_segments();
792 draw_selected_segments();
793 draw_special_segments();
794
795 // Highlight group segment and side.
796 if (current_group > -1)
797 if (Groupsegp[current_group]) {
798 draw_segment(g3_draw_line_context{*grd_curcanv, GROUPSEG_COLOR}, vcsegptr(Groupsegp[current_group]));
799 draw_seg_side(vcsegptr(Groupsegp[current_group]), Groupside[current_group], GROUPSIDE_COLOR);
800 }
801
802 // Highlight marked segment and side.
803 if (Markedsegp) {
804 draw_segment(g3_draw_line_context{*grd_curcanv, MARKEDSEG_COLOR}, Markedsegp);
805 draw_seg_side(Markedsegp,Markedside, MARKEDSIDE_COLOR);
806 }
807
808 // Highlight current segment and current side.
809 draw_segment(g3_draw_line_context{*grd_curcanv, CURSEG_COLOR}, Cursegp);
810
811 draw_seg_side(Cursegp,Curside, CURSIDE_COLOR);
812 draw_side_edge(Cursegp,Curside,Curedge, CUREDGE_COLOR);
813
814 // Draw coordinate axes if we are rendering the large view.
815 if (Show_axes_flag)
816 if (screen_canvas == LargeViewBox->canvas.get())
817 draw_coordinate_axes();
818
819 // Label the window
820 gr_set_fontcolor(*grd_curcanv, (v==current_view)?CRED:CWHITE, -1);
821 if ( screen_canvas == LargeViewBox->canvas.get() ) {
822 gr_ustring(*grd_curcanv, *grd_curcanv->cv_font, 5, 5, "USER VIEW");
823 switch (Large_view_index) {
824 case 0: gr_ustring(*grd_curcanv, *grd_curcanv->cv_font, 85, 5, "-- TOP"); break;
825 case 1: gr_ustring(*grd_curcanv, *grd_curcanv->cv_font, 85, 5, "-- FRONT"); break;
826 case 2: gr_ustring(*grd_curcanv, *grd_curcanv->cv_font, 85, 5, "-- RIGHT"); break;
827 }
828 } else
829 Error("Ortho views have been removed, what gives?\n");
830
831 }
832
833 g3_end_frame();
834
835 }
836
837 //find the segments that render at a given screen x,y
838 //parms other than x,y are like draw_world
839 //fills in globals N_found_segs & Found_segs
840 void find_segments(short x,short y,grs_canvas *screen_canvas,editor_view *v,const vmsegptridx_t mine_ptr,int depth)
841 {
842 vms_vector viewer_position;
843
844 gr_set_current_canvas(screen_canvas);
845
846 viewer_position = v->ev_matrix.fvec;
847 vm_vec_scale(viewer_position,-v->ev_dist);
848
849 vm_vec_add2(viewer_position,Ed_view_target);
850
851 g3_start_frame(*grd_curcanv);
852 g3_set_view_matrix(viewer_position,v->ev_matrix,v->ev_zoom);
853
854 render_start_frame();
855
856 #if DXX_USE_OGL
857 g3_end_frame();
858 #endif
859 uint8_t color{0};
860 gr_pixel(grd_curcanv->cv_bitmap, x, y, color); //set our search pixel to color zero
861 #if DXX_USE_OGL
862 g3_start_frame(*grd_curcanv);
863 #endif
864
865 Search_mode = -1;
866 Found_segs.clear();
867 Search_x = x; Search_y = y;
868
869 if (Draw_all_segments)
870 draw_mine_all(0);
871 else
872 draw_mine(mine_ptr,depth);
873
874 g3_end_frame();
875
876 Search_mode = 0;
877
878 }
879
880 namespace dsx {
881 void meddraw_init_views( grs_canvas * canvas)
882 {
883 #if DXX_BUILD_DESCENT == 2
884 // sticking these here so the correct D2 colors are used
885 edge_colors[0] = BM_XRGB(45/2,45/2,45/2);
886 edge_colors[1] = BM_XRGB(45/3,45/3,45/3);
887 edge_colors[2] = BM_XRGB(45/4,45/4,45/4);
888 #endif
889
890 Views[0]->ev_canv = canvas;
891 }
892 }