/**
* Build/Fund an industry
* @param tile tile where industry is built
* @param flags of operations to conduct
* @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
*/
CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
{
IndustryType it = GB(p1, 0, 8);
if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
const IndustrySpec *indspec = GetIndustrySpec(it);
/* Check if the to-be built/founded industry is available for this climate. */
if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
/* 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 && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
return CMD_ERROR;
}
if (_game_mode != GM_EDITOR && GetIndustryProbabilityCallback(it, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) {
return CMD_ERROR;
}
Randomizer randomizer;
randomizer.SetSeed(p2);
uint16 random_initial_bits = GB(p2, 0, 16);
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 (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 not built on water are built as OWNER_TOWN to not e.g. be build on owned land of the founder */
Owner prospector = OWNER_TOWN;
if ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) && _current_company < MAX_COMPANIES) prospector = _current_company;
Backup<CompanyByte> cur_company(_current_company, prospector, 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 (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
*/
tile = RandomTile();
/* Start with a random layout */
int layout = RandomRange(num_layouts);
/* 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(), _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_PROSPECTCREATION, &ind);
if (ret.Succeeded()) break;
}
if (ret.Succeeded()) break;
}
}
cur_company.Restore();
}
} else {
int layout = GB(p1, 8, 8);
if (layout >= num_layouts) return CMD_ERROR;
/* 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, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, &ind);
if (ret.Succeeded()) break;
}
/* If it still failed, there's no suitable layout to build here, return the error */
if (ret.Failed()) return ret;
}
if ((flags & DC_EXEC) && ind != NULL && _game_mode != GM_EDITOR) {
AdvertiseIndustryOpening(ind);
}
return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
}