From ca689c3948a56a3ecc660f90d002d4be9ba19f10 Mon Sep 17 00:00:00 2001 From: duncanspumpkin Date: Sun, 19 Sep 2021 08:28:25 +0100 Subject: [PATCH] Introduce methods from NSF These methods still call the same legacy functionality for now --- src/openrct2-ui/windows/Staff.cpp | 10 ++---- src/openrct2-ui/windows/StaffList.cpp | 4 +-- .../actions/StaffSetPatrolAreaAction.cpp | 9 +++-- src/openrct2/peep/Peep.h | 5 +++ src/openrct2/peep/Staff.cpp | 35 ++++++++++++++++--- src/openrct2/peep/Staff.h | 2 -- src/openrct2/rct1/S4Importer.cpp | 2 +- 7 files changed, 45 insertions(+), 22 deletions(-) diff --git a/src/openrct2-ui/windows/Staff.cpp b/src/openrct2-ui/windows/Staff.cpp index 2a691c0fab..19780be13f 100644 --- a/src/openrct2-ui/windows/Staff.cpp +++ b/src/openrct2-ui/windows/Staff.cpp @@ -530,7 +530,7 @@ void window_staff_overview_mousedown(rct_window* w, rct_widgetindex widgetIndex, } // Disable clear patrol area if no area is set. - if (gStaffModes[peep->StaffId] != StaffMode::Patrol) + if (!peep->HasPatrolArea()) { Dropdown::SetDisabled(1, true); } @@ -568,12 +568,8 @@ void window_staff_overview_dropdown(rct_window* w, rct_widgetindex widgetIndex, { return; } - for (int32_t i = 0; i < STAFF_PATROL_AREA_SIZE; i++) - { - gStaffPatrolAreas[peep->StaffId * STAFF_PATROL_AREA_SIZE + i] = 0; - } - assert(gStaffModes[peep->StaffId] == StaffMode::Patrol); - gStaffModes[peep->StaffId] = StaffMode::Walk; + // TODO: THIS SHOULD BE NETWORKED + peep->ClearPatrolArea(); gfx_invalidate_screen(); staff_update_greyed_patrol_areas(); diff --git a/src/openrct2-ui/windows/StaffList.cpp b/src/openrct2-ui/windows/StaffList.cpp index ef633b3bb2..61fae8ff24 100644 --- a/src/openrct2-ui/windows/StaffList.cpp +++ b/src/openrct2-ui/windows/StaffList.cpp @@ -414,7 +414,7 @@ public: DrawTextEllipsised(&dpi, { actionOffset, y }, actionColumnSize, format, ft); // True if a patrol path is set for the worker - if (gStaffModes[peep->StaffId] == StaffMode::Patrol) + if (peep->HasPatrolArea()) { gfx_draw_sprite(&dpi, ImageId(SPR_STAFF_PATROL_PATH), { nameColumnSize + 5, y }); } @@ -566,7 +566,7 @@ private: if (isPatrolAreaSet) { - if (gStaffModes[peep->StaffId] != StaffMode::Patrol) + if (!peep->HasPatrolArea()) { continue; } diff --git a/src/openrct2/actions/StaffSetPatrolAreaAction.cpp b/src/openrct2/actions/StaffSetPatrolAreaAction.cpp index 8f981a9468..13aff35e18 100644 --- a/src/openrct2/actions/StaffSetPatrolAreaAction.cpp +++ b/src/openrct2/actions/StaffSetPatrolAreaAction.cpp @@ -63,14 +63,13 @@ GameActions::Result::Ptr StaffSetPatrolAreaAction::Execute() const return MakeResult(GameActions::Status::InvalidParameters, STR_NONE); } - int32_t patrolOffset = staff->StaffId * STAFF_PATROL_AREA_SIZE; - - staff_toggle_patrol_area(staff->StaffId, _loc); + staff->TogglePatrolArea(_loc); bool isPatrolling = false; - for (int32_t i = 0; i < 128; i++) + const auto peepOffset = staff->StaffId * STAFF_PATROL_AREA_SIZE; + for (int32_t i = peepOffset; i < peepOffset + STAFF_PATROL_AREA_SIZE; i++) { - if (gStaffPatrolAreas[patrolOffset + i]) + if (gStaffPatrolAreas[i] != 0) { isPatrolling = true; break; diff --git a/src/openrct2/peep/Peep.h b/src/openrct2/peep/Peep.h index 08ca29df35..8dbced8f2c 100644 --- a/src/openrct2/peep/Peep.h +++ b/src/openrct2/peep/Peep.h @@ -881,6 +881,11 @@ public: static void ResetStats(); void Serialise(DataSerialiser& stream); + void ClearPatrolArea(); + void TogglePatrolArea(const CoordsXY& coords); + void SetPatrolArea(const CoordsXY& coords, bool value); + bool HasPatrolArea() const; + private: void UpdatePatrolling(); void UpdateMowing(); diff --git a/src/openrct2/peep/Staff.cpp b/src/openrct2/peep/Staff.cpp index b61136f668..91727cf43f 100644 --- a/src/openrct2/peep/Staff.cpp +++ b/src/openrct2/peep/Staff.cpp @@ -174,7 +174,7 @@ bool Staff::IsLocationInPatrol(const CoordsXY& loc) const return false; // Check if staff has patrol area - if (gStaffModes[StaffId] != StaffMode::Patrol) + if (!HasPatrolArea()) return true; return IsPatrolAreaSet(loc); @@ -392,9 +392,9 @@ bool staff_is_patrol_area_set_for_type(StaffType type, const CoordsXY& coords) return staff_is_patrol_area_set(STAFF_MAX_COUNT + static_cast(type), coords); } -void staff_set_patrol_area(int32_t staffIndex, const CoordsXY& coords, bool value) +void Staff::SetPatrolArea(const CoordsXY& coords, bool value) { - int32_t peepOffset = staffIndex * STAFF_PATROL_AREA_SIZE; + int32_t peepOffset = StaffId * STAFF_PATROL_AREA_SIZE; auto [offset, bitIndex] = getPatrolAreaOffsetIndex(coords); uint32_t* addr = &gStaffPatrolAreas[peepOffset + offset]; if (value) @@ -407,13 +407,38 @@ void staff_set_patrol_area(int32_t staffIndex, const CoordsXY& coords, bool valu } } -void staff_toggle_patrol_area(int32_t staffIndex, const CoordsXY& coords) +void Staff::ClearPatrolArea() { - int32_t peepOffset = staffIndex * STAFF_PATROL_AREA_SIZE; + const auto peepOffset = StaffId * STAFF_PATROL_AREA_SIZE; + std::fill_n(&gStaffPatrolAreas[peepOffset], STAFF_PATROL_AREA_SIZE, 0); + gStaffModes[StaffId] = StaffMode::Walk; +} + +void Staff::TogglePatrolArea(const CoordsXY& coords) +{ + int32_t peepOffset = StaffId * STAFF_PATROL_AREA_SIZE; auto [offset, bitIndex] = getPatrolAreaOffsetIndex(coords); gStaffPatrolAreas[peepOffset + offset] ^= (1 << bitIndex); } +bool Staff::HasPatrolArea() const +{ + if (gStaffModes[StaffId] != StaffMode::Patrol) + { + return false; + } + + const auto peepOffset = StaffId * STAFF_PATROL_AREA_SIZE; + for (int32_t i = peepOffset; i < peepOffset + STAFF_PATROL_AREA_SIZE; i++) + { + if (gStaffPatrolAreas[i] != 0) + { + return true; + } + } + return false; +} + /** * * rct2: 0x006BFBE8 diff --git a/src/openrct2/peep/Staff.h b/src/openrct2/peep/Staff.h index 795b9d0fcc..cbd4f05482 100644 --- a/src/openrct2/peep/Staff.h +++ b/src/openrct2/peep/Staff.h @@ -65,8 +65,6 @@ void staff_set_name(uint16_t spriteIndex, const char* name); bool staff_hire_new_member(StaffType staffType, EntertainerCostume entertainerType); void staff_update_greyed_patrol_areas(); bool staff_is_patrol_area_set_for_type(StaffType type, const CoordsXY& coords); -void staff_set_patrol_area(int32_t staffIndex, const CoordsXY& coords, bool value); -void staff_toggle_patrol_area(int32_t staffIndex, const CoordsXY& coords); colour_t staff_get_colour(StaffType staffType); bool staff_set_colour(StaffType staffType, colour_t value); uint32_t staff_get_available_entertainer_costumes(); diff --git a/src/openrct2/rct1/S4Importer.cpp b/src/openrct2/rct1/S4Importer.cpp index 6916c69a81..536177741c 100644 --- a/src/openrct2/rct1/S4Importer.cpp +++ b/src/openrct2/rct1/S4Importer.cpp @@ -1293,7 +1293,7 @@ namespace RCT1 x <<= 7; int32_t y = val & 0x3E0; y <<= 2; - staff_set_patrol_area(staffmember->StaffId, { x, y }, true); + staffmember->SetPatrolArea({ x, y }, true); } } }