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

149 lines
5.6 KiB
C++
Raw Normal View History

2017-06-01 21:55:10 +02:00
#pragma region Copyright (c) 2014-2017 OpenRCT2 Developers
2016-06-25 22:59:24 +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
#include "../core/IStream.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"
2017-12-17 00:51:13 +00:00
#include "ObjectJsonHelpers.h"
#include "WallObject.h"
2016-06-25 22:59:24 +01:00
2016-07-01 21:01:06 +01:00
void WallObject::ReadLegacy(IReadObjectContext * context, IStream * stream)
2016-06-25 22:59:24 +01:00
{
stream->Seek(6, STREAM_SEEK_CURRENT);
2016-06-25 22:59:24 +01:00
_legacyType.wall.tool_id = stream->ReadValue<uint8>();
_legacyType.wall.flags = stream->ReadValue<uint8>();
_legacyType.wall.height = stream->ReadValue<uint8>();
_legacyType.wall.flags2 = stream->ReadValue<uint8>();
_legacyType.wall.price = stream->ReadValue<uint16>();
_legacyType.wall.scenery_tab_id = stream->ReadValue<uint8>();
2017-02-02 18:52:35 +00:00
_legacyType.wall.scrolling_mode = stream->ReadValue<uint8>();
2016-06-25 22:59:24 +01:00
2017-12-11 13:06:59 +00:00
GetStringTable().Read(context, stream, OBJ_STRING_ID_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(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative.");
}
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
}
2016-07-11 22:09:15 +01:00
void WallObject::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
x += 14;
y += (_legacyType.wall.height * 2) + 16;
uint32 imageId = 0x20D00000 | _legacyType.image;
if (_legacyType.wall.flags & WALL_SCENERY_HAS_SECONDARY_COLOUR)
{
imageId |= 0x92000000;
}
gfx_draw_sprite(dpi, imageId, x, y, 0);
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, x, y, 0);
}
else if (_legacyType.wall.flags & WALL_SCENERY_IS_DOOR)
{
imageId++;
gfx_draw_sprite(dpi, imageId, x, y, 0);
}
}
2017-12-17 00:51:13 +00:00
void WallObject::ReadJson(IReadObjectContext * context, const json_t * root)
{
auto properties = json_object_get(root, "properties");
_legacyType.wall.tool_id = ObjectJsonHelpers::ParseCursor(ObjectJsonHelpers::GetString(properties, "cursor"), CURSOR_FENCE_DOWN);
_legacyType.wall.height = json_integer_value(json_object_get(properties, "height"));
_legacyType.wall.price = json_integer_value(json_object_get(properties, "price"));
auto jScrollingMode = json_object_get(properties, "scrollingMode");
_legacyType.wall.scrolling_mode = jScrollingMode != nullptr ?
json_integer_value(jScrollingMode) :
-1;
SetPrimarySceneryGroup(ObjectJsonHelpers::GetString(json_object_get(properties, "sceneryGroup")));
// Flags
_legacyType.wall.flags = ObjectJsonHelpers::GetFlags<uint8>(properties, {
{ "hasPrimaryColour", WALL_SCENERY_HAS_PRIMARY_COLOUR },
{ "hasSecondaryColour", WALL_SCENERY_HAS_SECONDARY_COLOUR },
{ "hasTernaryColour", WALL_SCENERY_HAS_TERNARY_COLOUR },
2017-12-17 12:55:56 +00:00
{ "hasGlass", WALL_SCENERY_HAS_GLASS },
2017-12-17 00:51:13 +00:00
{ "isBanner", WALL_SCENERY_IS_BANNER },
{ "isDoor", WALL_SCENERY_IS_DOOR },
{ "isLongDoorAnimation", WALL_SCENERY_LONG_DOOR_ANIMATION }});
_legacyType.wall.flags2 = ObjectJsonHelpers::GetFlags<uint8>(properties, {
{ "isOpaque", WALL_SCENERY_2_IS_OPAQUE },
{ "isAnimated", WALL_SCENERY_2_ANIMATED }});
// HACK To avoid 'negated' properties in JSON, handle this separately until
// flag is inverted in this code base.
if (!ObjectJsonHelpers::GetBoolean(properties, "isAllowedOnSlope", false))
{
_legacyType.wall.flags |= WALL_SCENERY_CANT_BUILD_ON_SLOPE;
}
// 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))
{
if ((_legacyType.wall.flags & WALL_SCENERY_HAS_SECONDARY_COLOUR) ||
(_legacyType.wall.flags & WALL_SCENERY_HAS_TERNARY_COLOUR))
{
_legacyType.wall.flags2 |= WALL_SCENERY_2_NO_SELECT_PRIMARY_COLOUR;
}
}
// Door sound
auto jDoorSound = json_object_get(properties, "scrollingMode");
if (jDoorSound != nullptr)
{
auto doorSound = json_integer_value(jDoorSound);
_legacyType.wall.flags2 |= (doorSound << WALL_SCENERY_2_DOOR_SOUND_SHIFT) & WALL_SCENERY_2_DOOR_SOUND_MASK;
}
ObjectJsonHelpers::LoadStrings(root, GetStringTable());
2018-02-08 21:31:30 +00:00
ObjectJsonHelpers::LoadImages(context, root, GetImageTable());
2017-12-17 00:51:13 +00:00
}