2016-06-25 23:47:09 +02:00
|
|
|
/*****************************************************************************
|
2018-06-15 14:07:34 +02:00
|
|
|
* Copyright (c) 2014-2018 OpenRCT2 developers
|
2016-06-25 23:47:09 +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-25 23:47:09 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-06-25 23:47:09 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-12-17 18:00:45 +01:00
|
|
|
#pragma warning(disable : 4706) // assignment within conditional expression
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include "LargeSceneryObject.h"
|
|
|
|
|
2016-06-25 23:47:09 +02:00
|
|
|
#include "../core/IStream.hpp"
|
|
|
|
#include "../core/Memory.hpp"
|
2018-01-05 22:17:33 +01:00
|
|
|
#include "../drawing/Drawing.h"
|
2017-12-17 18:00:45 +01:00
|
|
|
#include "../interface/Cursors.h"
|
2018-01-06 18:32:25 +01:00
|
|
|
#include "../localisation/Language.h"
|
2018-05-01 19:12:54 +02:00
|
|
|
#include "../world/Location.hpp"
|
2017-12-17 18:00:45 +01:00
|
|
|
#include "ObjectJsonHelpers.h"
|
2016-06-25 23:47:09 +02:00
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include <algorithm>
|
2018-11-21 23:16:04 +01:00
|
|
|
#include <iterator>
|
2018-06-22 23:03:20 +02:00
|
|
|
|
|
|
|
void LargeSceneryObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
|
2016-06-25 23:47:09 +02:00
|
|
|
{
|
2016-07-03 19:02:18 +02:00
|
|
|
stream->Seek(6, STREAM_SEEK_CURRENT);
|
2018-06-20 17:28:51 +02:00
|
|
|
_legacyType.large_scenery.tool_id = stream->ReadValue<uint8_t>();
|
|
|
|
_legacyType.large_scenery.flags = stream->ReadValue<uint8_t>();
|
|
|
|
_legacyType.large_scenery.price = stream->ReadValue<int16_t>();
|
|
|
|
_legacyType.large_scenery.removal_price = stream->ReadValue<int16_t>();
|
2016-07-10 01:15:27 +02:00
|
|
|
stream->Seek(5, STREAM_SEEK_CURRENT);
|
2016-06-25 23:47:09 +02:00
|
|
|
_legacyType.large_scenery.scenery_tab_id = 0xFF;
|
2018-06-20 17:28:51 +02:00
|
|
|
_legacyType.large_scenery.scrolling_mode = stream->ReadValue<uint8_t>();
|
2016-07-10 01:15:27 +02:00
|
|
|
stream->Seek(4, STREAM_SEEK_CURRENT);
|
2016-06-25 23:47:09 +02:00
|
|
|
|
2017-12-11 14:06:59 +01:00
|
|
|
GetStringTable().Read(context, stream, OBJ_STRING_ID_NAME);
|
2016-06-25 23:47:09 +02:00
|
|
|
|
2016-07-09 20:16:38 +02:00
|
|
|
rct_object_entry sgEntry = stream->ReadValue<rct_object_entry>();
|
|
|
|
SetPrimarySceneryGroup(&sgEntry);
|
2016-06-25 23:47:09 +02:00
|
|
|
|
2017-02-04 09:12:44 +01:00
|
|
|
if (_legacyType.large_scenery.flags & LARGE_SCENERY_FLAG_3D_TEXT)
|
2016-06-25 23:47:09 +02:00
|
|
|
{
|
2018-02-07 20:56:46 +01:00
|
|
|
_3dFont = std::make_unique<rct_large_scenery_text>();
|
|
|
|
stream->Read(_3dFont.get());
|
|
|
|
_legacyType.large_scenery.text = _3dFont.get();
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_tiles = ReadTiles(stream);
|
|
|
|
|
2017-12-11 14:06:59 +01:00
|
|
|
GetImageTable().Read(context, stream);
|
2016-07-02 18:23:06 +02:00
|
|
|
|
|
|
|
// Validate properties
|
|
|
|
if (_legacyType.large_scenery.price <= 0)
|
|
|
|
{
|
|
|
|
context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Price can not be free or negative.");
|
|
|
|
}
|
|
|
|
if (_legacyType.large_scenery.removal_price <= 0)
|
|
|
|
{
|
|
|
|
// Make sure you don't make a profit when placing then removing.
|
|
|
|
money16 reimbursement = _legacyType.large_scenery.removal_price;
|
|
|
|
if (reimbursement > _legacyType.large_scenery.price)
|
|
|
|
{
|
|
|
|
context->LogError(OBJECT_ERROR_INVALID_PROPERTY, "Sell price can not be more than buy price.");
|
|
|
|
}
|
|
|
|
}
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LargeSceneryObject::Load()
|
|
|
|
{
|
2017-12-11 14:06:59 +01:00
|
|
|
GetStringTable().Sort();
|
2016-06-25 23:47:09 +02:00
|
|
|
_legacyType.name = language_allocate_object_string(GetName());
|
2017-12-11 14:06:59 +01:00
|
|
|
_baseImageId = gfx_object_allocate_images(GetImageTable().GetImages(), GetImageTable().GetCount());
|
2016-07-03 20:48:21 +02:00
|
|
|
_legacyType.image = _baseImageId;
|
2016-06-25 23:47:09 +02:00
|
|
|
|
2018-02-07 20:56:46 +01:00
|
|
|
_legacyType.large_scenery.tiles = _tiles.data();
|
2016-06-26 17:55:46 +02:00
|
|
|
|
2017-02-04 09:12:44 +01:00
|
|
|
if (_legacyType.large_scenery.flags & LARGE_SCENERY_FLAG_3D_TEXT)
|
2016-06-25 23:47:09 +02:00
|
|
|
{
|
|
|
|
_legacyType.large_scenery.text_image = _legacyType.image;
|
2017-02-04 09:12:44 +01:00
|
|
|
if (_3dFont->flags & LARGE_SCENERY_TEXT_FLAG_VERTICAL)
|
2016-06-25 23:47:09 +02:00
|
|
|
{
|
2017-12-19 12:31:35 +01:00
|
|
|
_legacyType.image += _3dFont->num_images * 2;
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-19 12:31:35 +01:00
|
|
|
_legacyType.image += _3dFont->num_images * 4;
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
2017-12-19 12:31:35 +01:00
|
|
|
_legacyType.large_scenery.text = _3dFont.get();
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LargeSceneryObject::Unload()
|
|
|
|
{
|
|
|
|
language_free_object_string(_legacyType.name);
|
2017-12-11 14:06:59 +01:00
|
|
|
gfx_object_free_images(_baseImageId, GetImageTable().GetCount());
|
2016-07-03 19:02:18 +02:00
|
|
|
|
|
|
|
_legacyType.name = 0;
|
|
|
|
_legacyType.image = 0;
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void LargeSceneryObject::DrawPreview(rct_drawpixelinfo* dpi, int32_t width, int32_t height) const
|
2016-07-02 13:29:13 +02:00
|
|
|
{
|
2018-06-20 17:28:51 +02:00
|
|
|
int32_t x = width / 2;
|
|
|
|
int32_t y = (height / 2) - 39;
|
2016-07-02 13:29:13 +02:00
|
|
|
|
2018-06-20 17:28:51 +02:00
|
|
|
uint32_t imageId = 0xB2D00000 | _legacyType.image;
|
2016-07-02 13:29:13 +02:00
|
|
|
gfx_draw_sprite(dpi, imageId, x, y, 0);
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
std::vector<rct_large_scenery_tile> LargeSceneryObject::ReadTiles(IStream* stream)
|
2016-06-25 23:47:09 +02:00
|
|
|
{
|
|
|
|
auto tiles = std::vector<rct_large_scenery_tile>();
|
2018-06-20 17:28:51 +02:00
|
|
|
while (stream->ReadValue<uint16_t>() != 0xFFFF)
|
2016-06-25 23:47:09 +02:00
|
|
|
{
|
|
|
|
stream->Seek(-2, STREAM_SEEK_CURRENT);
|
|
|
|
auto tile = stream->ReadValue<rct_large_scenery_tile>();
|
|
|
|
tiles.push_back(tile);
|
|
|
|
}
|
2016-07-07 18:55:07 +02:00
|
|
|
tiles.push_back({ -1, -1, -1, 255, 0xFFFF });
|
2018-02-07 20:56:46 +01:00
|
|
|
return tiles;
|
2016-06-25 23:47:09 +02:00
|
|
|
}
|
2017-12-17 18:00:45 +01:00
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
void LargeSceneryObject::ReadJson(IReadObjectContext* context, const json_t* root)
|
2017-12-17 18:00:45 +01:00
|
|
|
{
|
|
|
|
auto properties = json_object_get(root, "properties");
|
|
|
|
|
2018-07-21 11:50:45 +02:00
|
|
|
_legacyType.large_scenery.tool_id = ObjectJsonHelpers::ParseCursor(
|
|
|
|
ObjectJsonHelpers::GetString(properties, "cursor"), CURSOR_STATUE_DOWN);
|
2017-12-17 18:00:45 +01:00
|
|
|
_legacyType.large_scenery.price = json_integer_value(json_object_get(properties, "price"));
|
2017-12-19 13:32:02 +01:00
|
|
|
_legacyType.large_scenery.removal_price = json_integer_value(json_object_get(properties, "removalPrice"));
|
2017-12-17 18:00:45 +01:00
|
|
|
|
|
|
|
auto jScrollingMode = json_object_get(properties, "scrollingMode");
|
2018-06-22 23:03:20 +02:00
|
|
|
_legacyType.large_scenery.scrolling_mode = jScrollingMode != nullptr ? json_integer_value(jScrollingMode) : -1;
|
2017-12-17 18:00:45 +01:00
|
|
|
|
|
|
|
// Flags
|
2018-06-22 23:03:20 +02:00
|
|
|
_legacyType.large_scenery.flags = ObjectJsonHelpers::GetFlags<uint8_t>(
|
|
|
|
properties,
|
2018-07-07 14:40:56 +02:00
|
|
|
{
|
|
|
|
{ "hasPrimaryColour", LARGE_SCENERY_FLAG_HAS_PRIMARY_COLOUR },
|
|
|
|
{ "hasSecondaryColour", LARGE_SCENERY_FLAG_HAS_SECONDARY_COLOUR },
|
|
|
|
{ "isAnimated", LARGE_SCENERY_FLAG_ANIMATED },
|
|
|
|
{ "isPhotogenic", LARGE_SCENERY_FLAG_PHOTOGENIC },
|
|
|
|
});
|
2017-12-17 18:00:45 +01:00
|
|
|
|
|
|
|
// Tiles
|
|
|
|
auto jTiles = json_object_get(properties, "tiles");
|
|
|
|
if (jTiles != nullptr)
|
|
|
|
{
|
|
|
|
_tiles = ReadJsonTiles(jTiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read text
|
|
|
|
auto j3dFont = json_object_get(properties, "3dFont");
|
|
|
|
if (j3dFont != nullptr)
|
|
|
|
{
|
|
|
|
_3dFont = ReadJson3dFont(j3dFont);
|
|
|
|
_legacyType.large_scenery.flags |= LARGE_SCENERY_FLAG_3D_TEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPrimarySceneryGroup(ObjectJsonHelpers::GetString(json_object_get(properties, "sceneryGroup")));
|
|
|
|
|
|
|
|
ObjectJsonHelpers::LoadStrings(root, GetStringTable());
|
2018-02-08 22:31:30 +01:00
|
|
|
ObjectJsonHelpers::LoadImages(context, root, GetImageTable());
|
2017-12-17 18:00:45 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
std::vector<rct_large_scenery_tile> LargeSceneryObject::ReadJsonTiles(const json_t* jTiles)
|
2017-12-17 18:00:45 +01:00
|
|
|
{
|
|
|
|
std::vector<rct_large_scenery_tile> tiles;
|
|
|
|
size_t index;
|
2018-06-22 23:03:20 +02:00
|
|
|
const json_t* jTile;
|
2017-12-17 18:00:45 +01:00
|
|
|
json_array_foreach(jTiles, index, jTile)
|
|
|
|
{
|
2018-06-04 19:50:46 +02:00
|
|
|
rct_large_scenery_tile tile = {};
|
2017-12-17 18:00:45 +01:00
|
|
|
tile.x_offset = json_integer_value(json_object_get(jTile, "x"));
|
|
|
|
tile.y_offset = json_integer_value(json_object_get(jTile, "y"));
|
|
|
|
tile.z_offset = json_integer_value(json_object_get(jTile, "z"));
|
|
|
|
tile.z_clearance = json_integer_value(json_object_get(jTile, "clearance"));
|
|
|
|
if (!ObjectJsonHelpers::GetBoolean(jTile, "hasSupports"))
|
|
|
|
{
|
|
|
|
tile.flags |= LARGE_SCENERY_TILE_FLAG_NO_SUPPORTS;
|
|
|
|
}
|
|
|
|
if (ObjectJsonHelpers::GetBoolean(jTile, "allowSupportsAbove"))
|
|
|
|
{
|
|
|
|
tile.flags |= LARGE_SCENERY_TILE_FLAG_ALLOW_SUPPORTS_ABOVE;
|
|
|
|
}
|
2017-12-19 11:26:20 +01:00
|
|
|
|
|
|
|
// All corners are by default occupied
|
|
|
|
auto jCorners = json_object_get(jTile, "corners");
|
|
|
|
auto corners = 0xF;
|
|
|
|
if (jCorners != nullptr)
|
|
|
|
{
|
|
|
|
corners = json_integer_value(jCorners);
|
|
|
|
}
|
|
|
|
tile.flags |= (corners & 0xFF) << 12;
|
|
|
|
|
|
|
|
auto walls = json_integer_value(json_object_get(jTile, "walls"));
|
|
|
|
tile.flags |= (walls & 0xFF) << 8;
|
|
|
|
|
2017-12-17 18:00:45 +01:00
|
|
|
tiles.push_back(tile);
|
|
|
|
}
|
2017-12-19 11:26:20 +01:00
|
|
|
|
|
|
|
// HACK Add end of tiles marker
|
|
|
|
// We should remove this later by improving the code base to use tiles array length
|
|
|
|
tiles.push_back({ -1, -1, -1, 0xFF, 0xFFFF });
|
|
|
|
|
2017-12-17 18:00:45 +01:00
|
|
|
return tiles;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
std::unique_ptr<rct_large_scenery_text> LargeSceneryObject::ReadJson3dFont(const json_t* j3dFont)
|
2017-12-17 18:00:45 +01:00
|
|
|
{
|
2017-12-19 12:31:35 +01:00
|
|
|
auto font = std::make_unique<rct_large_scenery_text>();
|
|
|
|
|
|
|
|
auto jOffsets = json_object_get(j3dFont, "offsets");
|
|
|
|
if (jOffsets != nullptr)
|
|
|
|
{
|
|
|
|
auto offsets = ReadJsonOffsets(jOffsets);
|
2018-11-21 23:16:04 +01:00
|
|
|
auto numOffsets = std::min(std::size(font->offset), offsets.size());
|
2017-12-19 12:31:35 +01:00
|
|
|
std::copy_n(offsets.data(), numOffsets, font->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
font->max_width = json_integer_value(json_object_get(j3dFont, "maxWidth"));
|
|
|
|
font->num_images = json_integer_value(json_object_get(j3dFont, "numImages"));
|
2018-06-22 23:03:20 +02:00
|
|
|
font->flags = ObjectJsonHelpers::GetFlags<uint8_t>(
|
2018-07-07 14:40:56 +02:00
|
|
|
j3dFont,
|
|
|
|
{
|
|
|
|
{ "isVertical", LARGE_SCENERY_TEXT_FLAG_VERTICAL },
|
|
|
|
{ "isTwoLine", LARGE_SCENERY_TEXT_FLAG_TWO_LINE },
|
|
|
|
});
|
2017-12-19 12:31:35 +01:00
|
|
|
|
|
|
|
auto jGlyphs = json_object_get(j3dFont, "glyphs");
|
|
|
|
if (jGlyphs != nullptr)
|
|
|
|
{
|
|
|
|
auto glyphs = ReadJsonGlyphs(jGlyphs);
|
2018-11-21 23:16:04 +01:00
|
|
|
auto numGlyphs = std::min(std::size(font->glyphs), glyphs.size());
|
2017-12-19 12:31:35 +01:00
|
|
|
std::copy_n(glyphs.data(), numGlyphs, font->glyphs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
std::vector<LocationXY16> LargeSceneryObject::ReadJsonOffsets(const json_t* jOffsets)
|
2017-12-19 12:31:35 +01:00
|
|
|
{
|
|
|
|
std::vector<LocationXY16> offsets;
|
|
|
|
size_t index;
|
2018-06-22 23:03:20 +02:00
|
|
|
const json_t* jOffset;
|
2017-12-19 12:31:35 +01:00
|
|
|
json_array_foreach(jOffsets, index, jOffset)
|
|
|
|
{
|
2018-06-04 19:50:46 +02:00
|
|
|
LocationXY16 offset = {};
|
2017-12-19 12:31:35 +01:00
|
|
|
offset.x = json_integer_value(json_object_get(jOffset, "x"));
|
|
|
|
offset.y = json_integer_value(json_object_get(jOffset, "y"));
|
|
|
|
offsets.push_back(offset);
|
|
|
|
}
|
|
|
|
return offsets;
|
|
|
|
}
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
std::vector<rct_large_scenery_text_glyph> LargeSceneryObject::ReadJsonGlyphs(const json_t* jGlpyhs)
|
2017-12-19 12:31:35 +01:00
|
|
|
{
|
|
|
|
std::vector<rct_large_scenery_text_glyph> glyphs;
|
|
|
|
size_t index;
|
2018-06-22 23:03:20 +02:00
|
|
|
const json_t* jGlyph;
|
2017-12-19 12:31:35 +01:00
|
|
|
json_array_foreach(jGlpyhs, index, jGlyph)
|
|
|
|
{
|
|
|
|
rct_large_scenery_text_glyph glyph;
|
|
|
|
glyph.image_offset = json_integer_value(json_object_get(jGlyph, "image"));
|
|
|
|
glyph.width = json_integer_value(json_object_get(jGlyph, "width"));
|
|
|
|
glyph.height = json_integer_value(json_object_get(jGlyph, "height"));
|
|
|
|
glyphs.push_back(glyph);
|
|
|
|
}
|
|
|
|
return glyphs;
|
2017-12-17 18:00:45 +01:00
|
|
|
}
|