changeset 19702:27b284e39a4d draft

(svn r24623) -Feature: Allow GameScripts to construct and prospect industries without having a sponsor
author zuu <zuu@openttd.org>
date Mon, 22 Oct 2012 18:56:21 +0000
parents 075f7c783b28
children 369929081fbc
files src/command.cpp src/industry_cmd.cpp src/script/api/game_changelog.hpp src/script/api/script_industrytype.cpp src/script/api/script_industrytype.hpp
diffstat 5 files changed, 27 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -242,7 +242,7 @@
 
 	DEF_CMD(CmdChangeServiceInt,                               0, CMDT_VEHICLE_MANAGEMENT    ), // CMD_CHANGE_SERVICE_INT
 
-	DEF_CMD(CmdBuildIndustry,                                  0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
+	DEF_CMD(CmdBuildIndustry,                          CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_BUILD_INDUSTRY
 	DEF_CMD(CmdSetCompanyManagerFace,                          0, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_COMPANY_MANAGER_FACE
 	DEF_CMD(CmdSetCompanyColour,                               0, CMDT_OTHER_MANAGEMENT      ), // CMD_SET_COMPANY_COLOUR
 
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -1827,6 +1827,7 @@
  * @param p1 various bitstuffed elements
  * - p1 = (bit  0 -  7) - industry type see build_industry.h and see industry.h
  * - p1 = (bit  8 - 15) - first layout to try
+ * - p1 = (bit 16     ) - 0 = prospect, 1 = fund (only valid if current company is DEITY)
  * @param p2 seed to use for desyncfree randomisations
  * @param text unused
  * @return the cost of this operation or an error
@@ -1843,11 +1844,11 @@
 
 	/* If the setting for raw-material industries is not on, you cannot build raw-material industries.
 	 * Raw material industries are industries that do not accept cargo (at least for now) */
-	if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
+	if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
 		return CMD_ERROR;
 	}
 
-	if (_game_mode != GM_EDITOR && GetIndustryProbabilityCallback(it, IACT_USERCREATION, 1) == 0) {
+	if (_game_mode != GM_EDITOR && GetIndustryProbabilityCallback(it, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) {
 		return CMD_ERROR;
 	}
 
@@ -1857,16 +1858,17 @@
 	uint32 random_var8f = randomizer.Next();
 	int num_layouts = indspec->num_table;
 	CommandCost ret = CommandCost(STR_ERROR_SITE_UNSUITABLE);
+	const bool deity_prospect = _current_company == OWNER_DEITY && !HasBit(p1, 16);
 
 	Industry *ind = NULL;
-	if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
+	if (deity_prospect || (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry())) {
 		if (flags & DC_EXEC) {
 			/* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
 			Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
 			/* Prospecting has a chance to fail, however we cannot guarantee that something can
 			 * be built on the map, so the chance gets lower when the map is fuller, but there
 			 * is nothing we can really do about that. */
-			if (Random() <= indspec->prospecting_chance) {
+			if (deity_prospect || Random() <= indspec->prospecting_chance) {
 				for (int i = 0; i < 5000; i++) {
 					/* We should not have more than one Random() in a function call
 					 * because parameter evaluation order is not guaranteed in the c++ standard
@@ -1877,7 +1879,7 @@
 					/* Check now each layout, starting with the random one */
 					for (int j = 0; j < num_layouts; j++) {
 						layout = (layout + 1) % num_layouts;
-						ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, cur_company.GetOriginalValue(), IACT_PROSPECTCREATION, &ind);
+						ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, cur_company.GetOriginalValue(), _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_PROSPECTCREATION, &ind);
 						if (ret.Succeeded()) break;
 					}
 					if (ret.Succeeded()) break;
@@ -1892,7 +1894,7 @@
 		/* Check subsequently each layout, starting with the given layout in p1 */
 		for (int i = 0; i < num_layouts; i++) {
 			layout = (layout + 1) % num_layouts;
-			ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, _current_company, IACT_USERCREATION, &ind);
+			ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, _current_company, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, &ind);
 			if (ret.Succeeded()) break;
 		}
 
--- a/src/script/api/game_changelog.hpp
+++ b/src/script/api/game_changelog.hpp
@@ -29,6 +29,7 @@
  * \li GSStation::IsAirportClosed
  * \li GSStation::OpenCloseAirport
  * \li GSController::Break
+ * \li GSIndustryType::BuildIndustry, GSIndustryType::CanBuildIndustry, GSIndustryType::ProspectIndustry and GSIndustryType::CanProspectIndustry when outside GSCompanyMode scope
  *
  * \b 1.2.2
  *
--- a/src/script/api/script_industrytype.cpp
+++ b/src/script/api/script_industrytype.cpp
@@ -94,7 +94,9 @@
 {
 	if (!IsValidIndustryType(industry_type)) return false;
 
-	if (::GetIndustryProbabilityCallback(industry_type, IACT_USERCREATION, 1) == 0) return false;
+	const bool deity = ScriptObject::GetCompany() == OWNER_DEITY;
+	if (::GetIndustryProbabilityCallback(industry_type, deity ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) return false;
+	if (deity) return true;
 	if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return true;
 
 	/* raw_industry_construction == 1 means "Build as other industries" */
@@ -105,26 +107,25 @@
 {
 	if (!IsValidIndustryType(industry_type)) return false;
 
-	if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return false;
-	if (::GetIndustryProbabilityCallback(industry_type, IACT_USERCREATION, 1) == 0) return false;
+	const bool deity = ScriptObject::GetCompany() == OWNER_DEITY;
+	if (!deity && !::GetIndustrySpec(industry_type)->IsRawIndustry()) return false;
+	if (::GetIndustryProbabilityCallback(industry_type, deity ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) return false;
 
 	/* raw_industry_construction == 2 means "prospect" */
-	return _settings_game.construction.raw_industry_construction == 2;
+	return deity || _settings_game.construction.raw_industry_construction == 2;
 }
 
 /* static */ bool ScriptIndustryType::BuildIndustry(IndustryType industry_type, TileIndex tile)
 {
-	EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
 	EnforcePrecondition(false, CanBuildIndustry(industry_type));
 	EnforcePrecondition(false, ScriptMap::IsValidTile(tile));
 
 	uint32 seed = ::InteractiveRandom();
-	return ScriptObject::DoCommand(tile, (::InteractiveRandomRange(::GetIndustrySpec(industry_type)->num_table) << 8) | industry_type, seed, CMD_BUILD_INDUSTRY);
+	return ScriptObject::DoCommand(tile, (1 << 16) | (::InteractiveRandomRange(::GetIndustrySpec(industry_type)->num_table) << 8) | industry_type, seed, CMD_BUILD_INDUSTRY);
 }
 
 /* static */ bool ScriptIndustryType::ProspectIndustry(IndustryType industry_type)
 {
-	EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
 	EnforcePrecondition(false, CanProspectIndustry(industry_type));
 
 	uint32 seed = ::InteractiveRandom();
--- a/src/script/api/script_industrytype.hpp
+++ b/src/script/api/script_industrytype.hpp
@@ -113,6 +113,9 @@
 	 * @pre IsValidIndustryType(industry_type).
 	 * @return True if you can build this type of industry at locations of your choice.
 	 * @note Returns false if you can only prospect this type of industry, or not build it at all.
+	 * @game @note If no valid ScriptCompanyMode active in scope, the script can
+	 * @game build as long as the industry type can be built. (a NewGRF can for example
+	 * @game reject construction based on current year)
 	 */
 	static bool CanBuildIndustry(IndustryType industry_type);
 
@@ -123,6 +126,9 @@
 	 * @return True if you can prospect this type of industry.
 	 * @note If the setting "Manual primary industry construction method" is set
 	 * to either "None" or "as other industries" this function always returns false.
+	 * @game @note If no valid ScriptCompanyMode active in scope, the script can
+	 * @game prospect as long as the industry type can be built. (a NewGRF can for
+	 * @game example reject construction based on current year)
 	 */
 	static bool CanProspectIndustry(IndustryType industry_type);
 
@@ -131,7 +137,6 @@
 	 * @param industry_type The type of the industry to build.
 	 * @param tile The tile to build the industry on.
 	 * @pre CanBuildIndustry(industry_type).
-	 * @game @pre Valid ScriptCompanyMode active in scope.
 	 * @return True if the industry was successfully build.
 	 */
 	static bool BuildIndustry(IndustryType industry_type, TileIndex tile);
@@ -141,10 +146,12 @@
 	 * an industry on a random place on the map.
 	 * @param industry_type The type of the industry.
 	 * @pre CanProspectIndustry(industry_type).
-	 * @game @pre Valid ScriptCompanyMode active in scope.
 	 * @return True if no error occurred while trying to prospect.
 	 * @note Even if true is returned there is no guarantee a new industry is build.
 	 * @note If true is returned the money is paid, whether a new industry was build or not.
+	 * @game @note if no valid ScriptCompanyMode exist in scope, prospection will not fail
+	 * @game due to the general chance that prospection may fail. However prospection can still
+	 * @game fail if OpenTTD is unable to find a suitable location to place the industry.
 	 */
 	static bool ProspectIndustry(IndustryType industry_type);