From 26ac0c48f0a05c696e804e321c4bcde364127061 Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 20 Jan 2022 21:33:37 +0000 Subject: [PATCH] Add: [Script] ObjectType::ResolveNewGRFID to resolve object id from grfid and grf_local_id --- src/script/api/ai_changelog.hpp | 2 ++ src/script/api/game_changelog.hpp | 2 ++ src/script/api/script_objecttype.cpp | 8 ++++++++ src/script/api/script_objecttype.hpp | 9 +++++++++ 4 files changed, 21 insertions(+) diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp index 2f99ac864f..dc4af49213 100644 --- a/src/script/api/ai_changelog.hpp +++ b/src/script/api/ai_changelog.hpp @@ -17,6 +17,8 @@ * * This version is not yet released. The following changes are not set in stone yet. * + * \li AIObjectType::ResolveNewGRFID + * * \b 12.0 * * API additions: diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp index 8041e779d4..e99ac2f1f5 100644 --- a/src/script/api/game_changelog.hpp +++ b/src/script/api/game_changelog.hpp @@ -17,6 +17,8 @@ * * This version is not yet released. The following changes are not set in stone yet. * + * \li GSObjectType::ResolveNewGRFID + * * \b 12.0 * * API additions: diff --git a/src/script/api/script_objecttype.cpp b/src/script/api/script_objecttype.cpp index cd97761de2..50af8705cd 100644 --- a/src/script/api/script_objecttype.cpp +++ b/src/script/api/script_objecttype.cpp @@ -44,3 +44,11 @@ return ScriptObject::Command::Do(tile, object_type, view); } + +/* static */ ObjectType ScriptObjectType::ResolveNewGRFID(uint32 grfid, uint16 grf_local_id) +{ + EnforcePrecondition(INVALID_OBJECT_TYPE, IsInsideBS(grf_local_id, 0x00, NUM_OBJECTS_PER_GRF)); + + grfid = BSWAP32(grfid); // Match people's expectations. + return _object_mngr.GetID(grf_local_id, grfid); +} diff --git a/src/script/api/script_objecttype.hpp b/src/script/api/script_objecttype.hpp index d3f5d4a51c..94da70c467 100644 --- a/src/script/api/script_objecttype.hpp +++ b/src/script/api/script_objecttype.hpp @@ -52,6 +52,15 @@ public: * @return True if the object was successfully build. */ static bool BuildObject(ObjectType object_type, uint8 view, TileIndex tile); + + /** + * Get a specific object-type from a grf. + * @param grf_id The ID of the NewGRF. + * @param grf_local_id The ID of the object, local to the NewGRF. + * @pre 0x00 <= grf_local_id < NUM_OBJECTS_PER_GRF. + * @return the object-type ID, local to the current game (this diverges from the grf_local_id). + */ + static ObjectType ResolveNewGRFID(uint32 grfid, uint16 grf_local_id); }; #endif /* SCRIPT_OBJECTTYPE_HPP */