mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-11 08:49:50 +00:00
(svn r22976) -Codechange: Split group statistics into separate struct.
This commit is contained in:
parent
7133887ed6
commit
b6766c2ed5
@ -477,9 +477,7 @@ void SetCachedEngineCounts()
|
|||||||
/* Recalculate */
|
/* Recalculate */
|
||||||
Group *g;
|
Group *g;
|
||||||
FOR_ALL_GROUPS(g) {
|
FOR_ALL_GROUPS(g) {
|
||||||
g->num_vehicle = 0;
|
g->statistics.Clear();
|
||||||
free(g->num_engines);
|
|
||||||
g->num_engines = CallocT<EngineID>(engines);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const Vehicle *v;
|
const Vehicle *v;
|
||||||
@ -496,8 +494,8 @@ void SetCachedEngineCounts()
|
|||||||
assert(v->type == g->vehicle_type);
|
assert(v->type == g->vehicle_type);
|
||||||
assert(v->owner == g->owner);
|
assert(v->owner == g->owner);
|
||||||
|
|
||||||
g->num_engines[v->engine_type]++;
|
g->statistics.num_engines[v->engine_type]++;
|
||||||
if (v->IsPrimaryVehicle()) g->num_vehicle++;
|
if (v->IsPrimaryVehicle()) g->statistics.num_vehicle++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
19
src/group.h
19
src/group.h
@ -21,16 +21,25 @@
|
|||||||
typedef Pool<Group, GroupID, 16, 64000> GroupPool;
|
typedef Pool<Group, GroupID, 16, 64000> GroupPool;
|
||||||
extern GroupPool _group_pool; ///< Pool of groups.
|
extern GroupPool _group_pool; ///< Pool of groups.
|
||||||
|
|
||||||
|
/** Statistics and caches on the vehicles in a group. */
|
||||||
|
struct GroupStatistics {
|
||||||
|
uint16 num_vehicle; ///< Number of vehicles.
|
||||||
|
uint16 *num_engines; ///< Caches the number of engines of each type the company owns.
|
||||||
|
|
||||||
|
GroupStatistics();
|
||||||
|
~GroupStatistics();
|
||||||
|
|
||||||
|
void Clear();
|
||||||
|
};
|
||||||
|
|
||||||
/** Group data. */
|
/** Group data. */
|
||||||
struct Group : GroupPool::PoolItem<&_group_pool> {
|
struct Group : GroupPool::PoolItem<&_group_pool> {
|
||||||
char *name; ///< Group Name
|
char *name; ///< Group Name
|
||||||
|
|
||||||
uint16 num_vehicle; ///< Number of vehicles in the group
|
|
||||||
OwnerByte owner; ///< Group Owner
|
OwnerByte owner; ///< Group Owner
|
||||||
VehicleTypeByte vehicle_type; ///< Vehicle type of the group
|
VehicleTypeByte vehicle_type; ///< Vehicle type of the group
|
||||||
|
|
||||||
bool replace_protection; ///< If set to true, the global autoreplace have no effect on the group
|
bool replace_protection; ///< If set to true, the global autoreplace have no effect on the group
|
||||||
uint16 *num_engines; ///< Caches the number of engines of each type the company owns (no need to save this)
|
GroupStatistics statistics; ///< NOSAVE: Statistics and caches on the vehicles in the group.
|
||||||
|
|
||||||
Group(CompanyID owner = INVALID_COMPANY);
|
Group(CompanyID owner = INVALID_COMPANY);
|
||||||
~Group();
|
~Group();
|
||||||
@ -77,7 +86,7 @@ uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
|
|||||||
static inline void IncreaseGroupNumVehicle(GroupID id_g)
|
static inline void IncreaseGroupNumVehicle(GroupID id_g)
|
||||||
{
|
{
|
||||||
Group *g = Group::GetIfValid(id_g);
|
Group *g = Group::GetIfValid(id_g);
|
||||||
if (g != NULL) g->num_vehicle++;
|
if (g != NULL) g->statistics.num_vehicle++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,7 +96,7 @@ static inline void IncreaseGroupNumVehicle(GroupID id_g)
|
|||||||
static inline void DecreaseGroupNumVehicle(GroupID id_g)
|
static inline void DecreaseGroupNumVehicle(GroupID id_g)
|
||||||
{
|
{
|
||||||
Group *g = Group::GetIfValid(id_g);
|
Group *g = Group::GetIfValid(id_g);
|
||||||
if (g != NULL) g->num_vehicle--;
|
if (g != NULL) g->statistics.num_vehicle--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -32,6 +32,29 @@ GroupID _new_group_id;
|
|||||||
GroupPool _group_pool("Group");
|
GroupPool _group_pool("Group");
|
||||||
INSTANTIATE_POOL_METHODS(Group)
|
INSTANTIATE_POOL_METHODS(Group)
|
||||||
|
|
||||||
|
GroupStatistics::GroupStatistics()
|
||||||
|
{
|
||||||
|
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
GroupStatistics::~GroupStatistics()
|
||||||
|
{
|
||||||
|
free(this->num_engines);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear all caches.
|
||||||
|
*/
|
||||||
|
void GroupStatistics::Clear()
|
||||||
|
{
|
||||||
|
this->num_vehicle = 0;
|
||||||
|
|
||||||
|
/* This is also called when NewGRF change. So the number of engines might have changed. Reallocate. */
|
||||||
|
free(this->num_engines);
|
||||||
|
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the num engines of a groupID. Decrease the old one and increase the new one
|
* Update the num engines of a groupID. Decrease the old one and increase the new one
|
||||||
* @note called in SetTrainGroupID and UpdateTrainGroupID
|
* @note called in SetTrainGroupID and UpdateTrainGroupID
|
||||||
@ -43,10 +66,10 @@ static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g
|
|||||||
{
|
{
|
||||||
if (old_g != new_g) {
|
if (old_g != new_g) {
|
||||||
/* Decrease the num engines of EngineID i of the old group if it's not the default one */
|
/* Decrease the num engines of EngineID i of the old group if it's not the default one */
|
||||||
if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->num_engines[i]--;
|
if (!IsDefaultGroupID(old_g) && Group::IsValidID(old_g)) Group::Get(old_g)->statistics.num_engines[i]--;
|
||||||
|
|
||||||
/* Increase the num engines of EngineID i of the new group if it's not the default one */
|
/* Increase the num engines of EngineID i of the new group if it's not the default one */
|
||||||
if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->num_engines[i]++;
|
if (!IsDefaultGroupID(new_g) && Group::IsValidID(new_g)) Group::Get(new_g)->statistics.num_engines[i]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,16 +78,11 @@ static inline void UpdateNumEngineGroup(EngineID i, GroupID old_g, GroupID new_g
|
|||||||
Group::Group(Owner owner)
|
Group::Group(Owner owner)
|
||||||
{
|
{
|
||||||
this->owner = owner;
|
this->owner = owner;
|
||||||
|
|
||||||
if (!Company::IsValidID(owner)) return;
|
|
||||||
|
|
||||||
this->num_engines = CallocT<uint16>(Engine::GetPoolSize());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Group::~Group()
|
Group::~Group()
|
||||||
{
|
{
|
||||||
free(this->name);
|
free(this->name);
|
||||||
free(this->num_engines);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -414,14 +432,14 @@ void UpdateTrainGroupID(Train *v)
|
|||||||
*/
|
*/
|
||||||
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
|
uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e)
|
||||||
{
|
{
|
||||||
if (Group::IsValidID(id_g)) return Group::Get(id_g)->num_engines[id_e];
|
if (Group::IsValidID(id_g)) return Group::Get(id_g)->statistics.num_engines[id_e];
|
||||||
|
|
||||||
uint num = Company::Get(company)->num_engines[id_e];
|
uint num = Company::Get(company)->num_engines[id_e];
|
||||||
if (!IsDefaultGroupID(id_g)) return num;
|
if (!IsDefaultGroupID(id_g)) return num;
|
||||||
|
|
||||||
const Group *g;
|
const Group *g;
|
||||||
FOR_ALL_GROUPS(g) {
|
FOR_ALL_GROUPS(g) {
|
||||||
if (g->owner == company) num -= g->num_engines[id_e];
|
if (g->owner == company) num -= g->statistics.num_engines[id_e];
|
||||||
}
|
}
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -303,8 +303,8 @@ public:
|
|||||||
|
|
||||||
SetDParam(0, STR_GROUP_NAME);
|
SetDParam(0, STR_GROUP_NAME);
|
||||||
SetDParam(1, g->index);
|
SetDParam(1, g->index);
|
||||||
SetDParam(2, g->num_vehicle);
|
SetDParam(2, g->statistics.num_vehicle);
|
||||||
SetDParam(3, g->num_vehicle);
|
SetDParam(3, g->statistics.num_vehicle);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -391,7 +391,7 @@ public:
|
|||||||
DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 8, y1, STR_GROUP_NAME, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK);
|
DrawString(r.left + WD_FRAMERECT_LEFT + 8, r.right - WD_FRAMERECT_RIGHT - 8, y1, STR_GROUP_NAME, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK);
|
||||||
|
|
||||||
/* draw the number of vehicles of the group */
|
/* draw the number of vehicles of the group */
|
||||||
SetDParam(0, g->num_vehicle);
|
SetDParam(0, g->statistics.num_vehicle);
|
||||||
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y1 + 1, STR_TINY_COMMA, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK, SA_RIGHT);
|
DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y1 + 1, STR_TINY_COMMA, (this->vli.index == g->index) ? TC_WHITE : TC_BLACK, SA_RIGHT);
|
||||||
|
|
||||||
y1 += this->tiny_step_height;
|
y1 += this->tiny_step_height;
|
||||||
|
@ -697,7 +697,7 @@ void Vehicle::PreDestructor()
|
|||||||
if (this->owner == _local_company) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
|
if (this->owner == _local_company) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
|
||||||
|
|
||||||
DeleteGroupHighlightOfVehicle(this);
|
DeleteGroupHighlightOfVehicle(this);
|
||||||
if (Group::IsValidID(this->group_id)) Group::Get(this->group_id)->num_engines[this->engine_type]--;
|
if (Group::IsValidID(this->group_id)) Group::Get(this->group_id)->statistics.num_engines[this->engine_type]--;
|
||||||
if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
|
if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user