@@ -301,6 +301,17 @@ enum class PatrolAreaValue
301301
302302static 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 */
350361void 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 */
443458void 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 */
636663static 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
0 commit comments