Changeset - r15642:8b9b2e97124a
[Not reviewed]
master
0 3 0
alberth - 14 years ago 2010-08-02 20:19:10
alberth@openttd.org
(svn r20317) -Codechange: Move variable declaration to their first use.
3 files changed with 15 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/company_cmd.cpp
Show inline comments
 
@@ -308,46 +308,42 @@ CommandCost CheckTileOwnership(TileIndex
 

	
 
	/* no need to get the name of the owner unless we're the local company (saves some time) */
 
	if (IsLocalCompany()) GetNameOfOwner(owner, tile);
 
	return_cmd_error(STR_ERROR_OWNED_BY);
 
}
 

	
 
/**
 
 * Generate the name of a company from the last build coordinate.
 
 * @param c Company to give a name.
 
 */
 
static void GenerateCompanyName(Company *c)
 
{
 
	TileIndex tile;
 
	Town *t;
 
	StringID str;
 
	Company *cc;
 
	uint32 strp;
 
	/* Reserve space for extra unicode character. We need to do this to be able
 
	 * to detect too long company name. */
 
	char buffer[MAX_LENGTH_COMPANY_NAME_BYTES + MAX_CHAR_LENGTH];
 

	
 
	if (c->name_1 != STR_SV_UNNAMED) return;
 
	if (c->last_build_coordinate == 0) return;
 

	
 
	tile = c->last_build_coordinate;
 
	if (tile == 0) return;
 
	Town *t = ClosestTownFromTile(c->last_build_coordinate, UINT_MAX);
 

	
 
	t = ClosestTownFromTile(tile, UINT_MAX);
 

	
 
	StringID str;
 
	uint32 strp;
 
	if (t->name == NULL && IsInsideMM(t->townnametype, SPECSTR_TOWNNAME_START, SPECSTR_TOWNNAME_LAST + 1)) {
 
		str = t->townnametype - SPECSTR_TOWNNAME_START + SPECSTR_PLAYERNAME_START;
 
		strp = t->townnameparts;
 

	
 
verify_name:;
 
		/* No companies must have this name already */
 
		Company *cc;
 
		FOR_ALL_COMPANIES(cc) {
 
			if (cc->name_1 == str && cc->name_2 == strp) goto bad_town_name;
 
		}
 

	
 
		GetString(buffer, str, lastof(buffer));
 
		if (strlen(buffer) >= MAX_LENGTH_COMPANY_NAME_BYTES) goto bad_town_name;
 

	
 
set_name:;
 
		c->name_1 = str;
 
		c->name_2 = strp;
 

	
 
		MarkWholeScreenDirty();
src/economy.cpp
Show inline comments
 
@@ -117,34 +117,33 @@ static PriceMultipliers _price_base_mult
 
/**
 
 * Calculate the value of the company. That is the value of all
 
 * assets (vehicles, stations, etc) and money minus the loan,
 
 * except when including_loan is \c false which is useful when
 
 * we want to calculate the value for bankruptcy.
 
 * @param c              the company to get the value of.
 
 * @param including_loan include the loan in the company value.
 
 * @return the value of the company.
 
 */
 
Money CalculateCompanyValue(const Company *c, bool including_loan)
 
{
 
	Owner owner = c->index;
 
	Money value = 0;
 

	
 
	Station *st;
 
	uint num = 0;
 

	
 
	FOR_ALL_STATIONS(st) {
 
		if (st->owner == owner) num += CountBits((byte)st->facilities);
 
	}
 

	
 
	value += num * _price[PR_STATION_VALUE] * 25;
 
	Money value = num * _price[PR_STATION_VALUE] * 25;
 

	
 
	Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->owner != owner) continue;
 

	
 
		if (v->type == VEH_TRAIN ||
 
				v->type == VEH_ROAD ||
 
				(v->type == VEH_AIRCRAFT && Aircraft::From(v)->IsNormalAircraft()) ||
 
				v->type == VEH_SHIP) {
 
			value += v->value * 3 >> 1;
 
		}
 
	}
 
@@ -547,36 +546,36 @@ static void CompanyCheckBankrupt(Company
 

	
 
			if (c->is_ai) AI::Stop(c->index);
 

	
 
			CompanyID c_index = c->index;
 
			delete c;
 
			AI::BroadcastNewEvent(new AIEventCompanyBankrupt(c_index));
 
	}
 
}
 

	
 
static void CompaniesGenStatistics()
 
{
 
	Station *st;
 
	Company *c;
 

	
 
	Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
 
	FOR_ALL_STATIONS(st) {
 
		cur_company.Change(st->owner);
 
		CommandCost cost(EXPENSES_PROPERTY, _price[PR_STATION_VALUE] >> 1);
 
		SubtractMoneyFromCompany(cost);
 
	}
 
	cur_company.Restore();
 

	
 
	if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_month)) return;
 

	
 
	Company *c;
 
	FOR_ALL_COMPANIES(c) {
 
		memmove(&c->old_economy[1], &c->old_economy[0], sizeof(c->old_economy) - sizeof(c->old_economy[0]));
 
		c->old_economy[0] = c->cur_economy;
 
		memset(&c->cur_economy, 0, sizeof(c->cur_economy));
 

	
 
		if (c->num_valid_stat_ent != MAX_HISTORY_MONTHS) c->num_valid_stat_ent++;
 

	
 
		UpdateCompanyRatingAndValue(c, true);
 
		if (c->block_preview != 0) c->block_preview--;
 
		CompanyCheckBankrupt(c);
 
	}
 

	
 
@@ -1432,50 +1431,47 @@ void CompaniesMonthlyLoop()
 
{
 
	CompaniesGenStatistics();
 
	if (_settings_game.economy.inflation) {
 
		AddInflation();
 
		RecomputePrices();
 
	}
 
	CompaniesPayInterest();
 
	HandleEconomyFluctuations();
 
}
 

	
 
static void DoAcquireCompany(Company *c)
 
{
 
	Company *owner;
 
	int i;
 
	Money value;
 
	CompanyID ci = c->index;
 

	
 
	CompanyNewsInformation *cni = MallocT<CompanyNewsInformation>(1);
 
	cni->FillData(c, Company::Get(_current_company));
 

	
 
	SetDParam(0, STR_NEWS_COMPANY_MERGER_TITLE);
 
	SetDParam(1, c->bankrupt_value == 0 ? STR_NEWS_MERGER_TAKEOVER_TITLE : STR_NEWS_COMPANY_MERGER_DESCRIPTION);
 
	SetDParamStr(2, cni->company_name);
 
	SetDParamStr(3, cni->other_company_name);
 
	SetDParam(4, c->bankrupt_value);
 
	AddCompanyNewsItem(STR_MESSAGE_NEWS_FORMAT, NS_COMPANY_MERGER, cni);
 
	AI::BroadcastNewEvent(new AIEventCompanyMerger(ci, _current_company));
 

	
 
	ChangeOwnershipOfCompanyItems(ci, _current_company);
 

	
 
	if (c->bankrupt_value == 0) {
 
		owner = Company::Get(_current_company);
 
		Company *owner = Company::Get(_current_company);
 
		owner->current_loan += c->current_loan;
 
	}
 

	
 
	value = CalculateCompanyValue(c) >> 2;
 
	Money value = CalculateCompanyValue(c) >> 2;
 
	Backup<CompanyByte> cur_company(_current_company, FILE_LINE);
 
	for (i = 0; i != 4; i++) {
 
	for (int i = 0; i != 4; i++) {
 
		if (c->share_owners[i] != COMPANY_SPECTATOR) {
 
			cur_company.Change(c->share_owners[i]);
 
			SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -value));
 
		}
 
	}
 
	cur_company.Restore();
 

	
 
	if (c->is_ai) AI::Stop(c->index);
 

	
 
	DeleteCompanyWindows(ci);
 
	InvalidateWindowClassesData(WC_TRAINS_LIST, 0);
 
	InvalidateWindowClassesData(WC_SHIPS_LIST, 0);
 
@@ -1509,30 +1505,29 @@ CommandCost CmdBuyShareInCompany(TileInd
 
	/* Protect new companies from hostile takeovers */
 
	if (_cur_year - c->inaugurated_year < 6) return_cmd_error(STR_ERROR_PROTECTED);
 

	
 
	/* Those lines are here for network-protection (clients can be slow) */
 
	if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 0) return cost;
 

	
 
	/* We can not buy out a real company (temporarily). TODO: well, enable it obviously */
 
	if (GetAmountOwnedBy(c, COMPANY_SPECTATOR) == 1 && !c->is_ai) return cost;
 

	
 
	cost.AddCost(CalculateCompanyValue(c) >> 2);
 
	if (flags & DC_EXEC) {
 
		OwnerByte *b = c->share_owners;
 
		int i;
 

	
 
		while (*b != COMPANY_SPECTATOR) b++; // share owners is guaranteed to contain at least one COMPANY_SPECTATOR
 
		*b = _current_company;
 

	
 
		for (i = 0; c->share_owners[i] == _current_company;) {
 
		for (int i = 0; c->share_owners[i] == _current_company;) {
 
			if (++i == 4) {
 
				c->bankrupt_value = 0;
 
				DoAcquireCompany(c);
 
				break;
 
			}
 
		}
 
		SetWindowDirty(WC_COMPANY, target_company);
 
	}
 
	return cost;
 
}
 

	
 
/**
src/engine.cpp
Show inline comments
 
@@ -515,34 +515,33 @@ void SetYearEngineAgingStops()
 

	
 
		/* Base year ending date on half the model life */
 
		YearMonthDay ymd;
 
		ConvertDateToYMD(ei->base_intro + (ei->lifelength * DAYS_IN_LEAP_YEAR) / 2, &ymd);
 

	
 
		_year_engine_aging_stops = max(_year_engine_aging_stops, ymd.year);
 
	}
 
}
 

	
 
void StartupOneEngine(Engine *e, Date aging_date)
 
{
 
	const EngineInfo *ei = &e->info;
 
	uint32 r;
 

	
 
	e->age = 0;
 
	e->flags = 0;
 
	e->company_avail = 0;
 

	
 
	/* Don't randomise the start-date in the first two years after gamestart to ensure availability
 
	 * of engines in early starting games.
 
	 * Note: TTDP uses fixed 1922 */
 
	r = Random();
 
	uint32 r = Random();
 
	e->intro_date = ei->base_intro <= ConvertYMDToDate(_settings_game.game_creation.starting_year + 2, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
 
	if (e->intro_date <= _date) {
 
		e->age = (aging_date - e->intro_date) >> 5;
 
		e->company_avail = (CompanyMask)-1;
 
		e->flags |= ENGINE_AVAILABLE;
 
	}
 

	
 
	e->reliability_start = GB(r, 16, 14) + 0x7AE0;
 
	r = Random();
 
	e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
 
	e->reliability_final = GB(r, 16, 14) + 0x3FFF;
 

	
 
@@ -592,32 +591,32 @@ static void AcceptEnginePreview(EngineID
 
	} else if (e->type == VEH_ROAD) {
 
		SetBit(c->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
 
	}
 

	
 
	e->preview_company_rank = 0xFF;
 
	if (company == _local_company) {
 
		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
 
	}
 
}
 

	
 
static CompanyID GetBestCompany(uint8 pp)
 
{
 
	const Company *c;
 
	int32 best_hist;
 
	CompanyID best_company;
 
	CompanyMask mask = 0;
 

	
 
	do {
 
		best_hist = -1;
 
		int32 best_hist = -1;
 
		best_company = INVALID_COMPANY;
 

	
 
		const Company *c;
 
		FOR_ALL_COMPANIES(c) {
 
			if (c->block_preview == 0 && !HasBit(mask, c->index) &&
 
					c->old_economy[0].performance_history > best_hist) {
 
				best_hist = c->old_economy[0].performance_history;
 
				best_company = c->index;
 
			}
 
		}
 

	
 
		if (best_company == INVALID_COMPANY) return INVALID_COMPANY;
 

	
 
		SetBit(mask, best_company);
 
	} while (--pp != 0);
0 comments (0 inline, 0 general)