Changeset - r4849:41dc3967353a
[Not reviewed]
master
0 8 0
Darkvater - 18 years ago 2006-10-14 22:22:48
darkvater@openttd.org
(svn r6775) -Codechange: Use some more proper types, especially Owner and PlayerID as
these are used intermixed often.
8 files changed with 15 insertions and 16 deletions:
0 comments (0 inline, 0 general)
functions.h
Show inline comments
 
@@ -31,49 +31,49 @@ static inline Point RemapCoords(int x, i
 
#endif
 
	return pt;
 
}
 

	
 
static inline Point RemapCoords2(int x, int y)
 
{
 
	return RemapCoords(x, y, GetSlopeZ(x, y));
 
}
 

	
 

	
 
/* clear_land.c */
 
void DrawHillyLandTile(const TileInfo *ti);
 
void DrawClearLandTile(const TileInfo *ti, byte set);
 
void DrawClearLandFence(const TileInfo *ti);
 
void TileLoopClearHelper(TileIndex tile);
 

	
 
/* water_land.c */
 
void DrawShipDepotSprite(int x, int y, int image);
 
void TileLoop_Water(TileIndex tile);
 

	
 
/* players.c */
 
bool CheckPlayerHasMoney(int32 cost);
 
void SubtractMoneyFromPlayer(int32 cost);
 
void SubtractMoneyFromPlayerFract(PlayerID player, int32 cost);
 
bool CheckOwnership(PlayerID owner);
 
bool CheckOwnership(Owner owner);
 
bool CheckTileOwnership(TileIndex tile);
 
StringID GetPlayerNameString(PlayerID player, uint index);
 

	
 
/* standard */
 
void ShowInfo(const char *str);
 
void CDECL ShowInfoF(const char *str, ...);
 
void NORETURN CDECL error(const char *str, ...);
 

	
 
/* openttd.c */
 

	
 
/**************
 
 * Warning: DO NOT enable this unless you understand what it does
 
 *
 
 * If enabled, in a network game all randoms will be dumped to the
 
 *  stdout if the first client joins (or if you are a client). This
 
 *  is to help finding desync problems.
 
 *
 
 * Warning: DO NOT enable this unless you understand what it does
 
 **************/
 

	
 
//#define RANDOM_DEBUG
 

	
 

	
 
// Enable this to produce higher quality random numbers.
map.h
Show inline comments
 
@@ -46,54 +46,55 @@ uint ScaleByMapSize(uint); // Scale rela
 
uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
 

	
 
typedef uint32 TileIndex;
 
typedef int32 TileIndexDiff;
 

	
 
static inline TileIndex TileXY(uint x, uint y)
 
{
 
	return (y * MapSizeX()) + x;
 
}
 

	
 
static inline TileIndexDiff TileDiffXY(int x, int y)
 
{
 
	// Multiplication gives much better optimization on MSVC than shifting.
 
	// 0 << shift isn't optimized to 0 properly.
 
	// Typically x and y are constants, and then this doesn't result
 
	// in any actual multiplication in the assembly code..
 
	return (y * MapSizeX()) + x;
 
}
 

	
 
static inline TileIndex TileVirtXY(uint x, uint y)
 
{
 
	return (y >> 4 << MapLogX()) + (x >> 4);
 
}
 

	
 
typedef enum Owner {
 
typedef byte Owner;
 
enum Owners {
 
	OWNER_TOWN      = 0x0F, // a town owns the tile
 
	OWNER_NONE      = 0x10, // nobody owns the tile
 
	OWNER_WATER     = 0x11, // "water" owns the tile
 
	OWNER_END       = 0x12,
 
} Owner;
 
};
 

	
 
enum {
 
	INVALID_TILE = (TileIndex)-1
 
};
 

	
 
enum {
 
	TILE_SIZE   = 16,   /* Tiles are 16x16 "units" in size */
 
	TILE_PIXELS = 32,   /* a tile is 32x32 pixels */
 
	TILE_HEIGHT =  8,   /* The standard height-difference between tiles on two levels is 8 (z-diff 8) */
 
};
 

	
 

	
 
static inline uint TileX(TileIndex tile)
 
{
 
	return tile & MapMaxX();
 
}
 

	
 
static inline uint TileY(TileIndex tile)
 
{
 
	return tile >> MapLogX();
 
}
 

	
 

	
 
typedef struct TileIndexDiffC {
openttd.h
Show inline comments
 
@@ -271,49 +271,49 @@ enum {
 
	CT_DIAMONDS     = 10,
 

	
 
	// Toyland
 
	CT_SUGAR        =  1,
 
	CT_TOYS         =  3,
 
	CT_BATTERIES    =  4,
 
	CT_CANDY        =  5,
 
	CT_TOFFEE       =  6,
 
	CT_COLA         =  7,
 
	CT_COTTON_CANDY =  8,
 
	CT_BUBBLES      =  9,
 
	CT_PLASTIC      = 10,
 
	CT_FIZZY_DRINKS = 11,
 

	
 
	NUM_CARGO       = 12,
 

	
 
	CT_NO_REFIT     = 0xFE,
 
	CT_INVALID      = 0xFF
 
};
 

	
 
typedef uint AcceptedCargo[NUM_CARGO];
 

	
 
typedef struct TileDesc {
 
	StringID str;
 
	byte owner;
 
	Owner owner;
 
	Date build_date;
 
	uint32 dparam[2];
 
} TileDesc;
 

	
 
typedef struct {
 
	int32 left;
 
	int32 top;
 
	byte width_1, width_2;
 
} ViewportSign;
 

	
 

	
 
typedef void DrawTileProc(TileInfo *ti);
 
typedef uint GetSlopeZProc(TileIndex tile, uint x, uint y);
 
typedef int32 ClearTileProc(TileIndex tile, byte flags);
 
typedef void GetAcceptedCargoProc(TileIndex tile, AcceptedCargo res);
 
typedef void GetTileDescProc(TileIndex tile, TileDesc *td);
 
/* GetTileTrackStatusProcs return a value that contains the possible tracks
 
 * that can be taken on a given tile by a given transport. The return value is
 
 * composed as follows: 0xaabbccdd. ccdd and aabb are bitmasks of trackdirs,
 
 * where bit n corresponds to trackdir n. ccdd are the trackdirs that are
 
 * present in the tile (1==present, 0==not present), aabb is the signal
 
 * status, if applicable (0==green/no signal, 1==red, note that this is
 
 * reversed from map3/2[tile] for railway signals).
 
 *
player.h
Show inline comments
 
@@ -181,49 +181,49 @@ typedef struct Player {
 
	byte quarters_of_bankrupcy;
 
	byte bankrupt_asked; // which players were asked about buying it?
 
	int16 bankrupt_timeout;
 
	int32 bankrupt_value;
 

	
 
	bool is_active;
 
	bool is_ai;
 
	PlayerAI ai;
 
	PlayerAiNew ainew;
 

	
 
	int64 yearly_expenses[3][13];
 
	PlayerEconomyEntry cur_economy;
 
	PlayerEconomyEntry old_economy[24];
 
	EngineRenewList engine_renew_list; // Defined later
 
	bool engine_renew;
 
	bool renew_keep_length;
 
	int16 engine_renew_months;
 
	uint32 engine_renew_money;
 
	uint16 num_engines[TOTAL_NUM_ENGINES]; // caches the number of engines of each type the player owns (no need to save this)
 
} Player;
 

	
 
uint16 GetDrawStringPlayerColor(PlayerID player);
 

	
 
void ChangeOwnershipOfPlayerItems(PlayerID old_player, PlayerID new_player);
 
void GetNameOfOwner(PlayerID owner, TileIndex tile);
 
void GetNameOfOwner(Owner owner, TileIndex tile);
 
int64 CalculateCompanyValue(const Player* p);
 
void InvalidatePlayerWindows(const Player* p);
 
void UpdatePlayerMoney32(Player *p);
 
#define FOR_ALL_PLAYERS(p) for (p = _players; p != endof(_players); p++)
 

	
 
VARDEF PlayerID _local_player;
 
VARDEF PlayerID _current_player;
 

	
 
/* Player identifiers All players below MAX_PLAYERS are playable
 
 * players, above, they are special, computer controlled players */
 
enum {
 
	PLAYER_SPECTATOR   = 255, ///< Spectator in Multiplayer or the player in the scenario editor
 
	MAX_PLAYERS        = 8,
 
};
 

	
 
VARDEF Player _players[MAX_PLAYERS];
 
// NOSAVE: can be determined from player structs
 
VARDEF byte _player_colors[MAX_PLAYERS];
 

	
 
static inline byte ActivePlayerCount(void)
 
{
 
	const Player *p;
 
	byte count = 0;
 

	
players.c
Show inline comments
 
@@ -238,85 +238,85 @@ void SubtractMoneyFromPlayer(int32 cost)
 

	
 
void SubtractMoneyFromPlayerFract(PlayerID player, int32 cost)
 
{
 
	Player *p = GetPlayer(player);
 
	byte m = p->player_money_fraction;
 

	
 
	p->player_money_fraction = m - (byte)cost;
 
	cost >>= 8;
 
	if (p->player_money_fraction > m) cost++;
 
	if (cost != 0) SubtractMoneyFromAnyPlayer(p, cost);
 
}
 

	
 
// the player_money field is kept as it is, but money64 contains the actual amount of money.
 
void UpdatePlayerMoney32(Player *p)
 
{
 
	if (p->money64 < -2000000000) {
 
		p->player_money = -2000000000;
 
	} else if (p->money64 > 2000000000) {
 
		p->player_money = 2000000000;
 
	} else {
 
		p->player_money = (int32)p->money64;
 
	}
 
}
 

	
 
void GetNameOfOwner(PlayerID owner, TileIndex tile)
 
void GetNameOfOwner(Owner owner, TileIndex tile)
 
{
 
	SetDParam(2, owner);
 

	
 
	if (owner != OWNER_TOWN) {
 
		if (owner >= MAX_PLAYERS) {
 
			SetDParam(0, STR_0150_SOMEONE);
 
		} else {
 
			const Player* p = GetPlayer(owner);
 

	
 
			SetDParam(0, p->name_1);
 
			SetDParam(1, p->name_2);
 
		}
 
	} else {
 
		const Town* t = ClosestTownFromTile(tile, (uint)-1);
 

	
 
		SetDParam(0, STR_TOWN);
 
		SetDParam(1, t->index);
 
	}
 
}
 

	
 

	
 
bool CheckOwnership(PlayerID owner)
 
{
 
	assert(owner <= OWNER_WATER);
 
	assert(owner < OWNER_END);
 

	
 
	if (owner == _current_player) return true;
 
	_error_message = STR_013B_OWNED_BY;
 
	GetNameOfOwner(owner, 0);
 
	return false;
 
}
 

	
 
bool CheckTileOwnership(TileIndex tile)
 
{
 
	PlayerID owner = GetTileOwner(tile);
 
	Owner owner = GetTileOwner(tile);
 

	
 
	assert(owner <= OWNER_WATER);
 
	assert(owner < OWNER_END);
 

	
 
	if (owner == _current_player) return true;
 
	_error_message = STR_013B_OWNED_BY;
 

	
 
	// no need to get the name of the owner unless we're the local player (saves some time)
 
	if (IsLocalPlayer()) GetNameOfOwner(owner, tile);
 
	return false;
 
}
 

	
 
static void GenerateCompanyName(Player *p)
 
{
 
	TileIndex tile;
 
	Town *t;
 
	StringID str;
 
	Player *pp;
 
	uint32 strp;
 
	char buffer[100];
 

	
 
	if (p->name_1 != STR_SV_UNNAMED) return;
 

	
 
	tile = p->last_build_coordinate;
 
	if (tile == 0) return;
 

	
 
	t = ClosestTownFromTile(tile, (uint)-1);
road_cmd.c
Show inline comments
 
@@ -18,104 +18,102 @@
 
#include "player.h"
 
#include "town.h"
 
#include "gfx.h"
 
#include "sound.h"
 
#include "yapf/yapf.h"
 
#include "depot.h"
 

	
 

	
 
static uint CountRoadBits(RoadBits r)
 
{
 
	uint count = 0;
 

	
 
	if (r & ROAD_NW) ++count;
 
	if (r & ROAD_SW) ++count;
 
	if (r & ROAD_SE) ++count;
 
	if (r & ROAD_NE) ++count;
 
	return count;
 
}
 

	
 

	
 
static bool CheckAllowRemoveRoad(TileIndex tile, RoadBits remove, bool* edge_road)
 
{
 
	RoadBits present;
 
	RoadBits n;
 
	byte owner;
 
	Owner owner;
 
	*edge_road = true;
 

	
 
	if (_game_mode == GM_EDITOR) return true;
 

	
 
	// Only do the special processing for actual players.
 
	if (_current_player >= MAX_PLAYERS) return true;
 

	
 
	owner = IsLevelCrossingTile(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile);
 

	
 
	// Only do the special processing if the road is owned
 
	// by a town
 
	if (owner != OWNER_TOWN) {
 
		return owner == OWNER_NONE || CheckOwnership(owner);
 
	}
 
	if (owner != OWNER_TOWN) return (owner == OWNER_NONE) || CheckOwnership(owner);
 

	
 
	if (_cheats.magic_bulldozer.value) return true;
 

	
 
	// Get a bitmask of which neighbouring roads has a tile
 
	n = 0;
 
	present = GetAnyRoadBits(tile);
 
	if (present & ROAD_NE && GetAnyRoadBits(TILE_ADDXY(tile,-1, 0)) & ROAD_SW) n |= ROAD_NE;
 
	if (present & ROAD_SE && GetAnyRoadBits(TILE_ADDXY(tile, 0, 1)) & ROAD_NW) n |= ROAD_SE;
 
	if (present & ROAD_SW && GetAnyRoadBits(TILE_ADDXY(tile, 1, 0)) & ROAD_NE) n |= ROAD_SW;
 
	if (present & ROAD_NW && GetAnyRoadBits(TILE_ADDXY(tile, 0,-1)) & ROAD_SE) n |= ROAD_NW;
 

	
 
	// If 0 or 1 bits are set in n, or if no bits that match the bits to remove,
 
	// then allow it
 
	if ((n & (n - 1)) != 0 && (n & remove) != 0) {
 
		Town *t;
 
		*edge_road = false;
 
		// you can remove all kind of roads with extra dynamite
 
		if (_patches.extra_dynamite) return true;
 

	
 
		t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 

	
 
		SetDParam(0, t->index);
 
		_error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 

	
 
/** Delete a piece of road.
 
 * @param tile tile where to remove road from
 
 * @param p1 road piece flags
 
 * @param p2 unused
 
 */
 
int32 CmdRemoveRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	// cost for removing inner/edge -roads
 
	static const uint16 road_remove_cost[2] = {50, 18};
 

	
 
	PlayerID owner;
 
	Owner owner;
 
	Town *t;
 
	/* true if the roadpiece was always removeable,
 
	 * false if it was a center piece. Affects town ratings drop */
 
	bool edge_road;
 
	RoadBits pieces;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) */
 
	if (p1 >> 4) return CMD_ERROR;
 
	pieces = p1;
 

	
 
	if (!IsTileType(tile, MP_STREET) && !IsTileType(tile, MP_TUNNELBRIDGE)) return CMD_ERROR;
 

	
 
	owner = IsLevelCrossingTile(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile);
 

	
 
	if (owner == OWNER_TOWN && _game_mode != GM_EDITOR) {
 
		if (IsTileType(tile, MP_TUNNELBRIDGE)) { // index of town is not saved for bridge (no space)
 
			t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 
		} else {
 
			t = GetTownByTile(tile);
 
		}
 
	} else {
 
		t = NULL;
road_map.h
Show inline comments
 
@@ -157,49 +157,49 @@ static inline void TerminateRoadWorks(Ti
 
}
 

	
 

	
 
static inline DiagDirection GetRoadDepotDirection(TileIndex t)
 
{
 
	assert(GetRoadTileType(t) == ROAD_TILE_DEPOT);
 
	return (DiagDirection)GB(_m[t].m5, 0, 2);
 
}
 

	
 

	
 
/**
 
 * Returns the RoadBits on an arbitrary tile
 
 * Special behavior:
 
 * - road depots: entrance is treated as road piece
 
 * - road tunnels: entrance is treated as road piece
 
 * - bridge ramps: start of the ramp is treated as road piece
 
 * - bridge middle parts: bridge itself is ignored
 
 */
 
RoadBits GetAnyRoadBits(TileIndex);
 

	
 

	
 
TrackBits GetAnyRoadTrackBits(TileIndex tile);
 

	
 

	
 
static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, uint town)
 
static inline void MakeRoadNormal(TileIndex t, Owner owner, RoadBits bits, TownID town)
 
{
 
	SetTileType(t, MP_STREET);
 
	SetTileOwner(t, owner);
 
	_m[t].m2 = town;
 
	_m[t].m3 = 0;
 
	_m[t].m4 = 0 << 7 | 0 << 4 | 0;
 
	_m[t].m5 = ROAD_TILE_NORMAL << 4 | bits;
 
}
 

	
 

	
 
static inline void MakeRoadCrossing(TileIndex t, Owner road, Owner rail, Axis roaddir, RailType rt, uint town)
 
{
 
	SetTileType(t, MP_STREET);
 
	SetTileOwner(t, rail);
 
	_m[t].m2 = town;
 
	_m[t].m3 = road;
 
	_m[t].m4 = 0 << 7 | 0 << 4 | rt;
 
	_m[t].m5 = ROAD_TILE_CROSSING << 4 | roaddir << 3 | 0 << 2;
 
}
 

	
 

	
 
static inline void MakeRoadDepot(TileIndex t, Owner owner, DiagDirection dir)
 
{
 
	SetTileType(t, MP_STREET);
tunnelbridge_cmd.c
Show inline comments
 
@@ -687,49 +687,49 @@ static int32 DoClearBridge(TileIndex til
 

	
 
	t = ClosestTownFromTile(tile, (uint)-1); //needed for town rating penalty
 
	// check if you're allowed to remove the bridge owned by a town.
 
	// removal allowal depends on difficulty settings
 
	if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR) {
 
		if (!CheckforTownRating(flags, t, TUNNELBRIDGE_REMOVE)) return CMD_ERROR;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		TileIndex c;
 
		Track track;
 

	
 
		//checks if the owner is town then decrease town rating by RATING_TUNNEL_BRIDGE_DOWN_STEP until
 
		// you have a "Poor" (0) town rating
 
		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
 
			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
 

	
 
		DoClearSquare(tile);
 
		DoClearSquare(endtile);
 
		for (c = tile + delta; c != endtile; c += delta) {
 
			if (IsTransportUnderBridge(c)) {
 
				if (GetTransportTypeUnderBridge(c) == TRANSPORT_RAIL) {
 
					MakeRailNormal(c, GetTileOwner(c), GetRailBitsUnderBridge(c), GetRailType(c));
 
				} else {
 
					uint town = IsTileOwner(c, OWNER_TOWN) ? ClosestTownFromTile(c, (uint)-1)->index : 0;
 
					TownID town = IsTileOwner(c, OWNER_TOWN) ? ClosestTownFromTile(c, (uint)-1)->index : 0;
 
					MakeRoadNormal(c, GetTileOwner(c), GetRoadBitsUnderBridge(c), town);
 
				}
 
				MarkTileDirtyByTile(c);
 
			} else {
 
				if (IsClearUnderBridge(c)) {
 
					DoClearSquare(c);
 
				} else {
 
					if (GetTileSlope(c, NULL) == SLOPE_FLAT) {
 
						if (IsTileOwner(c, OWNER_WATER)) {
 
							MakeWater(c);
 
						} else {
 
							MakeCanal(c, GetTileOwner(c));
 
						}
 
					} else {
 
						MakeShore(c);
 
					}
 
					MarkTileDirtyByTile(c);
 
				}
 
			}
 
		}
 

	
 
		UpdateSignalsOnSegment(tile, ReverseDiagDir(direction));
 
		UpdateSignalsOnSegment(endtile, direction);
 
		track = AxisToTrack(DiagDirToAxis(direction));
0 comments (0 inline, 0 general)