Files
OpenRCT2-Web/src/openrct2/object/SceneryGroupObject.cpp
T

246 lines
8.2 KiB
C++
Raw Normal View History

2016-06-26 00:06:55 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 OpenRCT2 developers
2016-06-26 00:06:55 +01: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
2016-06-26 00:06:55 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-06-26 00:06:55 +01:00
*****************************************************************************/
2017-12-14 18:43:59 +00:00
#pragma warning(disable : 4706) // assignment within conditional expression
2018-06-22 23:03:20 +02:00
#include "SceneryGroupObject.h"
#include "../Context.h"
2016-06-26 00:06:55 +01:00
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
2016-06-26 00:06:55 +01:00
#include "../core/Memory.hpp"
2017-12-14 18:43:59 +00:00
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
2021-12-07 20:55:27 +00:00
#include "../drawing/Image.h"
2021-11-25 23:47:24 +02:00
#include "../entity/Staff.h"
2017-12-14 18:43:59 +00:00
#include "../localisation/Language.h"
#include "../world/Scenery.h"
2022-01-24 12:30:22 +01:00
#include "ObjectLimits.h"
2016-07-09 02:01:27 +01:00
#include "ObjectManager.h"
#include "ObjectRepository.h"
2018-06-22 23:03:20 +02:00
#include <unordered_map>
2016-06-26 00:06:55 +01:00
using namespace OpenRCT2;
2022-01-24 12:30:22 +01:00
// Example entry: "$DAT:09F55406|00STBEN "
// 5 for $DAT:, 8 for the checksum, 1 for the vertical bar, 8 for the .DAT name.
static constexpr uint8_t DatEntryPrefixLength = 5;
static constexpr uint8_t DatEntryFlagsLength = 8;
static constexpr uint8_t DatEntrySeparatorLength = 1;
static constexpr uint8_t DatEntryLength = DatEntryPrefixLength + DatEntryFlagsLength + DatEntrySeparatorLength
+ DAT_NAME_LENGTH;
static constexpr uint8_t DatEntryFlagsStart = DatEntryPrefixLength;
static constexpr uint8_t DatEntryNameStart = DatEntryPrefixLength + DatEntryFlagsLength + DatEntrySeparatorLength;
2018-06-22 23:03:20 +02:00
void SceneryGroupObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
2016-06-26 00:06:55 +01:00
{
stream->Seek(6, STREAM_SEEK_CURRENT);
2016-06-26 00:06:55 +01:00
stream->Seek(0x80 * 2, STREAM_SEEK_CURRENT);
stream->Seek(1, STREAM_SEEK_CURRENT); // entry_count
2023-01-21 16:39:35 +01:00
stream->Seek(1, STREAM_SEEK_CURRENT); // Pad107;
_legacyType.priority = stream->ReadValue<uint8_t>();
2023-01-21 16:39:35 +01:00
stream->Seek(1, STREAM_SEEK_CURRENT); // Pad109;
_legacyType.entertainer_costumes = stream->ReadValue<uint32_t>();
2016-06-26 00:06:55 +01:00
GetStringTable().Read(context, stream, ObjectStringID::NAME);
_items = ReadItems(stream);
2018-02-07 20:37:20 +00:00
GetImageTable().Read(context, stream);
2016-06-26 00:06:55 +01:00
}
void SceneryGroupObject::Load()
{
2017-12-11 13:06:59 +00:00
GetStringTable().Sort();
_legacyType.name = LanguageAllocateObjectString(GetName());
_legacyType.image = GfxObjectAllocateImages(GetImageTable().GetImages(), GetImageTable().GetCount());
_legacyType.SceneryEntries.clear();
2016-06-26 00:06:55 +01:00
}
void SceneryGroupObject::Unload()
{
LanguageFreeObjectString(_legacyType.name);
GfxObjectFreeImages(_legacyType.image, GetImageTable().GetCount());
_legacyType.name = 0;
_legacyType.image = 0;
2016-06-26 00:06:55 +01:00
}
void SceneryGroupObject::DrawPreview(DrawPixelInfo* dpi, int32_t width, int32_t height) const
2016-07-02 12:29:13 +01:00
{
auto screenCoords = ScreenCoordsXY{ width / 2, height / 2 };
2016-07-02 12:29:13 +01:00
2021-11-29 14:36:36 +01:00
const auto imageId = ImageId(_legacyType.image + 1, COLOUR_DARK_GREEN);
GfxDrawSprite(dpi, imageId, screenCoords - ScreenCoordsXY{ 15, 14 });
2016-07-02 12:29:13 +01:00
}
2021-09-27 21:54:41 +01:00
static std::optional<uint8_t> GetSceneryType(const ObjectType type)
{
switch (type)
{
case ObjectType::SmallScenery:
return SCENERY_TYPE_SMALL;
case ObjectType::LargeScenery:
return SCENERY_TYPE_LARGE;
case ObjectType::Walls:
return SCENERY_TYPE_WALL;
case ObjectType::Banners:
return SCENERY_TYPE_BANNER;
case ObjectType::PathBits:
return SCENERY_TYPE_PATH_ITEM;
default:
return std::nullopt;
}
}
2016-07-09 02:01:27 +01:00
void SceneryGroupObject::UpdateEntryIndexes()
{
auto context = GetContext();
2018-08-29 00:52:10 +02:00
auto& objectRepository = context->GetObjectRepository();
auto& objectManager = context->GetObjectManager();
2016-07-09 02:01:27 +01:00
_legacyType.SceneryEntries.clear();
2018-06-22 23:03:20 +02:00
for (const auto& objectEntry : _items)
2016-07-09 02:01:27 +01:00
{
auto ori = objectRepository.FindObject(objectEntry);
2018-06-22 23:03:20 +02:00
if (ori == nullptr)
continue;
if (ori->LoadedObject == nullptr)
continue;
2016-07-09 02:01:27 +01:00
2021-09-03 13:43:06 +03:00
auto entryIndex = objectManager.GetLoadedObjectEntryIndex(ori->LoadedObject.get());
if (entryIndex == OBJECT_ENTRY_INDEX_NULL)
{
// Some parks have manually deleted objects from the save so they might not be loaded
// silently remove the object from the SceneryGroupObject
continue;
}
2016-07-09 02:01:27 +01:00
2021-09-27 21:54:41 +01:00
auto sceneryType = GetSceneryType(ori->Type);
2021-09-13 19:47:13 +03:00
if (sceneryType.has_value())
2018-06-22 23:03:20 +02:00
{
_legacyType.SceneryEntries.push_back({ sceneryType.value(), entryIndex });
2016-07-09 02:01:27 +01:00
}
}
}
2018-06-22 23:03:20 +02:00
void SceneryGroupObject::SetRepositoryItem(ObjectRepositoryItem* item) const
{
2018-05-14 13:05:59 +01:00
item->SceneryGroupInfo.Entries = _items;
}
std::vector<ObjectEntryDescriptor> SceneryGroupObject::ReadItems(IStream* stream)
2016-06-26 00:06:55 +01:00
{
auto items = std::vector<ObjectEntryDescriptor>();
while (stream->ReadValue<uint8_t>() != 0xFF)
2016-06-26 00:06:55 +01:00
{
stream->Seek(-1, STREAM_SEEK_CURRENT);
auto entry = stream->ReadValue<RCTObjectEntry>();
items.emplace_back(entry);
2016-06-26 00:06:55 +01:00
}
return items;
2016-06-26 00:06:55 +01:00
}
2017-12-14 18:43:59 +00:00
2020-08-12 23:05:23 +01:00
void SceneryGroupObject::ReadJson(IReadObjectContext* context, json_t& root)
2017-12-14 18:43:59 +00:00
{
2020-08-12 23:05:23 +01:00
Guard::Assert(root.is_object(), "SceneryGroupObject::ReadJson expects parameter root to be object");
2017-12-14 18:43:59 +00:00
2020-08-12 23:05:23 +01:00
auto properties = root["properties"];
if (properties.is_object())
2017-12-14 18:43:59 +00:00
{
2022-12-05 07:36:10 +01:00
_legacyType.priority = Json::GetNumber<uint8_t>(properties["priority"], 40);
2020-08-12 23:05:23 +01:00
_legacyType.entertainer_costumes = ReadJsonEntertainerCostumes(properties["entertainerCostumes"]);
2022-01-23 15:40:08 +01:00
_items = ReadJsonEntries(context, properties["entries"]);
2017-12-14 18:43:59 +00:00
}
2020-08-12 23:05:23 +01:00
PopulateTablesFromJson(context, root);
2017-12-14 18:43:59 +00:00
}
2020-08-12 23:05:23 +01:00
uint32_t SceneryGroupObject::ReadJsonEntertainerCostumes(json_t& jCostumes)
2017-12-14 18:43:59 +00:00
{
uint32_t costumes = 0;
2020-08-12 23:05:23 +01:00
for (auto& jCostume : jCostumes)
2017-12-14 18:43:59 +00:00
{
2020-08-12 23:05:23 +01:00
auto entertainer = ParseEntertainerCostume(Json::GetString(jCostume));
auto peepSprite = EntertainerCostumeToSprite(entertainer);
costumes |= 1 << (static_cast<uint8_t>(peepSprite));
2017-12-14 18:43:59 +00:00
}
return costumes;
}
EntertainerCostume SceneryGroupObject::ParseEntertainerCostume(const std::string& s)
2017-12-14 18:43:59 +00:00
{
2018-06-22 23:03:20 +02:00
if (s == "panda")
return EntertainerCostume::Panda;
2018-06-22 23:03:20 +02:00
if (s == "tiger")
return EntertainerCostume::Tiger;
2018-06-22 23:03:20 +02:00
if (s == "elephant")
return EntertainerCostume::Elephant;
2018-06-22 23:03:20 +02:00
if (s == "roman")
return EntertainerCostume::Roman;
2018-06-22 23:03:20 +02:00
if (s == "gorilla")
return EntertainerCostume::Gorilla;
2018-06-22 23:03:20 +02:00
if (s == "snowman")
return EntertainerCostume::Snowman;
2018-06-22 23:03:20 +02:00
if (s == "knight")
return EntertainerCostume::Knight;
2018-06-22 23:03:20 +02:00
if (s == "astronaut")
return EntertainerCostume::Astronaut;
2018-06-22 23:03:20 +02:00
if (s == "bandit")
return EntertainerCostume::Bandit;
2018-06-22 23:03:20 +02:00
if (s == "sheriff")
return EntertainerCostume::Sheriff;
2018-06-22 23:03:20 +02:00
if (s == "pirate")
return EntertainerCostume::Pirate;
return EntertainerCostume::Panda;
2017-12-14 18:43:59 +00:00
}
2022-01-23 15:40:08 +01:00
std::vector<ObjectEntryDescriptor> SceneryGroupObject::ReadJsonEntries(IReadObjectContext* context, json_t& jEntries)
2017-12-14 18:43:59 +00:00
{
std::vector<ObjectEntryDescriptor> entries;
2020-08-12 23:05:23 +01:00
for (const auto& jEntry : jEntries)
2017-12-14 18:43:59 +00:00
{
auto entryName = Json::GetString(jEntry);
if (String::StartsWith(entryName, "$DAT:"))
{
2022-01-24 12:30:22 +01:00
if (entryName.length() != DatEntryLength)
{
2022-01-23 15:40:08 +01:00
std::string errorMessage = "Malformed DAT entry in scenery group: " + entryName;
context->LogError(ObjectError::InvalidProperty, errorMessage.c_str());
continue;
}
try
{
RCTObjectEntry entry = {};
2022-01-24 12:30:22 +01:00
entry.flags = std::stoul(entryName.substr(DatEntryFlagsStart, DatEntryFlagsLength), nullptr, 16);
std::memcpy(entry.name, entryName.c_str() + DatEntryNameStart, DAT_NAME_LENGTH);
entry.checksum = 0;
entries.emplace_back(entry);
}
catch (std::invalid_argument&)
{
2022-01-23 15:40:08 +01:00
std::string errorMessage = "Malformed flags in DAT entry in scenery group: " + entryName;
context->LogError(ObjectError::InvalidProperty, errorMessage.c_str());
}
}
else
{
entries.emplace_back(entryName);
}
2017-12-14 18:43:59 +00:00
}
return entries;
}
uint16_t SceneryGroupObject::GetNumIncludedObjects() const
{
return static_cast<uint16_t>(_items.size());
}