Changeset - r19282:4d6e684192fc
[Not reviewed]
master
0 1 0
frosch - 12 years ago 2012-04-28 16:07:28
frosch@openttd.org
(svn r24185) -Codechange: Deduplicate some only partly deduplicated code.
1 file changed with 20 insertions and 30 deletions:
0 comments (0 inline, 0 general)
src/industry_cmd.cpp
Show inline comments
 
@@ -1573,24 +1573,43 @@ static CommandCost CheckIfFarEnoughFromC
 

	
 
		/* check if there are any conflicting industry types around */
 
		if (i->type == indspec->conflicting[0] ||
 
				i->type == indspec->conflicting[1] ||
 
				i->type == indspec->conflicting[2]) {
 
			return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
 
		}
 
	}
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Advertise about a new industry opening.
 
 * @param ind Industry being opened.
 
 */
 
static void AdvertiseIndustryOpening(const Industry *ind)
 
{
 
	const IndustrySpec *ind_spc = GetIndustrySpec(ind->type);
 
	SetDParam(0, ind_spc->name);
 
	if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
 
		SetDParam(1, STR_TOWN_NAME);
 
		SetDParam(2, ind->town->index);
 
	} else {
 
		SetDParam(1, ind->town->index);
 
	}
 
	AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
 
	AI::BroadcastNewEvent(new ScriptEventIndustryOpen(ind->index));
 
	Game::NewEvent(new ScriptEventIndustryOpen(ind->index));
 
}
 

	
 
/**
 
 * Put an industry on the map.
 
 * @param i       Just allocated poolitem, mostly empty.
 
 * @param tile    North tile of the industry.
 
 * @param type    Type of the industry.
 
 * @param it      Industrylayout to build.
 
 * @param layout  Number of the layout.
 
 * @param t       Nearest town.
 
 * @param founder Founder of the industry; OWNER_NONE in case of random construction.
 
 * @param initial_random_bits Random bits for the industry.
 
 */
 
static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, const IndustryTileTable *it, byte layout, Town *t, Owner founder, uint16 initial_random_bits)
 
{
 
@@ -1858,35 +1877,25 @@ CommandCost CmdBuildIndustry(TileIndex t
 
		/* 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);
 
			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) {
 
		/* Created a new industry in-game, advertise the event. */
 
		SetDParam(0, indspec->name);
 
		if (indspec->new_industry_text > STR_LAST_STRINGID) {
 
			SetDParam(1, STR_TOWN_NAME);
 
			SetDParam(2, ind->town->index);
 
		} else {
 
			SetDParam(1, ind->town->index);
 
		}
 
		AddIndustryNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
 
		AI::BroadcastNewEvent(new ScriptEventIndustryOpen(ind->index));
 
		Game::NewEvent(new ScriptEventIndustryOpen(ind->index));
 
		AdvertiseIndustryOpening(ind);
 
	}
 

	
 
	return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
 
}
 

	
 

	
 
/**
 
 * Create a new industry of random layout.
 
 * @param tile The location to build the industry.
 
 * @param type The industry type to build.
 
 * @param creation_type The circumstances the industry is created under.
 
 * @return the created industry or NULL if it failed.
 
@@ -1967,43 +1976,24 @@ static uint GetNumberOfIndustries()
 
		10,   // very low
 
		25,   // low
 
		55,   // normal
 
		80,   // high
 
	};
 

	
 
	assert(lengthof(numof_industry_table) == ID_END);
 
	uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.industry_density : (uint)ID_VERY_LOW;
 
	return ScaleByMapSize(numof_industry_table[difficulty]);
 
}
 

	
 
/**
 
 * Advertise about a new industry opening.
 
 * @param ind Industry being opened.
 
 */
 
static void AdvertiseIndustryOpening(const Industry *ind)
 
{
 
	const IndustrySpec *ind_spc = GetIndustrySpec(ind->type);
 
	SetDParam(0, ind_spc->name);
 
	if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
 
		SetDParam(1, STR_TOWN_NAME);
 
		SetDParam(2, ind->town->index);
 
	} else {
 
		SetDParam(1, ind->town->index);
 
	}
 
	AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
 
	AI::BroadcastNewEvent(new ScriptEventIndustryOpen(ind->index));
 
	Game::NewEvent(new ScriptEventIndustryOpen(ind->index));
 
}
 

	
 
/**
 
 * Try to place the industry in the game.
 
 * Since there is no feedback why placement fails, there is no other option
 
 * than to try a few times before concluding it does not work.
 
 * @param type     Industry type of the desired industry.
 
 * @param try_hard Try very hard to find a place. (Used to place at least one industry per type.)
 
 * @return Pointer to created industry, or \c NULL if creation failed.
 
 */
 
static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
 
{
 
	uint tries = try_hard ? 10000u : 2000u;
 
	for (; tries > 0; tries--) {
 
		Industry *ind = CreateNewIndustry(RandomTile(), type, creation_type);
0 comments (0 inline, 0 general)