summaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2021-08-09 23:34:50 +0200
committerGitHub <noreply@github.com>2021-08-09 23:34:50 +0200
commit8706dcd9c1ac8f695b7a0cb1a204fb6bcbaa492f (patch)
tree8797cfea8ea6f51850e799ae4539d289a4f7548f /src/script
parentd58772ecdadb67cbd571ab46eb1c822c421da8c0 (diff)
downloadopenttd-8706dcd9c1ac8f695b7a0cb1a204fb6bcbaa492f.tar.xz
Add: [Script] Basic information about loaded NewGRFs for scripts. (#9464)
Currently, scripts use various heuristics to detect loaded NewGRFs that are inherently unreliable. The list of loaded NewGRFs is easily accessible to a human player, and thus giving scripts the same information is consistent with the current approach to not give scripts more information than a human player.
Diffstat (limited to 'src/script')
-rw-r--r--src/script/api/CMakeLists.txt2
-rw-r--r--src/script/api/ai_changelog.hpp4
-rw-r--r--src/script/api/game_changelog.hpp4
-rw-r--r--src/script/api/script_newgrf.cpp58
-rw-r--r--src/script/api/script_newgrf.hpp56
5 files changed, 124 insertions, 0 deletions
diff --git a/src/script/api/CMakeLists.txt b/src/script/api/CMakeLists.txt
index c21707d43..a6390e2bb 100644
--- a/src/script/api/CMakeLists.txt
+++ b/src/script/api/CMakeLists.txt
@@ -179,6 +179,7 @@ add_files(
script_log.hpp
script_map.hpp
script_marine.hpp
+ script_newgrf.hpp
script_news.hpp
script_object.hpp
script_order.hpp
@@ -246,6 +247,7 @@ add_files(
script_log.cpp
script_map.cpp
script_marine.cpp
+ script_newgrf.cpp
script_news.cpp
script_object.cpp
script_order.cpp
diff --git a/src/script/api/ai_changelog.hpp b/src/script/api/ai_changelog.hpp
index f63b41fa9..15e171027 100644
--- a/src/script/api/ai_changelog.hpp
+++ b/src/script/api/ai_changelog.hpp
@@ -17,6 +17,10 @@
*
* This version is not yet released. The following changes are not set in stone yet.
*
+ * API additions:
+ * \li AINewGRF
+ * \li AINewGRFList
+ *
* \b 1.11.0
*
* API additions:
diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp
index 2702a98e7..5d214efb1 100644
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -17,6 +17,10 @@
*
* This version is not yet released. The following changes are not set in stone yet.
*
+ * API additions:
+ * \li GSNewGRF
+ * \li GSNewGRFList
+ *
* \b 1.11.0
*
* API additions:
diff --git a/src/script/api/script_newgrf.cpp b/src/script/api/script_newgrf.cpp
new file mode 100644
index 000000000..387093092
--- /dev/null
+++ b/src/script/api/script_newgrf.cpp
@@ -0,0 +1,58 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+/** @file script_newgrf.cpp Implementation of ScriptNewGRF and friends. */
+
+#include "../../stdafx.h"
+#include "script_newgrf.hpp"
+#include "../../core/bitmath_func.hpp"
+#include "../../newgrf_config.h"
+#include "../../string_func.h"
+
+#include "../../safeguards.h"
+
+ScriptNewGRFList::ScriptNewGRFList()
+{
+ for (auto c = _grfconfig; c != nullptr; c = c->next) {
+ if (!HasBit(c->flags, GCF_STATIC)) {
+ this->AddItem(c->ident.grfid);
+ }
+ }
+}
+
+/* static */ bool ScriptNewGRF::IsLoaded(uint32 grfid)
+{
+ for (auto c = _grfconfig; c != nullptr; c = c->next) {
+ if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+/* static */ uint32 ScriptNewGRF::GetVersion(uint32 grfid)
+{
+ for (auto c = _grfconfig; c != nullptr; c = c->next) {
+ if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
+ return c->version;
+ }
+ }
+
+ return 0;
+}
+
+/* static */ char *ScriptNewGRF::GetName(uint32 grfid)
+{
+ for (auto c = _grfconfig; c != nullptr; c = c->next) {
+ if (!HasBit(c->flags, GCF_STATIC) && c->ident.grfid == grfid) {
+ return ::stredup(c->GetName());
+ }
+ }
+
+ return nullptr;
+}
diff --git a/src/script/api/script_newgrf.hpp b/src/script/api/script_newgrf.hpp
new file mode 100644
index 000000000..73f870cc1
--- /dev/null
+++ b/src/script/api/script_newgrf.hpp
@@ -0,0 +1,56 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+/** @file script_newgrf.hpp NewGRF info for scripts. */
+
+#ifndef SCRIPT_NEWGRF_HPP
+#define SCRIPT_NEWGRF_HPP
+
+#include "script_list.hpp"
+
+/**
+ * Create a list of loaded NewGRFs.
+ * @api ai game
+ * @ingroup ScriptList
+ */
+class ScriptNewGRFList : public ScriptList {
+public:
+ ScriptNewGRFList();
+};
+
+
+/**
+ * Class that handles all NewGRF related functions.
+ * @api ai game
+ */
+class ScriptNewGRF : public ScriptObject {
+public:
+ /**
+ * Check if a NewGRF with a given grfid is loaded.
+ * @param grfid The grfid to check.
+ * @return True if and only if a NewGRF with the given grfid is loaded in the game.
+ */
+ static bool IsLoaded(uint32 grfid);
+
+ /**
+ * Get the version of a loaded NewGRF.
+ * @param grfid The NewGRF to query.
+ * @pre ScriptNewGRF::IsLoaded(grfid).
+ * @return Version of the NewGRF or 0 if the NewGRF specifies no version.
+ */
+ static uint32 GetVersion(uint32 grfid);
+
+ /**
+ * Get the BridgeID of a bridge at a given tile.
+ * @param grfid The NewGRF to query.
+ * @pre ScriptNewGRF::IsLoaded(grfid).
+ * @return The name of the NewGRF or nullptr if no name is defined.
+ */
+ static char *GetName(uint32 grfid);
+};
+
+#endif /* SCRIPT_NEWGRF_HPP */