From b588e519c4a1ad57e13792bc262a51118923ac75 Mon Sep 17 00:00:00 2001 From: LRFLEW Date: Sun, 14 Jun 2015 02:08:28 -0500 Subject: [PATCH] Some Cleanup of Past PRs --- data/language/english_uk.txt | 2 +- src/peep/staff.c | 18 ++---------------- src/scenario.c | 12 ++++++++++++ src/scenario.h | 1 + src/util/util.c | 2 +- src/windows/cheats.c | 3 +-- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/data/language/english_uk.txt b/data/language/english_uk.txt index 379cf6b46c..8ba1fcb4cb 100644 --- a/data/language/english_uk.txt +++ b/data/language/english_uk.txt @@ -3619,7 +3619,7 @@ STR_5282 :RCT1 Ride Open/Close Lights STR_5283 :RCT1 Park Open/Close Lights STR_5284 :RCT1 Scenario Selection Font STR_5285 :EXPLODE!!! -STR_5286 :{MEDIUMFONT}{BLACK}Makes guests explode +STR_5286 :{MEDIUMFONT}{BLACK}Makes some guests explode STR_5287 :Ride is already broken down STR_5288 :Ride is closed STR_5289 :No breakdowns available for this ride diff --git a/src/peep/staff.c b/src/peep/staff.c index f6fa857107..c1f5979f58 100644 --- a/src/peep/staff.c +++ b/src/peep/staff.c @@ -187,14 +187,7 @@ void game_command_hire_new_staff_member(int* eax, int* ebx, int* ecx, int* edx, } if (count > 0) { - uint32 max = ((uint32)0xFFFFFFFF) - (((uint32)0xFFFFFFFF) % count) - 1; - if (max + count == 0) max = ((uint32)0xFFFFFFFF); - uint32 rand; - do { - rand = scenario_rand(); - } while (rand > max); - rand %= count; - + uint32 rand = scenario_rand_max(count); for (i = 0; i < 4; ++i) { if (RCT2_ADDRESS(RCT2_ADDRESS_PARK_ENTRANCE_X, uint16)[i] != SPRITE_LOCATION_NULL) { if (rand == 0) break; @@ -215,14 +208,7 @@ void game_command_hire_new_staff_member(int* eax, int* ebx, int* ecx, int* edx, z = newPeep->z; } } else { - uint32 max = ((uint32)0xFFFFFFFF) - (((uint32)0xFFFFFFFF) % count) - 1; - if (max + count == 0) max = ((uint32)0xFFFFFFFF); - uint32 rand; - do { - rand = scenario_rand(); - } while (rand > max); - rand %= count; - + uint32 rand = scenario_rand_max(count); FOR_ALL_GUESTS(sprite_index, guest) if (guest->state == PEEP_STATE_WALKING) { if (rand == 0) break; diff --git a/src/scenario.c b/src/scenario.c index af22c23b1e..4cd598a604 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -648,6 +648,18 @@ unsigned int scenario_rand() return RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_1, uint32) = ror32(eax, 3); } +unsigned int scenario_rand_max(unsigned int max) +{ + if (max < 2) return 0; + if ((max & (max - 1)) == 0) + return scenario_rand() & (max - 1); + unsigned int rand, cap = ~((unsigned int)0) - (~((unsigned int)0) % max) - 1; + do { + rand = scenario_rand(); + } while (rand > cap); + return rand % max; +} + /** * Prepare rides, for the finish five rollercoasters objective. * rct2: 0x006788F7 diff --git a/src/scenario.h b/src/scenario.h index 56771d16c1..9e015fddf1 100644 --- a/src/scenario.h +++ b/src/scenario.h @@ -411,6 +411,7 @@ int scenario_load_and_play(const rct_scenario_basic *scenario); int scenario_load_and_play_from_path(const char *path); void scenario_update(); unsigned int scenario_rand(); +unsigned int scenario_rand_max(unsigned int max); int scenario_prepare_for_save(); int scenario_save(char *path, int flags); void scenario_set_filename(const char *value); diff --git a/src/util/util.c b/src/util/util.c index 8194f87cba..4ed997066c 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -158,4 +158,4 @@ int strcicmp(char const *a, char const *b) bool utf8_is_bom(const char *str) { return str[0] == 0xEF && str[1] == 0xBB && str[2] == 0xBF; -} \ No newline at end of file +} diff --git a/src/windows/cheats.c b/src/windows/cheats.c index 3048369028..81b81c514d 100644 --- a/src/windows/cheats.c +++ b/src/windows/cheats.c @@ -538,8 +538,7 @@ static void cheat_explode_guests() rct_peep *peep; FOR_ALL_GUESTS(sprite_index, peep) { - unsigned int rand = scenario_rand(); - if ((rand & 0x07) == 0) { + if (scenario_rand_max(6) == 0) { peep->flags |= PEEP_FLAGS_EXPLODE; } }