Skip to content

Commit 8a378ad

Browse files
Remove uses of GET_PEEP macro and replace with GetEntity (OpenRCT2#12467)
* Use TryGetEntity and GetEntity instead of macro * Use GetEntity for ui guest window * Remove final GET_PEEP macro uses * Fix remaining issues
1 parent ba10b84 commit 8a378ad

26 files changed

+496
-268
lines changed

src/openrct2-ui/windows/Guest.cpp

Lines changed: 186 additions & 102 deletions
Large diffs are not rendered by default.

src/openrct2-ui/windows/Staff.cpp

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,17 @@ enum class PatrolAreaValue
301301

302302
static PatrolAreaValue _staffPatrolAreaPaintValue = PatrolAreaValue::NONE;
303303

304+
static Staff* GetStaff(rct_window* w)
305+
{
306+
auto staff = GetEntity<Staff>(w->number);
307+
if (staff == nullptr)
308+
{
309+
window_close(w);
310+
return nullptr;
311+
}
312+
return staff;
313+
}
314+
304315
/**
305316
*
306317
* rct2: 0x006BEE98
@@ -349,7 +360,11 @@ rct_window* window_staff_open(Peep* peep)
349360
*/
350361
void window_staff_disable_widgets(rct_window* w)
351362
{
352-
Peep* peep = GetEntity<Peep>(w->number);
363+
const auto peep = GetStaff(w);
364+
if (peep == nullptr)
365+
{
366+
return;
367+
}
353368
uint64_t disabled_widgets = (1 << WIDX_TAB_4);
354369

355370
if (peep != nullptr && peep->StaffType == STAFF_TYPE_SECURITY)
@@ -442,7 +457,11 @@ void window_staff_set_page(rct_window* w, int32_t page)
442457
*/
443458
void window_staff_overview_mouseup(rct_window* w, rct_widgetindex widgetIndex)
444459
{
445-
Peep* peep = GET_PEEP(w->number);
460+
const auto peep = GetStaff(w);
461+
if (peep == nullptr)
462+
{
463+
return;
464+
}
446465

447466
switch (widgetIndex)
448467
{
@@ -569,7 +588,11 @@ void window_staff_overview_mousedown(rct_window* w, rct_widgetindex widgetIndex,
569588
window_dropdown_show_text(dropdownPos, extray, w->colours[1], 0, 2);
570589
gDropdownDefaultIndex = 0;
571590

572-
Peep* peep = GET_PEEP(w->number);
591+
const auto peep = GetStaff(w);
592+
if (peep == nullptr)
593+
{
594+
return;
595+
}
573596

574597
// Disable clear patrol area if no area is set.
575598
if (!(gStaffModes[peep->StaffId] & 2))
@@ -592,7 +615,11 @@ void window_staff_overview_dropdown(rct_window* w, rct_widgetindex widgetIndex,
592615
// Clear patrol
593616
if (dropdownIndex == 1)
594617
{
595-
Peep* peep = GET_PEEP(w->number);
618+
const auto peep = GetStaff(w);
619+
if (peep == nullptr)
620+
{
621+
return;
622+
}
596623
for (int32_t i = 0; i < STAFF_PATROL_AREA_SIZE; i++)
597624
{
598625
gStaffPatrolAreas[peep->StaffId * STAFF_PATROL_AREA_SIZE + i] = 0;
@@ -635,7 +662,11 @@ void window_staff_overview_update(rct_window* w)
635662
*/
636663
static void window_staff_set_order(rct_window* w, int32_t order_id)
637664
{
638-
Peep* peep = GET_PEEP(w->number);
665+
const auto peep = GetStaff(w);
666+
if (peep == nullptr)
667+
{
668+
return;
669+
}
639670

640671
uint8_t newOrders = peep->StaffOrders ^ (1 << order_id);
641672
auto staffSetOrdersAction = StaffSetOrdersAction(w->number, newOrders);
@@ -741,7 +772,11 @@ void window_staff_stats_update(rct_window* w)
741772
w->frame_no++;
742773
widget_invalidate(w, WIDX_TAB_3);
743774

744-
Peep* peep = GET_PEEP(w->number);
775+
auto peep = GetStaff(w);
776+
if (peep == nullptr)
777+
{
778+
return;
779+
}
745780
if (peep->WindowInvalidateFlags & PEEP_INVALIDATE_STAFF_STATS)
746781
{
747782
peep->WindowInvalidateFlags &= ~PEEP_INVALIDATE_STAFF_STATS;
@@ -774,7 +809,11 @@ void window_staff_stats_invalidate(rct_window* w)
774809

775810
w->pressed_widgets |= 1ULL << (w->page + WIDX_TAB_1);
776811

777-
Peep* peep = GET_PEEP(w->number);
812+
const auto peep = GetStaff(w);
813+
if (peep == nullptr)
814+
{
815+
return;
816+
}
778817

779818
auto ft = Formatter::Common();
780819
peep->FormatNameTo(ft);
@@ -809,7 +848,11 @@ void window_staff_options_invalidate(rct_window* w)
809848

810849
w->pressed_widgets |= 1ULL << (w->page + WIDX_TAB_1);
811850

812-
Peep* peep = GET_PEEP(w->number);
851+
const auto peep = GetStaff(w);
852+
if (peep == nullptr)
853+
{
854+
return;
855+
}
813856
auto ft = Formatter::Common();
814857
peep->FormatNameTo(ft);
815858

@@ -885,7 +928,11 @@ void window_staff_overview_invalidate(rct_window* w)
885928

886929
w->pressed_widgets |= 1ULL << (w->page + WIDX_TAB_1);
887930

888-
Peep* peep = GET_PEEP(w->number);
931+
const auto peep = GetStaff(w);
932+
if (peep == nullptr)
933+
{
934+
return;
935+
}
889936
auto ft = Formatter::Common();
890937
peep->FormatNameTo(ft);
891938

@@ -948,7 +995,11 @@ void window_staff_overview_paint(rct_window* w, rct_drawpixelinfo* dpi)
948995
}
949996

950997
// Draw the centred label
951-
Peep* peep = GET_PEEP(w->number);
998+
const auto peep = GetStaff(w);
999+
if (peep == nullptr)
1000+
{
1001+
return;
1002+
}
9521003
auto ft = Formatter::Common();
9531004
peep->FormatActionTo(ft);
9541005
rct_widget* widget = &w->widgets[WIDX_BTM_LABEL];
@@ -1024,7 +1075,11 @@ void window_staff_overview_tab_paint(rct_window* w, rct_drawpixelinfo* dpi)
10241075

10251076
screenCoords = ScreenCoordsXY{ 14, 20 };
10261077

1027-
Peep* peep = GET_PEEP(w->number);
1078+
const auto peep = GetStaff(w);
1079+
if (peep == nullptr)
1080+
{
1081+
return;
1082+
}
10281083

10291084
if (peep->AssignedPeepType == PeepType::Staff && peep->StaffType == STAFF_TYPE_ENTERTAINER)
10301085
screenCoords.y++;
@@ -1091,7 +1146,11 @@ void window_staff_stats_paint(rct_window* w, rct_drawpixelinfo* dpi)
10911146
window_staff_options_tab_paint(w, dpi);
10921147
window_staff_stats_tab_paint(w, dpi);
10931148

1094-
Peep* peep = GET_PEEP(w->number);
1149+
const auto peep = GetStaff(w);
1150+
if (peep == nullptr)
1151+
{
1152+
return;
1153+
}
10951154

10961155
auto screenCoords = w->windowPos
10971156
+ ScreenCoordsXY{ window_staff_stats_widgets[WIDX_RESIZE].left + 4, window_staff_stats_widgets[WIDX_RESIZE].top + 4 };
@@ -1173,8 +1232,11 @@ void window_staff_overview_tool_update(rct_window* w, rct_widgetindex widgetInde
11731232
w->picked_peep_frame = 0;
11741233
}
11751234

1176-
Peep* peep;
1177-
peep = GET_PEEP(w->number);
1235+
const auto peep = GetStaff(w);
1236+
if (peep == nullptr)
1237+
{
1238+
return;
1239+
}
11781240

11791241
uint32_t imageId = g_peep_animation_entries[peep->SpriteType].sprite_animation[PEEP_ACTION_SPRITE_TYPE_UI].base_image;
11801242
imageId += w->picked_peep_frame >> 2;
@@ -1327,7 +1389,11 @@ void window_staff_viewport_init(rct_window* w)
13271389

13281390
focus.sprite_id = w->number;
13291391

1330-
Peep* peep = GET_PEEP(w->number);
1392+
const auto peep = GetStaff(w);
1393+
if (peep == nullptr)
1394+
{
1395+
return;
1396+
}
13311397

13321398
if (peep->State == PEEP_STATE_PICKED)
13331399
{
@@ -1398,7 +1464,11 @@ void window_staff_options_mousedown(rct_window* w, rct_widgetindex widgetIndex,
13981464
return;
13991465
}
14001466

1401-
Peep* peep = GET_PEEP(w->number);
1467+
const auto peep = GetStaff(w);
1468+
if (peep == nullptr)
1469+
{
1470+
return;
1471+
}
14021472
int32_t checkedIndex = -1;
14031473
// This will be moved below where Items Checked is when all
14041474
// of dropdown related functions are finished. This prevents

src/openrct2-ui/windows/StaffList.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,13 @@ void window_staff_list_scrollmousedown(rct_window* w, int32_t scrollIndex, const
479479
}
480480
else
481481
{
482-
auto peep = GET_PEEP(spriteIndex);
483-
auto intent = Intent(WC_PEEP);
484-
intent.putExtra(INTENT_EXTRA_PEEP, peep);
485-
context_open_intent(&intent);
482+
auto peep = GetEntity<Staff>(spriteIndex);
483+
if (peep != nullptr)
484+
{
485+
auto intent = Intent(WC_PEEP);
486+
intent.putExtra(INTENT_EXTRA_PEEP, peep);
487+
context_open_intent(&intent);
488+
}
486489
}
487490
break;
488491
}
@@ -695,7 +698,11 @@ void window_staff_list_scrollpaint(rct_window* w, rct_drawpixelinfo* dpi, int32_
695698

696699
if (y + 11 >= dpi->y)
697700
{
698-
auto peep = GET_PEEP(spriteIndex);
701+
auto peep = GetEntity<Staff>(spriteIndex);
702+
if (peep == nullptr)
703+
{
704+
continue;
705+
}
699706
int32_t format = (_quick_fire_mode ? STR_RED_STRINGID : STR_BLACK_STRING);
700707

701708
if (i == _windowStaffListHighlightedIndex)

src/openrct2-ui/windows/TitleCommandEditor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ static void window_title_command_editor_tool_down(
622622
if (info.sprite->generic.Is<Peep>())
623623
{
624624
validSprite = true;
625-
auto peep = GET_PEEP(spriteIndex);
625+
auto peep = GetEntity<Peep>(spriteIndex);
626626
if (peep != nullptr)
627627
{
628628
uint8_t formatArgs[32]{};

src/openrct2/actions/GuestSetFlagsAction.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ DEFINE_GAME_ACTION(GuestSetFlagsAction, GAME_COMMAND_GUEST_SET_FLAGS, GameAction
4545

4646
GameActionResult::Ptr Query() const override
4747
{
48-
Peep* peep = GET_PEEP(_peepId);
48+
Peep* peep = TryGetEntity<Peep>(_peepId);
4949
if (peep == nullptr)
5050
{
5151
log_error("Used invalid sprite index for peep: %u", static_cast<uint32_t>(_peepId));
@@ -56,7 +56,7 @@ DEFINE_GAME_ACTION(GuestSetFlagsAction, GAME_COMMAND_GUEST_SET_FLAGS, GameAction
5656

5757
GameActionResult::Ptr Execute() const override
5858
{
59-
Peep* peep = GET_PEEP(_peepId);
59+
Peep* peep = TryGetEntity<Peep>(_peepId);
6060
if (peep == nullptr)
6161
{
6262
log_error("Used invalid sprite index for peep: %u", static_cast<uint32_t>(_peepId));

src/openrct2/actions/GuestSetNameAction.hpp

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ DEFINE_GAME_ACTION(GuestSetNameAction, GAME_COMMAND_SET_GUEST_NAME, GameActionRe
7272
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_NAME_GUEST, STR_NONE);
7373
}
7474

75-
auto peep = GET_PEEP(_spriteIndex);
76-
if (peep->AssignedPeepType != PeepType::Guest)
75+
auto guest = TryGetEntity<Guest>(_spriteIndex);
76+
if (guest == nullptr)
7777
{
7878
log_warning("Invalid game command for sprite %u", _spriteIndex);
7979
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_NAME_GUEST, STR_NONE);
@@ -84,41 +84,36 @@ DEFINE_GAME_ACTION(GuestSetNameAction, GAME_COMMAND_SET_GUEST_NAME, GameActionRe
8484

8585
GameActionResult::Ptr Execute() const override
8686
{
87-
auto peep = GET_PEEP(_spriteIndex);
88-
if (peep->AssignedPeepType != PeepType::Guest)
87+
auto guest = TryGetEntity<Guest>(_spriteIndex);
88+
if (guest == nullptr)
8989
{
9090
log_warning("Invalid game command for sprite %u", _spriteIndex);
9191
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_CANT_NAME_GUEST, STR_NONE);
9292
}
9393

94-
auto curName = peep->GetName();
94+
auto curName = guest->GetName();
9595
if (curName == _name)
9696
{
9797
return std::make_unique<GameActionResult>(GA_ERROR::OK, STR_NONE);
9898
}
9999

100-
if (!peep->SetName(_name))
100+
if (!guest->SetName(_name))
101101
{
102102
return std::make_unique<GameActionResult>(GA_ERROR::UNKNOWN, STR_CANT_NAME_GUEST, STR_NONE);
103103
}
104104

105105
// Easter egg functions are for guests only
106-
Guest* guest = peep->AsGuest();
107-
108-
if (guest != nullptr)
109-
{
110-
guest->HandleEasterEggName();
111-
}
106+
guest->HandleEasterEggName();
112107

113108
gfx_invalidate_screen();
114109

115110
auto intent = Intent(INTENT_ACTION_REFRESH_GUEST_LIST);
116111
context_broadcast_intent(&intent);
117112

118113
auto res = std::make_unique<GameActionResult>();
119-
res->Position.x = peep->x;
120-
res->Position.y = peep->y;
121-
res->Position.z = peep->z;
114+
res->Position.x = guest->x;
115+
res->Position.y = guest->y;
116+
res->Position.z = guest->z;
122117
return res;
123118
}
124119
};

src/openrct2/actions/PeepPickupAction.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ DEFINE_GAME_ACTION(PeepPickupAction, GAME_COMMAND_PICKUP_GUEST, GameActionResult
6565
return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_ERR_CANT_PLACE_PERSON_HERE);
6666
}
6767

68-
Peep* const peep = GET_PEEP(_spriteId);
68+
auto* const peep = TryGetEntity<Peep>(_spriteId);
6969
if (!peep || peep->sprite_identifier != SPRITE_IDENTIFIER_PEEP)
7070
{
7171
log_error("Failed to pick up peep for sprite %d", _spriteId);
@@ -124,7 +124,7 @@ DEFINE_GAME_ACTION(PeepPickupAction, GAME_COMMAND_PICKUP_GUEST, GameActionResult
124124

125125
GameActionResult::Ptr Execute() const override
126126
{
127-
Peep* const peep = GET_PEEP(_spriteId);
127+
Peep* const peep = TryGetEntity<Peep>(_spriteId);
128128
if (!peep || peep->sprite_identifier != SPRITE_IDENTIFIER_PEEP)
129129
{
130130
log_error("Failed to pick up peep for sprite %d", _spriteId);

src/openrct2/actions/SetCheatAction.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ DEFINE_GAME_ACTION(SetCheatAction, GAME_COMMAND_CHEAT, GameActionResult)
664664
{
665665
offset++;
666666
}
667-
auto peep = GET_PEEP(vehicle->peep[i + offset]);
667+
auto peep = TryGetEntity<Guest>(vehicle->peep[i + offset]);
668668
if (peep != nullptr)
669669
{
670670
vehicle->mass -= peep->Mass;

src/openrct2/actions/StaffFireAction.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ DEFINE_GAME_ACTION(StaffFireAction, GAME_COMMAND_FIRE_STAFF_MEMBER, GameActionRe
4747
return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE);
4848
}
4949

50-
auto peep = GET_PEEP(_spriteId);
51-
if (peep == nullptr || peep->sprite_identifier != SPRITE_IDENTIFIER_PEEP || peep->AssignedPeepType != PeepType::Staff)
50+
auto staff = TryGetEntity<Staff>(_spriteId);
51+
if (staff == nullptr)
5252
{
5353
log_error("Invalid spriteId. spriteId = %u", _spriteId);
5454
return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE);
@@ -59,14 +59,14 @@ DEFINE_GAME_ACTION(StaffFireAction, GAME_COMMAND_FIRE_STAFF_MEMBER, GameActionRe
5959

6060
GameActionResult::Ptr Execute() const override
6161
{
62-
auto peep = GET_PEEP(_spriteId);
63-
if (peep == nullptr || peep->sprite_identifier != SPRITE_IDENTIFIER_PEEP || peep->AssignedPeepType != PeepType::Staff)
62+
auto staff = TryGetEntity<Staff>(_spriteId);
63+
if (staff == nullptr)
6464
{
6565
log_error("Invalid spriteId. spriteId = %u", _spriteId);
6666
return MakeResult(GA_ERROR::INVALID_PARAMETERS, STR_NONE);
6767
}
6868
window_close_by_class(WC_FIRE_PROMPT);
69-
peep_sprite_remove(peep);
69+
peep_sprite_remove(staff);
7070
return MakeResult();
7171
}
7272
};

0 commit comments

Comments
 (0)