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
 
@@ -35,12 +35,54 @@ struct Object : ObjectPool::PoolItem<&_o
 
	/**
 
	 * Get the object associated with a tile.
 
	 * @param tile The tile to fetch the object for.
 
	 * @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)
 
#define FOR_ALL_OBJECTS(var) FOR_ALL_OBJECTS_FROM(var, 0)
 

	
 
#endif /* OBJECT_BASE_H */
src/object_cmd.cpp
Show inline comments
 
@@ -36,22 +36,24 @@
 

	
 
#include "table/strings.h"
 
#include "table/object_land.h"
 

	
 
ObjectPool _object_pool("Object");
 
INSTANTIATE_POOL_METHODS(Object)
 
uint16 Object::counts[NUM_OBJECTS];
 

	
 
/* static */ Object *Object::GetByTile(TileIndex tile)
 
{
 
	return Object::Get(GetObjectIndex(tile));
 
}
 

	
 
/** Initialize/reset the objects. */
 
void InitializeObjects()
 
{
 
	_object_pool.CleanPool();
 
	Object::ResetTypeCounts();
 
}
 

	
 
void BuildObject(ObjectType type, TileIndex tile, CompanyID owner, Town *town)
 
{
 
	const ObjectSpec *spec = ObjectSpec::Get(type);
 

	
 
@@ -65,12 +67,14 @@ void BuildObject(ObjectType type, TileIn
 

	
 
	TILE_AREA_LOOP(t, ta) {
 
		WaterClass wc = (IsWaterTile(t) ? GetWaterClass(t) : WATER_CLASS_INVALID);
 
		MakeObject(t, type, owner, o->index, wc, Random());
 
		MarkTileDirtyByTile(t);
 
	}
 

	
 
	Object::IncTypeCount(type);
 
}
 

	
 
/**
 
 * Increase the animation stage of a whole structure.
 
 * @param tile The tile of the structure.
 
 */
 
@@ -314,12 +318,13 @@ static CommandCost ClearTile_Object(Tile
 

	
 
		default:
 
			break;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		Object::DecTypeCount(type);
 
		TILE_AREA_LOOP(tile_cur, ta) MakeWaterKeepingClass(tile_cur, GetTileOwner(tile_cur));
 
		delete o;
 
	}
 

	
 
	return cost;
 
}
src/saveload/afterload.cpp
Show inline comments
 
@@ -1872,12 +1872,13 @@ bool AfterLoadGame()
 
					o->location.tile = t;
 
					o->location.w    = size;
 
					o->location.h    = size;
 
					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));
 
					assert(IsTileType(northern_tile, MP_OBJECT));
 
					_m[t].m2 = _m[northern_tile].m2;
 
				}
src/saveload/object_sl.cpp
Show inline comments
 
@@ -8,12 +8,13 @@
 
 */
 

	
 
/** @file object_sl.cpp Code handling saving and loading of objects */
 

	
 
#include "../stdafx.h"
 
#include "../object_base.h"
 
#include "../object_map.h"
 

	
 
#include "saveload.h"
 
#include "newgrf_sl.h"
 

	
 
static const SaveLoad _object_desc[] = {
 
	    SLE_VAR(Object, location.tile,              SLE_UINT32),
 
@@ -47,12 +48,13 @@ static void Load_OBJS()
 

	
 
static void Ptrs_OBJS()
 
{
 
	Object *o;
 
	FOR_ALL_OBJECTS(o) {
 
		SlObject(o, _object_desc);
 
		Object::IncTypeCount(GetObjectType(o->location.tile));
 
	}
 
}
 

	
 
static void Save_OBID()
 
{
 
	Save_NewGRFMapping(_object_mngr);
0 comments (0 inline, 0 general)