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

579 lines
19 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"
2025-02-17 07:24:03 -05: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/WindowBase.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"
#include "scenario/Scenario.h"
#include "scripting/ScriptEngine.h"
2018-06-22 23:25:16 +02:00
#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"
#include "world/Map.h"
2025-09-04 20:56:31 +02:00
#include "world/MapLimits.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
{
static std::array<std::vector<uint8_t>, EnumValue(ObjectType::count)> _editorSelectedObjectFlags;
2025-03-01 15:44:57 +01:00
static void ConvertSaveToScenarioCallback(ModalResult result, const utf8* path);
static void SetAllLandOwned();
static void FinaliseMainView();
static void ClearMapForEditing(bool fromSave);
static void ObjectListLoad()
{
auto* context = GetContext();
context->OpenProgress(STR_LOADING_GENERIC);
// 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 : kMinimumRequiredObjects)
2021-10-27 14:21:14 +02:00
{
objectManager.LoadObject(entry);
}
context->CloseProgress();
}
static WindowBase* OpenEditorWindows()
2022-06-07 08:26:58 +01:00
{
auto* main = ContextOpenWindow(WindowClass::mainWindow);
ContextOpenWindow(WindowClass::topToolbar);
2025-09-17 23:49:17 +02:00
ContextOpenWindowView(WindowView::editorBottomToolbar);
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());
auto& gameState = getGameState();
2024-03-01 20:26:20 +00:00
Audio::StopAll();
gameStateInitAll(gameState, kDefaultMapSize);
2025-03-05 21:45:04 +01:00
gLegacyScene = LegacyScene::scenarioEditor;
gameState.editorStep = EditorStep::ObjectSelection;
2025-08-21 20:50:23 +02:00
gameState.park.flags |= PARK_FLAGS_SHOW_REAL_GUEST_NAMES;
2025-08-22 18:07:55 +02:00
gameState.scenarioOptions.category = Scenario::Category::other;
ObjectListLoad();
2023-01-16 21:14:50 +01:00
ViewportInitAll();
WindowBase* mainWindow = OpenEditorWindows();
mainWindow->setViewportLocation(TileCoordsXYZ{ 75, 75, 14 }.ToCoordsXYZ());
LoadPalette();
gScreenAge = 0;
gameState.scenarioOptions.name = LanguageGetString(STR_MY_NEW_SCENARIO);
GameLoadScripts();
GameNotifyMapChanged();
}
/**
*
* rct2: 0x00672781
*/
void ConvertSaveToScenario()
{
ToolCancel();
auto intent = Intent(WindowClass::loadsave);
intent.PutEnumExtra<LoadSaveAction>(INTENT_EXTRA_LOADSAVE_ACTION, LoadSaveAction::load);
intent.PutEnumExtra<LoadSaveType>(INTENT_EXTRA_LOADSAVE_TYPE, LoadSaveType::park);
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);
}
2025-03-01 15:44:57 +01:00
static void ConvertSaveToScenarioCallback(ModalResult result, const utf8* path)
{
2025-03-01 15:44:57 +01:00
if (result != ModalResult::ok)
{
return;
}
if (!GetContext()->LoadParkFromFile(path))
{
return;
}
auto& gameState = getGameState();
2024-01-24 22:17:32 +01:00
ScenarioReset(gameState);
2025-03-05 21:45:04 +01:00
gLegacyScene = LegacyScene::scenarioEditor;
gameState.editorStep = EditorStep::OptionsSelection;
2025-08-22 18:07:55 +02:00
gameState.scenarioOptions.category = Scenario::Category::other;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
2022-06-07 08:26:58 +01:00
OpenEditorWindows();
FinaliseMainView();
gScreenAge = 0;
GameLoadScripts();
GameNotifyMapChanged();
#ifdef ENABLE_SCRIPTING
// Clear the plugin storage before saving
auto& scriptEngine = GetContext()->GetScriptEngine();
scriptEngine.ClearParkStorage();
#endif
}
/**
*
* 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();
2025-03-05 21:45:04 +01:00
gLegacyScene = LegacyScene::trackDesigner;
2018-06-22 23:25:16 +02:00
gScreenAge = 0;
auto& gameState = getGameState();
gameStateInitAll(gameState, kDefaultMapSize);
gameState.editorStep = EditorStep::ObjectSelection;
SetAllLandOwned();
ObjectListLoad();
2023-01-16 21:14:50 +01:00
ViewportInitAll();
WindowBase* mainWindow = OpenEditorWindows();
mainWindow->setViewportLocation(TileCoordsXYZ{ 75, 75, 14 }.ToCoordsXYZ());
LoadPalette();
GameLoadScripts();
GameNotifyMapChanged();
}
/**
*
* 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();
2025-03-05 21:45:04 +01:00
gLegacyScene = LegacyScene::trackDesignsManager;
2018-06-22 23:25:16 +02:00
gScreenAge = 0;
auto& gameState = getGameState();
gameStateInitAll(gameState, kDefaultMapSize);
SetAllLandOwned();
gameState.editorStep = EditorStep::ObjectSelection;
ObjectListLoad();
2023-01-16 21:14:50 +01:00
ViewportInitAll();
WindowBase* mainWindow = OpenEditorWindows();
mainWindow->setViewportLocation(TileCoordsXYZ{ 75, 75, 14 }.ToCoordsXYZ());
LoadPalette();
GameLoadScripts();
GameNotifyMapChanged();
}
/**
*
* rct2: 0x0068ABEC
*/
static void SetAllLandOwned()
{
auto& gameState = getGameState();
MapRange range = { 2 * kCoordsXYStep, 2 * kCoordsXYStep, (gameState.mapSize.x - 3) * kCoordsXYStep,
(gameState.mapSize.y - 3) * kCoordsXYStep };
auto landSetRightsAction = GameActions::LandSetRightsAction(range, GameActions::LandSetRightSetting::SetForSale);
2019-03-17 08:25:51 +00:00
landSetRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND);
GameActions::Execute(&landSetRightsAction, gameState);
2019-03-17 08:25:51 +00:00
auto landBuyRightsAction = GameActions::LandBuyRightsAction(range, GameActions::LandBuyRightSetting::BuyLand);
2019-03-17 08:25:51 +00:00
landBuyRightsAction.SetFlags(GAME_COMMAND_FLAG_NO_SPEND);
GameActions::Execute(&landBuyRightsAction, gameState);
}
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());
getGameState().editorStep = EditorStep::LandscapeEditor;
2018-06-22 23:25:16 +02:00
gScreenAge = 0;
2025-03-05 21:45:04 +01:00
gLegacyScene = LegacyScene::scenarioEditor;
2023-01-16 21:14:50 +01:00
ViewportInitAll();
2022-06-07 08:26:58 +01:00
OpenEditorWindows();
FinaliseMainView();
GameLoadScripts();
GameNotifyMapChanged();
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.
auto* windowMgr = Ui::GetWindowManager();
windowMgr->CloseAll();
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({});
}
2025-08-31 14:29:36 +02:00
getGameState().entities.ResetAllEntities();
2022-03-08 00:14:52 +00:00
UpdateConsolidatedPatrolAreas();
auto& gameState = getGameState();
auto& park = gameState.park;
auto& scenarioOptions = gameState.scenarioOptions;
park.numGuestsInPark = 0;
park.numGuestsHeadingForPark = 0;
park.numGuestsInParkLastWeek = 0;
park.guestChangeModifier = 0;
if (fromSave)
{
park.flags |= PARK_FLAGS_NO_MONEY;
if (park.entranceFee == 0)
{
park.flags |= PARK_FLAGS_PARK_FREE_ENTRY;
}
else
{
park.flags &= ~PARK_FLAGS_PARK_FREE_ENTRY;
}
park.flags &= ~PARK_FLAGS_SPRITES_INITIALISED;
scenarioOptions.guestInitialCash = std::clamp(scenarioOptions.guestInitialCash, 10.00_GBP, kMaxEntranceFee);
scenarioOptions.initialCash = std::min<money64>(scenarioOptions.initialCash, 100000);
FinanceResetCashToInitial();
park.bankLoan = std::clamp<money64>(park.bankLoan, 0.00_GBP, 5000000.00_GBP);
park.maxBankLoan = std::clamp<money64>(park.maxBankLoan, 0.00_GBP, 5000000.00_GBP);
park.bankLoanInterestRate = std::clamp<uint8_t>(park.bankLoanInterestRate, 5, kMaxBankLoanInterestRate);
}
2025-02-04 16:25:17 +01:00
ClimateReset();
2025-03-18 23:25:25 +01:00
News::InitQueue(gameState);
}
/**
*
* rct2: 0x0067009A
*/
void OpenWindowsForCurrentStep()
{
2025-03-05 21:45:04 +01:00
if (!isInEditorMode())
{
return;
}
auto* windowMgr = Ui::GetWindowManager();
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;
}
2025-03-05 21:45:04 +01:00
if (gLegacyScene == LegacyScene::trackDesignsManager)
2018-06-22 23:25:16 +02:00
{
2023-01-17 20:46:55 +01:00
ObjectManagerUnloadAllObjects();
2018-06-22 23:25:16 +02: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;
}
ContextOpenWindow(WindowClass::editorInventionList);
2018-06-22 23:25:16 +02:00
break;
case EditorStep::OptionsSelection:
case EditorStep::ObjectiveSelection:
case EditorStep::ScenarioDetails:
if (windowMgr->FindByClass(WindowClass::editorScenarioOptions) != nullptr)
2018-06-22 23:25:16 +02:00
{
return;
}
ContextOpenWindow(WindowClass::editorScenarioOptions);
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.
2025-03-05 21:45:04 +01:00
const bool isTrackDesignerManager = isInTrackDesignerOrManager();
if (isTrackDesignerManager)
{
return { ObjectType::none, kStringIdNone };
}
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[] = {
2025-10-05 01:10:06 +02:00
{ 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, kStringIdNone };
}
/**
*
* rct2: 0x0066FEAC
*/
ResultWithMessage CheckPark()
{
auto& gameState = getGameState();
auto& park = gameState.park;
int32_t parkSize = Park::UpdateSize(park);
if (parkSize == 0)
{
return { false, STR_PARK_MUST_OWN_SOME_LAND };
}
2025-08-21 20:50:23 +02:00
if (gameState.park.entrances.empty())
{
return { false, STR_NO_PARK_ENTRANCES };
}
2025-08-21 20:50:23 +02:00
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;
}
}
if (gameState.peepSpawns.empty())
{
return { false, STR_PEEP_SPAWNS_NOT_SET };
}
2025-01-19 16:59:45 +01:00
return { true, kStringIdNone };
}
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
{
if (index != kObjectEntryIndexNull)
2018-02-11 17:56:12 +00: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();
}