Use correct type for EntityId and fix issues

This commit is contained in:
ζeh Matt 2022-02-13 01:48:36 +02:00
parent c1bd0ff047
commit 52047805db
No known key found for this signature in database
GPG Key ID: 18CE582C71A225B0
12 changed files with 67 additions and 61 deletions

View File

@ -55,8 +55,8 @@ namespace OpenRCT2::Scripting
obj.Set("index", value.SaveIndex);
break;
case TitleScript::Location:
obj.Set("x", value.X);
obj.Set("y", value.Y);
obj.Set("x", value.Location.X);
obj.Set("y", value.Location.Y);
break;
case TitleScript::Rotate:
obj.Set("rotations", value.Rotations);
@ -65,10 +65,10 @@ namespace OpenRCT2::Scripting
obj.Set("zoom", value.Zoom);
break;
case TitleScript::Follow:
if (value.SpriteIndex.IsNull())
if (value.Follow.SpriteIndex.IsNull())
obj.Set("id", nullptr);
else
obj.Set("id", value.SpriteIndex.ToUnderlying());
obj.Set("id", value.Follow.SpriteIndex.ToUnderlying());
break;
case TitleScript::Speed:
obj.Set("speed", value.Speed);
@ -103,8 +103,8 @@ namespace OpenRCT2::Scripting
command.SaveIndex = value["index"].as_int();
break;
case TitleScript::Location:
command.X = value["x"].as_int();
command.Y = value["y"].as_int();
command.Location.X = value["x"].as_int();
command.Location.Y = value["y"].as_int();
break;
case TitleScript::Rotate:
command.Rotations = value["rotations"].as_int();
@ -117,11 +117,11 @@ namespace OpenRCT2::Scripting
auto dukId = value["id"];
if (dukId.type() == DukValue::Type::NUMBER)
{
command.SpriteIndex = EntityId::FromUnderlying(dukId.as_int());
command.Follow.SpriteIndex = EntityId::FromUnderlying(dukId.as_int());
}
else
{
command.SpriteIndex = EntityId::GetNull();
command.Follow.SpriteIndex = EntityId::GetNull();
}
break;
}

View File

@ -254,7 +254,7 @@ private:
break;
case TitleScript::Location:
{
auto loc = TileCoordsXY(command.X, command.Y).ToCoordsXY().ToTileCentre();
auto loc = TileCoordsXY(command.Location.X, command.Location.Y).ToCoordsXY().ToTileCentre();
SetViewLocation(loc);
break;
}
@ -274,7 +274,7 @@ private:
gGameSpeed = std::clamp<uint8_t>(command.Speed, 1, 4);
break;
case TitleScript::Follow:
FollowSprite(command.SpriteIndex);
FollowSprite(command.Follow.SpriteIndex);
break;
case TitleScript::Restart:
Reset();

View File

@ -243,8 +243,8 @@ void WindowTitleCommandEditorOpen(TitleSequence* sequence, int32_t index, bool i
_command.SaveIndex = SAVE_INDEX_INVALID;
break;
case TitleScript::Location:
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y);
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y);
break;
case TitleScript::Rotate:
case TitleScript::Zoom:
@ -254,9 +254,9 @@ void WindowTitleCommandEditorOpen(TitleSequence* sequence, int32_t index, bool i
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Milliseconds);
break;
case TitleScript::Follow:
if (!_command.SpriteIndex.IsNull())
if (!_command.Follow.SpriteIndex.IsNull())
{
window_follow_sprite(window, _command.SpriteIndex);
window_follow_sprite(window, _command.Follow.SpriteIndex);
}
break;
case TitleScript::Undefined:
@ -314,10 +314,10 @@ static void WindowTitleCommandEditorMouseup(rct_window* w, rct_widgetindex widge
if (_command.Type == TitleScript::Location)
{
auto tileCoord = GetLocation();
_command.X = static_cast<uint8_t>(tileCoord.x);
_command.Y = static_cast<uint8_t>(tileCoord.y);
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y);
_command.Location.X = static_cast<uint8_t>(tileCoord.x);
_command.Location.Y = static_cast<uint8_t>(tileCoord.y);
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y);
}
else if (_command.Type == TitleScript::Zoom)
{
@ -432,7 +432,7 @@ static void WindowTitleCommandEditorDropdown(rct_window* w, rct_widgetindex widg
switch (widgetIndex)
{
case WIDX_COMMAND_DROPDOWN:
if (!_command.SpriteIndex.IsNull())
if (!_command.Follow.SpriteIndex.IsNull())
{
window_unfollow_sprite(w);
}
@ -446,10 +446,10 @@ static void WindowTitleCommandEditorDropdown(rct_window* w, rct_widgetindex widg
case TitleScript::Location:
{
auto tileCoord = GetLocation();
_command.X = static_cast<uint8_t>(tileCoord.x);
_command.Y = static_cast<uint8_t>(tileCoord.y);
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y);
_command.Location.X = static_cast<uint8_t>(tileCoord.x);
_command.Location.Y = static_cast<uint8_t>(tileCoord.y);
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y);
break;
}
case TitleScript::Rotate:
@ -471,8 +471,8 @@ static void WindowTitleCommandEditorDropdown(rct_window* w, rct_widgetindex widg
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Zoom);
break;
case TitleScript::Follow:
_command.SpriteIndex = EntityId::GetNull();
_command.SpriteName[0] = '\0';
_command.Follow.SpriteIndex = EntityId::GetNull();
_command.Follow.SpriteName[0] = '\0';
window_unfollow_sprite(w);
// This is incorrect
w->viewport->flags &= ~VIEWPORT_FLAG_GRIDLINES;
@ -588,9 +588,9 @@ static void WindowTitleCommandEditorTextinput(rct_window* w, rct_widgetindex wid
{
if (*end == '\0')
{
_command.X = static_cast<uint8_t>(value);
_command.Location.X = static_cast<uint8_t>(value);
}
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.X);
snprintf(textbox1Buffer, BUF_SIZE, "%d", _command.Location.X);
w->Invalidate();
}
else
@ -603,9 +603,9 @@ static void WindowTitleCommandEditorTextinput(rct_window* w, rct_widgetindex wid
{
if (*end == '\0')
{
_command.Y = static_cast<uint8_t>(value);
_command.Location.Y = static_cast<uint8_t>(value);
}
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Y);
snprintf(textbox2Buffer, BUF_SIZE, "%d", _command.Location.Y);
w->Invalidate();
}
else
@ -643,7 +643,7 @@ static void WindowTitleCommandEditorToolDown(rct_window* w, rct_widgetindex widg
validSprite = true;
Formatter ft;
peep->FormatNameTo(ft);
format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, &peep->Id);
format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, &peep->Id);
}
else if (vehicle != nullptr)
{
@ -654,7 +654,7 @@ static void WindowTitleCommandEditorToolDown(rct_window* w, rct_widgetindex widg
{
Formatter ft;
ride->FormatNameTo(ft);
format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, ft.Data());
format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_STRINGID, ft.Data());
}
}
else if (litter != nullptr)
@ -663,24 +663,24 @@ static void WindowTitleCommandEditorToolDown(rct_window* w, rct_widgetindex widg
if (name != STR_NONE)
{
validSprite = true;
format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, name, nullptr);
format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, name, nullptr);
}
}
else if (balloon != nullptr)
{
validSprite = true;
format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_SHOP_ITEM_SINGULAR_BALLOON, nullptr);
format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_SHOP_ITEM_SINGULAR_BALLOON, nullptr);
}
else if (duck != nullptr)
{
validSprite = true;
format_string(_command.SpriteName, USER_STRING_MAX_LENGTH, STR_DUCK, nullptr);
format_string(_command.Follow.SpriteName, USER_STRING_MAX_LENGTH, STR_DUCK, nullptr);
}
if (validSprite)
{
_command.SpriteIndex = entity->sprite_index;
window_follow_sprite(w, _command.SpriteIndex);
_command.Follow.SpriteIndex = entity->sprite_index;
window_follow_sprite(w, _command.Follow.SpriteIndex);
tool_cancel();
w->Invalidate();
}
@ -790,10 +790,10 @@ static void WindowTitleCommandEditorPaint(rct_window* w, rct_drawpixelinfo* dpi)
uint8_t colour = COLOUR_BLACK;
rct_string_id spriteString = STR_TITLE_COMMAND_EDITOR_FORMAT_SPRITE_NAME;
auto ft = Formatter();
if (!_command.SpriteIndex.IsNull())
if (!_command.Follow.SpriteIndex.IsNull())
{
window_draw_viewport(dpi, w);
ft.Add<utf8*>(_command.SpriteName);
ft.Add<utf8*>(_command.Follow.SpriteName);
}
else
{

View File

@ -946,8 +946,8 @@ static void WindowTitleEditorScrollpaintCommands(rct_window* w, rct_drawpixelinf
{
auto commandName = STR_TITLE_EDITOR_COMMAND_LOCATION;
ft.Add<rct_string_id>(commandName);
ft.Add<uint16_t>(command.X);
ft.Add<uint16_t>(command.Y);
ft.Add<uint16_t>(command.Location.X);
ft.Add<uint16_t>(command.Location.Y);
break;
}
case TitleScript::Rotate:
@ -974,7 +974,7 @@ static void WindowTitleEditorScrollpaintCommands(rct_window* w, rct_drawpixelinf
case TitleScript::Follow:
{
auto commandName = STR_TITLE_EDITOR_COMMAND_FOLLOW;
if (command.SpriteIndex.IsNull())
if (command.Follow.SpriteIndex.IsNull())
{
commandName = STR_TITLE_EDITOR_COMMAND_FOLLOW_NO_SPRITE;
ft.Add<rct_string_id>(commandName);
@ -982,7 +982,7 @@ static void WindowTitleEditorScrollpaintCommands(rct_window* w, rct_drawpixelinf
else
{
ft.Add<rct_string_id>(commandName);
ft.Add<utf8*>(command.SpriteName);
ft.Add<utf8*>(command.Follow.SpriteName);
}
break;
}

View File

@ -519,7 +519,7 @@ void game_unload_scripts()
*/
void reset_all_sprite_quadrant_placements()
{
for (size_t i = 0; i < MAX_ENTITIES; i++)
for (EntityId::UnderlyingType i = 0; i < MAX_ENTITIES; i++)
{
auto* spr = GetEntity(EntityId::FromUnderlying(i));
if (spr != nullptr && spr->Type != EntityType::Null)

View File

@ -83,7 +83,7 @@ struct GameStateSnapshot_t
if (saving)
{
for (size_t i = 0; i < numSprites; i++)
for (EntityId::UnderlyingType i = 0; i < numSprites; i++)
{
auto entity = getEntity(EntityId::FromUnderlying(i));
if (entity == nullptr || entity->base.Type == EntityType::Null)

View File

@ -20,4 +20,4 @@ using BannerIndex = TIdentifier<uint16_t, std::numeric_limits<uint16_t>::max(),
using RideId = TIdentifier<uint16_t, std::numeric_limits<uint16_t>::max(), struct RideIdTag>;
using EntityId = TIdentifier<uint64_t, std::numeric_limits<uint16_t>::max(), struct EntityIdTag>;
using EntityId = TIdentifier<uint16_t, std::numeric_limits<uint16_t>::max(), struct EntityIdTag>;

View File

@ -8,7 +8,10 @@
*****************************************************************************/
#pragma once
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <vector>
template<typename Handle, typename V> class GroupVector
@ -18,7 +21,7 @@ template<typename Handle, typename V> class GroupVector
public:
bool Contains(Handle handle, V value)
{
const auto index = static_cast<std::size_t>(handle);
const auto index = static_cast<size_t>(handle);
if (index >= _data.size())
return false;
@ -28,7 +31,7 @@ public:
void Add(Handle handle, V value)
{
const auto index = static_cast<std::size_t>(handle);
const auto index = static_cast<size_t>(handle);
if (index >= _data.size())
{
_data.resize(index + 1);
@ -44,7 +47,7 @@ public:
void Set(Handle handle, std::vector<V>&& values)
{
const auto index = static_cast<std::size_t>(handle);
const auto index = static_cast<size_t>(handle);
if (index >= _data.size())
{
_data.resize(index + 1);
@ -54,7 +57,7 @@ public:
std::vector<V>* GetAll(Handle handle)
{
const auto index = static_cast<std::size_t>(handle);
const auto index = static_cast<size_t>(handle);
if (index < _data.size())
{
return &_data[index];
@ -69,7 +72,7 @@ public:
void RemoveHandle(Handle handle)
{
const auto index = static_cast<std::size_t>(handle);
const auto index = static_cast<size_t>(handle);
if (index < _data.size())
{
_data[index].clear();

View File

@ -9,6 +9,9 @@
#pragma once
#include <cstdint>
#include <cstdio>
template<typename T, T TNullValue, typename TTag> class TIdentifier
{
enum class ValueType : T
@ -48,7 +51,7 @@ public:
}
// Support for static_cast<size_t>.
explicit operator std::size_t() const noexcept
explicit operator size_t() const noexcept
{
return static_cast<std::size_t>(ToUnderlying());
}

View File

@ -217,7 +217,7 @@ void ResetEntitySpatialIndices()
{
vec.clear();
}
for (size_t i = 0; i < MAX_ENTITIES; i++)
for (EntityId::UnderlyingType i = 0; i < MAX_ENTITIES; i++)
{
auto* spr = GetEntity(EntityId::FromUnderlying(i));
if (spr != nullptr && spr->Type != EntityType::Null)

View File

@ -350,8 +350,8 @@ static std::vector<TitleCommand> LegacyScriptRead(const std::vector<uint8_t>& sc
else if (_stricmp(token, "LOCATION") == 0)
{
command.Type = TitleScript::Location;
command.X = atoi(part1) & 0xFF;
command.Y = atoi(part2) & 0xFF;
command.Location.X = atoi(part1) & 0xFF;
command.Location.Y = atoi(part2) & 0xFF;
}
else if (_stricmp(token, "ROTATE") == 0)
{
@ -371,8 +371,8 @@ static std::vector<TitleCommand> LegacyScriptRead(const std::vector<uint8_t>& sc
else if (_stricmp(token, "FOLLOW") == 0)
{
command.Type = TitleScript::Follow;
command.SpriteIndex = EntityId::FromUnderlying(atoi(part1) & 0xFFFF);
safe_strcpy(command.SpriteName, part2, USER_STRING_MAX_LENGTH);
command.Follow.SpriteIndex = EntityId::FromUnderlying(atoi(part1) & 0xFFFF);
safe_strcpy(command.Follow.SpriteName, part2, USER_STRING_MAX_LENGTH);
}
else if (_stricmp(token, "WAIT") == 0)
{
@ -526,7 +526,7 @@ static std::string LegacyScriptWrite(const TitleSequence& seq)
case TitleScript::EndLoop:
break;
case TitleScript::Location:
String::Format(buffer, sizeof(buffer), "LOCATION %u %u", command.X, command.Y);
String::Format(buffer, sizeof(buffer), "LOCATION %u %u", command.Location.X, command.Location.Y);
sb.Append(buffer);
break;
case TitleScript::Rotate:
@ -538,9 +538,9 @@ static std::string LegacyScriptWrite(const TitleSequence& seq)
sb.Append(buffer);
break;
case TitleScript::Follow:
String::Format(buffer, sizeof(buffer), "FOLLOW %u ", command.SpriteIndex);
String::Format(buffer, sizeof(buffer), "FOLLOW %u ", command.Follow.SpriteIndex);
sb.Append(buffer);
sb.Append(command.SpriteName);
sb.Append(command.Follow.SpriteName);
break;
case TitleScript::Speed:
String::Format(buffer, sizeof(buffer), "SPEED %u", command.Speed);

View File

@ -27,14 +27,14 @@ struct TitleCommand
{
uint8_t X;
uint8_t Y;
};
} Location;
uint8_t Rotations; // ROTATE (counter-clockwise)
uint8_t Zoom; // ZOOM
struct // FOLLOW
{
EntityId SpriteIndex;
utf8 SpriteName[USER_STRING_MAX_LENGTH];
};
} Follow;
uint8_t Speed; // SPEED
uint16_t Milliseconds; // WAIT
utf8 Scenario[TITLE_COMMAND_SCENARIO_LENGTH]; // LOADSC