diff --git a/regression/regression/main.nut b/regression/regression/main.nut index b37b54f21c..ac0494e1cf 100644 --- a/regression/regression/main.nut +++ b/regression/regression/main.nut @@ -828,6 +828,13 @@ function Regression::List() print(" []:"); print(" 4000 => " + list[4000]); + print(" clone:"); + local list3 = clone list; + print(" Clone ListDump:"); + foreach (idx, val in list3) { + print(" " + idx + " => " + val); + } + list.Clear(); print(" IsEmpty(): " + list.IsEmpty()); @@ -860,6 +867,12 @@ function Regression::List() it = list.Next(); print(" " + it + " => " + list.GetValue(it)); } + + print(" Clone ListDump:"); + foreach (idx, val in list3) { + print(" " + idx + " => " + val); + } + } function Regression::Map() diff --git a/regression/regression/result.txt b/regression/regression/result.txt index 510be2a4da..676d30ec0c 100644 --- a/regression/regression/result.txt +++ b/regression/regression/result.txt @@ -575,6 +575,13 @@ 4006 => 12 []: 4000 => 50 + clone: + Clone ListDump: + 1005 => 1005 + 4000 => 50 + 4001 => 8002 + 4002 => 8004 + 4006 => 12 IsEmpty(): true 0 => 5 (true) ERROR: Next() is invalid as Begin() is never called @@ -584,6 +591,12 @@ ERROR: IsEnd() is invalid as Begin() is never called 2 => 6 (true) 3 => 6 (true) 9 => 0 (false) + Clone ListDump: + 1005 => 1005 + 4000 => 50 + 4001 => 8002 + 4002 => 8004 + 4006 => 12 --Company-- SetName(): true @@ -9795,7 +9808,7 @@ ERROR: IsEnd() is invalid as Begin() is never called constructor failed with: excessive CPU usage in list filter function Your script made an error: excessive CPU usage in valuator function -*FUNCTION [Start()] regression/main.nut line [2120] +*FUNCTION [Start()] regression/main.nut line [2133] [Infinite] CLOSURE [list] INSTANCE diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index d359902418..f76a586f07 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -34,6 +34,7 @@ * \li AIWaypoint::GetWaypointID now returns the StationID of any type of waypoint * \li AIList instances can now be saved * \li AIVehicleList_Station accepts an optional AIVehicle::VehicleType parameter + * \li AIList instances can now be cloned * * \b 14.0 * diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index e57e873a14..7bb7203d20 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -35,6 +35,7 @@ * \li GSWaypoint::GetWaypointID now returns the StationID of any type of waypoint * \li GSList instances can now be saved * \li GSVehicleList_Station accepts an optional GSVehicle::VehicleType parameter + * \li GSList instances can now be cloned * * \b 14.0 * diff --git a/src/script/api/script_list.cpp b/src/script/api/script_list.cpp index e7e2f9caff..134b80287b 100644 --- a/src/script/api/script_list.cpp +++ b/src/script/api/script_list.cpp @@ -453,6 +453,20 @@ bool ScriptList::LoadObject(HSQUIRRELVM vm) return true; } +ScriptObject *ScriptList::CloneObject() +{ + ScriptList *clone = new ScriptList(); + clone->CopyList(this); + return clone; +} + +void ScriptList::CopyList(const ScriptList *list) +{ + this->Sort(list->sorter_type, list->sort_ascending); + this->items = list->items; + this->buckets = list->buckets; +} + ScriptList::ScriptList() { /* Default sorter */ diff --git a/src/script/api/script_list.hpp b/src/script/api/script_list.hpp index 8a50c6d4e9..6027ea6273 100644 --- a/src/script/api/script_list.hpp +++ b/src/script/api/script_list.hpp @@ -145,6 +145,13 @@ protected: virtual bool SaveObject(HSQUIRRELVM vm) override; virtual bool LoadObject(HSQUIRRELVM vm) override; + virtual ScriptObject *CloneObject() override; + + /** + * Copy the content of a list. + * @param list The list that will be copied. + */ + void CopyList(const ScriptList *list); public: typedef std::set ScriptItemList; ///< The list of items inside the bucket diff --git a/src/script/api/script_tilelist.cpp b/src/script/api/script_tilelist.cpp index 4eefa8daca..9f89ae0c6d 100644 --- a/src/script/api/script_tilelist.cpp +++ b/src/script/api/script_tilelist.cpp @@ -23,6 +23,13 @@ bool ScriptTileList::SaveObject(HSQUIRRELVM vm) return true; } +ScriptObject *ScriptTileList::CloneObject() +{ + ScriptTileList *clone = new ScriptTileList(); + clone->CopyList(this); + return clone; +} + void ScriptTileList::AddRectangle(TileIndex t1, TileIndex t2) { if (!::IsValidTile(t1)) return; diff --git a/src/script/api/script_tilelist.hpp b/src/script/api/script_tilelist.hpp index a6308ec182..4b2a2ef9f6 100644 --- a/src/script/api/script_tilelist.hpp +++ b/src/script/api/script_tilelist.hpp @@ -22,6 +22,7 @@ class ScriptTileList : public ScriptList { protected: virtual bool SaveObject(HSQUIRRELVM) override; + virtual ScriptObject *CloneObject() override; public: /** * Adds the rectangle between tile_from and tile_to to the to-be-evaluated tiles.