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

202 lines
7.0 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
2016-06-26 00:06:55 +01:00
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
2017-12-14 18:43:59 +00:00
#pragma warning(disable : 4706) // assignment within conditional expression
#include <unordered_map>
#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"
2016-06-26 00:06:55 +01:00
#include "SceneryGroupObject.h"
using namespace OpenRCT2;
2016-07-01 21:01:06 +01: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>();
2017-11-22 21:42:41 +00:00
_legacyType.pad_107 = stream->ReadValue<uint8>();
_legacyType.priority = stream->ReadValue<uint8>();
2016-06-26 00:06:55 +01:00
_legacyType.pad_109 = stream->ReadValue<uint8>();
2016-09-11 00:40:51 +01:00
_legacyType.entertainer_costumes = stream->ReadValue<uint32>();
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
}
2016-07-11 22:09:15 +01:00
void SceneryGroupObject::DrawPreview(rct_drawpixelinfo * dpi, sint32 width, sint32 height) const
2016-07-02 12:29:13 +01:00
{
2016-07-11 22:09:15 +01:00
sint32 x = width / 2;
sint32 y = height / 2;
2016-07-02 12:29:13 +01:00
uint32 imageId = _legacyType.image + 0x20600001;
gfx_draw_sprite(dpi, imageId, x - 15, y - 14, 0);
}
2016-07-09 02:01:27 +01:00
void SceneryGroupObject::UpdateEntryIndexes()
{
auto context = GetContext();
IObjectRepository * objectRepository = context->GetObjectRepository();
IObjectManager * objectManager = context->GetObjectManager();
2016-07-09 02:01:27 +01:00
_legacyType.entry_count = 0;
for (const auto &objectEntry : _items)
2016-07-09 02:01:27 +01:00
{
auto ori = objectRepository->FindObject(&objectEntry);
2016-07-09 02:01:27 +01:00
if (ori == nullptr) continue;
if (ori->LoadedObject == nullptr) continue;
uint16 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;
2016-07-09 02:01:27 +01:00
switch (objectType) {
case OBJECT_TYPE_SMALL_SCENERY: break;
case OBJECT_TYPE_PATH_BITS: sceneryEntry |= 0x100; break;
2017-12-14 18:43:59 +00:00
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;
}
if (sceneryEntry != 0xFFFF)
{
_legacyType.scenery_entries[_legacyType.entry_count] = sceneryEntry;
_legacyType.entry_count++;
2016-07-09 02:01:27 +01:00
}
}
}
void SceneryGroupObject::SetRepositoryItem(ObjectRepositoryItem * item) const
{
Memory::Free(item->ThemeObjects);
item->NumThemeObjects = (uint16)_items.size();
item->ThemeObjects = Memory::AllocateArray<rct_object_entry>(_items.size());
for (size_t i = 0; i < _items.size(); i++)
{
item->ThemeObjects[i] = _items[i];
}
}
std::vector<rct_object_entry> SceneryGroupObject::ReadItems(IStream * stream)
2016-06-26 00:06:55 +01:00
{
auto items = std::vector<rct_object_entry>();
2017-10-03 00:00:32 +02:00
while (stream->ReadValue<uint8>() != 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
void SceneryGroupObject::ReadJson(IReadObjectContext * context, const json_t * root)
{
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
}
uint32 SceneryGroupObject::ReadJsonEntertainerCostumes(const json_t * jCostumes)
{
uint32 costumes = 0;
size_t index;
json_t * jCostume;
json_array_foreach(jCostumes, index, jCostume)
{
auto costume = ObjectJsonHelpers::GetString(jCostume);
auto entertainer = ParseEntertainerCostume(costume);
// For some reason the flags are +4 from the actual costume IDs
// See staff_get_available_entertainer_costumes
costumes |= 1 << (entertainer + 4);
}
return costumes;
}
uint32 SceneryGroupObject::ParseEntertainerCostume(const std::string &s)
{
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;
return ENTERTAINER_COSTUME_PANDA;
}
std::vector<rct_object_entry> SceneryGroupObject::ReadJsonEntries(const json_t * jEntries)
{
std::vector<rct_object_entry> entries;
size_t index;
json_t * jEntry;
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;
}