# HG changeset patch # User rubidium # Date 2010-08-28 18:23:14 # Node ID ff98c5749e5e2181fb5e57ae0dc3032006164f1f # Parent 71ffc94a16b5ad45fd83a420b25a5db94a329255 (svn r20656) -Codechange: implement counting of objects diff --git a/src/object_base.h b/src/object_base.h --- a/src/object_base.h +++ b/src/object_base.h @@ -38,6 +38,48 @@ struct Object : ObjectPool::PoolItem<&_o * @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) diff --git a/src/object_cmd.cpp b/src/object_cmd.cpp --- a/src/object_cmd.cpp +++ b/src/object_cmd.cpp @@ -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, TileIn MakeObject(t, type, owner, o->index, wc, Random()); MarkTileDirtyByTile(t); } + + Object::IncTypeCount(type); } /** @@ -317,6 +321,7 @@ static CommandCost ClearTile_Object(Tile } if (flags & DC_EXEC) { + Object::DecTypeCount(type); TILE_AREA_LOOP(tile_cur, ta) MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur)); delete o; } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -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)); diff --git a/src/saveload/object_sl.cpp b/src/saveload/object_sl.cpp --- a/src/saveload/object_sl.cpp +++ b/src/saveload/object_sl.cpp @@ -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)); } }