(svn r20656) -Codechange: implement counting of objects

pull/155/head
rubidium 14 years ago
parent a4bb23b7ad
commit 63478d8533

@ -38,6 +38,48 @@ struct Object : ObjectPool::PoolItem<&_object_pool> {
* @return The object.
*/
static Object *GetByTile(TileIndex tile);
/**
* Increment the count of objects for this type.
* @param type ObjectType to increment
* @pre type < NUM_OBJECTS
*/
static inline void IncTypeCount(ObjectType type)
{
assert(type < NUM_OBJECTS);
counts[type]++;
}
/**
* Decrement the count of objects for this type.
* @param type ObjectType to decrement
* @pre type < NUM_OBJECTS
*/
static inline void DecTypeCount(ObjectType type)
{
assert(type < NUM_OBJECTS);
counts[type]--;
}
/**
* Get the count of objects for this type.
* @param type ObjectType to query
* @pre type < NUM_OBJECTS
*/
static inline uint16 GetTypeCount(ObjectType type)
{
assert(type < NUM_OBJECTS);
return counts[type];
}
/** Resets object counts. */
static inline void ResetTypeCounts()
{
memset(&counts, 0, sizeof(counts));
}
protected:
static uint16 counts[NUM_OBJECTS]; ///< Number of objects per type ingame
};
#define FOR_ALL_OBJECTS_FROM(var, start) FOR_ALL_ITEMS_FROM(Object, object_index, var, start)

@ -39,6 +39,7 @@
ObjectPool _object_pool("Object");
INSTANTIATE_POOL_METHODS(Object)
uint16 Object::counts[NUM_OBJECTS];
/* static */ Object *Object::GetByTile(TileIndex tile)
{
@ -49,6 +50,7 @@ INSTANTIATE_POOL_METHODS(Object)
void InitializeObjects()
{
_object_pool.CleanPool();
Object::ResetTypeCounts();
}
void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town)
@ -68,6 +70,8 @@ void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town)
MakeObject(t, type, owner, o->index, wc, Random());
MarkTileDirtyByTile(t);
}
Object::IncTypeCount(type);
}
/**
@ -317,6 +321,7 @@ static CommandCost ClearTile_Object(TileIndex tile, DoCommandFlag flags)
}
if (flags & DC_EXEC) {
Object::DecTypeCount(type);
TILE_AREA_LOOP(tile_cur, ta) MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur));
delete o;
}

@ -1875,6 +1875,7 @@ bool AfterLoadGame()
o->build_date = _date;
o->town = type == OBJECT_STATUE ? Town::Get(_m[t].m2) : CalcClosestTownFromTile(t, UINT_MAX);
_m[t].m2 = o->index;
Object::IncTypeCount(type);
} else {
/* We're at an offset, so get the ID from our "root". */
TileIndex northern_tile = t - TileXY(GB(offset, 0, 4), GB(offset, 4, 4));

@ -11,6 +11,7 @@
#include "../stdafx.h"
#include "../object_base.h"
#include "../object_map.h"
#include "saveload.h"
#include "newgrf_sl.h"
@ -50,6 +51,7 @@ static void Ptrs_OBJS()
Object *o;
FOR_ALL_OBJECTS(o) {
SlObject(o, _object_desc);
Object::IncTypeCount(GetObjectType(o->location.tile));
}
}

Loading…
Cancel
Save