Changeset - r7382:750cdd87ddef
[Not reviewed]
master
0 3 0
rubidium - 17 years ago 2007-08-02 12:51:57
rubidium@openttd.org
(svn r10751) -Codechange: make the group struct use the pool item class as super class.
3 files changed with 43 insertions and 57 deletions:
0 comments (0 inline, 0 general)
src/group.h
Show inline comments
 
@@ -10,46 +10,37 @@
 
enum {
 
	ALL_GROUP     = 0xFFFD,
 
	DEFAULT_GROUP = 0xFFFE,
 
	INVALID_GROUP = 0xFFFF,
 
};
 

	
 
struct Group {
 
struct Group;
 
DECLARE_OLD_POOL(Group, Group, 5, 2047)
 

	
 
struct Group : PoolItem<Group, GroupID, &_Group_pool> {
 
	StringID string_id;                     ///< Group Name
 

	
 
	uint16 num_vehicle;                     ///< Number of vehicles wich belong to the group
 
	PlayerID owner;                         ///< Group Owner
 
	GroupID index;                          ///< Array index
 
	VehicleTypeByte vehicle_type;           ///< Vehicle type of the group
 

	
 
	bool replace_protection;                ///< If set to true, the global autoreplace have no effect on the group
 
	uint16 num_engines[TOTAL_NUM_ENGINES];  ///< Caches the number of engines of each type the player owns (no need to save this)
 

	
 
	Group(StringID str = STR_NULL);
 
	virtual ~Group();
 

	
 
	void QuickFree();
 

	
 
	bool IsValid() const;
 
};
 

	
 
DECLARE_OLD_POOL(Group, Group, 5, 2047)
 

	
 

	
 
static inline bool IsValidGroup(const Group *g)
 
{
 
	return g->string_id != STR_NULL;
 
}
 

	
 
static inline void DestroyGroup(Group *g)
 
{
 
	DeleteName(g->string_id);
 
}
 

	
 
static inline void DeleteGroup(Group *g)
 
{
 
	DestroyGroup(g);
 
	g->string_id = STR_NULL;
 
}
 

	
 
static inline bool IsValidGroupID(GroupID index)
 
{
 
	return index < GetGroupPoolSize() && IsValidGroup(GetGroup(index));
 
	return index < GetGroupPoolSize() && GetGroup(index)->IsValid();
 
}
 

	
 
static inline bool IsDefaultGroupID(GroupID index)
 
{
 
	return index == DEFAULT_GROUP;
 
}
 
@@ -61,13 +52,13 @@ static inline bool IsDefaultGroupID(Grou
 
 */
 
static inline bool IsAllGroupID(GroupID id_g)
 
{
 
	return id_g == ALL_GROUP;
 
}
 

	
 
#define FOR_ALL_GROUPS_FROM(g, start) for (g = GetGroup(start); g != NULL; g = (g->index + 1U < GetGroupPoolSize()) ? GetGroup(g->index + 1) : NULL) if (IsValidGroup(g))
 
#define FOR_ALL_GROUPS_FROM(g, start) for (g = GetGroup(start); g != NULL; g = (g->index + 1U < GetGroupPoolSize()) ? GetGroup(g->index + 1) : NULL) if (g->IsValid())
 
#define FOR_ALL_GROUPS(g) FOR_ALL_GROUPS_FROM(g, 0)
 

	
 
/**
 
 * Get the current size of the GroupPool
 
 */
 
static inline uint GetGroupArraySize(void)
src/group_cmd.cpp
Show inline comments
 
@@ -16,12 +16,13 @@
 
#include "train.h"
 
#include "aircraft.h"
 
#include "string.h"
 
#include "window.h"
 
#include "vehicle_gui.h"
 
#include "strings.h"
 
#include "misc/autoptr.hpp"
 

	
 
/**
 
 * Update the num engines of a groupID. Decrease the old one and increase the new one
 
 * @note called in SetTrainGroupID and UpdateTrainGroupID
 
 * @param i     EngineID we have to update
 
 * @param old_g index of the old group
 
@@ -36,47 +37,40 @@ static inline void UpdateNumEngineGroup(
 
		/* Increase the num engines of EngineID i of the new group if it's not the new one */
 
		if (!IsDefaultGroupID(new_g) && IsValidGroupID(new_g)) GetGroup(new_g)->num_engines[i]++;
 
	}
 
}
 

	
 

	
 
/**
 
 * Called if a new block is added to the group-pool
 
 */
 
static void GroupPoolNewBlock(uint start_item)
 
DEFINE_OLD_POOL_GENERIC(Group, Group)
 

	
 

	
 
Group::Group(StringID str)
 
{
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 * TODO - This is just a temporary stage, this will be removed. */
 
	for (Group *g = GetGroup(start_item); g != NULL; g = (g->index + 1U < GetGroupPoolSize()) ? GetGroup(g->index + 1) : NULL) g->index = start_item++;
 
	this->string_id = str;
 
}
 

	
 
DEFINE_OLD_POOL(Group, Group, GroupPoolNewBlock, NULL)
 

	
 
static Group *AllocateGroup(void)
 
Group::~Group()
 
{
 
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
 
	 * TODO - This is just a temporary stage, this will be removed. */
 
	for (Group *g = GetGroup(0); g != NULL; g = (g->index + 1U < GetGroupPoolSize()) ? GetGroup(g->index + 1) : NULL) {
 
		if (!IsValidGroup(g)) {
 
			const GroupID index = g->index;
 
	this->QuickFree();
 
	this->string_id = STR_NULL;
 
}
 

	
 
			memset(g, 0, sizeof(*g));
 
			g->index = index;
 
void Group::QuickFree()
 
{
 
	DeleteName(this->string_id);
 
}
 

	
 
			return g;
 
		}
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
	return (AddBlockToPool(&_Group_pool)) ? AllocateGroup() : NULL;
 
bool Group::IsValid() const
 
{
 
	return this->string_id != STR_NULL;
 
}
 

	
 
void InitializeGroup(void)
 
{
 
	CleanPool(&_Group_pool);
 
	AddBlockToPool(&_Group_pool);
 
	_Group_pool.CleanPool();
 
	_Group_pool.AddBlockToPool();
 
}
 

	
 

	
 
static WindowClass GetWCForVT(VehicleType vt)
 
{
 
	switch (vt) {
 
@@ -97,22 +91,27 @@ static WindowClass GetWCForVT(VehicleTyp
 
 */
 
CommandCost CmdCreateGroup(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	VehicleType vt = (VehicleType)p1;
 
	if (!IsPlayerBuildableVehicleType(vt)) return CMD_ERROR;
 

	
 
	Group *g = AllocateGroup();
 
	AutoPtrT<Group> g_auto_delete;
 

	
 
	Group *g = new Group(STR_EMPTY);
 
	if (g == NULL) return CMD_ERROR;
 

	
 
	g_auto_delete = g;
 

	
 
	if (flags & DC_EXEC) {
 
		g->owner = _current_player;
 
		g->string_id = STR_EMPTY;
 
		g->replace_protection = false;
 
		g->vehicle_type = vt;
 

	
 
		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
 

	
 
		g_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 

	
 
@@ -150,13 +149,13 @@ CommandCost CmdDeleteGroup(TileIndex til
 
		}
 

	
 
		VehicleType vt = g->vehicle_type;
 

	
 
		/* Delete the Replace Vehicle Windows */
 
		DeleteWindowById(WC_REPLACE_VEHICLE, g->vehicle_type);
 
		DeleteGroup(g);
 
		delete g;
 

	
 
		InvalidateWindowData(GetWCForVT(vt), (vt << 11) | VLW_GROUP_LIST | _current_player);
 
	}
 

	
 
	return CommandCost();
 
}
 
@@ -413,13 +412,13 @@ void UpdateTrainGroupID(Vehicle *v)
 

	
 
void RemoveAllGroupsForPlayer(const Player *p)
 
{
 
	Group *g;
 

	
 
	FOR_ALL_GROUPS(g) {
 
		if (p->index == g->owner) DeleteGroup(g);
 
		if (p->index == g->owner) delete g;
 
	}
 
}
 

	
 

	
 
static const SaveLoad _group_desc[] = {
 
  SLE_VAR(Group, string_id,          SLE_UINT16),
 
@@ -444,17 +443,13 @@ static void Save_GROUP(void)
 

	
 
static void Load_GROUP(void)
 
{
 
	int index;
 

	
 
	while ((index = SlIterateArray()) != -1) {
 
		if (!AddBlockIfNeeded(&_Group_pool, index)) {
 
			error("Groups: failed loading savegame: too many groups");
 
		}
 

	
 
		Group *g = GetGroup(index);
 
		Group *g = new (index) Group();
 
		SlObject(g, _group_desc);
 
	}
 
}
 

	
 
extern const ChunkHandler _group_chunk_handlers[] = {
 
	{ 'GRPS', Save_GROUP, Load_GROUP, CH_ARRAY | CH_LAST},
src/strings.cpp
Show inline comments
 
@@ -846,13 +846,13 @@ static char* FormatString(char* buff, co
 
			}
 

	
 
			case SCC_GROUP_NAME: { // {GROUP}
 
				const Group *g = GetGroup(GetInt32(&argv));
 
				int64 args[1];
 

	
 
				assert(IsValidGroup(g));
 
				assert(g->IsValid());
 

	
 
				args[0] = g->index;
 
				buff = GetStringWithArgs(buff, IsCustomName(g->string_id) ? g->string_id : (StringID)STR_GROUP_NAME_FORMAT, args, last);
 

	
 
				break;
 
			}
0 comments (0 inline, 0 general)