From 3e02f99bcd649b5b285c7e90b24abc355f146046 Mon Sep 17 00:00:00 2001 From: Kp Date: Mon, 1 Nov 2021 03:37:20 +0000 Subject: [PATCH] Use enum class for materialization_center_number --- common/main/fuelcen.h | 2 +- common/main/fwd-segment.h | 2 ++ common/main/segment.h | 7 ++++++- similar/editor/group.cpp | 2 +- similar/editor/medwall.cpp | 11 +++++------ similar/editor/mine.cpp | 4 ++-- similar/editor/segment.cpp | 8 ++++---- similar/main/dumpmine.cpp | 12 ++++++------ similar/main/fuelcen.cpp | 15 ++++++--------- similar/main/gamemine.cpp | 14 +++++++++++--- similar/main/segment.cpp | 2 +- 11 files changed, 45 insertions(+), 34 deletions(-) diff --git a/common/main/fuelcen.h b/common/main/fuelcen.h index 5c4f4b04d..726b515b6 100644 --- a/common/main/fuelcen.h +++ b/common/main/fuelcen.h @@ -155,7 +155,7 @@ struct d_level_shared_robotcenter_state { unsigned Num_robot_centers; // Original D1/D2 size: 20 - std::array RobotCenters; + enumerated_array RobotCenters; }; extern d_level_shared_robotcenter_state LevelSharedRobotcenterState; diff --git a/common/main/fwd-segment.h b/common/main/fwd-segment.h index c83b5ec71..153ebd0ba 100644 --- a/common/main/fwd-segment.h +++ b/common/main/fwd-segment.h @@ -66,6 +66,8 @@ void delete_segment_from_group(vmsegptridx_t segment_num, unsigned group_num); namespace dcx { +enum class materialization_center_number : uint8_t; + typedef uint_fast32_t sidenum_fast_t; enum sidenum_t : uint8_t diff --git a/common/main/segment.h b/common/main/segment.h index d4cce7c2d..c4bd43a8f 100644 --- a/common/main/segment.h +++ b/common/main/segment.h @@ -82,6 +82,11 @@ enum class segment_special : uint8_t goal_red, }; +enum class materialization_center_number : uint8_t +{ + None = 0xff, +}; + struct shared_side { struct illegal_type; @@ -189,7 +194,7 @@ struct shared_segment std::array children; // indices of 6 children segments, front, left, top, right, bottom, back std::array verts; // vertex ids of 4 front and 4 back vertices segment_special special; // what type of center this is - int8_t matcen_num; // which center segment is associated with. + materialization_center_number matcen_num; // which center segment is associated with. uint8_t station_idx; /* if DXX_BUILD_DESCENT_II */ uint8_t s2_flags; diff --git a/similar/editor/group.cpp b/similar/editor/group.cpp index da3cbe1a3..fcd7a03e4 100644 --- a/similar/editor/group.cpp +++ b/similar/editor/group.cpp @@ -599,7 +599,7 @@ static int med_copy_group(const unsigned delta_flag, const vmsegptridx_t base_se shared_segment &s = *vmsegptr(gs); s.group = new_current_group; s.special = segment_special::nothing; - s.matcen_num = -1; + s.matcen_num = materialization_center_number::None; } auto &vcvertptr = Vertices.vcptr; diff --git a/similar/editor/medwall.cpp b/similar/editor/medwall.cpp index 7ccc4b572..b1ebc3185 100644 --- a/similar/editor/medwall.cpp +++ b/similar/editor/medwall.cpp @@ -1051,20 +1051,19 @@ int check_walls() { auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters; std::array CountedWalls; - int matcen_num; unsigned wall_count = 0; range_for (const auto &&segp, vmsegptridx) { if (segp->segnum != segment_none) { // Check fuelcenters - matcen_num = segp->matcen_num; - if (matcen_num == 0) - if (RobotCenters[0].segnum != segp) { - segp->matcen_num = -1; + const auto matcen_num = segp->matcen_num; + if (matcen_num == materialization_center_number{0u}) + if (RobotCenters[matcen_num].segnum != segp) { + segp->matcen_num = materialization_center_number::None; } - if (matcen_num > -1) + if (matcen_num != materialization_center_number::None) RobotCenters[matcen_num].segnum = segp; for (auto &&[idx, s] : enumerate(segp->shared_segment::sides)) diff --git a/similar/editor/mine.cpp b/similar/editor/mine.cpp index e32b8e363..c77c7cfa7 100644 --- a/similar/editor/mine.cpp +++ b/similar/editor/mine.cpp @@ -540,7 +540,7 @@ static void write_special(const shared_segment &seg, const unsigned bit_mask, PH if (bit_mask & (1 << MAX_SIDES_PER_SEGMENT)) { PHYSFSX_writeU8(SaveFile, underlying_value(seg.special)); - PHYSFSX_writeU8(SaveFile, seg.matcen_num); + PHYSFSX_writeU8(SaveFile, underlying_value(seg.matcen_num)); PHYSFS_writeULE16(SaveFile, seg.station_idx); } } @@ -604,7 +604,7 @@ int save_mine_data_compiled(PHYSFS_File *SaveFile) bit_mask |= (1 << sidenum); } - if (seg.s.special != segment_special::nothing || seg.s.matcen_num != 0 || seg.s.station_idx != station_none) + if (seg.s.special != segment_special::nothing || seg.s.matcen_num != materialization_center_number::None || seg.s.station_idx != station_none) bit_mask |= (1 << MAX_SIDES_PER_SEGMENT); if (New_file_format_save) diff --git a/similar/editor/segment.cpp b/similar/editor/segment.cpp index 0d1b4ac15..b13930d87 100644 --- a/similar/editor/segment.cpp +++ b/similar/editor/segment.cpp @@ -672,7 +672,7 @@ static int med_attach_segment_rotated(const vmsegptridx_t destseg, const csmuseg nsp->segnum = segnum; static_cast(nsp).objects = object_none; - nsp->matcen_num = -1; + nsp->matcen_num = materialization_center_number::None; // Copy group value. nsp->group = destseg->group; @@ -1298,7 +1298,7 @@ void med_create_segment(const vmsegptridx_t sp,fix cx, fix cy, fix cz, fix lengt } sp->group = -1; - sp->matcen_num = -1; + sp->matcen_num = materialization_center_number::None; // Create relative-to-center vertices, which are the rotated points on the box defined by length, width, height sp->verts[0] = med_add_vertex(vertex{vm_vec_rotate({+width/2, +height/2, -length/2}, mp)}); @@ -1334,7 +1334,7 @@ void med_create_segment(const vmsegptridx_t sp,fix cx, fix cy, fix cz, fix lengt sp->special = segment_special::nothing; sp->station_idx = station_none; usp.static_light = 0; - sp->matcen_num = -1; + sp->matcen_num = materialization_center_number::None; copy_tmaps_to_segment(sp, vcsegptr(&New_segment)); @@ -1391,7 +1391,7 @@ void med_create_new_segment(const vms_vector &scale) sp->special = segment_special::nothing; sp->station_idx = station_none; usp.static_light = 0; - sp->matcen_num = -1; + sp->matcen_num = materialization_center_number::None; } // ------------------------------------------------------------------------------- diff --git a/similar/main/dumpmine.cpp b/similar/main/dumpmine.cpp index e17c9494e..e6406431d 100644 --- a/similar/main/dumpmine.cpp +++ b/similar/main/dumpmine.cpp @@ -457,8 +457,8 @@ static void write_segment_text(fvcsegptridx &vcsegptridx, PHYSFS_File *my_file) PHYSFSX_printf(my_file, "Segment %4hu:", static_cast(segp)); if (segp->special != segment_special::nothing) PHYSFSX_printf(my_file, " special = %3i (%s), station_idx=%3i", underlying_value(segp->special), Special_names[segp->special], segp->station_idx); - if (segp->matcen_num != -1) - PHYSFSX_printf(my_file, " matcen = %3i", segp->matcen_num); + if (segp->matcen_num != materialization_center_number::None) + PHYSFSX_printf(my_file, " matcen = %3i", underlying_value(segp->matcen_num)); PHYSFSX_puts_literal(my_file, "\n"); } @@ -498,14 +498,14 @@ static void write_matcen_text(PHYSFS_File *my_file) auto &station = Station[fuelcen_num]; if (station.Type != segment_special::robotmaker) { - err_printf(my_file, "Error: Matcen %" PRIuFAST32 " corresponds to Station %i, which has type %i (%s).", i, fuelcen_num, underlying_value(station.Type), Special_names[station.Type]); + err_printf(my_file, "Error: Matcen %i corresponds to Station %i, which has type %i (%s).", underlying_value(i), fuelcen_num, underlying_value(station.Type), Special_names[station.Type]); continue; } int trigger_count=0; const auto segnum = station.segnum; - PHYSFSX_printf(my_file, "FuelCenter[%02" PRIuFAST32 "].Segment = %04i ", i, segnum); - PHYSFSX_printf(my_file, "Segment[%04i].matcen_num = %02i ", segnum, Segments[segnum].matcen_num); + PHYSFSX_printf(my_file, "FuelCenter[%02i].Segment = %04i ", underlying_value(i), segnum); + PHYSFSX_printf(my_file, "Segment[%04i].matcen_num = %02i ", segnum, underlying_value(Segments[segnum].matcen_num)); // Find trigger for this materialization center. range_for (auto &&t, vctrgptridx) @@ -523,7 +523,7 @@ static void write_matcen_text(PHYSFS_File *my_file) PHYSFSX_puts_literal(my_file, "\n"); if (trigger_count == 0) - err_printf(my_file, "Error: Matcen %" PRIuFAST32 " in segment %i has no trigger!", i, segnum); + err_printf(my_file, "Error: Matcen %i in segment %i has no trigger!", underlying_value(i), segnum); } } diff --git a/similar/main/fuelcen.cpp b/similar/main/fuelcen.cpp index 49886a124..a1fcabba9 100644 --- a/similar/main/fuelcen.cpp +++ b/similar/main/fuelcen.cpp @@ -107,7 +107,7 @@ static void reset_all_robot_centers() if (i.special == segment_special::robotmaker) { i.special = segment_special::nothing; - i.matcen_num = -1; + i.matcen_num = materialization_center_number::None; } } #endif @@ -166,7 +166,7 @@ static void matcen_create(const vmsegptridx_t segp) station.Timer = -1; station.Flag = 0; - const auto next_robot_center_idx = LevelSharedRobotcenterState.Num_robot_centers++; + const auto next_robot_center_idx = static_cast(LevelSharedRobotcenterState.Num_robot_centers++); segp->matcen_num = next_robot_center_idx; auto &robotcenter = RobotCenters[next_robot_center_idx]; robotcenter.fuelcen_num = next_fuelcenter_idx; @@ -200,8 +200,7 @@ void trigger_matcen(const vmsegptridx_t segp) FuelCenter *robotcen; assert(segp->special == segment_special::robotmaker); - assert(segp->matcen_num < LevelUniqueFuelcenterState.Num_fuelcenters); - Assert((segp->matcen_num >= 0) && (segp->matcen_num <= Highest_segment_index)); + assert(underlying_value(segp->matcen_num) < LevelUniqueFuelcenterState.Num_fuelcenters); robotcen = &Station[RobotCenters[segp->matcen_num].fuelcen_num]; @@ -272,7 +271,7 @@ Restart: ; { shared_segment &sfj = vmsegptr(fj.segnum); if (sfj.matcen_num > segp.matcen_num) - --sfj.matcen_num; + sfj.matcen_num = static_cast(underlying_value(sfj.matcen_num) - 1); } } } @@ -359,7 +358,6 @@ static void robotmaker_proc(const d_vclip_array &Vclip, fvmsegptridx &vmsegptrid auto &vcobjptr = Objects.vcptr; auto &vmobjptridx = Objects.vmptridx; auto &RobotCenters = LevelSharedRobotcenterState.RobotCenters; - int matcen_num; fix top_time; if (robotcen->Enabled == 0) @@ -382,11 +380,10 @@ static void robotmaker_proc(const d_vclip_array &Vclip, fvmsegptridx &vmsegptrid } const auto &&segp = vmsegptr(robotcen->segnum); - matcen_num = segp->matcen_num; + const auto matcen_num = segp->matcen_num; - if ( matcen_num == -1 ) { + if (!RobotCenters.valid_index(matcen_num)) return; - } matcen_info *mi = &RobotCenters[matcen_num]; for (unsigned i = 0;; ++i) diff --git a/similar/main/gamemine.cpp b/similar/main/gamemine.cpp index dd5e62e61..1ff8851b8 100644 --- a/similar/main/gamemine.cpp +++ b/similar/main/gamemine.cpp @@ -76,13 +76,21 @@ static segment_special build_segment_special_from_untrusted(uint8_t untrusted) } } +static materialization_center_number build_materialization_center_number_from_untrusted(uint8_t untrusted) +{ + if (decltype(d_level_shared_robotcenter_state::RobotCenters)::valid_index(untrusted)) + return materialization_center_number{untrusted}; + else + return materialization_center_number::None; +} + /* * reads a segment2 structure from a PHYSFS_File */ static void segment2_read(const msmusegment s2, PHYSFS_File *fp) { s2.s.special = build_segment_special_from_untrusted(PHYSFSX_readByte(fp)); - s2.s.matcen_num = PHYSFSX_readByte(fp); + s2.s.matcen_num = build_materialization_center_number_from_untrusted(PHYSFSX_readByte(fp)); /* station_idx is overwritten by the caller in some cases, but set * it here for compatibility with how the game previously worked */ s2.s.station_idx = PHYSFSX_readByte(fp); @@ -414,12 +422,12 @@ static void read_special(shared_segment &segp, const unsigned bit_mask, PHYSFS_F // Read ubyte Segments[segnum].special segp.special = build_segment_special_from_untrusted(PHYSFSX_readByte(LoadFile)); // Read byte Segments[segnum].matcen_num - segp.matcen_num = PHYSFSX_readByte(LoadFile); + segp.matcen_num = build_materialization_center_number_from_untrusted(PHYSFSX_readByte(LoadFile)); // Read short Segments[segnum].value segp.station_idx = PHYSFSX_readShort(LoadFile); } else { segp.special = segment_special::nothing; - segp.matcen_num = -1; + segp.matcen_num = materialization_center_number::None; segp.station_idx = station_none; } } diff --git a/similar/main/segment.cpp b/similar/main/segment.cpp index 12c982531..3c1819f09 100644 --- a/similar/main/segment.cpp +++ b/similar/main/segment.cpp @@ -71,7 +71,7 @@ void dl_index_read(dl_index *di, PHYSFS_File *fp) void segment2_write(const cscusegment s2, PHYSFS_File *fp) { PHYSFSX_writeU8(fp, underlying_value(s2.s.special)); - PHYSFSX_writeU8(fp, s2.s.matcen_num); + PHYSFSX_writeU8(fp, underlying_value(s2.s.matcen_num)); PHYSFSX_writeU8(fp, s2.s.station_idx); PHYSFSX_writeU8(fp, s2.s.s2_flags); PHYSFSX_writeFix(fp, s2.u.static_light);