diff --git a/data/language/english_uk.txt b/data/language/english_uk.txt index 3e57a568c3..6c49fdf65b 100644 --- a/data/language/english_uk.txt +++ b/data/language/english_uk.txt @@ -3831,3 +3831,9 @@ STR_5489 :{SMALLFONT}{BLACK}Show only tracked guests STR_5490 :Disable audio on focus loss STR_5491 :Inventions list STR_5492 :Scenario options + +STR_6000 :Send Message +STR_6001 :Type the message you would like to send. +STR_6002 :Player List +STR_6003 :Player: +STR_6004 :Ping: diff --git a/projects/openrct2.vcxproj b/projects/openrct2.vcxproj index 156eefd8f4..a6886387b4 100644 --- a/projects/openrct2.vcxproj +++ b/projects/openrct2.vcxproj @@ -44,6 +44,7 @@ + @@ -74,6 +75,7 @@ + @@ -96,6 +98,8 @@ + + @@ -202,6 +206,7 @@ + @@ -230,6 +235,7 @@ + @@ -341,7 +347,7 @@ Disabled true 1Byte - _CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;_USE_MATH_DEFINES;CURL_STATICLIB;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;_CURL_STATICLIB;%(PreprocessorDefinitions) MultiThreaded true $(IntDir)fake\%(RelativeDir) @@ -372,7 +378,7 @@ xcopy /YS "$(SolutionDir)\..\Data" "$(TargetDir)\Data" false - _CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;_USE_MATH_DEFINES;CURL_STATICLIB;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;CURL_STATICLIB;%(PreprocessorDefinitions) $(IntDir)fake\%(RelativeDir) true Speed @@ -412,7 +418,7 @@ xcopy /YS "$(SolutionDir)\..\Data" "$(TargetDir)\Data" false - _CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;_USE_MATH_DEFINES;CURL_STATICLIB;ENABLE_TESTS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;HAVE_CONFIG_H;CURL_STATICLIB;ENABLE_TESTS;%(PreprocessorDefinitions) $(IntDir)fake\%(RelativeDir) true diff --git a/projects/openrct2.vcxproj.filters b/projects/openrct2.vcxproj.filters index cfc5b0b556..a1068acb6d 100644 --- a/projects/openrct2.vcxproj.filters +++ b/projects/openrct2.vcxproj.filters @@ -498,6 +498,13 @@ Source\Localisation + + + Source\Network + + + Source\Windows + Source\Drawing @@ -740,5 +747,8 @@ Source\Drawing + + Source\Network + \ No newline at end of file diff --git a/projects/openrct2.vcxproj.user b/projects/openrct2.vcxproj.user index 093ce205e6..6e0ad43592 100644 --- a/projects/openrct2.vcxproj.user +++ b/projects/openrct2.vcxproj.user @@ -1,21 +1,23 @@  + + false + + + $(TargetDir)\openrct2.exe + WindowsLocalDebugger + $(TargetDir) + $(TargetDir) WindowsLocalDebugger $(TargetDir)\openrct2.exe - - + "C:\Program Files (x86)\Infogrames\RollerCoaster Tycoon 2\Scenarios\Crazy Castle.SC6" --port 5552 --server 127.0.0.1 $(TargetDir) WindowsLocalDebugger $(TargetDir)\openrct2.exe - - - - - false $(TargetDir)\openrct2.exe diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index 4927d5ea9e..0ba7743bee 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -780,6 +780,9 @@ void Mixer_Init(const char* device) void* Mixer_Play_Effect(int id, int loop, int volume, float pan, double rate, int deleteondone) { + if (!gConfigSound.sound) { + return 0; + } if (id >= countof(gMixer.css1sources)) { return 0; } @@ -842,6 +845,9 @@ void Mixer_Channel_SetGroup(void* channel, int group) void* Mixer_Play_Music(int pathid, int loop, int streaming) { + if (!gConfigSound.sound) { + return 0; + } if (streaming) { const char* filename = get_file_path(pathid); diff --git a/src/cmdline.c b/src/cmdline.c index c964e26e20..10d0b78169 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -23,6 +23,7 @@ #include "addresses.h" #include "cmdline.h" #include "interface/screenshot.h" +#include "network/network.h" #include "openrct2.h" #include "platform/platform.h" #include "util/util.h" @@ -45,6 +46,10 @@ typedef int (*cmdline_action)(const char **argv, int argc); int gExitCode = 0; +int gNetworkStart = NETWORK_MODE_NONE; +char gNetworkStartHost[128]; +int gNetworkStartPort = NETWORK_DEFAULT_PORT; + static void print_launch_information(); static int cmdline_call_action(const char **argv, int argc); @@ -64,13 +69,16 @@ static const char *const usage[] = { int cmdline_run(const char **argv, int argc) { // - int version = 0, verbose = 0, width = 0, height = 0; + int version = 0, verbose = 0, width = 0, height = 0, port = 0; + char *server = NULL; argparse_option_t options[] = { OPT_HELP(), OPT_BOOLEAN('v', "version", &version, "show version information and exit"), OPT_BOOLEAN(0, "verbose", &verbose, "log verbose messages"), OPT_INTEGER('m', "mode", &sprite_mode, "the type of sprite conversion. 0 = default, 1 = simple closest pixel match, 2 = dithering"), + OPT_STRING(0, "server", &server, "server to connect to"), + OPT_INTEGER(0, "port", &port, "port"), OPT_END() }; @@ -88,6 +96,16 @@ int cmdline_run(const char **argv, int argc) if (verbose) _log_levels[DIAGNOSTIC_LEVEL_VERBOSE] = 1; + if (port != 0) { + gNetworkStart = NETWORK_MODE_SERVER; + gNetworkStartPort = port; + } + + if (server != NULL) { + gNetworkStart = NETWORK_MODE_CLIENT; + strncpy(gNetworkStartHost, server, sizeof(gNetworkStartHost)); + } + if (argc != 0) { gExitCode = cmdline_call_action(argv, argc); if (gExitCode != 0) diff --git a/src/cmdline.h b/src/cmdline.h index b86393f047..f8e99229d1 100644 --- a/src/cmdline.h +++ b/src/cmdline.h @@ -28,6 +28,10 @@ extern int gExitCode; int sprite_mode; +extern int gNetworkStart; +extern char gNetworkStartHost[128]; +extern int gNetworkStartPort; + int cmdline_run(const char **argv, int argc); int cmdline_for_sprite(const char **argv, int argc); diff --git a/src/config.c b/src/config.c index 459eba5c33..32246efe8f 100644 --- a/src/config.c +++ b/src/config.c @@ -986,6 +986,7 @@ static const uint16 _defaultShortcutKeys[SHORTCUT_COUNT] = { SDL_SCANCODE_EQUALS, // SHORTCUT_INCREASE_GAME_SPEED, CTRL | ALT | SDL_SCANCODE_C, // SHORTCUT_OPEN_CHEAT_WINDOW, SDL_SCANCODE_T, // SHORTCUT_REMOVE_TOP_BOTTOM_TOOLBAR_TOGGLE, + SDL_SCANCODE_C, // SHORTCUT_OPEN_CHAT_WINDOW SDL_SCANCODE_UP, // SHORTCUT_SCROLL_MAP_UP SDL_SCANCODE_LEFT, // SHORTCUT_SCROLL_MAP_LEFT SDL_SCANCODE_DOWN, // SHORTCUT_SCROLL_MAP_DOWN diff --git a/src/config.h b/src/config.h index 5678b3f444..e8410ac7f2 100644 --- a/src/config.h +++ b/src/config.h @@ -72,6 +72,7 @@ enum { SHORTCUT_INCREASE_GAME_SPEED, SHORTCUT_OPEN_CHEAT_WINDOW, SHORTCUT_REMOVE_TOP_BOTTOM_TOOLBAR_TOGGLE, + SHORTCUT_OPEN_CHAT_WINDOW, SHORTCUT_SCROLL_MAP_UP, SHORTCUT_SCROLL_MAP_LEFT, SHORTCUT_SCROLL_MAP_DOWN, diff --git a/src/drawing/drawing.h b/src/drawing/drawing.h index 49b36f6c57..64ce9c6018 100644 --- a/src/drawing/drawing.h +++ b/src/drawing/drawing.h @@ -135,6 +135,7 @@ void draw_string_right_underline(rct_drawpixelinfo *dpi, int format, void *args, int string_get_height_raw(char *buffer); void gfx_draw_string_centred_wrapped_partial(rct_drawpixelinfo *dpi, int x, int y, int width, int colour, rct_string_id format, void *args, int ticks); void gfx_draw_string_with_y_offsets(rct_drawpixelinfo *dpi, utf8 *text, int colour, int x, int y, const sint8 *yOffsets); +int gfx_clip_string(char* buffer, int width); bool ttf_initialise(); void ttf_dispose(); diff --git a/src/drawing/rain.c b/src/drawing/rain.c index cfae4449f2..2357ef7069 100644 --- a/src/drawing/rain.c +++ b/src/drawing/rain.c @@ -167,8 +167,8 @@ void update_rain_animation() */ static void draw_light_rain(int left, int top, int width, int height) { - int x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 8; - int y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 3) + 7; + int x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 8; + int y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 3) + 7; y_start = -y_start; x_start += left; @@ -176,8 +176,8 @@ static void draw_light_rain(int left, int top, int width, int height) gfx_draw_rain(left, top, width, height, x_start, y_start); - x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 0x18; - y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 4) + 0x0D; + x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 0x18; + y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 4) + 0x0D; y_start = -y_start; x_start += left; @@ -191,8 +191,8 @@ static void draw_light_rain(int left, int top, int width, int height) */ static void draw_heavy_rain(int left, int top, int width, int height) { - int x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int); - int y_start = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 5; + int x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32); + int y_start = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 5; y_start = -y_start; x_start += left; @@ -200,8 +200,8 @@ static void draw_heavy_rain(int left, int top, int width, int height) gfx_draw_rain(left, top, width, height, x_start, y_start); - x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 0x10; - y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 6) + 5; + x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 0x10; + y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 6) + 5; y_start = -y_start; x_start += left; @@ -209,8 +209,8 @@ static void draw_heavy_rain(int left, int top, int width, int height) gfx_draw_rain(left, top, width, height, x_start, y_start); - x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 8; - y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 3) + 7; + x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 8; + y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 3) + 7; y_start = -y_start; x_start += left; @@ -218,8 +218,8 @@ static void draw_heavy_rain(int left, int top, int width, int height) gfx_draw_rain(left, top, width, height, x_start, y_start); - x_start = -RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) + 0x18; - y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) * 4) + 0x0D; + x_start = -(int)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) + 0x18; + y_start = (RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) * 4) + 0x0D; y_start = -y_start; x_start += left; diff --git a/src/game.c b/src/game.c index a5eab478be..6a497edfa9 100644 --- a/src/game.c +++ b/src/game.c @@ -34,6 +34,7 @@ #include "management/marketing.h" #include "management/news_item.h" #include "management/research.h" +#include "network/network.h" #include "object.h" #include "openrct2.h" #include "peep/peep.h" @@ -60,6 +61,30 @@ int gGameSpeed = 1; float gDayNightCycle = 0; +GAME_COMMAND_CALLBACK_POINTER* game_command_callback = 0; +GAME_COMMAND_CALLBACK_POINTER* game_command_callback_table[] = { + 0, + game_command_callback_ride_construct_new, +}; + +int game_command_callback_get_index(GAME_COMMAND_CALLBACK_POINTER* callback) +{ + for (int i = 0; i < countof(game_command_callback_table); i++ ) { + if (game_command_callback_table[i] == callback) { + return i; + } + } + return 0; +} + +GAME_COMMAND_CALLBACK_POINTER* game_command_callback_get_callback(int index) +{ + if (index < countof(game_command_callback_table)) { + return game_command_callback_table[index]; + } + return 0; +} + void game_increase_game_speed() { gGameSpeed = min(gConfigGeneral.debugging_tools ? 5 : 4, gGameSpeed + 1); @@ -232,6 +257,13 @@ void game_update() numUpdates = clamp(1, numUpdates, 4); } + if (network_get_mode() == NETWORK_MODE_CLIENT) { + if (network_get_server_tick() - RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) >= 10) { + // make sure client doesn't fall behind the server too much + numUpdates += 10; + } + } + // Update the game one or more times if (RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) == 0) { for (i = 0; i < numUpdates; i++) { @@ -270,7 +302,7 @@ void game_update() // the flickering frequency is reduced by 4, compared to the original // it was done due to inability to reproduce original frequency // and decision that the original one looks too fast - if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32) % 4 == 0) + if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) % 4 == 0) RCT2_GLOBAL(RCT2_ADDRESS_WINDOW_MAP_FLASHING_FLAGS, uint16) ^= (1 << 15); // Handle guest map flashing @@ -287,8 +319,6 @@ void game_update() window_map_tooltip_update_visibility(); - RCT2_GLOBAL(0x01388698, uint16)++; - // Input RCT2_GLOBAL(0x0141F568, uint8) = RCT2_GLOBAL(0x0013CA740, uint8); game_handle_input(); @@ -302,8 +332,16 @@ void game_update() void game_logic_update() { - RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32)++; - RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, sint32)++; + network_update(); + if (network_get_mode() == NETWORK_MODE_CLIENT) { + if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) == network_get_server_tick()) { + // dont run past the server + return; + } + } + + RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32)++; + RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32)++; RCT2_GLOBAL(0x009DEA66, sint16)++; if (RCT2_GLOBAL(0x009DEA66, sint16) == 0) RCT2_GLOBAL(0x009DEA66, sint16)--; @@ -327,6 +365,8 @@ void game_logic_update() climate_update_sound(); editor_open_windows_for_current_step(); + RCT2_GLOBAL(0x01388698, uint16)++; + // Update windows //window_dispatch_update_all(); @@ -402,6 +442,16 @@ int game_do_command_p(int command, int *eax, int *ebx, int *ecx, int *edx, int * // Increment nest count RCT2_GLOBAL(0x009A8C28, uint8)++; + // Remove ghost scenery so it doesn't interfere with incoming network command + if ((flags & GAME_COMMAND_FLAG_NETWORKED) && !(flags & GAME_COMMAND_FLAG_GHOST) && + (command == GAME_COMMAND_PLACE_FENCE || + command == GAME_COMMAND_PLACE_SCENERY || + command == GAME_COMMAND_PLACE_LARGE_SCENERY || + command == GAME_COMMAND_PLACE_BANNER || + command == GAME_COMMAND_PLACE_PATH)) { + scenery_remove_ghost_tool_placement(); + } + *ebx &= ~GAME_COMMAND_FLAG_APPLY; // Primary command @@ -415,7 +465,7 @@ int game_do_command_p(int command, int *eax, int *ebx, int *ecx, int *edx, int * if (cost != MONEY32_UNDEFINED) { // Check funds insufficientFunds = 0; - if (RCT2_GLOBAL(0x009A8C28, uint8) == 1 && !(flags & 4) && !(flags & 0x20) && cost != 0) + if (RCT2_GLOBAL(0x009A8C28, uint8) == 1 && !(flags & GAME_COMMAND_FLAG_2) && !(flags & GAME_COMMAND_FLAG_5) && cost != 0) insufficientFunds = game_check_affordability(cost); if (insufficientFunds != MONEY32_UNDEFINED) { @@ -425,18 +475,34 @@ int game_do_command_p(int command, int *eax, int *ebx, int *ecx, int *edx, int * *edi = original_edi; *ebp = original_ebp; - if (!(flags & 1)) { + if (!(flags & GAME_COMMAND_FLAG_APPLY)) { // Decrement nest count RCT2_GLOBAL(0x009A8C28, uint8)--; return cost; } + if (network_get_mode() != NETWORK_MODE_NONE && !(flags & GAME_COMMAND_FLAG_NETWORKED) && !(flags & GAME_COMMAND_FLAG_GHOST) && !(flags & GAME_COMMAND_FLAG_5) && RCT2_GLOBAL(0x009A8C28, uint8) == 1) { + network_send_gamecmd(*eax, *ebx, *ecx, *edx, *esi, *edi, *ebp, game_command_callback_get_index(game_command_callback)); + if (network_get_mode() == NETWORK_MODE_CLIENT) { + game_command_callback = 0; + // Decrement nest count + RCT2_GLOBAL(0x009A8C28, uint8)--; + return cost; + } + } + // Secondary command if (game_do_command_table[command] == 0) { new_game_command_table[command](eax, ebx, ecx, edx, esi, edi, ebp); } else { RCT2_CALLFUNC_X(game_do_command_table[command], eax, ebx, ecx, edx, esi, edi, ebp); } + + if (game_command_callback) { + game_command_callback(*eax, *ebx, *ecx, *edx, *esi, *edi, *ebp); + game_command_callback = 0; + } + *edx = *ebx; if (*edx != MONEY32_UNDEFINED && *edx < cost) @@ -468,7 +534,7 @@ int game_do_command_p(int command, int *eax, int *ebx, int *ecx, int *edx, int * RCT2_GLOBAL(0x009A8C28, uint8)--; // Show error window - if (RCT2_GLOBAL(0x009A8C28, uint8) == 0 && (flags & 1) && RCT2_GLOBAL(0x0141F568, uint8) == RCT2_GLOBAL(0x013CA740, uint8) && !(flags & 8)) + if (RCT2_GLOBAL(0x009A8C28, uint8) == 0 && (flags & GAME_COMMAND_FLAG_APPLY) && RCT2_GLOBAL(0x0141F568, uint8) == RCT2_GLOBAL(0x013CA740, uint8) && !(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED)) window_error_open(RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TITLE, uint16), RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, uint16)); return MONEY32_UNDEFINED; @@ -714,14 +780,69 @@ int game_load_sv6(SDL_RWops* rw) return 1; } +// Load game state for multiplayer +int game_load_network(SDL_RWops* rw) +{ + int i, j; + + rct_s6_header *s6Header = (rct_s6_header*)0x009E34E4; + rct_s6_info *s6Info = (rct_s6_info*)0x0141F570; + + // Read first chunk + sawyercoding_read_chunk(rw, (uint8*)s6Header); + if (s6Header->type == S6_TYPE_SAVEDGAME) { + // Read packed objects + if (s6Header->num_packed_objects > 0) { + j = 0; + for (i = 0; i < s6Header->num_packed_objects; i++) + j += object_load_packed(rw); + if (j > 0) + object_list_load(); + } + } + + uint8 load_success = object_read_and_load_entries(rw); + + // Read flags (16 bytes) + sawyercoding_read_chunk(rw, (uint8*)RCT2_ADDRESS_CURRENT_MONTH_YEAR); + + // Read map elements + memset((void*)RCT2_ADDRESS_MAP_ELEMENTS, 0, MAX_MAP_ELEMENTS * sizeof(rct_map_element)); + sawyercoding_read_chunk(rw, (uint8*)RCT2_ADDRESS_MAP_ELEMENTS); + + // Read game data, including sprites + sawyercoding_read_chunk(rw, (uint8*)0x010E63B8); + + // Read checksum + uint32 checksum; + SDL_RWread(rw, &checksum, sizeof(uint32), 1); + + if (!load_success){ + set_load_objects_fail_reason(); + if (RCT2_GLOBAL(RCT2_ADDRESS_INPUT_FLAGS, uint32) & INPUT_FLAG_5){ + RCT2_GLOBAL(0x14241BC, uint32) = 2; + //call 0x0040705E Sets cursor position and something else. Calls maybe wind func 8 probably pointless + RCT2_GLOBAL(0x14241BC, uint32) = 0; + RCT2_GLOBAL(RCT2_ADDRESS_INPUT_FLAGS, uint32) &= ~INPUT_FLAG_5; + } + + return 0;//This never gets called + } + + // The rest is the same as in scenario load and play + reset_loaded_objects(); + map_update_tile_pointers(); + reset_0x69EBE4(); + openrct2_reset_object_tween_locations(); + return 1; +} + /** * * rct2: 0x00675E1B */ int game_load_save(const char *path) { - rct_window *mainWindow; - log_verbose("loading saved game, %s", path); strcpy((char*)0x0141EF68, path); @@ -746,6 +867,17 @@ int game_load_save(const char *path) } SDL_RWclose(rw); + game_load_init(); + if (network_get_mode() == NETWORK_MODE_SERVER) { + network_send_map(); + } + return 1; +} + +void game_load_init() +{ + rct_window *mainWindow; + RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) = SCREEN_FLAGS_PLAYING; viewport_init_all(); game_create_windows(); @@ -780,11 +912,11 @@ int game_load_save(const char *path) load_palette(); gfx_invalidate_screen(); + window_update_all(); gGameSpeed = 1; scenario_set_filename((char*)0x0135936C); - return 1; } /* diff --git a/src/game.h b/src/game.h index fa9dbf8120..be95fcd879 100644 --- a/src/game.h +++ b/src/game.h @@ -86,11 +86,27 @@ enum GAME_COMMAND { GAME_COMMAND_CLEAR_SCENERY }; -// If this flag is set, the command is applied, otherwise only the cost is retrieved -#define GAME_COMMAND_FLAG_APPLY (1 << 0) +enum { + GAME_COMMAND_FLAG_APPLY = (1 << 0), // If this flag is set, the command is applied, otherwise only the cost is retrieved + GAME_COMMAND_FLAG_2 = (1 << 2), + GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED = (1 << 3), // Allow while paused + GAME_COMMAND_FLAG_4 = (1 << 4), + GAME_COMMAND_FLAG_5 = (1 << 5), + GAME_COMMAND_FLAG_GHOST = (1 << 6), + GAME_COMMAND_FLAG_NETWORKED = (1 << 31) // Game command is coming from network +}; + + + typedef void (GAME_COMMAND_POINTER)(int* eax, int* ebx, int* ecx, int* edx, int* esi, int* edi, int* ebp); +typedef void (GAME_COMMAND_CALLBACK_POINTER)(int eax, int ebx, int ecx, int edx, int esi, int edi, int ebp); + +extern GAME_COMMAND_CALLBACK_POINTER* game_command_callback; +int game_command_callback_get_index(GAME_COMMAND_CALLBACK_POINTER* callback); +GAME_COMMAND_CALLBACK_POINTER* game_command_callback_get_callback(int index); + extern int gGameSpeed; extern float gDayNightCycle; @@ -111,7 +127,9 @@ void game_reduce_game_speed(); void game_load_or_quit_no_save_prompt(); int game_load_sv6(SDL_RWops* rw); +int game_load_network(SDL_RWops* rw); int game_load_save(const char *path); +void game_load_init(); void game_pause_toggle(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); void pause_toggle(); int save_game(); diff --git a/src/input.c b/src/input.c index 792ccbbab7..fd7c50010b 100644 --- a/src/input.c +++ b/src/input.c @@ -25,6 +25,7 @@ #include "cursors.h" #include "game.h" #include "input.h" +#include "interface/chat.h" #include "interface/console.h" #include "interface/keyboard_shortcut.h" #include "interface/viewport.h" @@ -1344,6 +1345,9 @@ void game_handle_keyboard_input() } else if (gConsoleOpen) { console_input(key); continue; + } else if (gChatOpen) { + chat_input(key); + continue; } key |= RCT2_GLOBAL(RCT2_ADDRESS_PLACE_OBJECT_MODIFIER, uint8) << 8; diff --git a/src/interface/chat.c b/src/interface/chat.c new file mode 100644 index 0000000000..863327ddcc --- /dev/null +++ b/src/interface/chat.c @@ -0,0 +1,134 @@ +#include "../addresses.h" +#include "../audio/audio.h" +#include "../audio/mixer.h" +#include "../localisation/localisation.h" +#include "../network/network.h" +#include "../platform/platform.h" +#include "chat.h" + +#define CHAT_HISTORY_SIZE 10 +#define CHAT_INPUT_SIZE 256 + +bool gChatOpen = false; +char _chatCurrentLine[CHAT_INPUT_SIZE]; +char _chatHistory[CHAT_HISTORY_SIZE][CHAT_INPUT_SIZE]; +uint32 _chatHistoryTime[CHAT_HISTORY_SIZE]; +unsigned int _chatHistoryIndex = 0; +uint32 _chatCaretTicks = 0; +int _chatLeft; +int _chatTop; +int _chatRight; +int _chatBottom; + +static const char* chat_history_get(unsigned int index); +static uint32 chat_history_get_time(unsigned int index); +static void chat_clear_input(); + +void chat_open() +{ + gChatOpen = true; + platform_start_text_input(_chatCurrentLine, sizeof(_chatCurrentLine)); +} + +void chat_close() +{ + gChatOpen = false; + platform_stop_text_input(); +} + +void chat_toggle() +{ + if (gChatOpen) { + chat_close(); + } else { + chat_open(); + } +} + +void chat_init() +{ + memset(_chatHistory, 0, sizeof(_chatHistory)); + memset(_chatHistoryTime, 0, sizeof(_chatHistoryTime)); +} + +void chat_update() +{ + // Flash the caret + _chatCaretTicks = (_chatCaretTicks + 1) % 30; +} + +void chat_draw() +{ + rct_drawpixelinfo *dpi = (rct_drawpixelinfo*)RCT2_ADDRESS_SCREEN_DPI; + _chatLeft = 10; + _chatTop = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, uint16) - 40 - ((CHAT_HISTORY_SIZE + 1) * 10); + _chatRight = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, uint16) - 10; + _chatBottom = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, uint16) - 40; + char lineBuffer[CHAT_INPUT_SIZE + 10]; + char* lineCh = lineBuffer; + int x = _chatLeft; + int y = _chatBottom - (10 * 2); + RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, uint16) = 224; + RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_FLAGS, uint16) = 0; + for (int i = 0; i < CHAT_HISTORY_SIZE; i++, y -= 10) { + if (!gChatOpen && SDL_TICKS_PASSED(SDL_GetTicks(), chat_history_get_time(i) + 10000)) { + break; + } + strcpy(lineBuffer, chat_history_get(i)); + gfx_set_dirty_blocks(x, y, x + gfx_get_string_width(lineBuffer), y + 12); + gfx_draw_string(dpi, lineBuffer, 255, x, y); + } + if (gChatOpen) { + lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE); + lineCh = utf8_write_codepoint(lineCh, FORMAT_CELADON); + strcpy(lineCh, _chatCurrentLine); + y = _chatBottom - 10; + gfx_set_dirty_blocks(x, y, x + gfx_get_string_width(lineBuffer) + 7, y + 12); + if (_chatCaretTicks < 15) { + int caretX = x + gfx_get_string_width(lineBuffer); + int caretY = y + 10; + + gfx_fill_rect(dpi, caretX, caretY, caretX + 6, caretY + 1, 0x38); + } + gfx_draw_string(dpi, lineBuffer, 255, x, y); + } +} + +void chat_history_add(const char *src) +{ + int index = _chatHistoryIndex % CHAT_HISTORY_SIZE; + memset(_chatHistory[index], 0, CHAT_INPUT_SIZE); + memcpy(_chatHistory[index], src, min(strlen(src), CHAT_INPUT_SIZE)); + _chatHistoryTime[index] = SDL_GetTicks(); + _chatHistoryIndex++; + Mixer_Play_Effect(SOUND_NEWS_ITEM, 0, SDL_MIX_MAXVOLUME, 0, 1.5f, true); +} + +void chat_input(int c) +{ + switch (c) { + case SDL_SCANCODE_RETURN: + network_send_chat(_chatCurrentLine); + chat_clear_input(); + chat_close(); + break; + case SDL_SCANCODE_ESCAPE: + chat_close(); + break; + } +} + +static const char* chat_history_get(unsigned int index) +{ + return _chatHistory[(_chatHistoryIndex + CHAT_HISTORY_SIZE - index - 1) % CHAT_HISTORY_SIZE]; +} + +static uint32 chat_history_get_time(unsigned int index) +{ + return _chatHistoryTime[(_chatHistoryIndex + CHAT_HISTORY_SIZE - index - 1) % CHAT_HISTORY_SIZE]; +} + +static void chat_clear_input() +{ + _chatCurrentLine[0] = 0; +} \ No newline at end of file diff --git a/src/interface/chat.h b/src/interface/chat.h new file mode 100644 index 0000000000..b288558b00 --- /dev/null +++ b/src/interface/chat.h @@ -0,0 +1,21 @@ +#ifndef _CHAT_H_ +#define _CHAT_H_ + +#include "../common.h" +#include "../drawing/drawing.h" + +extern bool gChatOpen; + +void chat_open(); +void chat_close(); +void chat_toggle(); + +void chat_init(); +void chat_update(); +void chat_draw(); + +void chat_history_add(const char *src); +void chat_input(int c); + + +#endif \ No newline at end of file diff --git a/src/interface/keyboard_shortcut.c b/src/interface/keyboard_shortcut.c index a810083713..cd5e50903b 100644 --- a/src/interface/keyboard_shortcut.c +++ b/src/interface/keyboard_shortcut.c @@ -21,8 +21,10 @@ #include "../addresses.h" #include "../config.h" #include "../game.h" +#include "../interface/chat.h" #include "../input.h" #include "../localisation/localisation.h" +#include "../network/network.h" #include "keyboard_shortcut.h" #include "viewport.h" #include "window.h" @@ -493,6 +495,11 @@ static void shortcut_open_cheat_window() window_cheats_open(); } +static void shortcut_open_chat_window() +{ + chat_toggle(); +} + static const shortcut_action shortcut_table[SHORTCUT_COUNT] = { shortcut_close_top_most_window, shortcut_close_all_floating_windows, @@ -533,6 +540,7 @@ static const shortcut_action shortcut_table[SHORTCUT_COUNT] = { shortcut_increase_game_speed, shortcut_open_cheat_window, shortcut_remove_top_bottom_toolbar_toggle, + shortcut_open_chat_window, NULL, NULL, NULL, diff --git a/src/interface/window.h b/src/interface/window.h index a942bc1b7c..080c6c201a 100644 --- a/src/interface/window.h +++ b/src/interface/window.h @@ -446,12 +446,14 @@ enum { WC_CHANGELOG = 121, WC_TITLE_EDITOR = 122, WC_TITLE_COMMAND_EDITOR = 123, + WC_CHAT_HOST = 124, + WC_PLAYER_LIST = 125, + WC_NETWORK_STATUS = 126, // Only used for colour schemes WC_STAFF = 220, WC_EDITOR_TRACK_BOTTOM_TOOLBAR = 221, WC_EDITOR_SCENARIO_BOTTOM_TOOLBAR = 222, - } WINDOW_CLASS; enum PROMPT_MODE { @@ -611,6 +613,8 @@ void window_banner_open(rct_windownumber number); void window_sign_open(rct_windownumber number); void window_sign_small_open(rct_windownumber number); void window_cheats_open(); +void window_player_list_open(); +void window_network_status_open(const char* text); void window_research_open(); void window_research_development_page_paint(rct_window *w, rct_drawpixelinfo *dpi, int baseWidgetIndex); diff --git a/src/localisation/date.c b/src/localisation/date.c index 8312f5d798..d0af9956b0 100644 --- a/src/localisation/date.c +++ b/src/localisation/date.c @@ -48,5 +48,5 @@ void date_reset() { RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_YEAR, sint16) = MONTH_MARCH; RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_MONTH_TICKS, sint16) = 0; - RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32) = 0; + RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) = 0; } diff --git a/src/localisation/localisation.c b/src/localisation/localisation.c index 82ea9f0f54..141ce8805d 100644 --- a/src/localisation/localisation.c +++ b/src/localisation/localisation.c @@ -18,7 +18,6 @@ * along with this program. If not, see . *****************************************************************************/ -#include #include "../addresses.h" #include "../config.h" #include "../game.h" diff --git a/src/localisation/string_ids.h b/src/localisation/string_ids.h index 5ae91dcca2..1ec59b969b 100644 --- a/src/localisation/string_ids.h +++ b/src/localisation/string_ids.h @@ -2030,6 +2030,10 @@ enum { STR_DEBUG_DROPDOWN_INVENTIONS_LIST = 5491, STR_DEBUG_DROPDOWN_SCENARIO_OPTIONS = 5492, + STR_PLAYER_LIST = 6002, + STR_PLAYER = 6003, + STR_PING = 6004, + // Have to include resource strings (from scenarios and objects) for the time being now that language is partially working STR_COUNT = 32768 }; diff --git a/src/management/news_item.c b/src/management/news_item.c index d077920471..ee8bb0c151 100644 --- a/src/management/news_item.c +++ b/src/management/news_item.c @@ -434,3 +434,20 @@ void news_item_disable_news(uint8 type, uint32 assoc) } } +void news_item_add_to_queue_custom(rct_news_item *newNewsItem) +{ + int i = 0; + rct_news_item *newsItem = RCT2_ADDRESS(RCT2_ADDRESS_NEWS_ITEM_LIST, rct_news_item); + + // Find first open slot + while (newsItem->type != NEWS_ITEM_NULL) { + if (newsItem + 1 >= (rct_news_item*)0x013CB1CC) + news_item_close_current(); + else + newsItem++; + } + + *newsItem = *newNewsItem; + newsItem++; + newsItem->type = 0; +} diff --git a/src/management/news_item.h b/src/management/news_item.h index 1b37077606..a23be1c19e 100644 --- a/src/management/news_item.h +++ b/src/management/news_item.h @@ -66,4 +66,6 @@ bool news_item_is_empty(int index); bool news_item_is_queue_empty(); bool news_item_is_valid_idx(int index); +void news_item_add_to_queue_custom(rct_news_item *newNewsItem); + #endif diff --git a/src/network/network.cpp b/src/network/network.cpp new file mode 100644 index 0000000000..68b7e13772 --- /dev/null +++ b/src/network/network.cpp @@ -0,0 +1,1007 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ + +#ifndef DISABLE_NETWORK + +#include +#include +#include +#include "network.h" +extern "C" { +#include "../addresses.h" +#include "../game.h" +#include "../interface/chat.h" +#include "../interface/window.h" +#include "../localisation/date.h" +#include "../localisation/localisation.h" +#include "../scenario.h" +} + +#pragma comment(lib, "Ws2_32.lib") + +Network gNetwork; + +enum { + NETWORK_SUCCESS, + NETWORK_NO_DATA, + NETWORK_MORE_DATA, + NETWORK_DISCONNECTED +}; + +enum { + NETWORK_COMMAND_AUTH, + NETWORK_COMMAND_MAP, + NETWORK_COMMAND_CHAT, + NETWORK_COMMAND_GAMECMD, + NETWORK_COMMAND_TICK, + NETWORK_COMMAND_PLAYERLIST, + NETWORK_COMMAND_PING, + NETWORK_COMMAND_PINGLIST, + NETWORK_COMMAND_MAX +}; + +NetworkPacket::NetworkPacket() +{ + transferred = 0; + read = 0; + size = 0; + data = std::make_shared>(); +} + +std::unique_ptr NetworkPacket::Allocate() +{ + return std::unique_ptr(new NetworkPacket); // change to make_unique in c++14 +} + +std::unique_ptr NetworkPacket::Duplicate(NetworkPacket& packet) +{ + return std::unique_ptr(new NetworkPacket(packet)); // change to make_unique in c++14 +} + +uint8* NetworkPacket::GetData() +{ + return &(*data)[0]; +} + +void NetworkPacket::Write(uint8* bytes, unsigned int size) +{ + data->insert(data->end(), bytes, bytes + size); +} + +void NetworkPacket::WriteString(const char* string) +{ + Write((uint8*)string, strlen(string) + 1); +} + +const uint8* NetworkPacket::Read(unsigned int size) +{ + if (read + size > NetworkPacket::size) { + return 0; + } else { + uint8* data = &GetData()[read]; + read += size; + return data; + } +} + +const char* NetworkPacket::ReadString() +{ + char* str = (char*)&GetData()[read]; + char* strend = str; + while (read < size && *strend != 0) { + read++; + strend++; + } + if (*strend != 0) { + return 0; + } + read++; + return str; +} + +void NetworkPacket::Clear() +{ + transferred = 0; + read = 0; + data->clear(); +} + +NetworkPlayer::NetworkPlayer(const char* name) +{ + strncpy((char*)NetworkPlayer::name, name, sizeof(NetworkPlayer::name)); + NetworkPlayer::name[sizeof(NetworkPlayer::name) - 1] = 0; + ping = 0; + flags = 0; +} + +NetworkConnection::NetworkConnection() +{ + authstatus = NETWORK_AUTH_NONE; + player = 0; +} + +int NetworkConnection::ReadPacket() +{ + if (inboundpacket.transferred < sizeof(inboundpacket.size)) { + // read packet size + int readBytes = recv(socket, &((char*)&inboundpacket.size)[inboundpacket.transferred], sizeof(inboundpacket.size) - inboundpacket.transferred, 0); + if (readBytes == SOCKET_ERROR || readBytes == 0) { + if (WSAGetLastError() != WSAEWOULDBLOCK) { + return NETWORK_DISCONNECTED; + } else { + return NETWORK_NO_DATA; + } + } + inboundpacket.transferred += readBytes; + if (inboundpacket.transferred == sizeof(inboundpacket.size)) { + inboundpacket.size = ntohs(inboundpacket.size); + inboundpacket.data->resize(inboundpacket.size); + } + } else { + // read packet data + if (inboundpacket.data->capacity() > 0) { + int readBytes = recv(socket, (char*)&inboundpacket.GetData()[inboundpacket.transferred - sizeof(inboundpacket.size)], sizeof(inboundpacket.size) + inboundpacket.size - inboundpacket.transferred, 0); + if (readBytes == SOCKET_ERROR || readBytes == 0) { + if (WSAGetLastError() != WSAEWOULDBLOCK) { + return NETWORK_DISCONNECTED; + } else { + return NETWORK_NO_DATA; + } + } + inboundpacket.transferred += readBytes; + } + if (inboundpacket.transferred == sizeof(inboundpacket.size) + inboundpacket.size) { + return NETWORK_SUCCESS; + } + } + return NETWORK_MORE_DATA; +} + +int NetworkConnection::SendPacket(NetworkPacket& packet) +{ + while (1) { + if (packet.transferred < sizeof(packet.size)) { + // send packet size + uint16 size = htons(packet.size); + int sentBytes = send(socket, &((char*)&size)[packet.transferred], sizeof(size) - packet.transferred, 0); + if (sentBytes == SOCKET_ERROR) { + return 0; + } + packet.transferred += sentBytes; + } else { + // send packet data + int sentBytes = send(socket, (const char*)&packet.GetData()[packet.transferred - sizeof(packet.size)], packet.data->size(), 0); + if (sentBytes == SOCKET_ERROR) { + return 0; + } + packet.transferred += sentBytes; + if (packet.transferred == sizeof(packet.size) + packet.data->size()) { + return 1; + } + } + } + return 0; +} + +void NetworkConnection::QueuePacket(std::unique_ptr packet) +{ + if (authstatus == NETWORK_AUTH_OK || authstatus == NETWORK_AUTH_REQUESTED) { + packet->size = (uint16)packet->data->size(); + outboundpackets.push_back(std::move(packet)); + } +} + +void NetworkConnection::SendQueuedPackets() +{ + while (outboundpackets.size() > 0 && SendPacket(*(outboundpackets.front()).get())) { + outboundpackets.remove(outboundpackets.front()); + } +} + +Network::Network() +{ + wsa_initialized = false; + mode = NETWORK_MODE_NONE; + last_tick_sent_time = 0; + last_ping_sent_time = 0; + strcpy(password, ""); + client_command_handlers.resize(NETWORK_COMMAND_MAX, 0); + client_command_handlers[NETWORK_COMMAND_AUTH] = &Network::Client_Handle_AUTH; + client_command_handlers[NETWORK_COMMAND_MAP] = &Network::Client_Handle_MAP; + client_command_handlers[NETWORK_COMMAND_CHAT] = &Network::Client_Handle_CHAT; + client_command_handlers[NETWORK_COMMAND_GAMECMD] = &Network::Client_Handle_GAMECMD; + client_command_handlers[NETWORK_COMMAND_TICK] = &Network::Client_Handle_TICK; + client_command_handlers[NETWORK_COMMAND_PLAYERLIST] = &Network::Client_Handle_PLAYERLIST; + client_command_handlers[NETWORK_COMMAND_PING] = &Network::Client_Handle_PING; + client_command_handlers[NETWORK_COMMAND_PINGLIST] = &Network::Client_Handle_PINGLIST; + server_command_handlers.resize(NETWORK_COMMAND_MAX, 0); + server_command_handlers[NETWORK_COMMAND_AUTH] = &Network::Server_Handle_AUTH; + server_command_handlers[NETWORK_COMMAND_CHAT] = &Network::Server_Handle_CHAT; + server_command_handlers[NETWORK_COMMAND_GAMECMD] = &Network::Server_Handle_GAMECMD; + server_command_handlers[NETWORK_COMMAND_PING] = &Network::Server_Handle_PING; +} + +Network::~Network() +{ + Close(); +} + +bool Network::Init() +{ + if (!wsa_initialized) { + log_verbose("Initialising WSA"); + WSADATA wsa_data; + if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0) { + log_error("Unable to initialise winsock."); + return false; + } + wsa_initialized = true; + } + return true; +} + +void Network::Close() +{ + if (mode == NETWORK_MODE_CLIENT) { + closesocket(server_socket); + } else + if (mode == NETWORK_MODE_SERVER) { + closesocket(listening_socket); + for(auto it = client_connection_list.begin(); it != client_connection_list.end(); it++) { + closesocket((*it)->socket); + } + } + + mode = NETWORK_MODE_NONE; + + client_connection_list.clear(); + game_command_queue.clear(); + player_list.clear(); + + if (wsa_initialized) { + WSACleanup(); + wsa_initialized = false; + } +} + +bool Network::BeginClient(const char* host, unsigned short port) +{ + Close(); + if (!Init()) + return false; + + server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (server_socket == INVALID_SOCKET) { + log_error("Unable to create socket."); + return 0; + } + + SOCKADDR_IN server_address; + server_address.sin_family = AF_INET; + server_address.sin_addr.S_un.S_addr = inet_addr(host); + server_address.sin_port = htons(port); + + if (connect(server_socket, (SOCKADDR*)&server_address, sizeof(SOCKADDR_IN)) != 0) { + log_error("Unable to connect to host."); + return false; + } else { + printf("Connected to server!\n"); + } + + u_long nonblocking = 1; + if (ioctlsocket(server_socket, FIONBIO, &nonblocking) != NO_ERROR) { + closesocket(server_socket); + log_error("Failed to set non-blocking mode."); + } + + server_connection.socket = server_socket; + + mode = NETWORK_MODE_CLIENT; + + Client_Send_AUTH(OPENRCT2_VERSION, "Player", ""); + return true; +} + +bool Network::BeginServer(unsigned short port) +{ + Close(); + if (!Init()) + return false; + + log_verbose("Begin listening for clients"); + listening_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listening_socket == INVALID_SOCKET) { + log_error("Unable to create socket."); + return false; + } + + SOCKADDR_IN local_address; + local_address.sin_family = AF_INET; + local_address.sin_addr.S_un.S_addr = INADDR_ANY; + local_address.sin_port = htons(port); + + if (bind(listening_socket, (SOCKADDR*)&local_address, sizeof(SOCKADDR_IN)) != 0) { + closesocket(listening_socket); + log_error("Unable to bind to socket."); + return false; + } + + if (listen(listening_socket, SOMAXCONN) != 0) { + closesocket(listening_socket); + log_error("Unable to listen on socket."); + return false; + } + + u_long nonblocking = 1; + if (ioctlsocket(listening_socket, FIONBIO, &nonblocking) != NO_ERROR) { + closesocket(listening_socket); + log_error("Failed to set non-blocking mode."); + return false; + } + + NetworkPlayer* player = AddPlayer("Server Player"); + player->flags |= NETWORK_PLAYER_FLAG_ISSERVER; + player_id = player->id; + + printf("Ready for clients...\n"); + + mode = NETWORK_MODE_SERVER; + return true; +} + +int Network::GetMode() +{ + return mode; +} + +int Network::GetAuthStatus() +{ + if (GetMode() == NETWORK_MODE_CLIENT) { + return server_connection.authstatus; + } else + if (GetMode() == NETWORK_MODE_SERVER) { + return NETWORK_AUTH_OK; + } + return NETWORK_AUTH_NONE; +} + +uint32 Network::GetServerTick() +{ + return server_tick; +} + +uint8 Network::GetPlayerID() +{ + return player_id; +} + +void Network::Update() +{ + if (GetMode() == NETWORK_MODE_NONE) + return; + + if (GetMode() == NETWORK_MODE_CLIENT) { + if (!ProcessConnection(server_connection)) { + Close(); + } else { + ProcessGameCommandQueue(); + } + } else + if (GetMode() == NETWORK_MODE_SERVER) { + auto it = client_connection_list.begin(); + while (it != client_connection_list.end()) { + if (!ProcessConnection(*(*it))) { + RemoveClient((*it)); + it = client_connection_list.begin(); + } else { + it++; + } + } + if (SDL_GetTicks() - last_tick_sent_time >= 25) { + last_tick_sent_time = SDL_GetTicks(); + Server_Send_TICK(); + } + if (SDL_GetTicks() - last_ping_sent_time >= 3000) { + last_ping_sent_time = SDL_GetTicks(); + Server_Send_PING(); + Server_Send_PINGLIST(); + } + SOCKET socket = accept(listening_socket, NULL, NULL); + if (socket == INVALID_SOCKET) { + if (WSAGetLastError() != WSAEWOULDBLOCK) { + PrintError(); + log_error("Failed to accept client."); + } + } else { + u_long nonblocking = 1; + if (ioctlsocket(socket, FIONBIO, &nonblocking) != NO_ERROR) { + closesocket(socket); + log_error("Failed to set non-blocking mode."); + } else { + AddClient(socket); + } + } + } +} + +NetworkPlayer* Network::GetPlayerByID(int id) { + auto it = std::find_if(player_list.begin(), player_list.end(), [&id](std::unique_ptr const& player) { return player->id == id; }); + if (it != player_list.end()) { + return (*it).get(); + } + return 0; +} + +const char* Network::FormatChat(NetworkPlayer* fromplayer, const char* text) +{ + static char formatted[1024]; + char* lineCh = formatted; + formatted[0] = 0; + if (fromplayer) { + lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE); + lineCh = utf8_write_codepoint(lineCh, FORMAT_BABYBLUE); + strcpy(lineCh, (const char*)fromplayer->name); + strcat(lineCh, ": "); + } + strcat(formatted, text); + return formatted; +} + +void Network::SendPacketToClients(NetworkPacket& packet) +{ + for (auto it = client_connection_list.begin(); it != client_connection_list.end(); it++) { + (*it)->QueuePacket(std::move(NetworkPacket::Duplicate(packet))); + } +} + +bool Network::CheckSRAND(uint32 tick, uint32 srand0) +{ + if (tick == server_srand0_tick) { + server_srand0_tick = 0; + if (srand0 != server_srand0) { + return false; + } + } + return true; +} + +void Network::Client_Send_AUTH(const char* gameversion, const char* name, const char* password) +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_AUTH; + packet->WriteString(gameversion); + packet->WriteString(name); + packet->WriteString(password); + server_connection.authstatus = NETWORK_AUTH_REQUESTED; + server_connection.QueuePacket(std::move(packet)); +} + +void Network::Server_Send_MAP(NetworkConnection* connection) +{ + int buffersize = 0x600000; + std::vector buffer(buffersize); + SDL_RWops* rw = SDL_RWFromMem(&buffer[0], buffersize); + scenario_save_network(rw); + int size = (int)SDL_RWtell(rw); + int chunksize = 1000; + for (int i = 0; i < size; i += chunksize) { + int datasize = min(chunksize, size - i); + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_MAP << (uint32)size << (uint32)i; + packet->Write(&buffer[i], datasize); + if (connection) { + connection->QueuePacket(std::move(packet)); + } else { + SendPacketToClients(*packet); + } + } + SDL_RWclose(rw); +} + +void Network::Client_Send_CHAT(const char* text) +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_CHAT; + packet->Write((uint8*)text, strlen(text) + 1); + server_connection.QueuePacket(std::move(packet)); +} + +void Network::Server_Send_CHAT(const char* text) +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_CHAT; + packet->Write((uint8*)text, strlen(text) + 1); + SendPacketToClients(*packet); +} + +void Network::Client_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback) +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_GAMECMD << (uint32)RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) << eax << (ebx | GAME_COMMAND_FLAG_NETWORKED) << ecx << edx << esi << edi << ebp << callback; + server_connection.QueuePacket(std::move(packet)); +} + +void Network::Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 playerid, uint8 callback) +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_GAMECMD << (uint32)RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) << eax << (ebx | GAME_COMMAND_FLAG_NETWORKED) << ecx << edx << esi << edi << ebp << playerid << callback; + SendPacketToClients(*packet); +} + +void Network::Server_Send_TICK() +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_TICK << (uint32)RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) << (uint32)RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_0, uint32); + SendPacketToClients(*packet); +} + +void Network::Server_Send_PLAYERLIST() +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_PLAYERLIST << (uint32)player_list.size(); + for (unsigned int i = 0; i < player_list.size(); i++) { + packet->WriteString((const char*)player_list[i]->name); + *packet << player_list[i]->id << player_list[i]->flags; + } + SendPacketToClients(*packet); +} + +void Network::Client_Send_PING() +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_PING; + server_connection.QueuePacket(std::move(packet)); +} + +void Network::Server_Send_PING() +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_PING; + for (auto it = client_connection_list.begin(); it != client_connection_list.end(); it++) { + (*it)->ping_time = SDL_GetTicks(); + } + SendPacketToClients(*packet); +} + +void Network::Server_Send_PINGLIST() +{ + std::unique_ptr packet = std::move(NetworkPacket::Allocate()); + *packet << (uint32)NETWORK_COMMAND_PINGLIST << (uint32)player_list.size(); + for (unsigned int i = 0; i < player_list.size(); i++) { + *packet << player_list[i]->id << player_list[i]->ping; + } + SendPacketToClients(*packet); +} + +bool Network::ProcessConnection(NetworkConnection& connection) +{ + int packetStatus; + do { + packetStatus = connection.ReadPacket(); + switch(packetStatus) { + case NETWORK_DISCONNECTED: + // closed connection or network error + PrintError(); + if (GetMode() == NETWORK_MODE_CLIENT) { + printf("Server disconnected...\n"); + return false; + } else + if (GetMode() == NETWORK_MODE_SERVER) { + printf("Client disconnected...\n"); + return false; + } + break; + case NETWORK_SUCCESS: + // done reading in packet + ProcessPacket(connection, connection.inboundpacket); + break; + case NETWORK_MORE_DATA: + // more data required to be read + break; + case NETWORK_NO_DATA: + // could not read anything from socket + break; + } + } while (packetStatus == NETWORK_MORE_DATA || packetStatus == NETWORK_SUCCESS); + connection.SendQueuedPackets(); + return true; +} + +void Network::ProcessPacket(NetworkConnection& connection, NetworkPacket& packet) +{ + uint32 command; + packet >> command; + if (command < NETWORK_COMMAND_MAX) { + if (GetMode() == NETWORK_MODE_CLIENT) { + if (client_command_handlers[command]) { + (this->*client_command_handlers[command])(connection, packet); + } + } else + if (GetMode() == NETWORK_MODE_SERVER) { + if (server_command_handlers[command]) { + if (connection.authstatus == NETWORK_AUTH_OK || command == NETWORK_COMMAND_AUTH) { + (this->*server_command_handlers[command])(connection, packet); + } + } + } + } + packet.Clear(); +} + +void Network::ProcessGameCommandQueue() +{ + while (game_command_queue.begin() != game_command_queue.end() && game_command_queue.begin()->tick == RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32)) { + // run all the game commands at the current tick + const GameCommand& gc = (*game_command_queue.begin()); + if (GetPlayerID() == gc.playerid) { + game_command_callback = game_command_callback_get_callback(gc.callback); + } + game_do_command_p(gc.esi, (int*)&gc.eax, (int*)&gc.ebx, (int*)&gc.ecx, (int*)&gc.edx, (int*)&gc.esi, (int*)&gc.edi, (int*)&gc.ebp); + game_command_queue.erase(game_command_queue.begin()); + } +} + +void Network::AddClient(SOCKET socket) +{ + auto connection = std::unique_ptr(new NetworkConnection); // change to make_unique in c++14 + connection->socket = socket; + client_connection_list.push_back(std::move(connection)); +} + +void Network::RemoveClient(std::unique_ptr& connection) +{ + NetworkPlayer* connection_player = connection->player; + if (connection_player) { + char text[256]; + char* lineCh = text; + lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE); + lineCh = utf8_write_codepoint(lineCh, FORMAT_RED); + sprintf(lineCh, "%s has disconnected", connection_player->name); + chat_history_add(text); + gNetwork.Server_Send_CHAT(text); + } + player_list.erase(std::remove_if(player_list.begin(), player_list.end(), [connection_player](std::unique_ptr& player){ return player.get() == connection_player; }), player_list.end()); + client_connection_list.remove(connection); + Server_Send_PLAYERLIST(); +} + +NetworkPlayer* Network::AddPlayer(const char* name) +{ + NetworkPlayer* addedplayer = 0; + int newid = -1; + if (GetMode() == NETWORK_MODE_SERVER) { + // Find first unused player id + for (int id = 0; id < 255; id++) { + if (std::find_if(player_list.begin(), player_list.end(), [&id](std::unique_ptr const& player) { return player->id == id; }) == player_list.end()) { + newid = id; + break; + } + } + } else { + newid = 0; + } + if (newid != -1) { + std::unique_ptr player(new NetworkPlayer(name)); // change to make_unique in c++14 + player->id = newid; + addedplayer = player.get(); + player_list.push_back(std::move(player)); + if (GetMode() == NETWORK_MODE_SERVER) { + Server_Send_PLAYERLIST(); + } + } + return addedplayer; +} + +void Network::PrintError() +{ + wchar_t *s = NULL; + FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, WSAGetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&s, 0, NULL); + fprintf(stderr, "%S\n", s); + LocalFree(s); +} + +int Network::Client_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet) +{ + packet >> (uint32&)connection.authstatus >> (uint8&)player_id; + return 1; +} + +int Network::Server_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet) +{ + if (connection.authstatus != NETWORK_AUTH_OK) { + const char* gameversion = packet.ReadString(); + const char* name = packet.ReadString(); + const char* password = packet.ReadString(); + uint8 playerid = 0; + if (!gameversion || strcmp(gameversion, OPENRCT2_VERSION) != 0) { + connection.authstatus = NETWORK_AUTH_BADVERSION; + } else + if (!name) { + connection.authstatus = NETWORK_AUTH_BADNAME; + } else + if (!password || strcmp(password, Network::password) != 0) { + connection.authstatus = NETWORK_AUTH_BADPASSWORD; + } else { + connection.authstatus = NETWORK_AUTH_OK; + NetworkPlayer* player = AddPlayer(name); + connection.player = player; + if (player) { + playerid = player->id; + char text[256]; + char* lineCh = text; + lineCh = utf8_write_codepoint(lineCh, FORMAT_OUTLINE); + lineCh = utf8_write_codepoint(lineCh, FORMAT_GREEN); + sprintf(lineCh, "%s has joined the game", player->name); + chat_history_add(text); + gNetwork.Server_Send_CHAT(text); + Server_Send_MAP(&connection); + } + } + std::unique_ptr responsepacket = std::move(NetworkPacket::Allocate()); + *responsepacket << (uint32)NETWORK_COMMAND_AUTH << (uint32)connection.authstatus << (uint8)playerid; + connection.QueuePacket(std::move(responsepacket)); + } + return 1; +} + +int Network::Client_Handle_MAP(NetworkConnection& connection, NetworkPacket& packet) +{ + uint32 size, offset; + packet >> size >> offset; + if (offset > 0x600000) { + // too big + return 0; + } else { + int chunksize = packet.size - packet.read; + if (chunksize <= 0) { + return 0; + } + if (offset + chunksize > chunk_buffer.size()) { + chunk_buffer.resize(offset + chunksize); + } + char status[256]; + sprintf(status, "Downloading map ... (%d / %d)", (offset + chunksize) / 1000, size / 1000); + window_network_status_open(status); + memcpy(&chunk_buffer[offset], (void*)packet.Read(chunksize), chunksize); + if (offset + chunksize == size) { + SDL_RWops* rw = SDL_RWFromMem(&chunk_buffer[0], size); + if (game_load_network(rw)) { + game_load_init(); + if (RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) & 1) + pause_toggle(); + game_command_queue.clear(); + server_tick = RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32); + server_srand0_tick = 0; + window_network_status_open("Loaded new map from network"); + } + SDL_RWclose(rw); + } + } + return 1; +} + +int Network::Client_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet) +{ + const char* text = (char*)packet.Read(packet.size - packet.read); + if (text) { + chat_history_add(text); + } + return 1; +} + +int Network::Server_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet) +{ + const char* text = (const char*)packet.Read(packet.size - packet.read); + if (text) { + const char* formatted = FormatChat(connection.player, text); + chat_history_add(formatted); + Server_Send_CHAT(formatted); + } + return 1; +} + +int Network::Client_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket& packet) +{ + uint32 tick; + uint32 args[7]; + uint8 playerid; + uint8 callback; + packet >> tick >> args[0] >> args[1] >> args[2] >> args[3] >> args[4] >> args[5] >> args[6] >> playerid >> callback; + GameCommand gc = GameCommand(tick, args, playerid, callback); + game_command_queue.insert(gc); + return 1; +} + +int Network::Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket& packet) +{ + uint32 tick; + uint32 args[7]; + uint8 playerid; + uint8 callback; + if (connection.player) { + playerid = connection.player->id; + } + packet >> tick >> args[0] >> args[1] >> args[2] >> args[3] >> args[4] >> args[5] >> args[6] >> callback; + Server_Send_GAMECMD(args[0], args[1], args[2], args[3], args[4], args[5], args[6], playerid, callback); + game_do_command(args[0], args[1], args[2], args[3], args[4], args[5], args[6]); + return 1; +} + +int Network::Client_Handle_TICK(NetworkConnection& connection, NetworkPacket& packet) +{ + uint32 srand0; + packet >> server_tick >> srand0; + if (server_srand0_tick == 0) { + server_srand0 = srand0; + server_srand0_tick = server_tick; + } + return 1; +} + +int Network::Client_Handle_PLAYERLIST(NetworkConnection& connection, NetworkPacket& packet) +{ + player_list.clear(); + uint32 size; + packet >> size; + for (unsigned int i = 0; i < size; i++) { + const char* name = packet.ReadString(); + NetworkPlayer* player = AddPlayer(name); + if (player) { + packet >> player->id >> player->flags; + if (player->flags & NETWORK_PLAYER_FLAG_ISSERVER) { + server_connection.player = player; + } + } + } + return 1; +} + +int Network::Client_Handle_PING(NetworkConnection& connection, NetworkPacket& packet) +{ + Client_Send_PING(); + return 1; +} + +int Network::Server_Handle_PING(NetworkConnection& connection, NetworkPacket& packet) +{ + int ping = SDL_GetTicks() - connection.ping_time; + if (ping < 0) { + ping = 0; + } + if (connection.player) { + connection.player->ping = ping; + } + return 1; +} + +int Network::Client_Handle_PINGLIST(NetworkConnection& connection, NetworkPacket& packet) +{ + uint32 size; + packet >> size; + for (unsigned int i = 0; i < size; i++) { + uint8 id; + uint16 ping; + packet >> id >> ping; + NetworkPlayer* player = GetPlayerByID(id); + if (player) { + player->ping = ping; + } + } + return 1; +} + +int network_init() +{ + return gNetwork.Init(); +} + +void network_close() +{ + gNetwork.Close(); +} + +int network_begin_client(const char *host, int port) +{ + return gNetwork.BeginClient(host, port); +} + +int network_begin_server(int port) +{ + return gNetwork.BeginServer(port); +} + +void network_update() +{ + gNetwork.Update(); + if (!gNetwork.CheckSRAND(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32), RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_SRAND_0, uint32))) { + window_network_status_open("Network desync detected"); + gNetwork.Close(); + } +} + +int network_get_mode() +{ + return gNetwork.GetMode(); +} + +int network_get_authstatus() +{ + return gNetwork.GetAuthStatus(); +} + +uint32 network_get_server_tick() +{ + return gNetwork.GetServerTick(); +} + +uint8 network_get_player_id() +{ + return gNetwork.GetPlayerID(); +} + +int network_get_num_players() +{ + return gNetwork.player_list.size(); +} + +const char* network_get_player_name(unsigned int index) +{ + return (const char*)gNetwork.player_list[index]->name; +} + +uint32 network_get_player_flags(unsigned int index) +{ + return gNetwork.player_list[index]->flags; +} + +int network_get_player_ping(unsigned int index) +{ + return gNetwork.player_list[index]->ping; +} + +void network_send_map() +{ + gNetwork.Server_Send_MAP(); +} + +void network_send_chat(const char* text) +{ + if (gNetwork.GetMode() == NETWORK_MODE_CLIENT) { + gNetwork.Client_Send_CHAT(text); + } else + if (gNetwork.GetMode() == NETWORK_MODE_SERVER) { + NetworkPlayer* player = gNetwork.GetPlayerByID(gNetwork.GetPlayerID()); + const char* formatted = gNetwork.FormatChat(player, text); + chat_history_add(formatted); + gNetwork.Server_Send_CHAT(formatted); + } +} + +void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback) +{ + if (gNetwork.GetMode() == NETWORK_MODE_CLIENT) { + gNetwork.Client_Send_GAMECMD(eax, ebx, ecx, edx, esi, edi, ebp, callback); + } else + if (gNetwork.GetMode() == NETWORK_MODE_SERVER) { + gNetwork.Server_Send_GAMECMD(eax, ebx, ecx, edx, esi, edi, ebp, gNetwork.GetPlayerID(), callback); + } +} + +#endif /* DISABLE_NETWORK */ \ No newline at end of file diff --git a/src/network/network.h b/src/network/network.h new file mode 100644 index 0000000000..121ddf5b24 --- /dev/null +++ b/src/network/network.h @@ -0,0 +1,238 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ + +#ifndef _NETWORK_H_ +#define _NETWORK_H_ + +#ifndef DISABLE_NETWORK + +#define NETWORK_DEFAULT_PORT 11753 + +enum { + NETWORK_MODE_NONE, + NETWORK_MODE_CLIENT, + NETWORK_MODE_SERVER +}; + +enum { + NETWORK_AUTH_NONE, + NETWORK_AUTH_REQUESTED, + NETWORK_AUTH_OK, + NETWORK_AUTH_BADVERSION, + NETWORK_AUTH_BADNAME, + NETWORK_AUTH_BADPASSWORD +}; + +enum { + NETWORK_PLAYER_FLAG_ISSERVER = 1 << 0, +}; + +#ifdef __cplusplus + +#include +#include +#include +#include +extern "C" { +#include "../common.h" +#include "../platform/platform.h" +} + +template +struct ByteSwapT { }; +template <> +struct ByteSwapT<1> { static uint8 SwapBE(uint8 value) { return value; } }; +template <> +struct ByteSwapT<2> { static uint16 SwapBE(uint16 value) { return SDL_SwapBE16(value); } }; +template <> +struct ByteSwapT<4> { static uint32 SwapBE(uint32 value) { return SDL_SwapBE32(value); } }; +template +T ByteSwapBE(const T& value) { return ByteSwapT::SwapBE(value); } + +class NetworkPacket +{ +public: + NetworkPacket(); + static std::unique_ptr Allocate(); + static std::unique_ptr Duplicate(NetworkPacket& packet); + uint8* GetData(); + template + NetworkPacket& operator<<(T value) { T swapped = ByteSwapBE(value); uint8* bytes = (uint8*)&swapped; data->insert(data->end(), bytes, bytes + sizeof(value)); return *this; } + void Write(uint8* bytes, unsigned int size); + void WriteString(const char* string); + template + NetworkPacket& operator>>(T& value) { if (read + sizeof(value) > size) { value = 0; } else { value = ByteSwapBE(*((T*)&GetData()[read])); read += sizeof(value); } return *this; }; + const uint8* Read(unsigned int size); + const char* ReadString(); + void Clear(); + + uint16 size; + std::shared_ptr> data; + int transferred; + int read; +}; + +class NetworkPlayer +{ +public: + NetworkPlayer(const char* name); + uint8 id; + uint8 name[32 + 1]; + uint16 ping; + uint32 flags; +}; + +class NetworkConnection +{ +public: + NetworkConnection(); + int ReadPacket(); + void QueuePacket(std::unique_ptr packet); + void SendQueuedPackets(); + + SOCKET socket; + NetworkPacket inboundpacket; + int authstatus; + NetworkPlayer* player; + uint32 ping_time; + +private: + int SendPacket(NetworkPacket& packet); + std::list> outboundpackets; +}; + +class Network +{ +public: + Network(); + ~Network(); + bool Init(); + void Close(); + bool BeginClient(const char* host, unsigned short port); + bool BeginServer(unsigned short port); + int GetMode(); + int GetAuthStatus(); + uint32 GetServerTick(); + uint8 GetPlayerID(); + void Update(); + NetworkPlayer* GetPlayerByID(int id); + const char* FormatChat(NetworkPlayer* fromplayer, const char* text); + void SendPacketToClients(NetworkPacket& packet); + bool CheckSRAND(uint32 tick, uint32 srand0); + + void Client_Send_AUTH(const char* gameversion, const char* name, const char* password); + void Server_Send_MAP(NetworkConnection* connection = nullptr); + void Client_Send_CHAT(const char* text); + void Server_Send_CHAT(const char* text); + void Client_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback); + void Server_Send_GAMECMD(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 playerid, uint8 callback); + void Server_Send_TICK(); + void Server_Send_PLAYERLIST(); + void Client_Send_PING(); + void Server_Send_PING(); + void Server_Send_PINGLIST(); + + std::vector> player_list; + +private: + bool ProcessConnection(NetworkConnection& connection); + void ProcessPacket(NetworkConnection& connection, NetworkPacket& packet); + void ProcessGameCommandQueue(); + void AddClient(SOCKET socket); + void RemoveClient(std::unique_ptr& connection); + NetworkPlayer* AddPlayer(const char* name); + void PrintError(); + + struct GameCommand + { + GameCommand(uint32 t, uint32* args, uint8 p, uint8 cb) { tick = t, eax = args[0], ebx = args[1], ecx = args[2], edx = args[3], esi = args[4], edi = args[5], ebp = args[6]; playerid = p; callback = cb; }; + uint32 tick; + uint32 eax, ebx, ecx, edx, esi, edi, ebp; + uint8 playerid; + uint8 callback; + bool operator<(const GameCommand& comp) const { + return tick < comp.tick; + } + }; + + int mode; + bool wsa_initialized; + SOCKET server_socket; + SOCKET listening_socket; + NetworkConnection server_connection; + uint32 last_tick_sent_time; + uint32 last_ping_sent_time; + uint32 server_tick; + uint32 server_srand0; + uint32 server_srand0_tick; + uint8 player_id; + std::list> client_connection_list; + std::multiset game_command_queue; + std::vector chunk_buffer; + char password[33]; + +private: + std::vector client_command_handlers; + std::vector server_command_handlers; + int Client_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet); + int Server_Handle_AUTH(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_MAP(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet); + int Server_Handle_CHAT(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket& packet); + int Server_Handle_GAMECMD(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_TICK(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_PLAYERLIST(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_PING(NetworkConnection& connection, NetworkPacket& packet); + int Server_Handle_PING(NetworkConnection& connection, NetworkPacket& packet); + int Client_Handle_PINGLIST(NetworkConnection& connection, NetworkPacket& packet); +}; + +extern "C" { +#endif + +int network_init(); +void network_close(); +int network_begin_client(const char *host, int port); +int network_begin_server(int port); + +void network_update(); +int network_get_mode(); +int network_get_authstatus(); +uint32 network_get_server_tick(); +uint8 network_get_player_id(); +int network_get_num_players(); +const char* network_get_player_name(unsigned int index); +uint32 network_get_player_flags(unsigned int index); +int network_get_player_ping(unsigned int index); + +void network_send_map(); +void network_send_chat(const char* text); +void network_send_gamecmd(uint32 eax, uint32 ebx, uint32 ecx, uint32 edx, uint32 esi, uint32 edi, uint32 ebp, uint8 callback); + +void network_print_error(); + +#ifdef __cplusplus +} +#endif + +#endif /* DISABLE_NETWORK */ + +#endif \ No newline at end of file diff --git a/src/openrct2.c b/src/openrct2.c index 1a83f95a91..02d96a5f1e 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -26,10 +26,12 @@ #include "editor.h" #include "game.h" #include "hook.h" +#include "interface/chat.h" #include "interface/window.h" #include "interface/viewport.h" #include "localisation/localisation.h" #include "network/http.h" +#include "network/network.h" #include "openrct2.h" #include "platform/platform.h" #include "ride/ride.h" @@ -168,10 +170,10 @@ bool openrct2_initialise() config_save_default(); // TODO add configuration option to allow multiple instances - if (!gOpenRCT2Headless && !platform_lock_single_instance()) { - log_fatal("OpenRCT2 is already running."); - return false; - } + // if (!gOpenRCT2Headless && !platform_lock_single_instance()) { + // log_fatal("OpenRCT2 is already running."); + // return false; + // } get_system_info(); if (!gOpenRCT2Headless) { @@ -200,6 +202,8 @@ bool openrct2_initialise() if (!rct2_init()) return false; + chat_init(); + openrct2_copy_original_user_files_over(); // TODO move to audio initialise function @@ -248,7 +252,14 @@ void openrct2_launch() editor_load_landscape(gOpenRCT2StartupActionPath); } break; - } + } + + if (gNetworkStart == NETWORK_MODE_CLIENT) { + network_begin_client(gNetworkStartHost, gNetworkStartPort); + } else if (gNetworkStart == NETWORK_MODE_SERVER) { + network_begin_server(gNetworkStartPort); + } + openrct2_loop(); } openrct2_dispose(); @@ -259,6 +270,7 @@ void openrct2_launch() void openrct2_dispose() { + network_close(); http_dispose(); language_close_all(); platform_free(); @@ -360,6 +372,7 @@ static void openrct2_loop() invalidate_sprite(&g_sprite_list[i]); sprite_move(_spritelocations2[i].x, _spritelocations2[i].y, _spritelocations2[i].z, &g_sprite_list[i]); } + network_update(); } else { uncapTick = 0; currentTick = SDL_GetTicks(); diff --git a/src/peep/peep.c b/src/peep/peep.c index 01469693f3..8fac10960e 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -3739,7 +3739,7 @@ static void peep_update_entering_park(rct_peep* peep){ peep_window_state_update(peep); peep->var_2A = 0; - peep->time_in_park = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, sint32); + peep->time_in_park = RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32); RCT2_GLOBAL(RCT2_ADDRESS_GUESTS_IN_PARK, uint16)++; RCT2_GLOBAL(RCT2_ADDRESS_GUESTS_HEADING_FOR_PARK, uint16)--; RCT2_GLOBAL(RCT2_ADDRESS_BTM_TOOLBAR_DIRTY_FLAGS, uint16) |= BTM_TB_DIRTY_FLAG_PEEP_COUNT; diff --git a/src/rct2.c b/src/rct2.c index 14c69eef5a..24f00a2c45 100644 --- a/src/rct2.c +++ b/src/rct2.c @@ -29,6 +29,7 @@ #include "drawing/drawing.h" #include "editor.h" #include "game.h" +#include "interface/chat.h" #include "interface/console.h" #include "interface/viewport.h" #include "intro.h" @@ -74,7 +75,7 @@ int rct2_init() { log_verbose("initialising game"); - RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, int) = 0; + RCT2_GLOBAL(RCT2_ADDRESS_SCENARIO_TICKS, uint32) = 0; RCT2_GLOBAL(0x009AC310, char*) = RCT2_GLOBAL(RCT2_ADDRESS_CMDLINE, char*); get_system_time(); srand((unsigned int)time(0)); @@ -228,6 +229,7 @@ void rct2_draw() update_rain_animation(); update_palette_effects(); + chat_draw(); console_draw(RCT2_ADDRESS(RCT2_ADDRESS_SCREEN_DPI, rct_drawpixelinfo)); if (RCT2_GLOBAL(RCT2_ADDRESS_RUN_INTRO_TICK_PART, uint8) != 0) { @@ -381,6 +383,7 @@ void rct2_update_2() stop_completed_sounds(); // removes other sounds that are no longer playing in directsound twitch_update(); + chat_update(); console_update(); } diff --git a/src/ride/ride.c b/src/ride/ride.c index 4ebe33d133..df2c889005 100644 --- a/src/ride/ride.c +++ b/src/ride/ride.c @@ -32,6 +32,7 @@ #include "../management/finance.h" #include "../management/marketing.h" #include "../management/news_item.h" +#include "../network/network.h" #include "../peep/peep.h" #include "../peep/staff.h" #include "../rct1.h" @@ -812,7 +813,7 @@ int ride_create_ride(ride_list_item listItem) edx = *((uint16*)&listItem); eax = 0; ecx = 0; - ebx = 1; + ebx = GAME_COMMAND_FLAG_APPLY; edi = 0; esi = 0; @@ -831,9 +832,12 @@ void ride_construct_new(ride_list_item listItem) { int rideIndex; + game_command_callback = game_command_callback_ride_construct_new; rideIndex = ride_create_ride(listItem); - if (rideIndex != -1) - ride_construct(rideIndex); + + // moved to game_command_callback_ride_construct_new: + /*if (rideIndex != -1) + ride_construct(rideIndex);*/ } /** @@ -5072,6 +5076,13 @@ void game_command_create_ride(int *eax, int *ebx, int *ecx, int *edx, int *esi, *ebx = ride_create(*edx & 0xFF, (*edx >> 8) & 0xFF, *ebx & 0xFF, edi); } +void game_command_callback_ride_construct_new(int eax, int ebx, int ecx, int edx, int esi, int edi, int ebp) +{ + int rideIndex = edi; + if (rideIndex != -1) + ride_construct(rideIndex); +} + /** * * rct2: 0x006B49D9 diff --git a/src/ride/ride.h b/src/ride/ride.h index 2166b8a37a..4539de5f40 100644 --- a/src/ride/ride.h +++ b/src/ride/ride.h @@ -858,7 +858,7 @@ enum { #define STATION_DEPART_MASK (~STATION_DEPART_FLAG) // rct2: 0x009ACFA4 -rct_ride_type **gRideTypeList; +extern rct_ride_type **gRideTypeList; // rct2: 0x013628F8 extern rct_ride* g_ride_list; @@ -960,6 +960,7 @@ void game_command_set_ride_name(int *eax, int *ebx, int *ecx, int *edx, int *esi void game_command_set_ride_setting(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); int ride_get_refund_price(int ride_id); void game_command_create_ride(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); +void game_command_callback_ride_construct_new(int eax, int ebx, int ecx, int edx, int esi, int edi, int ebp); void game_command_demolish_ride(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); void game_command_set_ride_appearance(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); void game_command_set_ride_price(int *eax, int *ebx, int *ecx, int *edx, int *esi, int *edi, int *ebp); diff --git a/src/ride/track.c b/src/ride/track.c index 2ff3307319..c3eba159ff 100644 --- a/src/ride/track.c +++ b/src/ride/track.c @@ -1963,7 +1963,7 @@ int sub_6D2189(int* cost, uint8* ride_id){ entry_index = 0xFF; int eax = 0, ebx, ecx = 0, edx, esi, edi = 0, ebp = 0; - ebx = 41; + ebx = GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5; edx = track_design->type | (entry_index << 8); esi = GAME_COMMAND_CREATE_RIDE; @@ -3185,7 +3185,7 @@ void game_command_place_track_design(int* eax, int* ebx, int* ecx, int* edx, int RCT2_GLOBAL(0x009DEA60, sint16) = y + 16; RCT2_GLOBAL(0x009DEA62, sint16) = z; - if (!(flags & (1 << 3))){ + if (!(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED)){ if (RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 && !gConfigCheat.build_in_pause_mode){ RCT2_GLOBAL(0x00141E9AC, rct_string_id) = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED; *ebx = MONEY32_UNDEFINED; @@ -3232,7 +3232,7 @@ void game_command_place_track_design(int* eax, int* ebx, int* ecx, int* edx, int } else{ uint8 bl = 0; - if (flags & (1 << 6)){ + if (flags & GAME_COMMAND_FLAG_GHOST){ bl = 4; } else{ @@ -3506,7 +3506,7 @@ static money32 track_place(int rideIndex, int type, int originX, int originY, in if (!sub_68B044()) { return MONEY32_UNDEFINED; } - if (!(flags & (1 << 3))) { + if (!(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED)) { if (RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 && !gConfigCheat.build_in_pause_mode) { RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id) = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED; return MONEY32_UNDEFINED; @@ -3638,7 +3638,7 @@ static money32 track_place(int rideIndex, int type, int originX, int originY, in RCT2_GLOBAL(0x00F44060, void*) = &clearance_struct; - if (!gCheatsDisableClearanceChecks) { + if (!gCheatsDisableClearanceChecks || flags & GAME_COMMAND_FLAG_GHOST){ if (!map_can_construct_with_clear_at(x, y, baseZ, clearanceZ, (void*)0x006C5A5F, bl)) return MONEY32_UNDEFINED; } @@ -3649,7 +3649,7 @@ static money32 track_place(int rideIndex, int type, int originX, int originY, in // push baseZ and clearanceZ int cur_z = baseZ * 8; - if ((flags & GAME_COMMAND_FLAG_APPLY) && !(flags & (1 << 3))) { + if ((flags & GAME_COMMAND_FLAG_APPLY) && !(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED)) { footpath_remove_litter(x, y, z); // push bl bh?? if (rideTypeFlags & RIDE_TYPE_FLAG_18) { @@ -3755,7 +3755,7 @@ static money32 track_place(int rideIndex, int type, int originX, int originY, in if (entranceDirections & 0x20) { entranceDirections &= 0x0F; if (entranceDirections != 0) { - if (!(flags & GAME_COMMAND_FLAG_APPLY) && !(flags & 0x40)) { + if (!(flags & GAME_COMMAND_FLAG_APPLY) && !(flags & GAME_COMMAND_FLAG_GHOST)) { uint8 _bl = entranceDirections; for (int dl = bitscanforward(_bl); dl != -1; dl = bitscanforward(_bl)){ _bl &= ~(1 << dl); @@ -3836,7 +3836,7 @@ static money32 track_place(int rideIndex, int type, int originX, int originY, in entranceDirections = 0; if (ride->overall_view != 0xFFFF){ - if (!(flags & (1 << 5))){ + if (!(flags & GAME_COMMAND_FLAG_5)){ if (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_FLAT_RIDE)) { entranceDirections = RCT2_ADDRESS(0x0099CA64, uint8)[type * 16]; } @@ -3863,7 +3863,7 @@ static money32 track_place(int rideIndex, int type, int originX, int originY, in mapElement->properties.track.ride_index = rideIndex; mapElement->properties.track.type = type; mapElement->properties.track.colour = 0; - if (flags & (1 << 6)){ + if (flags & GAME_COMMAND_FLAG_GHOST){ mapElement->flags |= MAP_ELEMENT_FLAG_GHOST; } diff --git a/src/scenario.c b/src/scenario.c index 9ecb98e4a3..52c072b186 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -979,6 +979,72 @@ int scenario_save(SDL_RWops* rw, int flags) return 1; } +// Save game state without modifying any of the state for multiplayer +int scenario_save_network(SDL_RWops* rw) +{ + rct_window *w; + rct_viewport *viewport; + int viewX, viewY, viewZoom, viewRotation; + + /*map_reorganise_elements(); + reset_0x69EBE4(); + sprite_clear_all_unused(); + sub_677552(); + sub_674BCF();*/ + + // Set saved view + w = window_get_main(); + if (w != NULL) { + viewport = w->viewport; + + viewX = viewport->view_width / 2 + viewport->view_x; + viewY = viewport->view_height / 2 + viewport->view_y; + viewZoom = viewport->zoom; + viewRotation = RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_ROTATION, uint8); + } else { + viewX = 0; + viewY = 0; + viewZoom = 0; + viewRotation = 0; + } + + RCT2_GLOBAL(RCT2_ADDRESS_SAVED_VIEW_X, uint16) = viewX; + RCT2_GLOBAL(RCT2_ADDRESS_SAVED_VIEW_Y, uint16) = viewY; + RCT2_GLOBAL(RCT2_ADDRESS_SAVED_VIEW_ZOOM_AND_ROTATION, uint16) = viewZoom | (viewRotation << 8); + + // Prepare S6 + rct_s6_data *s6 = malloc(sizeof(rct_s6_data)); + s6->header.type = S6_TYPE_SAVEDGAME; + s6->header.num_packed_objects = scenario_get_num_packed_objects_to_write(); + s6->header.version = S6_RCT2_VERSION; + s6->header.magic_number = S6_MAGIC_NUMBER; + + memcpy(&s6->info, (rct_s6_info*)0x0141F570, sizeof(rct_s6_info)); + + for (int i = 0; i < 721; i++) { + uint32 chunkPtr = RCT2_ADDRESS(0x009ACFA4, uint32)[i]; + rct_object_entry_extended *entry = &(RCT2_ADDRESS(0x00F3F03C, rct_object_entry_extended)[i]); + + if (RCT2_ADDRESS(0x009ACFA4, uint32)[i] == 0xFFFFFFFF) { + memset(&s6->objects[i], 0xFF, sizeof(rct_object_entry)); + } else { + s6->objects[i] = *((rct_object_entry*)entry); + } + } + + memcpy(&s6->elapsed_months, (void*)0x00F663A8, 16); + memcpy(s6->map_elements, (void*)0x00F663B8, 0x180000); + memcpy(&s6->dword_010E63B8, (void*)0x010E63B8, 0x2E8570); + + scenario_fix_ghosts(s6); + scenario_save_s6(rw, s6); + + free(s6); + + gfx_invalidate_screen(); + return 1; +} + bool scenario_save_s6(SDL_RWops* rw, rct_s6_data *s6) { char *buffer; diff --git a/src/scenario.h b/src/scenario.h index d2cdd74b98..80df725b6f 100644 --- a/src/scenario.h +++ b/src/scenario.h @@ -427,6 +427,7 @@ unsigned int scenario_rand(); unsigned int scenario_rand_max(unsigned int max); int scenario_prepare_for_save(); int scenario_save(SDL_RWops* rw, int flags); +int scenario_save_network(SDL_RWops* rw); bool scenario_save_s6(SDL_RWops* rw, rct_s6_data *s6); void scenario_set_filename(const char *value); void scenario_failure(); diff --git a/src/windows/game_bottom_toolbar.c b/src/windows/game_bottom_toolbar.c index e21333a4cc..0b27eb209d 100644 --- a/src/windows/game_bottom_toolbar.c +++ b/src/windows/game_bottom_toolbar.c @@ -420,7 +420,7 @@ static void window_game_bottom_toolbar_draw_park_rating(rct_drawpixelinfo *dpi, bar_width = (factor * (90 + WIDTH_MOD)) / 256; gfx_fill_rect_inset(dpi, x, y + 1, x + (93 + WIDTH_MOD), y + 9, w->colours[1], 48); - if (!(colour & 0x80000000) || RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 || (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint8) & 8)) { + if (!(colour & 0x80000000) || RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 || (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) & 8)) { if (bar_width > 2) gfx_fill_rect_inset(dpi, x + 2, y + 2, x + bar_width - 1, y + 8, colour & 0x7FFFFFFF, 0); } diff --git a/src/windows/network_status.c b/src/windows/network_status.c new file mode 100644 index 0000000000..d2f49888a1 --- /dev/null +++ b/src/windows/network_status.c @@ -0,0 +1,145 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ + +#include "../interface/themes.h" +#include "../interface/widget.h" +#include "../interface/window.h" +#include "../localisation/localisation.h" +#include "../network/network.h" + +enum WINDOW_NETWORK_STATUS_WIDGET_IDX { + WIDX_BACKGROUND, + WIDX_TITLE, + WIDX_CLOSE, +}; + +static rct_widget window_network_status_widgets[] = { + { WWT_FRAME, 0, 0, 340, 0, 90, 0x0FFFFFFFF, STR_NONE }, // panel / background + { WWT_CAPTION, 0, 1, 338, 1, 14, STR_NONE, STR_WINDOW_TITLE_TIP }, // title bar + { WWT_CLOSEBOX, 0, 327, 337, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP }, // close x button + { WIDGETS_END }, +}; + +static char window_network_status_text[1024]; + +static void window_network_status_mouseup(rct_window *w, int widgetIndex); +static void window_network_status_update(rct_window *w); +static void window_network_status_invalidate(rct_window *w); +static void window_network_status_paint(rct_window *w, rct_drawpixelinfo *dpi); + +static rct_window_event_list window_network_status_events = { + NULL, + window_network_status_mouseup, + NULL, + NULL, + NULL, + NULL, + window_network_status_update, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + window_network_status_invalidate, + window_network_status_paint, + NULL +}; + +void window_network_status_open(const char* text) +{ + rct_window* window; + + strncpy(window_network_status_text, text, sizeof(window_network_status_text)); + + // Check if window is already open + window = window_bring_to_front_by_class(WC_NETWORK_STATUS); + if (window != NULL) + return; + + window = window_create_centred(320, 90, &window_network_status_events, WC_NETWORK_STATUS, WF_10 | WF_TRANSPARENT); + + window->widgets = window_network_status_widgets; + window->enabled_widgets = 1 << WIDX_CLOSE; + window_init_scroll_widgets(window); + window->no_list_items = 0; + window->selected_list_item = -1; + window->frame_no = 0; + window->min_width = 320; + window->min_height = 90; + window->max_width = window->min_width; + window->max_height = window->min_height; + + window->page = 0; + window->list_information_type = 0; + window->colours[0] = 7; + window->colours[1] = 7; + window->colours[2] = 7; +} + +static void window_network_status_mouseup(rct_window *w, int widgetIndex) +{ + switch (widgetIndex) { + case WIDX_CLOSE: + window_close(w); + break; + } +} + +static void window_network_status_update(rct_window *w) +{ + widget_invalidate(w, WIDX_BACKGROUND); +} + +static void window_network_status_invalidate(rct_window *w) +{ + window_network_status_widgets[WIDX_BACKGROUND].right = w->width - 1; + window_network_status_widgets[WIDX_BACKGROUND].bottom = w->height - 1; + window_network_status_widgets[WIDX_TITLE].right = w->width - 2; + window_network_status_widgets[WIDX_CLOSE].left = w->width - 2 - 0x0B; + window_network_status_widgets[WIDX_CLOSE].right = w->width - 2 - 0x0B + 0x0A; +} + +static void window_network_status_paint(rct_window *w, rct_drawpixelinfo *dpi) +{ + window_draw_widgets(w, dpi); + RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, uint16) = 224; + char buffer[sizeof(window_network_status_text) + 10]; + char* lineCh = buffer; + lineCh = utf8_write_codepoint(lineCh, FORMAT_BLACK); + strcpy(lineCh, window_network_status_text); + gfx_clip_string(buffer, 230); + int x = w->x + (w->width / 2); + int y = w->y + (w->height / 2); + x -= gfx_get_string_width(buffer) / 2; + gfx_draw_string(dpi, buffer, 0, x, y); +} diff --git a/src/windows/player_list.c b/src/windows/player_list.c new file mode 100644 index 0000000000..538887d547 --- /dev/null +++ b/src/windows/player_list.c @@ -0,0 +1,269 @@ +/***************************************************************************** + * Copyright (c) 2014 Ted John + * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. + * + * This file is part of OpenRCT2. + * + * OpenRCT2 is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + *****************************************************************************/ + +#include "../interface/themes.h" +#include "../interface/widget.h" +#include "../interface/window.h" +#include "../localisation/localisation.h" +#include "../network/network.h" + +enum WINDOW_PLAYER_LIST_WIDGET_IDX { + WIDX_BACKGROUND, + WIDX_TITLE, + WIDX_CLOSE, + WIDX_CONTENT_PANEL, + WIDX_TAB1, + WIDX_LIST, +}; + +static rct_widget window_player_list_widgets[] = { + { WWT_FRAME, 0, 0, 339, 0, 239, 0x0FFFFFFFF, STR_NONE }, // panel / background + { WWT_CAPTION, 0, 1, 338, 1, 14, STR_PLAYER_LIST, STR_WINDOW_TITLE_TIP }, // title bar + { WWT_CLOSEBOX, 0, 327, 337, 2, 13, STR_CLOSE_X, STR_CLOSE_WINDOW_TIP }, // close x button + { WWT_RESIZE, 1, 0, 339, 43, 239, 0x0FFFFFFFF, STR_NONE }, // content panel + { WWT_TAB, 1, 3, 33, 17, 43, 0x02000144E, STR_NONE }, // tab + { WWT_SCROLL, 1, 3, 336, 60, 236, 2, STR_NONE }, // list + { WIDGETS_END }, +}; + +static void window_player_list_mouseup(rct_window *w, int widgetIndex); +static void window_player_list_resize(rct_window *w); +static void window_player_list_mousedown(int widgetIndex, rct_window* w, rct_widget* widget); +static void window_player_list_update(rct_window *w); +static void window_player_list_scrollgetsize(rct_window *w, int scrollIndex, int *width, int *height); +static void window_player_list_scrollmouseover(rct_window *w, int scrollIndex, int x, int y); +static void window_player_list_invalidate(rct_window *w); +static void window_player_list_paint(rct_window *w, rct_drawpixelinfo *dpi); +static void window_player_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex); + +static rct_window_event_list window_player_list_events = { + NULL, + window_player_list_mouseup, + window_player_list_resize, + window_player_list_mousedown, + NULL, + NULL, + window_player_list_update, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + window_player_list_scrollgetsize, + NULL, + NULL, + window_player_list_scrollmouseover, + NULL, + NULL, + NULL, + NULL, + NULL, + NULL, + window_player_list_invalidate, + window_player_list_paint, + window_player_list_scrollpaint +}; + +static void window_player_list_refresh_list(rct_window *w); + +void window_player_list_open() +{ + rct_window* window; + + // Check if window is already open + window = window_bring_to_front_by_class(WC_PLAYER_LIST); + if (window != NULL) + return; + + window = window_create_auto_pos(320, 144, &window_player_list_events, WC_PLAYER_LIST, WF_10 | WF_RESIZABLE); + + window->widgets = window_player_list_widgets; + window->enabled_widgets = 1 << WIDX_CLOSE; + window_init_scroll_widgets(window); + window->no_list_items = 0; + window->selected_list_item = -1; + window->frame_no = 0; + window->min_width = 320; + window->min_height = 124; + window->max_width = 500; + window->max_height = 450; + + window->page = 0; + window->list_information_type = 0; + window->colours[0] = 7; + window->colours[1] = 7; + window->colours[2] = 7; +} + +static void window_player_list_mouseup(rct_window *w, int widgetIndex) +{ + switch (widgetIndex) { + case WIDX_CLOSE: + window_close(w); + break; + } +} + +static void window_player_list_resize(rct_window *w) +{ + if (w->width < w->min_width) { + window_invalidate(w); + w->width = w->min_width; + } + if (w->height < w->min_height) { + window_invalidate(w); + w->height = w->min_height; + } + + window_player_list_refresh_list(w); +} + +static void window_player_list_mousedown(int widgetIndex, rct_window* w, rct_widget* widget) +{ + switch (widgetIndex) { + case WIDX_TAB1: + if (w->page != widgetIndex - WIDX_TAB1) { + w->page = widgetIndex - WIDX_TAB1; + w->no_list_items = 0; + w->frame_no = 0; + w->selected_list_item = -1; + window_invalidate(w); + } + break; + } +} + +static void window_player_list_update(rct_window *w) +{ + widget_invalidate(w, WIDX_TAB1 + w->page); +} + +static void window_player_list_scrollgetsize(rct_window *w, int scrollIndex, int *width, int *height) +{ + int i; + + if (w->selected_list_item != -1) { + w->selected_list_item = -1; + window_invalidate(w); + } + + *height = network_get_num_players() * 10; + i = *height - window_player_list_widgets[WIDX_LIST].bottom + window_player_list_widgets[WIDX_LIST].top + 21; + if (i < 0) + i = 0; + if (i < w->scrolls[0].v_top) { + w->scrolls[0].v_top = i; + window_invalidate(w); + } +} + +static void window_player_list_scrollmouseover(rct_window *w, int scrollIndex, int x, int y) +{ + int index; + + index = y / 10; + if (index >= w->no_list_items) + return; + + w->selected_list_item = index; + window_invalidate(w); +} + +static void window_player_list_invalidate(rct_window *w) +{ + for (int i = 0; i < 1; i++) + w->pressed_widgets &= ~(1 << (WIDX_TAB1 + i)); + w->pressed_widgets |= 1LL << (WIDX_TAB1 + w->page); + window_player_list_widgets[WIDX_BACKGROUND].right = w->width - 1; + window_player_list_widgets[WIDX_BACKGROUND].bottom = w->height - 1; + window_player_list_widgets[WIDX_CONTENT_PANEL].right = w->width - 1; + window_player_list_widgets[WIDX_CONTENT_PANEL].bottom = w->height - 1; + window_player_list_widgets[WIDX_TITLE].right = w->width - 2; + window_player_list_widgets[WIDX_CLOSE].left = w->width - 2 - 0x0B; + window_player_list_widgets[WIDX_CLOSE].right = w->width - 2 - 0x0B + 0x0A; + window_player_list_widgets[WIDX_LIST].right = w->width - 4; + window_player_list_widgets[WIDX_LIST].bottom = w->height - 0x0F; +} + +static void window_player_list_paint(rct_window *w, rct_drawpixelinfo *dpi) +{ + window_draw_widgets(w, dpi); + gfx_draw_string_left(dpi, STR_PLAYER, w, w->colours[2], w->x + 6, 58 - 12 + w->y + 1); + gfx_draw_string_left(dpi, STR_PING, w, w->colours[2], w->x + 246, 58 - 12 + w->y + 1); +} + +static void window_player_list_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrollIndex) +{ + int y; + + gfx_fill_rect(dpi, dpi->x, dpi->y, dpi->x + dpi->width - 1, dpi->y + dpi->height - 1, ((char*)0x0141FC48)[w->colours[1] * 8]); + + y = 0; + for (int i = 0; i < network_get_num_players(); i++) { + if (y > dpi->y + dpi->height) { + break; + } + + if (y + 11 >= dpi->y) { + char buffer[300]; + char* lineCh = buffer; + int colour = 0; + if (i == w->selected_list_item) { + gfx_fill_rect(dpi, 0, y, 800, y + 9, 0x02000031); + strcpy(&buffer[0], network_get_player_name(i)); + colour = w->colours[2]; + } else { + if (network_get_player_flags(i) & NETWORK_PLAYER_FLAG_ISSERVER) { + lineCh = utf8_write_codepoint(lineCh, FORMAT_BABYBLUE); + } else { + lineCh = utf8_write_codepoint(lineCh, FORMAT_BLACK); + } + strcpy(lineCh, network_get_player_name(i)); + } + gfx_clip_string(buffer, 230); + gfx_draw_string(dpi, buffer, colour, 0, y - 1); + lineCh = buffer; + int ping = network_get_player_ping(i); + if (ping <= 100) { + lineCh = utf8_write_codepoint(lineCh, FORMAT_GREEN); + } else + if (ping <= 250) { + lineCh = utf8_write_codepoint(lineCh, FORMAT_YELLOW); + } else { + lineCh = utf8_write_codepoint(lineCh, FORMAT_RED); + } + sprintf(lineCh, "%d ms", ping); + gfx_draw_string(dpi, buffer, colour, 240, y - 1); + } + y += 10; + } +} + +static void window_player_list_refresh_list(rct_window *w) +{ + w->no_list_items = network_get_num_players(); + w->list_item_positions[0] = 0; + + w->selected_list_item = -1; + window_invalidate(w); +} diff --git a/src/windows/ride_construction.c b/src/windows/ride_construction.c index 7cc2652964..228e9b6e5c 100644 --- a/src/windows/ride_construction.c +++ b/src/windows/ride_construction.c @@ -29,6 +29,7 @@ #include "../interface/widget.h" #include "../interface/window.h" #include "../localisation/localisation.h" +#include "../network/network.h" #include "../ride/track.h" #include "dropdown.h" @@ -579,7 +580,7 @@ static void window_ride_construction_close(rct_window *w) hide_gridlines(); uint8 rideIndex = _currentRideIndex; - if (sub_6CAF80(rideIndex, &mapElement)) { + if (sub_6CAF80(rideIndex, &mapElement) || network_get_mode() == NETWORK_MODE_CLIENT) { window_ride_main_open(rideIndex); } else { int eax = RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8); diff --git a/src/windows/top_toolbar.c b/src/windows/top_toolbar.c index c55bad4f31..4c873ddb82 100644 --- a/src/windows/top_toolbar.c +++ b/src/windows/top_toolbar.c @@ -65,6 +65,7 @@ enum { WIDX_FINANCES, WIDX_RESEARCH, WIDX_NEWS, + WIDX_NETWORK, WIDX_SEPARATOR, }; @@ -105,6 +106,10 @@ typedef enum { DDIDX_SCENARIO_OPTIONS = 4 } TOP_TOOLBAR_DEBUG_DDIDX; +typedef enum { + DDIDX_PLAYER_LIST = 0 +} TOP_TOOLBAR_NETWORK_DDIDX; + enum { DDIDX_CHEATS, DDIDX_ENABLE_SANDBOX_MODE = 2, @@ -129,6 +134,7 @@ static const int left_aligned_widgets_order[] = { WIDX_ROTATE, WIDX_VIEW_MENU, WIDX_MAP, + WIDX_NETWORK, }; // from right to left @@ -179,6 +185,7 @@ static rct_widget window_top_toolbar_widgets[] = { { WWT_TRNBTN, 3, 0x001E, 0x003B, 0, 27, 0x20000000 | 0x15F9, 3235 }, // Finances { WWT_TRNBTN, 3, 0x001E, 0x003B, 0, 27, 0x20000000 | 0x15F9, 2275 }, // Research { WWT_TRNBTN, 3, 0x001E, 0x003B, 0, 27, 0x20000000 | 0x15F9, STR_SHOW_RECENT_MESSAGES_TIP }, // News + { WWT_TRNBTN, 1, 0x001E, 0x003B, 0, 27, 0x20000000 | 0x15F9, 2276 }, // Network { WWT_EMPTY, 0, 0, 10-1, 0, 0, 0xFFFFFFFF, STR_NONE }, // Artificial widget separator { WIDGETS_END }, @@ -234,6 +241,8 @@ void top_toolbar_init_rotate_menu(rct_window *window, rct_widget *widget); void top_toolbar_rotate_menu_dropdown(short dropdownIndex); void top_toolbar_init_debug_menu(rct_window *window, rct_widget *widget); void top_toolbar_debug_menu_dropdown(short dropdownIndex); +void top_toolbar_init_network_menu(rct_window *window, rct_widget *widget); +void top_toolbar_network_menu_dropdown(short dropdownIndex); void toggle_footpath_window(); void toggle_land_window(rct_window *topToolbar, int widgetIndex); @@ -461,6 +470,9 @@ static void window_top_toolbar_mousedown(int widgetIndex, rct_window*w, rct_widg case WIDX_DEBUG: top_toolbar_init_debug_menu(w, widget); break; + case WIDX_NETWORK: + top_toolbar_init_network_menu(w, widget); + break; } } @@ -560,6 +572,9 @@ static void window_top_toolbar_dropdown(rct_window *w, int widgetIndex, int drop case WIDX_DEBUG: top_toolbar_debug_menu_dropdown(dropdownIndex); break; + case WIDX_NETWORK: + top_toolbar_network_menu_dropdown(dropdownIndex); + break; } } @@ -598,6 +613,7 @@ static void window_top_toolbar_invalidate(rct_window *w) window_top_toolbar_widgets[WIDX_CHEATS].type = WWT_TRNBTN; window_top_toolbar_widgets[WIDX_DEBUG].type = gConfigGeneral.debugging_tools ? WWT_TRNBTN : WWT_EMPTY; window_top_toolbar_widgets[WIDX_NEWS].type = WWT_TRNBTN; + window_top_toolbar_widgets[WIDX_NETWORK].type = WWT_TRNBTN; if (RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & (SCREEN_FLAGS_SCENARIO_EDITOR | SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER)) { window_top_toolbar_widgets[WIDX_PAUSE].type = WWT_EMPTY; @@ -645,7 +661,7 @@ static void window_top_toolbar_invalidate(rct_window *w) } enabledWidgets = 0; - for (i = WIDX_PAUSE; i <= WIDX_NEWS; i++) + for (i = WIDX_PAUSE; i <= WIDX_NETWORK; i++) if (window_top_toolbar_widgets[i].type != WWT_EMPTY) enabledWidgets |= (1 << i); w->enabled_widgets = enabledWidgets; @@ -1637,13 +1653,11 @@ void top_toolbar_tool_update_scenery_clear(sint16 x, sint16 y){ if (!state_changed) return; - money32 cost = map_clear_scenery( - RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_X, sint16), - RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_Y, sint16), - RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_X, sint16), - RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_Y, sint16), - 0 - ); + int eax = RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_X, sint16); + int ecx = RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_Y, sint16); + int edi = RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_X, sint16); + int ebp = RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_Y, sint16); + money32 cost = game_do_command(eax, 0, ecx, 0, GAME_COMMAND_CLEAR_SCENERY, edi, ebp); if (RCT2_GLOBAL(0x00F1AD62, money32) != cost){ RCT2_GLOBAL(0x00F1AD62, money32) = cost; @@ -2472,7 +2486,7 @@ static void window_top_toolbar_tool_down(rct_window* w, int widgetIndex, int x, RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_X, sint16), 1, RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_Y, sint16), - 0, + (gClearSmallScenery | gClearLargeScenery << 1 | gClearFootpath << 2), GAME_COMMAND_CLEAR_SCENERY, RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_X, sint16), RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_Y, sint16) @@ -2681,7 +2695,7 @@ static void window_top_toolbar_tool_drag(rct_window* w, int widgetIndex, int x, RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_X, sint16), 1, RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_A_Y, sint16), - 0, + (gClearSmallScenery | gClearLargeScenery << 1 | gClearFootpath << 2), GAME_COMMAND_CLEAR_SCENERY, RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_X, sint16), RCT2_GLOBAL(RCT2_ADDRESS_MAP_SELECTION_B_Y, sint16) @@ -2862,6 +2876,21 @@ void top_toolbar_init_debug_menu(rct_window* w, rct_widget* widget) { RCT2_GLOBAL(0x9DEBA2, uint16) = 0; } +void top_toolbar_init_network_menu(rct_window* w, rct_widget* widget) { + gDropdownItemsFormat[0] = STR_PLAYER_LIST; + + window_dropdown_show_text( + w->x + widget->left, + w->y + widget->top, + widget->bottom - widget->top + 1, + w->colours[1] | 0x80, + 0, + 1 + ); + + RCT2_GLOBAL(0x9DEBA2, uint16) = 0; +} + void top_toolbar_debug_menu_dropdown(short dropdownIndex) { if (dropdownIndex == -1) dropdownIndex = RCT2_GLOBAL(0x9DEBA2, uint16); rct_window* w = window_get_main(); @@ -2887,6 +2916,18 @@ void top_toolbar_debug_menu_dropdown(short dropdownIndex) { } } +void top_toolbar_network_menu_dropdown(short dropdownIndex) { + if (dropdownIndex == -1) dropdownIndex = RCT2_GLOBAL(0x9DEBA2, uint16); + rct_window* w = window_get_main(); + if (w) { + switch (dropdownIndex) { + case DDIDX_PLAYER_LIST: + window_player_list_open(); + break; + } + } +} + /** * * rct2: 0x0066CDE4 diff --git a/src/world/climate.c b/src/world/climate.c index 60347f3d10..f72319fd3b 100644 --- a/src/world/climate.c +++ b/src/world/climate.c @@ -168,7 +168,7 @@ void climate_update() RCT2_GLOBAL(RCT2_ADDRESS_CLIMATE_UPDATE_TIMER, sint16)--; - } else if (!(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, sint32) & 0x7F)) { + } else if (!(RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) & 0x7F)) { if (temperature == target_temperature) { if (cur_gloom == next_gloom) { diff --git a/src/world/footpath.c b/src/world/footpath.c index 0fd76eeddb..1bfd7e83d0 100644 --- a/src/world/footpath.c +++ b/src/world/footpath.c @@ -34,10 +34,6 @@ void footpath_interrupt_peeps(int x, int y, int z); void sub_6A7642(int x, int y, rct_map_element *mapElement); void sub_6A76E9(int rideIndex); -enum { - FOOTPATH_CONSTRUCTION_FLAG_ALLOW_DURING_PAUSED = 1 << 3 -}; - const rct_xy16 word_981D6C[4] = { { -1, 0 }, { 0, 1 }, @@ -148,7 +144,7 @@ static money32 footpath_element_insert(int type, int x, int y, int z, int slope, if (!sub_68B044()) return MONEY32_UNDEFINED; - if ((flags & GAME_COMMAND_FLAG_APPLY) && !(flags & (FOOTPATH_CONSTRUCTION_FLAG_ALLOW_DURING_PAUSED | (1 << 6)))) + if ((flags & GAME_COMMAND_FLAG_APPLY) && !(flags & (GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_GHOST))) footpath_remove_litter(x, y, RCT2_GLOBAL(0x009DEA62, uint16)); // loc_6A649D: @@ -320,7 +316,7 @@ static money32 footpath_place_real(int type, int x, int y, int z, int slope, int RCT2_GLOBAL(0x009DEA60, uint16) = y + 16; RCT2_GLOBAL(0x009DEA62, uint16) = z * 8; - if (!(flags & FOOTPATH_CONSTRUCTION_FLAG_ALLOW_DURING_PAUSED) && RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 && !gConfigCheat.build_in_pause_mode) { + if (!(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED) && RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 && !gConfigCheat.build_in_pause_mode) { RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id) = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED; return MONEY32_UNDEFINED; } @@ -355,10 +351,13 @@ static money32 footpath_place_real(int type, int x, int y, int z, int slope, int return MONEY32_UNDEFINED; } + footpath_provisional_remove(); mapElement = map_get_footpath_element_slope((x / 32), (y / 32), z, slope); - return mapElement == NULL ? - footpath_element_insert(type, x, y, z, slope, flags) : - footpath_element_update(x, y, mapElement, type, flags); + if (mapElement == NULL) { + return footpath_element_insert(type, x, y, z, slope, flags); + } else { + return footpath_element_update(x, y, mapElement, type, flags); + } } /* rct2: 0x006BA23E */ @@ -381,7 +380,7 @@ money32 footpath_remove_real(int x, int y, int z, int flags) RCT2_GLOBAL(0x009DEA60, uint16) = y + 16; RCT2_GLOBAL(0x009DEA62, uint16) = z * 8; - if (!(flags & FOOTPATH_CONSTRUCTION_FLAG_ALLOW_DURING_PAUSED) && RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 && !gConfigCheat.build_in_pause_mode) { + if (!(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED) && RCT2_GLOBAL(RCT2_ADDRESS_GAME_PAUSED, uint8) != 0 && !gConfigCheat.build_in_pause_mode) { RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TEXT, rct_string_id) = STR_CONSTRUCTION_NOT_POSSIBLE_WHILE_GAME_IS_PAUSED; return MONEY32_UNDEFINED; } @@ -453,7 +452,7 @@ money32 footpath_provisional_set(int type, int x, int y, int z, int slope) footpath_provisional_remove(); - cost = footpath_place(type, x, y, z, slope, (1 << 6) | (1 << 5) | (1 << 4) | FOOTPATH_CONSTRUCTION_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_APPLY); + cost = footpath_place(type, x, y, z, slope, GAME_COMMAND_FLAG_GHOST | GAME_COMMAND_FLAG_5 | GAME_COMMAND_FLAG_4 | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_APPLY); if (cost != MONEY32_UNDEFINED) { RCT2_GLOBAL(RCT2_ADDRESS_PROVISIONAL_PATH_X, uint16) = x; RCT2_GLOBAL(RCT2_ADDRESS_PROVISIONAL_PATH_Y, uint16) = y; @@ -479,7 +478,7 @@ void footpath_provisional_remove() RCT2_GLOBAL(RCT2_ADDRESS_PROVISIONAL_PATH_X, uint16), RCT2_GLOBAL(RCT2_ADDRESS_PROVISIONAL_PATH_Y, uint16), RCT2_GLOBAL(RCT2_ADDRESS_PROVISIONAL_PATH_Z, uint16), - (1 << 0) | FOOTPATH_CONSTRUCTION_FLAG_ALLOW_DURING_PAUSED | (1 << 5) + GAME_COMMAND_FLAG_APPLY | GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED | GAME_COMMAND_FLAG_5 ); } } diff --git a/src/world/map.c b/src/world/map.c index 3004e105fa..d57bf00797 100644 --- a/src/world/map.c +++ b/src/world/map.c @@ -1370,7 +1370,7 @@ void game_command_set_banner_colour(int* eax, int* ebx, int* ecx, int* edx, int* * * rct2: 0x0068DFE4 */ -money32 map_clear_scenery_from_tile(int x, int y, int flags) +money32 map_clear_scenery_from_tile(int x, int y, int clear, int flags) { int type; money32 cost, totalCost; @@ -1384,8 +1384,14 @@ restart_from_beginning: type = map_element_get_type(mapElement); switch (type) { case MAP_ELEMENT_TYPE_PATH: - if (gClearFootpath) { - cost = footpath_remove_real(x * 32, y * 32, mapElement->base_height, flags); + if (clear & (1 << 2)) { + int eax = x * 32; + int ebx = flags; + int ecx = y * 32; + int edx = mapElement->base_height; + int edi, ebp; + cost = game_do_command(eax, ebx, ecx, edx, GAME_COMMAND_REMOVE_PATH, edi, ebp); + if (cost == MONEY32_UNDEFINED) return MONEY32_UNDEFINED; @@ -1394,14 +1400,13 @@ restart_from_beginning: goto restart_from_beginning; } break; case MAP_ELEMENT_TYPE_SCENERY: - if (gClearSmallScenery) { + if (clear & (1 << 0)) { int eax = x * 32; int ebx = (mapElement->type << 8) | flags; int ecx = y * 32; int edx = (mapElement->properties.scenery.type << 8) | (mapElement->base_height); - int esi, edi, ebp; - game_command_remove_scenery(&eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); - cost = ebx; + int edi, ebp; + cost = game_do_command(eax, ebx, ecx, edx, GAME_COMMAND_REMOVE_SCENERY, edi, ebp); if (cost == MONEY32_UNDEFINED) return MONEY32_UNDEFINED; @@ -1412,14 +1417,13 @@ restart_from_beginning: } break; case MAP_ELEMENT_TYPE_FENCE: - if (gClearSmallScenery) { + if (clear & (1 << 0)) { int eax = x * 32; int ebx = flags; int ecx = y * 32; int edx = (mapElement->base_height << 8) | (mapElement->type & MAP_ELEMENT_DIRECTION_MASK); - int esi, edi, ebp; - game_command_remove_fence(&eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); - cost = ebx; + int edi, ebp; + cost = game_do_command(eax, ebx, ecx, edx, GAME_COMMAND_REMOVE_FENCE, edi, ebp); if (cost == MONEY32_UNDEFINED) return MONEY32_UNDEFINED; @@ -1430,14 +1434,13 @@ restart_from_beginning: } break; case MAP_ELEMENT_TYPE_SCENERY_MULTIPLE: - if (gClearLargeScenery) { + if (clear & (1 << 1)) { int eax = x * 32; int ebx = flags | ((mapElement->type & MAP_ELEMENT_DIRECTION_MASK) << 8); int ecx = y * 32; int edx = mapElement->base_height | ((mapElement->properties.scenerymultiple.type >> 10) << 8); - int esi, edi, ebp; - game_command_remove_large_scenery(&eax, &ebx, &ecx, &edx, &esi, &edi, &ebp); - cost = ebx; + int edi, ebp; + cost = game_do_command(eax, ebx, ecx, edx, GAME_COMMAND_REMOVE_LARGE_SCENERY, edi, ebp); if (cost == MONEY32_UNDEFINED) return MONEY32_UNDEFINED; @@ -1454,7 +1457,7 @@ restart_from_beginning: return totalCost; } -money32 map_clear_scenery(int x0, int y0, int x1, int y1, int flags) +money32 map_clear_scenery(int x0, int y0, int x1, int y1, int clear, int flags) { int x, y, z; money32 totalCost, cost; @@ -1479,7 +1482,7 @@ money32 map_clear_scenery(int x0, int y0, int x1, int y1, int flags) for (y = y0; y <= y1; y += 32) { for (x = x0; x <= x1; x += 32) { if ((RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_FLAGS, uint8) & SCREEN_FLAGS_SCENARIO_EDITOR) || gCheatsSandboxMode || map_is_location_owned_or_has_rights(x, y)) { - cost = map_clear_scenery_from_tile(x / 32, y / 32, flags); + cost = map_clear_scenery_from_tile(x / 32, y / 32, clear, flags); if (cost != MONEY32_UNDEFINED) { noValidTiles = false; totalCost += cost; @@ -1504,6 +1507,7 @@ void game_command_clear_scenery(int* eax, int* ebx, int* ecx, int* edx, int* esi (sint16)(*ecx & 0xFFFF), (sint16)(*edi & 0xFFFF), (sint16)(*ebp & 0xFFFF), + *edx, *ebx & 0xFF ); } diff --git a/src/world/map.h b/src/world/map.h index f163b82969..3f71c14227 100644 --- a/src/world/map.h +++ b/src/world/map.h @@ -301,7 +301,7 @@ int map_can_construct_with_clear_at(int x, int y, int zLow, int zHigh, void *cle int map_can_construct_at(int x, int y, int zLow, int zHigh, uint8 bl); void rotate_map_coordinates(sint16 *x, sint16 *y, int rotation); rct_xy16 coordinate_3d_to_2d(const rct_xyz16* coordinate_3d, int rotation); -money32 map_clear_scenery(int x0, int y0, int x1, int y1, int flags); +money32 map_clear_scenery(int x0, int y0, int x1, int y1, int clear, int flags); money32 lower_water(sint16 x0, sint16 y0, sint16 x1, sint16 y1, uint8 flags); money32 raise_water(sint16 x0, sint16 y0, sint16 x1, sint16 y1, uint8 flags); money32 map_place_fence(int type, int x, int y, int z, int edge, int primaryColour, int secondaryColour, int tertiaryColour, int flags); diff --git a/src/world/park.c b/src/world/park.c index 2cb4a1e96d..c0ebba6acc 100644 --- a/src/world/park.c +++ b/src/world/park.c @@ -539,7 +539,7 @@ void park_update() return; // Every 5 seconds approximately - if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint8) % 512 == 0) { + if (RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_TICKS, uint32) % 512 == 0) { RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_PARK_RATING, uint16) = calculate_park_rating(); RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_PARK_VALUE, money32) = calculate_park_value(); RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_COMPANY_VALUE, money32) = calculate_company_value();