| 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 |
* Interrogation functions for segment data structure. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <math.h> |
| 29 |
#include <string.h> |
| 30 |
#include <ranges> |
| 31 |
#include "gr.h" |
| 32 |
#include "inferno.h" |
| 33 |
#include "segment.h" |
| 34 |
#include "editor.h" |
| 35 |
#include "editor/esegment.h" |
| 36 |
#include "dxxerror.h" |
| 37 |
#include "object.h" |
| 38 |
#include "gameseg.h" |
| 39 |
#include "render.h" |
| 40 |
#include "wall.h" |
| 41 |
#include "switch.h" |
| 42 |
#include "fuelcen.h" |
| 43 |
#include "seguvs.h" |
| 44 |
#include "kdefs.h" |
| 45 |
|
| 46 |
#include "medwall.h" |
| 47 |
|
| 48 |
#include "compiler-poison.h" |
| 49 |
#include "compiler-range_for.h" |
| 50 |
#include "d_levelstate.h" |
| 51 |
#include "d_range.h" |
| 52 |
#include "d_enumerate.h" |
| 53 |
#include "d_zip.h" |
| 54 |
#include "partial_range.h" |
| 55 |
#include "segiter.h" |
| 56 |
|
| 57 |
int Do_duplicate_vertex_check{0}; // Gets set to 1 in med_create_duplicate_vertex, means to check for duplicate vertices in compress_mine |
| 58 |
|
| 59 |
// Remap all vertices in polygons in a segment through translation table xlate_verts. |
| 60 |
int ToggleBottom(void) |
| 61 |
{ |
| 62 |
Render_only_bottom = !Render_only_bottom; |
| 63 |
Update_flags = UF_WORLD_CHANGED; |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
namespace dcx { |
| 68 |
|
| 69 |
// ------------------------------------------------------------------------------- |
| 70 |
// This function can be used to determine whether a vertex is used exactly once in |
| 71 |
// all segments, in which case it can be freely moved because it is not connected |
| 72 |
// to any other segment. |
| 73 |
// ------------------------------------------------------------------------------- |
| 74 |
int is_free_vertex(const fvcsegptr &vcsegptr, const vertnum_t vi) |
| 75 |
{ |
| 76 |
unsigned count{0}; |
| 77 |
for (const shared_segment &s : vcsegptr) |
| 78 |
{ |
| 79 |
auto sp = &s; |
| 80 |
if (sp->segnum != segment_none) |
| 81 |
range_for (auto &v, s.verts) |
| 82 |
if (v == vi) |
| 83 |
{ |
| 84 |
if (++ count > 1) |
| 85 |
return 0; |
| 86 |
} |
| 87 |
} |
| 88 |
return count; |
| 89 |
} |
| 90 |
|
| 91 |
} |
| 92 |
|
| 93 |
namespace { |
| 94 |
|
| 95 |
// ------------------------------------------------------------------------------- |
| 96 |
// Return true if one fixed point number is very close to another, else return false. |
| 97 |
static int fnear(fix f1, fix f2) |
| 98 |
{ |
| 99 |
return (abs(f1 - f2) <= FIX_EPSILON); |
| 100 |
} |
| 101 |
|
| 102 |
// ------------------------------------------------------------------------------- |
| 103 |
static int vnear(const vms_vector &vp1, const vms_vector &vp2) |
| 104 |
{ |
| 105 |
return fnear(vp1.x, vp2.x) && fnear(vp1.y, vp2.y) && fnear(vp1.z, vp2.z); |
| 106 |
} |
| 107 |
|
| 108 |
static void maintain_vertex_count(valptridx<vertex>::array_managed_type &Vertices, const vertnum_t v) |
| 109 |
{ |
| 110 |
const unsigned u = static_cast<unsigned>(v) + 1; |
| 111 |
if (Vertices.get_count() < u) |
| 112 |
Vertices.set_count(u); |
| 113 |
} |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
// ------------------------------------------------------------------------------- |
| 118 |
// Add the vertex *vp to the global list of vertices, return its index. |
| 119 |
// Search until a matching vertex is found (has nearly the same coordinates) or until Num_vertices |
| 120 |
// vertices have been looked at without a match. If no match, add a new vertex. |
| 121 |
vertnum_t med_add_vertex(const vertex &vp) |
| 122 |
{ |
| 123 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 124 |
int count; // number of used vertices found, for loops exits when count == Num_vertices |
| 125 |
|
| 126 |
// set_vertex_counts(); |
| 127 |
|
| 128 |
const auto Num_vertices = LevelSharedVertexState.Num_vertices; |
| 129 |
Assert(Num_vertices < MAX_SEGMENT_VERTICES); |
| 130 |
|
| 131 |
count = 0; |
| 132 |
constexpr vertnum_t free_index_sentinel{UINT32_MAX}; |
| 133 |
vertnum_t free_index = free_index_sentinel; |
| 134 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 135 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 136 |
for (const unsigned vi : xrange(MAX_SEGMENT_VERTICES)) |
| 137 |
{ |
| 138 |
const vertnum_t v{vi}; |
| 139 |
if (Vertex_active[v]) { |
| 140 |
if (vnear(vp, Vertices.vcptr(v))) { |
| 141 |
return v; |
| 142 |
} |
| 143 |
if (++ count >= Num_vertices) |
| 144 |
break; |
| 145 |
} else if (free_index == free_index_sentinel) |
| 146 |
/* No break here. There might be a later vertex which |
| 147 |
* matches the vnear test, and if there is, it should be |
| 148 |
* used instead of adding a new vertex at the first free |
| 149 |
* slot. |
| 150 |
*/ |
| 151 |
free_index = v; // we want free_index to be the first free slot to add a vertex |
| 152 |
} |
| 153 |
|
| 154 |
if (free_index == free_index_sentinel) |
| 155 |
free_index = vertnum_t{Num_vertices}; |
| 156 |
|
| 157 |
while (Vertex_active.valid_index(free_index) && Vertex_active[free_index]) |
| 158 |
free_index = static_cast<vertnum_t>(static_cast<unsigned>(free_index) + 1u); |
| 159 |
|
| 160 |
*Vertices.vmptr(free_index) = vp; |
| 161 |
Vertex_active[free_index] = 1; |
| 162 |
|
| 163 |
++LevelSharedVertexState.Num_vertices; |
| 164 |
|
| 165 |
maintain_vertex_count(Vertices, free_index); |
| 166 |
|
| 167 |
return free_index; |
| 168 |
} |
| 169 |
|
| 170 |
namespace dsx { |
| 171 |
|
| 172 |
// ------------------------------------------------------------------------------------------ |
| 173 |
// Returns the index of a free segment. |
| 174 |
// Scans the Segments array. |
| 175 |
segnum_t get_free_segment_number(segment_array &Segments) |
| 176 |
{ |
| 177 |
for (const auto &&[segnum, seg] : enumerate(Segments)) |
| 178 |
if (seg.segnum == segment_none) |
| 179 |
{ |
| 180 |
++ LevelSharedSegmentState.Num_segments; |
| 181 |
if (segnum > Highest_segment_index) |
| 182 |
Segments.set_count(segnum + 1); |
| 183 |
return segnum; |
| 184 |
} |
| 185 |
|
| 186 |
Assert(0); |
| 187 |
return segnum_t{}; |
| 188 |
} |
| 189 |
|
| 190 |
// ------------------------------------------------------------------------------- |
| 191 |
// Create a new segment, duplicating exactly, including vertex ids and children, the passed segment. |
| 192 |
segnum_t med_create_duplicate_segment(segment_array &Segments, const segment &sp) |
| 193 |
{ |
| 194 |
const auto segnum = get_free_segment_number(Segments); |
| 195 |
|
| 196 |
auto &nsp = *Segments.vmptr(segnum); |
| 197 |
nsp = sp; |
| 198 |
unique_segment &unsp = nsp; |
| 199 |
unsp.objects = object_none; |
| 200 |
|
| 201 |
return segnum; |
| 202 |
} |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
// ------------------------------------------------------------------------------- |
| 207 |
// Add the vertex *vp to the global list of vertices, return its index. |
| 208 |
// This is the same as med_add_vertex, except that it does not search for the presence of the vertex. |
| 209 |
vertnum_t med_create_duplicate_vertex(const vertex &vp) |
| 210 |
{ |
| 211 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 212 |
const auto Num_vertices = LevelSharedVertexState.Num_vertices; |
| 213 |
Assert(Num_vertices < MAX_SEGMENT_VERTICES); |
| 214 |
|
| 215 |
Do_duplicate_vertex_check = 1; |
| 216 |
|
| 217 |
vertnum_t free_index = vertnum_t{Num_vertices}; |
| 218 |
|
| 219 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 220 |
while (Vertex_active.valid_index(free_index) && Vertex_active[free_index]) |
| 221 |
free_index = static_cast<vertnum_t>(static_cast<unsigned>(free_index) + 1u); |
| 222 |
|
| 223 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 224 |
*Vertices.vmptr(free_index) = vp; |
| 225 |
Vertex_active[free_index] = 1; |
| 226 |
|
| 227 |
++LevelSharedVertexState.Num_vertices; |
| 228 |
|
| 229 |
maintain_vertex_count(Vertices, free_index); |
| 230 |
|
| 231 |
return vertnum_t{free_index}; |
| 232 |
} |
| 233 |
|
| 234 |
namespace { |
| 235 |
|
| 236 |
// ------------------------------------------------------------------------------- |
| 237 |
// Set the vertex *vp at index vnum in the global list of vertices, return its index (just for compatibility). |
| 238 |
vertnum_t med_set_vertex(d_level_shared_vertex_state &LevelSharedVertexState, const vertnum_t vnum, const vertex &vp) |
| 239 |
{ |
| 240 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 241 |
*Vertices.vmptr(vnum) = vp; |
| 242 |
|
| 243 |
// Just in case this vertex wasn't active, mark it as active. |
| 244 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 245 |
if (auto &va = Vertex_active[vnum]; !va) { |
| 246 |
va = 1; |
| 247 |
} |
| 248 |
|
| 249 |
return vnum; |
| 250 |
} |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
namespace dsx { |
| 255 |
|
| 256 |
// ------------------------------------------------------------------------------- |
| 257 |
void create_removable_wall(fvcvertptr &vcvertptr, const vmsegptridx_t sp, const sidenum_t sidenum, const texture1_value tmap_num) |
| 258 |
{ |
| 259 |
create_walls_on_side(vcvertptr, sp, sidenum); |
| 260 |
|
| 261 |
sp->unique_segment::sides[sidenum].tmap_num = tmap_num; |
| 262 |
|
| 263 |
assign_default_uvs_to_side(sp, sidenum); |
| 264 |
assign_light_to_side(sp, sidenum); |
| 265 |
} |
| 266 |
|
| 267 |
#if 0 |
| 268 |
|
| 269 |
// --------------------------------------------------------------------------------------------- |
| 270 |
// Orthogonalize matrix smat, returning result in rmat. |
| 271 |
// Does not modify smat. |
| 272 |
// Uses Gram-Schmidt process. |
| 273 |
// See page 172 of Strang, Gilbert, Linear Algebra and its Applications |
| 274 |
// Matt -- This routine should be moved to the vector matrix library. |
| 275 |
// It IS legal for smat == rmat. |
| 276 |
// We should also have the functions: |
| 277 |
// mat_a = mat_b * scalar; // we now have mat_a = mat_a * scalar; |
| 278 |
// mat_a = mat_b + mat_c * scalar; // or maybe not, maybe this is not primitive |
| 279 |
void make_orthogonal(vms_matrix *rmat,vms_matrix *smat) |
| 280 |
{ |
| 281 |
vms_matrix tmat; |
| 282 |
vms_vector tvec1,tvec2; |
| 283 |
float dot; |
| 284 |
|
| 285 |
// Copy source matrix to work area. |
| 286 |
tmat = *smat; |
| 287 |
|
| 288 |
// Normalize the three rows of the matrix tmat. |
| 289 |
vm_vec_normalize(&tmat.xrow); |
| 290 |
vm_vec_normalize(&tmat.yrow); |
| 291 |
vm_vec_normalize(&tmat.zrow); |
| 292 |
|
| 293 |
// Now, compute the first vector. |
| 294 |
// This is very easy -- just copy the (normalized) source vector. |
| 295 |
rmat->zrow = tmat.zrow; |
| 296 |
|
| 297 |
// Now, compute the second vector. |
| 298 |
// From page 172 of Strang, we use the equation: |
| 299 |
// b' = b - [transpose(q1) * b] * q1 |
| 300 |
// where: b = the second row of tmat |
| 301 |
// q1 = the first row of rmat |
| 302 |
// b' = the second row of rmat |
| 303 |
|
| 304 |
// Compute: transpose(q1) * b |
| 305 |
dot = vm_vec_build_dot(&rmat->zrow,&tmat.yrow); |
| 306 |
|
| 307 |
// Compute: b - dot * q1 |
| 308 |
rmat->yrow.x = tmat.yrow.x - fixmul(dot,rmat->zrow.x); |
| 309 |
rmat->yrow.y = tmat.yrow.y - fixmul(dot,rmat->zrow.y); |
| 310 |
rmat->yrow.z = tmat.yrow.z - fixmul(dot,rmat->zrow.z); |
| 311 |
|
| 312 |
// Now, compute the third vector. |
| 313 |
// From page 173 of Strang, we use the equation: |
| 314 |
// c' = c - (q1*c)*q1 - (q2*c)*q2 |
| 315 |
// where: c = the third row of tmat |
| 316 |
// q1 = the first row of rmat |
| 317 |
// q2 = the second row of rmat |
| 318 |
// c' = the third row of rmat |
| 319 |
|
| 320 |
// Compute: q1*c |
| 321 |
dot = vm_vec_build_dot(&rmat->zrow,&tmat.xrow); |
| 322 |
|
| 323 |
tvec1.x = fixmul(dot,rmat->zrow.x); |
| 324 |
tvec1.y = fixmul(dot,rmat->zrow.y); |
| 325 |
tvec1.z = fixmul(dot,rmat->zrow.z); |
| 326 |
|
| 327 |
// Compute: q2*c |
| 328 |
dot = vm_vec_build_dot(&rmat->yrow,&tmat.xrow); |
| 329 |
|
| 330 |
tvec2.x = fixmul(dot,rmat->yrow.x); |
| 331 |
tvec2.y = fixmul(dot,rmat->yrow.y); |
| 332 |
tvec2.z = fixmul(dot,rmat->yrow.z); |
| 333 |
|
| 334 |
vm_vec_sub(&rmat->xrow,vm_vec_sub(&rmat->xrow,&tmat.xrow,&tvec1),&tvec2); |
| 335 |
} |
| 336 |
|
| 337 |
#endif |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
namespace dcx { |
| 342 |
|
| 343 |
// ------------------------------------------------------------------------------------------ |
| 344 |
// Given a segment, extract the rotation matrix which defines it. |
| 345 |
// Do this by extracting the forward, right, up vectors and then making them orthogonal. |
| 346 |
// In the process of making the vectors orthogonal, favor them in the order forward, up, right. |
| 347 |
// This means that the forward vector will remain unchanged. |
| 348 |
vms_matrix med_extract_matrix_from_segment(const shared_segment &sp) |
| 349 |
{ |
| 350 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 351 |
|
| 352 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 353 |
auto &vcvertptr = Vertices.vcptr; |
| 354 |
const auto forwardvec{extract_forward_vector_from_segment(vcvertptr, sp)}; |
| 355 |
const auto upvec{extract_up_vector_from_segment(vcvertptr, sp)}; |
| 356 |
|
| 357 |
return (forwardvec == vms_vector{} || upvec == vms_vector{}) |
| 358 |
? vmd_identity_matrix |
| 359 |
: vm_vector_to_matrix_u(forwardvec, upvec); |
| 360 |
} |
| 361 |
|
| 362 |
} |
| 363 |
|
| 364 |
// ------------------------------------------------------------------------------------------ |
| 365 |
// Given a rotation matrix *rotmat which describes the orientation of a segment |
| 366 |
// and a side destside, return the rotation matrix which describes the orientation for the side. |
| 367 |
void update_matrix_based_on_side(vms_matrix &rotmat, const sidenum_t destside) |
| 368 |
{ |
| 369 |
switch (destside) { |
| 370 |
case sidenum_t::WLEFT: |
| 371 |
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix( |
| 372 |
vms_angvec{ |
| 373 |
.p = 0, |
| 374 |
.b = 0, |
| 375 |
.h = -16384 |
| 376 |
})); |
| 377 |
break; |
| 378 |
|
| 379 |
case sidenum_t::WTOP: |
| 380 |
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix( |
| 381 |
vms_angvec{ |
| 382 |
.p = -16384, |
| 383 |
.b = 0, |
| 384 |
.h = 0 |
| 385 |
})); |
| 386 |
break; |
| 387 |
|
| 388 |
case sidenum_t::WRIGHT: |
| 389 |
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix( |
| 390 |
vms_angvec{ |
| 391 |
.p = 0, |
| 392 |
.b = 0, |
| 393 |
.h = 16384 |
| 394 |
})); |
| 395 |
break; |
| 396 |
|
| 397 |
case sidenum_t::WBOTTOM: |
| 398 |
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix( |
| 399 |
vms_angvec{ |
| 400 |
.p = 16384, |
| 401 |
.b = -32768, // bank was -32768, but I think that was an erroneous compensation |
| 402 |
.h = 0 |
| 403 |
})); |
| 404 |
break; |
| 405 |
|
| 406 |
case sidenum_t::WFRONT: |
| 407 |
rotmat = vm_matrix_x_matrix(rotmat, vm_angles_2_matrix( |
| 408 |
vms_angvec{ |
| 409 |
.p = 0, |
| 410 |
.b = 0, |
| 411 |
.h = -32768 |
| 412 |
})); |
| 413 |
break; |
| 414 |
|
| 415 |
case sidenum_t::WBACK: |
| 416 |
break; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
namespace { |
| 421 |
|
| 422 |
// ------------------------------------------------------------------------------------- |
| 423 |
static void change_vertex_occurrences(fvmsegptr &vmsegptr, const vertnum_t dest, const vertnum_t src) |
| 424 |
{ |
| 425 |
// Fix vertices in groups |
| 426 |
range_for (auto &g, partial_range(GroupList, num_groups)) |
| 427 |
g.vertices.replace(src, dest); |
| 428 |
|
| 429 |
// now scan all segments, changing occurrences of src to dest |
| 430 |
for (shared_segment &segp : vmsegptr) |
| 431 |
if (segp.segnum != segment_none) |
| 432 |
{ |
| 433 |
auto &verts = segp.verts; |
| 434 |
std::replace(verts.begin(), verts.end(), src, dest); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
// -------------------------------------------------------------------------------------------------- |
| 439 |
static void compress_vertices(void) |
| 440 |
{ |
| 441 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 442 |
const auto Num_vertices = LevelSharedVertexState.Num_vertices; |
| 443 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 444 |
if (Vertices.get_count() == Num_vertices) |
| 445 |
return; |
| 446 |
|
| 447 |
unsigned vert = Vertices.get_count() - 1; //MAX_SEGMENT_VERTICES-1; |
| 448 |
|
| 449 |
auto &vmvertptr = Vertices.vmptr; |
| 450 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 451 |
for (unsigned hole = 0; hole < vert; ++hole) |
| 452 |
{ |
| 453 |
const vertnum_t vhole{hole}; |
| 454 |
if (auto &active_hole = Vertex_active[vhole]; !active_hole) |
| 455 |
{ |
| 456 |
// found an unused vertex which is a hole if a used vertex follows (not necessarily immediately) it. |
| 457 |
for (; hole < vert; --vert) |
| 458 |
{ |
| 459 |
const vertnum_t vvert{vert}; |
| 460 |
auto &active_vert = Vertex_active[vvert]; |
| 461 |
if (!active_vert) |
| 462 |
continue; |
| 463 |
active_hole = std::exchange(active_vert, 0); |
| 464 |
// Ok, hole is the index of a hole, vert is the index of a vertex which follows it. |
| 465 |
// Copy vert into hole, update pointers to it. |
| 466 |
auto &vp_vert = *vmvertptr(vvert); |
| 467 |
*vmvertptr(vhole) = vp_vert; |
| 468 |
vp_vert = {}; |
| 469 |
DXX_MAKE_VAR_UNDEFINED(vp_vert); |
| 470 |
change_vertex_occurrences(vmsegptr, vhole, vvert); |
| 471 |
vert--; |
| 472 |
break; |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
Vertices.set_count(Num_vertices); |
| 478 |
} |
| 479 |
|
| 480 |
// -------------------------------------------------------------------------------------------------- |
| 481 |
static void compress_segments(void) |
| 482 |
{ |
| 483 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 484 |
auto &vmobjptridx = Objects.vmptridx; |
| 485 |
if (Highest_segment_index == LevelSharedSegmentState.Num_segments - 1) |
| 486 |
return; |
| 487 |
|
| 488 |
auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters; |
| 489 |
auto &Walls = LevelUniqueWallSubsystemState.Walls; |
| 490 |
auto &vmwallptr = Walls.vmptr; |
| 491 |
auto holep = Segments.vmptridx.begin(); |
| 492 |
auto segp = Segments.vmptridx.end(); |
| 493 |
for (; holep != segp; ++holep) |
| 494 |
{ |
| 495 |
const vmsegptridx_t hole = *holep; |
| 496 |
const msmusegment suhole = hole; |
| 497 |
if (suhole.s.segnum != segment_none) |
| 498 |
continue; |
| 499 |
// found an unused segment which is a hole if a used segment follows (not necessarily immediately) it. |
| 500 |
for (; holep != --segp && segp.base()->segnum == segment_none;) |
| 501 |
{ |
| 502 |
} |
| 503 |
if (holep == segp) |
| 504 |
break; |
| 505 |
// Ok, hole is the index of a hole, seg is the index of a segment which follows it. |
| 506 |
// Copy seg into hole, update pointers to it, update Cursegp, Markedsegp if necessary. |
| 507 |
const vmsegptridx_t seg = *segp; |
| 508 |
const msmusegment suseg = seg; |
| 509 |
suhole.s = std::move(suseg.s); |
| 510 |
suhole.u = std::move(suseg.u); |
| 511 |
/* As a debugging measure, reset the expired segment to default values. |
| 512 |
* Nothing should access this segment until it is reallocated and |
| 513 |
* reinitialized, so any values should be safe to write here. |
| 514 |
*/ |
| 515 |
suseg.u = {}; |
| 516 |
suseg.s = {}; |
| 517 |
suseg.s.segnum = segment_none; |
| 518 |
if (Cursegp == seg) |
| 519 |
Cursegp = hole; |
| 520 |
if (Markedsegp == seg) |
| 521 |
Markedsegp = hole; |
| 522 |
// Fix segments in groups |
| 523 |
for (auto &g : partial_range(GroupList, num_groups)) |
| 524 |
g.segments.replace(seg, hole); |
| 525 |
|
| 526 |
// Fix walls |
| 527 |
for (wall &w : vmwallptr) |
| 528 |
if (auto &s = w.segnum; s == seg) |
| 529 |
s = hole; |
| 530 |
|
| 531 |
// Fix fuelcenters, robotcens, and triggers... added 2/1/95 -Yuan |
| 532 |
for (auto &f : partial_range(LevelUniqueFuelcenterState.Station, LevelUniqueFuelcenterState.Num_fuelcenters)) |
| 533 |
if (auto &s = f.segnum; s == seg) |
| 534 |
s = hole; |
| 535 |
|
| 536 |
for (auto &r : partial_range(RobotCenters, LevelSharedRobotcenterState.Num_robot_centers)) |
| 537 |
if (auto &s = r.segnum; s == seg) |
| 538 |
s = hole; |
| 539 |
|
| 540 |
auto &Triggers = LevelUniqueWallSubsystemState.Triggers; |
| 541 |
auto &vmtrgptr = Triggers.vmptr; |
| 542 |
for (trigger &t : vmtrgptr) |
| 543 |
{ |
| 544 |
for (auto &l : partial_range(t.seg, t.num_links)) |
| 545 |
if (l == seg) |
| 546 |
l = hole; |
| 547 |
} |
| 548 |
|
| 549 |
for (auto &s : suhole.s.children) |
| 550 |
{ |
| 551 |
if (IS_CHILD(s)) |
| 552 |
{ |
| 553 |
// Find out on what side the segment connection to the former seg is on in *csegp. |
| 554 |
shared_segment &ss = vmsegptr(s); |
| 555 |
for (auto &t : ss.children) |
| 556 |
{ |
| 557 |
if (t == seg) { |
| 558 |
t = hole; // It used to be connected to seg, so make it connected to hole |
| 559 |
break; |
| 560 |
} |
| 561 |
} // end for t |
| 562 |
} // end if |
| 563 |
} // end for s |
| 564 |
|
| 565 |
//Update object segment pointers |
| 566 |
for (auto &objp : objects_in<object_base>(suhole.u, vmobjptridx, vmsegptr)) |
| 567 |
{ |
| 568 |
assert(objp.segnum == seg); |
| 569 |
objp.segnum = hole; |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
Segments.set_count(LevelSharedSegmentState.Num_segments); |
| 574 |
med_create_new_segment_from_cursegp(); |
| 575 |
|
| 576 |
} |
| 577 |
|
| 578 |
} |
| 579 |
|
| 580 |
// ------------------------------------------------------------------------------- |
| 581 |
// Combine duplicate vertices. |
| 582 |
// If two vertices have the same coordinates, within some small tolerance, then assign |
| 583 |
// the same vertex number to the two vertices, freeing up one of the vertices. |
| 584 |
void med_combine_duplicate_vertices(enumerated_array<uint8_t, MAX_VERTICES, vertnum_t> &vlp) |
| 585 |
{ |
| 586 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 587 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 588 |
auto &vcvertptridx = Vertices.vcptridx; |
| 589 |
const std::ranges::subrange range{vcvertptridx}; |
| 590 |
// Note: ok to do to <, rather than <= because w for loop starts at v+1 |
| 591 |
if (range.empty()) |
| 592 |
return; |
| 593 |
for (auto i = range.begin();;) |
| 594 |
{ |
| 595 |
const auto &&v = *i; |
| 596 |
if (++i == range.end()) |
| 597 |
return; |
| 598 |
if (vlp[v]) { |
| 599 |
auto &vvp = *v; |
| 600 |
for (auto &&w : std::ranges::subrange(i, range.end())) |
| 601 |
if (vlp[w]) { // used to be Vertex_active[w] |
| 602 |
if (vnear(vvp, *w)) { |
| 603 |
change_vertex_occurrences(vmsegptr, v, w); |
| 604 |
} |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
// ------------------------------------------------------------------------------ |
| 611 |
// Compress mine at Segments and Vertices by squeezing out all holes. |
| 612 |
// If no holes (ie, an unused segment followed by a used segment), then no action. |
| 613 |
// If Cursegp or Markedsegp is a segment which gets moved to fill in a hole, then |
| 614 |
// they are properly updated. |
| 615 |
void med_compress_mine(void) |
| 616 |
{ |
| 617 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 618 |
if (Do_duplicate_vertex_check) { |
| 619 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 620 |
med_combine_duplicate_vertices(Vertex_active); |
| 621 |
Do_duplicate_vertex_check = 0; |
| 622 |
} |
| 623 |
|
| 624 |
compress_segments(); |
| 625 |
compress_vertices(); |
| 626 |
set_vertex_counts(); |
| 627 |
|
| 628 |
//--repair-- create_local_segment_data(); |
| 629 |
|
| 630 |
// This is necessary becuase a segment search (due to click in 3d window) uses the previous frame's |
| 631 |
// segment information, which could get changed by this. |
| 632 |
Update_flags = UF_WORLD_CHANGED; |
| 633 |
} |
| 634 |
|
| 635 |
namespace dsx { |
| 636 |
|
| 637 |
namespace { |
| 638 |
|
| 639 |
// ------------------------------------------------------------------------------------------ |
| 640 |
// Copy texture map ids for each face in sseg to dseg. |
| 641 |
static void copy_tmap_ids(unique_segment &dseg, const unique_segment &sseg) |
| 642 |
{ |
| 643 |
for (auto &&[ss, ds] : zip(sseg.sides, dseg.sides)) |
| 644 |
{ |
| 645 |
ds.tmap_num = ss.tmap_num; |
| 646 |
ds.tmap_num2 = texture2_value::None; |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
// ------------------------------------------------------------------------------------------ |
| 651 |
// Attach a segment with a rotated orientation. |
| 652 |
// Return value: |
| 653 |
// 0 = successful attach |
| 654 |
// 1 = No room in Segments[]. |
| 655 |
// 2 = No room in Vertices[]. |
| 656 |
// 3 = newside != WFRONT -- for now, the new segment must be attached at its (own) front side |
| 657 |
// 4 = already a face attached on destseg:destside |
| 658 |
static int med_attach_segment_rotated(const vmsegptridx_t destseg, const csmusegment newseg, const sidenum_t destside, const sidenum_t newside, const vms_matrix &attmat) |
| 659 |
{ |
| 660 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 661 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 662 |
vms_matrix rotmat2; |
| 663 |
vms_vector forvec,upvec; |
| 664 |
|
| 665 |
// Return if already a face attached on this side. |
| 666 |
if (IS_CHILD(destseg->children[destside])) |
| 667 |
return 4; |
| 668 |
|
| 669 |
const auto segnum = get_free_segment_number(Segments); |
| 670 |
|
| 671 |
forvec = attmat.fvec; |
| 672 |
upvec = attmat.uvec; |
| 673 |
|
| 674 |
// We are pretty confident we can add the segment. |
| 675 |
const auto &&nsp = destseg.absolute_sibling(segnum); |
| 676 |
|
| 677 |
nsp->segnum = segnum; |
| 678 |
static_cast<unique_segment &>(nsp).objects = object_none; |
| 679 |
nsp->matcen_num = materialization_center_number::None; |
| 680 |
|
| 681 |
// Copy group value. |
| 682 |
nsp->group = destseg->group; |
| 683 |
|
| 684 |
// Add segment to proper group list. |
| 685 |
if (nsp->group > -1) |
| 686 |
add_segment_to_group(nsp, nsp->group); |
| 687 |
|
| 688 |
// Copy the texture map ids. |
| 689 |
copy_tmap_ids(nsp,newseg); |
| 690 |
|
| 691 |
// clear all connections |
| 692 |
for (const auto side : MAX_SIDES_PER_SEGMENT) |
| 693 |
{ |
| 694 |
nsp->children[side] = segment_none; |
| 695 |
nsp->shared_segment::sides[side].wall_num = wall_none; |
| 696 |
} |
| 697 |
|
| 698 |
// Form the connection |
| 699 |
destseg->children[destside] = segnum; |
| 700 |
// destseg->sides[destside].render_flag = 0; |
| 701 |
nsp->children[newside] = destseg; |
| 702 |
|
| 703 |
// Copy vertex indices of the four vertices forming the joint |
| 704 |
auto &dvp = Side_to_verts[destside]; |
| 705 |
|
| 706 |
// Set the vertex indices for the four vertices forming the front of the new side |
| 707 |
for (auto &&[dst, src] : zip(unchecked_partial_range(nsp->verts, 4u), dvp)) |
| 708 |
dst = destseg->verts[src]; |
| 709 |
|
| 710 |
// The other 4 vertices must be created. |
| 711 |
// Their coordinates are determined by the 4 welded vertices and the vector from front |
| 712 |
// to back of the original *newseg. |
| 713 |
|
| 714 |
// Do lots of hideous matrix stuff, about 3/4 of which could probably be simplified out. |
| 715 |
auto rotmat{med_extract_matrix_from_segment(destseg)}; // get orientation matrix for destseg (orthogonal rotation matrix) |
| 716 |
update_matrix_based_on_side(rotmat,destside); |
| 717 |
const auto rotmat1{vm_vector_to_matrix_u(forvec, upvec)}; |
| 718 |
const auto rotmat4 = vm_matrix_x_matrix(rotmat,rotmat1); // this is the desired orientation of the new segment |
| 719 |
auto rotmat3{med_extract_matrix_from_segment(newseg)}; // this is the current orientation of the new segment |
| 720 |
vm_transpose_matrix(rotmat3); // get the inverse of the current orientation matrix |
| 721 |
vm_matrix_x_matrix(rotmat2,rotmat4,rotmat3); // now rotmat2 takes the current segment to the desired orientation |
| 722 |
|
| 723 |
// Warning -- look at this line! |
| 724 |
vm_transpose_matrix(rotmat2); // added 12:33 pm, 10/01/93 |
| 725 |
|
| 726 |
// Compute and rotate the center point of the attaching face. |
| 727 |
auto &vcvertptr = Vertices.vcptr; |
| 728 |
const auto vc0{compute_center_point_on_side(vcvertptr, newseg, newside)}; |
| 729 |
const auto vr{vm_vec_build_rotated(vc0, rotmat2)}; |
| 730 |
|
| 731 |
// Now translate the new segment so that the center point of the attaching faces are the same. |
| 732 |
const auto vc1{compute_center_point_on_side(vcvertptr, destseg, destside)}; |
| 733 |
const auto xlate_vec{vm_vec_build_sub(vc1, vr)}; |
| 734 |
|
| 735 |
// Now rotate the free vertices in the segment |
| 736 |
// Create and add the 4 new vertices. |
| 737 |
for (auto &&[dst, src] : zip(unchecked_partial_range(nsp->verts, 4u, MAX_VERTICES_PER_SEGMENT.value), newseg.s.verts)) |
| 738 |
{ |
| 739 |
vertex tv; |
| 740 |
vm_vec_rotate(tv, vcvertptr(src), rotmat2); |
| 741 |
vm_vec_add2(tv, xlate_vec); |
| 742 |
dst = med_add_vertex(tv); |
| 743 |
} |
| 744 |
|
| 745 |
set_vertex_counts(); |
| 746 |
|
| 747 |
// Now all the vertices are in place. Create the faces. |
| 748 |
validate_segment(vcvertptr, nsp); |
| 749 |
|
| 750 |
// Say to not render at the joint. |
| 751 |
// destseg->sides[destside].render_flag = 0; |
| 752 |
// nsp->sides[newside].render_flag = 0; |
| 753 |
|
| 754 |
Cursegp = nsp; |
| 755 |
|
| 756 |
return 0; |
| 757 |
} |
| 758 |
|
| 759 |
} |
| 760 |
|
| 761 |
// ------------------------------------------------------------------------------------------ |
| 762 |
// Attach side newside of newseg to side destside of destseg. |
| 763 |
// Copies *newseg into global array Segments, increments Num_segments. |
| 764 |
// Forms a weld between the two segments by making the new segment fit to the old segment. |
| 765 |
// Updates number of faces per side if necessitated by new vertex coordinates. |
| 766 |
// Updates Cursegp. |
| 767 |
// Return value: |
| 768 |
// 0 = successful attach |
| 769 |
// 1 = No room in Segments[]. |
| 770 |
// 2 = No room in Vertices[]. |
| 771 |
// 3 = newside != WFRONT -- for now, the new segment must be attached at its (own) front side |
| 772 |
// 4 = already a face attached on side newside |
| 773 |
int med_attach_segment(const vmsegptridx_t destseg, const csmusegment newseg, const sidenum_t destside, const sidenum_t newside) |
| 774 |
{ |
| 775 |
int rval; |
| 776 |
const auto ocursegp{Cursegp}; |
| 777 |
|
| 778 |
vms_angvec tang = {0,0,0}; |
| 779 |
const auto &&rotmat{vm_angles_2_matrix(tang)}; |
| 780 |
rval = med_attach_segment_rotated(destseg,newseg,destside,newside,rotmat); |
| 781 |
med_propagate_tmaps_to_segments(ocursegp,Cursegp,0); |
| 782 |
med_propagate_tmaps_to_back_side(Cursegp, Side_opposite[newside],0); |
| 783 |
copy_uvs_seg_to_seg(vmsegptr(&New_segment), Cursegp); |
| 784 |
|
| 785 |
return rval; |
| 786 |
} |
| 787 |
|
| 788 |
} |
| 789 |
|
| 790 |
namespace { |
| 791 |
|
| 792 |
// ------------------------------------------------------------------------------- |
| 793 |
// Delete a vertex, sort of. |
| 794 |
// Decrement the vertex count. If the count goes to 0, then the vertex is free (has been deleted). |
| 795 |
static void delete_vertex(const vertnum_t v) |
| 796 |
{ |
| 797 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 798 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 799 |
auto &va = Vertex_active[v]; |
| 800 |
assert(va >= 1); // abort if trying to delete a non-existent vertex |
| 801 |
--va; |
| 802 |
} |
| 803 |
|
| 804 |
// ------------------------------------------------------------------------------- |
| 805 |
// Update Num_vertices. |
| 806 |
// This routine should be called by anyone who calls delete_vertex. It could be called in delete_vertex, |
| 807 |
// but then it would be called much more often than necessary, and it is a slow routine. |
| 808 |
static void update_num_vertices(void) |
| 809 |
{ |
| 810 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 811 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 812 |
// Now count the number of vertices. |
| 813 |
unsigned n{0}; |
| 814 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 815 |
range_for (const auto v, partial_range(Vertex_active, Vertices.get_count())) |
| 816 |
if (v) |
| 817 |
++n; |
| 818 |
LevelSharedVertexState.Num_vertices = n; |
| 819 |
} |
| 820 |
|
| 821 |
} |
| 822 |
|
| 823 |
namespace dsx { |
| 824 |
|
| 825 |
// ------------------------------------------------------------------------------- |
| 826 |
// Set Vertex_active to number of occurrences of each vertex. |
| 827 |
// Set Num_vertices. |
| 828 |
void set_vertex_counts(void) |
| 829 |
{ |
| 830 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 831 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 832 |
unsigned Num_vertices{0}; |
| 833 |
|
| 834 |
Vertex_active = {}; |
| 835 |
|
| 836 |
// Count number of occurrences of each vertex. |
| 837 |
for (auto &seg : vmsegptr) |
| 838 |
{ |
| 839 |
if (seg.segnum != segment_none) |
| 840 |
for (auto &v : seg.verts) |
| 841 |
{ |
| 842 |
if (!Vertex_active[v]) |
| 843 |
Num_vertices++; |
| 844 |
++ Vertex_active[v]; |
| 845 |
} |
| 846 |
} |
| 847 |
LevelSharedVertexState.Num_vertices = Num_vertices; |
| 848 |
} |
| 849 |
|
| 850 |
namespace { |
| 851 |
|
| 852 |
// ------------------------------------------------------------------------------- |
| 853 |
// Delete all vertices in segment *sp from the vertex list if they are not contained in another segment. |
| 854 |
// This is kind of a dangerous routine. It modifies the global array Vertex_active, using the field as |
| 855 |
// a count. |
| 856 |
static void delete_vertices_in_segment(const shared_segment &sp) |
| 857 |
{ |
| 858 |
// init_vertices(); |
| 859 |
set_vertex_counts(); |
| 860 |
// Subtract one count for each appearance of vertex in deleted segment |
| 861 |
range_for (auto &v, sp.verts) |
| 862 |
delete_vertex(v); |
| 863 |
|
| 864 |
update_num_vertices(); |
| 865 |
} |
| 866 |
|
| 867 |
} |
| 868 |
|
| 869 |
// ------------------------------------------------------------------------------- |
| 870 |
// Delete segment *sp in Segments array. |
| 871 |
// Return value: |
| 872 |
// 0 successfully deleted. |
| 873 |
// 1 unable to delete. |
| 874 |
int med_delete_segment(const vmsegptridx_t sp) |
| 875 |
{ |
| 876 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 877 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 878 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 879 |
auto &vmobjptr = Objects.vmptr; |
| 880 |
auto &vmobjptridx = Objects.vmptridx; |
| 881 |
segnum_t segnum = sp; |
| 882 |
// Cannot delete segment if only segment. |
| 883 |
if (LevelSharedSegmentState.Num_segments == 1) |
| 884 |
return 1; |
| 885 |
|
| 886 |
// Don't try to delete if segment doesn't exist. |
| 887 |
if (sp->segnum == segment_none) { |
| 888 |
return 1; |
| 889 |
} |
| 890 |
|
| 891 |
// Delete its refueling center if it has one |
| 892 |
fuelcen_delete(sp); |
| 893 |
|
| 894 |
delete_vertices_in_segment(sp); |
| 895 |
|
| 896 |
-- LevelSharedSegmentState.Num_segments; |
| 897 |
|
| 898 |
// If deleted segment has walls on any side, wipe out the wall. |
| 899 |
for (const auto side : MAX_SIDES_PER_SEGMENT) |
| 900 |
if (sp->shared_segment::sides[side].wall_num != wall_none) |
| 901 |
wall_remove_side(sp, side); |
| 902 |
|
| 903 |
auto &vcvertptr = Vertices.vcptr; |
| 904 |
// Find out what this segment was connected to and break those connections at the other end. |
| 905 |
range_for (auto &side, sp->children) |
| 906 |
if (IS_CHILD(side)) { |
| 907 |
const auto &&csp = sp.absolute_sibling(side); |
| 908 |
for (const auto s : MAX_SIDES_PER_SEGMENT) |
| 909 |
if (csp->children[s] == segnum) { |
| 910 |
csp->children[s] = segment_none; // this is the side of connection, break it |
| 911 |
validate_segment_side(vcvertptr, csp, s); // we have converted a connection to a side so validate the segment |
| 912 |
med_propagate_tmaps_to_back_side(csp,s,0); |
| 913 |
} |
| 914 |
Cursegp = csp; |
| 915 |
med_create_new_segment_from_cursegp(); |
| 916 |
copy_uvs_seg_to_seg(vmsegptr(&New_segment), Cursegp); |
| 917 |
} |
| 918 |
|
| 919 |
sp->segnum = segment_none; // Mark segment as inactive. |
| 920 |
|
| 921 |
// If deleted segment = marked segment, then say there is no marked segment |
| 922 |
if (sp == Markedsegp) |
| 923 |
Markedsegp = segment_none; |
| 924 |
|
| 925 |
// If deleted segment = a Group segment ptr, then wipe it out. |
| 926 |
range_for (auto &s, partial_range(Groupsegp, num_groups)) |
| 927 |
if (s == sp) |
| 928 |
s = nullptr; |
| 929 |
|
| 930 |
// If deleted segment = group segment, wipe it off the group list. |
| 931 |
if (sp->group > -1) |
| 932 |
delete_segment_from_group(sp, sp->group); |
| 933 |
|
| 934 |
// If we deleted something which was not connected to anything, must now select a new current segment. |
| 935 |
if (Cursegp == sp) |
| 936 |
for (auto &&[s, seg] : enumerate(Segments)) |
| 937 |
if (seg.segnum != segment_none && s != segnum) |
| 938 |
{ |
| 939 |
Cursegp = imsegptridx(s); |
| 940 |
med_create_new_segment_from_cursegp(); |
| 941 |
break; |
| 942 |
} |
| 943 |
|
| 944 |
// If deleted segment contains objects, wipe out all objects |
| 945 |
range_for (const auto objnum, objects_in(*sp, vmobjptridx, vmsegptr)) |
| 946 |
{ |
| 947 |
//if an object is in the seg, delete it |
| 948 |
//if the object is the player, move to new curseg |
| 949 |
if (objnum == ConsoleObject) { |
| 950 |
ConsoleObject->pos = compute_segment_center(vcvertptr, Cursegp); |
| 951 |
obj_relink(vmobjptr, vmsegptr, objnum, Cursegp); |
| 952 |
} else |
| 953 |
obj_delete(LevelUniqueObjectState, Segments, objnum); |
| 954 |
} |
| 955 |
|
| 956 |
// If we are leaving many holes in Segments or Vertices, then compress mine, because it is inefficient to be that way |
| 957 |
// if ((Highest_segment_index > Num_segments+4) || (Highest_vertex_index > Num_vertices+4*8)) |
| 958 |
// med_compress_mine(); |
| 959 |
|
| 960 |
return 0; |
| 961 |
} |
| 962 |
|
| 963 |
namespace { |
| 964 |
|
| 965 |
// ------------------------------------------------------------------------------------------ |
| 966 |
// Copy texture maps from sseg to dseg |
| 967 |
static void copy_tmaps_to_segment(segment &dstseg, const segment &srcseg) |
| 968 |
{ |
| 969 |
shared_segment &shared_dst_seg = dstseg; |
| 970 |
unique_segment &unique_dst_seg = dstseg; |
| 971 |
const shared_segment &shared_src_seg = srcseg; |
| 972 |
const unique_segment &unique_src_seg = srcseg; |
| 973 |
range_for (const auto &&z, zip(shared_src_seg.sides, shared_dst_seg.sides, unique_src_seg.sides, unique_dst_seg.sides)) |
| 974 |
{ |
| 975 |
auto &shared_src_side = std::get<0>(z); |
| 976 |
auto &shared_dst_side = std::get<1>(z); |
| 977 |
auto &unique_src_side = std::get<2>(z); |
| 978 |
auto &unique_dst_side = std::get<3>(z); |
| 979 |
shared_dst_side.type = shared_src_side.type; |
| 980 |
unique_dst_side.tmap_num = unique_src_side.tmap_num; |
| 981 |
unique_dst_side.tmap_num2 = unique_src_side.tmap_num2; |
| 982 |
} |
| 983 |
|
| 984 |
} |
| 985 |
|
| 986 |
} |
| 987 |
|
| 988 |
// ------------------------------------------------------------------------------------------ |
| 989 |
// Rotate the segment *seg by the pitch, bank, heading defined by *rot, destructively |
| 990 |
// modifying its four free vertices in the global array Vertices. |
| 991 |
// It is illegal to rotate a segment which has connectivity != 1. |
| 992 |
// Pitch, bank, heading are about the point which is the average of the four points |
| 993 |
// forming the side of connection. |
| 994 |
// Return value: |
| 995 |
// 0 = successful rotation |
| 996 |
// 1 = Connectivity makes rotation illegal (connected to 0 or 2+ segments) |
| 997 |
// 2 = Rotation causes degeneracy, such as self-intersecting segment. |
| 998 |
// 3 = Unable to rotate because not connected to exactly 1 segment. |
| 999 |
int med_rotate_segment(const vmsegptridx_t seg, const vms_matrix &rotmat) |
| 1000 |
{ |
| 1001 |
std::optional<sidenum_t> newside; |
| 1002 |
|
| 1003 |
// Find side of attachment |
| 1004 |
for (const auto &&[idx, value] : enumerate(seg->children)) |
| 1005 |
if (IS_CHILD(value)) |
| 1006 |
{ |
| 1007 |
// Return if passed in segment is connected to other than 1 segment. |
| 1008 |
if (newside) |
| 1009 |
return 3; |
| 1010 |
newside = idx; |
| 1011 |
} |
| 1012 |
|
| 1013 |
const auto &&destseg = seg.absolute_sibling(seg->children[*newside]); |
| 1014 |
|
| 1015 |
if (Curside == sidenum_t::WFRONT) |
| 1016 |
Curside = sidenum_t::WBACK; |
| 1017 |
|
| 1018 |
// Before deleting the segment, copy its texture maps to New_segment |
| 1019 |
copy_tmaps_to_segment(vmsegptr(&New_segment), seg); |
| 1020 |
for (const auto &&[destside, child] : enumerate(destseg->children)) |
| 1021 |
if (child == seg) |
| 1022 |
{ |
| 1023 |
med_attach_segment_rotated(destseg, vmsegptr(&New_segment), static_cast<sidenum_t>(destside), AttachSide, rotmat); |
| 1024 |
break; |
| 1025 |
} |
| 1026 |
|
| 1027 |
// Save tmap_num on each side to restore after call to med_propagate_tmaps_to_segments and _back_side |
| 1028 |
// which will change the tmap nums. |
| 1029 |
per_side_array<texture1_value> side_tmaps; |
| 1030 |
for (auto &&[tm, us] : zip(side_tmaps, seg->unique_segment::sides)) |
| 1031 |
tm = us.tmap_num; |
| 1032 |
|
| 1033 |
auto back_side = Side_opposite[find_connect_side(destseg, seg)]; |
| 1034 |
|
| 1035 |
med_propagate_tmaps_to_segments(destseg, seg,0); |
| 1036 |
med_propagate_tmaps_to_back_side(seg, back_side,0); |
| 1037 |
|
| 1038 |
for (const auto &&[idx, side_tmap, us] : enumerate(zip(side_tmaps, seg->unique_segment::sides))) |
| 1039 |
if (idx != back_side) |
| 1040 |
{ |
| 1041 |
us.tmap_num = side_tmap; |
| 1042 |
} |
| 1043 |
|
| 1044 |
return 0; |
| 1045 |
} |
| 1046 |
|
| 1047 |
namespace { |
| 1048 |
|
| 1049 |
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ |
| 1050 |
|
| 1051 |
// ---------------------------------------------------------------------------- |
| 1052 |
// Compute the sum of the distances between the four pairs of points. |
| 1053 |
// The connections are: |
| 1054 |
// firstv1 : 0 (firstv1+1)%4 : 1 (firstv1+2)%4 : 2 (firstv1+3)%4 : 3 |
| 1055 |
static fix seg_seg_vertex_distsum(const shared_segment &seg1, const sidenum_t side1, const shared_segment &seg2, const sidenum_t side2, const unsigned firstv1) |
| 1056 |
{ |
| 1057 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1058 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1059 |
fix distsum; |
| 1060 |
|
| 1061 |
distsum = 0; |
| 1062 |
auto &vcvertptr = Vertices.vcptr; |
| 1063 |
auto &sv1 = Side_to_verts[side1]; |
| 1064 |
auto &sv2 = Side_to_verts[side2]; |
| 1065 |
for (const auto secondv : MAX_VERTICES_PER_SIDE) |
| 1066 |
{ |
| 1067 |
const auto firstv = static_cast<side_relative_vertnum>((4 - underlying_value(secondv) + (3 - firstv1)) % 4); |
| 1068 |
auto &seg1vert = *vcvertptr(seg1.verts[sv1[firstv]]); |
| 1069 |
auto &seg2vert = *vcvertptr(seg2.verts[sv2[secondv]]); |
| 1070 |
distsum += vm_vec_dist(seg1vert, seg2vert); |
| 1071 |
} |
| 1072 |
|
| 1073 |
return distsum; |
| 1074 |
|
| 1075 |
} |
| 1076 |
|
| 1077 |
// ---------------------------------------------------------------------------- |
| 1078 |
// Determine how to connect two segments together with the least amount of twisting. |
| 1079 |
// Returns vertex index in 0..3 on first segment. Assumed ordering of vertices |
| 1080 |
// on second segment is 0,1,2,3. |
| 1081 |
// So, if return value is 2, connect 2:0 3:1 0:2 1:3. |
| 1082 |
// Theory: |
| 1083 |
// We select an ordering of vertices for connection. For the first pair of vertices to be connected, |
| 1084 |
// compute the vector. For the three remaining pairs of vertices, compute the vectors from one vertex |
| 1085 |
// to the other. Compute the dot products of these vectors with the original vector. Add them up. |
| 1086 |
// The close we are to 3, the better fit we have. Reason: The largest value for the dot product is |
| 1087 |
// 1.0, and this occurs for a parallel set of vectors. |
| 1088 |
static int get_index_of_best_fit(const shared_segment &seg1, const sidenum_t side1, const shared_segment &seg2, const sidenum_t side2) |
| 1089 |
{ |
| 1090 |
int firstv; |
| 1091 |
fix min_distance; |
| 1092 |
int best_index{0}; |
| 1093 |
|
| 1094 |
min_distance = F1_0*30000; |
| 1095 |
|
| 1096 |
for (firstv=0; firstv<4; firstv++) { |
| 1097 |
fix t; |
| 1098 |
t = seg_seg_vertex_distsum(seg1, side1, seg2, side2, firstv); |
| 1099 |
if (t <= min_distance) { |
| 1100 |
min_distance = t; |
| 1101 |
best_index = firstv; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
return best_index; |
| 1106 |
|
| 1107 |
} |
| 1108 |
|
| 1109 |
} |
| 1110 |
|
| 1111 |
#define MAX_VALIDATIONS 50 |
| 1112 |
|
| 1113 |
// ---------------------------------------------------------------------------- |
| 1114 |
// Modify seg2 to share side2 with seg1:side1. This forms a connection between |
| 1115 |
// two segments without creating a new segment. It modifies seg2 by sharing |
| 1116 |
// vertices from seg1. seg1 is not modified. Four vertices from seg2 are |
| 1117 |
// deleted. |
| 1118 |
// Return code: |
| 1119 |
// 0 joint formed |
| 1120 |
// 1 -- no, this is legal! -- unable to form joint because one or more vertices of side2 is not free |
| 1121 |
// 2 unable to form joint because side1 is already used |
| 1122 |
int med_form_joint(const vmsegptridx_t seg1, const sidenum_t side1, const vmsegptridx_t seg2, const sidenum_t side2) |
| 1123 |
{ |
| 1124 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1125 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1126 |
int bfi,s1; |
| 1127 |
std::array<segnum_t, MAX_VALIDATIONS> validation_list; |
| 1128 |
uint_fast32_t nv; |
| 1129 |
|
| 1130 |
// Make sure that neither side is connected. |
| 1131 |
if (IS_CHILD(seg1->children[side1]) || IS_CHILD(seg2->children[side2])) |
| 1132 |
return 2; |
| 1133 |
|
| 1134 |
// Make sure there is no wall there |
| 1135 |
if ((seg1->shared_segment::sides[side1].wall_num != wall_none) || (seg2->shared_segment::sides[side2].wall_num != wall_none)) |
| 1136 |
return 2; |
| 1137 |
|
| 1138 |
// We can form the joint. Find the best orientation of vertices. |
| 1139 |
bfi = get_index_of_best_fit(seg1, side1, seg2, side2); |
| 1140 |
|
| 1141 |
auto &vp1 = Side_to_verts[side1]; |
| 1142 |
auto &vp2 = Side_to_verts[side2]; |
| 1143 |
|
| 1144 |
// Make a copy of the list of vertices in seg2 which will be deleted and set the |
| 1145 |
// associated vertex number, so that all occurrences of the vertices can be replaced. |
| 1146 |
std::array<vertnum_t, 4> lost_vertices, remap_vertices; |
| 1147 |
for (auto &&[lv, vi] : zip(lost_vertices, vp2)) |
| 1148 |
lv = seg2->verts[vi]; |
| 1149 |
|
| 1150 |
// Now, for each vertex in lost_vertices, determine which vertex it maps to. |
| 1151 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 1152 |
remap_vertices[3 - ((underlying_value(v) + bfi) % 4)] = seg1->verts[vp1[v]]; |
| 1153 |
|
| 1154 |
// Now, in all segments, replace all occurrences of vertices in lost_vertices with remap_vertices |
| 1155 |
|
| 1156 |
// Put the one segment we know are being modified into the validation list. |
| 1157 |
// Note: seg1 does not require a full validation, only a validation of the affected side. Its vertices do not move. |
| 1158 |
nv = 1; |
| 1159 |
validation_list[0] = seg2; |
| 1160 |
|
| 1161 |
for (auto &&[lv, rv] : zip(lost_vertices, remap_vertices)) |
| 1162 |
range_for (const auto &&segp, vmsegptridx) |
| 1163 |
{ |
| 1164 |
if (segp->segnum != segment_none) |
| 1165 |
range_for (auto &sv, segp->verts) |
| 1166 |
if (sv == lv) { |
| 1167 |
sv = rv; |
| 1168 |
// Add segment to list of segments to be validated. |
| 1169 |
for (s1=0; s1<nv; s1++) |
| 1170 |
if (validation_list[s1] == segp) |
| 1171 |
break; |
| 1172 |
if (s1 == nv) |
| 1173 |
validation_list[nv++] = segp; |
| 1174 |
Assert(nv < MAX_VALIDATIONS); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
// Form new connections. |
| 1179 |
seg1->children[side1] = seg2; |
| 1180 |
seg2->children[side2] = seg1; |
| 1181 |
|
| 1182 |
// validate all segments |
| 1183 |
auto &vcvertptr = Vertices.vcptr; |
| 1184 |
validate_segment_side(vcvertptr, seg1, side1); |
| 1185 |
range_for (auto &s, partial_const_range(validation_list, nv)) |
| 1186 |
{ |
| 1187 |
const auto &&segp = seg1.absolute_sibling(s); |
| 1188 |
validate_segment(vcvertptr, segp); |
| 1189 |
warn_if_concave_segment(segp); |
| 1190 |
} |
| 1191 |
|
| 1192 |
set_vertex_counts(); |
| 1193 |
|
| 1194 |
return 0; |
| 1195 |
} |
| 1196 |
|
| 1197 |
// ---------------------------------------------------------------------------- |
| 1198 |
// Create a new segment and use it to form a bridge between two existing segments. |
| 1199 |
// Specify two segment:side pairs. If either segment:side is not open (ie, segment->children[side] != -1) |
| 1200 |
// then it is not legal to form the brider. |
| 1201 |
// Return: |
| 1202 |
// 0 bridge segment formed |
| 1203 |
// 1 unable to form bridge because one (or both) of the sides is not open. |
| 1204 |
// Note that no new vertices are created by this process. |
| 1205 |
int med_form_bridge_segment(const vmsegptridx_t seg1, const sidenum_t side1, const vmsegptridx_t seg2, const sidenum_t side2) |
| 1206 |
{ |
| 1207 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1208 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1209 |
|
| 1210 |
if (IS_CHILD(seg1->children[side1]) || IS_CHILD(seg2->children[side2])) |
| 1211 |
return 1; |
| 1212 |
|
| 1213 |
const auto &&bs = seg1.absolute_sibling(get_free_segment_number(Segments)); |
| 1214 |
shared_segment &sbs = *bs; |
| 1215 |
unique_segment &ubs = *bs; |
| 1216 |
sbs.segnum = bs; |
| 1217 |
ubs.objects = object_none; |
| 1218 |
|
| 1219 |
// Copy vertices from seg2 into last 4 vertices of bridge segment. |
| 1220 |
{ |
| 1221 |
auto &sv = Side_to_verts[side2]; |
| 1222 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 1223 |
sbs.verts[(segment_relative_vertnum{static_cast<uint8_t>((3 - underlying_value(v)) + 4)})] = seg2->verts[sv[v]]; |
| 1224 |
} |
| 1225 |
|
| 1226 |
// Copy vertices from seg1 into first 4 vertices of bridge segment. |
| 1227 |
const auto bfi = get_index_of_best_fit(seg1, side1, seg2, side2); |
| 1228 |
|
| 1229 |
{ |
| 1230 |
auto &sv = Side_to_verts[side1]; |
| 1231 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 1232 |
bs->verts[(segment_relative_vertnum{static_cast<uint8_t>((underlying_value(v) + bfi) % 4)})] = seg1->verts[sv[v]]; |
| 1233 |
} |
| 1234 |
|
| 1235 |
// Form connections to children, first initialize all to unconnected. |
| 1236 |
for (auto &&[child, side] : zip(sbs.children, sbs.sides)) |
| 1237 |
{ |
| 1238 |
child = segment_none; |
| 1239 |
side.wall_num = wall_none; |
| 1240 |
} |
| 1241 |
|
| 1242 |
// Now form connections between segments. |
| 1243 |
|
| 1244 |
bs->children[AttachSide] = seg1; |
| 1245 |
bs->children[Side_opposite[AttachSide]] = seg2; |
| 1246 |
|
| 1247 |
seg1->children[side1] = bs; //seg2 - Segments; |
| 1248 |
seg2->children[side2] = bs; //seg1 - Segments; |
| 1249 |
|
| 1250 |
// Validate bridge segment, and if degenerate, clean up mess. |
| 1251 |
Degenerate_segment_found = 0; |
| 1252 |
|
| 1253 |
auto &vcvertptr = Vertices.vcptr; |
| 1254 |
validate_segment(vcvertptr, bs); |
| 1255 |
|
| 1256 |
if (Degenerate_segment_found) { |
| 1257 |
seg1->children[side1] = segment_none; |
| 1258 |
seg2->children[side2] = segment_none; |
| 1259 |
bs->children[AttachSide] = segment_none; |
| 1260 |
bs->children[Side_opposite[AttachSide]] = segment_none; |
| 1261 |
if (med_delete_segment(bs)) { |
| 1262 |
Int3(); |
| 1263 |
} |
| 1264 |
editor_status("Bridge segment would be degenerate, not created.\n"); |
| 1265 |
return 1; |
| 1266 |
} else { |
| 1267 |
validate_segment(vcvertptr, seg1); // used to only validate side, but segment does more error checking: ,side1); |
| 1268 |
validate_segment(vcvertptr, seg2); // ,side2); |
| 1269 |
med_propagate_tmaps_to_segments(seg1,bs,0); |
| 1270 |
|
| 1271 |
editor_status("Bridge segment formed."); |
| 1272 |
warn_if_concave_segment(bs); |
| 1273 |
return 0; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
// ------------------------------------------------------------------------------- |
| 1278 |
// Create a segment given center, dimensions, rotation matrix. |
| 1279 |
// Note that the created segment will always have planar sides and rectangular cross sections. |
| 1280 |
// It will be created with walls on all sides, ie not connected to anything. |
| 1281 |
void med_create_segment(const vmsegptridx_t sp,fix cx, fix cy, fix cz, fix length, fix width, fix height, const vms_matrix &mp) |
| 1282 |
{ |
| 1283 |
unique_segment &usp = sp; |
| 1284 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1285 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1286 |
++ LevelSharedSegmentState.Num_segments; |
| 1287 |
|
| 1288 |
sp->segnum = segnum_t{1}; // What to put here? I don't know. |
| 1289 |
|
| 1290 |
// Form connections to children, of which it has none. |
| 1291 |
for (auto &&[child, side] : zip(sp->children, sp->shared_segment::sides)) |
| 1292 |
{ |
| 1293 |
child = segment_none; |
| 1294 |
side.wall_num = wall_none; |
| 1295 |
} |
| 1296 |
|
| 1297 |
sp->group = -1; |
| 1298 |
sp->matcen_num = materialization_center_number::None; |
| 1299 |
|
| 1300 |
// Create relative-to-center vertices, which are the rotated points on the box defined by length, width, height |
| 1301 |
sp->verts[segment_relative_vertnum::_0] = med_add_vertex(vertex{vm_vec_build_rotated({+width/2, +height/2, -length/2}, mp)}); |
| 1302 |
sp->verts[segment_relative_vertnum::_1] = med_add_vertex(vertex{vm_vec_build_rotated({+width/2, -height/2, -length/2}, mp)}); |
| 1303 |
sp->verts[segment_relative_vertnum::_2] = med_add_vertex(vertex{vm_vec_build_rotated({-width/2, -height/2, -length/2}, mp)}); |
| 1304 |
sp->verts[segment_relative_vertnum::_3] = med_add_vertex(vertex{vm_vec_build_rotated({-width/2, +height/2, -length/2}, mp)}); |
| 1305 |
sp->verts[segment_relative_vertnum::_4] = med_add_vertex(vertex{vm_vec_build_rotated({+width/2, +height/2, +length/2}, mp)}); |
| 1306 |
sp->verts[segment_relative_vertnum::_5] = med_add_vertex(vertex{vm_vec_build_rotated({+width/2, -height/2, +length/2}, mp)}); |
| 1307 |
sp->verts[segment_relative_vertnum::_6] = med_add_vertex(vertex{vm_vec_build_rotated({-width/2, -height/2, +length/2}, mp)}); |
| 1308 |
sp->verts[segment_relative_vertnum::_7] = med_add_vertex(vertex{vm_vec_build_rotated({-width/2, +height/2, +length/2}, mp)}); |
| 1309 |
|
| 1310 |
// Now create the vector which is the center of the segment and add that to all vertices. |
| 1311 |
const vms_vector cv{cx, cy, cz}; |
| 1312 |
|
| 1313 |
// Now, add the center to all vertices, placing the segment in 3 space. |
| 1314 |
auto &vmvertptr = Vertices.vmptr; |
| 1315 |
range_for (auto &i, sp->verts) |
| 1316 |
vm_vec_add2(vmvertptr(i), cv); |
| 1317 |
|
| 1318 |
// Set scale vector. |
| 1319 |
// sp->scale.x = width; |
| 1320 |
// sp->scale.y = height; |
| 1321 |
// sp->scale.z = length; |
| 1322 |
|
| 1323 |
// Add faces to all sides. |
| 1324 |
auto &vcvertptr = Vertices.vcptr; |
| 1325 |
for (const auto f : MAX_SIDES_PER_SEGMENT) |
| 1326 |
create_walls_on_side(vcvertptr, sp, f); |
| 1327 |
|
| 1328 |
usp.objects = object_none; //no objects in this segment |
| 1329 |
|
| 1330 |
// Assume nothing special about this segment |
| 1331 |
sp->special = segment_special::nothing; |
| 1332 |
sp->station_idx = station_number::None; |
| 1333 |
usp.static_light = 0; |
| 1334 |
sp->matcen_num = materialization_center_number::None; |
| 1335 |
|
| 1336 |
copy_tmaps_to_segment(sp, vcsegptr(&New_segment)); |
| 1337 |
|
| 1338 |
assign_default_uvs_to_segment(sp); |
| 1339 |
} |
| 1340 |
|
| 1341 |
// ---------------------------------------------------------------------------------------------- |
| 1342 |
// Create New_segment using a specified scale factor. |
| 1343 |
void med_create_new_segment(const vms_vector &scale) |
| 1344 |
{ |
| 1345 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1346 |
const auto &&sp = vmsegptridx(&New_segment); |
| 1347 |
unique_segment &usp = sp; |
| 1348 |
fix length,width,height; |
| 1349 |
|
| 1350 |
length = scale.z; |
| 1351 |
width = scale.x; |
| 1352 |
height = scale.y; |
| 1353 |
|
| 1354 |
sp->segnum = segnum_t{1}; // What to put here? I don't know. |
| 1355 |
|
| 1356 |
// Create relative-to-center vertices, which are the points on the box defined by length, width, height |
| 1357 |
auto &verts = sp->verts; |
| 1358 |
verts[segment_relative_vertnum::_0] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 0}, {+width / 2, +height / 2, -length / 2}); |
| 1359 |
verts[segment_relative_vertnum::_1] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 1}, {+width / 2, -height / 2, -length / 2}); |
| 1360 |
verts[segment_relative_vertnum::_2] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 2}, {-width / 2, -height / 2, -length / 2}); |
| 1361 |
verts[segment_relative_vertnum::_3] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 3}, {-width / 2, +height / 2, -length / 2}); |
| 1362 |
verts[segment_relative_vertnum::_4] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 4}, {+width / 2, +height / 2, +length / 2}); |
| 1363 |
verts[segment_relative_vertnum::_5] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 5}, {+width / 2, -height / 2, +length / 2}); |
| 1364 |
verts[segment_relative_vertnum::_6] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 6}, {-width / 2, -height / 2, +length / 2}); |
| 1365 |
verts[segment_relative_vertnum::_7] = med_set_vertex(LevelSharedVertexState, vertnum_t{NEW_SEGMENT_VERTICES + 7}, {-width / 2, +height / 2, +length / 2}); |
| 1366 |
|
| 1367 |
// sp->scale = *scale; |
| 1368 |
|
| 1369 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1370 |
auto &vcvertptr = Vertices.vcptr; |
| 1371 |
// Form connections to children, of which it has none, init faces and tmaps. |
| 1372 |
for (const auto &&[s, child, ss, us] : enumerate(zip(sp->children, sp->shared_segment::sides, sp->unique_segment::sides))) |
| 1373 |
{ |
| 1374 |
child = segment_none; |
| 1375 |
ss.wall_num = wall_none; |
| 1376 |
create_walls_on_side(vcvertptr, sp, static_cast<sidenum_t>(s)); |
| 1377 |
us.tmap_num = build_texture1_value(1); // assign some stupid old tmap to this side. |
| 1378 |
us.tmap_num2 = texture2_value::None; |
| 1379 |
} |
| 1380 |
|
| 1381 |
Seg_orientation = {}; |
| 1382 |
|
| 1383 |
usp.objects = object_none; //no objects in this segment |
| 1384 |
|
| 1385 |
assign_default_uvs_to_segment(sp); |
| 1386 |
|
| 1387 |
// Assume nothing special about this segment |
| 1388 |
sp->special = segment_special::nothing; |
| 1389 |
sp->station_idx = station_number::None; |
| 1390 |
usp.static_light = 0; |
| 1391 |
sp->matcen_num = materialization_center_number::None; |
| 1392 |
} |
| 1393 |
|
| 1394 |
// ------------------------------------------------------------------------------- |
| 1395 |
void med_create_new_segment_from_cursegp(void) |
| 1396 |
{ |
| 1397 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1398 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1399 |
vms_vector scalevec; |
| 1400 |
|
| 1401 |
const auto uvec{med_extract_up_vector_from_segment_side(Cursegp, Curside)}; |
| 1402 |
const auto rvec{med_extract_right_vector_from_segment_side(Cursegp, Curside)}; |
| 1403 |
auto &vcvertptr = Vertices.vcptr; |
| 1404 |
const auto fvec{extract_forward_vector_from_segment(vcvertptr, Cursegp)}; |
| 1405 |
|
| 1406 |
scalevec.x = vm_vec_mag(rvec); |
| 1407 |
scalevec.y = vm_vec_mag(uvec); |
| 1408 |
scalevec.z = vm_vec_mag(fvec); |
| 1409 |
med_create_new_segment(scalevec); |
| 1410 |
} |
| 1411 |
|
| 1412 |
// ------------------------------------------------------------------------------- |
| 1413 |
// Initialize all vertices to inactive status. |
| 1414 |
void init_all_vertices(void) |
| 1415 |
{ |
| 1416 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1417 |
auto &Vertex_active = LevelSharedVertexState.get_vertex_active(); |
| 1418 |
Vertex_active = {}; |
| 1419 |
range_for (auto &s, Segments) |
| 1420 |
s.segnum = segment_none; |
| 1421 |
} |
| 1422 |
|
| 1423 |
// ----------------------------------------------------------------------------- |
| 1424 |
// Create coordinate axes in orientation of specified segment, stores vertices at *vp. |
| 1425 |
void create_coordinate_axes_from_segment(const shared_segment &sp, std::array<vertnum_t, 16> &vertnums) |
| 1426 |
{ |
| 1427 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1428 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1429 |
vms_vector t; |
| 1430 |
|
| 1431 |
auto rotmat{med_extract_matrix_from_segment(sp)}; |
| 1432 |
|
| 1433 |
auto &vcvertptr = Vertices.vcptr; |
| 1434 |
auto &vmvertptr = Vertices.vmptr; |
| 1435 |
vms_vector &v0{*vmvertptr(vertnums[0])}; |
| 1436 |
v0 = compute_segment_center(vcvertptr, sp); |
| 1437 |
|
| 1438 |
t = rotmat.rvec; |
| 1439 |
vm_vec_scale(t,i2f(32)); |
| 1440 |
vm_vec_add(vmvertptr(vertnums[1]), v0, t); |
| 1441 |
|
| 1442 |
t = rotmat.uvec; |
| 1443 |
vm_vec_scale(t,i2f(32)); |
| 1444 |
vm_vec_add(vmvertptr(vertnums[2]), v0, t); |
| 1445 |
|
| 1446 |
t = rotmat.fvec; |
| 1447 |
vm_vec_scale(t,i2f(32)); |
| 1448 |
vm_vec_add(vmvertptr(vertnums[3]), v0, t); |
| 1449 |
} |
| 1450 |
|
| 1451 |
namespace { |
| 1452 |
|
| 1453 |
// ----------------------------------------------------------------------------- |
| 1454 |
// Determine if a segment is concave. Returns true if concave |
| 1455 |
static int check_seg_concavity(const shared_segment &s) |
| 1456 |
{ |
| 1457 |
vms_vector n0; |
| 1458 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1459 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1460 |
auto &vcvertptr = Vertices.vcptr; |
| 1461 |
range_for (auto &sn, Side_to_verts) |
| 1462 |
for (unsigned vn = 0; vn <= 4; ++vn) |
| 1463 |
{ |
| 1464 |
const auto n1{vm_vec_normal( |
| 1465 |
vcvertptr(s.verts[sn[static_cast<side_relative_vertnum>(vn % 4)]]), |
| 1466 |
vcvertptr(s.verts[sn[static_cast<side_relative_vertnum>((vn + 1) % 4)]]), |
| 1467 |
vcvertptr(s.verts[sn[static_cast<side_relative_vertnum>((vn + 2) % 4)]]) |
| 1468 |
)}; |
| 1469 |
|
| 1470 |
//vm_vec_normalize(&n1); |
| 1471 |
|
| 1472 |
if (vn>0) if (vm_vec_build_dot(n0,n1) < f0_5) return 1; |
| 1473 |
|
| 1474 |
n0 = n1; |
| 1475 |
} |
| 1476 |
|
| 1477 |
return 0; |
| 1478 |
} |
| 1479 |
|
| 1480 |
} |
| 1481 |
|
| 1482 |
// ----------------------------------------------------------------------------- |
| 1483 |
// Find all concave segments and add to list |
| 1484 |
void find_concave_segs() |
| 1485 |
{ |
| 1486 |
Warning_segs.clear(); |
| 1487 |
|
| 1488 |
range_for (const auto &&s, vcsegptridx) |
| 1489 |
if (s->segnum != segment_none) |
| 1490 |
if (check_seg_concavity(s)) |
| 1491 |
Warning_segs.emplace_back(s); |
| 1492 |
} |
| 1493 |
|
| 1494 |
|
| 1495 |
// ----------------------------------------------------------------------------- |
| 1496 |
void warn_if_concave_segments(void) |
| 1497 |
{ |
| 1498 |
find_concave_segs(); |
| 1499 |
|
| 1500 |
if (!Warning_segs.empty()) |
| 1501 |
{ |
| 1502 |
editor_status_fmt("*** WARNING *** %d concave segments in mine! *** WARNING ***", Warning_segs.size()); |
| 1503 |
} |
| 1504 |
} |
| 1505 |
|
| 1506 |
// ----------------------------------------------------------------------------- |
| 1507 |
// Check segment s, if concave, warn |
| 1508 |
void warn_if_concave_segment(const vmsegptridx_t s) |
| 1509 |
{ |
| 1510 |
int result; |
| 1511 |
|
| 1512 |
result = check_seg_concavity(s); |
| 1513 |
|
| 1514 |
if (result) { |
| 1515 |
Warning_segs.emplace_back(s); |
| 1516 |
|
| 1517 |
editor_status("*** WARNING *** New segment is concave! *** WARNING ***"); |
| 1518 |
} //else |
| 1519 |
//editor_status(""); |
| 1520 |
} |
| 1521 |
|
| 1522 |
|
| 1523 |
// ------------------------------------------------------------------------------- |
| 1524 |
// Find segment adjacent to sp:side. |
| 1525 |
// Adjacent means a segment which shares all four vertices. |
| 1526 |
// If found, return a pair containing the found segment and side. |
| 1527 |
// If not found, return an empty optional. |
| 1528 |
std::optional<std::pair<vmsegptridx_t, sidenum_t>> med_find_adjacent_segment_side(const vmsegptridx_t sp, sidenum_t side) |
| 1529 |
{ |
| 1530 |
enumerated_array<vertnum_t, 4, side_relative_vertnum> abs_verts; |
| 1531 |
|
| 1532 |
// Stuff abs_verts[4] array with absolute vertex indices |
| 1533 |
for (const auto v : MAX_VERTICES_PER_SIDE) |
| 1534 |
abs_verts[v] = sp->verts[Side_to_verts[side][v]]; |
| 1535 |
|
| 1536 |
// Scan all segments, looking for a segment which contains the four abs_verts |
| 1537 |
range_for (const auto &&segp, vmsegptridx) |
| 1538 |
{ |
| 1539 |
if (segp != sp) |
| 1540 |
{ |
| 1541 |
range_for (auto &v, abs_verts) |
| 1542 |
{ // do for each vertex in abs_verts |
| 1543 |
range_for (auto &vv, segp->verts) // do for each vertex in segment |
| 1544 |
if (v == vv) |
| 1545 |
goto fass_found1; // Current vertex (indexed by v) is present in segment, try next |
| 1546 |
goto fass_next_seg; // This segment doesn't contain the vertex indexed by v |
| 1547 |
fass_found1: ; |
| 1548 |
} // end for v |
| 1549 |
|
| 1550 |
// All four vertices in sp:side are present in segment seg. |
| 1551 |
// Determine side and return |
| 1552 |
for (const auto &&[idx, value] : enumerate(Side_to_verts)) |
| 1553 |
{ |
| 1554 |
for (const auto v : value) |
| 1555 |
{ |
| 1556 |
range_for (auto &vv, abs_verts) |
| 1557 |
{ |
| 1558 |
if (segp->verts[v] == vv) |
| 1559 |
goto fass_found2; |
| 1560 |
} |
| 1561 |
goto fass_next_side; // Couldn't find vertex v in current side, so try next side. |
| 1562 |
fass_found2: ; |
| 1563 |
} |
| 1564 |
// Found all four vertices in current side. We are done! |
| 1565 |
return std::pair(segp, static_cast<sidenum_t>(idx)); |
| 1566 |
fass_next_side: ; |
| 1567 |
} |
| 1568 |
Assert(0); // Impossible -- we identified this segment as containing all 4 vertices of side "side", but we couldn't find them. |
| 1569 |
return std::nullopt; |
| 1570 |
fass_next_seg: ; |
| 1571 |
} |
| 1572 |
} |
| 1573 |
return std::nullopt; |
| 1574 |
} |
| 1575 |
|
| 1576 |
// ------------------------------------------------------------------------------- |
| 1577 |
// Find segment closest to sp:side. |
| 1578 |
// If a segment is found, return a `std::optional` that contains the found segment and side. |
| 1579 |
// If no segment is found, return an empty `std::optional`. |
| 1580 |
std::optional<std::pair<vmsegptridx_t, sidenum_t>> med_find_closest_threshold_segment_side(const vmsegptridx_t sp, sidenum_t side, const fix threshold) |
| 1581 |
{ |
| 1582 |
std::optional<std::pair<vmsegptridx_t, sidenum_t>> result; |
| 1583 |
if (IS_CHILD(sp->children[side])) |
| 1584 |
return result; |
| 1585 |
auto &vcvertptr{LevelSharedSegmentState.get_vertex_state().get_vertices().vcptr}; |
| 1586 |
const auto vsc{compute_center_point_on_side(vcvertptr, sp, side)}; |
| 1587 |
|
| 1588 |
fix closest_seg_dist{threshold}; |
| 1589 |
|
| 1590 |
// Scan all segments, looking for a segment which contains the four abs_verts |
| 1591 |
range_for (const auto &&segp, vmsegptridx) |
| 1592 |
{ |
| 1593 |
if (segp != sp) |
| 1594 |
for (const auto &&[idx, value] : enumerate(segp->children)) |
| 1595 |
{ |
| 1596 |
if (!IS_CHILD(value)) |
| 1597 |
{ |
| 1598 |
const auto vtc{compute_center_point_on_side(vcvertptr, segp, idx)}; |
| 1599 |
const auto current_dist = vm_vec_dist(vsc, vtc); |
| 1600 |
if (closest_seg_dist > current_dist) |
| 1601 |
{ |
| 1602 |
result = {segp, static_cast<sidenum_t>(idx)}; |
| 1603 |
closest_seg_dist = current_dist; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
} |
| 1607 |
} |
| 1608 |
return result; |
| 1609 |
} |
| 1610 |
|
| 1611 |
} |