summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/object_base.h42
-rw-r--r--src/object_cmd.cpp5
-rw-r--r--src/saveload/afterload.cpp1
-rw-r--r--src/saveload/object_sl.cpp2
4 files changed, 50 insertions, 0 deletions
diff --git a/src/object_base.h b/src/object_base.h
index a4e7eb0d7..d5f796191 100644
--- a/src/object_base.h
+++ b/src/object_base.h
@@ -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)
diff --git a/src/object_cmd.cpp b/src/object_cmd.cpp
index 660d32991..87d5f7369 100644
--- 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, 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;
}
diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp
index 388faceee..95a379c9d 100644
--- 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
index 0e36100c3..6218eea1e 100644
--- 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));
}
}