Changeset - r15958:ff98c5749e5e
[Not reviewed]
master
0 4 0
rubidium - 14 years ago 2010-08-28 18:23:14
rubidium@openttd.org
(svn r20656) -Codechange: implement counting of objects
4 files changed with 50 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/object_base.h
Show inline comments
 
@@ -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)
src/object_cmd.cpp
Show inline comments
 
@@ -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;
 
	}
src/saveload/afterload.cpp
Show inline comments
 
@@ -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));
src/saveload/object_sl.cpp
Show inline comments
 
@@ -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));
 
	}
 
}
 

	
0 comments (0 inline, 0 general)