changeset 18769:7af05225a1dc draft

(svn r23617) -Add: ScriptTown::ExpandTown, to grow a town (GameScript only)
author truebrain <truebrain@openttd.org>
date Mon, 19 Dec 2011 20:57:51 +0000
parents c5391e1c3dcd
children ebb9a25b6598
files src/command.cpp src/script/api/game/game_town.hpp.sq src/script/api/script_town.cpp src/script/api/script_town.hpp src/town_cmd.cpp
diffstat 5 files changed, 38 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -265,7 +265,7 @@
 	DEF_CMD(CmdFoundTown,                            CMD_NO_TEST, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_FOUND_TOWN; founding random town can fail only in exec run
 	DEF_CMD(CmdRenameTown,                            CMD_SERVER, CMDT_OTHER_MANAGEMENT      ), // CMD_RENAME_TOWN
 	DEF_CMD(CmdDoTownAction,                                   0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
-	DEF_CMD(CmdExpandTown,                           CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
+	DEF_CMD(CmdExpandTown,                             CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
 	DEF_CMD(CmdDeleteTown,                           CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
 
 	DEF_CMD(CmdOrderRefit,                                     0, CMDT_ROUTE_MANAGEMENT      ), // CMD_ORDER_REFIT
--- a/src/script/api/game/game_town.hpp.sq
+++ b/src/script/api/game/game_town.hpp.sq
@@ -55,6 +55,7 @@
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::IsCity,                            "IsCity",                            2, ".i");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetRoadReworkDuration,             "GetRoadReworkDuration",             2, ".i");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetExclusiveRightsDuration,        "GetExclusiveRightsDuration",        2, ".i");
+	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::ExpandTown,                        "ExpandTown",                        3, ".ii");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetRating,                         "GetRating",                         3, ".ii");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetAllowedNoise,                   "GetAllowedNoise",                   2, ".i");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetRoadLayout,                     "GetRoadLayout",                     2, ".i");
--- a/src/script/api/script_town.cpp
+++ b/src/script/api/script_town.cpp
@@ -200,6 +200,14 @@
 	return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, town_action, CMD_DO_TOWN_ACTION);
 }
 
+/* static */ bool ScriptTown::ExpandTown(TownID town_id, int houses)
+{
+	EnforcePrecondition(false, IsValidTown(town_id));
+	EnforcePrecondition(false, houses > 0);
+
+	return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, houses, CMD_EXPAND_TOWN);
+}
+
 /* static */ ScriptTown::TownRating ScriptTown::GetRating(TownID town_id, ScriptCompany::CompanyID company_id)
 {
 	if (!IsValidTown(town_id)) return TOWN_RATING_INVALID;
--- a/src/script/api/script_town.hpp
+++ b/src/script/api/script_town.hpp
@@ -313,6 +313,17 @@
 	static bool PerformTownAction(TownID town_id, TownAction town_action);
 
 	/**
+	 * Expand the town.
+	 * @param town_id The town to expand.
+	 * @param houses The amount of houses to grow the town with.
+	 * @pre IsValidTown(town_id).
+	 * @pre houses > 0.
+	 * @return True if the action succeeded.
+	 * @api -ai
+	 */
+	static bool ExpandTown(TownID town_id, int houses);
+
+	/**
 	 * Get the rating of a company within a town.
 	 * @param town_id The town to get the rating for.
 	 * @param company_id The company to get the rating for.
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -2439,26 +2439,33 @@
  * @param tile Unused.
  * @param flags Type of operation.
  * @param p1 Town ID to expand.
- * @param p2 Unused.
+ * @param p2 Amount to grow, or 0 to grow a random size up to the current amount of houses.
  * @param text Unused.
  * @return Empty cost or an error.
  */
 CommandCost CmdExpandTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
-	if (_game_mode != GM_EDITOR) return CMD_ERROR;
+	if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY) return CMD_ERROR;
 	Town *t = Town::GetIfValid(p1);
 	if (t == NULL) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
 		/* The more houses, the faster we grow */
-		uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3;
-		t->num_houses += amount;
-		UpdateTownRadius(t);
-
-		uint n = amount * 10;
-		do GrowTown(t); while (--n);
-
-		t->num_houses -= amount;
+		if (p2 == 0) {
+			uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3;
+			t->num_houses += amount;
+			UpdateTownRadius(t);
+
+			uint n = amount * 10;
+			do GrowTown(t); while (--n);
+
+			t->num_houses -= amount;
+		} else {
+			for (; p2 > 0; p2--) {
+				/* Try several times to grow, as we are really suppose to grow */
+				for (uint i = 0; i < 25; i++) if (GrowTown(t)) break;
+			}
+		}
 		UpdateTownRadius(t);
 
 		UpdateTownMaxPass(t);