Changeset - r9040:c7ebc3f55628
[Not reviewed]
master
0 2 0
rubidium - 16 years ago 2008-04-23 22:55:11
rubidium@openttd.org
(svn r12859) -Fix: make the town rating tests use less memory and much quicker; from 13% to unnoticable in the profile in games with lots of towns and lots of very active AIs.
2 files changed with 16 insertions and 7 deletions:
0 comments (0 inline, 0 general)
src/town.h
Show inline comments
 
@@ -121,13 +121,12 @@ struct Town : PoolItem<Town, TownID, &_T
 
	/* Player ratings as well as a mask that determines which players have a rating. */
 
	byte have_ratings;
 
	uint8 unwanted[MAX_PLAYERS]; ///< how many months companies aren't wanted by towns (bribe)
 
	PlayerByte exclusivity;      ///< which player has exslusivity
 
	uint8 exclusive_counter;     ///< months till the exclusivity expires
 
	int16 ratings[MAX_PLAYERS];
 
	int16 test_rating;
 

	
 
	/* Maximum amount of passengers and mail that can be transported. */
 
	uint32 max_pass;
 
	uint32 max_mail;
 
	uint32 new_max_pass;
 
	uint32 new_max_mail;
src/town_cmd.cpp
Show inline comments
 
@@ -2443,55 +2443,65 @@ Town *ClosestTownFromTile(TileIndex tile
 
	} else {
 
		return CalcClosestTownFromTile(tile, threshold);
 
	}
 
}
 

	
 
static bool _town_rating_test = false;
 
std::map<const Town *, int> _town_test_ratings;
 

	
 
void SetTownRatingTestMode(bool mode)
 
{
 
	static int ref_count = 0;
 
	if (mode) {
 
		if (ref_count == 0) {
 
			Town *t;
 
			FOR_ALL_TOWNS(t) t->test_rating = t->ratings[_current_player];
 
			_town_test_ratings.empty();
 
		}
 
		ref_count++;
 
	} else {
 
		assert(ref_count > 0);
 
		ref_count--;
 
	}
 
	_town_rating_test = !(ref_count == 0);
 
}
 

	
 
static int GetRating(const Town *t)
 
{
 
	if (_town_rating_test) {
 
		std::map<const Town *, int>::iterator it = _town_test_ratings.find(t);
 
		if (it != _town_test_ratings.end()) {
 
			return (*it).second;
 
		}
 
	}
 
	return t->ratings[_current_player];
 
}
 

	
 
void ChangeTownRating(Town *t, int add, int max)
 
{
 
	/* if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff */
 
	if (t == NULL ||
 
			!IsValidPlayer(_current_player) ||
 
			(_cheats.magic_bulldozer.value && add < 0)) {
 
		return;
 
	}
 

	
 
	SetBit(t->have_ratings, _current_player);
 

	
 
	int rating = _town_rating_test ? t->test_rating : t->ratings[_current_player];
 

	
 
	int rating = GetRating(t);
 
	if (add < 0) {
 
		if (rating > max) {
 
			rating += add;
 
			if (rating < max) rating = max;
 
		}
 
	} else {
 
		if (rating < max) {
 
			rating += add;
 
			if (rating > max) rating = max;
 
		}
 
	}
 
	if (_town_rating_test) {
 
		t->test_rating = rating;
 
		_town_test_ratings[t] = rating;
 
	} else {
 
		t->ratings[_current_player] = rating;
 
	}
 
}
 

	
 
/* penalty for removing town-owned stuff */
 
@@ -2511,13 +2521,13 @@ bool CheckforTownRating(uint32 flags, To
 
	/* check if you're allowed to remove the street/bridge/tunnel/industry
 
	 * owned by a town no removal if rating is lower than ... depends now on
 
	 * difficulty setting. Minimum town rating selected by difficulty level
 
	 */
 
	int modemod = _default_rating_settings[_opt.diff.town_council_tolerance][type];
 

	
 
	if ((_town_rating_test ? t->test_rating : t->ratings[_current_player]) < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) {
 
	if (GetRating(t) < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) {
 
		SetDParam(0, t->index);
 
		_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
 
		return false;
 
	}
 

	
 
	return true;
0 comments (0 inline, 0 general)