changeset 18780:f5ccc1e2ae55 draft

(svn r23628) -Add: ScriptSubsidy::Create, to create subsidies (GameScript only)
author truebrain <truebrain@openttd.org>
date Mon, 19 Dec 2011 21:01:12 +0000
parents 967b174c9628
children ee6ce5148ede
files src/command.cpp src/command_type.h src/script/api/game/game_subsidy.hpp.sq src/script/api/script_subsidy.cpp src/script/api/script_subsidy.hpp src/subsidy.cpp
diffstat 6 files changed, 96 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -147,6 +147,7 @@
 CommandProc CmdBuildCanal;
 CommandProc CmdBuildLock;
 
+CommandProc CmdCreateSubsidy;
 CommandProc CmdCompanyCtrl;
 CommandProc CmdCustomNewsItem;
 
@@ -282,6 +283,7 @@
 
 	DEF_CMD(CmdMoneyCheat,                           CMD_OFFLINE, CMDT_CHEAT                 ), // CMD_MONEY_CHEAT
 	DEF_CMD(CmdBuildCanal,                              CMD_AUTO, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_CANAL
+	DEF_CMD(CmdCreateSubsidy,                          CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CREATE_SUBSIDY
 	DEF_CMD(CmdCompanyCtrl,        CMD_SPECTATOR | CMD_CLIENT_ID, CMDT_SERVER_SETTING        ), // CMD_COMPANY_CTRL
 	DEF_CMD(CmdCustomNewsItem,                         CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_CUSTOM_NEWS_ITEM
 
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -261,6 +261,7 @@
 	CMD_MONEY_CHEAT,                  ///< do the money cheat
 	CMD_BUILD_CANAL,                  ///< build a canal
 
+	CMD_CREATE_SUBSIDY,               ///< create a new subsidy
 	CMD_COMPANY_CTRL,                 ///< used in multiplayer to create a new companies etc.
 	CMD_CUSTOM_NEWS_ITEM,             ///< create a custom news message
 	CMD_LEVEL_LAND,                   ///< level land
--- a/src/script/api/game/game_subsidy.hpp.sq
+++ b/src/script/api/game/game_subsidy.hpp.sq
@@ -27,6 +27,7 @@
 
 	SQGSSubsidy.DefSQStaticMethod(engine, &ScriptSubsidy::IsValidSubsidy,      "IsValidSubsidy",      2, ".i");
 	SQGSSubsidy.DefSQStaticMethod(engine, &ScriptSubsidy::IsAwarded,           "IsAwarded",           2, ".i");
+	SQGSSubsidy.DefSQStaticMethod(engine, &ScriptSubsidy::Create,              "Create",              6, ".iiiii");
 	SQGSSubsidy.DefSQStaticMethod(engine, &ScriptSubsidy::GetAwardedTo,        "GetAwardedTo",        2, ".i");
 	SQGSSubsidy.DefSQStaticMethod(engine, &ScriptSubsidy::GetExpireDate,       "GetExpireDate",       2, ".i");
 	SQGSSubsidy.DefSQStaticMethod(engine, &ScriptSubsidy::GetCargoType,        "GetCargoType",        2, ".i");
--- a/src/script/api/script_subsidy.cpp
+++ b/src/script/api/script_subsidy.cpp
@@ -12,6 +12,9 @@
 #include "../../stdafx.h"
 #include "script_subsidy.hpp"
 #include "script_date.hpp"
+#include "script_industry.hpp"
+#include "script_town.hpp"
+#include "script_error.hpp"
 #include "../../subsidy_base.h"
 #include "../../station_base.h"
 
@@ -27,6 +30,17 @@
 	return ::Subsidy::Get(subsidy_id)->IsAwarded();
 }
 
+/* static */ bool ScriptSubsidy::Create(CargoID cargo_type, SubsidyParticipantType from_type, uint16 from_id, SubsidyParticipantType to_type, uint16 to_id)
+{
+	EnforcePrecondition(false, ScriptCargo::IsValidCargo(cargo_type));
+	EnforcePrecondition(false, from_type == SPT_INDUSTRY || from_type == SPT_TOWN);
+	EnforcePrecondition(false, to_type == SPT_INDUSTRY || to_type == SPT_TOWN);
+	EnforcePrecondition(false, (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(from_id)) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(from_id)));
+	EnforcePrecondition(false, (to_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(to_id)) || (to_type == SPT_TOWN && ScriptTown::IsValidTown(to_id)));
+
+	return ScriptObject::DoCommand(0, from_type | (from_id << 8) | (cargo_type << 24), to_type | (to_id << 8), CMD_CREATE_SUBSIDY);
+}
+
 /* static */ ScriptCompany::CompanyID ScriptSubsidy::GetAwardedTo(SubsidyID subsidy_id)
 {
 	if (!IsAwarded(subsidy_id)) return ScriptCompany::COMPANY_INVALID;
--- a/src/script/api/script_subsidy.hpp
+++ b/src/script/api/script_subsidy.hpp
@@ -25,6 +25,9 @@
 	 * @note The list of values may grow in future.
 	 */
 	enum SubsidyParticipantType {
+		/* Values are important, as they represent the internal state of the game.
+		 *  It is orignally named SourceType. ST_HEADQUARTERS is intentionally
+		 *  left out, as it cannot be used for Subsidies. */
 		SPT_INDUSTRY =    0, ///< Subsidy participant is an industry
 		SPT_TOWN     =    1, ///< Subsidy participant is a town
 		SPT_INVALID  = 0xFF, ///< Invalid/unknown participant type
@@ -46,6 +49,23 @@
 	static bool IsAwarded(SubsidyID subsidy_id);
 
 	/**
+	 * Create a new subsidy.
+	 * @param cargo_type The type of cargo to cary for the subsidy.
+	 * @param from_type The type of the subsidy on the 'from' side.
+	 * @param from_id The ID of the 'from' side.
+	 * @param to_type The type of the subsidy on the 'to' side.
+	 * @param to_id The ID of the 'to' side.
+	 * @return True if the action succeeded.
+	 * @pre ScriptCargo::IsValidCargo(cargo_type)
+	 * @pre from_type == SPT_INDUSTRY || from_type == SPT_TOWN.
+	 * @pre to_type   == SPT_INDUSTRY || to_type   == SPT_TOWN.
+	 * @pre (from_type == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(from_id)) || (from_type == SPT_TOWN && ScriptTown::IsValidTown(from_id))
+	 * @pre (to_type   == SPT_INDUSTRY && ScriptIndustry::IsValidIndustry(to_id))   || (to_type   == SPT_TOWN && ScriptTown::IsValidTown(to_id))
+	 * @api -ai
+	 */
+	static bool Create(CargoID cargo_type, SubsidyParticipantType from_type, uint16 from_id, SubsidyParticipantType to_type, uint16 to_id);
+
+	/**
 	 * Get the company index of the company this subsidy is awarded to.
 	 * @param subsidy_id The SubsidyID to check.
 	 * @pre IsAwarded(subsidy_id).
--- a/src/subsidy.cpp
+++ b/src/subsidy.cpp
@@ -24,6 +24,7 @@
 #include "core/pool_func.hpp"
 #include "core/random_func.hpp"
 #include "game/game.hpp"
+#include "command_func.h"
 
 #include "table/strings.h"
 
@@ -205,8 +206,65 @@
 	SetPartOfSubsidyFlag(s->dst_type, s->dst, POS_DST);
 	AI::BroadcastNewEvent(new ScriptEventSubsidyOffer(s->index));
 	Game::NewEvent(new ScriptEventSubsidyOffer(s->index));
+
+	InvalidateWindowData(WC_SUBSIDIES_LIST, 0);
 }
 
+/**
+ * Create a new subsidy.
+ * @param tile unused.
+ * @param flags type of operation
+ * @param p1 various bitstuffed elements
+ * - p1 = (bit  0 -  7) - SourceType of source.
+ * - p1 = (bit  8 - 23) - SourceID of source.
+ * - p1 = (bit 24 - 31) - CargoID of subsidy.
+ * @param p2 various bitstuffed elements
+ * - p2 = (bit  0 -  7) - SourceType of destination.
+ * - p2 = (bit  8 - 23) - SourceID of destionation.
+ * @param text unused.
+ * @return the cost of this operation or an error
+ */
+CommandCost CmdCreateSubsidy(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+	if (!Subsidy::CanAllocateItem()) return CMD_ERROR;
+
+	CargoID cid = GB(p1, 24, 8);
+	SourceType src_type = (SourceType)GB(p1, 0, 8);
+	SourceID src = GB(p1, 8, 16);
+	SourceType dst_type = (SourceType)GB(p2, 0, 8);
+	SourceID dst = GB(p2, 8, 16);
+
+	if (_current_company != OWNER_DEITY) return CMD_ERROR;
+
+	if (cid >= NUM_CARGO || !::CargoSpec::Get(cid)->IsValid()) return CMD_ERROR;
+
+	switch (src_type) {
+		case ST_TOWN:
+			if (!Town::IsValidID(src)) return CMD_ERROR;
+			break;
+		case ST_INDUSTRY:
+			if (!Industry::IsValidID(src)) return CMD_ERROR;
+			break;
+		default:
+			return CMD_ERROR;
+	}
+	switch (dst_type) {
+		case ST_TOWN:
+			if (!Town::IsValidID(dst)) return CMD_ERROR;
+			break;
+		case ST_INDUSTRY:
+			if (!Industry::IsValidID(dst)) return CMD_ERROR;
+			break;
+		default:
+			return CMD_ERROR;
+	}
+
+	if (flags & DC_EXEC) {
+		CreateSubsidy(cid, src_type, src, dst_type, dst);
+	}
+
+	return CommandCost();
+}
 
 /** Tries to create a passenger subsidy between two towns.
  * @return True iff the subsidy was created.