| 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 |
* group functions |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <ranges> |
| 29 |
|
| 30 |
#include "gr.h" |
| 31 |
#include "ui.h" |
| 32 |
#include "inferno.h" |
| 33 |
#include "segment.h" |
| 34 |
#include "editor/editor.h" |
| 35 |
#include "editor/esegment.h" |
| 36 |
#include "editor/medmisc.h" |
| 37 |
#include "dxxerror.h" |
| 38 |
#include "physfsx.h" |
| 39 |
#include "gameseg.h" |
| 40 |
#include "bm.h" // For MAX_TEXTURES. |
| 41 |
#include "textures.h" |
| 42 |
#include "hash.h" |
| 43 |
#include "kdefs.h" |
| 44 |
#include "fwd-wall.h" |
| 45 |
#include "medwall.h" |
| 46 |
#include "compiler-range_for.h" |
| 47 |
#include "d_bitset.h" |
| 48 |
#include "d_enumerate.h" |
| 49 |
#include "d_levelstate.h" |
| 50 |
#include "d_zip.h" |
| 51 |
#include "partial_range.h" |
| 52 |
#include "segiter.h" |
| 53 |
|
| 54 |
namespace { |
| 55 |
|
| 56 |
static void validate_selected_segments(void); |
| 57 |
|
| 58 |
#if 0 |
| 59 |
struct group_top_fileinfo { |
| 60 |
int fileinfo_version; |
| 61 |
int fileinfo_sizeof; |
| 62 |
} group_top_fileinfo; // Should be same as first two fields below... |
| 63 |
|
| 64 |
struct group_fileinfo { |
| 65 |
int fileinfo_version; |
| 66 |
int fileinfo_sizeof; |
| 67 |
int header_offset; // Stuff common to game & editor |
| 68 |
int header_size; |
| 69 |
int editor_offset; // Editor specific stuff |
| 70 |
int editor_size; |
| 71 |
int vertex_offset; |
| 72 |
int vertex_howmany; |
| 73 |
int vertex_sizeof; |
| 74 |
int segment_offset; |
| 75 |
int segment_howmany; |
| 76 |
int segment_sizeof; |
| 77 |
int texture_offset; |
| 78 |
uint32_t texture_howmany; |
| 79 |
int texture_sizeof; |
| 80 |
} group_fileinfo; |
| 81 |
|
| 82 |
struct group_header { |
| 83 |
int num_vertices; |
| 84 |
int num_segments; |
| 85 |
} group_header; |
| 86 |
|
| 87 |
struct group_editor { |
| 88 |
int current_seg; |
| 89 |
int newsegment_offset; |
| 90 |
int newsegment_size; |
| 91 |
int Groupsegp; |
| 92 |
int Groupside; |
| 93 |
} group_editor; |
| 94 |
#endif |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
std::array<group, MAX_GROUPS+1> GroupList; |
| 99 |
std::array<segment *, MAX_GROUPS+1> Groupsegp; |
| 100 |
std::array<sidenum_t, MAX_GROUPS+1> Groupside; |
| 101 |
std::array<int, MAX_GROUPS+1> Group_orientation; |
| 102 |
int current_group=-1; |
| 103 |
unsigned num_groups; |
| 104 |
|
| 105 |
// -- void swap_negate_columns(vms_matrix *rotmat, int col1, int col2) |
| 106 |
// -- { |
| 107 |
// -- fix col1_1,col1_2,col1_3; |
| 108 |
// -- fix col2_1,col2_2,col2_3; |
| 109 |
// -- |
| 110 |
// -- switch (col1) { |
| 111 |
// -- case 0: |
| 112 |
// -- col1_1 = rotmat->m1; |
| 113 |
// -- col1_2 = rotmat->m2; |
| 114 |
// -- col1_3 = rotmat->m3; |
| 115 |
// -- break; |
| 116 |
// -- |
| 117 |
// -- case 1: |
| 118 |
// -- col1_1 = rotmat->m4; |
| 119 |
// -- col1_2 = rotmat->m5; |
| 120 |
// -- col1_3 = rotmat->m6; |
| 121 |
// -- break; |
| 122 |
// -- |
| 123 |
// -- case 2: |
| 124 |
// -- col1_1 = rotmat->m7; |
| 125 |
// -- col1_2 = rotmat->m8; |
| 126 |
// -- col1_3 = rotmat->m9; |
| 127 |
// -- break; |
| 128 |
// -- } |
| 129 |
// -- |
| 130 |
// -- switch (col2) { |
| 131 |
// -- case 0: |
| 132 |
// -- col2_1 = rotmat->m1; |
| 133 |
// -- col2_2 = rotmat->m2; |
| 134 |
// -- col2_3 = rotmat->m3; |
| 135 |
// -- break; |
| 136 |
// -- |
| 137 |
// -- case 1: |
| 138 |
// -- col2_1 = rotmat->m4; |
| 139 |
// -- col2_2 = rotmat->m5; |
| 140 |
// -- col2_3 = rotmat->m6; |
| 141 |
// -- break; |
| 142 |
// -- |
| 143 |
// -- case 2: |
| 144 |
// -- col2_1 = rotmat->m7; |
| 145 |
// -- col2_2 = rotmat->m8; |
| 146 |
// -- col2_3 = rotmat->m9; |
| 147 |
// -- break; |
| 148 |
// -- } |
| 149 |
// -- |
| 150 |
// -- switch (col2) { |
| 151 |
// -- case 0: |
| 152 |
// -- rotmat->m1 = -col1_1; |
| 153 |
// -- rotmat->m2 = -col1_2; |
| 154 |
// -- rotmat->m3 = -col1_3; |
| 155 |
// -- break; |
| 156 |
// -- |
| 157 |
// -- case 1: |
| 158 |
// -- rotmat->m4 = -col1_1; |
| 159 |
// -- rotmat->m5 = -col1_2; |
| 160 |
// -- rotmat->m6 = -col1_3; |
| 161 |
// -- break; |
| 162 |
// -- |
| 163 |
// -- case 2: |
| 164 |
// -- rotmat->m7 = -col1_1; |
| 165 |
// -- rotmat->m8 = -col1_2; |
| 166 |
// -- rotmat->m9 = -col1_3; |
| 167 |
// -- break; |
| 168 |
// -- } |
| 169 |
// -- |
| 170 |
// -- switch (col1) { |
| 171 |
// -- case 0: |
| 172 |
// -- rotmat->m1 = -col2_1; |
| 173 |
// -- rotmat->m2 = -col2_2; |
| 174 |
// -- rotmat->m3 = -col2_3; |
| 175 |
// -- break; |
| 176 |
// -- |
| 177 |
// -- case 1: |
| 178 |
// -- rotmat->m4 = -col2_1; |
| 179 |
// -- rotmat->m5 = -col2_2; |
| 180 |
// -- rotmat->m6 = -col2_3; |
| 181 |
// -- break; |
| 182 |
// -- |
| 183 |
// -- case 2: |
| 184 |
// -- rotmat->m7 = -col2_1; |
| 185 |
// -- rotmat->m8 = -col2_2; |
| 186 |
// -- rotmat->m9 = -col2_3; |
| 187 |
// -- break; |
| 188 |
// -- } |
| 189 |
// -- |
| 190 |
// -- } |
| 191 |
// -- |
| 192 |
// -- void swap_negate_rows(vms_matrix *rotmat, int row1, int row2) |
| 193 |
// -- { |
| 194 |
// -- fix row1_1,row1_2,row1_3; |
| 195 |
// -- fix row2_1,row2_2,row2_3; |
| 196 |
// -- |
| 197 |
// -- switch (row1) { |
| 198 |
// -- case 0: |
| 199 |
// -- row1_1 = rotmat->m1; |
| 200 |
// -- row1_2 = rotmat->m4; |
| 201 |
// -- row1_3 = rotmat->m7; |
| 202 |
// -- break; |
| 203 |
// -- |
| 204 |
// -- case 1: |
| 205 |
// -- row1_1 = rotmat->m2; |
| 206 |
// -- row1_2 = rotmat->m5; |
| 207 |
// -- row1_3 = rotmat->m8; |
| 208 |
// -- break; |
| 209 |
// -- |
| 210 |
// -- case 2: |
| 211 |
// -- row1_1 = rotmat->m3; |
| 212 |
// -- row1_2 = rotmat->m6; |
| 213 |
// -- row1_3 = rotmat->m9; |
| 214 |
// -- break; |
| 215 |
// -- } |
| 216 |
// -- |
| 217 |
// -- switch (row2) { |
| 218 |
// -- case 0: |
| 219 |
// -- row2_1 = rotmat->m1; |
| 220 |
// -- row2_2 = rotmat->m4; |
| 221 |
// -- row2_3 = rotmat->m7; |
| 222 |
// -- break; |
| 223 |
// -- |
| 224 |
// -- case 1: |
| 225 |
// -- row2_1 = rotmat->m2; |
| 226 |
// -- row2_2 = rotmat->m5; |
| 227 |
// -- row2_3 = rotmat->m8; |
| 228 |
// -- break; |
| 229 |
// -- |
| 230 |
// -- case 2: |
| 231 |
// -- row2_1 = rotmat->m3; |
| 232 |
// -- row2_2 = rotmat->m6; |
| 233 |
// -- row2_3 = rotmat->m9; |
| 234 |
// -- break; |
| 235 |
// -- } |
| 236 |
// -- |
| 237 |
// -- switch (row2) { |
| 238 |
// -- case 0: |
| 239 |
// -- rotmat->m1 = -row1_1; |
| 240 |
// -- rotmat->m4 = -row1_2; |
| 241 |
// -- rotmat->m7 = -row1_3; |
| 242 |
// -- break; |
| 243 |
// -- |
| 244 |
// -- case 1: |
| 245 |
// -- rotmat->m2 = -row1_1; |
| 246 |
// -- rotmat->m5 = -row1_2; |
| 247 |
// -- rotmat->m8 = -row1_3; |
| 248 |
// -- break; |
| 249 |
// -- |
| 250 |
// -- case 2: |
| 251 |
// -- rotmat->m3 = -row1_1; |
| 252 |
// -- rotmat->m6 = -row1_2; |
| 253 |
// -- rotmat->m9 = -row1_3; |
| 254 |
// -- break; |
| 255 |
// -- } |
| 256 |
// -- |
| 257 |
// -- switch (row1) { |
| 258 |
// -- case 0: |
| 259 |
// -- rotmat->m1 = -row2_1; |
| 260 |
// -- rotmat->m4 = -row2_2; |
| 261 |
// -- rotmat->m7 = -row2_3; |
| 262 |
// -- break; |
| 263 |
// -- |
| 264 |
// -- case 1: |
| 265 |
// -- rotmat->m2 = -row2_1; |
| 266 |
// -- rotmat->m5 = -row2_2; |
| 267 |
// -- rotmat->m8 = -row2_3; |
| 268 |
// -- break; |
| 269 |
// -- |
| 270 |
// -- case 2: |
| 271 |
// -- rotmat->m3 = -row2_1; |
| 272 |
// -- rotmat->m6 = -row2_2; |
| 273 |
// -- rotmat->m9 = -row2_3; |
| 274 |
// -- break; |
| 275 |
// -- } |
| 276 |
// -- |
| 277 |
// -- } |
| 278 |
// -- |
| 279 |
// -- // ------------------------------------------------------------------------------------------------ |
| 280 |
// -- void side_based_matrix(vms_matrix *rotmat,int destside) |
| 281 |
// -- { |
| 282 |
// -- vms_angvec rotvec; |
| 283 |
// -- vms_matrix r1,rtemp; |
| 284 |
// -- |
| 285 |
// -- switch (destside) { |
| 286 |
// -- case WLEFT: |
| 287 |
// -- // swap_negate_columns(rotmat,1,2); |
| 288 |
// -- // swap_negate_rows(rotmat,1,2); |
| 289 |
// -- break; |
| 290 |
// -- |
| 291 |
// -- case WTOP: |
| 292 |
// -- break; |
| 293 |
// -- |
| 294 |
// -- case WRIGHT: |
| 295 |
// -- // swap_negate_columns(rotmat,1,2); |
| 296 |
// -- // swap_negate_rows(rotmat,1,2); |
| 297 |
// -- break; |
| 298 |
// -- |
| 299 |
// -- case WBOTTOM: |
| 300 |
// -- break; |
| 301 |
// -- |
| 302 |
// -- case WFRONT: |
| 303 |
// -- break; |
| 304 |
// -- |
| 305 |
// -- case WBACK: |
| 306 |
// -- break; |
| 307 |
// -- } |
| 308 |
// -- |
| 309 |
// -- } |
| 310 |
|
| 311 |
namespace { |
| 312 |
|
| 313 |
// ------------------------------------------------------------------------------------------------ |
| 314 |
// Rotate a group about a point. |
| 315 |
// The segments in the group are indicated (by segment number) in group_seglist. There are group_size segments. |
| 316 |
// The point about which the groups is rotated is the center of first_seg:first_side. |
| 317 |
// delta_flag: |
| 318 |
// 0 absolute rotation, destination specified in terms of base_seg:base_side, used in moving or copying a group |
| 319 |
// 1 relative rotation, destination specified relative to current orientation of first_seg:first_side |
| 320 |
// Note: The group must exist in the mine, consisting of actual points in the world. If any points in the |
| 321 |
// segments in the group are shared by segments not in the group, those points will get rotated and the |
| 322 |
// segments not in the group will have their shapes modified. |
| 323 |
// Return value: |
| 324 |
// 0 group rotated |
| 325 |
// 1 unable to rotate group |
| 326 |
static void med_create_group_rotation_matrix(vms_matrix &result_mat, const unsigned delta_flag, const shared_segment &first_seg, const sidenum_t first_side, const shared_segment &base_seg, const sidenum_t base_side, const vms_matrix &orient_matrix, const int orientation) |
| 327 |
{ |
| 328 |
vms_matrix rotmat2,rotmat3; |
| 329 |
vms_angvec pbh = {0,0,0}; |
| 330 |
|
| 331 |
// Determine whether this rotation is a delta rotation, meaning to just rotate in place, or an absolute rotation, |
| 332 |
// which means that the destination rotation is specified, not as a delta, but as an absolute |
| 333 |
if (delta_flag) { |
| 334 |
// Create rotation matrix describing rotation. |
| 335 |
auto rotmat4{med_extract_matrix_from_segment(first_seg)}; // get rotation matrix describing current orientation of first seg |
| 336 |
update_matrix_based_on_side(rotmat4, first_side); |
| 337 |
rotmat3 = vm_transposed_matrix(orient_matrix); |
| 338 |
const auto vm_desired_orientation = vm_matrix_x_matrix(rotmat4,rotmat3); // this is the desired orientation of the new segment |
| 339 |
vm_transpose_matrix(rotmat4); |
| 340 |
vm_matrix_x_matrix(rotmat2,vm_desired_orientation,rotmat4); // this is the desired orientation of the new segment |
| 341 |
} else { |
| 342 |
// Create rotation matrix describing rotation. |
| 343 |
|
| 344 |
auto rotmat{med_extract_matrix_from_segment(base_seg)}; // get rotation matrix describing desired orientation |
| 345 |
update_matrix_based_on_side(rotmat, base_side); // modify rotation matrix for desired side |
| 346 |
|
| 347 |
// If the new segment is to be attached without rotation, then its orientation is the same as the base_segment |
| 348 |
vms_matrix rotmat4; |
| 349 |
vm_matrix_x_matrix(rotmat4,rotmat,orient_matrix); // this is the desired orientation of the new segment |
| 350 |
|
| 351 |
pbh.b = orientation*16384; |
| 352 |
auto rotmat3{vm_angles_2_matrix(pbh)}; |
| 353 |
rotmat4 = rotmat = vm_matrix_x_matrix(rotmat4, rotmat3); |
| 354 |
rotmat3 = med_extract_matrix_from_segment(first_seg); // get rotation matrix describing current orientation of first seg |
| 355 |
|
| 356 |
// It is curious that the following statement has no analogue in the med_attach_segment_rotated code. |
| 357 |
// Perhaps it is because segments are always attached at their front side. If the back side is the side |
| 358 |
// passed to the function, then the matrix is not modified, which might suggest that what you need to do below |
| 359 |
// is use Side_opposite[first_side]. |
| 360 |
update_matrix_based_on_side(rotmat3, Side_opposite[first_side]); // modify rotation matrix for desired side |
| 361 |
|
| 362 |
vm_transpose_matrix(rotmat3); // get the inverse of the current orientation matrix |
| 363 |
rotmat2 = vm_transposed_matrix(vm_matrix_x_matrix(rotmat,rotmat3)); // now rotmat2 takes the current segment to the desired orientation |
| 364 |
} |
| 365 |
result_mat = rotmat2; |
| 366 |
} |
| 367 |
|
| 368 |
static inline vms_matrix med_create_group_rotation_matrix(const unsigned delta_flag, const shared_segment &first_seg, const sidenum_t first_side, const shared_segment &base_seg, const sidenum_t base_side, const vms_matrix &orient_matrix, const int orientation) |
| 369 |
{ |
| 370 |
vms_matrix result_mat; |
| 371 |
return med_create_group_rotation_matrix(result_mat, delta_flag, first_seg, first_side, base_seg, base_side, orient_matrix, orientation), result_mat; |
| 372 |
} |
| 373 |
|
| 374 |
// ----------------------------------------------------------------------------------------- |
| 375 |
// Rotate all vertices and objects in group. |
| 376 |
static void med_rotate_group(const vms_matrix &rotmat, group::segment_array_type_t &group_seglist, const shared_segment &first_seg, const sidenum_t first_side) |
| 377 |
{ |
| 378 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 379 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 380 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 381 |
auto &vmobjptridx = Objects.vmptridx; |
| 382 |
auto &vcvertptr = Vertices.vcptr; |
| 383 |
auto &vmvertptridx = Vertices.vmptridx; |
| 384 |
const auto rotate_center{compute_center_point_on_side(vcvertptr, first_seg, first_side)}; |
| 385 |
|
| 386 |
// Create list of points to rotate. |
| 387 |
enumerated_bitset<MAX_VERTICES, vertnum_t> vertex_list{}; |
| 388 |
|
| 389 |
range_for (const auto &gs, group_seglist) |
| 390 |
{ |
| 391 |
auto &sp = *vmsegptr(gs); |
| 392 |
|
| 393 |
range_for (const auto v, sp.verts) |
| 394 |
vertex_list[v] = true; |
| 395 |
|
| 396 |
// Rotate center of all objects in group. |
| 397 |
for (auto &obj : objects_in<object_base>(sp, vmobjptridx, vcsegptr)) |
| 398 |
{ |
| 399 |
const auto tv{vm_vec_build_rotated(vm_vec_build_sub(obj.pos, rotate_center), rotmat)}; |
| 400 |
vm_vec_add(obj.pos, tv, rotate_center); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
// Do the pre-rotation xlate, do the rotation, do the post-rotation xlate |
| 405 |
range_for (auto &&v, vmvertptridx) |
| 406 |
if (vertex_list[v]) { |
| 407 |
const auto tv{vm_vec_build_rotated(vm_vec_build_sub(*v, rotate_center), rotmat)}; |
| 408 |
vm_vec_add(*v, tv, rotate_center); |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
// ------------------------------------------------------------------------------------------------ |
| 413 |
static void cgl_aux(const vmsegptridx_t segp, group::segment_array_type_t &seglistp, selected_segment_array_t *ignore_list, visited_segment_bitarray_t &visited) |
| 414 |
{ |
| 415 |
if (ignore_list) |
| 416 |
if (ignore_list->contains(segp)) |
| 417 |
return; |
| 418 |
|
| 419 |
if (auto &&v = visited[segp]; !v) { |
| 420 |
v = true; |
| 421 |
seglistp.emplace_back(segp); |
| 422 |
|
| 423 |
for (const auto c : segp->shared_segment::children) |
| 424 |
if (IS_CHILD(c)) |
| 425 |
cgl_aux(segp.absolute_sibling(c), seglistp, ignore_list, visited); |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
// ------------------------------------------------------------------------------------------------ |
| 430 |
// Sets Been_visited[n] if n is reachable from segp |
| 431 |
static void create_group_list(const vmsegptridx_t segp, group::segment_array_type_t &seglistp, selected_segment_array_t *ignore_list) |
| 432 |
{ |
| 433 |
visited_segment_bitarray_t visited; |
| 434 |
cgl_aux(segp, seglistp, ignore_list, visited); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
#define MXS MAX_SEGMENTS |
| 439 |
#define MXV MAX_VERTICES |
| 440 |
|
| 441 |
// ------------------------------------------------------------------------------------------------ |
| 442 |
static void duplicate_group(enumerated_array<uint8_t, MAX_VERTICES, vertnum_t> &vertex_ids, group::segment_array_type_t &segments) |
| 443 |
{ |
| 444 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 445 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 446 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 447 |
auto &vmobjptridx = Objects.vmptridx; |
| 448 |
group::segment_array_type_t new_segments; |
| 449 |
enumerated_array<vertnum_t, MAX_VERTICES, vertnum_t> new_vertex_ids; // If new_vertex_ids[v] != -1, then vertex v has been remapped to new_vertex_ids[v] |
| 450 |
constexpr vertnum_t undefined_vertex_id{UINT32_MAX}; |
| 451 |
|
| 452 |
// duplicate vertices |
| 453 |
new_vertex_ids.fill(undefined_vertex_id); |
| 454 |
|
| 455 |
// duplicate vertices |
| 456 |
auto &vcvertptridx = Vertices.vcptridx; |
| 457 |
range_for (auto &&v, vcvertptridx) |
| 458 |
{ |
| 459 |
if (const vertnum_t vn{v}; vertex_ids[vn]) |
| 460 |
new_vertex_ids[vn] = med_create_duplicate_vertex(*v); |
| 461 |
} |
| 462 |
|
| 463 |
// duplicate segments |
| 464 |
range_for(const auto &gs, segments) |
| 465 |
{ |
| 466 |
const auto &&segp = vmsegptr(gs); |
| 467 |
const auto &&new_segment_id = med_create_duplicate_segment(Segments, segp); |
| 468 |
new_segments.emplace_back(new_segment_id); |
| 469 |
for (auto &obj : objects_in<object>(segp, vmobjptridx, vmsegptr)) |
| 470 |
{ |
| 471 |
if (obj.type != OBJ_PLAYER) { |
| 472 |
const auto &&new_obj_id = obj_create_copy(obj, vmsegptridx(new_segment_id)); |
| 473 |
(void)new_obj_id; // FIXME! |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
// Now, for each segment in segment_ids, correct its children numbers by translating through new_segment_ids |
| 479 |
// and correct its vertex numbers by translating through new_vertex_ids |
| 480 |
range_for(const auto &gs, new_segments) |
| 481 |
{ |
| 482 |
shared_segment &sp = vmsegptr(gs); |
| 483 |
range_for (auto &seg, sp.children) |
| 484 |
{ |
| 485 |
if (IS_CHILD(seg)) { |
| 486 |
group::segment_array_type_t::iterator inew = new_segments.begin(); |
| 487 |
range_for (const auto i, segments) |
| 488 |
{ |
| 489 |
if (seg == i) |
| 490 |
{ |
| 491 |
seg = *inew; |
| 492 |
break; |
| 493 |
} |
| 494 |
++inew; |
| 495 |
} |
| 496 |
} |
| 497 |
} // end for (sidenum=0... |
| 498 |
|
| 499 |
// Now fixup vertex ids |
| 500 |
range_for (auto &v, sp.verts) |
| 501 |
{ |
| 502 |
if (const vertnum_t vn{v}; vertex_ids[vn]) |
| 503 |
v = new_vertex_ids[vn]; |
| 504 |
} |
| 505 |
} // end for (s=0... |
| 506 |
|
| 507 |
// Now, copy new_segment_ids into segment_ids |
| 508 |
segments = new_segments; |
| 509 |
|
| 510 |
// Now, copy new_vertex_ids into vertex_ids |
| 511 |
vertex_ids = {}; |
| 512 |
|
| 513 |
range_for (auto &v, new_vertex_ids) |
| 514 |
if (v != undefined_vertex_id) |
| 515 |
vertex_ids[v] = 1; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
// ------------------------------------------------------------------------------------------------ |
| 520 |
static int in_group(segnum_t segnum, int group_num) |
| 521 |
{ |
| 522 |
range_for(const auto& s, GroupList[group_num].segments) |
| 523 |
if (segnum == s) |
| 524 |
return 1; |
| 525 |
|
| 526 |
return 0; |
| 527 |
} |
| 528 |
|
| 529 |
// ------------------------------------------------------------------------------------------------ |
| 530 |
// Copy a group of segments. |
| 531 |
// The group is defined as all segments accessible from group_seg. |
| 532 |
// The group is copied so group_seg:group_side is incident upon base_seg:base_side. |
| 533 |
// group_seg and its vertices are bashed to coincide with base_seg. |
| 534 |
// If any vertex of base_seg is contained in a segment that is reachable from group_seg, then errror. |
| 535 |
static int med_copy_group(const unsigned delta_flag, const vmsegptridx_t base_seg, const sidenum_t base_side, vcsegptr_t group_seg, const sidenum_t group_side, const vms_matrix &orient_matrix) |
| 536 |
{ |
| 537 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 538 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 539 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 540 |
auto &vmobjptridx = Objects.vmptridx; |
| 541 |
int x; |
| 542 |
int new_current_group; |
| 543 |
|
| 544 |
if (IS_CHILD(base_seg->shared_segment::children[base_side])) { |
| 545 |
editor_status("Error -- unable to copy group, base_seg:base_side must be free."); |
| 546 |
return 1; |
| 547 |
} |
| 548 |
|
| 549 |
if (num_groups == MAX_GROUPS) { |
| 550 |
x = ui_messagebox( -2, -2, 2, "Warning: You have reached the MAXIMUM group number limit. Continue?", "No", "Yes" ); |
| 551 |
if (x==1) |
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
if (num_groups < MAX_GROUPS) { |
| 556 |
num_groups++; |
| 557 |
new_current_group = num_groups-1; |
| 558 |
} else |
| 559 |
new_current_group = 0; |
| 560 |
|
| 561 |
Assert(current_group >= 0); |
| 562 |
|
| 563 |
// Find groupsegp index |
| 564 |
auto gb = GroupList[current_group].segments.begin(); |
| 565 |
auto ge = GroupList[current_group].segments.end(); |
| 566 |
auto gp = Groupsegp[current_group]; |
| 567 |
const auto &&gi{std::ranges::find(gb, ge, gp, [](const segnum_t segnum) -> const segment * { return vcsegptr(segnum); })}; |
| 568 |
int gs_index = (gi == ge) ? 0 : std::distance(gb, gi); |
| 569 |
|
| 570 |
GroupList[new_current_group] = GroupList[current_group]; |
| 571 |
|
| 572 |
// Make a list of all vertices in group. |
| 573 |
enumerated_array<uint8_t, MAX_VERTICES, vertnum_t> in_vertex_list{}; |
| 574 |
if (group_seg == &New_segment) |
| 575 |
range_for (auto &v, group_seg->verts) |
| 576 |
in_vertex_list[v] = 1; |
| 577 |
else { |
| 578 |
range_for(const auto &gs, GroupList[new_current_group].segments) |
| 579 |
range_for (auto &v, vmsegptr(gs)->verts) |
| 580 |
in_vertex_list[v] = 1; |
| 581 |
} |
| 582 |
|
| 583 |
// Given a list of vertex indices (indicated by !0 in in_vertex_list) and segment indices (in list GroupList[current_group].segments, there |
| 584 |
// are GroupList[current_group].num_segments segments), copy all segments and vertices |
| 585 |
// Return updated lists of vertices and segments in in_vertex_list and GroupList[current_group].segments |
| 586 |
duplicate_group(in_vertex_list, GroupList[new_current_group].segments); |
| 587 |
|
| 588 |
//group_seg = &Segments[GroupList[new_current_group].segments[0]]; // connecting segment in group has been changed, so update group_seg |
| 589 |
|
| 590 |
{ |
| 591 |
const auto &&gs = vmsegptr(GroupList[new_current_group].segments[gs_index]); |
| 592 |
group_seg = gs; |
| 593 |
Groupsegp[new_current_group] = gs; |
| 594 |
} |
| 595 |
Groupside[new_current_group] = Groupside[current_group]; |
| 596 |
|
| 597 |
range_for(const auto &gs, GroupList[new_current_group].segments) |
| 598 |
{ |
| 599 |
shared_segment &s = *vmsegptr(gs); |
| 600 |
s.group = new_current_group; |
| 601 |
s.special = segment_special::nothing; |
| 602 |
s.matcen_num = materialization_center_number::None; |
| 603 |
} |
| 604 |
|
| 605 |
auto &vcvertptr = Vertices.vcptr; |
| 606 |
// Breaking connections between segments in the current group and segments not in the group. |
| 607 |
range_for(const auto &gs, GroupList[new_current_group].segments) |
| 608 |
{ |
| 609 |
const auto &&segp = base_seg.absolute_sibling(gs); |
| 610 |
for (auto &&[sidenum, child_segnum] : enumerate(segp->shared_segment::children)) |
| 611 |
if (IS_CHILD(child_segnum)) |
| 612 |
{ |
| 613 |
if (!in_group(child_segnum, new_current_group)) |
| 614 |
{ |
| 615 |
child_segnum = segment_none; |
| 616 |
validate_segment_side(vcvertptr, segp, sidenum); // we have converted a connection to a side so validate the segment |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
copy_uvs_seg_to_seg(vmsegptr(&New_segment), group_seg); |
| 622 |
|
| 623 |
// Now do the copy |
| 624 |
// First, xlate all vertices so center of group_seg:group_side is at origin |
| 625 |
const auto srcv{compute_center_point_on_side(vcvertptr, group_seg, group_side)}; |
| 626 |
auto &vmvertptridx = Vertices.vmptridx; |
| 627 |
range_for (auto &&v, vmvertptridx) |
| 628 |
if (in_vertex_list[v]) |
| 629 |
vm_vec_sub2(*v, srcv); |
| 630 |
|
| 631 |
// Now, translate all object positions. |
| 632 |
range_for(const auto &segnum, GroupList[new_current_group].segments) |
| 633 |
{ |
| 634 |
for (auto &obj : objects_in<object_base>(vmsegptr(segnum), vmobjptridx, vmsegptr)) |
| 635 |
vm_vec_sub2(obj.pos, srcv); |
| 636 |
} |
| 637 |
|
| 638 |
// Now, rotate segments in group so orientation of group_seg is same as base_seg. |
| 639 |
const auto rotmat = med_create_group_rotation_matrix(delta_flag, group_seg, group_side, base_seg, base_side, orient_matrix, 0); |
| 640 |
med_rotate_group(rotmat, GroupList[new_current_group].segments, group_seg, group_side); |
| 641 |
|
| 642 |
// Now xlate all vertices so group_seg:group_side shares center point with base_seg:base_side |
| 643 |
const auto destv{compute_center_point_on_side(vcvertptr, base_seg, base_side)}; |
| 644 |
range_for (auto &&v, vmvertptridx) |
| 645 |
if (in_vertex_list[v]) |
| 646 |
vm_vec_add2(*v, destv); |
| 647 |
|
| 648 |
// Now, xlate all object positions. |
| 649 |
range_for(const auto &segnum, GroupList[new_current_group].segments) |
| 650 |
{ |
| 651 |
for (auto &obj : objects_in<object_base>(vmsegptr(segnum), vmobjptridx, vmsegptr)) |
| 652 |
vm_vec_add2(obj.pos, destv); |
| 653 |
} |
| 654 |
|
| 655 |
// Now, copy all walls (ie, doors, illusionary, etc.) into the new group. |
| 656 |
copy_group_walls(current_group, new_current_group); |
| 657 |
|
| 658 |
current_group = new_current_group; |
| 659 |
|
| 660 |
// Now, form joint on connecting sides. |
| 661 |
med_form_joint(base_seg,base_side,vmsegptridx(Groupsegp[current_group]),Groupside[new_current_group]); |
| 662 |
|
| 663 |
validate_selected_segments(); |
| 664 |
med_combine_duplicate_vertices(in_vertex_list); |
| 665 |
|
| 666 |
return 0; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
// ------------------------------------------------------------------------------------------------ |
| 671 |
// Move a group of segments. |
| 672 |
// The group is defined as all segments accessible from group_seg. |
| 673 |
// The group is moved so group_seg:group_side is incident upon base_seg:base_side. |
| 674 |
// group_seg and its vertices are bashed to coincide with base_seg. |
| 675 |
// If any vertex of base_seg is contained in a segment that is reachable from group_seg, then errror. |
| 676 |
static int med_move_group(int delta_flag, const vmsegptridx_t base_seg, const sidenum_t base_side, const vmsegptridx_t group_seg, const sidenum_t group_side, const vms_matrix &orient_matrix, int orientation) |
| 677 |
{ |
| 678 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 679 |
auto &Objects = LevelUniqueObjectState.Objects; |
| 680 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 681 |
auto &vmobjptridx = Objects.vmptridx; |
| 682 |
if (IS_CHILD(base_seg->children[base_side])) |
| 683 |
if (base_seg->children[base_side] != group_seg) { |
| 684 |
editor_status("Error -- unable to move group, base_seg:base_side must be free or point to group_seg."); |
| 685 |
return 1; |
| 686 |
} |
| 687 |
|
| 688 |
// // See if any vertices in base_seg are contained in any vertex in group_list |
| 689 |
// for (v=0; v<MAX_VERTICES_PER_SEGMENT; v++) |
| 690 |
// for (s=0; s<GroupList[current_group].num_segments; s++) |
| 691 |
// for (vv=0; vv<MAX_VERTICES_PER_SEGMENT; vv++) |
| 692 |
// if (Segments[GroupList[current_group].segments[s]].verts[vv] == base_seg->verts[v]) { |
| 693 |
// editor_status("Error -- unable to move group, it shares a vertex with destination segment."); |
| 694 |
// return 1; |
| 695 |
// } |
| 696 |
|
| 697 |
enumerated_array<uint8_t, MAX_VERTICES, vertnum_t> in_vertex_list{}; |
| 698 |
enumerated_array<int8_t, MAX_VERTICES, vertnum_t> out_vertex_list{}; |
| 699 |
|
| 700 |
// Make a list of all vertices in group. |
| 701 |
range_for(const auto &gs, GroupList[current_group].segments) |
| 702 |
range_for (auto &v, vmsegptr(gs)->verts) |
| 703 |
in_vertex_list[v] = 1; |
| 704 |
|
| 705 |
// For all segments which are not in GroupList[current_group].segments, mark all their vertices in the out list. |
| 706 |
range_for (const auto &&segp, vmsegptridx) |
| 707 |
{ |
| 708 |
if (!GroupList[current_group].segments.contains(segp)) |
| 709 |
{ |
| 710 |
range_for (auto &v, segp->verts) |
| 711 |
out_vertex_list[v] = 1; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
// Now, for all vertices present in both the in (part of group segment) and out (part of non-group segment) |
| 716 |
// create an extra copy of the vertex so we can just move the ones in the in list. |
| 717 |
// Can't use Highest_vertex_index as loop termination because it gets increased by med_create_duplicate_vertex. |
| 718 |
|
| 719 |
auto &vcvertptr = Vertices.vcptr; |
| 720 |
auto &vmvertptridx = Vertices.vmptridx; |
| 721 |
range_for (auto &&v, vmvertptridx) |
| 722 |
if (in_vertex_list[v]) |
| 723 |
if (out_vertex_list[v]) { |
| 724 |
const auto new_vertex_id = med_create_duplicate_vertex(*v); |
| 725 |
in_vertex_list[v] = 0; |
| 726 |
in_vertex_list[new_vertex_id] = 1; |
| 727 |
|
| 728 |
// Create a new vertex and assign all occurrences of vertex v in IN list to new vertex number. |
| 729 |
range_for(const auto &gs, GroupList[current_group].segments) |
| 730 |
{ |
| 731 |
auto &sp = *vmsegptr(gs); |
| 732 |
range_for (auto &vv, sp.verts) |
| 733 |
if (vv == v) |
| 734 |
vv = new_vertex_id; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
range_for(const auto &gs, GroupList[current_group].segments) |
| 739 |
vmsegptr(gs)->group = current_group; |
| 740 |
|
| 741 |
// Breaking connections between segments in the group and segments not in the group. |
| 742 |
range_for(const auto &gs, GroupList[current_group].segments) |
| 743 |
{ |
| 744 |
const auto &&segp = base_seg.absolute_sibling(gs); |
| 745 |
for (const auto &&[idx0, value0] : enumerate(segp->children)) |
| 746 |
if (IS_CHILD(value0)) |
| 747 |
{ |
| 748 |
const auto &&csegp = base_seg.absolute_sibling(value0); |
| 749 |
if (csegp->group != current_group) |
| 750 |
{ |
| 751 |
for (const auto &&[idx1, value1] : enumerate(csegp->children)) |
| 752 |
if (IS_CHILD(value1)) |
| 753 |
{ |
| 754 |
auto &dsegp = *vmsegptr(value1); |
| 755 |
if (dsegp.group == current_group) |
| 756 |
{ |
| 757 |
value1 = segment_none; |
| 758 |
validate_segment_side(vcvertptr, csegp, static_cast<sidenum_t>(idx1)); // we have converted a connection to a side so validate the segment |
| 759 |
} |
| 760 |
} |
| 761 |
value0 = segment_none; |
| 762 |
validate_segment_side(vcvertptr, segp, static_cast<sidenum_t>(idx0)); // we have converted a connection to a side so validate the segment |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
copy_uvs_seg_to_seg(vmsegptr(&New_segment), vcsegptr(Groupsegp[current_group])); |
| 768 |
|
| 769 |
// Now do the move |
| 770 |
// First, xlate all vertices so center of group_seg:group_side is at origin |
| 771 |
const auto srcv{compute_center_point_on_side(vcvertptr, group_seg, group_side)}; |
| 772 |
range_for (auto &&v, vmvertptridx) |
| 773 |
if (in_vertex_list[v]) |
| 774 |
vm_vec_sub2(*v, srcv); |
| 775 |
|
| 776 |
// Now, move all object positions. |
| 777 |
range_for(const auto &segnum, GroupList[current_group].segments) |
| 778 |
{ |
| 779 |
for (auto &obj : objects_in<object_base>(vmsegptr(segnum), vmobjptridx, vmsegptr)) |
| 780 |
vm_vec_sub2(obj.pos, srcv); |
| 781 |
} |
| 782 |
|
| 783 |
// Now, rotate segments in group so orientation of group_seg is same as base_seg. |
| 784 |
const auto rotmat = med_create_group_rotation_matrix(delta_flag, group_seg, group_side, base_seg, base_side, orient_matrix, orientation); |
| 785 |
med_rotate_group(rotmat, GroupList[current_group].segments, group_seg, group_side); |
| 786 |
|
| 787 |
// Now xlate all vertices so group_seg:group_side shares center point with base_seg:base_side |
| 788 |
const auto destv{compute_center_point_on_side(vcvertptr, base_seg, base_side)}; |
| 789 |
range_for (auto &&v, vmvertptridx) |
| 790 |
if (in_vertex_list[v]) |
| 791 |
vm_vec_add2(*v, destv); |
| 792 |
|
| 793 |
// Now, rotate all object positions. |
| 794 |
range_for(const auto &segnum, GroupList[current_group].segments) |
| 795 |
{ |
| 796 |
for (auto &obj : objects_in<object_base>(vmsegptr(segnum), vmobjptridx, vmsegptr)) |
| 797 |
vm_vec_add2(obj.pos, destv); |
| 798 |
} |
| 799 |
|
| 800 |
// Now, form joint on connecting sides. |
| 801 |
med_form_joint(base_seg,base_side,group_seg,group_side); |
| 802 |
|
| 803 |
validate_selected_segments(); |
| 804 |
med_combine_duplicate_vertices(in_vertex_list); |
| 805 |
|
| 806 |
return 0; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
// ----------------------------------------------------------------------------- |
| 811 |
static segnum_t place_new_segment_in_world(void) |
| 812 |
{ |
| 813 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 814 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 815 |
const auto &&segnum = Segments.vmptridx(get_free_segment_number(Segments)); |
| 816 |
auto &seg = *segnum; |
| 817 |
seg = New_segment; |
| 818 |
|
| 819 |
auto &vcvertptr = Vertices.vcptr; |
| 820 |
for (auto &&[w, r] : zip(seg.verts, New_segment.verts)) |
| 821 |
w = med_create_duplicate_vertex(vcvertptr(r)); |
| 822 |
|
| 823 |
return segnum; |
| 824 |
|
| 825 |
} |
| 826 |
|
| 827 |
// ----------------------------------------------------------------------------- |
| 828 |
// Attach segment in the new-fangled way, which is by using the CopyGroup code. |
| 829 |
static int AttachSegmentNewAng(const vms_angvec &pbh) |
| 830 |
{ |
| 831 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 832 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 833 |
GroupList[current_group].segments.clear(); |
| 834 |
const auto newseg = place_new_segment_in_world(); |
| 835 |
GroupList[current_group].segments.emplace_back(newseg); |
| 836 |
|
| 837 |
const auto &&nsegp = vmsegptridx(newseg); |
| 838 |
if (!med_move_group(1, Cursegp, Curside, nsegp, AttachSide, vm_angles_2_matrix(pbh),0)) |
| 839 |
{ |
| 840 |
autosave_mine(mine_filename); |
| 841 |
|
| 842 |
med_propagate_tmaps_to_segments(Cursegp,nsegp,0); |
| 843 |
med_propagate_tmaps_to_back_side(nsegp, Side_opposite[AttachSide],0); |
| 844 |
copy_uvs_seg_to_seg(vmsegptr(&New_segment),nsegp); |
| 845 |
|
| 846 |
Cursegp = nsegp; |
| 847 |
Curside = Side_opposite[AttachSide]; |
| 848 |
med_create_new_segment_from_cursegp(); |
| 849 |
|
| 850 |
if (Lock_view_to_cursegp) |
| 851 |
{ |
| 852 |
auto &vcvertptr = Vertices.vcptr; |
| 853 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 854 |
} |
| 855 |
|
| 856 |
Update_flags |= UF_WORLD_CHANGED; |
| 857 |
mine_changed = 1; |
| 858 |
warn_if_concave_segment(Cursegp); |
| 859 |
} |
| 860 |
|
| 861 |
return 1; |
| 862 |
} |
| 863 |
|
| 864 |
} |
| 865 |
|
| 866 |
int AttachSegmentNew(void) |
| 867 |
{ |
| 868 |
vms_angvec pbh; |
| 869 |
|
| 870 |
pbh.p = 0; |
| 871 |
pbh.b = 0; |
| 872 |
pbh.h = 0; |
| 873 |
|
| 874 |
AttachSegmentNewAng(pbh); |
| 875 |
return 1; |
| 876 |
|
| 877 |
} |
| 878 |
|
| 879 |
namespace { |
| 880 |
// ----------------------------------------------------------------------------- |
| 881 |
void validate_selected_segments(void) |
| 882 |
{ |
| 883 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 884 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 885 |
auto &vcvertptr = Vertices.vcptr; |
| 886 |
range_for (const auto &gs, GroupList[current_group].segments) |
| 887 |
validate_segment(vcvertptr, vmsegptridx(gs)); |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
// ===================================================================================== |
| 892 |
|
| 893 |
|
| 894 |
// ----------------------------------------------------------------------------- |
| 895 |
namespace dsx { |
| 896 |
void delete_segment_from_group(const vmsegptridx_t segment_num, unsigned group_num) |
| 897 |
{ |
| 898 |
segment_num->group = -1; |
| 899 |
GroupList[group_num].segments.erase(segment_num); |
| 900 |
} |
| 901 |
} |
| 902 |
// ===================================================================================== |
| 903 |
|
| 904 |
|
| 905 |
// ----------------------------------------------------------------------------- |
| 906 |
void add_segment_to_group(segnum_t segment_num, int group_num) |
| 907 |
{ |
| 908 |
GroupList[group_num].segments.emplace_back(segment_num); |
| 909 |
} |
| 910 |
// ===================================================================================== |
| 911 |
|
| 912 |
|
| 913 |
// ----------------------------------------------------------------------------- |
| 914 |
int rotate_segment_new(const vms_angvec &pbh) |
| 915 |
{ |
| 916 |
group::segment_array_type_t selected_segs_save; |
| 917 |
int current_group_save; |
| 918 |
|
| 919 |
if (!IS_CHILD(Cursegp->children[Side_opposite[Curside]])) |
| 920 |
// -- I don't understand this, MK, 01/25/94: if (Cursegp->children[Curside] != group_seg-Segments) |
| 921 |
{ |
| 922 |
editor_status("Error -- unable to rotate group, Cursegp:Side_opposite[Curside] cannot be free."); |
| 923 |
return 1; |
| 924 |
} |
| 925 |
|
| 926 |
current_group_save = current_group; |
| 927 |
current_group = ROT_GROUP; |
| 928 |
Groupsegp[ROT_GROUP] = Cursegp; |
| 929 |
|
| 930 |
selected_segs_save = GroupList[current_group].segments; |
| 931 |
GroupList[ROT_GROUP].segments.clear(); |
| 932 |
const auto newseg{Cursegp}; |
| 933 |
const auto newseg_side = Side_opposite[Curside]; |
| 934 |
|
| 935 |
// Create list of segments to rotate. |
| 936 |
// Sever connection between first seg to rotate and its connection on Side_opposite[Curside]. |
| 937 |
const auto child_save = Cursegp->children[newseg_side]; // save connection we are about to sever |
| 938 |
Cursegp->children[newseg_side] = segment_none; // sever connection |
| 939 |
create_group_list(Cursegp, GroupList[ROT_GROUP].segments, NULL); // create list of segments in group |
| 940 |
Cursegp->children[newseg_side] = child_save; // restore severed connection |
| 941 |
GroupList[ROT_GROUP].segments.emplace_back(newseg); |
| 942 |
|
| 943 |
const auto baseseg = newseg->children[newseg_side]; |
| 944 |
if (!IS_CHILD(baseseg)) { |
| 945 |
editor_status("Error -- unable to rotate segment, side opposite curside is not attached."); |
| 946 |
GroupList[current_group].segments = selected_segs_save; |
| 947 |
current_group = current_group_save; |
| 948 |
return 1; |
| 949 |
} |
| 950 |
const auto &&basesegp = vmsegptridx(baseseg); |
| 951 |
const auto &&baseseg_side = find_connect_side(newseg, basesegp); |
| 952 |
|
| 953 |
auto tm1{med_extract_matrix_from_segment(newseg)}; |
| 954 |
tm1 = vmd_identity_matrix; |
| 955 |
const auto &&tm2{vm_angles_2_matrix(pbh)}; |
| 956 |
const auto orient_matrix = vm_matrix_x_matrix(tm1,tm2); |
| 957 |
|
| 958 |
basesegp->children[baseseg_side] = segment_none; |
| 959 |
newseg->children[newseg_side] = segment_none; |
| 960 |
|
| 961 |
if (!med_move_group(1, basesegp, baseseg_side, newseg, newseg_side, orient_matrix, 0)) |
| 962 |
{ |
| 963 |
Cursegp = newseg; |
| 964 |
med_create_new_segment_from_cursegp(); |
| 965 |
// validate_selected_segments(); |
| 966 |
med_propagate_tmaps_to_segments(basesegp, newseg, 1); |
| 967 |
med_propagate_tmaps_to_back_side(newseg, Curside, 1); |
| 968 |
} |
| 969 |
|
| 970 |
GroupList[current_group].segments = selected_segs_save; |
| 971 |
current_group = current_group_save; |
| 972 |
|
| 973 |
return 1; |
| 974 |
} |
| 975 |
|
| 976 |
// ----------------------------------------------------------------------------- |
| 977 |
// Attach segment in the new-fangled way, which is by using the CopyGroup code. |
| 978 |
int RotateSegmentNew(vms_angvec *pbh) |
| 979 |
{ |
| 980 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 981 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 982 |
int rval; |
| 983 |
|
| 984 |
autosave_mine(mine_filename); |
| 985 |
|
| 986 |
rval = rotate_segment_new(*pbh); |
| 987 |
|
| 988 |
if (Lock_view_to_cursegp) |
| 989 |
{ |
| 990 |
auto &vcvertptr = Vertices.vcptr; |
| 991 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 992 |
} |
| 993 |
|
| 994 |
Update_flags |= UF_WORLD_CHANGED; |
| 995 |
mine_changed = 1; |
| 996 |
warn_if_concave_segment(Cursegp); |
| 997 |
|
| 998 |
return rval; |
| 999 |
} |
| 1000 |
|
| 1001 |
#if 0 |
| 1002 |
static std::array<d_fname, MAX_TEXTURES> current_tmap_list; |
| 1003 |
|
| 1004 |
// ----------------------------------------------------------------------------- |
| 1005 |
// Save mine will: |
| 1006 |
// 1. Write file info, header info, editor info, vertex data, segment data, |
| 1007 |
// and new_segment in that order, marking their file offset. |
| 1008 |
// 2. Go through all the fields and fill in the offset, size, and sizeof |
| 1009 |
// values in the headers. |
| 1010 |
static int med_save_group( const char *filename, const group::vertex_array_type_t &vertex_ids, const group::segment_array_type_t &segment_ids) |
| 1011 |
{ |
| 1012 |
int header_offset, editor_offset, vertex_offset, segment_offset, texture_offset; |
| 1013 |
char ErrorMessage[100]; |
| 1014 |
int j; |
| 1015 |
|
| 1016 |
auto SaveFile = PHYSFSX_openWriteBuffered(filename); |
| 1017 |
if (!SaveFile) |
| 1018 |
{ |
| 1019 |
snprintf(ErrorMessage, sizeof(ErrorMessage), "ERROR: Unable to open %s\n", filename); |
| 1020 |
ui_messagebox( -2, -2, 1, ErrorMessage, "Ok" ); |
| 1021 |
return 1; |
| 1022 |
} |
| 1023 |
|
| 1024 |
//===================== SAVE FILE INFO ======================== |
| 1025 |
|
| 1026 |
group_fileinfo.fileinfo_version = MINE_VERSION; |
| 1027 |
group_fileinfo.fileinfo_sizeof = sizeof(group_fileinfo); |
| 1028 |
group_fileinfo.header_offset = -1; |
| 1029 |
group_fileinfo.header_size = sizeof(group_header); |
| 1030 |
group_fileinfo.editor_offset = -1; |
| 1031 |
group_fileinfo.editor_size = sizeof(group_editor); |
| 1032 |
group_fileinfo.vertex_offset = -1; |
| 1033 |
group_fileinfo.vertex_howmany = vertex_ids.size(); |
| 1034 |
group_fileinfo.vertex_sizeof = sizeof(vms_vector); |
| 1035 |
group_fileinfo.segment_offset = -1; |
| 1036 |
group_fileinfo.segment_howmany = segment_ids.size(); |
| 1037 |
group_fileinfo.segment_sizeof = sizeof(segment); |
| 1038 |
group_fileinfo.texture_offset = -1; |
| 1039 |
group_fileinfo.texture_howmany = 0; |
| 1040 |
group_fileinfo.texture_sizeof = 13; // num characters in a name |
| 1041 |
|
| 1042 |
// Write the fileinfo |
| 1043 |
PHYSFSX_writeBytes(SaveFile, &group_fileinfo, sizeof(group_fileinfo)); |
| 1044 |
|
| 1045 |
//===================== SAVE HEADER INFO ======================== |
| 1046 |
|
| 1047 |
group_header.num_vertices = vertex_ids.size(); |
| 1048 |
group_header.num_segments = segment_ids.size(); |
| 1049 |
|
| 1050 |
// Write the editor info |
| 1051 |
header_offset = PHYSFS_tell(SaveFile); |
| 1052 |
PHYSFSX_writeBytes(SaveFile, &group_header, sizeof(group_header)); |
| 1053 |
|
| 1054 |
//===================== SAVE EDITOR INFO ========================== |
| 1055 |
group_editor.newsegment_offset = -1; // To be written |
| 1056 |
group_editor.newsegment_size = sizeof(segment); |
| 1057 |
// Next 3 vars added 10/07 by JAS |
| 1058 |
group_editor.Groupsegp = 0; |
| 1059 |
if (Groupsegp[current_group]) { |
| 1060 |
const auto i = segment_ids.find(vmsegptridx(Groupsegp[current_group])); |
| 1061 |
if (i != segment_ids.end()) |
| 1062 |
group_editor.Groupsegp = std::distance(segment_ids.begin(), i); |
| 1063 |
} |
| 1064 |
group_editor.Groupside = Groupside[current_group]; |
| 1065 |
|
| 1066 |
editor_offset = PHYSFS_tell(SaveFile); |
| 1067 |
PHYSFSX_writeBytes(SaveFile, &group_editor, sizeof(group_editor)); |
| 1068 |
|
| 1069 |
|
| 1070 |
//===================== SAVE VERTEX INFO ========================== |
| 1071 |
|
| 1072 |
vertex_offset = PHYSFS_tell(SaveFile); |
| 1073 |
range_for (const auto &gv, vertex_ids) |
| 1074 |
{ |
| 1075 |
const vertex tvert = *vcvertptr(gv); |
| 1076 |
PHYSFSX_writeBytes(SaveFile, &tvert, sizeof(tvert)); |
| 1077 |
} |
| 1078 |
|
| 1079 |
//===================== SAVE SEGMENT INFO ========================= |
| 1080 |
|
| 1081 |
|
| 1082 |
segment_offset = PHYSFS_tell(SaveFile); |
| 1083 |
range_for (const auto &gs, segment_ids) |
| 1084 |
{ |
| 1085 |
auto &&tseg = *vmsegptr(gs); |
| 1086 |
|
| 1087 |
for (j=0;j<6;j++) { |
| 1088 |
group::segment_array_type_t::const_iterator i = segment_ids.find(tseg.children[j]); |
| 1089 |
tseg.children[j] = (i == segment_ids.end()) ? segment_none : std::distance(segment_ids.begin(), i); |
| 1090 |
} |
| 1091 |
|
| 1092 |
for (j=0;j<8;j++) |
| 1093 |
{ |
| 1094 |
group::vertex_array_type_t::const_iterator i = vertex_ids.find(tseg.verts[j]); |
| 1095 |
if (i != vertex_ids.end()) |
| 1096 |
tseg.verts[j] = std::distance(vertex_ids.begin(), i); |
| 1097 |
} |
| 1098 |
PHYSFSX_writeBytes(SaveFile, &tseg, sizeof(tseg)); |
| 1099 |
|
| 1100 |
} |
| 1101 |
|
| 1102 |
//===================== SAVE TEXTURE INFO ========================== |
| 1103 |
|
| 1104 |
texture_offset = PHYSFS_tell(SaveFile); |
| 1105 |
|
| 1106 |
for (unsigned i = 0, n = NumTextures; i < n; ++i) |
| 1107 |
{ |
| 1108 |
current_tmap_list[i] = TmapInfo[i].filename; |
| 1109 |
PHYSFSX_writeBytes(SaveFile, current_tmap_list[i].data(), current_tmap_list[i].size()); |
| 1110 |
} |
| 1111 |
|
| 1112 |
//============= REWRITE FILE INFO, TO SAVE OFFSETS =============== |
| 1113 |
|
| 1114 |
// Update the offset fields |
| 1115 |
group_fileinfo.header_offset = header_offset; |
| 1116 |
group_fileinfo.editor_offset = editor_offset; |
| 1117 |
group_fileinfo.vertex_offset = vertex_offset; |
| 1118 |
group_fileinfo.segment_offset = segment_offset; |
| 1119 |
group_fileinfo.texture_offset = texture_offset; |
| 1120 |
|
| 1121 |
// Write the fileinfo |
| 1122 |
PHYSFS_seek(SaveFile, 0); // Move to TOF |
| 1123 |
PHYSFSX_writeBytes(SaveFile, &group_fileinfo, sizeof(group_fileinfo)); |
| 1124 |
|
| 1125 |
//==================== CLOSE THE FILE ============================= |
| 1126 |
return 0; |
| 1127 |
} |
| 1128 |
|
| 1129 |
static std::array<d_fname, MAX_TEXTURES> old_tmap_list; |
| 1130 |
// static short tmap_xlate_table[MAX_TEXTURES]; // ZICO - FIXME |
| 1131 |
|
| 1132 |
// ----------------------------------------------------------------------------- |
| 1133 |
// Load group will: |
| 1134 |
//int med_load_group(char * filename) |
| 1135 |
static int med_load_group( const char *filename, group::vertex_array_type_t &vertex_ids, group::segment_array_type_t &segment_ids) |
| 1136 |
{ |
| 1137 |
int vertnum; |
| 1138 |
char ErrorMessage[200]; |
| 1139 |
int translate{0}; |
| 1140 |
char *temptr; |
| 1141 |
segment tseg; |
| 1142 |
auto &&[LoadFile, physfserr] = PHYSFSX_openReadBuffered(filename); |
| 1143 |
if (!LoadFile) |
| 1144 |
{ |
| 1145 |
snprintf(ErrorMessage, sizeof(ErrorMessage), "ERROR: Failed to open %s\n%s\n", filename, PHYSFS_getErrorByCode(physfserr)); |
| 1146 |
ui_messagebox( -2, -2, 1, ErrorMessage, "Ok" ); |
| 1147 |
return 1; |
| 1148 |
} |
| 1149 |
|
| 1150 |
//===================== READ FILE INFO ======================== |
| 1151 |
|
| 1152 |
// These are the default values... version and fileinfo_sizeof |
| 1153 |
// don't have defaults. |
| 1154 |
group_fileinfo.header_offset = -1; |
| 1155 |
group_fileinfo.header_size = sizeof(group_header); |
| 1156 |
group_fileinfo.editor_offset = -1; |
| 1157 |
group_fileinfo.editor_size = sizeof(group_editor); |
| 1158 |
group_fileinfo.vertex_offset = -1; |
| 1159 |
group_fileinfo.vertex_howmany = 0; |
| 1160 |
group_fileinfo.vertex_sizeof = sizeof(vms_vector); |
| 1161 |
group_fileinfo.segment_offset = -1; |
| 1162 |
group_fileinfo.segment_howmany = 0; |
| 1163 |
group_fileinfo.segment_sizeof = sizeof(segment); |
| 1164 |
group_fileinfo.texture_offset = -1; |
| 1165 |
group_fileinfo.texture_howmany = 0; |
| 1166 |
group_fileinfo.texture_sizeof = 13; // num characters in a name |
| 1167 |
|
| 1168 |
// Read in group_top_fileinfo to get size of saved fileinfo. |
| 1169 |
|
| 1170 |
if (!PHYSFS_seek(LoadFile, 0)) |
| 1171 |
Error( "Error seeking to 0 in group.c" ); |
| 1172 |
|
| 1173 |
if (PHYSFSX_readBytes(LoadFile, &group_top_fileinfo, sizeof(group_top_fileinfo)) != sizeof(group_top_fileinfo)) |
| 1174 |
Error( "Error reading top_fileinfo in group.c" ); |
| 1175 |
|
| 1176 |
// Check version number |
| 1177 |
if (group_top_fileinfo.fileinfo_version < COMPATIBLE_VERSION ) |
| 1178 |
{ |
| 1179 |
snprintf(ErrorMessage, sizeof(ErrorMessage), "You are trying to load %s\n" \ |
| 1180 |
"a version %d group, which is known to be incompatible\n" \ |
| 1181 |
"with the current expected version %d groups.", \ |
| 1182 |
filename, group_top_fileinfo.fileinfo_version, MINE_VERSION ); |
| 1183 |
|
| 1184 |
if (ui_messagebox( -2, -2, 2, ErrorMessage, "Forget it", "Try anyway" )==1) |
| 1185 |
{ |
| 1186 |
return 1; |
| 1187 |
} |
| 1188 |
|
| 1189 |
ui_messagebox( -2, -2, 1, "Good luck!", "I need it" ); |
| 1190 |
} |
| 1191 |
|
| 1192 |
// Now, Read in the fileinfo |
| 1193 |
|
| 1194 |
if (!PHYSFS_seek(LoadFile, 0)) |
| 1195 |
Error( "Error seeking to 0b in group.c" ); |
| 1196 |
|
| 1197 |
if (const auto buffer_size{group_top_fileinfo.fileinfo_sizeof}; PHYSFSX_readBytes(LoadFile, &group_fileinfo, buffer_size) != buffer_size) |
| 1198 |
Error( "Error reading group_fileinfo in group.c" ); |
| 1199 |
|
| 1200 |
//===================== READ HEADER INFO ======================== |
| 1201 |
|
| 1202 |
// Set default values. |
| 1203 |
group_header.num_vertices = 0; |
| 1204 |
group_header.num_segments = 0; |
| 1205 |
|
| 1206 |
if (group_fileinfo.header_offset > -1 ) |
| 1207 |
{ |
| 1208 |
if (!PHYSFS_seek(LoadFile, group_fileinfo.header_offset)) |
| 1209 |
Error( "Error seeking to header_offset in group.c" ); |
| 1210 |
|
| 1211 |
if (const auto buffer_size{group_fileinfo.header_size}; PHYSFSX_readBytes(LoadFile, &group_header, buffer_size) != buffer_size) |
| 1212 |
Error( "Error reading group_header in group.c" ); |
| 1213 |
} |
| 1214 |
|
| 1215 |
//===================== READ EDITOR INFO ========================== |
| 1216 |
|
| 1217 |
// Set default values |
| 1218 |
group_editor.current_seg = 0; |
| 1219 |
group_editor.newsegment_offset = -1; // To be written |
| 1220 |
group_editor.newsegment_size = sizeof(segment); |
| 1221 |
group_editor.Groupsegp = -1; |
| 1222 |
group_editor.Groupside = 0; |
| 1223 |
|
| 1224 |
if (group_fileinfo.editor_offset > -1 ) |
| 1225 |
{ |
| 1226 |
if (!PHYSFS_seek(LoadFile, group_fileinfo.editor_offset)) |
| 1227 |
Error( "Error seeking to editor_offset in group.c" ); |
| 1228 |
|
| 1229 |
if (const auto buffer_size{group_fileinfo.editor_size}; PHYSFSX_readBytes(LoadFile, &group_editor, buffer_size) != buffer_size) |
| 1230 |
Error( "Error reading group_editor in group.c" ); |
| 1231 |
|
| 1232 |
} |
| 1233 |
|
| 1234 |
//===================== READ VERTEX INFO ========================== |
| 1235 |
|
| 1236 |
if ( (group_fileinfo.vertex_offset > -1) && (group_fileinfo.vertex_howmany > 0)) |
| 1237 |
{ |
| 1238 |
if (!PHYSFS_seek(LoadFile, group_fileinfo.vertex_offset)) |
| 1239 |
Error( "Error seeking to vertex_offset in group.c" ); |
| 1240 |
|
| 1241 |
vertex_ids.clear(); |
| 1242 |
for (unsigned i = 0; i< group_header.num_vertices; ++i) |
| 1243 |
{ |
| 1244 |
vertex tvert; |
| 1245 |
if (PHYSFSX_readBytes(LoadFile, &tvert, sizeof(tvert)) != sizeof(tvert)) |
| 1246 |
Error( "Error reading tvert in group.c" ); |
| 1247 |
vertex_ids.emplace_back(med_create_duplicate_vertex(tvert)); |
| 1248 |
} |
| 1249 |
|
| 1250 |
} |
| 1251 |
|
| 1252 |
//==================== READ SEGMENT INFO =========================== |
| 1253 |
|
| 1254 |
if ( (group_fileinfo.segment_offset > -1) && (group_fileinfo.segment_howmany > 0)) |
| 1255 |
{ |
| 1256 |
if (!PHYSFS_seek(LoadFile, group_fileinfo.segment_offset)) |
| 1257 |
Error( "Error seeking to segment_offset in group.c" ); |
| 1258 |
|
| 1259 |
segment_ids.clear(); |
| 1260 |
for (unsigned i = 0; i < group_header.num_segments; ++i) |
| 1261 |
{ |
| 1262 |
if (PHYSFSX_readBytes(LoadFile, &tseg, sizeof(segment)) != sizeof(segment)) |
| 1263 |
Error( "Error reading tseg in group.c" ); |
| 1264 |
|
| 1265 |
group::segment_array_type_t::value_type s = get_free_segment_number(Segments); |
| 1266 |
segment_ids.emplace_back(s); |
| 1267 |
const auto &&segp = vmsegptridx(s); |
| 1268 |
*segp = tseg; |
| 1269 |
segp->objects = object_none; |
| 1270 |
|
| 1271 |
fuelcen_activate(segp); |
| 1272 |
} |
| 1273 |
|
| 1274 |
range_for (const auto &gs, segment_ids) |
| 1275 |
{ |
| 1276 |
auto &segp = *vmsegptr(gs); |
| 1277 |
// Fix vertices |
| 1278 |
range_for (auto &j, segp.verts) |
| 1279 |
{ |
| 1280 |
vertnum = vertex_ids[j]; |
| 1281 |
j = vertnum; |
| 1282 |
} |
| 1283 |
|
| 1284 |
// Fix children and walls. |
| 1285 |
for (const auto j : MAX_SIDES_PER_SEGMENT) |
| 1286 |
{ |
| 1287 |
auto &seg = Segments[gs]; |
| 1288 |
shared_segment &useg = seg; |
| 1289 |
unique_segment &useg = seg; |
| 1290 |
sseg.sides[j].wall_num = wall_none; |
| 1291 |
if (IS_CHILD(Segments[gs].children[j])) { |
| 1292 |
segnum_t segnum; |
| 1293 |
segnum = segment_ids[Segments[gs].children[j]]; |
| 1294 |
Segments[gs].children[j] = segnum; |
| 1295 |
} |
| 1296 |
//Translate textures. |
| 1297 |
if (translate == 1) { |
| 1298 |
int temp; |
| 1299 |
useg.sides[j].tmap_num = tmap_xlate_table[useg.sides[j].tmap_num]; |
| 1300 |
temp = useg.sides[j].tmap_num2; |
| 1301 |
// strip off orientation bits |
| 1302 |
if (const auto tmap_xlate = get_texture_index(temp); tmap_xlate != 0) |
| 1303 |
useg.sides[j].tmap_num2 = build_texture2_value(tmap_xlate_table[tmap_xlate], get_texture_rotation_high(temp)); // mask on original orientation bits |
| 1304 |
} |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
//===================== READ TEXTURE INFO ========================== |
| 1310 |
|
| 1311 |
if ( (group_fileinfo.texture_offset > -1) && (group_fileinfo.texture_howmany > 0)) |
| 1312 |
{ |
| 1313 |
if (!PHYSFS_seek(LoadFile, group_fileinfo.texture_offset)) |
| 1314 |
Error( "Error seeking to texture_offset in gamemine.c" ); |
| 1315 |
|
| 1316 |
range_for (auto &i, partial_range(old_tmap_list, group_fileinfo.texture_howmany)) |
| 1317 |
{ |
| 1318 |
std::array<char, FILENAME_LEN> a; |
| 1319 |
if (const auto buffer_size{std::min(static_cast<size_t>(group_fileinfo.texture_sizeof), a.size())}; PHYSFSX_readBytes(LoadFile, a.data(), buffer_size) != buffer_size) |
| 1320 |
Error( "Error reading old_tmap_list[i] in gamemine.c" ); |
| 1321 |
i.copy_if(a); |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
//=============== GENERATE TEXTURE TRANSLATION TABLE =============== |
| 1326 |
|
| 1327 |
translate = 0; |
| 1328 |
|
| 1329 |
Assert (NumTextures < MAX_TEXTURES); |
| 1330 |
{ |
| 1331 |
hashtable ht; |
| 1332 |
// Remove all the file extensions in the textures list |
| 1333 |
|
| 1334 |
for (unsigned i = 0; i < NumTextures; ++i) |
| 1335 |
{ |
| 1336 |
temptr = strchr(&TmapInfo[i].filename[0u], '.'); |
| 1337 |
if (temptr) *temptr = '\0'; |
| 1338 |
hashtable_insert( &ht, &TmapInfo[i].filename[0u], i ); |
| 1339 |
} |
| 1340 |
|
| 1341 |
// For every texture, search through the texture list |
| 1342 |
// to find a matching name. |
| 1343 |
for (unsigned j = 0; j < group_fileinfo.texture_howmany; ++j) |
| 1344 |
{ |
| 1345 |
// Remove this texture name's extension |
| 1346 |
temptr = strchr(&old_tmap_list[j][0u], '.'); |
| 1347 |
if (temptr) *temptr = '\0'; |
| 1348 |
|
| 1349 |
tmap_xlate_table[j] = hashtable_search( &ht, static_cast<const char *>(old_tmap_list[j])); |
| 1350 |
if (tmap_xlate_table[j] < 0 ) |
| 1351 |
tmap_xlate_table[j] = 0; |
| 1352 |
if (tmap_xlate_table[j] != j ) translate = 1; |
| 1353 |
} |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
//======================== CLOSE FILE ============================== |
| 1358 |
LoadFile.reset(); |
| 1359 |
|
| 1360 |
//========================= UPDATE VARIABLES ====================== |
| 1361 |
|
| 1362 |
if (group_editor.Groupsegp != -1 ) |
| 1363 |
Groupsegp[current_group] = &Segments[segment_ids[group_editor.Groupsegp]]; |
| 1364 |
else |
| 1365 |
Groupsegp[current_group] = NULL; |
| 1366 |
|
| 1367 |
Groupside[current_group] = group_editor.Groupside; |
| 1368 |
|
| 1369 |
warn_if_concave_segments(); |
| 1370 |
|
| 1371 |
return 0; |
| 1372 |
} |
| 1373 |
|
| 1374 |
static char group_filename[PATH_MAX] = "*.GRP"; |
| 1375 |
|
| 1376 |
static void checkforgrpext( char * f ) |
| 1377 |
{ |
| 1378 |
int i; |
| 1379 |
|
| 1380 |
for (i=1; f[i]; i++ ) |
| 1381 |
{ |
| 1382 |
if (f[i]=='.') return; |
| 1383 |
|
| 1384 |
if ((f[i]==' '||f[i]==0) ) |
| 1385 |
{ |
| 1386 |
f[i]='.'; |
| 1387 |
f[i+1]='G'; |
| 1388 |
f[i+2]= 'R'; |
| 1389 |
f[i+3]= 'P'; |
| 1390 |
f[i+4]=0; |
| 1391 |
return; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (i < 123) |
| 1396 |
{ |
| 1397 |
f[i]='.'; |
| 1398 |
f[i+1]='G'; |
| 1399 |
f[i+2]= 'R'; |
| 1400 |
f[i+3]= 'P'; |
| 1401 |
f[i+4]=0; |
| 1402 |
return; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
#endif |
| 1406 |
|
| 1407 |
//short vertex_list[MAX_VERTICES]; |
| 1408 |
|
| 1409 |
|
| 1410 |
int SaveGroup() |
| 1411 |
{ |
| 1412 |
ui_messagebox(-2, -2, 1, "ERROR: Groups are broken.", "Ok"); |
| 1413 |
return 0; |
| 1414 |
#if 0 |
| 1415 |
// Save group |
| 1416 |
int i; |
| 1417 |
|
| 1418 |
if (current_group == -1) |
| 1419 |
{ |
| 1420 |
ui_messagebox(-2, -2, 1, "ERROR: No current group.", "Ok"); |
| 1421 |
return 0; |
| 1422 |
} |
| 1423 |
|
| 1424 |
std::array<int8_t, MAX_VERTICES> vertex_list{}; |
| 1425 |
|
| 1426 |
// Make a list of all vertices in group. |
| 1427 |
range_for (const auto &gs, GroupList[current_group].segments) |
| 1428 |
range_for (auto &v, Segments[gs].verts) |
| 1429 |
{ |
| 1430 |
vertex_list[v] = 1; |
| 1431 |
} |
| 1432 |
|
| 1433 |
GroupList[current_group].vertices.clear(); |
| 1434 |
for (i=0; i<=Highest_vertex_index; i++) |
| 1435 |
if (vertex_list[i] == 1) { |
| 1436 |
GroupList[current_group].vertices.emplace_back(i); |
| 1437 |
} |
| 1438 |
med_save_group("TEMP.GRP", GroupList[current_group].vertices, GroupList[current_group].segments); |
| 1439 |
if (ui_get_filename( group_filename, "*.GRP", "SAVE GROUP" )) |
| 1440 |
{ |
| 1441 |
checkforgrpext(group_filename); |
| 1442 |
if (med_save_group(group_filename, GroupList[current_group].vertices, GroupList[current_group].segments)) |
| 1443 |
return 0; |
| 1444 |
mine_changed = 0; |
| 1445 |
} |
| 1446 |
|
| 1447 |
return 1; |
| 1448 |
#endif |
| 1449 |
} |
| 1450 |
|
| 1451 |
|
| 1452 |
int LoadGroup() |
| 1453 |
{ |
| 1454 |
ui_messagebox(-2, -2, 1, "ERROR: Groups are broken.", "Ok"); |
| 1455 |
return 0; |
| 1456 |
#if 0 |
| 1457 |
int x; |
| 1458 |
|
| 1459 |
if (num_groups == MAX_GROUPS) |
| 1460 |
{ |
| 1461 |
x = ui_messagebox( -2, -2, 2, "Warning: You are about to wipe out a group.", "ARGH! NO!", "No problemo." ); |
| 1462 |
if (x==1) return 0; |
| 1463 |
} |
| 1464 |
|
| 1465 |
if (num_groups < MAX_GROUPS) |
| 1466 |
{ |
| 1467 |
num_groups++; |
| 1468 |
current_group = num_groups-1; |
| 1469 |
} |
| 1470 |
else current_group = 0; |
| 1471 |
|
| 1472 |
if (ui_get_filename( group_filename, "*.GRP", "LOAD GROUP" )) |
| 1473 |
{ |
| 1474 |
checkforgrpext(group_filename); |
| 1475 |
med_load_group(group_filename, GroupList[current_group].vertices, GroupList[current_group].segments); |
| 1476 |
|
| 1477 |
if (!med_move_group(0, Cursegp, Curside, vmsegptridx(Groupsegp[current_group]), Groupside[current_group], vmd_identity_matrix, 0)) |
| 1478 |
{ |
| 1479 |
autosave_mine(mine_filename); |
| 1480 |
set_view_target_from_segment(Cursegp); |
| 1481 |
Update_flags |= UF_WORLD_CHANGED; |
| 1482 |
mine_changed = 1; |
| 1483 |
diagnostic_message("Group moved."); |
| 1484 |
return 0; |
| 1485 |
} else |
| 1486 |
return 1; |
| 1487 |
} else |
| 1488 |
|
| 1489 |
return 1; |
| 1490 |
#endif |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
int UngroupSegment( void ) |
| 1495 |
{ |
| 1496 |
if (Cursegp->group == current_group) { |
| 1497 |
|
| 1498 |
Cursegp->group = -1; |
| 1499 |
delete_segment_from_group(Cursegp, current_group); |
| 1500 |
|
| 1501 |
Update_flags |= UF_WORLD_CHANGED; |
| 1502 |
mine_changed = 1; |
| 1503 |
diagnostic_message_fmt("Segment Ungrouped from Group %d.", current_group); |
| 1504 |
|
| 1505 |
return 1; |
| 1506 |
} else |
| 1507 |
return 0; |
| 1508 |
} |
| 1509 |
|
| 1510 |
int GroupSegment( void ) |
| 1511 |
{ |
| 1512 |
if (Cursegp->group == -1) { |
| 1513 |
|
| 1514 |
Cursegp->group = current_group; |
| 1515 |
add_segment_to_group(Cursegp, current_group); |
| 1516 |
|
| 1517 |
Update_flags |= UF_WORLD_CHANGED; |
| 1518 |
mine_changed = 1; |
| 1519 |
diagnostic_message_fmt("Segment Added to Group %d.", current_group); |
| 1520 |
|
| 1521 |
return 1; |
| 1522 |
} else |
| 1523 |
return 0; |
| 1524 |
} |
| 1525 |
|
| 1526 |
int Degroup( void ) |
| 1527 |
{ |
| 1528 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1529 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1530 |
int i; |
| 1531 |
|
| 1532 |
// GroupList[current_group].num_segments = 0; |
| 1533 |
// Groupsegp[current_group] = 0; |
| 1534 |
|
| 1535 |
if (num_groups==0) return 0; |
| 1536 |
|
| 1537 |
range_for (const auto &gs, GroupList[current_group].segments) |
| 1538 |
delete_segment_from_group(vmsegptridx(gs), current_group); |
| 1539 |
|
| 1540 |
// delete_segment_from_group( &Segments[GroupList[current_group].segments[i]]-Segments, current_group ); |
| 1541 |
|
| 1542 |
for (i=current_group;i<num_groups-1;i++) |
| 1543 |
{ |
| 1544 |
GroupList[i] = GroupList[i+1]; |
| 1545 |
Groupsegp[i] = Groupsegp[i+1]; |
| 1546 |
} |
| 1547 |
|
| 1548 |
num_groups--; |
| 1549 |
|
| 1550 |
GroupList[num_groups].segments.clear(); |
| 1551 |
Groupsegp[num_groups] = 0; |
| 1552 |
|
| 1553 |
if (current_group > num_groups-1) current_group--; |
| 1554 |
|
| 1555 |
if (num_groups == 0) |
| 1556 |
current_group = -1; |
| 1557 |
|
| 1558 |
if (Lock_view_to_cursegp) |
| 1559 |
{ |
| 1560 |
auto &vcvertptr = Vertices.vcptr; |
| 1561 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 1562 |
} |
| 1563 |
Update_flags |= UF_WORLD_CHANGED; |
| 1564 |
mine_changed = 1; |
| 1565 |
diagnostic_message("Group UNgrouped."); |
| 1566 |
|
| 1567 |
return 1; |
| 1568 |
} |
| 1569 |
|
| 1570 |
int NextGroup( void ) |
| 1571 |
{ |
| 1572 |
|
| 1573 |
if (num_groups > 0) |
| 1574 |
{ |
| 1575 |
current_group++; |
| 1576 |
if (current_group >= num_groups ) current_group = 0; |
| 1577 |
|
| 1578 |
Update_flags |= UF_ED_STATE_CHANGED; |
| 1579 |
mine_changed = 1; |
| 1580 |
} |
| 1581 |
else editor_status("No Next Group\n"); |
| 1582 |
return 0; |
| 1583 |
} |
| 1584 |
|
| 1585 |
int PrevGroup( void ) |
| 1586 |
{ |
| 1587 |
if (num_groups > 0) |
| 1588 |
{ |
| 1589 |
current_group--; |
| 1590 |
if (current_group < 0 ) current_group = num_groups-1; |
| 1591 |
|
| 1592 |
Update_flags |= UF_ED_STATE_CHANGED; |
| 1593 |
mine_changed = 1; |
| 1594 |
} |
| 1595 |
else editor_status("No Previous Group\n"); |
| 1596 |
return 0; |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
// ----------------------------------------------------------------------------- |
| 1601 |
int MoveGroup(void) |
| 1602 |
{ |
| 1603 |
if (!Groupsegp[current_group]) { |
| 1604 |
editor_status("Error -- Cannot move group, no group segment."); |
| 1605 |
return 1; |
| 1606 |
} |
| 1607 |
|
| 1608 |
med_compress_mine(); |
| 1609 |
|
| 1610 |
if (!med_move_group(0, Cursegp, Curside, vmsegptridx(Groupsegp[current_group]), Groupside[current_group], vmd_identity_matrix, 0)) |
| 1611 |
{ |
| 1612 |
autosave_mine(mine_filename); |
| 1613 |
Update_flags |= UF_WORLD_CHANGED; |
| 1614 |
mine_changed = 1; |
| 1615 |
diagnostic_message("Group moved."); |
| 1616 |
return 0; |
| 1617 |
} else |
| 1618 |
return 1; |
| 1619 |
} |
| 1620 |
|
| 1621 |
|
| 1622 |
// ----------------------------------------------------------------------------- |
| 1623 |
int CopyGroup(void) |
| 1624 |
{ |
| 1625 |
segnum_t attach_seg; |
| 1626 |
|
| 1627 |
if (!Groupsegp[current_group]) { |
| 1628 |
editor_status("Error -- Cannot copy group, no group segment."); |
| 1629 |
return 1; |
| 1630 |
} |
| 1631 |
|
| 1632 |
// See if the attach side in the group is attached to another segment. |
| 1633 |
// If so, it must not be in the group for group copy to be legal. |
| 1634 |
attach_seg = Groupsegp[current_group]->children[Groupside[current_group]]; |
| 1635 |
if (attach_seg != segment_none) { |
| 1636 |
if (GroupList[current_group].segments.contains(attach_seg)) { |
| 1637 |
editor_status_fmt("Error -- Cannot copy group, attach side has a child (segment %i) attached.", attach_seg); |
| 1638 |
return 1; |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
med_compress_mine(); |
| 1643 |
|
| 1644 |
if (!med_copy_group(0, Cursegp, Curside, vcsegptr(Groupsegp[current_group]), Groupside[current_group], vmd_identity_matrix)) |
| 1645 |
{ |
| 1646 |
autosave_mine(mine_filename); |
| 1647 |
Update_flags |= UF_WORLD_CHANGED; |
| 1648 |
mine_changed = 1; |
| 1649 |
diagnostic_message("Group copied."); |
| 1650 |
return 0; |
| 1651 |
} else |
| 1652 |
return 1; |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
// ----------------------------------------------------------------------------- |
| 1657 |
int RotateGroup(void) |
| 1658 |
{ |
| 1659 |
|
| 1660 |
if (!Groupsegp[current_group]) { |
| 1661 |
editor_status("Error -- Cannot rotate group, no group segment."); |
| 1662 |
return 1; |
| 1663 |
} |
| 1664 |
|
| 1665 |
Group_orientation[current_group]++; |
| 1666 |
if ((Group_orientation[current_group] <0) || (Group_orientation[current_group] >4)) |
| 1667 |
Group_orientation[current_group]=0; |
| 1668 |
|
| 1669 |
med_compress_mine(); |
| 1670 |
|
| 1671 |
if (!med_move_group(0, Cursegp, Curside, vmsegptridx(Groupsegp[current_group]), Groupside[current_group], |
| 1672 |
vmd_identity_matrix, Group_orientation[current_group])) |
| 1673 |
{ |
| 1674 |
Update_flags |= UF_WORLD_CHANGED; |
| 1675 |
mine_changed = 1; |
| 1676 |
diagnostic_message("Group rotated."); |
| 1677 |
return 0; |
| 1678 |
} |
| 1679 |
else |
| 1680 |
return 1; |
| 1681 |
} |
| 1682 |
|
| 1683 |
|
| 1684 |
// ----------------------------------------------------------------------------- |
| 1685 |
// Creates a group from all segments connected to marked segment. |
| 1686 |
int SubtractFromGroup(void) |
| 1687 |
{ |
| 1688 |
int x, original_group; |
| 1689 |
if (!Markedsegp) { |
| 1690 |
editor_status("Error -- Cannot create group, no marked segment."); |
| 1691 |
return 1; |
| 1692 |
} |
| 1693 |
|
| 1694 |
med_compress_mine(); |
| 1695 |
autosave_mine(mine_filename); |
| 1696 |
|
| 1697 |
if (num_groups == MAX_GROUPS) { |
| 1698 |
x = ui_messagebox( -2, -2, 2, "Warning: You are about to wipe out a group.", "ARGH! NO!", "No problemo." ); |
| 1699 |
if (x==1) return 0; |
| 1700 |
} |
| 1701 |
|
| 1702 |
if (current_group == -1) { |
| 1703 |
editor_status("Error -- No current group. Cannot subtract."); |
| 1704 |
return 1; |
| 1705 |
} |
| 1706 |
|
| 1707 |
original_group = current_group; |
| 1708 |
|
| 1709 |
current_group = (current_group + 1) % MAX_GROUPS; |
| 1710 |
|
| 1711 |
// Create a list of segments to copy. |
| 1712 |
GroupList[current_group].segments.clear(); |
| 1713 |
create_group_list(Markedsegp, GroupList[current_group].segments, &Selected_segs); |
| 1714 |
|
| 1715 |
// Now, scan the two groups, forming a group which consists of only those segments common to the two groups. |
| 1716 |
auto intersects = [original_group](group::segment_array_type_t::const_reference r) -> bool { |
| 1717 |
bool contains = GroupList[original_group].segments.contains(r); |
| 1718 |
if (!contains) |
| 1719 |
Segments[r].group = -1; |
| 1720 |
return !contains; |
| 1721 |
}; |
| 1722 |
GroupList[current_group].segments.erase_if(intersects); |
| 1723 |
|
| 1724 |
// Replace Marked segment with Group Segment. |
| 1725 |
Groupsegp[current_group] = Markedsegp; |
| 1726 |
Groupside[current_group] = Markedside; |
| 1727 |
|
| 1728 |
range_for (const auto &gs, GroupList[current_group].segments) |
| 1729 |
Segments[gs].group = current_group; |
| 1730 |
|
| 1731 |
Update_flags |= UF_WORLD_CHANGED; |
| 1732 |
mine_changed = 1; |
| 1733 |
diagnostic_message("Group created."); |
| 1734 |
|
| 1735 |
return 1; |
| 1736 |
|
| 1737 |
} |
| 1738 |
|
| 1739 |
// ----------------------------------------------------------------------------- |
| 1740 |
// Creates a group from all segments already in CurrentGroup which can be reached from marked segment |
| 1741 |
// without passing through current segment. |
| 1742 |
int CreateGroup(void) |
| 1743 |
{ |
| 1744 |
int x; |
| 1745 |
|
| 1746 |
if (!Markedsegp) { |
| 1747 |
editor_status("Error -- Cannot create group, no marked segment."); |
| 1748 |
return 1; |
| 1749 |
} |
| 1750 |
|
| 1751 |
med_compress_mine(); |
| 1752 |
autosave_mine(mine_filename); |
| 1753 |
|
| 1754 |
if (num_groups == MAX_GROUPS) { |
| 1755 |
x = ui_messagebox( -2, -2, 2, "Warning: You are about to wipe out a group.", "ARGH! NO!", "No problemo." ); |
| 1756 |
if (x==1) |
| 1757 |
return 0; // Aborting at user's request. |
| 1758 |
} |
| 1759 |
|
| 1760 |
if (num_groups < MAX_GROUPS) { |
| 1761 |
num_groups++; |
| 1762 |
current_group = num_groups-1; |
| 1763 |
} else |
| 1764 |
current_group = 0; |
| 1765 |
|
| 1766 |
// Create a list of segments to copy. |
| 1767 |
GroupList[current_group].clear(); |
| 1768 |
create_group_list(Markedsegp, GroupList[current_group].segments, NULL); |
| 1769 |
|
| 1770 |
// Replace Marked segment with Group Segment. |
| 1771 |
Groupsegp[current_group] = Markedsegp; |
| 1772 |
Groupside[current_group] = Markedside; |
| 1773 |
// Markedsegp = 0; |
| 1774 |
// Markedside = WBACK; |
| 1775 |
|
| 1776 |
range_for (const auto &gs, GroupList[current_group].segments) |
| 1777 |
Segments[gs].group = current_group; |
| 1778 |
|
| 1779 |
Update_flags |= UF_WORLD_CHANGED; |
| 1780 |
mine_changed = 1; |
| 1781 |
diagnostic_message("Group created."); |
| 1782 |
|
| 1783 |
return 1; |
| 1784 |
|
| 1785 |
} |
| 1786 |
|
| 1787 |
// ----------------------------------------------------------------------------- |
| 1788 |
// Deletes current group. |
| 1789 |
int DeleteGroup( void ) |
| 1790 |
{ |
| 1791 |
auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state(); |
| 1792 |
auto &Vertices = LevelSharedVertexState.get_vertices(); |
| 1793 |
int i; |
| 1794 |
|
| 1795 |
autosave_mine(mine_filename); |
| 1796 |
|
| 1797 |
if (num_groups==0) return 0; |
| 1798 |
|
| 1799 |
range_for (const auto &gs, GroupList[current_group].segments) |
| 1800 |
{ |
| 1801 |
const auto &&segp = vmsegptridx(gs); |
| 1802 |
segp->group = -1; |
| 1803 |
med_delete_segment(segp); |
| 1804 |
} |
| 1805 |
|
| 1806 |
for (i=current_group;i<num_groups-1;i++) { |
| 1807 |
GroupList[i] = GroupList[i+1]; |
| 1808 |
Groupsegp[i] = Groupsegp[i+1]; |
| 1809 |
} |
| 1810 |
|
| 1811 |
num_groups--; |
| 1812 |
GroupList[num_groups].clear(); |
| 1813 |
Groupsegp[num_groups] = 0; |
| 1814 |
|
| 1815 |
if (current_group > num_groups-1) current_group--; |
| 1816 |
|
| 1817 |
if (num_groups==0) |
| 1818 |
current_group = -1; |
| 1819 |
|
| 1820 |
undo_status[Autosave_count] = "Delete Group UNDONE."; |
| 1821 |
if (Lock_view_to_cursegp) |
| 1822 |
{ |
| 1823 |
auto &vcvertptr = Vertices.vcptr; |
| 1824 |
set_view_target_from_segment(vcvertptr, Cursegp); |
| 1825 |
} |
| 1826 |
|
| 1827 |
Update_flags |= UF_WORLD_CHANGED; |
| 1828 |
mine_changed = 1; |
| 1829 |
diagnostic_message("Group deleted."); |
| 1830 |
// warn_if_concave_segments(); // This could be faster -- just check if deleted segment was concave, warn accordingly |
| 1831 |
|
| 1832 |
return 1; |
| 1833 |
|
| 1834 |
} |
| 1835 |
|
| 1836 |
|
| 1837 |
int MarkGroupSegment( void ) |
| 1838 |
{ |
| 1839 |
if ((Cursegp->group != -1) && (Cursegp->group == current_group)) |
| 1840 |
{ |
| 1841 |
autosave_mine(mine_filename); |
| 1842 |
Groupsegp[current_group] = Cursegp; |
| 1843 |
Groupside[current_group] = Curside; |
| 1844 |
editor_status("Group Segment Marked."); |
| 1845 |
Update_flags |= UF_ED_STATE_CHANGED; |
| 1846 |
undo_status[Autosave_count] = "Mark Group Segment UNDONE."; |
| 1847 |
mine_changed = 1; |
| 1848 |
return 1; |
| 1849 |
} |
| 1850 |
else return 0; |
| 1851 |
} |