1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 04:23:20 +01:00

Use vector for TitleSequence commands

Co-authored-by: Gabriel Guedes <gabriel.guedesaz@gmail.com>
This commit is contained in:
Matt
2020-09-27 16:30:20 +03:00
parent 7245f23422
commit 612249bd74
5 changed files with 82 additions and 103 deletions

View File

@@ -114,7 +114,7 @@ public:
}
// Check that position is valid
if (_position >= static_cast<int32_t>(_sequence->NumCommands))
if (_position >= static_cast<int32_t>(_sequence->Commands.size()))
{
_position = 0;
return false;
@@ -126,8 +126,8 @@ public:
_waitCounter--;
if (_waitCounter == 0)
{
const TitleCommand* command = &_sequence->Commands[_position];
if (command->Type == TITLE_SCRIPT_WAIT)
const auto& command = _sequence->Commands[_position];
if (command.Type == TITLE_SCRIPT_WAIT)
{
IncrementPosition();
}
@@ -137,14 +137,14 @@ public:
{
while (true)
{
const TitleCommand* command = &_sequence->Commands[_position];
const auto& command = _sequence->Commands[_position];
if (ExecuteCommand(command))
{
if (command->Type == TITLE_SCRIPT_WAIT)
if (command.Type == TITLE_SCRIPT_WAIT)
{
break;
}
if (command->Type != TITLE_SCRIPT_RESTART)
if (command.Type != TITLE_SCRIPT_RESTART)
{
IncrementPosition();
}
@@ -176,7 +176,7 @@ public:
void Seek(int32_t targetPosition) override
{
if (targetPosition < 0 || targetPosition >= static_cast<int32_t>(_sequence->NumCommands))
if (targetPosition < 0 || targetPosition >= static_cast<int32_t>(_sequence->Commands.size()))
{
throw std::runtime_error("Invalid position.");
}
@@ -192,7 +192,7 @@ public:
// Set position to the last LOAD command before target position
for (int32_t i = targetPosition; i >= 0; i--)
{
const TitleCommand* command = &_sequence->Commands[i];
const TitleCommand& command = _sequence->Commands[i];
if ((_position == i && _position != targetPosition) || TitleSequenceIsLoadCommand(command))
{
// Break if we have a new load command or if we're already in the range of the correct load command
@@ -225,7 +225,7 @@ private:
void IncrementPosition()
{
_position++;
if (_position >= static_cast<int32_t>(_sequence->NumCommands))
if (_position >= static_cast<int32_t>(_sequence->Commands.size()))
{
_position = 0;
}
@@ -239,13 +239,13 @@ private:
{
IncrementPosition();
command = &_sequence->Commands[_position];
} while (!TitleSequenceIsLoadCommand(command) && _position != entryPosition);
} while (!TitleSequenceIsLoadCommand(*command) && _position != entryPosition);
return _position != entryPosition;
}
bool ExecuteCommand(const TitleCommand* command)
bool ExecuteCommand(const TitleCommand& command)
{
switch (command->Type)
switch (command.Type)
{
case TITLE_SCRIPT_END:
_waitCounter = 1;
@@ -253,25 +253,25 @@ private:
case TITLE_SCRIPT_WAIT:
// The waitCounter is measured in 25-ms game ticks. Previously it was seconds * 40 ticks/second, now it is ms /
// 25 ms/tick
_waitCounter = std::max<int32_t>(1, command->Milliseconds / static_cast<uint32_t>(GAME_UPDATE_TIME_MS));
_waitCounter = std::max<int32_t>(1, command.Milliseconds / static_cast<uint32_t>(GAME_UPDATE_TIME_MS));
break;
case TITLE_SCRIPT_LOCATION:
{
auto loc = TileCoordsXY(command->X, command->Y).ToCoordsXY().ToTileCentre();
auto loc = TileCoordsXY(command.X, command.Y).ToCoordsXY().ToTileCentre();
SetViewLocation(loc);
break;
}
case TITLE_SCRIPT_ROTATE:
RotateView(command->Rotations);
RotateView(command.Rotations);
break;
case TITLE_SCRIPT_ZOOM:
SetViewZoom(command->Zoom);
SetViewZoom(command.Zoom);
break;
case TITLE_SCRIPT_SPEED:
gGameSpeed = std::clamp<uint8_t>(command->Speed, 1, 4);
gGameSpeed = std::clamp<uint8_t>(command.Speed, 1, 4);
break;
case TITLE_SCRIPT_FOLLOW:
FollowSprite(command->SpriteIndex);
FollowSprite(command.SpriteIndex);
break;
case TITLE_SCRIPT_RESTART:
Reset();
@@ -279,7 +279,7 @@ private:
case TITLE_SCRIPT_LOAD:
{
bool loadSuccess = false;
uint8_t saveIndex = command->SaveIndex;
uint8_t saveIndex = command.SaveIndex;
TitleSequenceParkHandle* parkHandle = TitleSequenceGetParkHandle(*_sequence, saveIndex);
if (parkHandle != nullptr)
{
@@ -300,14 +300,14 @@ private:
case TITLE_SCRIPT_LOADSC:
{
bool loadSuccess = false;
auto scenario = GetScenarioRepository()->GetByInternalName(command->Scenario);
auto scenario = GetScenarioRepository()->GetByInternalName(command.Scenario);
if (scenario != nullptr)
{
loadSuccess = LoadParkFromFile(scenario->path);
}
if (!loadSuccess)
{
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", command->Scenario);
Console::Error::WriteLine("Failed to load: \"%s\" for the title sequence.", command.Scenario);
return false;
}
break;

View File

@@ -329,13 +329,7 @@ static void window_title_command_editor_mouseup(rct_window* w, rct_widgetindex w
if (_window_title_command_editor_insert)
{
size_t insertIndex = _window_title_command_editor_index;
_sequence->NumCommands++;
_sequence->Commands = Memory::ReallocateArray(_sequence->Commands, _sequence->NumCommands);
for (size_t i = _sequence->NumCommands - 1; i > insertIndex; i--)
{
_sequence->Commands[i] = _sequence->Commands[i - 1];
}
_sequence->Commands[insertIndex] = command;
_sequence->Commands.insert(_sequence->Commands.begin() + insertIndex, command);
}
else
{

View File

@@ -381,14 +381,14 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
window_title_command_editor_open(_editingTitleSequence.get(), w->selected_list_item + 1, true);
else
window_title_command_editor_open(
_editingTitleSequence.get(), static_cast<int32_t>(_editingTitleSequence->NumCommands), true);
_editingTitleSequence.get(), static_cast<int32_t>(_editingTitleSequence->Commands.size()), true);
}
break;
case WIDX_TITLE_EDITOR_EDIT:
if (window_title_editor_check_can_edit())
{
if (w->selected_list_item != -1
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->NumCommands))
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->Commands.size()))
{
window_title_command_editor_open(_editingTitleSequence.get(), w->selected_list_item, false);
}
@@ -398,15 +398,10 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
if (window_title_editor_check_can_edit())
{
if (w->selected_list_item != -1
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->NumCommands))
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->Commands.size()))
{
for (int32_t i = w->selected_list_item; i < static_cast<int16_t>(_editingTitleSequence->NumCommands) - 1;
i++)
{
_editingTitleSequence->Commands[i] = _editingTitleSequence->Commands[i + 1];
}
_editingTitleSequence->NumCommands--;
if (w->selected_list_item >= static_cast<int16_t>(_editingTitleSequence->NumCommands))
_editingTitleSequence->Commands.erase(_editingTitleSequence->Commands.begin() + w->selected_list_item);
if (w->selected_list_item >= static_cast<int16_t>(_editingTitleSequence->Commands.size()))
{
w->selected_list_item--;
}
@@ -418,7 +413,7 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
{
int32_t position = w->selected_list_item;
if (title_is_previewing_sequence() && position != -1
&& position < static_cast<int32_t>(_editingTitleSequence->NumCommands))
&& position < static_cast<int32_t>(_editingTitleSequence->Commands.size()))
{
auto player = window_title_editor_get_player();
player->Seek(position);
@@ -430,13 +425,11 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
if (window_title_editor_check_can_edit())
{
if (w->selected_list_item != -1
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->NumCommands) - 1)
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->Commands.size()) - 1)
{
TitleCommand* a = &_editingTitleSequence->Commands[w->selected_list_item];
TitleCommand* b = &_editingTitleSequence->Commands[w->selected_list_item + 1];
TitleCommand tmp = *a;
*a = *b;
*b = tmp;
std::swap(
_editingTitleSequence->Commands[w->selected_list_item],
_editingTitleSequence->Commands[w->selected_list_item + 1]);
w->selected_list_item++;
TitleSequenceSave(*_editingTitleSequence);
}
@@ -446,13 +439,11 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
if (window_title_editor_check_can_edit())
{
if (w->selected_list_item > 0
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->NumCommands))
&& w->selected_list_item < static_cast<int16_t>(_editingTitleSequence->Commands.size()))
{
TitleCommand* a = &_editingTitleSequence->Commands[w->selected_list_item - 1];
TitleCommand* b = &_editingTitleSequence->Commands[w->selected_list_item];
TitleCommand tmp = *b;
*b = *a;
*a = tmp;
std::swap(
_editingTitleSequence->Commands[w->selected_list_item - 1],
_editingTitleSequence->Commands[w->selected_list_item]);
w->selected_list_item--;
TitleSequenceSave(*_editingTitleSequence);
}
@@ -490,7 +481,7 @@ static void window_title_editor_mouseup(rct_window* w, rct_widgetindex widgetInd
{
auto player = window_title_editor_get_player();
int32_t position = player->GetCurrentPosition() + 1;
if (position >= static_cast<int32_t>(_editingTitleSequence->NumCommands))
if (position >= static_cast<int32_t>(_editingTitleSequence->Commands.size()))
{
position = 0;
}
@@ -589,7 +580,7 @@ static void window_title_editor_scrollgetsize(rct_window* w, int32_t scrollIndex
if (w->selected_tab == WINDOW_TITLE_EDITOR_TAB_SAVES)
lineCount = _editingTitleSequence->Saves.size();
else if (w->selected_tab == WINDOW_TITLE_EDITOR_TAB_SCRIPT)
lineCount = _editingTitleSequence->NumCommands;
lineCount = _editingTitleSequence->Commands.size();
*height = static_cast<int32_t>(lineCount * SCROLLABLE_ROW_HEIGHT);
int32_t i = *height - window_title_editor_widgets[WIDX_TITLE_EDITOR_LIST].bottom
@@ -621,7 +612,7 @@ static void window_title_editor_scrollmousedown(rct_window* w, int32_t scrollInd
}
break;
case WINDOW_TITLE_EDITOR_TAB_SCRIPT:
if (index < static_cast<int32_t>(_editingTitleSequence->NumCommands))
if (index < static_cast<int32_t>(_editingTitleSequence->Commands.size()))
{
w->selected_list_item = index;
widget_invalidate(w, WIDX_TITLE_EDITOR_LIST);
@@ -640,7 +631,7 @@ static void window_title_editor_scrollmouseover(rct_window* w, int32_t scrollInd
_window_title_editor_highlighted_index = static_cast<int16_t>(index);
break;
case WINDOW_TITLE_EDITOR_TAB_SCRIPT:
if (index < static_cast<int32_t>(_editingTitleSequence->NumCommands))
if (index < static_cast<int32_t>(_editingTitleSequence->Commands.size()))
_window_title_editor_highlighted_index = static_cast<int16_t>(index);
break;
}
@@ -912,10 +903,10 @@ static void window_title_editor_scrollpaint_commands(rct_window* w, rct_drawpixe
}
auto screenCoords = ScreenCoordsXY{ 0, 0 };
for (int32_t i = 0; i < static_cast<int32_t>(_editingTitleSequence->NumCommands);
for (int32_t i = 0; i < static_cast<int32_t>(_editingTitleSequence->Commands.size());
i++, screenCoords.y += SCROLLABLE_ROW_HEIGHT)
{
TitleCommand* command = &_editingTitleSequence->Commands[i];
TitleCommand& command = _editingTitleSequence->Commands[i];
bool selected = false;
bool hover = false;
bool error = false;
@@ -939,51 +930,51 @@ static void window_title_editor_scrollpaint_commands(rct_window* w, rct_drawpixe
auto ft = Formatter::Common();
rct_string_id commandName = STR_NONE;
switch (command->Type)
switch (command.Type)
{
case TITLE_SCRIPT_LOAD:
commandName = STR_TITLE_EDITOR_COMMAND_LOAD_FILE;
if (command->SaveIndex == SAVE_INDEX_INVALID)
if (command.SaveIndex == SAVE_INDEX_INVALID)
{
commandName = STR_TITLE_EDITOR_COMMAND_LOAD_NO_SAVE;
error = true;
}
else
{
ft.Add<const char*>(_editingTitleSequence->Saves[command->SaveIndex].c_str());
ft.Add<const char*>(_editingTitleSequence->Saves[command.SaveIndex].c_str());
}
break;
case TITLE_SCRIPT_LOCATION:
commandName = STR_TITLE_EDITOR_COMMAND_LOCATION;
ft.Add<uint16_t>(command->X);
ft.Add<uint16_t>(command->Y);
ft.Add<uint16_t>(command.X);
ft.Add<uint16_t>(command.Y);
break;
case TITLE_SCRIPT_ROTATE:
commandName = STR_TITLE_EDITOR_COMMAND_ROTATE;
ft.Add<uint16_t>(command->Rotations);
ft.Add<uint16_t>(command.Rotations);
break;
case TITLE_SCRIPT_ZOOM:
commandName = STR_TITLE_EDITOR_COMMAND_ZOOM;
ft.Add<uint16_t>(command->Zoom);
ft.Add<uint16_t>(command.Zoom);
break;
case TITLE_SCRIPT_SPEED:
commandName = STR_TITLE_EDITOR_COMMAND_SPEED;
ft.Add<rct_string_id>(SpeedNames[command->Speed - 1]);
ft.Add<rct_string_id>(SpeedNames[command.Speed - 1]);
break;
case TITLE_SCRIPT_FOLLOW:
commandName = STR_TITLE_EDITOR_COMMAND_FOLLOW;
if (command->SpriteIndex == SPRITE_INDEX_NULL)
if (command.SpriteIndex == SPRITE_INDEX_NULL)
{
commandName = STR_TITLE_EDITOR_COMMAND_FOLLOW_NO_SPRITE;
}
else
{
ft.Add<utf8*>(command->SpriteName);
ft.Add<utf8*>(command.SpriteName);
}
break;
case TITLE_SCRIPT_WAIT:
commandName = STR_TITLE_EDITOR_COMMAND_WAIT;
ft.Add<uint16_t>(command->Milliseconds);
ft.Add<uint16_t>(command.Milliseconds);
break;
case TITLE_SCRIPT_RESTART:
commandName = STR_TITLE_EDITOR_RESTART;
@@ -995,8 +986,8 @@ static void window_title_editor_scrollpaint_commands(rct_window* w, rct_drawpixe
{
commandName = STR_TITLE_EDITOR_COMMAND_LOAD_FILE;
const char* name = "";
auto scenario = GetScenarioRepository()->GetByInternalName(command->Scenario);
if (command->Scenario[0] == '\0')
auto scenario = GetScenarioRepository()->GetByInternalName(command.Scenario);
if (command.Scenario[0] == '\0')
{
commandName = STR_TITLE_EDITOR_COMMAND_LOAD_NO_SCENARIO;
}
@@ -1012,7 +1003,7 @@ static void window_title_editor_scrollpaint_commands(rct_window* w, rct_drawpixe
break;
}
default:
log_warning("Unknown command %d", command->Type);
log_warning("Unknown command %d", command.Type);
}
char buffer[256];

View File

@@ -35,7 +35,7 @@ static std::vector<utf8*> GetSaves(IZipArchive* zip);
static std::vector<TitleCommand> LegacyScriptRead(utf8* script, size_t scriptLength, std::vector<utf8*> saves);
static void LegacyScriptGetLine(OpenRCT2::IStream* stream, char* parts);
static std::vector<uint8_t> ReadScriptFile(const utf8* path);
static std::string LegacyScriptWrite(TitleSequence& seq);
static std::string LegacyScriptWrite(const TitleSequence& seq);
std::unique_ptr<TitleSequence> CreateTitleSequence()
{
@@ -95,8 +95,7 @@ std::unique_ptr<TitleSequence> LoadTitleSequence(const utf8* path)
{
seq->Saves.push_back(save);
}
seq->NumCommands = commands.size();
seq->Commands = Collections::ToArray(commands);
seq->Commands = commands;
seq->IsZip = isZip;
return seq;
}
@@ -105,7 +104,6 @@ void FreeTitleSequence(TitleSequence& seq)
{
Memory::Free(seq.Name);
Memory::Free(seq.Path);
Memory::Free(seq.Commands);
}
TitleSequenceParkHandle* TitleSequenceGetParkHandle(TitleSequence& seq, size_t index)
@@ -302,20 +300,19 @@ bool TitleSequenceRemovePark(TitleSequence& seq, size_t index)
seq.Saves.erase(seq.Saves.begin() + index);
// Update load commands
for (size_t i = 0; i < seq.NumCommands; i++)
for (auto& command : seq.Commands)
{
TitleCommand* command = &seq.Commands[i];
if (command->Type == TITLE_SCRIPT_LOAD)
if (command.Type == TITLE_SCRIPT_LOAD)
{
if (command->SaveIndex == index)
if (command.SaveIndex == index)
{
// Park no longer exists, so reset load command to invalid
command->SaveIndex = SAVE_INDEX_INVALID;
command.SaveIndex = SAVE_INDEX_INVALID;
}
else if (command->SaveIndex > index)
else if (command.SaveIndex > index)
{
// Park index will have shifted by -1
command->SaveIndex--;
command.SaveIndex--;
}
}
}
@@ -524,7 +521,7 @@ static std::vector<uint8_t> ReadScriptFile(const utf8* path)
return result;
}
static std::string LegacyScriptWrite(TitleSequence& seq)
static std::string LegacyScriptWrite(const TitleSequence& seq)
{
utf8 buffer[128];
auto sb = StringBuilder(128);
@@ -532,56 +529,55 @@ static std::string LegacyScriptWrite(TitleSequence& seq)
sb.Append("# SCRIPT FOR ");
sb.Append(seq.Name);
sb.Append("\n");
for (size_t i = 0; i < seq.NumCommands; i++)
for (const auto& command : seq.Commands)
{
const TitleCommand* command = &seq.Commands[i];
switch (command->Type)
switch (command.Type)
{
case TITLE_SCRIPT_LOAD:
if (command->SaveIndex == 0xFF)
if (command.SaveIndex == 0xFF)
{
sb.Append("LOAD <No save file>");
}
else
{
sb.Append("LOAD ");
sb.Append(seq.Saves[command->SaveIndex].c_str());
sb.Append(seq.Saves[command.SaveIndex].c_str());
}
break;
case TITLE_SCRIPT_LOADSC:
if (command->Scenario[0] == '\0')
if (command.Scenario[0] == '\0')
{
sb.Append("LOADSC <No scenario name>");
}
else
{
sb.Append("LOADSC ");
sb.Append(command->Scenario);
sb.Append(command.Scenario);
}
break;
case TITLE_SCRIPT_LOCATION:
String::Format(buffer, sizeof(buffer), "LOCATION %u %u", command->X, command->Y);
String::Format(buffer, sizeof(buffer), "LOCATION %u %u", command.X, command.Y);
sb.Append(buffer);
break;
case TITLE_SCRIPT_ROTATE:
String::Format(buffer, sizeof(buffer), "ROTATE %u", command->Rotations);
String::Format(buffer, sizeof(buffer), "ROTATE %u", command.Rotations);
sb.Append(buffer);
break;
case TITLE_SCRIPT_ZOOM:
String::Format(buffer, sizeof(buffer), "ZOOM %u", command->Zoom);
String::Format(buffer, sizeof(buffer), "ZOOM %u", command.Zoom);
sb.Append(buffer);
break;
case TITLE_SCRIPT_FOLLOW:
String::Format(buffer, sizeof(buffer), "FOLLOW %u ", command->SpriteIndex);
String::Format(buffer, sizeof(buffer), "FOLLOW %u ", command.SpriteIndex);
sb.Append(buffer);
sb.Append(command->SpriteName);
sb.Append(command.SpriteName);
break;
case TITLE_SCRIPT_SPEED:
String::Format(buffer, sizeof(buffer), "SPEED %u", command->Speed);
String::Format(buffer, sizeof(buffer), "SPEED %u", command.Speed);
sb.Append(buffer);
break;
case TITLE_SCRIPT_WAIT:
String::Format(buffer, sizeof(buffer), "WAIT %u", command->Milliseconds);
String::Format(buffer, sizeof(buffer), "WAIT %u", command.Milliseconds);
sb.Append(buffer);
break;
case TITLE_SCRIPT_RESTART:
@@ -596,9 +592,9 @@ static std::string LegacyScriptWrite(TitleSequence& seq)
return sb.GetBuffer();
}
bool TitleSequenceIsLoadCommand(const TitleCommand* command)
bool TitleSequenceIsLoadCommand(const TitleCommand& command)
{
switch (command->Type)
switch (command.Type)
{
case TITLE_SCRIPT_LOAD:
case TITLE_SCRIPT_LOADSC:

View File

@@ -45,9 +45,7 @@ struct TitleSequence
const utf8* Name = nullptr;
const utf8* Path = nullptr;
size_t NumCommands = 0;
TitleCommand* Commands = 0;
std::vector<TitleCommand> Commands;
std::vector<std::string> Saves;
bool IsZip = false;
@@ -95,4 +93,4 @@ bool TitleSequenceAddPark(TitleSequence& seq, const utf8* path, const utf8* name
bool TitleSequenceRenamePark(TitleSequence& seq, size_t index, const utf8* name);
bool TitleSequenceRemovePark(TitleSequence& seq, size_t index);
bool TitleSequenceIsLoadCommand(const TitleCommand* command);
bool TitleSequenceIsLoadCommand(const TitleCommand& command);