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

156 lines
6.1 KiB
C++
Raw Normal View History

2016-06-25 22:59:24 +01:00
/*****************************************************************************
* Copyright (c) 2014-2020 OpenRCT2 developers
2016-06-25 22:59:24 +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-25 22:59:24 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2016-06-25 22:59:24 +01:00
*****************************************************************************/
2018-06-22 23:03:20 +02:00
#include "WallObject.h"
2016-06-25 22:59:24 +01:00
#include "../core/IStream.hpp"
#include "../core/Json.hpp"
2019-08-18 11:48:56 +02:00
#include "../core/String.hpp"
2018-01-05 21:17:33 +00:00
#include "../drawing/Drawing.h"
2017-12-17 00:51:13 +00:00
#include "../interface/Cursors.h"
2018-01-06 18:32:25 +01:00
#include "../localisation/Language.h"
2019-02-14 22:41:48 +01:00
#include "../world/Banner.h"
2016-06-25 22:59:24 +01:00
void WallObject::ReadLegacy(IReadObjectContext* context, OpenRCT2::IStream* stream)
2016-06-25 22:59:24 +01:00
{
stream->Seek(6, OpenRCT2::STREAM_SEEK_CURRENT);
_legacyType.wall.tool_id = static_cast<CursorID>(stream->ReadValue<uint8_t>());
_legacyType.wall.flags = stream->ReadValue<uint8_t>();
_legacyType.wall.height = stream->ReadValue<uint8_t>();
_legacyType.wall.flags2 = stream->ReadValue<uint8_t>();
_legacyType.wall.price = stream->ReadValue<uint16_t>();
2020-04-27 15:01:37 +02:00
_legacyType.wall.scenery_tab_id = OBJECT_ENTRY_INDEX_NULL;
stream->Seek(1, OpenRCT2::STREAM_SEEK_CURRENT);
_legacyType.wall.scrolling_mode = stream->ReadValue<uint8_t>();
2016-06-25 22:59:24 +01:00
GetStringTable().Read(context, stream, ObjectStringID::NAME);
2016-06-25 22:59:24 +01:00
rct_object_entry sgEntry = stream->ReadValue<rct_object_entry>();
SetPrimarySceneryGroup(&sgEntry);
2016-06-25 22:59:24 +01:00
2017-12-11 13:06:59 +00:00
GetImageTable().Read(context, stream);
2016-07-02 17:23:06 +01:00
// Validate properties
2016-07-09 23:03:09 +01:00
if (_legacyType.wall.price <= 0)
2016-07-02 17:23:06 +01:00
{
context->LogError(ObjectError::InvalidProperty, "Price can not be free or negative.");
2016-07-02 17:23:06 +01:00
}
2019-08-18 11:48:56 +02:00
// Autofix this object (will be turned into an official object later).
auto identifier = GetLegacyIdentifier();
if (identifier == "XXWLBR03")
2019-08-18 11:48:56 +02:00
{
_legacyType.wall.flags2 &= ~WALL_SCENERY_2_DOOR_SOUND_MASK;
_legacyType.wall.flags2 |= (1u << WALL_SCENERY_2_DOOR_SOUND_SHIFT) & WALL_SCENERY_2_DOOR_SOUND_MASK;
}
2016-06-25 22:59:24 +01:00
}
void WallObject::Load()
{
2017-12-11 13:06:59 +00:00
GetStringTable().Sort();
2016-06-25 22:59:24 +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-25 22:59:24 +01:00
}
void WallObject::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-25 22:59:24 +01:00
}
2018-06-22 23:03:20 +02:00
void WallObject::DrawPreview(rct_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
screenCoords.x += 14;
screenCoords.y += (_legacyType.wall.height * 2) + 16;
2016-07-02 12:29:13 +01:00
uint32_t imageId = 0x20D00000 | _legacyType.image;
2016-07-02 12:29:13 +01:00
if (_legacyType.wall.flags & WALL_SCENERY_HAS_SECONDARY_COLOUR)
{
imageId |= 0x92000000;
}
gfx_draw_sprite(dpi, imageId, screenCoords, 0);
2016-07-02 12:29:13 +01:00
2017-02-02 18:52:35 +00:00
if (_legacyType.wall.flags & WALL_SCENERY_HAS_GLASS)
2016-07-02 12:29:13 +01:00
{
imageId = _legacyType.image + 0x44500006;
gfx_draw_sprite(dpi, imageId, screenCoords, 0);
2016-07-02 12:29:13 +01:00
}
else if (_legacyType.wall.flags & WALL_SCENERY_IS_DOOR)
{
imageId++;
gfx_draw_sprite(dpi, imageId, screenCoords, 0);
2016-07-02 12:29:13 +01:00
}
}
2017-12-17 00:51:13 +00:00
2020-08-12 23:05:23 +01:00
void WallObject::ReadJson(IReadObjectContext* context, json_t& root)
2017-12-17 00:51:13 +00:00
{
2020-08-12 23:05:23 +01:00
Guard::Assert(root.is_object(), "WallObject::ReadJson expects parameter root to be object");
2017-12-17 00:51:13 +00:00
2020-08-12 23:05:23 +01:00
auto properties = root["properties"];
2017-12-17 00:51:13 +00:00
2020-08-12 23:05:23 +01:00
if (properties.is_object())
2017-12-17 00:51:13 +00:00
{
_legacyType.wall.tool_id = Cursor::FromString(Json::GetString(properties["cursor"]), CursorID::FenceDown);
2020-08-12 23:05:23 +01:00
_legacyType.wall.height = Json::GetNumber<uint8_t>(properties["height"]);
_legacyType.wall.price = Json::GetNumber<int16_t>(properties["price"]);
2017-12-17 00:51:13 +00:00
2020-08-12 23:05:23 +01:00
_legacyType.wall.scrolling_mode = Json::GetNumber<uint8_t>(properties["scrollingMode"], SCROLLING_MODE_NONE);
SetPrimarySceneryGroup(Json::GetString(properties["sceneryGroup"]));
// clang-format off
_legacyType.wall.flags = Json::GetFlags<uint8_t>(
properties,
{
{ "hasPrimaryColour", WALL_SCENERY_HAS_PRIMARY_COLOUR, Json::FlagType::Normal },
{ "isAllowedOnSlope", WALL_SCENERY_CANT_BUILD_ON_SLOPE, Json::FlagType::Inverted },
2020-08-12 23:05:23 +01:00
{ "hasSecondaryColour", WALL_SCENERY_HAS_SECONDARY_COLOUR, Json::FlagType::Normal },
{ "hasTernaryColour", WALL_SCENERY_HAS_TERNARY_COLOUR, Json::FlagType::Normal },
{ "hasGlass", WALL_SCENERY_HAS_GLASS, Json::FlagType::Normal },
{ "isBanner", WALL_SCENERY_IS_BANNER, Json::FlagType::Normal },
{ "isDoor", WALL_SCENERY_IS_DOOR, Json::FlagType::Normal },
{ "isLongDoorAnimation", WALL_SCENERY_LONG_DOOR_ANIMATION, Json::FlagType::Normal },
});
// clang-format on
_legacyType.wall.flags2 = Json::GetFlags<uint8_t>(
properties,
{
{ "isOpaque", WALL_SCENERY_2_IS_OPAQUE },
{ "isAnimated", WALL_SCENERY_2_ANIMATED },
});
// HACK WALL_SCENERY_HAS_PRIMARY_COLOUR actually means, has any colour but we simplify the
// JSON and handle this on load. We should change code base in future to reflect the JSON.
if (!(_legacyType.wall.flags & WALL_SCENERY_HAS_PRIMARY_COLOUR))
2017-12-17 00:51:13 +00:00
{
2020-08-12 23:05:23 +01:00
if (_legacyType.wall.flags & (WALL_SCENERY_HAS_SECONDARY_COLOUR | WALL_SCENERY_HAS_TERNARY_COLOUR))
{
_legacyType.wall.flags |= WALL_SCENERY_HAS_PRIMARY_COLOUR;
_legacyType.wall.flags2 |= WALL_SCENERY_2_NO_SELECT_PRIMARY_COLOUR;
}
}
// Door sound
auto jDoorSound = properties["doorSound"];
if (jDoorSound.is_number())
{
auto doorSound = Json::GetNumber<uint8_t>(jDoorSound);
_legacyType.wall.flags2 |= (doorSound << WALL_SCENERY_2_DOOR_SOUND_SHIFT) & WALL_SCENERY_2_DOOR_SOUND_MASK;
2017-12-17 00:51:13 +00:00
}
}
2020-08-12 23:05:23 +01:00
PopulateTablesFromJson(context, root);
2017-12-17 00:51:13 +00:00
}