2016-06-26 00:31:16 +01:00
|
|
|
/*****************************************************************************
|
2024-12-31 20:30:04 +01:00
|
|
|
* Copyright (c) 2014-2025 OpenRCT2 developers
|
2016-06-26 00:31:16 +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:31:16 +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:31:16 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
|
2017-12-14 13:28:08 +00:00
|
|
|
#pragma warning(disable : 4706) // assignment within conditional expression
|
|
|
|
|
|
2018-06-22 23:03:20 +02:00
|
|
|
#include "WaterObject.h"
|
|
|
|
|
|
|
|
|
|
#include "../OpenRCT2.h"
|
2016-06-26 00:31:16 +01:00
|
|
|
#include "../core/IStream.hpp"
|
2020-10-02 11:31:20 +01:00
|
|
|
#include "../core/Json.hpp"
|
2021-12-11 18:06:06 -05:00
|
|
|
#include "../localisation/Formatter.h"
|
2018-01-06 18:32:25 +01:00
|
|
|
#include "../localisation/Language.h"
|
|
|
|
|
#include "../localisation/StringIds.h"
|
2020-05-17 20:29:56 +02:00
|
|
|
#include "../world/Location.hpp"
|
2016-06-26 00:31:16 +01:00
|
|
|
|
2021-12-11 18:06:06 -05:00
|
|
|
#include <array>
|
|
|
|
|
#include <cstring>
|
2018-06-22 23:03:20 +02:00
|
|
|
#include <memory>
|
|
|
|
|
|
2025-08-17 11:46:03 +02:00
|
|
|
namespace OpenRCT2
|
2016-06-26 00:31:16 +01:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
void WaterObject::ReadLegacy(IReadObjectContext* context, IStream* stream)
|
2017-12-14 13:28:08 +00:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
stream->Seek(14, STREAM_SEEK_CURRENT);
|
|
|
|
|
_legacyType.flags = stream->ReadValue<uint16_t>();
|
2020-08-12 23:05:23 +01:00
|
|
|
|
2025-10-07 22:08:15 +02:00
|
|
|
GetStringTable().Read(context, stream, ObjectStringID::name);
|
2025-08-17 11:46:03 +02:00
|
|
|
GetImageTable().Read(context, stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaterObject::Load()
|
|
|
|
|
{
|
|
|
|
|
GetStringTable().Sort();
|
|
|
|
|
_legacyType.string_idx = LanguageAllocateObjectString(GetName());
|
|
|
|
|
_legacyType.image_id = LoadImages();
|
|
|
|
|
_legacyType.palette_index_1 = _legacyType.image_id + 1;
|
|
|
|
|
_legacyType.palette_index_2 = _legacyType.image_id + 4;
|
|
|
|
|
|
|
|
|
|
LoadPalette();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaterObject::Unload()
|
|
|
|
|
{
|
|
|
|
|
UnloadImages();
|
|
|
|
|
LanguageFreeObjectString(_legacyType.string_idx);
|
|
|
|
|
|
|
|
|
|
_legacyType.string_idx = 0;
|
|
|
|
|
_legacyType.image_id = 0;
|
|
|
|
|
_legacyType.palette_index_1 = 0;
|
|
|
|
|
_legacyType.palette_index_2 = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-26 12:58:33 +01:00
|
|
|
void WaterObject::DrawPreview(Drawing::RenderTarget& rt, int32_t width, int32_t height) const
|
2025-08-17 11:46:03 +02:00
|
|
|
{
|
|
|
|
|
// Write (no image)
|
|
|
|
|
auto screenCoords = ScreenCoordsXY{ width / 2, height / 2 };
|
2025-10-16 20:06:18 +02:00
|
|
|
DrawTextBasic(rt, screenCoords, STR_WINDOW_NO_IMAGE, {}, { TextAlignment::centre });
|
2025-08-17 11:46:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaterObject::ReadJson([[maybe_unused]] IReadObjectContext* context, json_t& root)
|
|
|
|
|
{
|
|
|
|
|
Guard::Assert(root.is_object(), "WaterObject::ReadJson expects parameter root to be object");
|
|
|
|
|
|
|
|
|
|
auto properties = root["properties"];
|
|
|
|
|
|
|
|
|
|
PopulateTablesFromJson(context, root);
|
|
|
|
|
|
|
|
|
|
if (properties.is_object())
|
2020-08-12 23:05:23 +01:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
_legacyType.flags = Json::GetFlags<uint16_t>(
|
|
|
|
|
properties,
|
2020-08-12 23:05:23 +01:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
{ "allowDucks", WATER_FLAGS_ALLOW_DUCKS },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
auto jPalettes = properties["palettes"];
|
|
|
|
|
if (jPalettes.is_object())
|
|
|
|
|
{
|
|
|
|
|
// Images which are actually palette data
|
|
|
|
|
static const char* paletteNames[] = {
|
|
|
|
|
"general", "waves-0", "waves-1", "waves-2", "sparkles-0", "sparkles-1", "sparkles-2",
|
|
|
|
|
};
|
|
|
|
|
for (auto paletteName : paletteNames)
|
|
|
|
|
{
|
|
|
|
|
auto jPalette = jPalettes[paletteName];
|
|
|
|
|
if (jPalette.is_object())
|
|
|
|
|
{
|
|
|
|
|
ReadJsonPalette(jPalette);
|
|
|
|
|
}
|
2020-08-12 23:05:23 +01:00
|
|
|
}
|
2017-12-14 13:28:08 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:46:03 +02:00
|
|
|
void WaterObject::ReadJsonPalette(json_t& jPalette)
|
2017-12-14 13:28:08 +00:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
Guard::Assert(jPalette.is_object(), "WaterObject::ReadJsonPalette expects parameter jPalette to be object");
|
|
|
|
|
|
|
|
|
|
auto jColours = jPalette["colours"];
|
|
|
|
|
auto numColours = jColours.size();
|
|
|
|
|
|
|
|
|
|
// This pointer gets memcopied in ImageTable::AddImage so it's fine for the unique_ptr to go out of scope
|
|
|
|
|
auto data = std::make_unique<uint8_t[]>(numColours * 3);
|
|
|
|
|
size_t dataIndex = 0;
|
|
|
|
|
|
|
|
|
|
for (auto& jColour : jColours)
|
2017-12-14 13:28:08 +00:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
if (jColour.is_string())
|
|
|
|
|
{
|
|
|
|
|
auto colour = ParseColour(Json::GetString(jColour));
|
|
|
|
|
data[dataIndex + 0] = (colour >> 16) & 0xFF;
|
|
|
|
|
data[dataIndex + 1] = (colour >> 8) & 0xFF;
|
|
|
|
|
data[dataIndex + 2] = colour & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
dataIndex += 3;
|
2017-12-14 13:28:08 +00:00
|
|
|
}
|
2025-08-17 11:46:03 +02:00
|
|
|
|
|
|
|
|
G1Element g1 = {};
|
|
|
|
|
g1.offset = data.get();
|
2025-12-27 14:30:36 +01:00
|
|
|
g1.numColours = static_cast<int16_t>(numColours);
|
|
|
|
|
g1.startIndex = Json::GetNumber<int16_t>(jPalette["index"]);
|
2025-08-17 11:46:03 +02:00
|
|
|
g1.flags = G1_FLAG_PALETTE;
|
|
|
|
|
|
|
|
|
|
auto& imageTable = GetImageTable();
|
|
|
|
|
imageTable.AddImage(&g1);
|
2017-12-14 13:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
2025-08-17 11:46:03 +02:00
|
|
|
uint32_t WaterObject::ParseColour(const std::string& s) const
|
2017-12-14 13:28:08 +00:00
|
|
|
{
|
2025-08-17 11:46:03 +02:00
|
|
|
uint8_t r = 0;
|
|
|
|
|
uint8_t g = 0;
|
|
|
|
|
uint8_t b = 0;
|
|
|
|
|
if (s[0] == '#' && s.size() == 7)
|
|
|
|
|
{
|
|
|
|
|
// Expect #RRGGBB
|
|
|
|
|
r = std::stoul(s.substr(1, 2), nullptr, 16) & 0xFF;
|
|
|
|
|
g = std::stoul(s.substr(3, 2), nullptr, 16) & 0xFF;
|
|
|
|
|
b = std::stoul(s.substr(5, 2), nullptr, 16) & 0xFF;
|
|
|
|
|
}
|
|
|
|
|
return (b << 16) | (g << 8) | r;
|
2017-12-14 13:28:08 +00:00
|
|
|
}
|
2025-08-17 11:46:03 +02:00
|
|
|
} // namespace OpenRCT2
|