mirror of https://github.com/OpenTTD/OpenTTD
(svn r13077) -Codechange: move function that updates cached num_engines to engine.cpp, make it run only 1 loop
parent
bb3bd587a6
commit
a9cc987929
|
@ -27,6 +27,7 @@
|
||||||
#include "settings_type.h"
|
#include "settings_type.h"
|
||||||
#include "oldpool_func.h"
|
#include "oldpool_func.h"
|
||||||
#include "core/alloc_func.hpp"
|
#include "core/alloc_func.hpp"
|
||||||
|
#include "vehicle_func.h"
|
||||||
#include "map"
|
#include "map"
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
@ -140,6 +141,42 @@ void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint
|
||||||
qsort(&((*el)[begin]), num_items, sizeof(EngineID), compare);
|
qsort(&((*el)[begin]), num_items, sizeof(EngineID), compare);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetCachedEngineCounts()
|
||||||
|
{
|
||||||
|
uint engines = GetEnginePoolSize();
|
||||||
|
|
||||||
|
/* Set up the engine count for all players */
|
||||||
|
Player *p;
|
||||||
|
FOR_ALL_PLAYERS(p) {
|
||||||
|
free(p->num_engines);
|
||||||
|
p->num_engines = CallocT<EngineID>(engines);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Recalculate */
|
||||||
|
Group *g;
|
||||||
|
FOR_ALL_GROUPS(g) {
|
||||||
|
free(g->num_engines);
|
||||||
|
g->num_engines = CallocT<EngineID>(engines);
|
||||||
|
}
|
||||||
|
|
||||||
|
const Vehicle *v;
|
||||||
|
FOR_ALL_VEHICLES(v) {
|
||||||
|
if (!IsEngineCountable(v)) continue;
|
||||||
|
|
||||||
|
assert(v->engine_type < engines);
|
||||||
|
|
||||||
|
GetPlayer(v->owner)->num_engines[v->engine_type]++;
|
||||||
|
|
||||||
|
if (v->group_id == DEFAULT_GROUP) continue;
|
||||||
|
|
||||||
|
g = GetGroup(v->group_id);
|
||||||
|
assert(v->type == g->vehicle_type);
|
||||||
|
assert(v->owner == g->owner);
|
||||||
|
|
||||||
|
g->num_engines[v->engine_type]++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SetupEngines()
|
void SetupEngines()
|
||||||
{
|
{
|
||||||
_Engine_pool.CleanPool();
|
_Engine_pool.CleanPool();
|
||||||
|
|
|
@ -27,6 +27,7 @@ void DeleteCustomEngineNames();
|
||||||
|
|
||||||
bool IsEngineBuildable(EngineID engine, VehicleType type, PlayerID player);
|
bool IsEngineBuildable(EngineID engine, VehicleType type, PlayerID player);
|
||||||
CargoID GetEngineCargoType(EngineID engine);
|
CargoID GetEngineCargoType(EngineID engine);
|
||||||
|
void SetCachedEngineCounts();
|
||||||
|
|
||||||
typedef int CDECL EngList_SortTypeFunction(const void*, const void*); ///< argument type for EngList_Sort()
|
typedef int CDECL EngList_SortTypeFunction(const void*, const void*); ///< argument type for EngList_Sort()
|
||||||
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare); ///< qsort of the engine list
|
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare); ///< qsort of the engine list
|
||||||
|
|
|
@ -1335,42 +1335,17 @@ static bool InitializeWindowsAndCaches()
|
||||||
UpdateAllTownVirtCoords();
|
UpdateAllTownVirtCoords();
|
||||||
UpdateAllWaypointSigns();
|
UpdateAllWaypointSigns();
|
||||||
|
|
||||||
/* Recalculate */
|
Player *p;
|
||||||
Group *g;
|
FOR_ALL_PLAYERS(p) {
|
||||||
FOR_ALL_GROUPS(g) {
|
|
||||||
g->num_engines = CallocT<uint16>(GetEnginePoolSize());
|
|
||||||
|
|
||||||
const Vehicle *v;
|
|
||||||
FOR_ALL_VEHICLES(v) {
|
|
||||||
if (!IsEngineCountable(v)) continue;
|
|
||||||
|
|
||||||
if (v->group_id != g->index || v->type != g->vehicle_type || v->owner != g->owner) continue;
|
|
||||||
|
|
||||||
g->num_engines[v->engine_type]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set up the engine count for all players */
|
|
||||||
Player *players[MAX_PLAYERS];
|
|
||||||
const Vehicle *v;
|
|
||||||
|
|
||||||
for (PlayerID i = PLAYER_FIRST; i < MAX_PLAYERS; i++) {
|
|
||||||
players[i] = GetPlayer(i);
|
|
||||||
|
|
||||||
/* For each player, verify (while loading a scenario) that the inauguration date is the current year and set it
|
/* For each player, verify (while loading a scenario) that the inauguration date is the current year and set it
|
||||||
* accordingly if it is not the case. No need to set it on players that are not been used already,
|
* accordingly if it is not the case. No need to set it on players that are not been used already,
|
||||||
* thus the MIN_YEAR (which is really nothing more than Zero, initialized value) test */
|
* thus the MIN_YEAR (which is really nothing more than Zero, initialized value) test */
|
||||||
if (_file_to_saveload.filetype == FT_SCENARIO && players[i]->inaugurated_year != MIN_YEAR)
|
if (_file_to_saveload.filetype == FT_SCENARIO && p->inaugurated_year != MIN_YEAR) {
|
||||||
players[i]->inaugurated_year = _cur_year;
|
p->inaugurated_year = _cur_year;
|
||||||
|
}
|
||||||
free(players[i]->num_engines);
|
|
||||||
players[i]->num_engines = CallocT<uint16>(GetEnginePoolSize());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FOR_ALL_VEHICLES(v) {
|
SetCachedEngineCounts();
|
||||||
if (!IsEngineCountable(v)) continue;
|
|
||||||
players[v->owner]->num_engines[v->engine_type]++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue