summaryrefslogtreecommitdiff
path: root/src/script/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/script/api')
-rw-r--r--src/script/api/game/game_goal.hpp.sq36
-rw-r--r--src/script/api/script_goal.cpp51
-rw-r--r--src/script/api/script_goal.hpp73
-rw-r--r--src/script/api/script_object.cpp12
-rw-r--r--src/script/api/script_object.hpp11
-rw-r--r--src/script/api/script_types.hpp5
-rw-r--r--src/script/api/template/template_goal.hpp.sq27
7 files changed, 215 insertions, 0 deletions
diff --git a/src/script/api/game/game_goal.hpp.sq b/src/script/api/game/game_goal.hpp.sq
new file mode 100644
index 000000000..54b1c6aeb
--- /dev/null
+++ b/src/script/api/game/game_goal.hpp.sq
@@ -0,0 +1,36 @@
+/* $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_goal.hpp"
+#include "../template/template_goal.hpp.sq"
+
+
+template <> const char *GetClassName<ScriptGoal, ST_GS>() { return "GSGoal"; }
+
+void SQGSGoal_Register(Squirrel *engine)
+{
+ DefSQClass<ScriptGoal, ST_GS> SQGSGoal("GSGoal");
+ SQGSGoal.PreRegister(engine);
+ SQGSGoal.AddConstructor<void (ScriptGoal::*)(), 1>(engine, "x");
+
+ SQGSGoal.DefSQConst(engine, ScriptGoal::GOAL_INVALID, "GOAL_INVALID");
+ SQGSGoal.DefSQConst(engine, ScriptGoal::GT_NONE, "GT_NONE");
+ SQGSGoal.DefSQConst(engine, ScriptGoal::GT_TILE, "GT_TILE");
+ SQGSGoal.DefSQConst(engine, ScriptGoal::GT_INDUSTRY, "GT_INDUSTRY");
+ SQGSGoal.DefSQConst(engine, ScriptGoal::GT_TOWN, "GT_TOWN");
+ SQGSGoal.DefSQConst(engine, ScriptGoal::GT_COMPANY, "GT_COMPANY");
+
+ SQGSGoal.DefSQStaticMethod(engine, &ScriptGoal::IsValidGoal, "IsValidGoal", 2, ".i");
+ SQGSGoal.DefSQStaticMethod(engine, &ScriptGoal::New, "New", 5, ".i.ii");
+ SQGSGoal.DefSQStaticMethod(engine, &ScriptGoal::Remove, "Remove", 2, ".i");
+
+ SQGSGoal.PostRegister(engine);
+}
diff --git a/src/script/api/script_goal.cpp b/src/script/api/script_goal.cpp
new file mode 100644
index 000000000..85aaeedb6
--- /dev/null
+++ b/src/script/api/script_goal.cpp
@@ -0,0 +1,51 @@
+/* $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_goal.cpp Implementation of ScriptGoal. */
+
+#include "../../stdafx.h"
+#include "script_goal.hpp"
+#include "script_error.hpp"
+#include "script_industry.hpp"
+#include "script_map.hpp"
+#include "script_town.hpp"
+#include "../script_instance.hpp"
+#include "../../command_type.h"
+#include "../../settings_type.h"
+#include "../../openttd.h"
+#include "../../network/network.h"
+#include "../../goal_base.h"
+#include "../../string_func.h"
+
+/* static */ bool ScriptGoal::IsValidGoal(GoalID goal_id)
+{
+ return ::Goal::IsValidID(goal_id);
+}
+
+/* static */ ScriptGoal::GoalID ScriptGoal::New(ScriptCompany::CompanyID company, const char *goal, GoalType type, uint32 destination)
+{
+ EnforcePrecondition(GOAL_INVALID, !StrEmpty(goal));
+ EnforcePrecondition(GOAL_INVALID, company == ScriptCompany::COMPANY_INVALID || ScriptCompany::ResolveCompanyID(company) != ScriptCompany::COMPANY_INVALID);
+ EnforcePrecondition(GOAL_INVALID, (type == GT_NONE && destination == 0) || (type == GT_TILE && ScriptMap::IsValidTile(destination)) || (type == GT_INDUSTRY && ScriptIndustry::IsValidIndustry(destination)) || (type == GT_TOWN && ScriptTown::IsValidTown(destination)) || (type == GT_COMPANY && ScriptCompany::ResolveCompanyID((ScriptCompany::CompanyID)destination) != ScriptCompany::COMPANY_INVALID));
+
+ uint8 c = company;
+ if (company == ScriptCompany::COMPANY_INVALID) c = INVALID_COMPANY;
+
+ if (!ScriptObject::DoCommand(0, type | (c << 8), destination, CMD_CREATE_GOAL, goal, &ScriptInstance::DoCommandReturnGoalID)) return GOAL_INVALID;
+
+ /* In case of test-mode, we return GoalID 0 */
+ return (ScriptGoal::GoalID)0;
+}
+
+/* static */ bool ScriptGoal::Remove(GoalID goal_id)
+{
+ EnforcePrecondition(false, IsValidGoal(goal_id));
+
+ return ScriptObject::DoCommand(0, goal_id, 0, CMD_REMOVE_GOAL);
+}
diff --git a/src/script/api/script_goal.hpp b/src/script/api/script_goal.hpp
new file mode 100644
index 000000000..ac7d8f449
--- /dev/null
+++ b/src/script/api/script_goal.hpp
@@ -0,0 +1,73 @@
+/* $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_goal.hpp Everything to manipulate the current running goal. */
+
+#ifndef SCRIPT_GOAL_HPP
+#define SCRIPT_GOAL_HPP
+
+#include "script_object.hpp"
+#include "script_company.hpp"
+#include "../../goal_type.h"
+
+/**
+ * Class that handles some goal related functions.
+ * @api game
+ */
+class ScriptGoal : public ScriptObject {
+public:
+ /**
+ * The goal IDs.
+ */
+ enum GoalID {
+ /* Note: these values represent part of the in-game GoalID enum */
+ GOAL_INVALID = ::INVALID_GOALTYPE, ///< An invalid goal id.
+ };
+
+ /**
+ * Goal types that can be given to a goal.
+ */
+ enum GoalType {
+ /* Note: these values represent part of the in-game GoalType enum */
+ GT_NONE = ::GT_NONE, ///< Destination is not linked.
+ GT_TILE = ::GT_TILE, ///< Destination is a tile.
+ GT_INDUSTRY = ::GT_INDUSTRY, ///< Destination is an industry.
+ GT_TOWN = ::GT_TOWN, ///< Destination is a town.
+ GT_COMPANY = ::GT_COMPANY, ///< Destination is a company.
+ };
+
+ /**
+ * Check whether this is a valid goalID.
+ * @param goal_id The GoalID to check.
+ * @return True if and only if this goal is valid.
+ */
+ static bool IsValidGoal(GoalID goal_id);
+
+ /**
+ * Create a new goal.
+ * @param company The company to create the goal for, or ScriptCompany::COMPANY_INVALID for all.
+ * @param goal The goal to add to the GUI.
+ * @param type The type of the goal.
+ * @param destination The destination of the #type type.
+ * @return The new GoalID, or GOAL_INVALID if it failed.
+ * @pre goal != NULL.
+ * @pre company == COMPANY_INVALID || ResolveCompanyID(company) != COMPANY_INVALID.
+ */
+ static GoalID New(ScriptCompany::CompanyID company, const char *goal, GoalType type, uint32 destination);
+
+ /**
+ * Remove a goal from the list.
+ * @param goal_id The goal to remove.
+ * @return True if the action succeeded.
+ * @pre IsValidGoal(goal_id).
+ */
+ static bool Remove(GoalID goal_id);
+};
+
+#endif /* SCRIPT_GOAL_HPP */
diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp
index d9a432b44..72185c786 100644
--- a/src/script/api/script_object.cpp
+++ b/src/script/api/script_object.cpp
@@ -16,6 +16,7 @@
#include "../../network/network.h"
#include "../../tunnelbridge.h"
#include "../../genworld.h"
+#include "../../goal_type.h"
#include "../script_storage.hpp"
#include "../script_instance.hpp"
@@ -142,6 +143,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
SetNewVehicleID(_new_vehicle_id);
SetNewSignID(_new_sign_id);
SetNewGroupID(_new_group_id);
+ SetNewGoalID(_new_goal_id);
}
/* static */ bool ScriptObject::GetLastCommandRes()
@@ -179,6 +181,16 @@ ScriptObject::ActiveInstance::~ActiveInstance()
return GetStorage()->new_group_id;
}
+/* static */ void ScriptObject::SetNewGoalID(GoalID goal_id)
+{
+ GetStorage()->new_goal_id = goal_id;
+}
+
+/* static */ GroupID ScriptObject::GetNewGoalID()
+{
+ return GetStorage()->new_goal_id;
+}
+
/* static */ void ScriptObject::SetAllowDoCommand(bool allow)
{
GetStorage()->allow_do_command = allow;
diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp
index d12f006ee..86bdbf8df 100644
--- a/src/script/api/script_object.hpp
+++ b/src/script/api/script_object.hpp
@@ -161,6 +161,11 @@ protected:
static GroupID GetNewGroupID();
/**
+ * Get the latest stored new_goal_id.
+ */
+ static GoalID GetNewGoalID();
+
+ /**
* Store a allow_do_command per company.
* @param allow The new allow.
*/
@@ -228,6 +233,12 @@ private:
* @param group_id The new GroupID.
*/
static void SetNewGroupID(GroupID group_id);
+
+ /**
+ * Store a new_goal_id per company.
+ * @param goal_id The new GoalID.
+ */
+ static void SetNewGoalID(GoalID goal_id);
};
#endif /* SCRIPT_OBJECT_HPP */
diff --git a/src/script/api/script_types.hpp b/src/script/api/script_types.hpp
index 1cce06571..2b763f4f5 100644
--- a/src/script/api/script_types.hpp
+++ b/src/script/api/script_types.hpp
@@ -32,6 +32,10 @@
* <td> introduction, preview \ref dynamic_engines "(2)" </td>
* <td> engines retires \ref dynamic_engines "(2)" </td>
* <td> no \ref dynamic_engines "(2)" </td></tr>
+ * <tr><td>#GoalID </td><td> goal </td>
+ * <td> creation </td>
+ * <td> deletion </td>
+ * <td> yes </td></tr>
* <tr><td>#GroupID </td><td> vehicle group </td>
* <td> creation </td>
* <td> deletion </td>
@@ -87,6 +91,7 @@ typedef uint BridgeType; ///< Internal name, not of any use for you.
typedef byte CargoID; ///< The ID of a cargo.
class CommandCost; ///< The cost of a command.
typedef uint16 EngineID; ///< The ID of an engine.
+typedef uint16 GoalID; ///< The ID of a goal.
typedef uint16 GroupID; ///< The ID of a group.
typedef uint16 IndustryID; ///< The ID of an industry.
typedef uint8 IndustryType; ///< The ID of an industry-type.
diff --git a/src/script/api/template/template_goal.hpp.sq b/src/script/api/template/template_goal.hpp.sq
new file mode 100644
index 000000000..012864324
--- /dev/null
+++ b/src/script/api/template/template_goal.hpp.sq
@@ -0,0 +1,27 @@
+/* $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_goal.hpp"
+
+namespace SQConvert {
+ /* Allow enums to be used as Squirrel parameters */
+ template <> inline ScriptGoal::GoalID GetParam(ForceType<ScriptGoal::GoalID>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (ScriptGoal::GoalID)tmp; }
+ template <> inline int Return<ScriptGoal::GoalID>(HSQUIRRELVM vm, ScriptGoal::GoalID res) { sq_pushinteger(vm, (int32)res); return 1; }
+ template <> inline ScriptGoal::GoalType GetParam(ForceType<ScriptGoal::GoalType>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (ScriptGoal::GoalType)tmp; }
+ template <> inline int Return<ScriptGoal::GoalType>(HSQUIRRELVM vm, ScriptGoal::GoalType res) { sq_pushinteger(vm, (int32)res); return 1; }
+
+ /* Allow ScriptGoal to be used as Squirrel parameter */
+ template <> inline ScriptGoal *GetParam(ForceType<ScriptGoal *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptGoal *)instance; }
+ template <> inline ScriptGoal &GetParam(ForceType<ScriptGoal &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptGoal *)instance; }
+ template <> inline const ScriptGoal *GetParam(ForceType<const ScriptGoal *>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (ScriptGoal *)instance; }
+ template <> inline const ScriptGoal &GetParam(ForceType<const ScriptGoal &>, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(ScriptGoal *)instance; }
+ template <> inline int Return<ScriptGoal *>(HSQUIRRELVM vm, ScriptGoal *res) { if (res == NULL) { sq_pushnull(vm); return 1; } res->AddRef(); Squirrel::CreateClassInstanceVM(vm, "Goal", res, NULL, DefSQDestructorCallback<ScriptGoal>, true); return 1; }
+} // namespace SQConvert