Files
OpenRCT2-Web/src/openrct2/Game.cpp
T

1277 lines
36 KiB
C++
Raw Normal View History

2014-04-02 00:31:55 +01:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 OpenRCT2 developers
2015-10-09 18:22:37 +02:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2015-10-09 18:22:37 +02:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-04-02 00:31:55 +01:00
*****************************************************************************/
2015-10-09 18:22:37 +02:00
2018-06-22 23:25:16 +02:00
#include "Game.h"
2017-12-13 13:02:24 +01:00
#include "Cheats.h"
2017-03-26 21:42:07 +01:00
#include "Context.h"
2018-06-22 23:25:16 +02:00
#include "Editor.h"
#include "FileClassifier.h"
#include "Input.h"
#include "OpenRCT2.h"
#include "ParkImporter.h"
#include "ReplayManager.h"
2019-02-21 09:34:30 +00:00
#include "actions/LoadOrQuitAction.hpp"
2018-06-22 23:25:16 +02:00
#include "audio/audio.h"
#include "config/Config.h"
#include "core/FileScanner.h"
2017-06-08 23:02:39 +01:00
#include "interface/Screenshot.h"
2018-01-05 23:45:53 +00:00
#include "interface/Viewport.h"
2018-01-06 00:05:16 +00:00
#include "interface/Window.h"
2018-01-06 18:32:25 +01:00
#include "localisation/Localisation.h"
2017-10-06 22:37:06 +02:00
#include "management/Finance.h"
2017-10-11 21:38:26 +02:00
#include "management/Marketing.h"
#include "management/NewsItem.h"
2017-10-05 16:22:37 +02:00
#include "management/Research.h"
2015-02-12 11:30:57 +00:00
#include "network/network.h"
2018-01-02 20:36:42 +01:00
#include "object/Object.h"
2018-06-22 23:25:16 +02:00
#include "object/ObjectList.h"
2017-10-13 10:06:36 +02:00
#include "peep/Peep.h"
#include "peep/Staff.h"
2015-02-12 20:51:40 +00:00
#include "platform/platform.h"
2018-01-09 10:40:42 +01:00
#include "rct1/RCT1.h"
2017-12-31 13:21:34 +01:00
#include "ride/Ride.h"
2018-01-18 22:33:06 +00:00
#include "ride/RideRatings.h"
#include "ride/Station.h"
2017-10-17 13:51:47 +02:00
#include "ride/Track.h"
#include "ride/TrackDesign.h"
2017-10-11 23:32:15 +02:00
#include "ride/Vehicle.h"
2018-01-02 17:58:43 +00:00
#include "scenario/Scenario.h"
2016-11-21 00:16:47 +00:00
#include "title/TitleScreen.h"
2018-06-02 00:07:14 +01:00
#include "ui/UiContext.h"
#include "ui/WindowManager.h"
2017-12-13 13:02:24 +01:00
#include "util/SawyerCoding.h"
#include "util/Util.h"
2017-10-19 10:01:05 +02:00
#include "windows/Intent.h"
2017-12-14 10:34:12 +01:00
#include "world/Banner.h"
2017-03-11 11:24:18 +00:00
#include "world/Climate.h"
2017-12-14 10:34:12 +01:00
#include "world/Entrance.h"
2018-01-11 10:59:26 +01:00
#include "world/Footpath.h"
2018-01-03 14:56:08 +01:00
#include "world/Map.h"
2018-01-11 10:59:26 +01:00
#include "world/MapAnimation.h"
2017-12-31 21:40:00 +01:00
#include "world/Park.h"
2018-01-11 10:59:26 +01:00
#include "world/Scenery.h"
#include "world/Sprite.h"
2018-05-01 16:33:16 +02:00
#include "world/Surface.h"
2018-01-11 10:59:26 +01:00
#include "world/Water.h"
2018-06-22 23:25:16 +02:00
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-11-21 23:16:04 +01:00
#include <iterator>
2018-06-22 23:25:16 +02:00
#include <memory>
2016-01-04 16:22:15 +01:00
uint16_t gCurrentDeltaTime;
2018-06-22 23:25:16 +02:00
uint8_t gGamePaused = 0;
int32_t gGameSpeed = 1;
bool gDoSingleUpdate = false;
2018-06-22 23:25:16 +02:00
float gDayNightCycle = 0;
bool gInUpdateCode = false;
bool gInMapInitCode = false;
int32_t gGameCommandNestLevel;
2018-06-22 23:25:16 +02:00
bool gGameCommandIsNetworked;
std::string gCurrentLoadedPath;
2014-07-26 10:30:55 +01:00
2017-10-30 07:07:01 -04:00
bool gLoadKeepWindowsOpen = false;
uint8_t gUnk13CA740;
uint8_t gUnk141F568;
2016-09-10 14:46:53 +01:00
uint32_t gCurrentTicks;
uint32_t gCurrentRealTimeTicks;
2016-09-03 20:25:19 +00:00
// clang-format off
2017-12-20 14:35:43 +01:00
GAME_COMMAND_CALLBACK_POINTER * game_command_callback = nullptr;
2017-11-30 18:19:26 +01:00
static GAME_COMMAND_CALLBACK_POINTER * const game_command_callback_table[] = {
2017-12-20 14:35:43 +01:00
nullptr,
nullptr,
2019-02-15 14:10:48 +00:00
nullptr,
nullptr,
2019-02-20 15:52:27 +00:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_callback_place_banner,
2019-02-28 11:00:31 +00:00
nullptr,
2019-03-11 13:44:34 +01:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_callback_pickup_guest,
2018-03-15 22:44:24 +00:00
game_command_callback_pickup_staff
2015-07-23 17:22:03 -06:00
};
// clang-format on
int32_t game_command_playerid = -1;
2015-07-23 17:22:03 -06:00
rct_string_id gGameCommandErrorTitle;
rct_string_id gGameCommandErrorText;
2018-06-22 23:25:16 +02:00
uint8_t gErrorType;
2016-07-14 14:07:49 +02:00
rct_string_id gErrorStringId;
2017-07-22 23:19:09 +00:00
using namespace OpenRCT2;
2018-06-22 23:25:16 +02:00
int32_t game_command_callback_get_index(GAME_COMMAND_CALLBACK_POINTER* callback)
2015-07-23 17:22:03 -06:00
{
2018-11-21 23:16:04 +01:00
for (uint32_t i = 0; i < std::size(game_command_callback_table); i++)
2017-11-30 18:19:26 +01:00
{
if (game_command_callback_table[i] == callback)
{
2017-06-06 23:24:18 +02:00
return i;
}
}
return 0;
2015-07-23 17:22:03 -06:00
}
2018-06-22 23:25:16 +02:00
GAME_COMMAND_CALLBACK_POINTER* game_command_callback_get_callback(uint32_t index)
2015-07-23 17:22:03 -06:00
{
2018-11-21 23:16:04 +01:00
if (index < std::size(game_command_callback_table))
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
return game_command_callback_table[index];
}
2017-12-20 14:35:43 +01:00
return nullptr;
2015-07-23 17:22:03 -06:00
}
void game_increase_game_speed()
{
2018-06-20 17:11:35 +02:00
gGameSpeed = std::min(gConfigGeneral.debugging_tools ? 5 : 4, gGameSpeed + 1);
2017-06-06 23:24:18 +02:00
if (gGameSpeed == 5)
gGameSpeed = 8;
window_invalidate_by_class(WC_TOP_TOOLBAR);
}
void game_reduce_game_speed()
{
2018-06-20 17:11:35 +02:00
gGameSpeed = std::max(1, gGameSpeed - 1);
2017-06-06 23:24:18 +02:00
if (gGameSpeed == 7)
gGameSpeed = 4;
window_invalidate_by_class(WC_TOP_TOOLBAR);
}
2014-04-11 02:42:39 +01:00
/**
2015-10-09 18:22:37 +02:00
*
2014-04-11 02:42:39 +01:00
* rct2: 0x0066B5C0 (part of 0x0066B3E8)
*/
void game_create_windows()
{
2017-08-12 23:06:12 +02:00
context_open_window(WC_MAIN_WINDOW);
2017-11-11 02:57:45 +01:00
context_open_window(WC_TOP_TOOLBAR);
2017-11-15 02:11:25 +01:00
context_open_window(WC_BOTTOM_TOOLBAR);
2017-06-06 23:24:18 +02:00
window_resize_gui(context_get_width(), context_get_height());
2014-04-11 02:42:39 +01:00
}
2017-11-30 18:19:26 +01:00
enum
{
2018-06-22 23:25:16 +02:00
SPR_GAME_PALETTE_DEFAULT = 1532,
SPR_GAME_PALETTE_WATER = 1533,
2017-06-06 23:24:18 +02:00
SPR_GAME_PALETTE_WATER_DARKER_1 = 1534,
SPR_GAME_PALETTE_WATER_DARKER_2 = 1535,
2018-06-22 23:25:16 +02:00
SPR_GAME_PALETTE_3 = 1536,
SPR_GAME_PALETTE_3_DARKER_1 = 1537,
SPR_GAME_PALETTE_3_DARKER_2 = 1538,
SPR_GAME_PALETTE_4 = 1539,
SPR_GAME_PALETTE_4_DARKER_1 = 1540,
SPR_GAME_PALETTE_4_DARKER_2 = 1541,
2016-11-10 13:21:47 +01:00
};
/**
2018-06-22 23:25:16 +02:00
*
* rct2: 0x006838BD
*/
2014-09-23 13:33:55 -06:00
void update_palette_effects()
{
2018-06-22 23:25:16 +02:00
auto water_type = (rct_water_type*)object_entry_get_chunk(OBJECT_TYPE_WATER, 0);
2017-11-30 18:19:26 +01:00
if (gClimateLightningFlash == 1)
{
2017-06-06 23:24:18 +02:00
// Change palette to lighter colour during lightning
int32_t palette = SPR_GAME_PALETTE_DEFAULT;
2017-11-30 18:21:12 +01:00
if (water_type != nullptr)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
palette = water_type->image_id;
}
2018-06-22 23:25:16 +02:00
const rct_g1_element* g1 = gfx_get_g1_element(palette);
2017-11-30 18:21:12 +01:00
if (g1 != nullptr)
2017-10-26 13:14:37 +01:00
{
int32_t xoffset = g1->x_offset;
2017-10-26 13:14:37 +01:00
xoffset = xoffset * 4;
2018-06-22 23:25:16 +02:00
uint8_t* paletteOffset = gGamePalette + xoffset;
for (int32_t i = 0; i < g1->width; i++)
2017-10-26 13:14:37 +01:00
{
paletteOffset[(i * 4) + 0] = -((0xFF - g1->offset[(i * 3) + 0]) / 2) - 1;
paletteOffset[(i * 4) + 1] = -((0xFF - g1->offset[(i * 3) + 1]) / 2) - 1;
paletteOffset[(i * 4) + 2] = -((0xFF - g1->offset[(i * 3) + 2]) / 2) - 1;
}
platform_update_palette(gGamePalette, 10, 236);
2017-06-06 23:24:18 +02:00
}
gClimateLightningFlash++;
2017-11-30 18:19:26 +01:00
}
else
{
if (gClimateLightningFlash == 2)
{
2017-06-06 23:24:18 +02:00
// Change palette back to normal after lightning
int32_t palette = SPR_GAME_PALETTE_DEFAULT;
2017-11-30 18:21:12 +01:00
if (water_type != nullptr)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
palette = water_type->image_id;
}
2018-06-22 23:25:16 +02:00
const rct_g1_element* g1 = gfx_get_g1_element(palette);
2017-11-30 18:21:12 +01:00
if (g1 != nullptr)
2017-10-26 13:14:37 +01:00
{
int32_t xoffset = g1->x_offset;
2017-10-26 13:14:37 +01:00
xoffset = xoffset * 4;
2018-06-22 23:25:16 +02:00
uint8_t* paletteOffset = gGamePalette + xoffset;
for (int32_t i = 0; i < g1->width; i++)
2017-10-26 13:14:37 +01:00
{
paletteOffset[(i * 4) + 0] = g1->offset[(i * 3) + 0];
paletteOffset[(i * 4) + 1] = g1->offset[(i * 3) + 1];
paletteOffset[(i * 4) + 2] = g1->offset[(i * 3) + 2];
}
2017-06-06 23:24:18 +02:00
}
}
2017-06-06 23:24:18 +02:00
// Animate the water/lava/chain movement palette
uint32_t shade = 0;
2017-11-30 18:19:26 +01:00
if (gConfigGeneral.render_weather_gloom)
{
2018-01-12 22:53:41 +00:00
auto paletteId = climate_get_weather_gloom_palette_id(gClimateCurrent);
if (paletteId != PALETTE_NULL)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
shade = 1;
if (paletteId != PALETTE_DARKEN_1)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
shade = 2;
}
}
}
uint32_t j = gPaletteEffectFrame;
2018-06-22 23:25:16 +02:00
j = (((uint16_t)((~j / 2) * 128) * 15) >> 16);
uint32_t waterId = SPR_GAME_PALETTE_WATER;
2017-11-30 18:21:12 +01:00
if (water_type != nullptr)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
waterId = water_type->palette_index_1;
}
2018-06-22 23:25:16 +02:00
const rct_g1_element* g1 = gfx_get_g1_element(shade + waterId);
2017-11-30 18:21:12 +01:00
if (g1 != nullptr)
2017-10-26 13:14:37 +01:00
{
2018-06-22 23:25:16 +02:00
uint8_t* vs = &g1->offset[j * 3];
uint8_t* vd = &gGamePalette[230 * 4];
int32_t n = 5;
for (int32_t i = 0; i < n; i++)
2017-10-26 13:14:37 +01:00
{
vd[0] = vs[0];
vd[1] = vs[1];
vd[2] = vs[2];
vs += 9;
if (vs >= &g1->offset[9 * n])
{
vs -= 9 * n;
}
vd += 4;
2017-06-06 23:24:18 +02:00
}
}
2017-06-06 23:24:18 +02:00
waterId = SPR_GAME_PALETTE_3;
2017-11-30 18:21:12 +01:00
if (water_type != nullptr)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
waterId = water_type->palette_index_2;
}
2017-11-30 18:24:44 +01:00
g1 = gfx_get_g1_element(shade + waterId);
2017-11-30 18:21:12 +01:00
if (g1 != nullptr)
2017-10-26 13:14:37 +01:00
{
2018-06-22 23:25:16 +02:00
uint8_t* vs = &g1->offset[j * 3];
uint8_t* vd = &gGamePalette[235 * 4];
int32_t n = 5;
for (int32_t i = 0; i < n; i++)
2017-10-26 13:14:37 +01:00
{
vd[0] = vs[0];
vd[1] = vs[1];
vd[2] = vs[2];
vs += 9;
if (vs >= &g1->offset[9 * n])
{
vs -= 9 * n;
}
vd += 4;
2017-06-06 23:24:18 +02:00
}
}
2018-06-22 23:25:16 +02:00
j = ((uint16_t)(gPaletteEffectFrame * -960) * 3) >> 16;
2017-06-06 23:24:18 +02:00
waterId = SPR_GAME_PALETTE_4;
2018-06-22 23:25:16 +02:00
g1 = gfx_get_g1_element(shade + waterId);
2017-11-30 18:21:12 +01:00
if (g1 != nullptr)
2017-10-26 13:14:37 +01:00
{
2018-06-22 23:25:16 +02:00
uint8_t* vs = &g1->offset[j * 3];
uint8_t* vd = &gGamePalette[243 * 4];
int32_t n = 3;
for (int32_t i = 0; i < n; i++)
2017-10-26 13:14:37 +01:00
{
vd[0] = vs[0];
vd[1] = vs[1];
vd[2] = vs[2];
vs += 3;
2017-11-30 18:19:26 +01:00
if (vs >= &g1->offset[3 * n])
{
2017-10-26 13:14:37 +01:00
vs -= 3 * n;
}
vd += 4;
2017-06-06 23:24:18 +02:00
}
}
2017-06-06 23:24:18 +02:00
platform_update_palette(gGamePalette, 230, 16);
2017-11-30 18:19:26 +01:00
if (gClimateLightningFlash == 2)
{
2017-06-06 23:24:18 +02:00
platform_update_palette(gGamePalette, 10, 236);
gClimateLightningFlash = 0;
}
}
}
2014-05-02 02:12:14 +01:00
/**
2015-10-09 18:22:37 +02:00
*
* rct2: 0x0069C62C
2014-05-02 02:12:14 +01:00
*
* @param cost (ebp)
*/
2019-02-10 23:24:40 +01:00
static int32_t game_check_affordability(int32_t cost, uint32_t flags)
2014-05-02 02:12:14 +01:00
{
2019-02-10 23:24:40 +01:00
// Only checked for game commands.
2017-11-30 18:19:26 +01:00
if (gUnk141F568 & 0xF0)
return cost;
2019-02-10 23:24:40 +01:00
if (finance_check_affordability(cost, flags))
2017-11-30 18:19:26 +01:00
return cost;
2015-08-16 13:05:49 -04:00
set_format_arg(0, uint32_t, cost);
2017-06-06 23:24:18 +02:00
gGameCommandErrorText = STR_NOT_ENOUGH_CASH_REQUIRES;
return MONEY32_UNDEFINED;
2014-05-02 02:12:14 +01:00
}
/**
2015-10-09 18:22:37 +02:00
*
2014-05-02 02:12:14 +01:00
* rct2: 0x006677F2
*
* @param ebx flags
* @param esi command
2014-05-02 02:12:14 +01:00
*/
int32_t game_do_command(int32_t eax, int32_t ebx, int32_t ecx, int32_t edx, int32_t esi, int32_t edi, int32_t ebp)
2014-05-02 02:12:14 +01:00
{
2017-06-06 23:24:18 +02:00
return game_do_command_p(esi, &eax, &ebx, &ecx, &edx, &esi, &edi, &ebp);
2014-05-02 22:21:08 +01:00
}
2014-08-05 19:15:28 +02:00
/**
2018-06-22 23:25:16 +02:00
*
* rct2: 0x006677F2 with pointers as arguments
*
* @param ebx flags
* @param esi command
*/
int32_t game_do_command_p(
uint32_t command, int32_t* eax, int32_t* ebx, int32_t* ecx, int32_t* edx, int32_t* esi, int32_t* edi, int32_t* ebp)
2014-08-05 19:15:28 +02:00
{
int32_t cost, flags;
int32_t original_ebx, original_edx, original_esi, original_edi, original_ebp;
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
*esi = command;
original_ebx = *ebx;
original_edx = *edx;
original_esi = *esi;
original_edi = *edi;
original_ebp = *ebp;
2014-08-05 19:15:28 +02:00
2018-11-21 23:16:04 +01:00
if (command >= std::size(new_game_command_table))
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
return MONEY32_UNDEFINED;
}
2016-01-20 15:45:09 -07:00
2017-06-06 23:24:18 +02:00
flags = *ebx;
2014-08-05 19:15:28 +02:00
auto* replayManager = GetContext()->GetReplayManager();
2018-12-28 20:31:07 +01:00
if (replayManager->IsReplaying())
{
// We only accept replay commands as long the replay is active.
if ((flags & GAME_COMMAND_FLAG_REPLAY) == 0)
{
// TODO: Introduce proper error.
gGameCommandErrorText = STR_CHEAT_BUILD_IN_PAUSE_MODE;
return MONEY32_UNDEFINED;
}
}
2017-11-30 18:19:26 +01:00
if (gGameCommandNestLevel == 0)
{
2018-06-22 23:25:16 +02:00
gGameCommandErrorText = STR_NONE;
2017-06-06 23:24:18 +02:00
gGameCommandIsNetworked = (flags & GAME_COMMAND_FLAG_NETWORKED) != 0;
}
2016-11-13 20:17:49 +01:00
2017-06-06 23:24:18 +02:00
// Increment nest count
gGameCommandNestLevel++;
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
// Remove ghost scenery so it doesn't interfere with incoming network command
2019-04-06 08:01:22 +01:00
if ((flags & GAME_COMMAND_FLAG_NETWORKED) && !(flags & GAME_COMMAND_FLAG_GHOST) && (command == GAME_COMMAND_PLACE_BANNER))
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
scenery_remove_ghost_tool_placement();
}
2015-07-18 10:49:08 -06:00
2017-11-30 18:19:26 +01:00
if (game_command_playerid == -1)
{
2017-06-06 23:24:18 +02:00
game_command_playerid = network_get_current_player_id();
}
2016-01-21 23:32:40 -07:00
2017-06-06 23:24:18 +02:00
// Log certain commands if we are in multiplayer and logging is enabled
bool serverLog = (network_get_mode() == NETWORK_MODE_SERVER) && gGameCommandNestLevel == 1
&& gConfigNetwork.log_server_actions;
2018-06-22 23:25:16 +02:00
bool clientLog = (network_get_mode() == NETWORK_MODE_CLIENT) && (flags & GAME_COMMAND_FLAG_NETWORKED)
&& gGameCommandNestLevel == 1 && gConfigNetwork.log_server_actions;
2017-11-30 18:19:26 +01:00
if (serverLog || clientLog)
{
2017-06-06 23:24:18 +02:00
game_log_multiplayer_command(command, eax, ebx, ecx, edx, edi, ebp);
}
2017-06-06 23:24:18 +02:00
*ebx &= ~GAME_COMMAND_FLAG_APPLY;
2015-10-09 18:22:37 +02:00
// Make sure the camera position won't change if the command skips setting them.
gCommandPosition.x = LOCATION_NULL;
gCommandPosition.y = LOCATION_NULL;
gCommandPosition.z = LOCATION_NULL;
2017-06-06 23:24:18 +02:00
// First call for validity and price check
new_game_command_table[command](eax, ebx, ecx, edx, esi, edi, ebp);
cost = *ebx;
2014-08-05 19:15:28 +02:00
2017-11-30 18:19:26 +01:00
if (cost != MONEY32_UNDEFINED)
{
2017-06-06 23:24:18 +02:00
// Check funds
int32_t insufficientFunds = 0;
2019-04-01 18:58:16 +01:00
if (gGameCommandNestLevel == 1 && !(flags & GAME_COMMAND_FLAG_NO_SPEND) && cost != 0)
2019-02-10 23:24:40 +01:00
insufficientFunds = game_check_affordability(cost, flags);
2014-08-05 19:15:28 +02:00
2017-11-30 18:19:26 +01:00
if (insufficientFunds != MONEY32_UNDEFINED)
{
2017-06-06 23:24:18 +02:00
*ebx = original_ebx;
*edx = original_edx;
*esi = original_esi;
*edi = original_edi;
*ebp = original_ebp;
2014-08-05 19:15:28 +02:00
2017-11-30 18:19:26 +01:00
if (!(flags & GAME_COMMAND_FLAG_APPLY))
{
2017-06-06 23:24:18 +02:00
// Decrement nest count
gGameCommandNestLevel--;
return cost;
}
2014-08-05 19:15:28 +02:00
2018-06-22 23:25:16 +02:00
if (network_get_mode() != NETWORK_MODE_NONE && !(flags & GAME_COMMAND_FLAG_NETWORKED)
2019-04-01 18:58:16 +01:00
&& !(flags & GAME_COMMAND_FLAG_GHOST) && !(flags & GAME_COMMAND_FLAG_NO_SPEND)
2018-06-22 23:25:16 +02:00
&& gGameCommandNestLevel == 1) /* Send only top-level commands */
2017-11-30 18:19:26 +01:00
{
2019-02-21 09:37:56 +00:00
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)
2017-12-03 10:38:27 +01:00
{
2019-02-21 09:37:56 +00:00
// Client sent the command to the server, do not run it locally, just return. It will run when server
// sends it.
game_command_callback = nullptr;
// Decrement nest count
gGameCommandNestLevel--;
return cost;
2017-06-06 23:24:18 +02:00
}
}
2015-07-08 20:19:12 -06:00
2017-06-06 23:24:18 +02:00
// Second call to actually perform the operation
new_game_command_table[command](eax, ebx, ecx, edx, esi, edi, ebp);
2015-07-23 17:22:03 -06:00
2018-12-11 08:13:47 +01:00
if (replayManager != nullptr)
{
2018-12-11 08:13:47 +01:00
bool recordCommand = false;
bool commandExecutes = (flags & GAME_COMMAND_FLAG_APPLY) && (flags & GAME_COMMAND_FLAG_GHOST) == 0
2019-04-01 18:58:16 +01:00
&& (flags & GAME_COMMAND_FLAG_NO_SPEND) == 0;
2018-12-11 08:13:47 +01:00
if (replayManager->IsRecording() && commandExecutes)
recordCommand = true;
else if (replayManager->IsNormalising() && commandExecutes && (flags & GAME_COMMAND_FLAG_REPLAY) != 0)
recordCommand = true;
if (recordCommand && gGameCommandNestLevel == 1)
2018-12-11 08:13:47 +01:00
{
int32_t callback = game_command_callback_get_index(game_command_callback);
replayManager->AddGameCommand(
gCurrentTicks, *eax, original_ebx, *ecx, original_edx, original_esi, original_edi, original_ebp,
callback);
}
}
2017-06-06 23:24:18 +02:00
// Do the callback (required for multiplayer to work correctly), but only for top level commands
2017-11-30 18:19:26 +01:00
if (gGameCommandNestLevel == 1)
{
2017-06-17 13:41:40 +02:00
if (game_command_callback && !(flags & GAME_COMMAND_FLAG_GHOST))
{
2017-06-06 23:24:18 +02:00
game_command_callback(*eax, *ebx, *ecx, *edx, *esi, *edi, *ebp);
2017-12-20 14:35:43 +01:00
game_command_callback = nullptr;
2017-06-06 23:24:18 +02:00
}
}
2015-07-23 17:22:03 -06:00
2017-06-06 23:24:18 +02:00
game_command_playerid = -1;
2016-01-23 17:33:08 -07:00
2017-06-06 23:24:18 +02:00
*edx = *ebx;
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
if (*edx != MONEY32_UNDEFINED && *edx < cost)
cost = *edx;
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
// Decrement nest count
gGameCommandNestLevel--;
if (gGameCommandNestLevel != 0)
return cost;
2014-08-05 19:15:28 +02:00
2019-02-10 23:24:40 +01:00
// Check if money is required.
if (finance_check_money_required(flags))
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
// Update money balance
finance_payment(cost, gCommandExpenditureType);
2017-11-30 18:19:26 +01:00
if (gUnk141F568 == gUnk13CA740)
{
2017-06-06 23:24:18 +02:00
// Create a +/- money text effect
2017-06-29 17:42:36 +01:00
if (cost != 0 && game_is_not_paused())
2017-06-06 23:24:18 +02:00
money_effect_create(cost);
}
}
2014-08-05 19:15:28 +02:00
2018-06-22 23:25:16 +02:00
if (network_get_mode() == NETWORK_MODE_SERVER && !(flags & GAME_COMMAND_FLAG_NETWORKED)
&& !(flags & GAME_COMMAND_FLAG_GHOST))
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
network_set_player_last_action(network_get_player_index(network_get_current_player_id()), command);
network_add_player_money_spent(network_get_current_player_id(), cost);
}
2016-01-20 15:45:09 -07:00
2017-06-06 23:24:18 +02:00
// Start autosave timer after game command
if (gLastAutoSaveUpdate == AUTOSAVE_PAUSE)
gLastAutoSaveUpdate = platform_get_ticks();
2017-06-06 23:24:18 +02:00
return cost;
}
}
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
// Error occurred
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
// Decrement nest count
gGameCommandNestLevel--;
2016-11-13 20:17:49 +01:00
2017-06-06 23:24:18 +02:00
// Clear the game command callback to prevent the next command triggering it
2017-12-20 14:35:43 +01:00
game_command_callback = nullptr;
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
// Show error window
2018-06-22 23:25:16 +02:00
if (gGameCommandNestLevel == 0 && (flags & GAME_COMMAND_FLAG_APPLY) && gUnk141F568 == gUnk13CA740
&& !(flags & GAME_COMMAND_FLAG_ALLOW_DURING_PAUSED) && !(flags & GAME_COMMAND_FLAG_NETWORKED)
&& !(flags & GAME_COMMAND_FLAG_GHOST))
{
context_show_error(gGameCommandErrorTitle, gGameCommandErrorText);
}
2014-08-05 19:15:28 +02:00
2017-06-06 23:24:18 +02:00
return MONEY32_UNDEFINED;
2014-08-05 19:15:28 +02:00
}
2018-06-22 23:25:16 +02:00
void game_log_multiplayer_command(int command, const int* eax, const int* ebx, const int* ecx, int* edx, int* edi, int* ebp)
{
2017-06-06 23:24:18 +02:00
// Get player name
const char* player_name = "localhost";
2017-06-06 23:24:18 +02:00
int player_index = network_get_player_index(game_command_playerid);
if (player_index != -1)
{
player_name = network_get_player_name(player_index);
}
2017-06-06 23:24:18 +02:00
char log_msg[256];
2017-11-30 18:19:26 +01:00
if (command == GAME_COMMAND_CHEAT)
{
2017-06-06 23:24:18 +02:00
// Get cheat name
2018-06-22 23:25:16 +02:00
const char* cheat = cheats_get_cheat_string(*ecx, *edx, *edi);
char* args[2] = {
(char*)player_name,
(char*)cheat,
};
2017-06-06 23:24:18 +02:00
format_string(log_msg, 256, STR_LOG_CHEAT_USED, args);
network_append_server_log(log_msg);
2017-11-30 18:19:26 +01:00
}
else if (command == GAME_COMMAND_DEMOLISH_RIDE && (*ebp == 1 || *ebp == 0))
{ // ebp is 1 if command comes from ride window prompt, so we don't log "demolishing" ride previews
2017-06-06 23:24:18 +02:00
// Get ride name
2018-06-22 23:25:16 +02:00
Ride* ride = get_ride(*edx);
2017-06-06 23:24:18 +02:00
char ride_name[128];
format_string(ride_name, 128, ride->name, &ride->name_arguments);
char* args[2] = {
(char*)player_name,
ride_name,
};
2017-06-06 23:24:18 +02:00
format_string(log_msg, 256, STR_LOG_DEMOLISH_RIDE, args);
network_append_server_log(log_msg);
2017-11-30 18:19:26 +01:00
}
2019-04-05 19:16:23 +01:00
else if (command == GAME_COMMAND_PLACE_BANNER)
2017-11-30 18:19:26 +01:00
{
uint8_t flags = *ebx & 0xFF;
2017-11-30 18:19:26 +01:00
if (flags & GAME_COMMAND_FLAG_GHOST)
{
2017-06-06 23:24:18 +02:00
// Don't log ghost previews being removed
return;
}
2017-06-06 23:24:18 +02:00
// Log placing scenery
char* args[1] = {
(char*)player_name,
};
2017-06-06 23:24:18 +02:00
format_string(log_msg, 256, STR_LOG_PLACE_SCENERY, args);
network_append_server_log(log_msg);
2017-11-30 18:19:26 +01:00
}
else if (command == GAME_COMMAND_REMOVE_BANNER)
2017-11-30 18:19:26 +01:00
{
uint8_t flags = *ebx & 0xFF;
2017-11-30 18:19:26 +01:00
if (flags & GAME_COMMAND_FLAG_GHOST)
{
2017-06-06 23:24:18 +02:00
// Don't log ghost previews being removed
return;
}
2017-06-06 23:24:18 +02:00
// Log removing scenery
char* args[1] = {
(char*)player_name,
};
2017-06-06 23:24:18 +02:00
format_string(log_msg, 256, STR_LOG_REMOVE_SCENERY, args);
network_append_server_log(log_msg);
2017-11-30 18:19:26 +01:00
}
}
void pause_toggle()
{
2017-06-06 23:24:18 +02:00
gGamePaused ^= GAME_PAUSED_NORMAL;
window_invalidate_by_class(WC_TOP_TOOLBAR);
2017-11-30 18:19:26 +01:00
if (gGamePaused & GAME_PAUSED_NORMAL)
{
audio_stop_all_music_and_sounds();
2017-06-06 23:24:18 +02:00
}
}
2014-08-05 19:15:28 +02:00
2016-04-23 13:34:55 +01:00
bool game_is_paused()
{
2017-06-06 23:24:18 +02:00
return gGamePaused != 0;
2016-04-23 13:34:55 +01:00
}
bool game_is_not_paused()
{
2017-06-06 23:24:18 +02:00
return gGamePaused == 0;
2016-04-23 13:34:55 +01:00
}
2014-05-02 22:21:08 +01:00
/**
2014-11-20 18:40:10 +00:00
*
2014-05-02 22:21:08 +01:00
* rct2: 0x0066DC0F
*/
static void load_landscape()
{
2018-02-05 22:59:44 +01:00
auto intent = Intent(WC_LOADSAVE);
intent.putExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_LOAD | LOADSAVETYPE_LANDSCAPE);
context_open_intent(&intent);
2014-05-02 22:21:08 +01:00
}
2018-06-22 23:25:16 +02:00
void utf8_to_rct2_self(char* buffer, size_t length)
{
auto temp = utf8_to_rct2(buffer);
2016-11-13 20:17:49 +01:00
2018-06-22 23:25:16 +02:00
size_t i = 0;
const char* src = temp.data();
char* dst = buffer;
2017-11-30 18:19:26 +01:00
while (*src != 0 && i < length - 1)
{
2018-06-22 23:25:16 +02:00
if (*src == (char)(uint8_t)0xFF)
2017-11-30 18:19:26 +01:00
{
if (i < length - 3)
{
2017-06-06 23:24:18 +02:00
*dst++ = *src++;
*dst++ = *src++;
*dst++ = *src++;
2017-11-30 18:19:26 +01:00
}
else
{
2017-06-06 23:24:18 +02:00
break;
}
i += 3;
2017-11-30 18:19:26 +01:00
}
else
{
2017-06-06 23:24:18 +02:00
*dst++ = *src++;
i++;
}
}
2017-11-30 18:19:26 +01:00
do
{
2017-06-06 23:24:18 +02:00
*dst++ = '\0';
i++;
2018-06-22 23:25:16 +02:00
} while (i < length);
}
2018-06-22 23:25:16 +02:00
void rct2_to_utf8_self(char* buffer, size_t length)
{
2017-11-30 18:19:26 +01:00
if (length > 0)
{
auto temp = rct2_to_utf8(buffer, RCT2_LANGUAGE_ID_ENGLISH_UK);
safe_strcpy(buffer, temp.data(), length);
2017-06-06 23:24:18 +02:00
}
}
2015-07-29 23:57:43 +01:00
/**
* Converts all the user strings and news item strings to UTF-8.
*/
void game_convert_strings_to_utf8()
{
2017-06-06 23:24:18 +02:00
// Scenario details
gScenarioCompletedBy = rct2_to_utf8(gScenarioCompletedBy, RCT2_LANGUAGE_ID_ENGLISH_UK);
gScenarioName = rct2_to_utf8(gScenarioName, RCT2_LANGUAGE_ID_ENGLISH_UK);
gScenarioDetails = rct2_to_utf8(gScenarioDetails, RCT2_LANGUAGE_ID_ENGLISH_UK);
2015-07-29 23:57:43 +01:00
2017-06-06 23:24:18 +02:00
// User strings
2018-06-22 23:25:16 +02:00
for (auto* string : gUserStrings)
2017-11-23 15:52:06 +01:00
{
2017-12-20 02:50:18 +01:00
if (!str_is_null_or_empty(string))
2017-11-23 15:52:06 +01:00
{
2017-12-20 02:50:18 +01:00
rct2_to_utf8_self(string, RCT12_USER_STRING_MAX_LENGTH);
utf8_remove_formatting(string, true);
2017-06-06 23:24:18 +02:00
}
}
2015-07-29 23:57:43 +01:00
// News items
game_convert_news_items_to_utf8();
}
void game_convert_news_items_to_utf8()
{
for (int32_t i = 0; i < MAX_NEWS_ITEMS; i++)
2017-11-30 18:19:26 +01:00
{
2018-06-22 23:25:16 +02:00
NewsItem* newsItem = news_item_get(i);
2017-11-30 18:19:26 +01:00
if (!str_is_null_or_empty(newsItem->Text))
{
rct2_to_utf8_self(newsItem->Text, sizeof(newsItem->Text));
}
}
2015-07-29 23:57:43 +01:00
}
/**
* Converts all the user strings and news item strings to RCT2 encoding.
*/
2018-06-22 23:25:16 +02:00
void game_convert_strings_to_rct2(rct_s6_data* s6)
2015-07-29 23:57:43 +01:00
{
2017-06-06 23:24:18 +02:00
// Scenario details
utf8_to_rct2_self(s6->scenario_completed_name, sizeof(s6->scenario_completed_name));
utf8_to_rct2_self(s6->scenario_name, sizeof(s6->scenario_name));
utf8_to_rct2_self(s6->scenario_description, sizeof(s6->scenario_description));
2015-07-29 23:57:43 +01:00
2017-06-06 23:24:18 +02:00
// User strings
2018-06-22 23:25:16 +02:00
for (auto* userString : s6->custom_strings)
2017-11-23 15:52:06 +01:00
{
if (!str_is_null_or_empty(userString))
{
utf8_to_rct2_self(userString, RCT12_USER_STRING_MAX_LENGTH);
2017-06-06 23:24:18 +02:00
}
}
2015-07-29 23:57:43 +01:00
2017-06-06 23:24:18 +02:00
// News items
2018-06-22 23:25:16 +02:00
for (auto& newsItem : s6->news_items)
2017-11-30 18:19:26 +01:00
{
2017-12-20 02:50:18 +01:00
if (!str_is_null_or_empty(newsItem.Text))
2017-11-30 18:19:26 +01:00
{
2017-12-20 02:50:18 +01:00
utf8_to_rct2_self(newsItem.Text, sizeof(newsItem.Text));
2017-06-06 23:24:18 +02:00
}
}
2015-07-29 23:57:43 +01:00
}
2015-10-07 20:53:55 +02:00
// OpenRCT2 workaround to recalculate some values which are saved redundantly in the save to fix corrupted files.
// For example recalculate guest count by looking at all the guests instead of trusting the value in the file.
void game_fix_save_vars()
{
2017-06-06 23:24:18 +02:00
// Recalculates peep count after loading a save to fix corrupted files
2019-02-28 19:28:58 +00:00
Peep* peep;
uint16_t spriteIndex;
uint16_t peepCount = 0;
2018-06-22 23:25:16 +02:00
FOR_ALL_GUESTS (spriteIndex, peep)
2017-11-30 18:24:44 +01:00
{
if (!peep->outside_of_park)
peepCount++;
}
2015-10-07 20:53:55 +02:00
2017-06-06 23:24:18 +02:00
gNumGuestsInPark = peepCount;
2017-06-06 23:24:18 +02:00
peep_sort();
2016-05-08 01:39:59 +02:00
// Peeps to remove have to be cached here, as removing them from within the loop breaks iteration
2019-02-28 19:28:58 +00:00
std::vector<Peep*> peepsToRemove;
// Fix possibly invalid field values
2018-06-22 23:25:16 +02:00
FOR_ALL_GUESTS (spriteIndex, peep)
{
if (peep->current_ride_station >= MAX_STATIONS)
{
const uint8_t srcStation = peep->current_ride_station;
const uint8_t rideIdx = peep->current_ride;
if (rideIdx == RIDE_ID_NULL)
{
continue;
}
set_format_arg(0, uint32_t, peep->id);
2018-06-22 23:25:16 +02:00
utf8* curName = gCommonStringFormatBuffer;
rct_string_id curId = peep->name_string_idx;
format_string(curName, 256, curId, gCommonFormatArgs);
log_warning("Peep %u (%s) has invalid ride station = %u for ride %u.", spriteIndex, curName, srcStation, rideIdx);
int8_t station = ride_get_first_valid_station_exit(get_ride(rideIdx));
if (station == -1)
{
log_warning("Couldn't find station, removing peep %u", spriteIndex);
peepsToRemove.push_back(peep);
2018-06-22 23:25:16 +02:00
}
else
{
log_warning("Amending ride station to %u.", station);
peep->current_ride_station = station;
}
}
}
if (peepsToRemove.size() > 0)
{
// Some broken saves have broken spatial indexes
reset_sprite_spatial_index();
}
for (auto ptr : peepsToRemove)
{
2018-03-27 21:04:57 +01:00
ptr->Remove();
}
2017-06-06 23:24:18 +02:00
// Fixes broken saves where a surface element could be null
// and broken saves with incorrect invisible map border tiles
for (int32_t y = 0; y < MAXIMUM_MAP_SIZE_TECHNICAL; y++)
2017-11-30 18:19:26 +01:00
{
for (int32_t x = 0; x < MAXIMUM_MAP_SIZE_TECHNICAL; x++)
2017-11-30 18:19:26 +01:00
{
2018-11-01 13:53:50 +01:00
TileElement* tileElement = map_get_surface_element_at(x, y);
2017-11-30 18:21:12 +01:00
if (tileElement == nullptr)
2017-06-06 23:24:18 +02:00
{
log_error("Null map element at x = %d and y = %d. Fixing...", x, y);
2017-10-31 14:03:45 +01:00
tileElement = tile_element_insert(x, y, 14, 0);
2017-11-30 18:21:12 +01:00
if (tileElement == nullptr)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
log_error("Unable to fix: Map element limit reached.");
return;
}
}
// Fix the invisible border tiles.
2017-10-31 14:03:45 +01:00
// At this point, we can be sure that tileElement is not NULL.
2017-11-30 18:19:26 +01:00
if (x == 0 || x == gMapSize - 1 || y == 0 || y == gMapSize - 1)
{
2018-06-22 23:25:16 +02:00
tileElement->base_height = 2;
tileElement->clearance_height = 2;
2018-09-14 14:54:12 +02:00
tileElement->AsSurface()->SetSlope(0);
tileElement->AsSurface()->SetWaterHeight(0);
}
2017-06-06 23:24:18 +02:00
}
}
research_fix();
// Fix banner list pointing to NULL map elements
banner_reset_broken_index();
// Fix banners which share their index
fix_duplicated_banners();
// Fix invalid vehicle sprite sizes, thus preventing visual corruption of sprites
fix_invalid_vehicle_sprite_sizes();
2017-10-31 12:57:40 +01:00
// Fix gParkEntrance locations for which the tile_element no longer exists
fix_park_entrance_locations();
2015-10-07 20:53:55 +02:00
}
2015-07-05 09:19:01 -06:00
void game_load_init()
{
2018-06-22 23:25:16 +02:00
rct_window* mainWindow;
2015-07-05 09:19:01 -06:00
2017-06-06 23:24:18 +02:00
gScreenFlags = SCREEN_FLAGS_PLAYING;
audio_stop_all_music_and_sounds();
2017-10-30 07:07:01 -04:00
if (!gLoadKeepWindowsOpen)
{
viewport_init_all();
game_create_windows();
2017-11-02 20:16:30 -04:00
mainWindow = window_get_main();
}
else
{
mainWindow = window_get_main();
window_unfollow_sprite(mainWindow);
2017-10-30 07:07:01 -04:00
}
2014-05-04 16:21:15 +01:00
2018-06-02 00:07:14 +01:00
auto windowManager = GetContext()->GetUiContext()->GetWindowManager();
windowManager->SetMainView(gSavedViewX, gSavedViewY, gSavedViewZoom, gSavedViewRotation);
2014-05-04 16:21:15 +01:00
2017-06-06 23:24:18 +02:00
if (network_get_mode() != NETWORK_MODE_CLIENT)
{
reset_sprite_spatial_index();
}
reset_all_sprite_quadrant_placements();
scenery_set_default_placement_configuration();
2017-10-07 01:28:00 +02:00
2018-02-05 22:59:44 +01:00
auto intent = Intent(INTENT_ACTION_REFRESH_NEW_RIDES);
context_broadcast_intent(&intent);
2017-10-07 01:28:00 +02:00
2017-06-06 23:24:18 +02:00
gWindowUpdateTicks = 0;
2014-05-04 16:21:15 +01:00
2017-06-06 23:24:18 +02:00
load_palette();
2017-11-30 18:19:26 +01:00
if (!gOpenRCT2Headless)
{
2018-02-05 22:59:44 +01:00
intent = Intent(INTENT_ACTION_CLEAR_TILE_INSPECTOR_CLIPBOARD);
context_broadcast_intent(&intent);
2017-06-06 23:24:18 +02:00
window_update_all();
}
audio_stop_title_music();
2017-06-06 23:24:18 +02:00
gGameSpeed = 1;
2014-05-04 16:21:15 +01:00
}
2015-12-11 09:38:37 -06:00
/**
2014-06-21 15:50:13 +01:00
*
2015-12-11 09:38:37 -06:00
* rct2: 0x0069E9A7
2015-05-30 10:15:29 +01:00
* Call after a rotation or loading of a save to reset sprite quadrants
2014-06-21 15:50:13 +01:00
*/
2015-05-30 10:15:29 +01:00
void reset_all_sprite_quadrant_placements()
2014-11-23 23:03:45 +00:00
{
2017-11-30 18:19:26 +01:00
for (size_t i = 0; i < MAX_SPRITES; i++)
{
2018-06-22 23:25:16 +02:00
rct_sprite* spr = get_sprite(i);
if (spr->generic.sprite_identifier != SPRITE_IDENTIFIER_NULL)
2017-11-30 18:19:26 +01:00
{
sprite_move(spr->generic.x, spr->generic.y, spr->generic.z, spr);
2017-06-06 23:24:18 +02:00
}
}
2014-06-21 15:50:13 +01:00
}
2015-08-22 11:56:32 +01:00
void save_game()
2014-05-27 23:33:16 +02:00
{
2017-11-30 18:19:26 +01:00
if (!gFirstTimeSaving)
{
save_game_with_name(gScenarioSavePath.c_str());
2017-11-30 18:19:26 +01:00
}
else
{
2017-06-06 23:24:18 +02:00
save_game_as();
}
2015-08-19 21:54:25 +02:00
}
2017-09-12 00:04:03 +02:00
2018-10-14 22:36:41 +02:00
void save_game_cmd(const utf8* name /* = nullptr */)
{
if (name == nullptr)
{
save_game_with_name(gScenarioSavePath.c_str());
2018-10-14 22:36:41 +02:00
}
else
{
char savePath[MAX_PATH];
platform_get_user_directory(savePath, "save", sizeof(savePath));
safe_strcat_path(savePath, name, sizeof(savePath));
path_append_extension(savePath, ".sv6", sizeof(savePath));
save_game_with_name(savePath);
}
}
void save_game_with_name(const utf8* name)
{
log_verbose("Saving to %s", name);
if (scenario_save(name, 0x80000000 | (gConfigGeneral.save_plugin_data ? 1 : 0)))
{
log_verbose("Saved to %s", name);
gCurrentLoadedPath = name;
2018-10-14 22:36:41 +02:00
gScreenAge = 0;
}
}
2018-06-22 23:25:16 +02:00
void* create_save_game_as_intent()
2015-08-19 21:54:25 +02:00
{
2017-06-06 23:24:18 +02:00
char name[MAX_PATH];
safe_strcpy(name, path_get_filename(gScenarioSavePath.c_str()), MAX_PATH);
2017-06-06 23:24:18 +02:00
path_remove_extension(name);
2017-09-12 00:04:03 +02:00
2018-06-22 23:25:16 +02:00
Intent* intent = new Intent(WC_LOADSAVE);
2018-02-05 18:52:13 +01:00
intent->putExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_SAVE | LOADSAVETYPE_GAME);
2018-06-22 23:25:16 +02:00
intent->putExtra(INTENT_EXTRA_PATH, std::string{ name });
2017-09-12 00:04:03 +02:00
return intent;
}
void save_game_as()
{
2018-06-22 23:25:16 +02:00
auto* intent = (Intent*)create_save_game_as_intent();
2017-09-12 00:04:03 +02:00
context_open_intent(intent);
2018-02-05 18:52:13 +01:00
delete intent;
2014-05-27 23:33:16 +02:00
}
2018-06-22 23:25:16 +02:00
static int32_t compare_autosave_file_paths(const void* a, const void* b)
2017-11-30 18:19:26 +01:00
{
2018-06-22 23:25:16 +02:00
return strcmp(*(char**)a, *(char**)b);
2016-01-04 16:22:15 +01:00
}
static void limit_autosave_count(const size_t numberOfFilesToKeep, bool processLandscapeFolder)
2016-01-04 16:22:15 +01:00
{
2018-06-22 23:25:16 +02:00
size_t autosavesCount = 0;
2017-06-06 23:24:18 +02:00
size_t numAutosavesToDelete = 0;
2016-01-04 16:22:15 +01:00
2017-06-06 23:24:18 +02:00
utf8 filter[MAX_PATH];
2016-11-13 20:17:49 +01:00
2018-06-22 23:25:16 +02:00
utf8** autosaveFiles = nullptr;
2016-11-13 20:17:49 +01:00
2017-11-30 18:19:26 +01:00
if (processLandscapeFolder)
{
2017-06-06 23:24:18 +02:00
platform_get_user_directory(filter, "landscape", sizeof(filter));
safe_strcat_path(filter, "autosave", sizeof(filter));
2017-06-06 23:24:18 +02:00
safe_strcat_path(filter, "autosave_*.sc6", sizeof(filter));
2017-11-30 18:19:26 +01:00
}
else
{
2017-06-06 23:24:18 +02:00
platform_get_user_directory(filter, "save", sizeof(filter));
safe_strcat_path(filter, "autosave", sizeof(filter));
2017-06-06 23:24:18 +02:00
safe_strcat_path(filter, "autosave_*.sv6", sizeof(filter));
}
2016-11-13 20:17:49 +01:00
2017-06-06 23:24:18 +02:00
// At first, count how many autosaves there are
2017-11-30 18:19:26 +01:00
{
auto scanner = std::unique_ptr<IFileScanner>(Path::ScanDirectory(filter, false));
while (scanner->Next())
{
autosavesCount++;
}
2017-06-06 23:24:18 +02:00
}
2016-11-13 20:17:49 +01:00
2017-06-06 23:24:18 +02:00
// If there are fewer autosaves than the number of files to keep we don't need to delete anything
2017-11-30 18:19:26 +01:00
if (autosavesCount <= numberOfFilesToKeep)
{
2017-06-06 23:24:18 +02:00
return;
}
2016-11-13 20:17:49 +01:00
2018-06-22 23:25:16 +02:00
autosaveFiles = (utf8**)malloc(sizeof(utf8*) * autosavesCount);
2016-11-13 20:17:49 +01:00
2017-11-30 18:19:26 +01:00
{
auto scanner = std::unique_ptr<IFileScanner>(Path::ScanDirectory(filter, false));
for (size_t i = 0; i < autosavesCount; i++)
2017-11-30 18:19:26 +01:00
{
2018-06-22 23:25:16 +02:00
autosaveFiles[i] = (utf8*)malloc(sizeof(utf8) * MAX_PATH);
std::memset(autosaveFiles[i], 0, sizeof(utf8) * MAX_PATH);
if (scanner->Next())
2017-11-30 18:19:26 +01:00
{
if (processLandscapeFolder)
{
platform_get_user_directory(autosaveFiles[i], "landscape", sizeof(utf8) * MAX_PATH);
}
else
{
platform_get_user_directory(autosaveFiles[i], "save", sizeof(utf8) * MAX_PATH);
}
safe_strcat_path(autosaveFiles[i], "autosave", sizeof(utf8) * MAX_PATH);
safe_strcat_path(autosaveFiles[i], scanner->GetPathRelative(), sizeof(utf8) * MAX_PATH);
2017-11-30 18:19:26 +01:00
}
2017-06-06 23:24:18 +02:00
}
}
2016-01-04 16:22:15 +01:00
2018-06-22 23:25:16 +02:00
qsort(autosaveFiles, autosavesCount, sizeof(char*), compare_autosave_file_paths);
2016-01-04 16:22:15 +01:00
2017-06-06 23:24:18 +02:00
// Calculate how many saves we need to delete.
numAutosavesToDelete = autosavesCount - numberOfFilesToKeep;
2016-11-13 20:17:49 +01:00
2017-12-03 22:38:02 +01:00
for (size_t i = 0; numAutosavesToDelete > 0; i++, numAutosavesToDelete--)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
platform_file_delete(autosaveFiles[i]);
}
2016-11-13 20:17:49 +01:00
2017-12-01 09:35:01 +01:00
for (size_t i = 0; i < autosavesCount; i++)
2017-11-30 18:19:26 +01:00
{
2017-06-06 23:24:18 +02:00
free(autosaveFiles[i]);
}
2016-11-13 20:17:49 +01:00
2017-06-06 23:24:18 +02:00
free(autosaveFiles);
2016-01-04 16:22:15 +01:00
}
2015-08-19 21:54:25 +02:00
2015-02-21 11:05:15 +00:00
void game_autosave()
{
2018-06-22 23:25:16 +02:00
const char* subDirectory = "save";
const char* fileExtension = ".sv6";
uint32_t saveFlags = 0x80000000;
2017-11-30 18:19:26 +01:00
if (gScreenFlags & SCREEN_FLAGS_EDITOR)
{
2018-06-22 23:25:16 +02:00
subDirectory = "landscape";
2017-06-06 23:24:18 +02:00
fileExtension = ".sc6";
saveFlags |= 2;
}
2016-07-23 23:25:13 +02:00
2017-06-06 23:24:18 +02:00
// Retrieve current time
rct2_date currentDate;
platform_get_date_local(&currentDate);
rct2_time currentTime;
platform_get_time_local(&currentTime);
2016-07-23 23:25:13 +02:00
2017-06-06 23:24:18 +02:00
utf8 timeName[44];
2018-06-22 23:25:16 +02:00
snprintf(
2018-07-21 13:51:54 +02:00
timeName, sizeof(timeName), "autosave_%04u-%02u-%02u_%02u-%02u-%02u%s", currentDate.year, currentDate.month,
currentDate.day, currentTime.hour, currentTime.minute, currentTime.second, fileExtension);
2015-02-21 11:05:15 +00:00
int32_t autosavesToKeep = gConfigGeneral.autosave_amount;
limit_autosave_count(autosavesToKeep - 1, (gScreenFlags & SCREEN_FLAGS_EDITOR));
2016-07-23 23:25:13 +02:00
2017-06-06 23:24:18 +02:00
utf8 path[MAX_PATH];
utf8 backupPath[MAX_PATH];
platform_get_user_directory(path, subDirectory, sizeof(path));
safe_strcat_path(path, "autosave", sizeof(path));
platform_ensure_directory_exists(path);
2017-06-06 23:24:18 +02:00
safe_strcpy(backupPath, path, sizeof(backupPath));
safe_strcat_path(path, timeName, sizeof(path));
safe_strcat_path(backupPath, "autosave", sizeof(backupPath));
safe_strcat(backupPath, fileExtension, sizeof(backupPath));
safe_strcat(backupPath, ".bak", sizeof(backupPath));
2017-11-30 18:19:26 +01:00
if (platform_file_exists(path))
{
2017-06-06 23:24:18 +02:00
platform_file_copy(path, backupPath, true);
}
2015-02-21 11:05:15 +00:00
2017-06-06 23:24:18 +02:00
scenario_save(path, saveFlags);
2015-02-21 11:05:15 +00:00
}
2018-06-22 23:25:16 +02:00
static void game_load_or_quit_no_save_prompt_callback(int32_t result, const utf8* path)
{
2017-11-30 18:19:26 +01:00
if (result == MODAL_RESULT_OK)
{
window_close_by_class(WC_EDITOR_OBJECT_SELECTION);
context_load_park_from_file(path);
2017-06-06 23:24:18 +02:00
}
}
2014-05-02 22:21:08 +01:00
/**
2015-10-09 18:22:37 +02:00
*
2014-05-02 22:21:08 +01:00
* rct2: 0x0066DB79
*/
void game_load_or_quit_no_save_prompt()
{
2017-11-30 18:19:26 +01:00
switch (gSavePromptMode)
{
2018-06-22 23:25:16 +02:00
case PM_SAVE_BEFORE_LOAD:
2019-02-21 09:34:30 +00:00
{
auto loadOrQuitAction = LoadOrQuitAction(LoadOrQuitModes::CloseSavePrompt);
GameActions::Execute(&loadOrQuitAction);
2018-06-22 23:25:16 +02:00
tool_cancel();
if (gScreenFlags & SCREEN_FLAGS_SCENARIO_EDITOR)
{
load_landscape();
}
else
{
auto intent = Intent(WC_LOADSAVE);
intent.putExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME);
intent.putExtra(INTENT_EXTRA_CALLBACK, (void*)game_load_or_quit_no_save_prompt_callback);
context_open_intent(&intent);
}
break;
2019-02-21 09:34:30 +00:00
}
2018-06-22 23:25:16 +02:00
case PM_SAVE_BEFORE_QUIT:
2019-02-21 09:34:30 +00:00
{
auto loadOrQuitAction = LoadOrQuitAction(LoadOrQuitModes::CloseSavePrompt);
GameActions::Execute(&loadOrQuitAction);
2018-06-22 23:25:16 +02:00
tool_cancel();
if (input_test_flag(INPUT_FLAG_5))
{
input_set_flag(INPUT_FLAG_5, false);
}
gGameSpeed = 1;
gFirstTimeSaving = true;
title_load();
break;
2019-02-21 09:34:30 +00:00
}
2018-06-22 23:25:16 +02:00
default:
openrct2_finish();
break;
2017-06-06 23:24:18 +02:00
}
2014-05-02 22:21:08 +01:00
}
2018-06-22 23:25:16 +02:00
GAME_COMMAND_POINTER* new_game_command_table[GAME_COMMAND_COUNT] = {
nullptr,
nullptr,
nullptr,
2019-02-15 14:10:48 +00:00
nullptr,
2019-02-20 15:52:27 +00:00
nullptr,
2019-02-21 09:37:56 +00:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_create_ride,
game_command_demolish_ride,
game_command_set_ride_status,
2019-02-21 15:56:23 +00:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_set_ride_name,
2019-02-22 10:53:31 +00:00
nullptr,
2019-02-28 11:00:31 +00:00
nullptr,
2019-02-24 09:12:59 +00:00
nullptr,
2018-12-22 18:11:08 +01:00
nullptr,
nullptr,
2019-02-28 17:10:38 +00:00
nullptr,
2019-03-01 17:27:04 +00:00
nullptr,
2019-03-05 19:54:17 +00:00
nullptr,
nullptr,
2019-03-10 09:20:34 +00:00
nullptr,
nullptr,
2017-06-06 23:24:18 +02:00
game_command_set_guest_name,
game_command_set_staff_name,
2019-03-04 17:50:03 +00:00
nullptr,
nullptr,
2019-03-16 22:16:56 +00:00
nullptr,
2019-03-04 17:50:03 +00:00
nullptr,
nullptr,
2019-03-11 19:11:54 +00:00
nullptr,
2019-03-11 13:44:34 +01:00
nullptr,
2019-03-16 20:47:54 +00:00
nullptr,
nullptr,
nullptr,
2018-03-24 05:17:23 +01:00
nullptr,
2019-03-15 20:53:17 +00:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_buy_land_rights,
game_command_place_park_entrance,
2019-03-24 20:41:14 +00:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_set_maze_track,
game_command_set_park_entrance_fee,
2018-03-16 12:56:15 +00:00
nullptr,
2019-03-25 19:20:31 +00:00
nullptr,
nullptr,
2019-04-05 19:16:23 +01:00
nullptr,
2018-12-22 19:46:59 +01:00
nullptr,
2018-03-14 19:43:01 +00:00
nullptr,
nullptr,
2017-06-06 23:24:18 +02:00
game_command_place_track_design,
2018-03-15 22:44:24 +00:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_place_maze_design,
game_command_place_banner,
game_command_remove_banner,
2019-04-06 15:59:40 +01:00
nullptr,
2019-04-06 19:36:44 +01:00
nullptr,
2019-04-07 08:51:14 +01:00
nullptr,
2019-04-07 09:15:41 +01:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_set_land_ownership,
2019-01-02 12:59:37 +01:00
nullptr,
2018-03-24 05:45:08 +01:00
nullptr,
2018-03-24 04:52:47 +01:00
nullptr,
2019-04-07 10:10:36 +01:00
nullptr,
2019-01-27 18:05:54 -08:00
nullptr,
2017-06-06 23:24:18 +02:00
game_command_set_player_group,
game_command_modify_groups,
game_command_kick_player,
game_command_cheat,
game_command_pickup_guest,
game_command_pickup_staff,
game_command_balloon_press,
game_command_modify_tile,
game_command_edit_scenario_options,
2018-02-19 15:11:14 -05:00
NULL,
};