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

554 lines
18 KiB
C++
Raw Normal View History

/*****************************************************************************
2024-12-31 20:30:04 +01:00
* Copyright (c) 2014-2025 OpenRCT2 developers
*
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
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "Editor.h"
2018-06-22 23:25:16 +02:00
#include "Context.h"
2017-12-07 16:33:04 +01:00
#include "EditorObjectSelectionSession.h"
#include "FileClassifier.h"
2017-11-30 18:17:06 +01:00
#include "Game.h"
2018-05-30 12:50:25 +01:00
#include "GameState.h"
2017-12-07 16:33:04 +01:00
#include "OpenRCT2.h"
#include "ParkImporter.h"
#include "actions/LandBuyRightsAction.h"
#include "actions/LandSetRightsAction.h"
#include "actions/ResultWithMessage.h"
2017-12-07 16:33:04 +01:00
#include "audio/audio.h"
2024-12-15 17:39:52 +01:00
#include "core/EnumUtils.hpp"
#include "core/Path.hpp"
2023-07-02 12:59:20 +03:00
#include "core/String.hpp"
2021-11-24 17:19:52 +02:00
#include "entity/EntityList.h"
#include "entity/EntityRegistry.h"
2021-11-25 23:47:24 +02:00
#include "entity/Guest.h"
2022-03-08 00:14:52 +00:00
#include "entity/PatrolArea.h"
2021-11-25 23:47:24 +02:00
#include "entity/Staff.h"
2018-01-05 23:45:53 +00:00
#include "interface/Viewport.h"
#include "interface/Window_internal.h"
#include "localisation/LocalisationService.h"
#include "management/Finance.h"
2017-10-11 21:38:26 +02:00
#include "management/NewsItem.h"
2021-10-27 14:21:14 +02:00
#include "object/DefaultObjects.h"
#include "object/ObjectManager.h"
#include "object/ObjectRepository.h"
#include "peep/PeepAnimations.h"
2018-01-09 10:40:42 +01:00
#include "rct1/RCT1.h"
2018-03-19 23:28:40 +01:00
#include "scenario/Scenario.h"
2018-06-22 23:25:16 +02:00
#include "ui/UiContext.h"
#include "ui/WindowManager.h"
2017-10-19 10:01:05 +02:00
#include "windows/Intent.h"
#include "world/Climate.h"
2018-03-13 13:14:02 +01:00
#include "world/Entrance.h"
#include "world/Footpath.h"
2018-03-19 23:28:40 +01:00
#include "world/Park.h"
2018-06-22 23:25:16 +02:00
#include "world/Scenery.h"
#include <array>
2024-07-09 16:43:03 +02:00
#include <cassert>
2018-06-22 23:25:16 +02:00
#include <vector>
2018-06-02 00:07:14 +01:00
using namespace OpenRCT2;
namespace OpenRCT2::Editor
{
2020-11-09 12:07:38 +01:00
static std::array<std::vector<uint8_t>, EnumValue(ObjectType::Count)> _editorSelectedObjectFlags;
2018-06-22 23:25:16 +02:00
static void ConvertSaveToScenarioCallback(int32_t result, const utf8* path);
static void SetAllLandOwned();
static void FinaliseMainView();
static void ClearMapForEditing(bool fromSave);
static void ObjectListLoad()
{
auto* context = GetContext();
// Unload objects first, the repository is re-populated which owns the objects.
auto& objectManager = context->GetObjectManager();
2022-05-30 22:34:29 +01:00
objectManager.UnloadAll();
// Scan objects if necessary
const auto& localisationService = context->GetLocalisationService();
auto& objectRepository = context->GetObjectRepository();
objectRepository.LoadOrConstruct(localisationService.GetCurrentLanguage());
2022-05-30 22:34:29 +01:00
Audio::LoadAudioObjects();
// Reset loaded objects to just defaults
2021-10-27 14:21:14 +02:00
// Load minimum required objects (like surface and edge)
for (const auto& entry : MinimumRequiredObjects)
{
objectManager.LoadObject(entry);
}
}
static WindowBase* OpenEditorWindows()
2022-06-07 08:26:58 +01:00
{
2022-11-06 20:49:07 +00:00
auto* main = ContextOpenWindow(WindowClass::MainWindow);
ContextOpenWindow(WindowClass::TopToolbar);
ContextOpenWindowView(WV_EDITOR_BOTTOM_TOOLBAR);
2022-06-07 08:26:58 +01:00
return main;
}
/**
*
* rct2: 0x0066FFE1
*/
void Load()
{
2024-05-01 12:34:10 +02:00
// TODO: replace with dedicated scene
auto* context = GetContext();
context->SetActiveScene(context->GetGameScene());
2024-01-24 22:17:32 +01:00
auto& gameState = GetGameState();
2024-03-01 20:26:20 +00:00
Audio::StopAll();
ObjectListLoad();
2024-03-26 15:08:17 +01:00
gameStateInitAll(gameState, DEFAULT_MAP_SIZE);
gScreenFlags = SCREEN_FLAGS_SCENARIO_EDITOR;
2024-03-01 20:26:20 +00:00
gameState.EditorStep = EditorStep::ObjectSelection;
gameState.Park.Flags |= PARK_FLAGS_SHOW_REAL_GUEST_NAMES;
2024-01-24 22:17:32 +01:00
gameState.ScenarioCategory = SCENARIO_CATEGORY_OTHER;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
WindowBase* mainWindow = OpenEditorWindows();
mainWindow->SetLocation(TileCoordsXYZ{ 75, 75, 14 }.ToCoordsXYZ());
LoadPalette();
gScreenAge = 0;
2024-01-24 22:17:32 +01:00
gameState.ScenarioName = LanguageGetString(STR_MY_NEW_SCENARIO);
}
/**
*
* rct2: 0x00672781
*/
void ConvertSaveToScenario()
{
ToolCancel();
auto intent = Intent(WindowClass::Loadsave);
2023-01-17 20:46:55 +01:00
intent.PutExtra(INTENT_EXTRA_LOADSAVE_TYPE, LOADSAVETYPE_LOAD | LOADSAVETYPE_GAME);
2024-12-04 20:43:55 +02:00
intent.PutExtra(INTENT_EXTRA_CALLBACK, reinterpret_cast<CloseCallback>(ConvertSaveToScenarioCallback));
2022-11-06 20:49:07 +00:00
ContextOpenIntent(&intent);
}
2018-06-22 23:25:16 +02:00
static void ConvertSaveToScenarioCallback(int32_t result, const utf8* path)
{
if (result != MODAL_RESULT_OK)
{
return;
}
if (!GetContext()->LoadParkFromFile(path))
{
return;
}
2024-01-24 22:17:32 +01:00
auto& gameState = GetGameState();
ScenarioReset(gameState);
gScreenFlags = SCREEN_FLAGS_SCENARIO_EDITOR;
2024-03-01 20:26:20 +00:00
gameState.EditorStep = EditorStep::ObjectiveSelection;
2024-01-24 22:17:32 +01:00
gameState.ScenarioCategory = SCENARIO_CATEGORY_OTHER;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
2022-06-07 08:26:58 +01:00
OpenEditorWindows();
FinaliseMainView();
gScreenAge = 0;
}
/**
*
* rct2: 0x00672957
*/
void LoadTrackDesigner()
{
2024-05-01 12:34:10 +02:00
// TODO: replace with dedicated scene
auto* context = GetContext();
context->SetActiveScene(context->GetGameScene());
2024-03-01 20:26:20 +00:00
Audio::StopAll();
gScreenFlags = SCREEN_FLAGS_TRACK_DESIGNER;
2018-06-22 23:25:16 +02:00
gScreenAge = 0;
2023-01-17 20:46:55 +01:00
ObjectManagerUnloadAllObjects();
ObjectListLoad();
2024-03-26 15:08:17 +01:00
gameStateInitAll(GetGameState(), DEFAULT_MAP_SIZE);
SetAllLandOwned();
2024-03-01 20:26:20 +00:00
GetGameState().EditorStep = EditorStep::ObjectSelection;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
WindowBase* mainWindow = OpenEditorWindows();
mainWindow->SetLocation(TileCoordsXYZ{ 75, 75, 14 }.ToCoordsXYZ());
LoadPalette();
}
/**
*
* rct2: 0x006729FD
*/
void LoadTrackManager()
{
2024-05-01 12:34:10 +02:00
// TODO: replace with dedicated scene
auto* context = GetContext();
context->SetActiveScene(context->GetGameScene());
2024-03-01 20:26:20 +00:00
Audio::StopAll();
gScreenFlags = SCREEN_FLAGS_TRACK_MANAGER;
2018-06-22 23:25:16 +02:00
gScreenAge = 0;
2023-01-17 20:46:55 +01:00
ObjectManagerUnloadAllObjects();
ObjectListLoad();
2024-03-26 15:08:17 +01:00
gameStateInitAll(GetGameState(), DEFAULT_MAP_SIZE);
SetAllLandOwned();
2024-03-01 20:26:20 +00:00
GetGameState().EditorStep = EditorStep::ObjectSelection;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
WindowBase* mainWindow = OpenEditorWindows();
mainWindow->SetLocation(TileCoordsXYZ{ 75, 75, 14 }.ToCoordsXYZ());
LoadPalette();
}
/**
*
* rct2: 0x0068ABEC
*/
static void SetAllLandOwned()
{
2024-02-12 21:32:08 +00:00
auto& gameState = GetGameState();
2024-07-26 18:23:25 +10:00
MapRange range = { 2 * kCoordsXYStep, 2 * kCoordsXYStep, (gameState.MapSize.x - 3) * kCoordsXYStep,
(gameState.MapSize.y - 3) * kCoordsXYStep };
2019-03-17 08:25:51 +00:00
auto landSetRightsAction = LandSetRightsAction(range, LandSetRightSetting::SetForSale);
landSetRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND);
GameActions::Execute(&landSetRightsAction);
auto landBuyRightsAction = LandBuyRightsAction(range, LandBuyRightSetting::BuyLand);
landBuyRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND);
GameActions::Execute(&landBuyRightsAction);
}
2022-10-08 20:12:18 +02:00
static void AfterLoadCleanup(bool loadedFromSave)
{
ClearMapForEditing(loadedFromSave);
2024-05-01 12:34:10 +02:00
// TODO: replace with dedicated scene
auto* context = GetContext();
context->SetActiveScene(context->GetGameScene());
2024-03-01 20:26:20 +00:00
GetGameState().EditorStep = EditorStep::LandscapeEditor;
2018-06-22 23:25:16 +02:00
gScreenAge = 0;
gScreenFlags = SCREEN_FLAGS_SCENARIO_EDITOR;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
2022-06-07 08:26:58 +01:00
OpenEditorWindows();
FinaliseMainView();
2022-10-08 20:12:18 +02:00
}
bool LoadLandscape(const utf8* path)
2022-10-08 20:12:18 +02:00
{
// #4996: Make sure the object selection window closes here to prevent unload objects
// after we have loaded a new park.
WindowCloseAll();
if (!GetContext()->LoadParkFromFile(path))
2022-10-08 20:12:18 +02:00
return false;
auto extension = Path::GetExtension(path);
bool loadedFromSave = !ParkImporter::ExtensionIsScenario(extension);
2022-10-08 20:12:18 +02:00
AfterLoadCleanup(loadedFromSave);
return true;
}
static void ClearMapForEditing(bool fromSave)
{
2022-10-12 06:35:20 +01:00
MapRemoveAllRides();
2021-07-27 10:25:58 +01:00
UnlinkAllRideBanners();
RideInitAll();
//
for (auto* guest : EntityList<Guest>())
{
guest->SetName({});
}
for (auto* staff : EntityList<Staff>())
{
staff->SetName({});
}
2024-01-21 21:05:22 +00:00
auto& gameState = GetGameState();
2021-11-24 15:27:48 +02:00
ResetAllEntities();
2022-03-08 00:14:52 +00:00
UpdateConsolidatedPatrolAreas();
gameState.NumGuestsInPark = 0;
gameState.NumGuestsHeadingForPark = 0;
gameState.NumGuestsInParkLastWeek = 0;
2024-03-05 23:06:27 +00:00
gameState.GuestChangeModifier = 0;
if (fromSave)
{
gameState.Park.Flags |= PARK_FLAGS_NO_MONEY;
if (gameState.Park.EntranceFee == 0)
{
gameState.Park.Flags |= PARK_FLAGS_PARK_FREE_ENTRY;
}
else
{
gameState.Park.Flags &= ~PARK_FLAGS_PARK_FREE_ENTRY;
}
gameState.Park.Flags &= ~PARK_FLAGS_SPRITES_INITIALISED;
2024-01-22 23:28:16 +01:00
gameState.GuestInitialCash = std::clamp(gameState.GuestInitialCash, 10.00_GBP, MAX_ENTRANCE_FEE);
2024-09-02 21:42:37 +01:00
gameState.InitialCash = std::min<money64>(gameState.InitialCash, 100000);
FinanceResetCashToInitial();
gameState.BankLoan = std::clamp<money64>(gameState.BankLoan, 0.00_GBP, 5000000.00_GBP);
2024-02-13 20:36:35 +00:00
gameState.MaxBankLoan = std::clamp<money64>(gameState.MaxBankLoan, 0.00_GBP, 5000000.00_GBP);
gameState.BankLoanInterestRate = std::clamp<uint8_t>(gameState.BankLoanInterestRate, 5, MaxBankLoanInterestRate);
}
2024-01-21 21:05:22 +00:00
ClimateReset(gameState.Climate);
News::InitQueue();
}
/**
*
* rct2: 0x0067009A
*/
void OpenWindowsForCurrentStep()
{
if (!(gScreenFlags & SCREEN_FLAGS_EDITOR))
{
return;
}
auto* windowMgr = Ui::GetWindowManager();
2024-03-01 20:26:20 +00:00
switch (GetGameState().EditorStep)
{
case EditorStep::ObjectSelection:
if (windowMgr->FindByClass(WindowClass::EditorObjectSelection) != nullptr)
2018-06-22 23:25:16 +02:00
{
return;
}
if (windowMgr->FindByClass(WindowClass::InstallTrack) != nullptr)
2018-06-22 23:25:16 +02:00
{
return;
}
2018-06-22 23:25:16 +02:00
if (gScreenFlags & SCREEN_FLAGS_TRACK_MANAGER)
{
2023-01-17 20:46:55 +01:00
ObjectManagerUnloadAllObjects();
2018-06-22 23:25:16 +02:00
}
2022-11-06 20:49:07 +00:00
ContextOpenWindow(WindowClass::EditorObjectSelection);
2018-06-22 23:25:16 +02:00
break;
case EditorStep::InventionsListSetUp:
if (windowMgr->FindByClass(WindowClass::EditorInventionList) != nullptr)
2018-06-22 23:25:16 +02:00
{
return;
}
2022-11-06 20:49:07 +00:00
ContextOpenWindow(WindowClass::EditorInventionList);
2018-06-22 23:25:16 +02:00
break;
case EditorStep::OptionsSelection:
if (windowMgr->FindByClass(WindowClass::EditorScenarioOptions) != nullptr)
2018-06-22 23:25:16 +02:00
{
return;
}
2022-11-06 20:49:07 +00:00
ContextOpenWindow(WindowClass::EditorScenarioOptions);
2018-06-22 23:25:16 +02:00
break;
case EditorStep::ObjectiveSelection:
if (windowMgr->FindByClass(WindowClass::EditorObjectiveOptions) != nullptr)
2018-06-22 23:25:16 +02:00
{
return;
}
2022-11-06 20:49:07 +00:00
ContextOpenWindow(WindowClass::EditorObjectiveOptions);
2018-06-22 23:25:16 +02:00
break;
case EditorStep::LandscapeEditor:
case EditorStep::SaveScenario:
case EditorStep::RollercoasterDesigner:
case EditorStep::DesignsManager:
case EditorStep::Invalid:
break;
}
}
static void FinaliseMainView()
{
auto windowManager = Ui::GetWindowManager();
auto& gameState = GetGameState();
windowManager->SetMainView(gameState.SavedView, gameState.SavedViewZoom, gameState.SavedViewRotation);
ResetAllSpriteQuadrantPlacements();
2017-10-07 01:28:00 +02:00
windowManager->BroadcastIntent(Intent(INTENT_ACTION_SET_DEFAULT_SCENERY_CONFIG));
2018-06-02 00:07:14 +01:00
windowManager->BroadcastIntent(Intent(INTENT_ACTION_REFRESH_NEW_RIDES));
windowManager->BroadcastIntent(Intent(INTENT_ACTION_CLEAR_TILE_INSPECTOR_CLIPBOARD));
2017-10-07 01:28:00 +02:00
gWindowUpdateTicks = 0;
LoadPalette();
}
/**
*
* rct2: 0x006AB9B8
*/
2022-07-31 14:22:58 +02:00
std::pair<ObjectType, StringId> CheckObjectSelection()
{
constexpr std::pair<ObjectType, StringId> kBasicCheckPairs[] = {
{ ObjectType::Ride, STR_AT_LEAST_ONE_RIDE_OBJECT_MUST_BE_SELECTED },
{ ObjectType::Station, STR_AT_LEAST_ONE_STATION_OBJECT_MUST_BE_SELECTED },
{ ObjectType::TerrainSurface, STR_AT_LEAST_ONE_TERRAIN_SURFACE_OBJECT_MUST_BE_SELECTED },
{ ObjectType::TerrainEdge, STR_AT_LEAST_ONE_TERRAIN_EDGE_OBJECT_MUST_BE_SELECTED },
};
for (auto& pair : kBasicCheckPairs)
{
if (!EditorCheckObjectGroupAtLeastOneSelected(pair.first))
{
return { pair.first, pair.second };
}
}
// No checks beyond this point apply to the track designer or track designs manager.
const bool isTrackDesignerManager = gScreenFlags & (SCREEN_FLAGS_TRACK_DESIGNER | SCREEN_FLAGS_TRACK_MANAGER);
if (isTrackDesignerManager)
{
return { ObjectType::None, STR_NONE };
}
if (!EditorCheckObjectGroupAtLeastOneSurfaceSelected(false))
{
return { ObjectType::FootpathSurface, STR_AT_LEAST_ONE_FOOTPATH_NON_QUEUE_SURFACE_OBJECT_MUST_BE_SELECTED };
}
if (!EditorCheckObjectGroupAtLeastOneSurfaceSelected(true))
{
return { ObjectType::FootpathSurface, STR_AT_LEAST_ONE_FOOTPATH_QUEUE_SURFACE_OBJECT_MUST_BE_SELECTED };
}
constexpr std::pair<ObjectType, StringId> kParkCheckPairs[] = {
{ ObjectType::FootpathRailings, STR_AT_LEAST_ONE_FOOTPATH_RAILING_OBJECT_MUST_BE_SELECTED },
{ ObjectType::ParkEntrance, STR_PARK_ENTRANCE_TYPE_MUST_BE_SELECTED },
{ ObjectType::Water, STR_WATER_TYPE_MUST_BE_SELECTED },
{ ObjectType::PeepNames, STR_AT_LEAST_ONE_PEEP_NAMES_OBJECT_MUST_BE_SELECTED },
};
for (auto& pair : kParkCheckPairs)
{
if (!EditorCheckObjectGroupAtLeastOneSelected(pair.first))
{
return { pair.first, pair.second };
}
}
using OpenRCT2::AnimationPeepType;
constexpr std::pair<AnimationPeepType, StringId> kPeepCheckPairs[] = {
{ AnimationPeepType::Guest, STR_AT_LEAST_ONE_GUEST_PEEP_ANIMATIONS_OBJECT_MUST_BE_SELECTED },
{ AnimationPeepType::Handyman, STR_AT_LEAST_ONE_HANDYMAN_PEEP_ANIMATIONS_OBJECT_MUST_BE_SELECTED },
{ AnimationPeepType::Mechanic, STR_AT_LEAST_ONE_MECHANIC_PEEP_ANIMATIONS_OBJECT_MUST_BE_SELECTED },
{ AnimationPeepType::Security, STR_AT_LEAST_ONE_SECURITY_PEEP_ANIMATIONS_OBJECT_MUST_BE_SELECTED },
{ AnimationPeepType::Entertainer, STR_AT_LEAST_ONE_ENTERTAINER_PEEP_ANIMATIONS_OBJECT_MUST_BE_SELECTED },
};
for (auto& pair : kPeepCheckPairs)
{
if (!EditorCheckObjectGroupAtLeastOneOfPeepTypeSelected(EnumValue(pair.first)))
{
return { ObjectType::PeepAnimations, pair.second };
}
}
return { ObjectType::None, STR_NONE };
}
/**
*
* rct2: 0x0066FEAC
*/
ResultWithMessage CheckPark()
{
2024-03-26 19:01:50 +01:00
auto& gameState = GetGameState();
int32_t parkSize = Park::UpdateSize(gameState);
if (parkSize == 0)
{
return { false, STR_PARK_MUST_OWN_SOME_LAND };
}
if (gameState.Park.Entrances.empty())
{
return { false, STR_NO_PARK_ENTRANCES };
}
for (const auto& parkEntrance : gameState.Park.Entrances)
{
int32_t direction = DirectionReverse(parkEntrance.direction);
2022-10-04 07:51:27 +01:00
switch (FootpathIsConnectedToMapEdge(parkEntrance, direction, 0))
{
2018-06-22 23:25:16 +02:00
case FOOTPATH_SEARCH_NOT_FOUND:
return { false, STR_PARK_ENTRANCE_WRONG_DIRECTION_OR_NO_PATH };
2018-06-22 23:25:16 +02:00
case FOOTPATH_SEARCH_INCOMPLETE:
case FOOTPATH_SEARCH_TOO_COMPLEX:
return { false, STR_PARK_ENTRANCE_PATH_INCOMPLETE_OR_COMPLEX };
2018-06-22 23:25:16 +02:00
case FOOTPATH_SEARCH_SUCCESS:
// Run the search again and unown the path
2022-10-04 07:51:27 +01:00
FootpathIsConnectedToMapEdge(parkEntrance, direction, (1 << 5));
2018-06-22 23:25:16 +02:00
break;
}
}
2024-03-10 18:16:28 +00:00
if (gameState.PeepSpawns.empty())
{
return { false, STR_PEEP_SPAWNS_NOT_SET };
}
return { true, STR_NONE };
}
2020-11-09 12:07:38 +01:00
uint8_t GetSelectedObjectFlags(ObjectType objectType, size_t index)
2018-02-11 17:56:12 +00:00
{
uint8_t result = 0;
2020-11-09 12:07:38 +01:00
auto& list = _editorSelectedObjectFlags[EnumValue(objectType)];
2018-02-11 17:56:12 +00:00
if (list.size() > index)
{
result = list[index];
}
return result;
}
void ClearSelectedObject(ObjectType objectType, size_t index, uint32_t flags)
2018-02-11 17:56:12 +00:00
{
2020-11-09 12:07:38 +01:00
auto& list = _editorSelectedObjectFlags[EnumValue(objectType)];
2018-02-11 17:56:12 +00:00
if (list.size() <= index)
{
list.resize(index + 1);
}
list[index] &= ~flags;
}
void SetSelectedObject(ObjectType objectType, size_t index, uint32_t flags)
2018-02-11 17:56:12 +00:00
{
2021-10-27 14:21:14 +02:00
if (index != OBJECT_ENTRY_INDEX_NULL)
2018-02-11 17:56:12 +00:00
{
2024-04-12 21:52:21 +02:00
assert(static_cast<size_t>(objectType) < getObjectEntryGroupCount(ObjectType::Paths));
2021-10-27 14:21:14 +02:00
auto& list = _editorSelectedObjectFlags[EnumValue(objectType)];
if (list.size() <= index)
{
list.resize(index + 1);
}
list[index] |= flags;
2018-02-11 17:56:12 +00:00
}
}
} // namespace OpenRCT2::Editor
void EditorOpenWindowsForCurrentStep()
{
2018-02-01 18:49:14 +01:00
Editor::OpenWindowsForCurrentStep();
}