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

196 lines
5.3 KiB
C++
Raw Normal View History

2014-05-24 01:34:17 +01:00
/*****************************************************************************
2018-06-15 14:07:34 +02:00
* Copyright (c) 2014-2018 OpenRCT2 developers
2014-05-24 01:34:17 +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
2014-05-24 01:34:17 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-05-24 01:34:17 +01:00
*****************************************************************************/
2018-06-22 23:03:20 +02:00
#include "ObjectList.h"
2018-03-14 12:44:16 +01:00
#include "../Context.h"
2018-06-22 23:03:20 +02:00
#include "../Game.h"
2018-01-02 20:36:42 +01:00
#include "../object/Object.h"
#include "../util/SawyerCoding.h"
#include "../util/Util.h"
2018-06-22 23:03:20 +02:00
#include "ObjectManager.h"
#include "ObjectRepository.h"
2018-08-12 12:50:40 +01:00
#include <algorithm>
2018-06-22 23:03:20 +02:00
#include <cstring>
2014-05-24 01:34:17 +01:00
2014-09-29 12:58:48 +01:00
// 98DA00
2018-01-10 16:14:36 +01:00
// clang-format off
int32_t object_entry_group_counts[] = {
MAX_RIDE_OBJECTS, // rides
MAX_SMALL_SCENERY_OBJECTS, // small scenery
MAX_LARGE_SCENERY_OBJECTS, // large scenery
MAX_WALL_SCENERY_OBJECTS, // walls
MAX_BANNER_OBJECTS, // banners
MAX_PATH_OBJECTS, // paths
MAX_PATH_ADDITION_OBJECTS, // path bits
MAX_SCENERY_GROUP_OBJECTS, // scenery sets
MAX_PARK_ENTRANCE_OBJECTS, // park entrance
MAX_WATER_OBJECTS, // water
MAX_SCENARIO_TEXT_OBJECTS, // scenario text
MAX_TERRAIN_SURFACE_OBJECTS,
MAX_TERRAIN_EDGE_OBJECTS,
MAX_STATION_OBJECTS,
MAX_MUSIC_OBJECTS,
2014-05-24 18:07:29 +01:00
};
2014-10-01 21:49:37 +01:00
// 98DA2C
int32_t object_entry_group_encoding[] = {
2017-06-06 23:24:18 +02:00
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_RLE,
CHUNK_ENCODING_ROTATE
2014-10-01 21:49:37 +01:00
};
2018-01-10 16:14:36 +01:00
// clang-format on
2018-06-22 23:03:20 +02:00
bool object_entry_is_empty(const rct_object_entry* entry)
2014-05-24 16:48:13 +01:00
{
uint64_t a, b;
std::memcpy(&a, (uint8_t*)entry, 8);
std::memcpy(&b, (uint8_t*)entry + 8, 8);
2016-09-29 17:52:16 +01:00
2018-06-22 23:03:20 +02:00
if (a == 0xFFFFFFFFFFFFFFFF && b == 0xFFFFFFFFFFFFFFFF)
return true;
if (a == 0 && b == 0)
return true;
2017-06-06 23:24:18 +02:00
return false;
2014-05-24 16:48:13 +01:00
}
2018-06-22 23:03:20 +02:00
uint8_t object_entry_get_type(const rct_object_entry* objectEntry)
2018-02-08 20:34:05 +01:00
{
return (objectEntry->flags & 0x0F);
}
uint8_t object_entry_get_source_game_legacy(const rct_object_entry* objectEntry)
2017-11-13 14:14:24 +01:00
{
return (objectEntry->flags & 0xF0) >> 4;
}
2015-12-11 09:38:37 -06:00
/**
*
* rct2: 0x006AB344
*/
2016-09-25 21:24:29 -05:00
void object_create_identifier_name(char* string_buffer, size_t size, const rct_object_entry* object)
2016-01-06 20:41:21 +00:00
{
2017-06-06 23:24:18 +02:00
snprintf(string_buffer, size, "%.8s/%4X%4X", object->name, object->flags, object->checksum);
}
2015-12-11 09:38:37 -06:00
/**
*
* rct2: 0x006A9DA2
* bl = entry_index
* ecx = entry_type
*/
2018-06-22 23:03:20 +02:00
bool find_object_in_entry_group(const rct_object_entry* entry, uint8_t* entry_type, uint8_t* entry_index)
{
int32_t objectType = object_entry_get_type(entry);
2018-02-11 18:56:09 +00:00
if (objectType >= OBJECT_TYPE_COUNT)
{
2018-02-11 18:56:09 +00:00
return false;
2017-06-06 23:24:18 +02:00
}
auto& objectMgr = OpenRCT2::GetContext()->GetObjectManager();
2018-02-11 18:56:09 +00:00
auto maxObjects = object_entry_group_counts[objectType];
for (int32_t i = 0; i < maxObjects; i++)
2018-02-11 18:56:09 +00:00
{
auto loadedObj = objectMgr.GetLoadedObject(objectType, i);
if (loadedObj != nullptr)
2018-02-11 18:56:09 +00:00
{
auto thisEntry = object_entry_get_entry(objectType, i);
2018-02-11 18:56:09 +00:00
if (object_entry_compare(thisEntry, entry))
{
*entry_type = objectType;
*entry_index = i;
return true;
}
}
}
return false;
}
2018-06-22 23:03:20 +02:00
void get_type_entry_index(size_t index, uint8_t* outObjectType, uint8_t* outEntryIndex)
2016-06-15 14:59:01 +02:00
{
uint8_t objectType = OBJECT_TYPE_RIDE;
2017-12-20 15:38:40 +01:00
for (size_t groupCount : object_entry_group_counts)
2017-12-20 02:50:18 +01:00
{
2018-06-22 23:03:20 +02:00
if (index >= groupCount)
{
2017-06-06 23:24:18 +02:00
index -= groupCount;
objectType++;
2018-06-22 23:03:20 +02:00
}
else
{
2017-06-06 23:24:18 +02:00
break;
}
}
2016-06-15 14:59:01 +02:00
2018-06-22 23:03:20 +02:00
if (outObjectType != nullptr)
*outObjectType = objectType;
if (outEntryIndex != nullptr)
*outEntryIndex = (uint8_t)index;
}
2018-06-22 23:03:20 +02:00
const rct_object_entry* get_loaded_object_entry(size_t index)
{
uint8_t objectType, entryIndex;
2017-06-06 23:24:18 +02:00
get_type_entry_index(index, &objectType, &entryIndex);
2018-02-11 19:02:51 +00:00
return object_entry_get_entry(objectType, entryIndex);
2016-06-15 14:59:01 +02:00
}
2018-06-22 23:03:20 +02:00
void* get_loaded_object_chunk(size_t index)
{
uint8_t objectType, entryIndex;
2017-06-06 23:24:18 +02:00
get_type_entry_index(index, &objectType, &entryIndex);
2018-02-11 18:56:09 +00:00
return object_entry_get_chunk(objectType, entryIndex);
2016-06-15 14:59:01 +02:00
}
2016-07-09 15:24:00 +01:00
2018-06-22 23:03:20 +02:00
void object_entry_get_name_fixed(utf8* buffer, size_t bufferSize, const rct_object_entry* entry)
2016-07-09 15:24:00 +01:00
{
2018-06-20 17:11:35 +02:00
bufferSize = std::min((size_t)DAT_NAME_LENGTH + 1, bufferSize);
std::memcpy(buffer, entry->name, bufferSize - 1);
2017-06-06 23:24:18 +02:00
buffer[bufferSize - 1] = 0;
2016-07-09 15:24:00 +01:00
}
2018-02-11 18:56:09 +00:00
2018-06-22 23:03:20 +02:00
void* object_entry_get_chunk(int32_t objectType, size_t index)
2018-02-11 18:56:09 +00:00
{
2018-02-11 20:30:48 +00:00
size_t objectIndex = index;
for (int32_t i = 0; i < objectType; i++)
2018-02-11 20:30:48 +00:00
{
objectIndex += object_entry_group_counts[i];
}
2018-06-22 23:03:20 +02:00
void* result = nullptr;
2018-08-29 00:52:10 +02:00
auto& objectMgr = OpenRCT2::GetContext()->GetObjectManager();
auto obj = objectMgr.GetLoadedObject(objectIndex);
2018-02-11 20:30:48 +00:00
if (obj != nullptr)
{
result = obj->GetLegacyData();
}
return result;
2018-02-11 18:56:09 +00:00
}
2018-06-22 23:03:20 +02:00
const rct_object_entry* object_entry_get_entry(int32_t objectType, size_t index)
2018-02-11 18:56:09 +00:00
{
2018-06-22 23:03:20 +02:00
const rct_object_entry* result = nullptr;
2018-08-29 00:52:10 +02:00
auto& objectMgr = OpenRCT2::GetContext()->GetObjectManager();
auto obj = objectMgr.GetLoadedObject(objectType, index);
if (obj != nullptr)
{
result = obj->GetObjectEntry();
}
return result;
2018-02-11 18:56:09 +00:00
}