PlayerID data type?

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
Zojj
Engineer
Engineer
Posts: 97
Joined: 27 Apr 2007 17:58
Location: Vegas baby
Contact:

PlayerID data type?

Post by Zojj »

Can someone explain to me the PlayerID data type? It is very confusing. CargoID is just a byte.

Code: Select all

enum Owner {
	PLAYER_INACTIVE_CLIENT = 253,
	PLAYER_NEW_COMPANY = 254,
	PLAYER_SPECTATOR = 255,
	MAX_PLAYERS     = 8,
	...
};

typedef Owner PlayerID;  ///< PlayerID not a byte

/// but then there is this:

template <typename Tenum_t> struct TinyEnumT
{
	...
};

typedef TinyEnumT<Owner> OwnerByte;
typedef OwnerByte PlayerByte;

struct Player {
	...
	PlayerByte index;  
	...
};

p->index  ///< PlayerByte is a byte, but also a  PlayerID???

I can still work without understanding this, but I'd be grateful if someone helped me out. =)
I'm on the Zoloft to keep me from killing yall

My patches: Better graphs - Train acceleration - Crash rates
Rubidium
OpenTTD Developer
OpenTTD Developer
Posts: 3815
Joined: 09 Feb 2006 19:15

Re: PlayerID data type?

Post by Rubidium »

Zojj wrote:Can someone explain to me the PlayerID data type?
It is just the owner enum typedeffed to PlayerID; it's an enum for type strictness. But because the size of an enum is basically unknown, the compiler can take any size it thinks is appropriate, and we need to store it in the savegame for which we need to know the actual size, we use some trickery that makes PlayerByte. PlayerByte is the PlayerID enum which uses a variable that is guaranteed a byte large.

We need to know the size of the variable because we have different endians and storing the first byte of a variable would give trouble as little endian would store the low 8 bits, whereas big endian stores the high 8 bits.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 13 guests