From b2717d1426fb6c21987eee9754cf1cd1613a8a29 Mon Sep 17 00:00:00 2001 From: rubidium Date: Wed, 23 Apr 2008 22:55:11 +0000 Subject: (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. --- src/town.h | 1 - src/town_cmd.cpp | 22 ++++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/town.h b/src/town.h index 068567842..ab49775d3 100644 --- a/src/town.h +++ b/src/town.h @@ -124,7 +124,6 @@ struct Town : PoolItem { 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; diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 1f6c2bd61..0e7f0a2a9 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -2446,14 +2446,14 @@ Town *ClosestTownFromTile(TileIndex tile, uint threshold) } static bool _town_rating_test = false; +std::map _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 { @@ -2463,6 +2463,17 @@ void SetTownRatingTestMode(bool mode) _town_rating_test = !(ref_count == 0); } +static int GetRating(const Town *t) +{ + if (_town_rating_test) { + std::map::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 */ @@ -2474,8 +2485,7 @@ void ChangeTownRating(Town *t, int add, int max) 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; @@ -2488,7 +2498,7 @@ void ChangeTownRating(Town *t, int add, int max) } } if (_town_rating_test) { - t->test_rating = rating; + _town_test_ratings[t] = rating; } else { t->ratings[_current_player] = rating; } @@ -2514,7 +2524,7 @@ bool CheckforTownRating(uint32 flags, Town *t, byte type) */ 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; -- cgit v1.2.3-54-g00ecf