From b62452903a3af9e51dc308fd674a788214942382 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Wed, 13 Feb 2019 21:18:44 +0000 Subject: Add: AI functions to set/get vehicle group parent. --- src/script/api/ai/ai_group.hpp.sq | 2 ++ src/script/api/ai_changelog.hpp | 2 ++ src/script/api/script_group.cpp | 16 ++++++++++++++++ src/script/api/script_group.hpp | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+) (limited to 'src/script/api') diff --git a/src/script/api/ai/ai_group.hpp.sq b/src/script/api/ai/ai_group.hpp.sq index 52ade08d5..a5f0e25a0 100644 --- a/src/script/api/ai/ai_group.hpp.sq +++ b/src/script/api/ai/ai_group.hpp.sq @@ -31,6 +31,8 @@ void SQAIGroup_Register(Squirrel *engine) SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetVehicleType, "GetVehicleType", 2, ".i"); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::SetName, "SetName", 3, ".i."); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetName, "GetName", 2, ".i"); + SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::SetParent, "SetParent", 3, ".ii"); + SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetParent, "GetParent", 2, ".i"); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::EnableAutoReplaceProtection, "EnableAutoReplaceProtection", 3, ".ib"); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetAutoReplaceProtection, "GetAutoReplaceProtection", 2, ".i"); SQAIGroup.DefSQStaticMethod(engine, &ScriptGroup::GetNumEngines, "GetNumEngines", 3, ".ii"); diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index eb2b3a501..bba36b7ea 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -20,6 +20,8 @@ * 1.9.0 is not yet released. The following changes are not set in stone yet. * API additions: * \li AIAirport::GetMonthlyMaintenanceCost + * \li AIGroup::SetParent + * \li AIGroup::GetParent * * Other changes: * \li AIBridge::GetName takes one extra parameter to refer the vehicle type diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp index cf8dd51a8..7823fb28b 100644 --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -70,6 +70,22 @@ return GetString(STR_GROUP_NAME); } +/* static */ bool ScriptGroup::SetParent(GroupID group_id, GroupID parent_group_id) +{ + EnforcePrecondition(false, IsValidGroup(group_id)); + EnforcePrecondition(false, IsValidGroup(parent_group_id)); + + return ScriptObject::DoCommand(0, group_id | 1 << 16, parent_group_id, CMD_ALTER_GROUP); +} + +/* static */ ScriptGroup::GroupID ScriptGroup::GetParent(GroupID group_id) +{ + EnforcePrecondition((ScriptGroup::GroupID)INVALID_GROUP, IsValidGroup(group_id)); + + const Group *g = ::Group::GetIfValid(group_id); + return (ScriptGroup::GroupID)g->parent; +} + /* static */ bool ScriptGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable) { EnforcePrecondition(false, IsValidGroup(group_id)); diff --git a/src/script/api/script_group.hpp b/src/script/api/script_group.hpp index fd8888a17..7b375e29a 100644 --- a/src/script/api/script_group.hpp +++ b/src/script/api/script_group.hpp @@ -84,6 +84,24 @@ public: */ static char *GetName(GroupID group_id); + /** + * Set parent group of a group. + * @param group_id The group to set the parent for. + * @param parent_group_id The parent group to set. + * @pre IsValidGroup(group_id). + * @pre IsValidGroup(parent_group_id). + * @return True if and only if the parent group was changed. + */ + static bool SetParent(GroupID group_id, GroupID parent_group_id); + + /** + * Get parent group of a group. + * @param group_id The group to get the parent of. + * @pre IsValidGroup(group_id). + * @return The group id of the parent group. + */ + static GroupID GetParent(GroupID group_id); + /** * Enable or disable autoreplace protected. If the protection is * enabled, global autoreplace won't affect vehicles in this group. -- cgit v1.2.3-54-g00ecf