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

213 lines
6.6 KiB
C++
Raw Normal View History

2016-06-26 00:06:55 +01:00
/*****************************************************************************
2019-03-17 08:16:15 +01:00
* Copyright (c) 2014-2019 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/Memory.hpp"
2017-12-14 18:43:59 +00:00
#include "../core/String.hpp"
#include "../drawing/Drawing.h"
#include "../localisation/Language.h"
#include "../peep/Staff.h"
#include "ObjectJsonHelpers.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;
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);
_legacyType.entry_count = stream->ReadValue<uint8_t>();
_legacyType.pad_107 = stream->ReadValue<uint8_t>();
_legacyType.priority = stream->ReadValue<uint8_t>();
_legacyType.pad_109 = stream->ReadValue<uint8_t>();
_legacyType.entertainer_costumes = stream->ReadValue<uint32_t>();
2016-06-26 00:06:55 +01:00
2018-02-07 20:37:20 +00:00
GetStringTable().Read(context, stream, OBJ_STRING_ID_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();
2016-06-26 00:06:55 +01:00
_legacyType.name = language_allocate_object_string(GetName());
2017-12-11 13:06:59 +00:00
_legacyType.image = gfx_object_allocate_images(GetImageTable().GetImages(), GetImageTable().GetCount());
2016-06-26 00:06:55 +01:00
_legacyType.entry_count = 0;
}
void SceneryGroupObject::Unload()
{
language_free_object_string(_legacyType.name);
2017-12-11 13:06:59 +00:00
gfx_object_free_images(_legacyType.image, GetImageTable().GetCount());
_legacyType.name = 0;
_legacyType.image = 0;
2016-06-26 00:06:55 +01: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 12:29:13 +01:00
{
int32_t x = width / 2;
int32_t y = height / 2;
2016-07-02 12:29:13 +01:00
uint32_t imageId = _legacyType.image + 0x20600001;
2016-07-02 12:29:13 +01:00
gfx_draw_sprite(dpi, imageId, x - 15, y - 14, 0);
}
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.entry_count = 0;
2018-06-22 23:03:20 +02:00
for (const auto& objectEntry : _items)
2016-07-09 02:01:27 +01:00
{
2018-08-29 00:52:10 +02: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
2018-08-29 00:52:10 +02:00
uint16_t sceneryEntry = objectManager.GetLoadedObjectEntryIndex(ori->LoadedObject);
2016-07-16 14:17:36 +01:00
Guard::Assert(sceneryEntry != UINT8_MAX, GUARD_LINE);
2016-07-09 02:01:27 +01:00
2017-12-14 18:43:59 +00:00
auto objectType = ori->ObjectEntry.flags & 0x0F;
2018-06-22 23:03:20 +02:00
switch (objectType)
{
case OBJECT_TYPE_SMALL_SCENERY:
break;
case OBJECT_TYPE_PATH_BITS:
sceneryEntry |= 0x100;
break;
case OBJECT_TYPE_WALLS:
sceneryEntry |= 0x200;
break;
case OBJECT_TYPE_LARGE_SCENERY:
sceneryEntry |= 0x300;
break;
case OBJECT_TYPE_BANNERS:
sceneryEntry |= 0x400;
break;
default:
sceneryEntry = 0xFFFF;
break;
2017-12-14 18:43:59 +00:00
}
if (sceneryEntry != 0xFFFF)
{
_legacyType.scenery_entries[_legacyType.entry_count] = sceneryEntry;
_legacyType.entry_count++;
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;
}
2018-06-22 23:03:20 +02:00
std::vector<rct_object_entry> SceneryGroupObject::ReadItems(IStream* stream)
2016-06-26 00:06:55 +01:00
{
auto items = std::vector<rct_object_entry>();
while (stream->ReadValue<uint8_t>() != 0xFF)
2016-06-26 00:06:55 +01:00
{
stream->Seek(-1, STREAM_SEEK_CURRENT);
2017-12-14 18:43:59 +00:00
auto entry = stream->ReadValue<rct_object_entry>();
2016-06-26 00:06:55 +01:00
items.push_back(entry);
}
return items;
2016-06-26 00:06:55 +01:00
}
2017-12-14 18:43:59 +00:00
2018-06-22 23:03:20 +02:00
void SceneryGroupObject::ReadJson(IReadObjectContext* context, const json_t* root)
2017-12-14 18:43:59 +00:00
{
auto properties = json_object_get(root, "properties");
_legacyType.priority = json_integer_value(json_object_get(properties, "priority"));
// Entertainer cosumes
auto jCostumes = json_object_get(properties, "entertainerCostumes");
if (jCostumes != nullptr)
{
_legacyType.entertainer_costumes = ReadJsonEntertainerCostumes(jCostumes);
}
auto jEntries = json_object_get(properties, "entries");
if (jEntries != nullptr)
{
_items = ReadJsonEntries(jEntries);
}
ObjectJsonHelpers::LoadStrings(root, GetStringTable());
2018-02-08 21:31:30 +00:00
ObjectJsonHelpers::LoadImages(context, root, GetImageTable());
2017-12-14 18:43:59 +00:00
}
2018-06-22 23:03:20 +02:00
uint32_t SceneryGroupObject::ReadJsonEntertainerCostumes(const json_t* jCostumes)
2017-12-14 18:43:59 +00:00
{
uint32_t costumes = 0;
auto szCostumes = ObjectJsonHelpers::GetJsonStringArray(jCostumes);
for (const auto& szCostume : szCostumes)
2017-12-14 18:43:59 +00:00
{
auto entertainer = ParseEntertainerCostume(szCostume);
2017-12-14 18:43:59 +00:00
// For some reason the flags are +4 from the actual costume IDs
// See staff_get_available_entertainer_costumes
costumes |= 1 << (entertainer + 4);
}
return costumes;
}
2018-06-22 23:03:20 +02:00
uint32_t 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 ENTERTAINER_COSTUME_PANDA;
if (s == "tiger")
return ENTERTAINER_COSTUME_TIGER;
if (s == "elephant")
return ENTERTAINER_COSTUME_ELEPHANT;
if (s == "roman")
return ENTERTAINER_COSTUME_ROMAN;
if (s == "gorilla")
return ENTERTAINER_COSTUME_GORILLA;
if (s == "snowman")
return ENTERTAINER_COSTUME_SNOWMAN;
if (s == "knight")
return ENTERTAINER_COSTUME_KNIGHT;
if (s == "astronaut")
return ENTERTAINER_COSTUME_ASTRONAUT;
if (s == "bandit")
return ENTERTAINER_COSTUME_BANDIT;
if (s == "sheriff")
return ENTERTAINER_COSTUME_SHERIFF;
if (s == "pirate")
return ENTERTAINER_COSTUME_PIRATE;
2017-12-14 18:43:59 +00:00
return ENTERTAINER_COSTUME_PANDA;
}
2018-06-22 23:03:20 +02:00
std::vector<rct_object_entry> SceneryGroupObject::ReadJsonEntries(const json_t* jEntries)
2017-12-14 18:43:59 +00:00
{
std::vector<rct_object_entry> entries;
size_t index;
2018-06-22 23:03:20 +02:00
json_t* jEntry;
2017-12-14 18:43:59 +00:00
json_array_foreach(jEntries, index, jEntry)
{
auto entryId = json_string_value(jEntry);
if (entryId != nullptr)
{
auto entry = ObjectJsonHelpers::ParseObjectEntry(entryId);
entries.push_back(entry);
}
}
return entries;
}