Fix #9270: Refactor money effect

Changed static functions to rct_money_effect member functions. GetStringId now returns std::pair.
This commit is contained in:
aw20368 2019-05-21 15:22:47 -04:00 committed by Michael Steenbeek
parent 831d69083e
commit 8c20b635bd
8 changed files with 58 additions and 48 deletions

View File

@ -130,6 +130,7 @@ The following people are not part of the development team, but have been contrib
* Florian Will (w-flo) * Florian Will (w-flo)
* Trevor Harkness (tharkne) * Trevor Harkness (tharkne)
* Steve Xu (stevexu-umich) * Steve Xu (stevexu-umich)
* (aw20368)
## Toolchain ## Toolchain
* (Balletie) - macOS * (Balletie) - macOS

View File

@ -528,7 +528,7 @@ int32_t game_do_command_p(
{ {
// Create a +/- money text effect // Create a +/- money text effect
if (cost != 0 && game_is_not_paused()) if (cost != 0 && game_is_not_paused())
money_effect_create(cost); rct_money_effect::Create(cost);
} }
} }

View File

@ -301,7 +301,7 @@ namespace GameActions
if (result->Error == GA_ERROR::OK && finance_check_money_required(flags) && result->Cost != 0) if (result->Error == GA_ERROR::OK && finance_check_money_required(flags) && result->Cost != 0)
{ {
finance_payment(result->Cost, result->ExpenditureType); finance_payment(result->Cost, result->ExpenditureType);
money_effect_create(result->Cost); rct_money_effect::Create(result->Cost);
} }
if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && result->Error == GA_ERROR::OK) if (!(actionFlags & GA_FLAGS::CLIENT_ONLY) && result->Error == GA_ERROR::OK)

View File

@ -49,8 +49,7 @@ void misc_paint(paint_session* session, const rct_sprite* misc, int32_t imageDir
} }
const rct_money_effect* moneyEffect = &misc->money_effect; const rct_money_effect* moneyEffect = &misc->money_effect;
money32 value; auto [stringId, value] = moneyEffect->GetStringId();
rct_string_id stringId = money_effect_get_string_id(moneyEffect, &value);
paint_floating_money_effect( paint_floating_money_effect(
session, value, stringId, moneyEffect->y, moneyEffect->z, (int8_t*)&money_wave[moneyEffect->wiggle % 22], session, value, stringId, moneyEffect->y, moneyEffect->z, (int8_t*)&money_wave[moneyEffect->wiggle % 22],
moneyEffect->offset_x, session->CurrentRotation); moneyEffect->offset_x, session->CurrentRotation);

View File

@ -2041,7 +2041,7 @@ void Guest::SpendMoney(money16& peep_expend_type, money32 amount)
// needing to be synchronised // needing to be synchronised
if (network_get_mode() == NETWORK_MODE_NONE && !gOpenRCT2Headless) if (network_get_mode() == NETWORK_MODE_NONE && !gOpenRCT2Headless)
{ {
money_effect_create_at(amount, x, y, z, true); rct_money_effect::CreateAt(amount, x, y, z, true);
} }
} }

View File

@ -17,11 +17,27 @@
static constexpr const LocationXY16 _moneyEffectMoveOffset[] = { { 1, -1 }, { 1, 1 }, { -1, 1 }, { -1, -1 } }; static constexpr const LocationXY16 _moneyEffectMoveOffset[] = { { 1, -1 }, { 1, 1 }, { -1, 1 }, { -1, -1 } };
bool rct_sprite::IsMoneyEffect()
{
return this->money_effect.sprite_identifier == SPRITE_IDENTIFIER_MISC
&& this->money_effect.type == SPRITE_MISC_MONEY_EFFECT;
}
rct_money_effect* rct_sprite::AsMoneyEffect()
{
rct_money_effect* result = nullptr;
if (IsMoneyEffect())
{
result = (rct_money_effect*)this;
}
return result;
}
/** /**
* *
* rct2: 0x0067351F * rct2: 0x0067351F
*/ */
void money_effect_create_at(money32 value, int32_t x, int32_t y, int32_t z, bool vertical) void rct_money_effect::CreateAt(money32 value, int32_t x, int32_t y, int32_t z, bool vertical)
{ {
if (value == MONEY(0, 00)) if (value == MONEY(0, 00))
return; return;
@ -44,10 +60,9 @@ void money_effect_create_at(money32 value, int32_t x, int32_t y, int32_t z, bool
int16_t offsetX = 0; int16_t offsetX = 0;
if (!gOpenRCT2NoGraphics) if (!gOpenRCT2NoGraphics)
{ {
// Construct string to display auto [stringId, newValue] = moneyEffect->GetStringId();
rct_string_id stringId = money_effect_get_string_id(moneyEffect, &value);
char buffer[128]; char buffer[128];
format_string(buffer, 128, stringId, &value); format_string(buffer, 128, stringId, &newValue);
gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM; gCurrentFontSpriteBase = FONT_SPRITE_BASE_MEDIUM;
offsetX = -(gfx_get_string_width(buffer) / 2); offsetX = -(gfx_get_string_width(buffer) / 2);
} }
@ -59,7 +74,7 @@ void money_effect_create_at(money32 value, int32_t x, int32_t y, int32_t z, bool
* *
* rct2: 0x0069C5D0 * rct2: 0x0069C5D0
*/ */
void money_effect_create(money32 value) void rct_money_effect::Create(money32 value)
{ {
LocationXYZ16 mapPosition = { gCommandPosition.x, gCommandPosition.y, gCommandPosition.z }; LocationXYZ16 mapPosition = { gCommandPosition.x, gCommandPosition.y, gCommandPosition.z };
@ -79,66 +94,62 @@ void money_effect_create(money32 value)
mapPosition.z = tile_element_height(mapPosition.x, mapPosition.y); mapPosition.z = tile_element_height(mapPosition.x, mapPosition.y);
} }
mapPosition.z += 10; mapPosition.z += 10;
money_effect_create_at(-value, mapPosition.x, mapPosition.y, mapPosition.z, false); CreateAt(-value, mapPosition.x, mapPosition.y, mapPosition.z, false);
} }
/** /**
* *
* rct2: 0x00673232 * rct2: 0x00673232
*/ */
void money_effect_update(rct_money_effect* moneyEffect) void rct_money_effect::Update()
{ {
invalidate_sprite_2((rct_sprite*)moneyEffect); invalidate_sprite_2((rct_sprite*)this);
moneyEffect->wiggle++; wiggle++;
if (moneyEffect->wiggle >= 22) if (wiggle >= 22)
{ {
moneyEffect->wiggle = 0; wiggle = 0;
} }
moneyEffect->move_delay++; move_delay++;
if (moneyEffect->move_delay < 2) if (move_delay < 2)
{ {
return; return;
} }
int32_t x = moneyEffect->x; int32_t newX = x;
int32_t y = moneyEffect->y; int32_t newY = y;
int32_t z = moneyEffect->z; int32_t newZ = z;
moneyEffect->move_delay = 0; move_delay = 0;
if (moneyEffect->vertical) if (vertical)
{ {
z += 1; newZ += 1;
} }
y += _moneyEffectMoveOffset[get_current_rotation()].y; newY += _moneyEffectMoveOffset[get_current_rotation()].y;
x += _moneyEffectMoveOffset[get_current_rotation()].x; newX += _moneyEffectMoveOffset[get_current_rotation()].x;
sprite_move(x, y, z, (rct_sprite*)moneyEffect); sprite_move(newX, newY, newZ, (rct_sprite*)this);
moneyEffect->num_movements++; num_movements++;
if (moneyEffect->num_movements < 55) if (num_movements < 55)
{ {
return; return;
} }
sprite_remove((rct_sprite*)moneyEffect); sprite_remove((rct_sprite*)this);
} }
rct_string_id money_effect_get_string_id(const rct_money_effect* sprite, money32* outValue) std::pair<rct_string_id, money32> rct_money_effect::GetStringId() const
{ {
bool vertical = (sprite->vertical != 0);
rct_string_id spentStringId = vertical ? STR_MONEY_EFFECT_SPEND_HIGHP : STR_MONEY_EFFECT_SPEND; rct_string_id spentStringId = vertical ? STR_MONEY_EFFECT_SPEND_HIGHP : STR_MONEY_EFFECT_SPEND;
rct_string_id receiveStringId = vertical ? STR_MONEY_EFFECT_RECEIVE_HIGHP : STR_MONEY_EFFECT_RECEIVE; rct_string_id receiveStringId = vertical ? STR_MONEY_EFFECT_RECEIVE_HIGHP : STR_MONEY_EFFECT_RECEIVE;
rct_string_id stringId = receiveStringId; rct_string_id stringId = receiveStringId;
money32 value = sprite->value; money32 outValue = value;
if (value < 0) if (value < 0)
{ {
value *= -1; outValue *= -1;
stringId = spentStringId; stringId = spentStringId;
} }
if (outValue != nullptr)
{ return std::make_pair(stringId, outValue);
*outValue = value;
}
return stringId;
} }

View File

@ -541,7 +541,7 @@ static void sprite_misc_update(rct_sprite* sprite)
sprite_steam_particle_update((rct_steam_particle*)sprite); sprite_steam_particle_update((rct_steam_particle*)sprite);
break; break;
case SPRITE_MISC_MONEY_EFFECT: case SPRITE_MISC_MONEY_EFFECT:
money_effect_update(&sprite->money_effect); sprite->money_effect.Update();
break; break;
case SPRITE_MISC_CRASHED_VEHICLE_PARTICLE: case SPRITE_MISC_CRASHED_VEHICLE_PARTICLE:
crashed_vehicle_particle_update((rct_crashed_vehicle_particle*)sprite); crashed_vehicle_particle_update((rct_crashed_vehicle_particle*)sprite);

View File

@ -93,6 +93,11 @@ struct rct_money_effect : rct_sprite_common
money32 value; money32 value;
int16_t offset_x; int16_t offset_x;
uint16_t wiggle; uint16_t wiggle;
static void CreateAt(money32 value, int32_t x, int32_t y, int32_t z, bool vertical);
static void Create(money32 value);
void Update();
std::pair<rct_string_id, money32> GetStringId() const;
}; };
struct rct_crashed_vehicle_particle : rct_sprite_generic struct rct_crashed_vehicle_particle : rct_sprite_generic
@ -139,9 +144,11 @@ union rct_sprite
bool IsBalloon(); bool IsBalloon();
bool IsDuck(); bool IsDuck();
bool IsMoneyEffect();
bool IsPeep(); bool IsPeep();
rct_balloon* AsBalloon(); rct_balloon* AsBalloon();
rct_duck* AsDuck(); rct_duck* AsDuck();
rct_money_effect* AsMoneyEffect();
Peep* AsPeep(); Peep* AsPeep();
}; };
assert_struct_size(rct_sprite, 0x100); assert_struct_size(rct_sprite, 0x100);
@ -239,14 +246,6 @@ void duck_press(rct_duck* duck);
void duck_remove_all(); void duck_remove_all();
uint32_t duck_get_frame_image(const rct_duck* duck, int32_t direction); uint32_t duck_get_frame_image(const rct_duck* duck, int32_t direction);
///////////////////////////////////////////////////////////////
// Money effect
///////////////////////////////////////////////////////////////
void money_effect_create(money32 value);
void money_effect_create_at(money32 value, int32_t x, int32_t y, int32_t z, bool vertical);
void money_effect_update(rct_money_effect* moneyEffect);
rct_string_id money_effect_get_string_id(const rct_money_effect* sprite, money32* outValue);
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////
// Crash particles // Crash particles
/////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////