| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Include file for functions which need to access segment data structure. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <compare> |
| 29 |
#include <physfs.h> |
| 30 |
#include <type_traits> |
| 31 |
#include "pstypes.h" |
| 32 |
#include "maths.h" |
| 33 |
#include "vecmat.h" |
| 34 |
#include "dxxsconf.h" |
| 35 |
#include "dsx-ns.h" |
| 36 |
|
| 37 |
#include <cassert> |
| 38 |
#include <cstdint> |
| 39 |
#include <stdexcept> |
| 40 |
#include "fwd-segment.h" |
| 41 |
#include "countarray.h" |
| 42 |
#include "valptridx.h" |
| 43 |
#include "objnum.h" |
| 44 |
#include "pack.h" |
| 45 |
|
| 46 |
#ifdef DXX_BUILD_DESCENT |
| 47 |
namespace dsx { |
| 48 |
// Returns true if segnum references a child, else returns false. |
| 49 |
// Note that -1 means no connection, -2 means a connection to the outside world. |
| 50 |
static inline bool IS_CHILD(segnum_t s) |
| 51 |
{ |
| 52 |
return s != segment_none && s != segment_exit; |
| 53 |
} |
| 54 |
} |
| 55 |
#endif |
| 56 |
|
| 57 |
namespace dcx { |
| 58 |
|
| 59 |
static constexpr sidenum_t &operator++(sidenum_t &a) |
| 60 |
{ |
| 61 |
return a = static_cast<sidenum_t>(static_cast<unsigned>(a) + 1); |
| 62 |
} |
| 63 |
|
| 64 |
static constexpr side_relative_vertnum &operator++(side_relative_vertnum &a) |
| 65 |
{ |
| 66 |
return a = static_cast<side_relative_vertnum>(static_cast<uint8_t>(a) + 1); |
| 67 |
} |
| 68 |
|
| 69 |
//Structure for storing u,v,light values. |
| 70 |
//NOTE: this structure should be the same as the one in 3d.h |
| 71 |
struct uvl |
| 72 |
{ |
| 73 |
fix u, v, l; |
| 74 |
}; |
| 75 |
|
| 76 |
enum class side_type : uint8_t |
| 77 |
{ |
| 78 |
quad = 1, // render side as quadrilateral |
| 79 |
tri_02 = 2, // render side as two triangles, triangulated along edge from 0 to 2 |
| 80 |
tri_13 = 3, // render side as two triangles, triangulated along edge from 1 to 3 |
| 81 |
}; |
| 82 |
|
| 83 |
enum class side_relative_vertnum : uint8_t |
| 84 |
{ |
| 85 |
_0, |
| 86 |
_1, |
| 87 |
_2, |
| 88 |
_3, |
| 89 |
}; |
| 90 |
|
| 91 |
enum class segment_relative_vertnum : uint8_t |
| 92 |
{ |
| 93 |
_0, |
| 94 |
_1, |
| 95 |
_2, |
| 96 |
_3, |
| 97 |
_4, |
| 98 |
_5, |
| 99 |
_6, |
| 100 |
_7, |
| 101 |
}; |
| 102 |
|
| 103 |
enum class segment_special : uint8_t |
| 104 |
{ |
| 105 |
nothing, |
| 106 |
fuelcen, |
| 107 |
repaircen, |
| 108 |
controlcen, |
| 109 |
robotmaker, |
| 110 |
/* if DXX_BUILD_DESCENT == 2 */ |
| 111 |
goal_blue, |
| 112 |
goal_red, |
| 113 |
/* endif */ |
| 114 |
}; |
| 115 |
|
| 116 |
enum class materialization_center_number : uint8_t |
| 117 |
{ |
| 118 |
None = 0xff, |
| 119 |
}; |
| 120 |
|
| 121 |
enum class station_number : uint8_t |
| 122 |
{ |
| 123 |
None = 0xff, |
| 124 |
}; |
| 125 |
|
| 126 |
enum segnum_t : uint16_t |
| 127 |
{ |
| 128 |
}; |
| 129 |
|
| 130 |
enum class vertnum_t : uint32_t |
| 131 |
{ |
| 132 |
}; |
| 133 |
|
| 134 |
enum class wallnum_t : uint16_t |
| 135 |
{ |
| 136 |
None = 0xffff, |
| 137 |
}; |
| 138 |
|
| 139 |
struct shared_side |
| 140 |
{ |
| 141 |
struct illegal_type; |
| 142 |
side_type type; // replaces num_faces and tri_edge, 1 = quad, 2 = 0:2 triangulation, 3 = 1:3 triangulation |
| 143 |
wallnum_t wall_num; |
| 144 |
std::array<vms_vector, 2> normals; // 2 normals, if quadrilateral, both the same. |
| 145 |
}; |
| 146 |
|
| 147 |
enum class sidenum_t : uint8_t |
| 148 |
{ |
| 149 |
WLEFT = 0, |
| 150 |
WTOP = 1, |
| 151 |
WRIGHT = 2, |
| 152 |
WBOTTOM = 3, |
| 153 |
WBACK = 4, |
| 154 |
WFRONT = 5 |
| 155 |
}; |
| 156 |
|
| 157 |
enum class sidemask_t : uint8_t |
| 158 |
{ |
| 159 |
left = 1u << static_cast<uint8_t>(sidenum_t::WLEFT), |
| 160 |
top = 1u << static_cast<uint8_t>(sidenum_t::WTOP), |
| 161 |
right = 1u << static_cast<uint8_t>(sidenum_t::WRIGHT), |
| 162 |
bottom = 1u << static_cast<uint8_t>(sidenum_t::WBOTTOM), |
| 163 |
back = 1u << static_cast<uint8_t>(sidenum_t::WBACK), |
| 164 |
front = 1u << static_cast<uint8_t>(sidenum_t::WFRONT), |
| 165 |
}; |
| 166 |
|
| 167 |
static constexpr uint8_t operator&(const sidemask_t a, const sidemask_t b) |
| 168 |
{ |
| 169 |
return static_cast<uint8_t>(a) & static_cast<uint8_t>(b); |
| 170 |
} |
| 171 |
|
| 172 |
static constexpr sidemask_t operator~(const sidemask_t a) |
| 173 |
{ |
| 174 |
return static_cast<sidemask_t>(~static_cast<uint8_t>(a)); |
| 175 |
} |
| 176 |
|
| 177 |
static constexpr sidemask_t &operator|=(sidemask_t &a, const sidemask_t b) |
| 178 |
{ |
| 179 |
return a = static_cast<sidemask_t>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b)); |
| 180 |
} |
| 181 |
|
| 182 |
static constexpr sidemask_t build_sidemask(const sidenum_t s) |
| 183 |
{ |
| 184 |
return static_cast<sidemask_t>(1u << static_cast<uint8_t>(s)); |
| 185 |
} |
| 186 |
|
| 187 |
enum class sound_ambient_flags : uint8_t |
| 188 |
{ |
| 189 |
None = 0, |
| 190 |
water = 1, |
| 191 |
lava = 2, |
| 192 |
water_and_lava = water | lava, |
| 193 |
}; |
| 194 |
|
| 195 |
enum class texture1_value : uint16_t |
| 196 |
{ |
| 197 |
None, |
| 198 |
}; |
| 199 |
|
| 200 |
/* texture2_value uses the low 14 bits for the array index and the high |
| 201 |
* 2 bits for texture2_rotation_high |
| 202 |
*/ |
| 203 |
enum class texture2_value : uint16_t |
| 204 |
{ |
| 205 |
None, |
| 206 |
}; |
| 207 |
|
| 208 |
enum class texture2_rotation_low : uint8_t |
| 209 |
{ |
| 210 |
Normal = 0, |
| 211 |
_1 = 1, |
| 212 |
_2 = 2, |
| 213 |
_3 = 3, |
| 214 |
}; |
| 215 |
|
| 216 |
#define TEXTURE2_ROTATION_SHIFT 14u |
| 217 |
#define TEXTURE2_ROTATION_INDEX_MASK ((1u << TEXTURE2_ROTATION_SHIFT) - 1u) |
| 218 |
|
| 219 |
enum class texture2_rotation_high : uint16_t |
| 220 |
{ |
| 221 |
Normal = 0, |
| 222 |
_1 = static_cast<uint16_t>(texture2_rotation_low::_1) << TEXTURE2_ROTATION_SHIFT, |
| 223 |
_2 = static_cast<uint16_t>(texture2_rotation_low::_2) << TEXTURE2_ROTATION_SHIFT, |
| 224 |
_3 = static_cast<uint16_t>(texture2_rotation_low::_3) << TEXTURE2_ROTATION_SHIFT, |
| 225 |
}; |
| 226 |
|
| 227 |
static constexpr texture2_rotation_high &operator++(texture2_rotation_high &t) |
| 228 |
{ |
| 229 |
/* Cast to uint32, step to the next value, cast back. The cast back |
| 230 |
* will truncate to the low 16 bits, causing |
| 231 |
* texture2_rotation_high::_3 to roll over to |
| 232 |
* texture2_rotation_high::Normal. |
| 233 |
*/ |
| 234 |
return (t = static_cast<texture2_rotation_high>(static_cast<uint32_t>(t) + (1u << TEXTURE2_ROTATION_SHIFT))); |
| 235 |
} |
| 236 |
|
| 237 |
static constexpr texture_index get_texture_index(const texture1_value t) |
| 238 |
{ |
| 239 |
return static_cast<texture_index>(t); |
| 240 |
} |
| 241 |
|
| 242 |
static constexpr texture_index get_texture_index(const texture2_value t) |
| 243 |
{ |
| 244 |
return static_cast<texture_index>(static_cast<uint16_t>(t) & TEXTURE2_ROTATION_INDEX_MASK); |
| 245 |
} |
| 246 |
|
| 247 |
static constexpr texture2_rotation_high get_texture_rotation_high(const texture2_value t) |
| 248 |
{ |
| 249 |
return static_cast<texture2_rotation_high>(static_cast<uint16_t>(t) & ~TEXTURE2_ROTATION_INDEX_MASK); |
| 250 |
} |
| 251 |
|
| 252 |
static constexpr texture2_rotation_low get_texture_rotation_low(const texture2_rotation_high t) |
| 253 |
{ |
| 254 |
return static_cast<texture2_rotation_low>(static_cast<uint16_t>(t) >> TEXTURE2_ROTATION_SHIFT); |
| 255 |
} |
| 256 |
|
| 257 |
static constexpr texture2_rotation_low get_texture_rotation_low(const texture2_value t) |
| 258 |
{ |
| 259 |
return get_texture_rotation_low(get_texture_rotation_high(t)); |
| 260 |
} |
| 261 |
|
| 262 |
static constexpr texture1_value build_texture1_value(const texture_index t) |
| 263 |
{ |
| 264 |
return static_cast<texture1_value>(t); |
| 265 |
} |
| 266 |
|
| 267 |
static constexpr texture2_value build_texture2_value(const texture_index t, const texture2_rotation_high rotation) |
| 268 |
{ |
| 269 |
return static_cast<texture2_value>(static_cast<uint16_t>(t) | static_cast<uint16_t>(rotation)); |
| 270 |
} |
| 271 |
|
| 272 |
#undef TEXTURE2_ROTATION_SHIFT |
| 273 |
#undef TEXTURE2_ROTATION_INDEX_MASK |
| 274 |
|
| 275 |
[[nodiscard]] |
| 276 |
static constexpr side_relative_vertnum next_side_vertex(const side_relative_vertnum a, unsigned add = 1) |
| 277 |
{ |
| 278 |
return static_cast<side_relative_vertnum>((static_cast<uint8_t>(a) + add) & 3); |
| 279 |
} |
| 280 |
|
| 281 |
struct unique_side |
| 282 |
{ |
| 283 |
texture1_value tmap_num; |
| 284 |
texture2_value tmap_num2; |
| 285 |
enumerated_array<uvl, 4, side_relative_vertnum> uvls; |
| 286 |
}; |
| 287 |
|
| 288 |
#ifdef DXX_BUILD_DESCENT |
| 289 |
struct shared_segment |
| 290 |
{ |
| 291 |
#if DXX_USE_EDITOR |
| 292 |
segnum_t segnum; // segment number, not sure what it means |
| 293 |
short group; // group number to which the segment belongs. |
| 294 |
#endif |
| 295 |
per_side_array<segnum_t> children; // indices of 6 children segments, front, left, top, right, bottom, back |
| 296 |
enumerated_array<vertnum_t, MAX_VERTICES_PER_SEGMENT, segment_relative_vertnum> verts; // vertex ids of 4 front and 4 back vertices |
| 297 |
segment_special special; // what type of center this is |
| 298 |
materialization_center_number matcen_num; // which center segment is associated with. |
| 299 |
station_number station_idx; |
| 300 |
/* if DXX_BUILD_DESCENT == 2 */ |
| 301 |
sound_ambient_flags s2_flags; |
| 302 |
/* endif */ |
| 303 |
per_side_array<shared_side> sides; |
| 304 |
}; |
| 305 |
|
| 306 |
struct unique_segment |
| 307 |
{ |
| 308 |
objnum_t objects; // pointer to objects in this segment |
| 309 |
// If bit n (1 << n) is set, then side #n in segment has had light subtracted from original (editor-computed) value. |
| 310 |
sidemask_t light_subtracted; |
| 311 |
/* if DXX_BUILD_DESCENT == 2 */ |
| 312 |
sidemask_t slide_textures; |
| 313 |
/* endif */ |
| 314 |
fix static_light; |
| 315 |
per_side_array<unique_side> sides; |
| 316 |
}; |
| 317 |
|
| 318 |
struct segment : unique_segment, shared_segment |
| 319 |
{ |
| 320 |
}; |
| 321 |
|
| 322 |
template <typename S, typename U> |
| 323 |
struct susegment |
| 324 |
{ |
| 325 |
S &s; |
| 326 |
U &u; |
| 327 |
constexpr susegment(const susegment &) = default; |
| 328 |
constexpr susegment(susegment &&) = default; |
| 329 |
template <typename T> |
| 330 |
requires(std::is_convertible<T &&, S &>::value && std::is_convertible<T &&, U &>::value) |
| 331 |
constexpr susegment(T &&t) : |
| 332 |
s(t), u(t) |
| 333 |
{ |
| 334 |
} |
| 335 |
operator S &() const |
| 336 |
{ |
| 337 |
return s; |
| 338 |
} |
| 339 |
operator U &() const |
| 340 |
{ |
| 341 |
return u; |
| 342 |
} |
| 343 |
}; |
| 344 |
#endif |
| 345 |
|
| 346 |
struct count_segment_array_t : public count_array_t<segnum_t, MAX_SEGMENTS> {}; |
| 347 |
|
| 348 |
struct group |
| 349 |
{ |
| 350 |
struct segment_array_type_t : public count_segment_array_t {}; |
| 351 |
struct vertex_array_type_t : public count_array_t<vertnum_t, MAX_VERTICES> {}; |
| 352 |
segment_array_type_t segments; |
| 353 |
vertex_array_type_t vertices; |
| 354 |
void clear() |
| 355 |
{ |
| 356 |
segments.clear(); |
| 357 |
vertices.clear(); |
| 358 |
} |
| 359 |
}; |
| 360 |
|
| 361 |
#ifdef DXX_BUILD_DESCENT |
| 362 |
#define Highest_segment_index static_cast<segnum_t>(Segments.get_count() - 1) |
| 363 |
DXX_VALPTRIDX_DEFINE_GLOBAL_FACTORIES(segment, seg, Segments); |
| 364 |
#endif |
| 365 |
|
| 366 |
// Globals from mglobal.c |
| 367 |
struct vertex : vms_vector |
| 368 |
{ |
| 369 |
vertex() = default; |
| 370 |
vertex(const fix &a, const fix &b, const fix &c) : |
| 371 |
vms_vector{a, b, c} |
| 372 |
{ |
| 373 |
} |
| 374 |
explicit vertex(const vms_vector &v) : |
| 375 |
vms_vector(v) |
| 376 |
{ |
| 377 |
} |
| 378 |
}; |
| 379 |
|
| 380 |
static constexpr vertnum_t operator++(vertnum_t &v) |
| 381 |
{ |
| 382 |
return (v = static_cast<vertnum_t>(static_cast<unsigned>(v) + 1)); |
| 383 |
} |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
#ifdef DXX_BUILD_DESCENT |
| 388 |
struct shared_side::illegal_type : std::runtime_error |
| 389 |
{ |
| 390 |
const shared_segment *const m_segment; |
| 391 |
const shared_side *const m_side; |
| 392 |
illegal_type(const shared_segment &seg, const shared_side &s) : |
| 393 |
runtime_error("illegal side type"), |
| 394 |
m_segment(&seg), m_side(&s) |
| 395 |
{ |
| 396 |
} |
| 397 |
illegal_type(const shared_side &s) : |
| 398 |
runtime_error("illegal side type"), |
| 399 |
m_segment(nullptr), m_side(&s) |
| 400 |
{ |
| 401 |
} |
| 402 |
}; |
| 403 |
|
| 404 |
namespace dsx { |
| 405 |
|
| 406 |
#if DXX_BUILD_DESCENT == 2 |
| 407 |
enum class delta_light_index : uint16_t |
| 408 |
{ |
| 409 |
}; |
| 410 |
|
| 411 |
// New stuff, 10/14/95: For shooting out lights and monitors. |
| 412 |
// Light cast upon vert_light vertices in segnum:sidenum by some light |
| 413 |
struct delta_light : prohibit_void_ptr<delta_light> |
| 414 |
{ |
| 415 |
segnum_t segnum; |
| 416 |
sidenum_t sidenum; |
| 417 |
enumerated_array<uint8_t, 4, side_relative_vertnum> vert_light; |
| 418 |
}; |
| 419 |
|
| 420 |
// Light at segnum:sidenum casts light on count sides beginning at index (in array Delta_lights) |
| 421 |
struct dl_index { |
| 422 |
segnum_t segnum; |
| 423 |
sidenum_t sidenum; |
| 424 |
uint8_t count; |
| 425 |
delta_light_index index; |
| 426 |
constexpr std::strong_ordering operator<=>(const dl_index &rhs) const |
| 427 |
{ |
| 428 |
if (const auto r = segnum <=> rhs.segnum; r != std::strong_ordering::equal) |
| 429 |
return r; |
| 430 |
return sidenum <=> rhs.sidenum; |
| 431 |
} |
| 432 |
constexpr bool operator==(const dl_index &) const = default; |
| 433 |
}; |
| 434 |
|
| 435 |
struct d_level_shared_destructible_light_state |
| 436 |
{ |
| 437 |
valptridx<dl_index>::array_managed_type Dl_indices; |
| 438 |
d_delta_light_array Delta_lights; |
| 439 |
}; |
| 440 |
#endif |
| 441 |
|
| 442 |
} |
| 443 |
#endif |
| 444 |
|
| 445 |
namespace dcx { |
| 446 |
|
| 447 |
#ifdef DXX_BUILD_DESCENT |
| 448 |
struct d_level_shared_vertex_state |
| 449 |
{ |
| 450 |
#if DXX_USE_EDITOR |
| 451 |
/* Number of elements in Vertex_active which have a nonzero value. |
| 452 |
* This can be less than the index of the highest defined vertex if |
| 453 |
* there are unused vertices. |
| 454 |
*/ |
| 455 |
unsigned Num_vertices; |
| 456 |
#endif |
| 457 |
private: |
| 458 |
vertex_array Vertices; |
| 459 |
#if DXX_USE_EDITOR |
| 460 |
enumerated_array<uint8_t, MAX_VERTICES, vertnum_t> Vertex_active; // !0 means vertex is in use, 0 means not in use. |
| 461 |
#endif |
| 462 |
public: |
| 463 |
auto &get_vertices() |
| 464 |
{ |
| 465 |
return Vertices; |
| 466 |
} |
| 467 |
const auto &get_vertices() const |
| 468 |
{ |
| 469 |
return Vertices; |
| 470 |
} |
| 471 |
#if DXX_USE_EDITOR |
| 472 |
auto &get_vertex_active() |
| 473 |
{ |
| 474 |
return Vertex_active; |
| 475 |
} |
| 476 |
const auto &get_vertex_active() const |
| 477 |
{ |
| 478 |
return Vertex_active; |
| 479 |
} |
| 480 |
#endif |
| 481 |
}; |
| 482 |
|
| 483 |
struct d_level_shared_segment_state |
| 484 |
{ |
| 485 |
unsigned Num_segments; |
| 486 |
d_level_shared_vertex_state LevelSharedVertexState; |
| 487 |
auto &get_segments() |
| 488 |
{ |
| 489 |
return Segments; |
| 490 |
} |
| 491 |
const auto &get_segments() const |
| 492 |
{ |
| 493 |
return Segments; |
| 494 |
} |
| 495 |
auto &get_vertex_state() |
| 496 |
{ |
| 497 |
return LevelSharedVertexState; |
| 498 |
} |
| 499 |
const auto &get_vertex_state() const |
| 500 |
{ |
| 501 |
return LevelSharedVertexState; |
| 502 |
} |
| 503 |
}; |
| 504 |
|
| 505 |
struct d_level_unique_automap_state |
| 506 |
{ |
| 507 |
enumerated_array<uint8_t, MAX_SEGMENTS, segnum_t> Automap_visited; |
| 508 |
}; |
| 509 |
|
| 510 |
struct d_level_unique_segment_state |
| 511 |
{ |
| 512 |
auto &get_segments() |
| 513 |
{ |
| 514 |
return Segments; |
| 515 |
} |
| 516 |
const auto &get_segments() const |
| 517 |
{ |
| 518 |
return Segments; |
| 519 |
} |
| 520 |
}; |
| 521 |
|
| 522 |
extern d_level_unique_automap_state LevelUniqueAutomapState; |
| 523 |
extern d_level_unique_segment_state LevelUniqueSegmentState; |
| 524 |
#endif |
| 525 |
|
| 526 |
template <unsigned bits> |
| 527 |
requires( |
| 528 |
/* For every segment, require that the bits of the mask for that segment |
| 529 |
* all reside in a single byte. Since a byte is 8 bits wide, this |
| 530 |
* constrains `bits` to be 1, 2, 4, or 8. Any other value would mean that |
| 531 |
* some segments have a mask that straddles 2 bytes. Current logic would |
| 532 |
* not handle a cross-byte value correctly, so disallow such attempts. |
| 533 |
* |
| 534 |
* A mask of 8 is not useful, since the caller could use |
| 535 |
* `enumerated_array<uint8_t, MAX_SEGMENTS, segnum_t>` for that and avoid |
| 536 |
* the shifting/masking logic on assignment. Therefore, exclude |
| 537 |
* `bits == 8`, even though the class should work correctly in that case. |
| 538 |
*/ |
| 539 |
bits == 1 || bits == 2 || bits == 4 |
| 540 |
) |
| 541 |
class visited_segment_mask_t |
| 542 |
{ |
| 543 |
/* For a segment with index `n`, the mask for that segment is stored in |
| 544 |
* `a[n / divisor]`. |
| 545 |
*/ |
| 546 |
static constexpr unsigned divisor{8u / bits}; |
| 547 |
static constexpr unsigned bitmask_low_aligned{(1u << bits) - 1u}; |
| 548 |
using array_t = std::array<uint8_t, (MAX_SEGMENTS + (divisor - 1)) / divisor>; |
| 549 |
/* Initialize `a` so that the storage is cleared. */ |
| 550 |
array_t a{}; |
| 551 |
/* The shift count depends on `bits`, but not on the type of the data being |
| 552 |
* tracked, so store it in a base class, to avoid redundant instantiations. |
| 553 |
*/ |
| 554 |
struct maskproxy_shift_count_type |
| 555 |
{ |
| 556 |
const unsigned shift; |
| 557 |
typename array_t::value_type mask() const |
| 558 |
{ |
| 559 |
return bitmask_low_aligned << shift; |
| 560 |
} |
| 561 |
constexpr maskproxy_shift_count_type(const unsigned s) : |
| 562 |
shift{s * bits} |
| 563 |
{ |
| 564 |
} |
| 565 |
}; |
| 566 |
template <typename byte_type> |
| 567 |
struct maskproxy_assignable_type : maskproxy_shift_count_type |
| 568 |
{ |
| 569 |
using maskproxy_shift_count_type::shift; |
| 570 |
byte_type &byte; |
| 571 |
constexpr maskproxy_assignable_type(const unsigned s, byte_type &byte) : |
| 572 |
maskproxy_shift_count_type{s}, byte{byte} |
| 573 |
{ |
| 574 |
} |
| 575 |
constexpr operator uint8_t() const |
| 576 |
{ |
| 577 |
return (byte >> shift) & bitmask_low_aligned; |
| 578 |
} |
| 579 |
/* For the special case that the mask is 1 bit wide, accept only |
| 580 |
* `bool`. This version can avoid clearing the mask bits before |
| 581 |
* assigning them, since `(old_value | true) == true`, and |
| 582 |
* `(old_value & false) == false`. |
| 583 |
*/ |
| 584 |
maskproxy_assignable_type &operator=(const bool value) |
| 585 |
requires(bits == 1) |
| 586 |
{ |
| 587 |
const auto m{this->mask()}; |
| 588 |
if (value) |
| 589 |
byte |= m; |
| 590 |
else |
| 591 |
byte &= ~m; |
| 592 |
return *this; |
| 593 |
} |
| 594 |
/* For the general case that the mask is not 1 bit wide, the bits to be |
| 595 |
* written must be cleared before the new value is bitwise-or'd in, |
| 596 |
* since `(old_value | 2) == 2` is not guaranteed. |
| 597 |
*/ |
| 598 |
maskproxy_assignable_type &operator=(const uint8_t u) |
| 599 |
requires(bits != 1) |
| 600 |
{ |
| 601 |
assert(!(u & ~bitmask_low_aligned)); |
| 602 |
byte = (byte & ~this->mask()) | (u << shift); |
| 603 |
return *this; |
| 604 |
} |
| 605 |
}; |
| 606 |
/* clang Class Template Argument Deduction fails to deduce from the |
| 607 |
* available non-template constructor. Define an explicit deduction guide |
| 608 |
* to tell it what it should have implicitly deduced on its own. |
| 609 |
* |
| 610 |
* - [gcc-9, gcc-14]: succeed without this |
| 611 |
* - [clang-14, clang-17]: fail without this |
| 612 |
* - (clang-17, ...): untested |
| 613 |
*/ |
| 614 |
template <typename byte_type> |
| 615 |
maskproxy_assignable_type(unsigned, byte_type &) -> maskproxy_assignable_type<byte_type>; |
| 616 |
static constexpr auto make_maskproxy(auto &a, const segnum_t s) |
| 617 |
{ |
| 618 |
const auto segnum{static_cast<std::underlying_type_t<segnum_t>>(s)}; |
| 619 |
return maskproxy_assignable_type{{segnum % divisor}, a.at({segnum / divisor})}; |
| 620 |
} |
| 621 |
public: |
| 622 |
auto operator[](const segnum_t segnum) |
| 623 |
{ |
| 624 |
return make_maskproxy(a, segnum); |
| 625 |
} |
| 626 |
auto operator[](const segnum_t segnum) const |
| 627 |
{ |
| 628 |
return make_maskproxy(a, segnum); |
| 629 |
} |
| 630 |
}; |
| 631 |
|
| 632 |
using visited_segment_bitarray_t = visited_segment_mask_t<1>; |
| 633 |
|
| 634 |
#ifdef DXX_BUILD_DESCENT |
| 635 |
imsegidx_t read_untrusted_segnum_le16(NamedPHYSFS_File fp); |
| 636 |
imsegidx_t read_untrusted_segnum_le32(NamedPHYSFS_File fp); |
| 637 |
imsegidx_t read_untrusted_segnum_xe16(NamedPHYSFS_File fp, physfsx_endian swap); |
| 638 |
imsegidx_t read_untrusted_segnum_xe32(NamedPHYSFS_File fp, physfsx_endian swap); |
| 639 |
#endif |
| 640 |
|
| 641 |
} |
| 642 |
|
| 643 |
#ifdef DXX_BUILD_DESCENT |
| 644 |
namespace dsx { |
| 645 |
|
| 646 |
#if DXX_BUILD_DESCENT == 2 |
| 647 |
struct d_level_shared_segment_state : ::dcx::d_level_shared_segment_state |
| 648 |
{ |
| 649 |
d_level_shared_destructible_light_state DestructibleLights; |
| 650 |
segnum_t Secret_return_segment; |
| 651 |
vms_matrix Secret_return_orient; |
| 652 |
}; |
| 653 |
#endif |
| 654 |
|
| 655 |
extern d_level_shared_segment_state LevelSharedSegmentState; |
| 656 |
|
| 657 |
} |
| 658 |
#endif |