2016-06-26 01:06:55 +02:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2016-06-26 01:06:55 +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
|
2016-06-26 01:06:55 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-06-26 01:06:55 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-12-14 19:43:59 +01:00
|
|
|
#pragma warning(disable : 4706) // assignment within conditional expression
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include "SceneryGroupObject.h"
|
|
|
|
|
2018-04-10 14:19:23 +02:00
|
|
|
#include "../Context.h"
|
2016-06-26 01:06:55 +02:00
|
|
|
#include "../core/IStream.hpp"
|
2020-10-02 12:31:20 +02:00
|
|
|
#include "../core/Json.hpp"
|
2016-06-26 01:06:55 +02:00
|
|
|
#include "../core/Memory.hpp"
|
2017-12-14 19:43:59 +01:00
|
|
|
#include "../core/String.hpp"
|
|
|
|
#include "../drawing/Drawing.h"
|
|
|
|
#include "../localisation/Language.h"
|
|
|
|
#include "../peep/Staff.h"
|
2016-07-09 03:01:27 +02:00
|
|
|
#include "ObjectManager.h"
|
2016-07-02 17:52:44 +02:00
|
|
|
#include "ObjectRepository.h"
|
2018-06-22 23:03:20 +02:00
|
|
|
|
|
|
|
#include <unordered_map>
|
2016-06-26 01:06:55 +02:00
|
|
|
|
2018-04-10 14:19:23 +02:00
|
|
|
using namespace OpenRCT2;
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void SceneryGroupObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
|
2016-06-26 01:06:55 +02:00
|
|
|
{
|
2016-07-03 19:02:18 +02:00
|
|
|
stream->Seek(6, STREAM_SEEK_CURRENT);
|
2016-06-26 01:06:55 +02:00
|
|
|
stream->Seek(0x80 * 2, STREAM_SEEK_CURRENT);
|
2018-06-20 17:28:51 +02:00
|
|
|
_legacyType.entry_count = stream->ReadValue<uint8_t>();
|
2020-03-13 14:44:34 +01:00
|
|
|
stream->Seek(1, STREAM_SEEK_CURRENT); // pad_107;
|
2018-06-20 17:28:51 +02:00
|
|
|
_legacyType.priority = stream->ReadValue<uint8_t>();
|
2020-03-13 14:44:34 +01:00
|
|
|
stream->Seek(1, STREAM_SEEK_CURRENT); // pad_109;
|
2018-06-20 17:28:51 +02:00
|
|
|
_legacyType.entertainer_costumes = stream->ReadValue<uint32_t>();
|
2016-06-26 01:06:55 +02:00
|
|
|
|
2020-08-29 04:36:58 +02:00
|
|
|
GetStringTable().Read(context, stream, ObjectStringID::NAME);
|
2018-02-07 20:56:46 +01:00
|
|
|
_items = ReadItems(stream);
|
2018-02-07 21:37:20 +01:00
|
|
|
GetImageTable().Read(context, stream);
|
2016-06-26 01:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SceneryGroupObject::Load()
|
|
|
|
{
|
2017-12-11 14:06:59 +01:00
|
|
|
GetStringTable().Sort();
|
2016-06-26 01:06:55 +02:00
|
|
|
_legacyType.name = language_allocate_object_string(GetName());
|
2017-12-11 14:06:59 +01:00
|
|
|
_legacyType.image = gfx_object_allocate_images(GetImageTable().GetImages(), GetImageTable().GetCount());
|
2016-06-26 01:06:55 +02:00
|
|
|
_legacyType.entry_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SceneryGroupObject::Unload()
|
|
|
|
{
|
|
|
|
language_free_object_string(_legacyType.name);
|
2017-12-11 14:06:59 +01:00
|
|
|
gfx_object_free_images(_legacyType.image, GetImageTable().GetCount());
|
2016-07-03 19:02:18 +02:00
|
|
|
|
|
|
|
_legacyType.name = 0;
|
|
|
|
_legacyType.image = 0;
|
2016-06-26 01:06:55 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void SceneryGroupObject::DrawPreview(rct_drawpixelinfo* dpi, int32_t width, int32_t height) const
|
2016-07-02 13:29:13 +02:00
|
|
|
{
|
2020-06-23 23:32:34 +02:00
|
|
|
auto screenCoords = ScreenCoordsXY{ width / 2, height / 2 };
|
2016-07-02 13:29:13 +02:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t imageId = _legacyType.image + 0x20600001;
|
2020-06-23 23:32:34 +02:00
|
|
|
gfx_draw_sprite(dpi, imageId, screenCoords - ScreenCoordsXY{ 15, 14 }, 0);
|
2016-07-02 13:29:13 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 22:54:41 +02: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 03:01:27 +02:00
|
|
|
void SceneryGroupObject::UpdateEntryIndexes()
|
|
|
|
{
|
2018-04-10 14:19:23 +02:00
|
|
|
auto context = GetContext();
|
2018-08-29 00:52:10 +02:00
|
|
|
auto& objectRepository = context->GetObjectRepository();
|
|
|
|
auto& objectManager = context->GetObjectManager();
|
2016-07-09 03:01:27 +02:00
|
|
|
|
|
|
|
_legacyType.entry_count = 0;
|
2018-06-22 23:03:20 +02:00
|
|
|
for (const auto& objectEntry : _items)
|
2016-07-09 03:01:27 +02:00
|
|
|
{
|
2020-12-02 16:50:21 +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 03:01:27 +02:00
|
|
|
|
2021-09-03 12:43:06 +02:00
|
|
|
auto entryIndex = objectManager.GetLoadedObjectEntryIndex(ori->LoadedObject.get());
|
2020-03-18 21:27:53 +01:00
|
|
|
Guard::Assert(entryIndex != OBJECT_ENTRY_INDEX_NULL, GUARD_LINE);
|
2016-07-09 03:01:27 +02:00
|
|
|
|
2021-09-27 22:54:41 +02:00
|
|
|
auto sceneryType = GetSceneryType(ori->Type);
|
2021-09-13 18:47:13 +02:00
|
|
|
if (sceneryType.has_value())
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2021-09-13 18:47:13 +02:00
|
|
|
_legacyType.scenery_entries[_legacyType.entry_count] = { sceneryType.value(), entryIndex };
|
2017-12-14 19:43:59 +01:00
|
|
|
_legacyType.entry_count++;
|
2016-07-09 03:01:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void SceneryGroupObject::SetRepositoryItem(ObjectRepositoryItem* item) const
|
2016-07-02 17:52:44 +02:00
|
|
|
{
|
2018-05-14 14:05:59 +02:00
|
|
|
item->SceneryGroupInfo.Entries = _items;
|
2016-07-02 17:52:44 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 16:50:21 +01:00
|
|
|
std::vector<ObjectEntryDescriptor> SceneryGroupObject::ReadItems(IStream* stream)
|
2016-06-26 01:06:55 +02:00
|
|
|
{
|
2020-12-02 16:50:21 +01:00
|
|
|
auto items = std::vector<ObjectEntryDescriptor>();
|
2018-06-20 17:28:51 +02:00
|
|
|
while (stream->ReadValue<uint8_t>() != 0xFF)
|
2016-06-26 01:06:55 +02:00
|
|
|
{
|
|
|
|
stream->Seek(-1, STREAM_SEEK_CURRENT);
|
2017-12-14 19:43:59 +01:00
|
|
|
auto entry = stream->ReadValue<rct_object_entry>();
|
2021-01-08 21:59:55 +01:00
|
|
|
items.emplace_back(entry);
|
2016-06-26 01:06:55 +02:00
|
|
|
}
|
2018-02-07 20:56:46 +01:00
|
|
|
return items;
|
2016-06-26 01:06:55 +02:00
|
|
|
}
|
2017-12-14 19:43:59 +01:00
|
|
|
|
2020-08-13 00:05:23 +02:00
|
|
|
void SceneryGroupObject::ReadJson(IReadObjectContext* context, json_t& root)
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2020-08-13 00:05:23 +02:00
|
|
|
Guard::Assert(root.is_object(), "SceneryGroupObject::ReadJson expects parameter root to be object");
|
2017-12-14 19:43:59 +01:00
|
|
|
|
2020-08-13 00:05:23 +02:00
|
|
|
auto properties = root["properties"];
|
2017-12-14 19:43:59 +01:00
|
|
|
|
2020-08-13 00:05:23 +02:00
|
|
|
if (properties.is_object())
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2020-08-13 00:05:23 +02:00
|
|
|
_legacyType.priority = Json::GetNumber<uint8_t>(properties["priority"]);
|
|
|
|
_legacyType.entertainer_costumes = ReadJsonEntertainerCostumes(properties["entertainerCostumes"]);
|
|
|
|
|
|
|
|
_items = ReadJsonEntries(properties["entries"]);
|
2017-12-14 19:43:59 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 00:05:23 +02:00
|
|
|
PopulateTablesFromJson(context, root);
|
2017-12-14 19:43:59 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 00:05:23 +02:00
|
|
|
uint32_t SceneryGroupObject::ReadJsonEntertainerCostumes(json_t& jCostumes)
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t costumes = 0;
|
2020-08-13 00:05:23 +02:00
|
|
|
for (auto& jCostume : jCostumes)
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2020-08-13 00:05:23 +02:00
|
|
|
auto entertainer = ParseEntertainerCostume(Json::GetString(jCostume));
|
2020-09-08 21:29:18 +02:00
|
|
|
auto peepSprite = EntertainerCostumeToSprite(entertainer);
|
|
|
|
costumes |= 1 << (static_cast<uint8_t>(peepSprite));
|
2017-12-14 19:43:59 +01:00
|
|
|
}
|
|
|
|
return costumes;
|
|
|
|
}
|
|
|
|
|
2020-09-08 21:29:18 +02:00
|
|
|
EntertainerCostume SceneryGroupObject::ParseEntertainerCostume(const std::string& s)
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "panda")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Panda;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "tiger")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Tiger;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "elephant")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Elephant;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "roman")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Roman;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "gorilla")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Gorilla;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "snowman")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Snowman;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "knight")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Knight;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "astronaut")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Astronaut;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "bandit")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Bandit;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "sheriff")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Sheriff;
|
2018-06-22 23:03:20 +02:00
|
|
|
if (s == "pirate")
|
2020-09-08 21:29:18 +02:00
|
|
|
return EntertainerCostume::Pirate;
|
|
|
|
return EntertainerCostume::Panda;
|
2017-12-14 19:43:59 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 16:50:21 +01:00
|
|
|
std::vector<ObjectEntryDescriptor> SceneryGroupObject::ReadJsonEntries(json_t& jEntries)
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2020-12-02 16:50:21 +01:00
|
|
|
std::vector<ObjectEntryDescriptor> entries;
|
2020-08-13 00:05:23 +02:00
|
|
|
|
2021-01-08 21:59:55 +01:00
|
|
|
for (const auto& jEntry : jEntries)
|
2017-12-14 19:43:59 +01:00
|
|
|
{
|
2021-01-08 21:59:55 +01:00
|
|
|
entries.emplace_back(Json::GetString(jEntry));
|
2017-12-14 19:43:59 +01:00
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
2021-05-26 17:00:50 +02:00
|
|
|
|
|
|
|
uint16_t SceneryGroupObject::GetNumIncludedObjects() const
|
|
|
|
{
|
|
|
|
return static_cast<uint16_t>(_items.size());
|
|
|
|
}
|