Close #12434: Refactor ENTERTAINER_COSTUME to use strong enum (#12865)

This commit is contained in:
Jacob Schwartz 2020-09-08 15:29:18 -04:00 committed by GitHub
parent 30ffced8b9
commit fe71ce437d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 75 additions and 65 deletions

View file

@ -282,7 +282,7 @@ static constexpr const uint32_t window_staff_page_enabled_widgets[] = {
};
// clang-format on
static uint8_t _availableCostumes[ENTERTAINER_COSTUME_COUNT];
static EntertainerCostume _availableCostumes[static_cast<uint8_t>(EntertainerCostume::Count)];
enum class PatrolAreaValue
{
@ -1472,12 +1472,12 @@ void window_staff_options_mousedown(rct_window* w, rct_widgetindex widgetIndex,
int32_t numCostumes = staff_get_available_entertainer_costume_list(_availableCostumes);
for (int32_t i = 0; i < numCostumes; i++)
{
uint8_t costume = _availableCostumes[i];
if (peep->SpriteType == PEEP_SPRITE_TYPE_ENTERTAINER_PANDA + costume)
EntertainerCostume costume = _availableCostumes[i];
if (peep->SpriteType == EntertainerCostumeToSprite(costume))
{
checkedIndex = i;
}
gDropdownItemsArgs[i] = StaffCostumeNames[costume];
gDropdownItemsArgs[i] = StaffCostumeNames[static_cast<uint8_t>(costume)];
gDropdownItemsFormat[i] = STR_DROPDOWN_MENU_LABEL;
}
@ -1510,7 +1510,7 @@ void window_staff_options_dropdown(rct_window* w, rct_widgetindex widgetIndex, i
if (dropdownIndex == -1)
return;
uint8_t costume = _availableCostumes[dropdownIndex];
EntertainerCostume costume = _availableCostumes[dropdownIndex];
auto staffSetCostumeAction = StaffSetCostumeAction(w->number, costume);
GameActions::Execute(&staffSetCostumeAction);
}

View file

@ -129,7 +129,7 @@ static rct_widget window_staff_list_widgets[] = {
static int32_t _windowStaffListHighlightedIndex;
static int32_t _windowStaffListSelectedTab = WINDOW_STAFF_LIST_TAB_HANDYMEN;
static uint8_t window_staff_list_get_random_entertainer_costume();
static EntertainerCostume window_staff_list_get_random_entertainer_costume();
struct staff_naming_convention
{
@ -231,10 +231,10 @@ static void window_staff_list_mouseup(rct_window* w, rct_widgetindex widgetIndex
case WIDX_STAFF_LIST_HIRE_BUTTON:
{
StaffType staffType = static_cast<StaffType>(_windowStaffListSelectedTab);
ENTERTAINER_COSTUME costume = ENTERTAINER_COSTUME_COUNT;
auto costume = EntertainerCostume::Count;
if (staffType == StaffType::Entertainer)
{
costume = static_cast<ENTERTAINER_COSTUME>(window_staff_list_get_random_entertainer_costume());
costume = window_staff_list_get_random_entertainer_costume();
}
staff_hire_new_member(staffType, costume);
break;
@ -756,10 +756,10 @@ void window_staff_list_scrollpaint(rct_window* w, rct_drawpixelinfo* dpi, int32_
}
}
static uint8_t window_staff_list_get_random_entertainer_costume()
static EntertainerCostume window_staff_list_get_random_entertainer_costume()
{
uint8_t result = ENTERTAINER_COSTUME_PANDA;
uint8_t costumeList[ENTERTAINER_COSTUME_COUNT];
auto result = EntertainerCostume::Panda;
EntertainerCostume costumeList[static_cast<uint8_t>(EntertainerCostume::Count)];
int32_t numCostumes = staff_get_available_entertainer_costume_list(costumeList);
if (numCostumes > 0)
{

View file

@ -55,12 +55,12 @@ DEFINE_GAME_ACTION(StaffHireNewAction, GAME_COMMAND_HIRE_NEW_STAFF_MEMBER, Staff
private:
bool _autoPosition = false;
uint8_t _staffType = static_cast<uint8_t>(StaffType::Count);
uint8_t _entertainerType = ENTERTAINER_COSTUME::ENTERTAINER_COSTUME_COUNT;
EntertainerCostume _entertainerType = EntertainerCostume::Count;
uint32_t _staffOrders = 0;
public:
StaffHireNewAction() = default;
StaffHireNewAction(bool autoPosition, StaffType staffType, ENTERTAINER_COSTUME entertainerType, uint32_t staffOrders)
StaffHireNewAction(bool autoPosition, StaffType staffType, EntertainerCostume entertainerType, uint32_t staffOrders)
: _autoPosition(autoPosition)
, _staffType(static_cast<uint8_t>(staffType))
, _entertainerType(entertainerType)
@ -112,7 +112,7 @@ private:
if (_staffType == static_cast<uint8_t>(StaffType::Entertainer))
{
if (_entertainerType >= ENTERTAINER_COSTUME_COUNT)
if (static_cast<uint8_t>(_entertainerType) >= static_cast<uint8_t>(EntertainerCostume::Count))
{
// Invalid entertainer costume
log_error("Tried to use invalid entertainer type: %u", static_cast<uint32_t>(_entertainerType));
@ -121,7 +121,7 @@ private:
}
uint32_t availableCostumes = staff_get_available_entertainer_costumes();
if (!(availableCostumes & (1 << _entertainerType)))
if (!(availableCostumes & (1 << static_cast<uint8_t>(_entertainerType))))
{
// Entertainer costume unavailable
log_error("Tried to use unavailable entertainer type: %u", static_cast<uint32_t>(_entertainerType));
@ -204,7 +204,7 @@ private:
PeepSpriteType spriteType = spriteTypes[_staffType];
if (_staffType == static_cast<uint8_t>(StaffType::Entertainer))
{
spriteType = static_cast<PeepSpriteType>(PEEP_SPRITE_TYPE_ENTERTAINER_PANDA + _entertainerType);
spriteType = EntertainerCostumeToSprite(_entertainerType);
}
newPeep->Name = nullptr;
newPeep->SpriteType = spriteType;

View file

@ -42,13 +42,13 @@ DEFINE_GAME_ACTION(StaffSetCostumeAction, GAME_COMMAND_SET_STAFF_COSTUME, GameAc
{
private:
uint16_t _spriteIndex;
uint8_t _costume;
EntertainerCostume _costume;
public:
StaffSetCostumeAction()
{
}
StaffSetCostumeAction(uint16_t spriteIndex, uint8_t costume)
StaffSetCostumeAction(uint16_t spriteIndex, EntertainerCostume costume)
: _spriteIndex(spriteIndex)
, _costume(costume)
{
@ -80,7 +80,7 @@ public:
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_NONE);
}
auto spriteType = static_cast<PeepSpriteType>(_costume + 4);
auto spriteType = EntertainerCostumeToSprite(_costume);
if (spriteType > std::size(peep_slow_walking_types))
{
log_warning("Invalid game command for sprite %u", _spriteIndex);
@ -98,7 +98,7 @@ public:
return std::make_unique<GameActionResult>(GA_ERROR::INVALID_PARAMETERS, STR_NONE);
}
auto spriteType = static_cast<PeepSpriteType>(_costume + 4);
auto spriteType = EntertainerCostumeToSprite(_costume);
staff->SpriteType = spriteType;
staff->PeepFlags &= ~PEEP_FLAGS_SLOW_WALK;
if (peep_slow_walking_types[spriteType])

View file

@ -449,7 +449,7 @@ static int32_t cc_staff(InteractiveConsole& console, const arguments_t& argv)
{
console.WriteFormatLine("staff set energy <staff id> <value 0-255>");
console.WriteFormatLine("staff set costume <staff id> <costume id>");
for (int32_t i = 0; i < ENTERTAINER_COSTUME_COUNT; i++)
for (int32_t i = 0; i < static_cast<uint8_t>(EntertainerCostume::Count); i++)
{
char costume_name[128] = { 0 };
rct_string_id costume = StaffCostumeNames[i];
@ -499,13 +499,13 @@ static int32_t cc_staff(InteractiveConsole& console, const arguments_t& argv)
console.WriteLineError("Specified staff is not entertainer");
return 1;
}
if (!int_valid[1] || int_val[1] < 0 || int_val[1] >= ENTERTAINER_COSTUME_COUNT)
if (!int_valid[1] || int_val[1] < 0 || int_val[1] >= static_cast<uint8_t>(EntertainerCostume::Count))
{
console.WriteLineError("Invalid costume ID");
return 1;
}
uint8_t costume = int_val[1];
EntertainerCostume costume = static_cast<EntertainerCostume>(int_val[1]);
auto staffSetCostumeAction = StaffSetCostumeAction(int_val[0], costume);
GameActions::Execute(&staffSetCostumeAction);
}

View file

@ -115,7 +115,7 @@ void SceneryGroupObject::ReadJson(IReadObjectContext* context, const json_t* roo
auto properties = json_object_get(root, "properties");
_legacyType.priority = json_integer_value(json_object_get(properties, "priority"));
// Entertainer cosumes
// Entertainer costumes
auto jCostumes = json_object_get(properties, "entertainerCostumes");
if (jCostumes != nullptr)
{
@ -139,39 +139,37 @@ uint32_t SceneryGroupObject::ReadJsonEntertainerCostumes(const json_t* jCostumes
for (const auto& szCostume : szCostumes)
{
auto entertainer = ParseEntertainerCostume(szCostume);
// For some reason the flags are +4 from the actual costume IDs
// See staff_get_available_entertainer_costumes
costumes |= 1 << (entertainer + 4);
auto peepSprite = EntertainerCostumeToSprite(entertainer);
costumes |= 1 << (static_cast<uint8_t>(peepSprite));
}
return costumes;
}
uint32_t SceneryGroupObject::ParseEntertainerCostume(const std::string& s)
EntertainerCostume SceneryGroupObject::ParseEntertainerCostume(const std::string& s)
{
if (s == "panda")
return ENTERTAINER_COSTUME_PANDA;
return EntertainerCostume::Panda;
if (s == "tiger")
return ENTERTAINER_COSTUME_TIGER;
return EntertainerCostume::Tiger;
if (s == "elephant")
return ENTERTAINER_COSTUME_ELEPHANT;
return EntertainerCostume::Elephant;
if (s == "roman")
return ENTERTAINER_COSTUME_ROMAN;
return EntertainerCostume::Roman;
if (s == "gorilla")
return ENTERTAINER_COSTUME_GORILLA;
return EntertainerCostume::Gorilla;
if (s == "snowman")
return ENTERTAINER_COSTUME_SNOWMAN;
return EntertainerCostume::Snowman;
if (s == "knight")
return ENTERTAINER_COSTUME_KNIGHT;
return EntertainerCostume::Knight;
if (s == "astronaut")
return ENTERTAINER_COSTUME_ASTRONAUT;
return EntertainerCostume::Astronaut;
if (s == "bandit")
return ENTERTAINER_COSTUME_BANDIT;
return EntertainerCostume::Bandit;
if (s == "sheriff")
return ENTERTAINER_COSTUME_SHERIFF;
return EntertainerCostume::Sheriff;
if (s == "pirate")
return ENTERTAINER_COSTUME_PIRATE;
return ENTERTAINER_COSTUME_PANDA;
return EntertainerCostume::Pirate;
return EntertainerCostume::Panda;
}
std::vector<rct_object_entry> SceneryGroupObject::ReadJsonEntries(const json_t* jEntries)

View file

@ -16,6 +16,8 @@
struct ObjectRepositoryItem;
enum class EntertainerCostume : uint8_t;
class SceneryGroupObject final : public Object
{
private:
@ -46,6 +48,6 @@ public:
private:
static std::vector<rct_object_entry> ReadItems(OpenRCT2::IStream* stream);
static uint32_t ReadJsonEntertainerCostumes(const json_t* jCostumes);
static uint32_t ParseEntertainerCostume(const std::string& s);
static EntertainerCostume ParseEntertainerCostume(const std::string& s);
static std::vector<rct_object_entry> ReadJsonEntries(const json_t* jEntries);
};

View file

@ -103,7 +103,7 @@ void staff_reset_modes()
/**
* Hires a new staff member of the given type.
*/
bool staff_hire_new_member(StaffType staffType, ENTERTAINER_COSTUME entertainerType)
bool staff_hire_new_member(StaffType staffType, EntertainerCostume entertainerType)
{
bool autoPosition = gConfigGeneral.auto_staff_placement;
if (gInputPlaceObjectModifier & PLACE_OBJECT_MODIFIER_SHIFT_Z)
@ -1097,7 +1097,15 @@ uint8_t Staff::GetCostume() const
void Staff::SetCostume(uint8_t value)
{
SpriteType = static_cast<PeepSpriteType>(value + PEEP_SPRITE_TYPE_ENTERTAINER_PANDA);
auto costume = static_cast<EntertainerCostume>(value);
SpriteType = EntertainerCostumeToSprite(costume);
}
PeepSpriteType EntertainerCostumeToSprite(EntertainerCostume entertainerType)
{
uint8_t value = static_cast<uint8_t>(entertainerType);
PeepSpriteType newSpriteType = static_cast<PeepSpriteType>(value + PEEP_SPRITE_TYPE_ENTERTAINER_PANDA);
return newSpriteType;
}
colour_t staff_get_colour(StaffType staffType)
@ -1153,21 +1161,21 @@ uint32_t staff_get_available_entertainer_costumes()
entertainerCostumes >>= 4;
// Fix #6593: force enable the default costumes, which normally get enabled through the default scenery groups.
entertainerCostumes |= (1 << ENTERTAINER_COSTUME_PANDA) | (1 << ENTERTAINER_COSTUME_TIGER)
| (1 << ENTERTAINER_COSTUME_ELEPHANT);
entertainerCostumes |= (1 << static_cast<uint8_t>(EntertainerCostume::Panda))
| (1 << static_cast<uint8_t>(EntertainerCostume::Tiger)) | (1 << static_cast<uint8_t>(EntertainerCostume::Elephant));
return entertainerCostumes;
}
int32_t staff_get_available_entertainer_costume_list(uint8_t* costumeList)
int32_t staff_get_available_entertainer_costume_list(EntertainerCostume* costumeList)
{
uint32_t availableCostumes = staff_get_available_entertainer_costumes();
int32_t numCostumes = 0;
for (uint8_t i = 0; i < ENTERTAINER_COSTUME_COUNT; i++)
for (uint8_t i = 0; i < static_cast<uint8_t>(EntertainerCostume::Count); i++)
{
if (availableCostumes & (1 << i))
{
costumeList[numCostumes++] = i;
costumeList[numCostumes++] = static_cast<EntertainerCostume>(i);
}
}
return numCostumes;

View file

@ -35,26 +35,26 @@ enum STAFF_ORDERS
STAFF_ORDERS_FIX_RIDES = (1 << 1)
};
enum ENTERTAINER_COSTUME : uint8_t
enum class EntertainerCostume : uint8_t
{
ENTERTAINER_COSTUME_PANDA,
ENTERTAINER_COSTUME_TIGER,
ENTERTAINER_COSTUME_ELEPHANT,
ENTERTAINER_COSTUME_ROMAN,
ENTERTAINER_COSTUME_GORILLA,
ENTERTAINER_COSTUME_SNOWMAN,
ENTERTAINER_COSTUME_KNIGHT,
ENTERTAINER_COSTUME_ASTRONAUT,
ENTERTAINER_COSTUME_BANDIT,
ENTERTAINER_COSTUME_SHERIFF,
ENTERTAINER_COSTUME_PIRATE,
Panda,
Tiger,
Elephant,
Roman,
Gorilla,
Snowman,
Knight,
Astronaut,
Bandit,
Sheriff,
Pirate,
ENTERTAINER_COSTUME_COUNT
Count
};
extern const money32 gStaffWageTable[static_cast<uint8_t>(StaffType::Count)];
extern const rct_string_id StaffCostumeNames[ENTERTAINER_COSTUME_COUNT];
extern const rct_string_id StaffCostumeNames[static_cast<uint8_t>(EntertainerCostume::Count)];
extern uint32_t gStaffPatrolAreas[(STAFF_MAX_COUNT + static_cast<uint8_t>(StaffType::Count)) * STAFF_PATROL_AREA_SIZE];
extern StaffMode gStaffModes[STAFF_MAX_COUNT + static_cast<uint8_t>(StaffType::Count)];
@ -65,7 +65,7 @@ extern colour_t gStaffSecurityColour;
void staff_reset_modes();
void staff_set_name(uint16_t spriteIndex, const char* name);
bool staff_hire_new_member(StaffType staffType, ENTERTAINER_COSTUME entertainerType);
bool staff_hire_new_member(StaffType staffType, EntertainerCostume entertainerType);
void staff_update_greyed_patrol_areas();
bool staff_is_location_on_patrol_edge(Peep* mechanic, const CoordsXY& loc);
bool staff_can_ignore_wide_flag(Peep* mechanic, const CoordsXYZ& staffPos, TileElement* path);
@ -76,6 +76,8 @@ 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();
int32_t staff_get_available_entertainer_costume_list(uint8_t* costumeList);
int32_t staff_get_available_entertainer_costume_list(EntertainerCostume* costumeList);
PeepSpriteType EntertainerCostumeToSprite(EntertainerCostume entertainerType);
#endif