From c7a85c51148c00d577547118fa297ed5f8c0f45f Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Sat, 7 Nov 2015 17:56:03 +0000 Subject: [PATCH] add desync debug checks for scenario_rand --- projects/openrct2.vcxproj | 2 +- src/game.c | 8 ++++++++ src/game.h | 1 + src/scenario.c | 7 +++++++ src/world/climate.c | 11 ++++++----- 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/projects/openrct2.vcxproj b/projects/openrct2.vcxproj index e41e31ab10..05dae3caf6 100644 --- a/projects/openrct2.vcxproj +++ b/projects/openrct2.vcxproj @@ -366,7 +366,7 @@ Disabled true 1Byte - DEBUG;_CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;_USE_MATH_DEFINES;CURL_STATICLIB;SDL_MAIN_HANDLED;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) + DEBUG;DEBUG_DESYNC;_CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;_USE_MATH_DEFINES;CURL_STATICLIB;SDL_MAIN_HANDLED;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreaded true $(IntDir)fake\%(RelativeDir) diff --git a/src/game.c b/src/game.c index 1312661de4..c15739fe7e 100644 --- a/src/game.c +++ b/src/game.c @@ -60,6 +60,7 @@ int gGameSpeed = 1; float gDayNightCycle = 0; +bool gInUpdateCode = false; GAME_COMMAND_CALLBACK_POINTER* game_command_callback = 0; GAME_COMMAND_CALLBACK_POINTER* game_command_callback_table[] = { @@ -335,6 +336,9 @@ void game_update() void game_logic_update() { + /////////////////////////// + gInUpdateCode = true; + /////////////////////////// network_update(); if (network_get_mode() == NETWORK_MODE_CLIENT && network_get_status() == NETWORK_STATUS_CONNECTED) { if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) >= network_get_server_tick()) { @@ -361,6 +365,10 @@ void game_logic_update() research_update(); ride_ratings_update_all(); ride_measurements_update(); + /////////////////////////// + gInUpdateCode = false; + /////////////////////////// + map_animation_invalidate_all(); vehicle_sounds_update(); peep_update_crowd_noise(); diff --git a/src/game.h b/src/game.h index 286c21fb1f..f2ef77a7e0 100644 --- a/src/game.h +++ b/src/game.h @@ -114,6 +114,7 @@ GAME_COMMAND_CALLBACK_POINTER* game_command_callback_get_callback(int index); extern int gGameSpeed; extern float gDayNightCycle; +extern bool gInUpdateCode; void game_increase_game_speed(); void game_reduce_game_speed(); diff --git a/src/scenario.c b/src/scenario.c index 597c0d452e..18124dfaa8 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -715,6 +715,13 @@ static int scenario_create_ducks() */ unsigned int scenario_rand() { +#if DEBUG_DESYNC + if (!gInUpdateCode) { + log_warning("scenario_rand called from outside game update"); + assert(false); + } +#endif + int eax = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_0, uint32); RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_0, uint32) += ror32(RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_1, uint32) ^ 0x1234567F, 7); return RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_1, uint32) = ror32(eax, 3); diff --git a/src/world/climate.c b/src/world/climate.c index eeae821230..5692acb13b 100644 --- a/src/world/climate.c +++ b/src/world/climate.c @@ -23,6 +23,7 @@ #include "../audio/mixer.h" #include "../config.h" #include "../drawing/drawing.h" +#include "../game.h" #include "../localisation/date.h" #include "../scenario.h" #include "../interface/window.h" @@ -69,7 +70,7 @@ static unsigned int _thunderSoundId; static int _thunderVolume; static int _thunderStereoEcho = 0; -static void climate_determine_future_weather(); +static void climate_determine_future_weather(int randomDistribution); static void climate_update_rain_sound(); static void climate_update_thunder_sound(); @@ -114,7 +115,7 @@ void climate_reset(int climate) _rainVolume = 1; } - climate_determine_future_weather(); + climate_determine_future_weather(rand()); } sint8 step_weather_level(sint8 cur_weather_level, sint8 next_weather_level) { @@ -171,7 +172,7 @@ void climate_update() if (cur_rain == next_rain) { RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_WEATHER, sint8) = gClimateNextWeather; - climate_determine_future_weather(); + climate_determine_future_weather(scenario_rand()); RCT2_GLOBAL(RCT2_ADDRESS_BTM_TOOLBAR_DIRTY_FLAGS, uint32) |= BTM_TB_DIRTY_FLAG_CLIMATE; } else if (next_rain <= 2) { // Safe-guard RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_RAIN_LEVEL, sint8) = step_weather_level(cur_rain, next_rain); @@ -209,7 +210,7 @@ void climate_force_weather(uint8 weather){ * * rct2: 0x006C461C */ -static void climate_determine_future_weather() +static void climate_determine_future_weather(int randomDistribution) { sint8 climate = RCT2_GLOBAL(RCT2_ADDRESS_CLIMATE, sint8); const rct_weather_transition* climate_table = climate_transitions[climate]; @@ -217,7 +218,7 @@ static void climate_determine_future_weather() rct_weather_transition transition = climate_table[month]; // Generate a random variable with values 0 upto distribution_size-1 and chose weather from the distribution table accordingly - sint8 next_weather = transition.distribution[ ((scenario_rand() & 0xFF) * transition.distribution_size) >> 8 ]; + sint8 next_weather = transition.distribution[ ((randomDistribution & 0xFF) * transition.distribution_size) >> 8 ]; gClimateNextWeather = next_weather; _climateNextTemperature = transition.base_temperature + climate_weather_data[next_weather].temp_delta;