summaryrefslogtreecommitdiff
path: root/src/script/api
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2012-07-15 17:11:14 +0000
committeralberth <alberth@openttd.org>2012-07-15 17:11:14 +0000
commiteb56e5c0b185eb33cbf62c1344f90500badae354 (patch)
treedff20fb8ea6342a6e6cb563071470a21c90bf4d0 /src/script/api
parent05aaf18d38044eb52ea046c189469f4af440dc4b (diff)
downloadopenttd-eb56e5c0b185eb33cbf62c1344f90500badae354.tar.xz
(svn r24406) -Feature: Allow game scripts to monitor cargo pickups and deliveries done by companies.
Diffstat (limited to 'src/script/api')
-rw-r--r--src/script/api/game/game_cargomonitor.hpp.sq31
-rw-r--r--src/script/api/game_changelog.hpp1
-rw-r--r--src/script/api/script_cargomonitor.cpp44
-rw-r--r--src/script/api/script_cargomonitor.hpp92
-rw-r--r--src/script/api/template/template_cargomonitor.hpp.sq21
5 files changed, 189 insertions, 0 deletions
diff --git a/src/script/api/game/game_cargomonitor.hpp.sq b/src/script/api/game/game_cargomonitor.hpp.sq
new file mode 100644
index 000000000..b3638c4de
--- /dev/null
+++ b/src/script/api/game/game_cargomonitor.hpp.sq
@@ -0,0 +1,31 @@
+/* $Id$ */
+
+/*
+ * 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/>.
+ */
+
+/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */
+
+#include "../script_cargomonitor.hpp"
+#include "../template/template_cargomonitor.hpp.sq"
+
+
+template <> const char *GetClassName<ScriptCargoMonitor, ST_GS>() { return "GSCargoMonitor"; }
+
+void SQGSCargoMonitor_Register(Squirrel *engine)
+{
+ DefSQClass<ScriptCargoMonitor, ST_GS> SQGSCargoMonitor("GSCargoMonitor");
+ SQGSCargoMonitor.PreRegister(engine);
+ SQGSCargoMonitor.AddConstructor<void (ScriptCargoMonitor::*)(), 1>(engine, "x");
+
+ SQGSCargoMonitor.DefSQStaticMethod(engine, &ScriptCargoMonitor::GetTownDeliveryAmount, "GetTownDeliveryAmount", 5, ".iiib");
+ SQGSCargoMonitor.DefSQStaticMethod(engine, &ScriptCargoMonitor::GetIndustryDeliveryAmount, "GetIndustryDeliveryAmount", 5, ".iiib");
+ SQGSCargoMonitor.DefSQStaticMethod(engine, &ScriptCargoMonitor::GetTownPickupAmount, "GetTownPickupAmount", 5, ".iiib");
+ SQGSCargoMonitor.DefSQStaticMethod(engine, &ScriptCargoMonitor::GetIndustryPickupAmount, "GetIndustryPickupAmount", 5, ".iiib");
+ SQGSCargoMonitor.DefSQStaticMethod(engine, &ScriptCargoMonitor::StopAllMonitoring, "StopAllMonitoring", 1, ".");
+
+ SQGSCargoMonitor.PostRegister(engine);
+}
diff --git a/src/script/api/game_changelog.hpp b/src/script/api/game_changelog.hpp
index 60ac30d2f..a3a2780a5 100644
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -20,6 +20,7 @@
* 1.3.0 is not yet released. The following changes are not set in stone yet.
*
* API additions:
+ * \li GSCargoMonitor
* \li GSEventExclusiveTransportRights
* \li GSEventRoadReconstruction
* \li GSNews::NT_ACCIDENT, GSNews::NT_COMPANY_INFO, GSNews::NT_ADVICE, GSNews::NT_ACCEPTANCE
diff --git a/src/script/api/script_cargomonitor.cpp b/src/script/api/script_cargomonitor.cpp
new file mode 100644
index 000000000..e19821bba
--- /dev/null
+++ b/src/script/api/script_cargomonitor.cpp
@@ -0,0 +1,44 @@
+/* $Id$ */
+
+/*
+ * 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_cargomonitor.cpp Code to monitor cargo pickup and deliveries by companies. */
+
+#include "../../stdafx.h"
+#include "script_cargomonitor.hpp"
+
+/* static */ uint32 ScriptCargoMonitor::GetTownDeliveryAmount(ScriptCompany::CompanyID company, CargoID cargo, TownID town_id, bool keep_monitoring)
+{
+ CargoMonitorID monitor = EncodeCargoTownMonitor(static_cast<CompanyID>(company), cargo, town_id);
+ return GetDeliveryAmount(monitor, keep_monitoring);
+}
+
+/* static */ uint32 ScriptCargoMonitor::GetIndustryDeliveryAmount(ScriptCompany::CompanyID company, CargoID cargo, IndustryID industry_id, bool keep_monitoring)
+{
+ CargoMonitorID monitor = EncodeCargoIndustryMonitor(static_cast<CompanyID>(company), cargo, industry_id);
+ return GetDeliveryAmount(monitor, keep_monitoring);
+}
+
+/* static */ uint32 ScriptCargoMonitor::GetTownPickupAmount(ScriptCompany::CompanyID company, CargoID cargo, TownID town_id, bool keep_monitoring)
+{
+ CargoMonitorID monitor = EncodeCargoTownMonitor(static_cast<CompanyID>(company), cargo, town_id);
+ return GetPickupAmount(monitor, keep_monitoring);
+}
+
+/* static */ uint32 ScriptCargoMonitor::GetIndustryPickupAmount(ScriptCompany::CompanyID company, CargoID cargo, IndustryID industry_id, bool keep_monitoring)
+{
+ CargoMonitorID monitor = EncodeCargoIndustryMonitor(static_cast<CompanyID>(company), cargo, industry_id);
+ return GetPickupAmount(monitor, keep_monitoring);
+}
+
+/* static */ void ScriptCargoMonitor::StopAllMonitoring()
+{
+ ClearCargoPickupMonitoring();
+ ClearCargoDeliveryMonitoring();
+}
+
diff --git a/src/script/api/script_cargomonitor.hpp b/src/script/api/script_cargomonitor.hpp
new file mode 100644
index 000000000..aa06a8f4a
--- /dev/null
+++ b/src/script/api/script_cargomonitor.hpp
@@ -0,0 +1,92 @@
+/* $Id$ */
+
+/*
+ * 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_cargomonitor.hpp Everything to monitor cargo pickup and deliveries by companies. */
+
+#ifndef SCRIPT_CARGO_MONITOR_HPP
+#define SCRIPT_CARGO_MONITOR_HPP
+
+#include "script_list.hpp"
+#include "script_object.hpp"
+#include "script_company.hpp"
+#include "../../cargomonitor.h"
+
+/**
+ * Class that handles all cargo movement monitoring related functions.
+ *
+ * To get an understanding of what users are transporting, this class provides cargo pick-up and delivery monitoring functions.
+ * It works as follows:
+ * - Select a company, a cargo-type, and an industry that gets the cargo triplet.
+ * - Perform a call to #GetIndustryDeliveryAmount, setting 'keep_monitoring' to \c true.
+ * The return value is not important, but from this moment the program accumulates all deliveries by
+ * the given company to the given industry of the given cargo type.
+ * - Some time later, perform another call to #GetIndustryDeliveryAmount. It returns the accumulated
+ * amount of cargo that the company has delivered.
+ * The call causes the collected amount to be reset. On the next call you will thus always get the
+ * delivered amount since the previous call.
+ * - When monitoring the deliveries is not interesting any more, set 'keep_monitoring' to \c false.
+ * The collecting process that happens between calls is stopped.
+ *
+ * In the same way you can monitor town deliveries, and you can monitor pick-up from towns and industries.
+ * The latter get added at the moment the cargo is delivered. This prevents users from getting credit for
+ * picking up cargo without delivering it.
+ *
+ * The active monitors are saved and loaded. You can reset to the empty state with #StopAllMonitoring.
+ *
+ * @api game
+ */
+class ScriptCargoMonitor : public ScriptObject {
+public:
+ /**
+ * Get the amount of cargo delivered to a town by a company since the last query, and update the monitoring state.
+ * @param company %Company to query.
+ * @param cargo Cargo type to query.
+ * @param town_id %Town to query.
+ * @param keep_monitoring If \c true, the given combination continues to be monitored for the next call. If \c false, monitoring ends.
+ * @return Amount of delivered cargo of the given cargo type to the given town by the given company since the last call.
+ */
+ static uint32 GetTownDeliveryAmount(ScriptCompany::CompanyID company, CargoID cargo, TownID town_id, bool keep_monitoring);
+
+ /**
+ * Get the amount of cargo delivered to an industry by a company since the last query, and update the monitoring state.
+ * @param company %Company to query.
+ * @param cargo Cargo type to query.
+ * @param industry_id %Industry to query.
+ * @param keep_monitoring If \c true, the given combination continues to be monitored for the next call. If \c false, monitoring ends.
+ * @return Amount of delivered cargo of the given cargo type to the given industry by the given company since the last call.
+ */
+ static uint32 GetIndustryDeliveryAmount(ScriptCompany::CompanyID company, CargoID cargo, IndustryID industry_id, bool keep_monitoring);
+
+ /**
+ * Get the amount of cargo picked up (and delivered) from a town by a company since the last query, and update the monitoring state.
+ * @param company %Company to query.
+ * @param cargo Cargo type to query.
+ * @param town_id %Town to query.
+ * @param keep_monitoring If \c true, the given combination continues to be monitored for the next call. If \c false, monitoring ends.
+ * @return Amount of picked up cargo of the given cargo type to the given town by the given company since the last call.
+ * @note Amounts of picked-up cargo are added during final delivery of it, to prevent users from getting credit for picking up without delivering it.
+ */
+ static uint32 GetTownPickupAmount(ScriptCompany::CompanyID company, CargoID cargo, TownID town_id, bool keep_monitoring);
+
+ /**
+ * Get the amount of cargo picked up (and delivered) from an industry by a company since the last query, and update the monitoring state.
+ * @param company %Company to query.
+ * @param cargo Cargo type to query.
+ * @param industry_id %Industry to query.
+ * @param keep_monitoring If \c true, the given combination continues to be monitored for the next call. If \c false, monitoring ends.
+ * @return Amount of picked up cargo of the given cargo type to the given industry by the given company since the last call.
+ * @note Amounts of picked-up cargo are added during final delivery of it, to prevent users from getting credit for picking up without delivering it.
+ */
+ static uint32 GetIndustryPickupAmount(ScriptCompany::CompanyID company, CargoID cargo, IndustryID industry_id, bool keep_monitoring);
+
+ /** Stop monitoring everything. */
+ static void StopAllMonitoring();
+};
+
+#endif /* SCRIPT_CARGO_MONITOR_HPP */
diff --git a/src/script/api/template/template_cargomonitor.hpp.sq b/src/script/api/template/template_cargomonitor.hpp.sq
new file mode 100644
index 000000000..ce0a358ac
--- /dev/null
+++ b/src/script/api/template/template_cargomonitor.hpp.sq
@@ -0,0 +1,21 @@
+/* $Id$ */
+
+/*
+ * 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/>.
+ */
+
+/* THIS FILE IS AUTO-GENERATED; PLEASE DO NOT ALTER MANUALLY */
+
+#include "../script_cargomonitor.hpp"
+
+namespace SQConvert {
+ /* Allow ScriptCargoMonitor to be used as Squirrel parameter */
+ template <> inline ScriptCargoMonitor *GetParam(ForceType<ScriptCargoMonitor *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptCargoMonitor *)instance; }
+ template <> inline ScriptCargoMonitor &GetParam(ForceType<ScriptCargoMonitor &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptCargoMonitor *)instance; }
+ template <> inline const ScriptCargoMonitor *GetParam(ForceType<const ScriptCargoMonitor *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptCargoMonitor *)instance; }
+ template <> inline const ScriptCargoMonitor &GetParam(ForceType<const ScriptCargoMonitor &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptCargoMonitor *)instance; }
+ template <> inline int Return<ScriptCargoMonitor *>(HSQUIRRELVM vm, ScriptCargoMonitor *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "CargoMonitor", res, NULL, DefSQDestructorCallback<ScriptCargoMonitor>, true); return 1; }
+} // namespace SQConvert