From 39662aabefa851a153b66571a50fd4b78b83ced1 Mon Sep 17 00:00:00 2001 From: dP Date: Sat, 25 Sep 2021 14:39:40 +0300 Subject: Add: allow gamescripts to build neutral objects (#9568) --- src/command.cpp | 2 +- src/object_cmd.cpp | 2 +- src/script/api/CMakeLists.txt | 4 +++ src/script/api/ai_changelog.hpp | 2 ++ src/script/api/game_changelog.hpp | 2 ++ src/script/api/script_objecttype.cpp | 45 +++++++++++++++++++++++++ src/script/api/script_objecttype.hpp | 57 ++++++++++++++++++++++++++++++++ src/script/api/script_objecttypelist.cpp | 23 +++++++++++++ src/script/api/script_objecttypelist.hpp | 26 +++++++++++++++ 9 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 src/script/api/script_objecttype.cpp create mode 100644 src/script/api/script_objecttype.hpp create mode 100644 src/script/api/script_objecttypelist.cpp create mode 100644 src/script/api/script_objecttypelist.hpp diff --git a/src/command.cpp b/src/command.cpp index 91cb12a28..7d1a72dec 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -225,7 +225,7 @@ static const Command _command_proc_table[] = { DEF_CMD(CmdBuildSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_SIGNALS DEF_CMD(CmdRemoveSingleSignal, CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_SIGNALS DEF_CMD(CmdTerraformLand, CMD_ALL_TILES | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_TERRAFORM_LAND - DEF_CMD(CmdBuildObject, CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_OBJECT + DEF_CMD(CmdBuildObject, CMD_DEITY | CMD_NO_WATER | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_OBJECT DEF_CMD(CmdBuildTunnel, CMD_DEITY | CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_TUNNEL DEF_CMD(CmdRemoveFromRailStation, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_REMOVE_FROM_RAIL_STATION DEF_CMD(CmdConvertRail, 0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_CONVERT_RAILD diff --git a/src/object_cmd.cpp b/src/object_cmd.cpp index dc517dc5f..14645f93d 100644 --- a/src/object_cmd.cpp +++ b/src/object_cmd.cpp @@ -349,7 +349,7 @@ CommandCost CmdBuildObject(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 } if (flags & DC_EXEC) { - BuildObject(type, tile, _current_company, nullptr, view); + BuildObject(type, tile, _current_company == OWNER_DEITY ? OWNER_NONE : _current_company, nullptr, view); /* Make sure the HQ starts at the right size. */ if (type == OBJECT_HQ) UpdateCompanyHQ(tile, hq_score); diff --git a/src/script/api/CMakeLists.txt b/src/script/api/CMakeLists.txt index a6390e2bb..b7e0e3014 100644 --- a/src/script/api/CMakeLists.txt +++ b/src/script/api/CMakeLists.txt @@ -182,6 +182,8 @@ add_files( script_newgrf.hpp script_news.hpp script_object.hpp + script_objecttype.hpp + script_objecttypelist.hpp script_order.hpp script_priorityqueue.hpp script_rail.hpp @@ -250,6 +252,8 @@ add_files( script_newgrf.cpp script_news.cpp script_object.cpp + script_objecttype.cpp + script_objecttypelist.cpp script_order.cpp script_priorityqueue.cpp script_rail.cpp diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index f977168e4..c1ea5e152 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -26,6 +26,8 @@ * \li AITile::IsSeaTile * \li AITile::IsRiverTile * \li AITile::BT_CLEAR_WATER + * \li AIObjectTypeList + * \li AIObjectType * * \b 1.11.0 * diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index 8eab8956e..741d3f56d 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -25,6 +25,8 @@ * \li GSTile::IsSeaTile * \li GSTile::IsRiverTile * \li GSTile::BT_CLEAR_WATER + * \li GSObjectTypeList + * \li GSObjectType * * \b 1.11.0 * diff --git a/src/script/api/script_objecttype.cpp b/src/script/api/script_objecttype.cpp new file mode 100644 index 000000000..27519a664 --- /dev/null +++ b/src/script/api/script_objecttype.cpp @@ -0,0 +1,45 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file script_objecttype.cpp Implementation of ScriptObjectType. */ + +#include "../../stdafx.h" + +#include "script_objecttype.hpp" + +#include "script_error.hpp" +#include "script_map.hpp" + +#include "../../safeguards.h" + +/* static */ bool ScriptObjectType::IsValidObjectType(ObjectType object_type) +{ + if (object_type >= NUM_OBJECTS) return false; + return ObjectSpec::Get(object_type)->IsEverAvailable(); +} + +/* static */ char *ScriptObjectType::GetName(ObjectType object_type) +{ + EnforcePrecondition(nullptr, IsValidObjectType(object_type)); + + return GetString(ObjectSpec::Get(object_type)->name); +} + +/* static */ uint8 ScriptObjectType::GetViews(ObjectType object_type) +{ + EnforcePrecondition(0, IsValidObjectType(object_type)); + + return ObjectSpec::Get(object_type)->views; +} + +/* static */ bool ScriptObjectType::BuildObject(ObjectType object_type, uint8 view, TileIndex tile) +{ + EnforcePrecondition(false, IsValidObjectType(object_type)); + EnforcePrecondition(false, ScriptMap::IsValidTile(tile)); + + return ScriptObject::DoCommand(tile, object_type, view, CMD_BUILD_OBJECT); +} diff --git a/src/script/api/script_objecttype.hpp b/src/script/api/script_objecttype.hpp new file mode 100644 index 000000000..d3f5d4a51 --- /dev/null +++ b/src/script/api/script_objecttype.hpp @@ -0,0 +1,57 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file script_objecttype.hpp Everything to query and build industries. */ + +#ifndef SCRIPT_OBJECTTYPE_HPP +#define SCRIPT_OBJECTTYPE_HPP + +#include "script_list.hpp" + +#include "../../newgrf_object.h" + +/** + * Class that handles all object-type related functions. + * @api ai game + */ +class ScriptObjectType : public ScriptObject { +public: + /** + * Checks whether the given object-type is valid. + * @param object_type The type to check. + * @return True if and only if the object-type is valid. + */ + static bool IsValidObjectType(ObjectType object_type); + + /** + * Get the name of an object-type. + * @param object_type The type to get the name for. + * @pre IsValidObjectType(object_type). + * @return The name of an object. + */ + static char *GetName(ObjectType object_type); + + /** + * Get the number of views for an object-type. + * @param object_type The type to get the number of views for. + * @pre IsValidObjectType(object_type). + * @return The number of views for an object. + */ + static uint8 GetViews(ObjectType object_type); + + /** + * Build an object of the specified type. + * @param object_type The type of the object to build. + * @param view The view for teh object. + * @param tile The tile to build the object on. + * @pre IsValidObjectType(object_type). + * @return True if the object was successfully build. + */ + static bool BuildObject(ObjectType object_type, uint8 view, TileIndex tile); +}; + +#endif /* SCRIPT_OBJECTTYPE_HPP */ diff --git a/src/script/api/script_objecttypelist.cpp b/src/script/api/script_objecttypelist.cpp new file mode 100644 index 000000000..0086260b2 --- /dev/null +++ b/src/script/api/script_objecttypelist.cpp @@ -0,0 +1,23 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file script_objecttypelist.cpp Implementation of ScriptObjectTypeList. */ + +#include "../../stdafx.h" +#include "script_objecttypelist.hpp" +#include "../../newgrf_object.h" + +#include "../../safeguards.h" + +ScriptObjectTypeList::ScriptObjectTypeList() +{ + for (int i = 0; i < NUM_OBJECTS; i++) { + const ObjectSpec *spec = ObjectSpec::Get(i); + if (!spec->IsEverAvailable()) continue; + this->AddItem(i); + } +} diff --git a/src/script/api/script_objecttypelist.hpp b/src/script/api/script_objecttypelist.hpp new file mode 100644 index 000000000..0276838e8 --- /dev/null +++ b/src/script/api/script_objecttypelist.hpp @@ -0,0 +1,26 @@ +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file script_objecttypelist.hpp List all available object types. */ + +#ifndef SCRIPT_OBJECTTYPELIST_HPP +#define SCRIPT_OBJECTTYPELIST_HPP + +#include "script_objecttype.hpp" + +/** + * Creates a list of valid object types. + * @api ai game + * @ingroup ScriptList + */ +class ScriptObjectTypeList : public ScriptList { +public: + ScriptObjectTypeList(); +}; + + +#endif /* SCRIPT_OBJECTTYPELIST_HPP */ -- cgit v1.2.3-70-g09d2