Changeset - r1977:1f8b99c96041
[Not reviewed]
master
! ! !
tron - 19 years ago 2005-06-24 12:38:35
tron@openttd.org
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
59 files changed:
0 comments (0 inline, 0 general)
ai.c
Show inline comments
 
@@ -272,90 +272,93 @@ static void AiRestoreVehicleOrders(Vehic
 
	int i;
 

	
 
	for (i = 0; bak->order[i].type != OT_NOTHING; i++)
 
		if (CmdFailed(DoCommandP(0, v->index + (i << 16), PackOrder(&bak->order[i]), NULL, CMD_INSERT_ORDER | CMD_NO_TEST_IF_IN_NETWORK)))
 
			break;
 
}
 

	
 
static void AiHandleReplaceTrain(Player *p)
 
{
 
	Vehicle *v = p->ai.cur_veh;
 
	BackuppedOrders orderbak[1];
 
	int veh;
 
	uint tile;
 

	
 
	// wait until the vehicle reaches the depot.
 
	if (!IsTileDepotType(v->tile, TRANSPORT_RAIL) || v->u.rail.track != 0x80 || !(v->vehstatus&VS_STOPPED)) {
 
		AiHandleGotoDepot(p, CMD_TRAIN_GOTO_DEPOT);
 
		return;
 
	}
 

	
 
	veh = AiChooseTrainToReplaceWith(p, v);
 
	if (veh != -1) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		tile = v->tile;
 

	
 
		if (!CmdFailed(DoCommandByTile(0, v->index, 2, DC_EXEC, CMD_SELL_RAIL_WAGON)) &&
 
			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_RAIL_VEHICLE)) ) {
 
			veh = _new_train_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_TRAIN);
 

	
 
			DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
 
		}
 
	}
 
}
 

	
 
static void AiHandleReplaceRoadVeh(Player *p)
 
{
 
	Vehicle *v = p->ai.cur_veh;
 
	BackuppedOrders orderbak[1];
 
	int veh;
 
	uint tile;
 

	
 
	if (!IsTileDepotType(v->tile, TRANSPORT_ROAD) || v->u.road.state != 254 || !(v->vehstatus&VS_STOPPED)) {
 
		AiHandleGotoDepot(p, CMD_SEND_ROADVEH_TO_DEPOT);
 
		return;
 
	}
 

	
 
	veh = AiChooseRoadVehToReplaceWith(p, v);
 
	if (veh != -1) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		tile = v->tile;
 

	
 
		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_ROAD_VEH)) &&
 
			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_ROAD_VEH)) ) {
 
			veh = _new_roadveh_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_ROADVEH);
 

	
 
			DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
 
		}
 
	}
 
}
 

	
 
static void AiHandleReplaceAircraft(Player *p)
 
{
 
	Vehicle *v = p->ai.cur_veh;
 
	int veh;
 
	BackuppedOrders orderbak[1];
 
	uint tile;
 

	
 
	if (!IsAircraftHangarTile(v->tile) && !(v->vehstatus&VS_STOPPED)) {
 
		AiHandleGotoDepot(p, CMD_SEND_AIRCRAFT_TO_HANGAR);
 
		return;
 
	}
 

	
 
	veh = AiChooseAircraftToReplaceWith(p, v);
 
	if (veh != -1) {
 
		TileIndex tile;
 

	
 
		BackupVehicleOrders(v, orderbak);
 
		tile = v->tile;
 

	
 
		if (!CmdFailed(DoCommandByTile(0, v->index, 0, DC_EXEC, CMD_SELL_AIRCRAFT)) &&
 
			  !CmdFailed(DoCommandByTile(tile, veh, 0, DC_EXEC, CMD_BUILD_AIRCRAFT)) ) {
 
			veh = _new_aircraft_id;
 
			AiRestoreVehicleOrders(GetVehicle(veh), orderbak);
 
			DoCommandByTile(0, veh, 0, DC_EXEC, CMD_START_STOP_AIRCRAFT);
 

	
 
			DoCommandByTile(0, veh, _ai_service_interval, DC_EXEC, CMD_CHANGE_TRAIN_SERVICE_INT);
 
		}
 
	}
 
@@ -1523,29 +1526,28 @@ static void AiStateWantNewRoute(Player *
 
		// time out?
 
		if (--i == 0) {
 
			if (++p->ai.state_counter == 556) p->ai.state = AIS_0;
 
			break;
 
		}
 
	}
 
}
 

	
 
static bool AiCheckTrackResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
 
{
 
	uint values[NUM_CARGO];
 
	int w,h;
 
	uint tile2;
 
	int rad;
 

	
 
	for(;p->mode != 4;p++) if (p->mode == 1) {
 
		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 
		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 

	
 
		w = ((p->attr>>1) & 7);
 
		h = ((p->attr>>4) & 7);
 
		if (p->attr&1) intswap(w, h);
 

	
 

	
 
		if (_patches.modified_catchment) {
 
			rad = CA_TRAIN;
 
		} else {
 
			rad = 4;
 
		}
 

	
 
@@ -1824,25 +1826,25 @@ static TileIndex AiGetEdgeOfDefaultRailB
 
	while (p->mode != 3 || !((--cmd) & 0x80)) p++;
 

	
 
	return tile + ToTileIndexDiff(p->tileoffs) - TileOffsByDir(*dir = p->attr);
 
}
 

	
 
typedef struct AiRailPathFindData {
 
	TileIndex tile;
 
	TileIndex tile2;
 
	int count;
 
	bool flag;
 
} AiRailPathFindData;
 

	
 
static bool AiEnumFollowTrack(uint tile, AiRailPathFindData *a, int track, uint length, byte *state)
 
static bool AiEnumFollowTrack(TileIndex tile, AiRailPathFindData *a, int track, uint length, byte *state)
 
{
 
	if (a->flag)
 
		return true;
 

	
 
	if (length > 20 || tile == a->tile) {
 
		a->flag = true;
 
		return true;
 
	}
 

	
 
	if (DistanceMax(tile, a->tile2) < 4)
 
		a->count++;
 

	
 
@@ -2166,25 +2168,25 @@ static void AiBuildRailConstruct(Player 
 

	
 
	if (arf.best_tile != 0) {
 
		for(i=0; i!=2; i++)
 
			AiDoTerraformLand(arf.best_tile, arf.best_dir, 3, 0);
 
	}
 
}
 

	
 
static bool AiRemoveTileAndGoForward(Player *p)
 
{
 
	byte b;
 
	int bit;
 
	const byte *ptr;
 
	uint tile = p->ai.cur_tile_a;
 
	TileIndex tile = p->ai.cur_tile_a;
 
	int offs;
 
	TileIndex tilenew;
 

	
 
	if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 
		if (!(_map5[tile] & 0x80)) {
 
			// Clear the tunnel and continue at the other side of it.
 
			if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR)) )
 
				return false;
 
			p->ai.cur_tile_a = TILE_MASK(_build_tunnel_endtile - TileOffsByDir(p->ai.cur_dir_a));
 
			return true;
 
		}
 

	
 
@@ -2485,25 +2487,26 @@ static bool AiCheckRoadResources(TileInd
 
	int rad;
 

	
 
	if (_patches.modified_catchment) {
 
		rad = CA_TRUCK;		//Same as CA_BUS at the moment?
 
	} else {			//change that at some point?
 
		rad = 4;
 
	}
 

	
 
	for(;;p++) {
 
		if (p->mode == 4) {
 
			return true;
 
		} else if (p->mode == 1) {
 
			uint tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 
			TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 

	
 
			if (cargo & 0x80) {
 
				GetProductionAroundTiles(values, tile2, 1, 1, rad);
 
				return values[cargo & 0x7F] != 0;
 
			} else {
 
				GetAcceptanceAroundTiles(values, tile2, 1, 1, rad);
 
				return (values[cargo]&~7) != 0;
 
			}
 
		}
 
	}
 
}
 

	
 
static bool _want_road_truck_station;
 
@@ -2756,53 +2759,53 @@ static bool AiCheckRoadPathBetter(AiRoad
 
			arf->best_tile = 0;
 
			better = true;
 
		}
 
	}
 
	arf->recursive_mode = 0;
 
	arf->cur_best_dist = (uint)-1;
 
	arf->cur_best_depth = 0xff;
 

	
 
	return better;
 
}
 

	
 

	
 
static bool AiEnumFollowRoad(uint tile, AiRoadEnum *a, int track, uint length, byte *state)
 
static bool AiEnumFollowRoad(TileIndex tile, AiRoadEnum *a, int track, uint length, byte *state)
 
{
 
	uint dist = DistanceManhattan(tile, a->dest);
 
	uint tile2;
 

	
 
	if (dist <= a->best_dist) {
 
		tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
 
		TileIndex tile2 = TILE_MASK(tile + TileOffsByDir(_dir_by_track[track]));
 

	
 
		if (IsTileType(tile2, MP_STREET) &&
 
				(_map5[tile2]&0xF0) == 0) {
 
			a->best_dist = dist;
 
			a->best_tile = tile;
 
			a->best_track = track;
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 
static const uint16 _ai_road_table_and[4] = {
 
	0x1009,
 
	0x16,
 
	0x520,
 
	0x2A00,
 
};
 

	
 
static bool AiCheckRoadFinished(Player *p)
 
{
 
	AiRoadEnum are;
 
	uint tile;
 
	TileIndex tile;
 
	int dir = p->ai.cur_dir_a;
 
	uint32 bits;
 
	int i;
 

	
 
	are.dest = p->ai.cur_tile_b;
 
	tile = TILE_MASK(p->ai.cur_tile_a + TileOffsByDir(dir));
 

	
 
	bits = GetTileTrackStatus(tile, TRANSPORT_ROAD) & _ai_road_table_and[dir];
 
	if (bits == 0) {
 
		return false;
 
	}
 

	
 
@@ -2815,25 +2818,25 @@ static bool AiCheckRoadFinished(Player *
 
	if (DistanceManhattan(tile, are.dest) <= are.best_dist)
 
		return false;
 

	
 
	if (are.best_dist == 0)
 
		return true;
 

	
 
	p->ai.cur_tile_a = are.best_tile;
 
	p->ai.cur_dir_a = _dir_by_track[are.best_track];
 
	return false;
 
}
 

	
 

	
 
static bool AiBuildRoadHelper(uint tile, int flags, int type)
 
static bool AiBuildRoadHelper(TileIndex tile, int flags, int type)
 
{
 
	static const byte _road_bits[] = {
 
		8+2,
 
		1+4,
 
		1+8,
 
		4+2,
 
		1+2,
 
		8+4,
 
	};
 
	return !CmdFailed(DoCommandByTile(tile, _road_bits[type], 0, flags, CMD_BUILD_ROAD));
 
}
 

	
 
@@ -2953,25 +2956,25 @@ static void AiBuildRoadRecursive(AiRoadF
 
	AiCheckBuildRoadTunnelHere(arf, tile, p+1);
 

	
 
	arf->depth--;
 
}
 

	
 
int sw;
 

	
 

	
 
static void AiBuildRoadConstruct(Player *p)
 
{
 
	AiRoadFinder arf;
 
	int i;
 
	uint tile;
 
	TileIndex tile;
 

	
 
	// Reached destination?
 
	if (AiCheckRoadFinished(p)) {
 
		p->ai.state_mode = 255;
 
		return;
 
	}
 

	
 
	// Setup recursive finder and call it.
 
	arf.player = p;
 
	arf.final_tile = p->ai.cur_tile_b;
 
	arf.final_dir = p->ai.cur_dir_b;
 
	arf.depth = 0;
 
@@ -3155,25 +3158,26 @@ static void AiStateBuildRoad(Player *p)
 
}
 

	
 
static int AiGetStationIdFromRoadBlock(TileIndex tile, int id)
 
{
 
	const AiDefaultBlockData *p = _road_default_block_data[id]->data;
 
	while (p->mode != 1) p++;
 
	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
 
}
 

	
 
static void AiStateBuildRoadVehicles(Player *p)
 
{
 
	const AiDefaultBlockData *ptr;
 
	uint tile,loco_id;
 
	TileIndex tile;
 
	uint loco_id;
 
	int veh, i;
 

	
 
	ptr = _road_default_block_data[p->ai.src.cur_building_rule]->data;
 
	for(;ptr->mode != 0;ptr++) {}
 
	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
 

	
 
	veh = AiChooseRoadVehToBuild(p->ai.cargo_type, p->player_money, tile);
 
	if (veh == -1) {
 
		p->ai.state = AIS_0;
 
		return;
 
	}
 

	
 
@@ -3227,25 +3231,26 @@ static void AiStateDeleteRoadBlocks(Play
 
					DoCommandByTile(TILE_ADD(aib->use_tile, ToTileIndexDiff(b->tileoffs)), 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 
				}
 
				b++;
 
			}
 
		}
 
	} while (++aib,--num);
 

	
 
	p->ai.state = AIS_0;
 
}
 

	
 
static bool AiCheckIfHangar(Station *st)
 
{
 
	uint tile = st->airport_tile;
 
	TileIndex tile = st->airport_tile;
 

	
 
	// HANGAR of airports
 
	// 0x20 - hangar large airport (32)
 
	// 0x41 - hangar small airport (65)
 
	return (_map5[tile] == 32 || _map5[tile] == 65);
 
}
 

	
 
static void AiStateAirportStuff(Player *p)
 
{
 
	Station *st;
 
	byte acc_planes;
 
	int i;
 
	AiBuildRec *aib;
 
@@ -3339,35 +3344,35 @@ static int32 AiDoBuildDefaultAirportBloc
 
		ret = DoCommandByTile(TILE_MASK(tile + ToTileIndexDiff(p->tileoffs)), p->attr,0,flag | DC_AUTO | DC_NO_WATER,CMD_BUILD_AIRPORT);
 
		if (CmdFailed(ret)) return CMD_ERROR;
 
		total_cost += ret;
 
	}
 

	
 
	return total_cost;
 
}
 

	
 
static bool AiCheckAirportResources(TileIndex tile, const AiDefaultBlockData *p, byte cargo)
 
{
 
	uint values[NUM_CARGO];
 
	int w,h;
 
	uint tile2;
 
	int rad;
 

	
 
	if (_patches.modified_catchment) {
 
		rad = CA_AIR_LARGE;		//I Have NFI what airport the
 
	} else {				//AI is going to build here
 
		rad = 4;
 
	}
 

	
 
	for(;p->mode==0;p++) {
 
		tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 
		TileIndex tile2 = TILE_ADD(tile, ToTileIndexDiff(p->tileoffs));
 

	
 
		w = _airport_size_x[p->attr];
 
		h = _airport_size_y[p->attr];
 
		if (cargo & 0x80) {
 
			GetProductionAroundTiles(values, tile2, w, h, rad);
 
			return values[cargo & 0x7F] != 0;
 
		} else {
 
			GetAcceptanceAroundTiles(values, tile2, w, h, rad);
 
			return values[cargo] >= 8;
 
		}
 
	}
 
	return true;
 
}
 
@@ -3472,25 +3477,25 @@ static void AiStateBuildDefaultAirportBl
 
}
 

	
 
static int AiGetStationIdFromAircraftBlock(TileIndex tile, int id)
 
{
 
	const AiDefaultBlockData *p = _airport_default_block_data[id];
 
	while (p->mode != 1) p++;
 
	return _map2[TILE_ADD(tile, ToTileIndexDiff(p->tileoffs))];
 
}
 

	
 
static void AiStateBuildAircraftVehicles(Player *p)
 
{
 
	const AiDefaultBlockData *ptr;
 
	uint tile;
 
	TileIndex tile;
 
	int veh;
 
	int i;
 
	uint loco_id;
 

	
 
	ptr = _airport_default_block_data[p->ai.src.cur_building_rule];
 
	for(;ptr->mode!=0;ptr++) {}
 

	
 
	tile = TILE_ADD(p->ai.src.use_tile, ToTileIndexDiff(ptr->tileoffs));
 

	
 
	veh = AiChooseAircraftToBuild(p->player_money, p->ai.build_kind!=0 ? 1 : 0);
 
	if (veh == -1) {
 
		return;
ai.h
Show inline comments
 
@@ -231,29 +231,29 @@ enum {
 
#define AI_PATHFINDER_FLAG_TUNNEL 2
 

	
 
typedef void AiNew_StateFunction(Player *p);
 

	
 
// ai_new.c
 
void AiNewDoGameLoop(Player *p);
 

	
 
// ai_pathfinder.c
 
AyStar *new_AyStar_AiPathFinder(int max_tiles_around, Ai_PathFinderInfo *PathFinderInfo);
 
void clean_AyStar_AiPathFinder(AyStar *aystar, Ai_PathFinderInfo *PathFinderInfo);
 

	
 
// ai_shared.c
 
int AiNew_GetRailDirection(uint tile_a, uint tile_b, uint tile_c);
 
int AiNew_GetRoadDirection(uint tile_a, uint tile_b, uint tile_c);
 
int AiNew_GetDirection(uint tile_a, uint tile_b);
 
int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c);
 
int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c);
 
int AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b);
 
bool AiNew_SetSpecialVehicleFlag(Player *p, Vehicle *v, uint flag);
 
uint AiNew_GetSpecialVehicleFlag(Player *p, Vehicle *v);
 

	
 
// ai_build.c
 
bool AiNew_Build_CompanyHQ(Player *p, uint tile);
 
int AiNew_Build_Station(Player *p, byte type, uint tile, byte length, byte numtracks, byte direction, byte flag);
 
int AiNew_Build_Bridge(Player *p, uint tile_a, uint tile_b, byte flag);
 
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile);
 
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag);
 
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag);
 
int AiNew_Build_RoutePart(Player *p, Ai_PathFinderInfo *PathFinderInfo, byte flag);
 
int AiNew_PickVehicle(Player *p);
 
int AiNew_Build_Vehicle(Player *p, uint tile, byte flag);
 
int AiNew_Build_Depot(Player *p, uint tile, byte direction, byte flag);
 
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag);
 
int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag);
 

	
 

	
 
#endif
ai_build.c
Show inline comments
 
@@ -2,55 +2,58 @@
 
#include "openttd.h"
 
#include "debug.h"
 
#include "map.h"
 
#include "tile.h"
 
#include "command.h"
 
#include "ai.h"
 
#include "engine.h"
 
#include "station.h"
 

	
 
// Build HQ
 
//  Params:
 
//    tile : tile where HQ is going to be build
 
bool AiNew_Build_CompanyHQ(Player *p, uint tile) {
 
bool AiNew_Build_CompanyHQ(Player *p, TileIndex tile)
 
{
 
	if (CmdFailed(DoCommandByTile(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ)))
 
		return false;
 
	DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_COMPANY_HQ);
 
	return true;
 
}
 

	
 
// Build station
 
//  Params:
 
//    type : AI_TRAIN/AI_BUS/AI_TRUCK : indicates the type of station
 
//    tile : tile where station is going to be build
 
//    length : in case of AI_TRAIN: length of station
 
//    numtracks : in case of AI_TRAIN: tracks of station
 
//    direction : the direction of the station
 
//    flag : flag passed to DoCommand (normally 0 to get the cost or DC_EXEC to build it)
 
int AiNew_Build_Station(Player *p, byte type, uint tile, byte length, byte numtracks, byte direction, byte flag) {
 
int AiNew_Build_Station(Player *p, byte type, TileIndex tile, byte length, byte numtracks, byte direction, byte flag)
 
{
 
	if (type == AI_TRAIN)
 
		return DoCommandByTile(tile, direction + (numtracks << 8) + (length << 16), 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_RAILROAD_STATION);
 

	
 
	if (type == AI_BUS)
 
		return DoCommandByTile(tile, direction, RS_BUS, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
 

	
 
	return DoCommandByTile(tile, direction, RS_TRUCK, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_STOP);
 
}
 

	
 
// Builds a brdige. The second best out of the ones available for this player
 
//  Params:
 
//   tile_a : starting point
 
//   tile_b : end point
 
//   flag : flag passed to DoCommand
 
int AiNew_Build_Bridge(Player *p, uint tile_a, uint tile_b, byte flag) {
 
int AiNew_Build_Bridge(Player *p, TileIndex tile_a, TileIndex tile_b, byte flag)
 
{
 
	int bridge_type, bridge_len, type, type2;
 

	
 
	// Find a good bridgetype (the best money can buy)
 
	bridge_len = GetBridgeLength(tile_a, tile_b);
 
	type = type2 = 0;
 
	for (bridge_type = MAX_BRIDGES-1; bridge_type >= 0; bridge_type--) {
 
		if (CheckBridge_Stuff(bridge_type, bridge_len)) {
 
			type2 = type;
 
			type = bridge_type;
 
			// We found two bridges, exit
 
			if (type2 != 0)
 
				break;
 
@@ -224,35 +227,36 @@ int AiNew_PickVehicle(Player *p) {
 
        	if (!HASBIT(GetEngine(i)->player_avail, _current_player) || GetEngine(i)->reliability * 100 < AI_VEHICLE_MIN_RELIABILTY << 16) continue;
 
        	// Can we build it?
 
        	ret = DoCommandByTile(0, i, 0, DC_QUERY_COST, CMD_BUILD_ROAD_VEH);
 
        	if (!CmdFailed(ret)) break;
 
       	}
 
       	// We did not find a vehicle :(
 
       	if (CmdFailed(ret)) { return -1; }
 
       	return i;
 
    }
 
}
 

	
 
// Builds the best vehicle possible
 
int AiNew_Build_Vehicle(Player *p, uint tile, byte flag) {
 
int AiNew_Build_Vehicle(Player *p, TileIndex tile, byte flag)
 
{
 
	int i = AiNew_PickVehicle(p);
 
	if (i == -1) return CMD_ERROR;
 

	
 
	if (p->ainew.tbt == AI_TRAIN)
 
		return CMD_ERROR;
 

	
 
	return DoCommandByTile(tile, i, 0, flag, CMD_BUILD_ROAD_VEH);
 
}
 

	
 
int AiNew_Build_Depot(Player *p, uint tile, byte direction, byte flag)
 
int AiNew_Build_Depot(Player *p, TileIndex tile, byte direction, byte flag)
 
{
 
	static const byte _roadbits_by_dir[4] = {2,1,8,4};
 
	int ret, ret2;
 
    if (p->ainew.tbt == AI_TRAIN)
 
    	return DoCommandByTile(tile, 0, direction, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_TRAIN_DEPOT);
 

	
 
		ret = DoCommandByTile(tile, direction, 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD_DEPOT);
 
		if (CmdFailed(ret)) return ret;
 
		// Try to build the road from the depot
 
		ret2 = DoCommandByTile(tile + TileOffsByDir(direction), _roadbits_by_dir[direction], 0, flag | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD);
 
		// If it fails, ignore it..
 
		if (CmdFailed(ret2)) return ret;
ai_pathfinder.c
Show inline comments
 
@@ -2,25 +2,25 @@
 
#include "openttd.h"
 
#include "debug.h"
 
#include "map.h"
 
#include "tile.h"
 
#include "command.h"
 
#include "ai.h"
 
#include "depot.h"
 

	
 
#define TEST_STATION_NO_DIR 0xFF
 

	
 
// Tests if a station can be build on the given spot
 
// TODO: make it train compatible
 
static bool TestCanBuildStationHere(uint tile, byte dir)
 
static bool TestCanBuildStationHere(TileIndex tile, byte dir)
 
{
 
	Player *p = GetPlayer(_current_player);
 

	
 
	if (dir == TEST_STATION_NO_DIR) {
 
		int32 ret;
 
		// TODO: currently we only allow spots that can be access from al 4 directions...
 
		//  should be fixed!!!
 
		for (dir = 0; dir < 4; dir++) {
 
			ret = AiNew_Build_Station(p, p->ainew.tbt, tile, 1, 1, dir, DC_QUERY_COST);
 
			if (!CmdFailed(ret)) return true;
 
		}
 
		return false;
ai_shared.c
Show inline comments
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "debug.h"
 
#include "map.h"
 
#include "ai.h"
 
#include "vehicle.h"
 

	
 
int AiNew_GetRailDirection(uint tile_a, uint tile_b, uint tile_c) {
 
int AiNew_GetRailDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
 
{
 
	// 0 = vert
 
	// 1 = horz
 
	// 2 = dig up-left
 
	// 3 = dig down-right
 
	// 4 = dig down-left
 
	// 5 = dig up-right
 

	
 
	int x1, x2, x3;
 
	int y1, y2, y3;
 

	
 
	x1 = TileX(tile_a);
 
	x2 = TileX(tile_b);
 
@@ -37,25 +38,26 @@ int AiNew_GetRailDirection(uint tile_a, 
 
	if (y1 > y2) {
 
		if (x2 > x3) return 5;
 
		else return 3;
 
	}
 
	if (x1 > x2) {
 
		if (y2 > y3) return 4;
 
		else return 3;
 
	}
 

	
 
	return 0;
 
}
 

	
 
int AiNew_GetRoadDirection(uint tile_a, uint tile_b, uint tile_c) {
 
int AiNew_GetRoadDirection(TileIndex tile_a, TileIndex tile_b, TileIndex tile_c)
 
{
 
	int x1, x2, x3;
 
	int y1, y2, y3;
 
	int r;
 

	
 
	x1 = TileX(tile_a);
 
	x2 = TileX(tile_b);
 
	x3 = TileX(tile_c);
 

	
 
	y1 = TileY(tile_a);
 
	y2 = TileY(tile_b);
 
	y3 = TileY(tile_c);
 

	
 
@@ -66,25 +68,26 @@ int AiNew_GetRoadDirection(uint tile_a, 
 
	if (x1 > x2) r += 2;
 
	if (y1 > y2) r += 4;
 

	
 
	if (x2 < x3) r += 2;
 
	if (y2 < y3) r += 4;
 
	if (x2 > x3) r += 8;
 
	if (y2 > y3) r += 1;
 

	
 
	return r;
 
}
 

	
 
// Get's the direction between 2 tiles seen from tile_a
 
int AiNew_GetDirection(uint tile_a, uint tile_b) {
 
int AiNew_GetDirection(TileIndex tile_a, TileIndex tile_b)
 
{
 
	if (TileY(tile_a) < TileY(tile_b)) return 1;
 
	if (TileY(tile_a) > TileY(tile_b)) return 3;
 
	if (TileX(tile_a) < TileX(tile_b)) return 2;
 
	return 0;
 
}
 

	
 
// This functions looks up if this vehicle is special for this AI
 
//  and returns his flag
 
uint AiNew_GetSpecialVehicleFlag(Player *p, Vehicle *v) {
 
	int i;
 
	for (i=0;i<AI_MAX_SPECIAL_VEHICLES;i++) {
 
		if (p->ainew.special_vehicles[i].veh_id == v->index) {
aircraft_cmd.c
Show inline comments
 
@@ -835,25 +835,26 @@ static bool AircraftController(Vehicle *
 
	Station *st;
 
	const AirportMovingData *amd;
 
	Vehicle *u;
 
	byte z,dirdiff,newdir,maxz,curz;
 
	GetNewVehiclePosResult gp;
 
	uint dist;
 
	int x,y;
 

	
 
	st = GetStation(v->u.air.targetairport);
 

	
 
	// prevent going to 0,0 if airport is deleted.
 
	{
 
		uint tile = st->airport_tile;
 
		TileIndex tile = st->airport_tile;
 

	
 
		if (tile == 0) tile = st->xy;
 
		// xy of destination
 
		x = TileX(tile) * 16;
 
		y = TileY(tile) * 16;
 
	}
 

	
 
	// get airport moving data
 
	assert(v->u.air.pos < GetAirport(st->airport_type)->nofelements);
 
	amd = &_airport_moving_datas[st->airport_type][v->u.air.pos];
 

	
 
	// Helicopter raise
 
	if (amd->flag & AMED_HELI_RAISE) {
aircraft_gui.c
Show inline comments
 
@@ -62,25 +62,25 @@ static void DrawAircraftImage(const Vehi
 
	int image = GetAircraftImage(v, 6);
 
	uint32 ormod = SPRITE_PALETTE(PLAYER_SPRITE_COLOR(v->owner));
 
	if (v->vehstatus & VS_CRASHED)
 
		ormod = PALETTE_CRASH;
 
	DrawSprite(image | ormod, x+25, y+10);
 
	if (v->subtype == 0)
 
		DrawSprite(0xF3D, x+25, y+5);
 
	if (v->index == selection) {
 
		DrawFrameRect(x-1, y-1, x+58, y+21, 0xF, FR_BORDERONLY);
 
	}
 
}
 

	
 
void CcBuildAircraft(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildAircraft(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (success) {
 
		v = GetVehicle(_new_aircraft_id);
 
		if (v->tile == _backup_orders_tile) {
 
			_backup_orders_tile = 0;
 
			RestoreVehicleOrders(v, _backup_orders_data);
 
		}
 
		ShowAircraftViewWindow(v);
 
	}
 
}
 
@@ -197,25 +197,25 @@ static const Widget _new_aircraft_widget
 
{  WWT_RESIZEBOX,     RESIZE_TB,    14,   228,   239,   162,   173, 0x0,											STR_RESIZE_BUTTON},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _new_aircraft_desc = {
 
	-1, -1, 240, 174,
 
	WC_BUILD_VEHICLE,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
 
	_new_aircraft_widgets,
 
	NewAircraftWndProc
 
};
 

	
 
static void ShowBuildAircraftWindow(uint tile)
 
static void ShowBuildAircraftWindow(TileIndex tile)
 
{
 
	Window *w;
 

	
 
	DeleteWindowById(WC_BUILD_VEHICLE, tile);
 

	
 
	w = AllocateWindowDesc(&_new_aircraft_desc);
 
	w->window_number = tile;
 
	w->vscroll.cap = 4;
 
	w->widget[2].unkA = (w->vscroll.cap << 8) + 1;
 

	
 
	w->resize.step_height = 24;
 

	
 
@@ -497,25 +497,26 @@ static const Widget _aircraft_view_widge
 
{ WIDGETS_END }
 
};
 

	
 
static void AircraftViewWndProc(Window *w, WindowEvent *e)
 
{
 
	switch(e->event) {
 
	case WE_PAINT: {
 
		Vehicle *v = GetVehicle(w->window_number);
 
		uint32 disabled = 1<<8;
 
		StringID str;
 

	
 
		{
 
			uint tile = v->tile;
 
			TileIndex tile = v->tile;
 

	
 
			if (IsTileType(tile, MP_STATION) &&
 
					(_map5[tile] == 32 || _map5[tile] == 65) &&
 
					v->vehstatus&VS_STOPPED)
 
						disabled = 0;
 
		}
 

	
 
		if (v->owner != _local_player)
 
			disabled |= 1<<8 | 1<<7;
 
		w->disabled_state = disabled;
 

	
 
		/* draw widgets & caption */
 
		SetDParam(0, v->string_id);
 
@@ -617,25 +618,25 @@ void ShowAircraftViewWindow(Vehicle *v)
 
{
 
	Window *w;
 

	
 
	w = AllocateWindowDescFront(&_aircraft_view_desc, v->index);
 
	if (w) {
 
		w->caption_color = v->owner;
 
		AssignWindowViewport(w, 3, 17, 0xE2, 0x54, w->window_number | (1 << 31), 0);
 
	}
 
}
 

	
 
static void DrawAircraftDepotWindow(Window *w)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	Vehicle *v;
 
	int num,x,y;
 

	
 
	tile = w->window_number;
 

	
 
	/* setup disabled buttons */
 
	w->disabled_state =
 
		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 7));
 

	
 
	/* determine amount of items for scroller */
 
	num = 0;
 
	FOR_ALL_VEHICLES(v) {
 
@@ -668,25 +669,25 @@ static void DrawAircraftDepotWindow(Wind
 

	
 
			if ((x+=74) == 2 + 74 * w->hscroll.cap) {
 
				x = 2;
 
				y += 24;
 
			}
 
		}
 
	}
 
}
 

	
 
static int GetVehicleFromAircraftDepotWndPt(Window *w, int x, int y, Vehicle **veh) {
 
	uint xt,row,xm,ym;
 
	Vehicle *v;
 
	uint tile;
 
	TileIndex tile;
 
	int pos;
 

	
 
	xt = x / 74;
 
	xm = x % 74;
 
	if (xt >= w->hscroll.cap)
 
		return 1;
 

	
 
	row = (y - 14) / 24;
 
	ym = (y - 14) % 24;
 
	if (row >= w->vscroll.cap)
 
		return 1;
 

	
 
@@ -835,25 +836,25 @@ static const Widget _aircraft_depot_widg
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _aircraft_depot_desc = {
 
	-1, -1, 331, 74,
 
	WC_VEHICLE_DEPOT,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
 
	_aircraft_depot_widgets,
 
	AircraftDepotWndProc
 
};
 

	
 

	
 
void ShowAircraftDepotWindow(uint tile)
 
void ShowAircraftDepotWindow(TileIndex tile)
 
{
 
	Window *w;
 

	
 
	w = AllocateWindowDescFront(&_aircraft_depot_desc, tile);
 
	if (w) {
 
		w->caption_color = GetTileOwner(tile);
 
		w->vscroll.cap = 2;
 
		w->hscroll.cap = 4;
 
		w->resize.step_width = 74;
 
		w->resize.step_height = 24;
 
		WP(w,traindepot_d).sel = INVALID_VEHICLE;
 
		_backup_orders_tile = 0;
 
@@ -1019,25 +1020,25 @@ static void PlayerAircraftWndProc(Window
 

	
 
				if (id_v >= vl->list_length) return; // click out of list bound
 

	
 
				v = GetVehicle(vl->sort_list[id_v].index);
 

	
 
				assert(v->type == VEH_Aircraft && v->subtype <= 2);
 

	
 
				ShowAircraftViewWindow(v);
 
			}
 
		} break;
 

	
 
		case 9: { /* Build new Vehicle */
 
			uint tile;
 
			TileIndex tile;
 

	
 
			if (!IsWindowOfPrototype(w, _player_aircraft_widgets))
 
				break;
 

	
 
			tile = _last_built_aircraft_depot_tile;
 
			do {
 
				if (IsAircraftHangarTile(tile) && IsTileOwner(tile, _local_player)) {
 
					ShowAircraftDepotWindow(tile);
 
					ShowBuildAircraftWindow(tile);
 
					return;
 
				}
 

	
airport_gui.c
Show inline comments
 
@@ -10,38 +10,38 @@
 
#include "sound.h"
 
#include "command.h"
 
#include "vehicle.h"
 
#include "station.h"
 
#include "airport.h"
 
#include "depot.h"
 

	
 
static byte _selected_airport_type;
 

	
 
static void ShowBuildAirportPicker(void);
 

	
 

	
 
void CcBuildAirport(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildAirport(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
		ResetObjectToPlace();
 
	}
 
}
 

	
 
static void PlaceAirport(uint tile)
 
static void PlaceAirport(TileIndex tile)
 
{
 
	DoCommandP(tile, _selected_airport_type, 0, CcBuildAirport, CMD_BUILD_AIRPORT | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_A001_CAN_T_BUILD_AIRPORT_HERE));
 
}
 

	
 
static void PlaceAir_DemolishArea(uint tile)
 
static void PlaceAir_DemolishArea(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, 4);
 
}
 

	
 

	
 
static void BuildAirClick_Airport(Window *w)
 
{
 
	if (HandlePlacePushButton(w, 3, SPR_CURSOR_AIRPORT, 1, PlaceAirport)) ShowBuildAirportPicker();
 
}
 

	
 
static void BuildAirClick_Demolish(Window *w)
 
{
aystar.h
Show inline comments
 
@@ -18,25 +18,25 @@ enum {
 
	AYSTAR_STILL_BUSY,
 
	AYSTAR_NO_PATH,
 
	AYSTAR_LIMIT_REACHED,
 
	AYSTAR_DONE
 
};
 

	
 
enum{
 
	AYSTAR_INVALID_NODE = -1,
 
};
 

	
 
typedef struct AyStarNode AyStarNode;
 
struct AyStarNode {
 
	uint tile;
 
	TileIndex tile;
 
	uint direction;
 
	uint user_data[2];
 
};
 

	
 
// The resulting path has nodes looking like this.
 
typedef struct PathNode PathNode;
 
struct PathNode {
 
	AyStarNode node;
 
	// The parent of this item
 
	PathNode *parent;
 
};
 

	
bridge_gui.c
Show inline comments
 
@@ -15,25 +15,25 @@ static struct BridgeData {
 
	TileIndex end_tile;
 
	byte type;
 
	byte indexes[MAX_BRIDGES];
 
	int32 costs[MAX_BRIDGES];
 
} _bridge;
 

	
 
extern const uint16 _bridge_type_price_mod[MAX_BRIDGES];
 

	
 
extern const PalSpriteID _bridge_sprites[MAX_BRIDGES];
 
extern const uint16 _bridge_speeds[MAX_BRIDGES];
 
extern const StringID _bridge_material[MAX_BRIDGES];
 

	
 
void CcBuildBridge(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
 
}
 

	
 
static void BuildBridge(Window *w, int i)
 
{
 
	DeleteWindow(w);
 
	DoCommandP(_bridge.end_tile, _bridge.start_tile, _bridge.indexes[i] | (_bridge.type << 8), CcBuildBridge,
 
		CMD_BUILD_BRIDGE | CMD_AUTO | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
 
}
 

	
 
static void BuildBridgeWndProc(Window *w, WindowEvent *e)
 
@@ -101,25 +101,25 @@ static const Widget _build_road_bridge_w
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _build_road_bridge_desc = {
 
	-1, -1, 200, 102,
 
	WC_BUILD_BRIDGE,WC_BUILD_TOOLBAR,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
 
	_build_road_bridge_widgets,
 
	BuildBridgeWndProc
 
};
 

	
 

	
 
void ShowBuildBridgeWindow(uint start, uint end, byte bridge_type)
 
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type)
 
{
 
	int j = 0;
 
	int32 ret;
 
	uint16 errmsg;
 

	
 
	DeleteWindowById(WC_BUILD_BRIDGE, 0);
 

	
 
	_bridge.type = bridge_type;
 
	_bridge.start_tile = start;
 
	_bridge.end_tile = end;
 

	
 
	errmsg = 0xFFFF;
clear_cmd.c
Show inline comments
 
@@ -73,25 +73,25 @@ static void TerraformAddDirtyTile(Terraf
 

	
 
	ts->tile_table[ts->tile_table_count++] = tile;
 
}
 

	
 
static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
 
{
 
	TerraformAddDirtyTile(ts, tile+TILE_XY(0,-1));
 
	TerraformAddDirtyTile(ts, tile+TILE_XY(-1,-1));
 
	TerraformAddDirtyTile(ts, tile+TILE_XY(-1,0));
 
	TerraformAddDirtyTile(ts, tile);
 
}
 

	
 
static int TerraformProc(TerraformerState *ts, uint tile, int mode)
 
static int TerraformProc(TerraformerState *ts, TileIndex tile, int mode)
 
{
 
	int r;
 
	int32 ret;
 

	
 
	assert(tile < MapSize());
 

	
 
	if ((r=TerraformAllowTileProcess(ts, tile)) <= 0)
 
		return r;
 

	
 
	if (IsTileType(tile, MP_RAILWAY)) {
 
		static const byte _railway_modes[4] = {8, 0x10, 4, 0x20};
 
		static const byte _railway_dangslopes[4] = {0xd, 0xe, 7, 0xb};
 
@@ -121,25 +121,25 @@ static int TerraformProc(TerraformerStat
 
		return -1;
 
	}
 

	
 
	ts->cost += ret;
 

	
 
	if (ts->tile_table_count >= 625)
 
		return -1;
 
	ts->tile_table[ts->tile_table_count++] = tile;
 

	
 
	return 0;
 
}
 

	
 
static bool TerraformTileHeight(TerraformerState *ts, uint tile, int height)
 
static bool TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
 
{
 
	int nh;
 
	TerraformerHeightMod *mod;
 
	int count;
 

	
 
	assert(tile < MapSize());
 

	
 
	if (height < 0) {
 
		_error_message = STR_1003_ALREADY_AT_SEA_LEVEL;
 
		return false;
 
	}
 

	
 
@@ -263,25 +263,25 @@ int32 CmdTerraformLand(int x, int y, uin
 
		if (!TerraformTileHeight(&ts, tile+TILE_XY(0,0),
 
				TileHeight(tile + TILE_XY(0, 0)) + direction))
 
					return CMD_ERROR;
 
	}
 

	
 
	if (direction == -1) {
 
		/* Check if tunnel would take damage */
 
		int count;
 
		TileIndex *ti = ts.tile_table;
 

	
 
		for (count = ts.tile_table_count; count != 0; count--, ti++) {
 
			uint z, t;
 
			uint tile = *ti;
 
			TileIndex tile = *ti;
 

	
 
			z = TerraformGetHeightOfTile(&ts, tile + TILE_XY(0,0));
 
			t = TerraformGetHeightOfTile(&ts, tile + TILE_XY(1,0));
 
			if (t <= z) z = t;
 
			t = TerraformGetHeightOfTile(&ts, tile + TILE_XY(1,1));
 
			if (t <= z) z = t;
 
			t = TerraformGetHeightOfTile(&ts, tile + TILE_XY(0,1));
 
			if (t <= z) z = t;
 

	
 
			if (!CheckTunnelInWay(tile, z*8))
 
				return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
 
		}
 
@@ -408,25 +408,25 @@ int32 CmdPurchaseLandArea(int x, int y, 
 

	
 
	if (flags & DC_EXEC) {
 
		ModifyTile(tile,
 
			MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5,
 
			3 /* map5 */
 
			);
 
	}
 

	
 
	return cost + _price.purchase_land * 10;
 
}
 

	
 

	
 
static int32 ClearTile_Clear(uint tile, byte flags)
 
static int32 ClearTile_Clear(TileIndex tile, byte flags)
 
{
 
	static const int32 * _clear_price_table[] = {
 
			NULL,
 
			&_price.clear_1, &_price.clear_1,&_price.clear_1,
 
			&_price.purchase_land,&_price.purchase_land,&_price.purchase_land,&_price.purchase_land,
 
			&_price.clear_2,&_price.clear_2,&_price.clear_2,&_price.clear_2,
 
			&_price.clear_3,&_price.clear_3,&_price.clear_3,&_price.clear_3,
 
			&_price.purchase_land,&_price.purchase_land,&_price.purchase_land,&_price.purchase_land,
 
			&_price.purchase_land,&_price.purchase_land,&_price.purchase_land,&_price.purchase_land,
 
			&_price.clear_2,&_price.clear_2,&_price.clear_2,&_price.clear_2,
 
	};
 
	const int32 *price = _clear_price_table[_map5[tile] & 0x1F];
 
@@ -535,30 +535,30 @@ static void DrawTile_Clear(TileInfo *ti)
 
}
 

	
 
static uint GetSlopeZ_Clear(TileInfo *ti)
 
{
 
	return GetPartialZ(ti->x & 0xF, ti->y & 0xF, ti->tileh) + ti->z;
 
}
 

	
 
static uint GetSlopeTileh_Clear(TileInfo *ti)
 
{
 
	return ti->tileh;
 
}
 

	
 
static void GetAcceptedCargo_Clear(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Clear(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* unused */
 
}
 

	
 
static void AnimateTile_Clear(uint tile)
 
static void AnimateTile_Clear(TileIndex tile)
 
{
 
	/* unused */
 
}
 

	
 
void TileLoopClearHelper(TileIndex tile)
 
{
 
	byte img_1;
 
	byte img_2;
 
	static const byte img_by_map5[] = { 0, 0, 0, 2, 1, 1, 0, 0 };
 
	TileIndex dirty = INVALID_TILE;
 

	
 
	switch (GetTileType(tile)) {
 
@@ -633,25 +633,25 @@ void TileLoopClearHelper(TileIndex tile)
 
		if (img_1 == 1 && img_2 == 1) {
 
			_map3_hi[tile] &= ~(3 << 2);
 
			dirty = tile;
 
		}
 
	}
 

	
 
	if (dirty != INVALID_TILE)
 
		MarkTileDirtyByTile(dirty);
 
}
 

	
 

	
 
/* convert into snowy tiles */
 
static void TileLoopClearAlps(uint tile)
 
static void TileLoopClearAlps(TileIndex tile)
 
{
 
	int k;
 
	byte m5,tmp;
 

	
 
	/* distance from snow line, in steps of 8 */
 
	k = GetTileZ(tile) - _opt.snow_line;
 

	
 
	m5 = _map5[tile] & 0x1C;
 
	tmp = _map5[tile] & 3;
 

	
 
	if (k < -8) {
 
		/* snow_m2_down */
 
@@ -694,44 +694,44 @@ static void TileLoopClearAlps(uint tile)
 
		if (m5 != 0x10) {
 
			m5 = 0x10;
 
		} else if (tmp != 3) {
 
			m5 = tmp + 1 + 0x10;
 
		} else
 
			return;
 
	}
 

	
 
	_map5[tile] = m5;
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
static void TileLoopClearDesert(uint tile)
 
static void TileLoopClearDesert(TileIndex tile)
 
{
 
 	if ( (_map5[tile] & 0x1C) == 0x14)
 
		return;
 

	
 
	if (GetMapExtraBits(tile) == 1) {
 
		_map5[tile] = 0x17;
 
	} else {
 
		if (GetMapExtraBits(tile+TILE_XY(1,0)) != 1 &&
 
				GetMapExtraBits(tile+TILE_XY(-1,0)) != 1 &&
 
				GetMapExtraBits(tile+TILE_XY(0,1)) != 1 &&
 
				GetMapExtraBits(tile+TILE_XY(0,-1)) != 1)
 
					return;
 
		_map5[tile] = 0x15;
 
	}
 

	
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
static void TileLoop_Clear(uint tile)
 
static void TileLoop_Clear(TileIndex tile)
 
{
 
	byte m5,m3;
 

	
 
	TileLoopClearHelper(tile);
 

	
 
	if (_opt.landscape == LT_DESERT) {
 
		TileLoopClearDesert(tile);
 
	} else if (_opt.landscape == LT_HILLY) {
 
		TileLoopClearAlps(tile);
 
	}
 

	
 
	m5 = _map5[tile];
 
@@ -768,89 +768,91 @@ static void TileLoop_Clear(uint tile)
 
		if ( (m3 & 0xF) >= 9) /* NOTE: will not work properly if m3&0xF == 0xF */
 
			m3 &= ~0xF;
 
		_map3_lo[tile] = m3;
 
	}
 

	
 
	_map5[tile] = m5;
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
void GenerateClearTile(void)
 
{
 
	int i,j;
 
	uint tile,tile_new;
 
	TileIndex tile;
 
	uint32 r;
 

	
 
	/* add hills */
 
	i = ScaleByMapSize((Random() & 0x3FF) + 0x400);
 
	do {
 
		tile = TILE_MASK(Random());
 
		if (IsTileType(tile, MP_CLEAR))
 
			_map5[tile] = (byte)((_map5[tile] & ~(3<<2)) | (1<<2));
 
	} while (--i);
 

	
 
	/* add grey squares */
 
	i = ScaleByMapSize((Random() & 0x7F) + 0x80);
 
	do {
 
		r = Random();
 
		tile = TILE_MASK(r);
 
		if (IsTileType(tile, MP_CLEAR)) {
 
			j = ((r >> 16) & 0xF) + 5;
 
			for(;;) {
 
				TileIndex tile_new;
 

	
 
				_map5[tile] = (byte)((_map5[tile] & ~(3<<2)) | (2<<2));
 
				do {
 
					if (--j == 0) goto get_out;
 
					tile_new = tile + TileOffsByDir(Random() & 3);
 
				} while (!IsTileType(tile_new, MP_CLEAR));
 
				tile = tile_new;
 
			}
 
get_out:;
 
		}
 
	} while (--i);
 
}
 

	
 
static void ClickTile_Clear(uint tile)
 
static void ClickTile_Clear(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static uint32 GetTileTrackStatus_Clear(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_Clear(TileIndex tile, TransportType mode)
 
{
 
	return 0;
 
}
 

	
 
static const StringID _clear_land_str[4+8-1] = {
 
	STR_080B_ROUGH_LAND,
 
	STR_080A_ROCKS,
 
	STR_080E_FIELDS,
 
	STR_080F_SNOW_COVERED_LAND,
 
	STR_0810_DESERT,
 
	0,
 
	0,
 
	STR_080C_BARE_LAND,
 
	STR_080D_GRASS,
 
	STR_080D_GRASS,
 
	STR_080D_GRASS,
 
};
 

	
 
static void GetTileDesc_Clear(uint tile, TileDesc *td)
 
static void GetTileDesc_Clear(TileIndex tile, TileDesc *td)
 
{
 
	int i = (_map5[tile]>>2) & 7;
 
	if (i == 0)
 
		i = (_map5[tile] & 3) + 8;
 
	td->str = _clear_land_str[i - 1];
 
	td->owner = GetTileOwner(tile);
 
}
 

	
 
static void ChangeTileOwner_Clear(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Clear(TileIndex tile, byte old_player, byte new_player)
 
{
 
	return;
 
}
 

	
 
void InitializeClearLand(void)
 
{
 
	_opt.snow_line = _patches.snow_line_height * 8;
 
}
 

	
 
const TileTypeProcs _tile_type_clear_procs = {
 
	DrawTile_Clear,						/* draw_tile_proc */
 
	GetSlopeZ_Clear,					/* get_slope_z_proc */
depot.c
Show inline comments
 
@@ -24,25 +24,25 @@ static void DepotPoolNewBlock(uint start
 
		depot->index = start_item++;
 
}
 

	
 
/* Initialize the town-pool */
 
MemoryPool _depot_pool = { "Depots", DEPOT_POOL_MAX_BLOCKS, DEPOT_POOL_BLOCK_SIZE_BITS, sizeof(Depot), &DepotPoolNewBlock, 0, 0, NULL };
 

	
 

	
 
/**
 
 * Gets a depot from a tile
 
 *
 
 * @return Returns the depot if the tile had a depot, else it returns NULL
 
 */
 
Depot *GetDepotByTile(uint tile)
 
Depot *GetDepotByTile(TileIndex tile)
 
{
 
	Depot *depot;
 

	
 
	FOR_ALL_DEPOTS(depot) {
 
		if (depot->xy == tile)
 
			return depot;
 
	}
 

	
 
	return NULL;
 
}
 

	
 
/**
 
@@ -64,25 +64,25 @@ Depot *AllocateDepot(void)
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
	if (AddBlockToPool(&_depot_pool))
 
		return AllocateDepot();
 

	
 
	return NULL;
 
}
 

	
 
/**
 
 * Delete a depot
 
 */
 
void DoDeleteDepot(uint tile)
 
void DoDeleteDepot(TileIndex tile)
 
{
 
	Order order;
 
	Depot *depot;
 

	
 
	/* Get the depot */
 
	depot = GetDepotByTile(tile);
 

	
 
	/* Clear the tile */
 
	DoClearSquare(tile);
 

	
 
	/* Clear the depot */
 
	depot->xy = 0;
depot.h
Show inline comments
 
@@ -103,18 +103,18 @@ static inline DiagDirection GetDepotDire
 
			/* Water is stubborn, it stores the directions in a different order. */
 
			switch (_map5[tile] & 3) {
 
				case 0: return DIAGDIR_NE;
 
				case 1: return DIAGDIR_SW;
 
				case 2: return DIAGDIR_NW;
 
				case 3: return DIAGDIR_SE;
 
			}
 
		default:
 
			return INVALID_DIAGDIR; /* Not reached */
 
	}
 
}
 

	
 
Depot *GetDepotByTile(uint tile);
 
Depot *GetDepotByTile(TileIndex tile);
 
void InitializeDepot(void);
 
Depot *AllocateDepot(void);
 
void DoDeleteDepot(uint tile);
 
void DoDeleteDepot(TileIndex tile);
 

	
 
#endif /* DEPOT_H */
disaster_cmd.c
Show inline comments
 
@@ -138,25 +138,25 @@ static void SetDisasterVehiclePos(Vehicl
 
			EndVehicleMove(u);
 
		}
 
	}
 
}
 

	
 

	
 
static void DisasterTick_Zeppeliner(Vehicle *v)
 
{
 
	GetNewVehiclePosResult gp;
 
	Station *st;
 
	int x,y;
 
	byte z;
 
	uint tile;
 
	TileIndex tile;
 

	
 
	++v->tick_counter;
 

	
 
	if (v->current_order.station < 2) {
 
		if (v->tick_counter&1)
 
			return;
 

	
 
		GetNewVehiclePos(v, &gp);
 

	
 
		SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
 

	
 
		if (v->current_order.station == 1) {
 
@@ -321,25 +321,25 @@ static void DisasterTick_UFO(Vehicle *v)
 

	
 
// destroy?
 
		if (v->age > 50) {
 
			CreateEffectVehicleRel(v, 0, 7, 8, EV_EXPLOSION_LARGE);
 
			SndPlayVehicleFx(SND_12_EXPLOSION, v);
 
			DeleteDisasterVeh(v);
 
		}
 
	}
 
}
 

	
 
static void DestructIndustry(Industry *i)
 
{
 
	uint tile;
 
	TileIndex tile;
 

	
 
	for(tile=0; tile != MapSize(); tile++) {
 
		if (IsTileType(tile, MP_INDUSTRY) &&	_map2[tile] == i->index) {
 
			_map_owner[tile] = 0;
 
			MarkTileDirtyByTile(tile);
 
		}
 
	}
 
}
 

	
 
// Airplane which destroys an oil refinery
 
static void DisasterTick_2(Vehicle *v)
 
{
 
@@ -380,25 +380,25 @@ static void DisasterTick_2(Vehicle *v)
 
			v->current_order.station = 2;
 
			v->age = 0;
 

	
 
			i = GetIndustry(v->dest_tile);
 
			DestructIndustry(i);
 

	
 
			SetDParam(0, i->town->index);
 
			AddNewsItem(STR_B002_OIL_REFINERY_EXPLOSION, NEWS_FLAGS(NM_THIN,NF_VIEWPORT|NF_TILE,NT_ACCIDENT,0), i->xy, 0);
 
			SndPlayTileFx(SND_12_EXPLOSION, i->xy);
 
		}
 
	} else if (v->current_order.station == 0) {
 
		int x,y;
 
		uint tile;
 
		TileIndex tile;
 
		int ind;
 

	
 
		x = v->x_pos - 15*16;
 
		y = v->y_pos;
 

	
 
		if ( (uint)x > MapMaxX() * 16-1)
 
			return;
 

	
 
		tile = TILE_FROM_XY(x,y);
 
		if (!IsTileType(tile, MP_INDUSTRY))
 
			return;
 

	
 
@@ -451,25 +451,25 @@ static void DisasterTick_3(Vehicle *v)
 
			v->current_order.station = 2;
 
			v->age = 0;
 

	
 
			i = GetIndustry(v->dest_tile);
 
			DestructIndustry(i);
 

	
 
			SetDParam(0, i->town->index);
 
			AddNewsItem(STR_B003_FACTORY_DESTROYED_IN_SUSPICIOUS, NEWS_FLAGS(NM_THIN,NF_VIEWPORT|NF_TILE,NT_ACCIDENT,0), i->xy, 0);
 
			SndPlayTileFx(SND_12_EXPLOSION, i->xy);
 
		}
 
	} else if (v->current_order.station == 0) {
 
		int x,y;
 
		uint tile;
 
		TileIndex tile;
 
		int ind;
 

	
 
		x = v->x_pos - 15*16;
 
		y = v->y_pos;
 

	
 
		if ( (uint)x > MapMaxX() * 16-1)
 
			return;
 

	
 
		tile = TILE_FROM_XY(x,y);
 
		if (!IsTileType(tile, MP_INDUSTRY))
 
			return;
 

	
 
@@ -495,25 +495,26 @@ static void DisasterTick_3b(Vehicle *v)
 
	BeginVehicleMove(v);
 
	EndVehicleMove(v);
 
}
 

	
 
// Big UFO which lands on a piece of rail.
 
// Will be shot down by a plane
 
static void DisasterTick_4(Vehicle *v)
 
{
 
	GetNewVehiclePosResult gp;
 
	byte z;
 
	Vehicle *u,*w;
 
	Town *t;
 
	uint tile,tile_org;
 
	TileIndex tile;
 
	TileIndex tile_org;
 

	
 
	v->tick_counter++;
 

	
 
	if (v->current_order.station == 1) {
 
		int x = TileX(v->dest_tile) * 16 + 8;
 
		int y = TileY(v->dest_tile) * 16 + 8;
 
		if (abs(v->x_pos - x) + abs(v->y_pos - y) >= 8) {
 
			v->direction = GetDirectionTowards(v, x, y);
 

	
 
			GetNewVehiclePos(v, &gp);
 
			SetDisasterVehiclePos(v, gp.x, gp.y, v->z_pos);
 
			return;
 
@@ -629,25 +630,25 @@ static void DisasterTick_4b(Vehicle *v)
 
		BEGIN_TILE_LOOP(tile,6,6,v->tile - TILE_XY(3,3))
 
			tile = TILE_MASK(tile);
 
			DisasterClearSquare(tile);
 
		END_TILE_LOOP(tile,6,6,v->tile - TILE_XY(3,3))
 
	}
 
}
 

	
 
// Submarine handler
 
static void DisasterTick_5_and_6(Vehicle *v)
 
{
 
	uint32 r;
 
	GetNewVehiclePosResult gp;
 
	uint tile;
 
	TileIndex tile;
 

	
 
	v->tick_counter++;
 

	
 
	if (++v->age > 8880) {
 
		VehiclePositionChanged(v);
 
		BeginVehicleMove(v);
 
		EndVehicleMove(v);
 
		DeleteVehicle(v);
 
		return;
 
	}
 

	
 
	if (!(v->tick_counter&1))
 
@@ -905,26 +906,27 @@ static void Disaster7_Init(void)
 
	int maxloop = 15;
 
	int index = Random() & 0xF;
 

	
 
	do {
 
		FOR_ALL_INDUSTRIES(i) {
 
			if (i->xy != 0 && i->type == IT_COAL_MINE	&& --index < 0) {
 

	
 
				SetDParam(0, i->town->index);
 
				AddNewsItem(STR_B005_COAL_MINE_SUBSIDENCE_LEAVES,
 
					NEWS_FLAGS(NM_THIN,NF_VIEWPORT|NF_TILE,NT_ACCIDENT,0), i->xy + TILE_XY(1,1), 0);
 

	
 
				{
 
					uint tile = i->xy;
 
					TileIndex tile = i->xy;
 
					TileIndexDiff step = TileOffsByDir(Random() & 3);
 

	
 
					int count = 30;
 
					do {
 
						DisasterClearSquare(tile);
 
						tile = TILE_MASK(tile + step);
 
					} while (--count);
 
				}
 
				return;
 
			}
 
		}
 
	} while (--maxloop != 0);
 
}
 

	
dock_gui.c
Show inline comments
 
@@ -7,64 +7,64 @@
 
#include "station.h"
 
#include "gui.h"
 
#include "viewport.h"
 
#include "gfx.h"
 
#include "sound.h"
 
#include "command.h"
 

	
 
static void ShowBuildDockStationPicker(void);
 
static void ShowBuildDocksDepotPicker(void);
 

	
 
static byte _ship_depot_direction;
 

	
 
void CcBuildDocks(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildDocks(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_02_SPLAT, tile);
 
		ResetObjectToPlace();
 
	}
 
}
 

	
 
void CcBuildCanal(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildCanal(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) SndPlayTileFx(SND_02_SPLAT, tile);
 
}
 

	
 

	
 
static void PlaceDocks_Dock(uint tile)
 
static void PlaceDocks_Dock(TileIndex tile)
 
{
 
	DoCommandP(tile, 0, 0, CcBuildDocks, CMD_BUILD_DOCK | CMD_AUTO | CMD_MSG(STR_9802_CAN_T_BUILD_DOCK_HERE));
 
}
 

	
 
static void PlaceDocks_Depot(uint tile)
 
static void PlaceDocks_Depot(TileIndex tile)
 
{
 
	DoCommandP(tile, _ship_depot_direction, 0, CcBuildDocks, CMD_BUILD_SHIP_DEPOT | CMD_AUTO | CMD_MSG(STR_3802_CAN_T_BUILD_SHIP_DEPOT));
 
}
 

	
 
static void PlaceDocks_Buoy(uint tile)
 
static void PlaceDocks_Buoy(TileIndex tile)
 
{
 
	DoCommandP(tile, 0, 0, CcBuildDocks, CMD_BUILD_BUOY | CMD_AUTO | CMD_MSG(STR_9835_CAN_T_POSITION_BUOY_HERE));
 
}
 

	
 
static void PlaceDocks_DemolishArea(uint tile)
 
static void PlaceDocks_DemolishArea(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_AND_Y | GUI_PlaceProc_DemolishArea);
 
}
 

	
 
static void PlaceDocks_BuildCanal(uint tile)
 
static void PlaceDocks_BuildCanal(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_OR_Y);
 
}
 

	
 
static void PlaceDocks_BuildLock(uint tile)
 
static void PlaceDocks_BuildLock(TileIndex tile)
 
{
 
	DoCommandP(tile, 0, 0, CcBuildDocks, CMD_BUILD_LOCK | CMD_AUTO | CMD_MSG(STR_CANT_BUILD_LOCKS));
 
}
 

	
 

	
 
static void BuildDocksClick_Canal(Window *w)
 
{
 
	HandlePlacePushButton(w, 3, SPR_CURSOR_CANAL, 1, PlaceDocks_BuildCanal);
 
}
 

	
 
static void BuildDocksClick_Lock(Window *w)
 
{
 
@@ -155,25 +155,26 @@ static void BuildDocksToolbWndProc(Windo
 
	case WE_ABORT_PLACE_OBJ:
 
		UnclickWindowButtons(w);
 
		SetWindowDirty(w);
 

	
 
		w = FindWindowById(WC_BUILD_STATION, 0);
 
		if (w != NULL) WP(w,def_d).close=true;
 

	
 
		w = FindWindowById(WC_BUILD_DEPOT, 0);
 
		if (w != NULL) WP(w,def_d).close=true;
 
		break;
 

	
 
	case WE_PLACE_PRESIZE: {
 
		uint tile_from, tile_to;
 
		TileIndex tile_from;
 
		TileIndex tile_to;
 

	
 
		tile_from = tile_to = e->place.tile;
 
		switch(GetTileSlope(tile_from, NULL)) {
 
		case 3: tile_to += TILE_XY(-1,0); break;
 
		case 6:	tile_to += TILE_XY(0,-1);	break;
 
		case 9:	tile_to += TILE_XY(0,1);	break;
 
		case 12:tile_to += TILE_XY(1,0);	break;
 
		}
 
		VpSetPresizeRange(tile_from, tile_to);
 
	} break;
 
	}
 
}
dummy_land.c
Show inline comments
 
@@ -9,61 +9,62 @@ static void DrawTile_Dummy(TileInfo *ti)
 
	DrawGroundSpriteAt(0x3EC, ti->x, ti->y, ti->z);
 
}
 

	
 

	
 
static uint GetSlopeZ_Dummy(TileInfo *ti) {
 
	return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z;
 
}
 

	
 
static uint GetSlopeTileh_Dummy(TileInfo *ti) {
 
	return ti->tileh;
 
}
 

	
 
static int32 ClearTile_Dummy(uint tile, byte flags) {
 
static int32 ClearTile_Dummy(TileIndex tile, byte flags)
 
{
 
	return_cmd_error(STR_0001_OFF_EDGE_OF_MAP);
 
}
 

	
 

	
 
static void GetAcceptedCargo_Dummy(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Dummy(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* not used */
 
}
 

	
 
static void GetTileDesc_Dummy(uint tile, TileDesc *td)
 
static void GetTileDesc_Dummy(TileIndex tile, TileDesc *td)
 
{
 
	td->str = STR_EMPTY;
 
	td->owner = OWNER_NONE;
 
}
 

	
 
static void AnimateTile_Dummy(uint tile)
 
static void AnimateTile_Dummy(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void TileLoop_Dummy(uint tile)
 
static void TileLoop_Dummy(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void ClickTile_Dummy(uint tile)
 
static void ClickTile_Dummy(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void ChangeTileOwner_Dummy(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Dummy(TileIndex tile, byte old_player, byte new_player)
 
{
 
	/* not used */
 
}
 

	
 
static uint32 GetTileTrackStatus_Dummy(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_Dummy(TileIndex tile, TransportType mode)
 
{
 
	return 0;
 
}
 

	
 
const TileTypeProcs _tile_type_dummy_procs = {
 
	DrawTile_Dummy,						/* draw_tile_proc */
 
	GetSlopeZ_Dummy,					/* get_slope_z_proc */
 
	ClearTile_Dummy,					/* clear_tile_proc */
 
	GetAcceptedCargo_Dummy,		/* get_accepted_cargo_proc */
 
	GetTileDesc_Dummy,				/* get_tile_desc_proc */
 
	GetTileTrackStatus_Dummy,	/* get_tile_track_status_proc */
 
	ClickTile_Dummy,					/* click_tile_proc */
economy.c
Show inline comments
 
@@ -38,25 +38,25 @@ void CheckSwitchToEuro(void)
 
{
 
	if (_currency_specs[_opt.currency].to_euro != CF_NOEURO &&
 
			_currency_specs[_opt.currency].to_euro != CF_ISEURO &&
 
			_cur_year >= (_currency_specs[_opt.currency].to_euro-MAX_YEAR_BEGIN_REAL)) {
 
		_opt.currency = 2; // this is the index of euro above.
 
		AddNewsItem(STR_EURO_INTRODUCE, NEWS_FLAGS(NM_NORMAL,0,NT_ECONOMY,0), 0, 0);
 
	}
 
}
 

	
 
void UpdatePlayerHouse(Player *p, uint score)
 
{
 
	byte val;
 
	uint tile = p->location_of_house;
 
	TileIndex tile = p->location_of_house;
 

	
 
	if (tile == 0)
 
		return;
 

	
 
	(val = 128, score < 170) ||
 
	(val+= 4, score < 350) ||
 
	(val+= 4, score < 520) ||
 
	(val+= 4, score < 720) ||
 
	(val+= 4, true);
 

	
 
/* house is already big enough */
 
	if (val <= _map5[tile])
 
@@ -783,25 +783,26 @@ void StartupEconomy(void)
 
		_price_frac[i] = 0;
 
	}
 

	
 
	_economy.interest_rate = _opt.diff.initial_interest;
 
	_economy.infl_amount = _opt.diff.initial_interest;
 
	_economy.infl_amount_pr = max(0, _opt.diff.initial_interest - 1);
 
	_economy.max_loan_unround = _economy.max_loan = _opt.diff.max_loan * 1000;
 
	_economy.fluct = (byte)(Random()) + 168;
 
}
 

	
 
Pair SetupSubsidyDecodeParam(Subsidy *s, bool mode)
 
{
 
	int tile, tile2;
 
	TileIndex tile;
 
	TileIndex tile2;
 
	Industry *i;
 
	Town *t;
 
	Station *st;
 
	Pair tp;
 

	
 
	/* if mode is false, convert into plural */
 
	SetDParam(0, _cargoc.names_s[s->cargo_type] + (mode?0:32));
 

	
 
	if (s->age < 12) {
 
		if (!(s->cargo_type == CT_PASSENGERS || s->cargo_type == CT_MAIL)) {
 
			SetDParam(1, STR_2029);
 
			i = GetIndustry(s->from);
economy.h
Show inline comments
 
@@ -64,15 +64,15 @@ int _score_part[MAX_PLAYERS][NUM_SCORE];
 

	
 
int UpdateCompanyRatingAndValue(Player *p, bool update);
 
void UpdatePlayerHouse(Player *p, uint score);
 

	
 

	
 
VARDEF Subsidy _subsidies[MAX_PLAYERS];
 
Pair SetupSubsidyDecodeParam(Subsidy *s, bool mode);
 
void DeleteSubsidyWithIndustry(uint16 index);
 
void DeleteSubsidyWithStation(uint16 index);
 
void RemoteSubsidyAdd(Subsidy *s_new);
 

	
 
int32 GetTransportedGoodsIncome(uint num_pieces, uint dist, byte transit_days, byte cargo_type);
 
uint MoveGoodsToStation(uint tile, int w, int h, int type, uint amount);
 
uint MoveGoodsToStation(TileIndex tile, int w, int h, int type, uint amount);
 

	
 
#endif /* ECONOMY_H */
functions.h
Show inline comments
 
@@ -55,32 +55,32 @@ void TileLoopClearHelper(TileIndex tile)
 

	
 
/* station_land.c */
 
void StationPickerDrawSprite(int x, int y, int railtype, int image);
 

	
 
/* track_land.c */
 
void DrawTrainDepotSprite(int x, int y, int image, int railtype);
 

	
 
/* road_land.c */
 
void DrawRoadDepotSprite(int x, int y, int image);
 

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

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

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

	
 
/* ttd.c */
 

	
 
// **************
 
// * Warning: DO NOT enable this unless you understand what it does
 
// *
 
@@ -115,49 +115,49 @@ void DrawPlayerFace(uint32 face, int col
 
/* texteff.c */
 
void MoveAllTextEffects(void);
 
void AddTextEffect(StringID msg, int x, int y, uint16 duration);
 
void InitTextEffects(void);
 
void DrawTextEffects(DrawPixelInfo *dpi);
 

	
 
void InitTextMessage(void);
 
void DrawTextMessage(void);
 
void CDECL AddTextMessage(uint16 color, uint8 duration, const char *message, ...);
 
void UndrawTextMessage(void);
 
void TextMessageDailyLoop(void);
 

	
 
bool AddAnimatedTile(uint tile);
 
void DeleteAnimatedTile(uint tile);
 
bool AddAnimatedTile(TileIndex tile);
 
void DeleteAnimatedTile(TileIndex tile);
 
void AnimateAnimatedTiles(void);
 
void InitializeAnimatedTiles(void);
 

	
 
/* tunnelbridge_cmd.c */
 
bool CheckTunnelInWay(uint tile, int z);
 
bool CheckTunnelInWay(TileIndex tile, int z);
 
bool CheckBridge_Stuff(byte bridge_type, int bridge_len);
 
uint32 GetBridgeLength(TileIndex begin, TileIndex end);
 
int CalcBridgeLenCostFactor(int x);
 

	
 
typedef void CommandCallback(bool success, uint tile, uint32 p1, uint32 p2);
 
typedef void CommandCallback(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, CommandCallback *callback, uint32 cmd);
 

	
 
/* network.c */
 
void NetworkUDPClose(void);
 
void NetworkStartUp(void);
 
void NetworkShutDown(void);
 
void NetworkGameLoop(void);
 
void NetworkUDPGameLoop(void);
 
bool NetworkServerStart(void);
 
bool NetworkClientConnectGame(const char* host, unsigned short port);
 
void NetworkReboot(void);
 
void NetworkDisconnect(void);
 
void NetworkSend_Command(uint32 tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback);
 
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback);
 

	
 
/* misc_cmd.c */
 
void PlaceTreesRandomly(void);
 

	
 
void InitializeLandscapeVariables(bool only_constants);
 

	
 
/* misc.c */
 
void DeleteName(StringID id);
 
char *GetName(int id, char *buff);
 

	
 
// AllocateNameUnique also tests if the name used is not used anywere else
 
//  and if it is used, it returns an error.
 
@@ -188,30 +188,30 @@ bool ScrollWindowTo(int x, int y, Window
 
bool ScrollMainWindowToTile(TileIndex tile);
 
bool ScrollMainWindowTo(int x, int y);
 
void DrawSprite(uint32 img, int x, int y);
 
uint GetCorrectTileHeight(TileIndex tile);
 
bool EnsureNoVehicle(TileIndex tile);
 
bool EnsureNoVehicleZ(TileIndex tile, byte z);
 
void MarkAllViewportsDirty(int left, int top, int right, int bottom);
 
void ShowCostOrIncomeAnimation(int x, int y, int z, int32 cost);
 
void ShowFeederIncomeAnimation(int x, int y, int z, int32 cost);
 

	
 
void DrawFoundation(TileInfo *ti, uint f);
 

	
 
bool CheckIfAuthorityAllows(uint tile);
 
Town *ClosestTownFromTile(uint tile, uint threshold);
 
bool CheckIfAuthorityAllows(TileIndex tile);
 
Town *ClosestTownFromTile(TileIndex tile, uint threshold);
 
void ChangeTownRating(Town *t, int add, int max);
 

	
 
uint GetRoadBitsByTile(TileIndex tile);
 
int GetTownRadiusGroup(Town *t, uint tile);
 
int GetTownRadiusGroup(Town *t, TileIndex tile);
 
void ShowNetworkChatQueryWindow(byte desttype, byte dest);
 
void ShowNetworkGiveMoneyWindow(byte player);
 
void ShowNetworkNeedGamePassword(void);
 
void ShowNetworkNeedCompanyPassword(void);
 
int FindFirstBit(uint32 x);
 
void ShowHighscoreTable(int difficulty, int8 rank);
 
void ShowEndGameChart(void);
 
TileIndex AdjustTileCoordRandomly(TileIndex a, byte rng);
 

	
 
enum SaveOrLoadResult {
 
	SL_OK = 0, // completed successfully
 
	SL_ERROR = 1, // error that was caught before internal structures were modified
gui.h
Show inline comments
 
#ifndef GUI_H
 
#define GUI_H
 

	
 
#include "window.h"
 

	
 
/* main_gui.c */
 
void SetupColorsAndInitialWindow(void);
 
void CcPlaySound10(bool success, uint tile, uint32 p1, uint32 p2);
 
void CcBuildCanal(bool success, uint tile, uint32 p1, uint32 p2);
 
void CcTerraform(bool success, uint tile, uint32 p1, uint32 p2);
 
void CcPlaySound10(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
void CcBuildCanal(bool success, TileIndex tile, uint32 p1, uint32 p2);
 
void CcTerraform(bool success, TileIndex tile, uint32 p1, uint32 p2);
 

	
 
/* settings_gui.c */
 
void ShowGameOptions(void);
 
void ShowGameDifficulty(void);
 
void ShowPatchesSelection(void);
 
void ShowNewgrf(void);
 
void ShowCustCurrency(void);
 

	
 
/* graph_gui.c */
 
void ShowOperatingProfitGraph(void);
 
void ShowIncomeGraph(void);
 
void ShowDeliveredCargoGraph(void);
 
@@ -24,25 +24,25 @@ void ShowPerformanceHistoryGraph(void);
 
void ShowCompanyValueGraph(void);
 
void ShowCargoPaymentRates(void);
 
void ShowCompanyLeagueTable(void);
 
void ShowPerformanceRatingDetail(void);
 

	
 
/* news_gui.c */
 
void ShowLastNewsMessage(void);
 
void ShowMessageOptions(void);
 
void ShowMessageHistory(void);
 

	
 
/* traintoolb_gui.c */
 
void ShowBuildRailToolbar(int index, int button);
 
void PlaceProc_BuyLand(uint tile);
 
void PlaceProc_BuyLand(TileIndex tile);
 

	
 
/* train_gui.c */
 
void ShowPlayerTrains(int player, int station);
 
void ShowTrainViewWindow(Vehicle *v);
 
void ShowTrainDetailsWindow(Vehicle *v);
 
void ShowOrdersWindow(Vehicle *v);
 

	
 
void ShowRoadVehViewWindow(Vehicle *v);
 

	
 
/* road_gui.c */
 
void ShowBuildRoadToolbar(void);
 
void ShowBuildRoadScenToolbar(void);
 
@@ -52,28 +52,28 @@ void ShowPlayerRoadVehicles(int player, 
 
void ShowBuildDocksToolbar(void);
 
void ShowPlayerShips(int player, int station);
 

	
 
void ShowShipViewWindow(Vehicle *v);
 

	
 
/* aircraft_gui.c */
 
void ShowBuildAirToolbar(void);
 
void ShowPlayerAircraft(int player, int station);
 

	
 
/* terraform_gui.c */
 
void ShowTerraformToolbar(void);
 

	
 
void PlaceProc_DemolishArea(uint tile);
 
void PlaceProc_LowerLand(uint tile);
 
void PlaceProc_RaiseLand(uint tile);
 
void PlaceProc_LevelLand(uint tile);
 
void PlaceProc_DemolishArea(TileIndex tile);
 
void PlaceProc_LowerLand(TileIndex tile);
 
void PlaceProc_RaiseLand(TileIndex tile);
 
void PlaceProc_LevelLand(TileIndex tile);
 
bool GUIPlaceProcDragXY(const WindowEvent *we);
 

	
 
enum { // max 32 - 4 = 28 types
 
	GUI_PlaceProc_DemolishArea    = 0 << 4,
 
	GUI_PlaceProc_LevelArea       = 1 << 4,
 
	GUI_PlaceProc_DesertArea      = 2 << 4,
 
	GUI_PlaceProc_WaterArea       = 3 << 4,
 
	GUI_PlaceProc_ConvertRailArea = 4 << 4
 
};
 

	
 
/* misc_gui.c */
 
void PlaceLandBlockInfo(void);
industry_cmd.c
Show inline comments
 
@@ -28,26 +28,26 @@ void IndustryPoolNewBlock(uint start_ite
 

	
 
	FOR_ALL_INDUSTRIES_FROM(i, start_item)
 
		i->index = start_item++;
 
}
 

	
 
/* Initialize the industry-pool */
 
MemoryPool _industry_pool = { "Industry", INDUSTRY_POOL_MAX_BLOCKS, INDUSTRY_POOL_BLOCK_SIZE_BITS, sizeof(Industry), &IndustryPoolNewBlock, 0, 0, NULL };
 

	
 
byte _industry_sound_ctr;
 
TileIndex _industry_sound_tile;
 

	
 
void ShowIndustryViewWindow(int industry);
 
void BuildOilRig(uint tile);
 
void DeleteOilRig(uint tile);
 
void BuildOilRig(TileIndex tile);
 
void DeleteOilRig(TileIndex tile);
 

	
 
typedef struct DrawIndustryTileStruct {
 
	uint32 sprite_1;
 
	uint32 sprite_2;
 

	
 
	byte subtile_xy;
 
	byte width_height;
 
	byte dz;
 
	byte proc;
 
} DrawIndustryTileStruct;
 

	
 

	
 
@@ -392,52 +392,52 @@ static void DrawTile_Industry(TileInfo *
 
	}
 
}
 

	
 

	
 
static uint GetSlopeZ_Industry(TileInfo *ti) {
 
	return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z;
 
}
 

	
 
static uint GetSlopeTileh_Industry(TileInfo *ti) {
 
	return 0;
 
}
 

	
 
static void GetAcceptedCargo_Industry(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Industry(TileIndex tile, AcceptedCargo ac)
 
{
 
	int m5 = _map5[tile];
 
	int a;
 

	
 
	a = _industry_map5_accepts_1[m5];
 
	if (a >= 0) ac[a] = (a == 0) ? 1 : 8;
 

	
 
	a = _industry_map5_accepts_2[m5];
 
	if (a >= 0) ac[a] = 8;
 

	
 
	a = _industry_map5_accepts_3[m5];
 
	if (a >= 0) ac[a] = 8;
 
}
 

	
 
static void GetTileDesc_Industry(uint tile, TileDesc *td)
 
static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
 
{
 
	Industry *i = GetIndustry(_map2[tile]);
 

	
 
	td->owner = i->owner;
 
	td->str = STR_4802_COAL_MINE + i->type;
 
	if ((_map_owner[tile] & 0x80) == 0) {
 
		SetDParamX(td->dparam, 0, td->str);
 
		td->str = STR_2058_UNDER_CONSTRUCTION;
 
	}
 
}
 

	
 
static int32 ClearTile_Industry(uint tile, byte flags)
 
static int32 ClearTile_Industry(TileIndex tile, byte flags)
 
{
 
	Industry *i = GetIndustry(_map2[tile]);
 

	
 
	/*	* water can destroy industries
 
			* in editor you can bulldoze industries
 
			* with magic_bulldozer cheat you can destroy industries
 
			* (area around OILRIG is water, so water shouldn't flood it
 
	*/
 
	if ((_current_player != OWNER_WATER && _game_mode != GM_EDITOR &&
 
			!_cheats.magic_bulldozer.value) ||
 
			(_current_player == OWNER_WATER && i->type == IT_OIL_RIG) ) {
 
 		SetDParam(0, STR_4802_COAL_MINE + i->type);
 
@@ -450,25 +450,25 @@ static int32 ClearTile_Industry(uint til
 
	return 0;
 
}
 

	
 

	
 
static const byte _industry_min_cargo[] = {
 
	5, 5, 5, 30, 5, 5, 5, 5,
 
	5, 5, 5, 5, 2, 5, 5, 5,
 
	5, 5, 5, 15, 15, 5, 5, 5,
 
	5, 5, 30, 5, 30, 5, 5, 5,
 
	5, 5, 5, 5, 5,
 
};
 

	
 
static void TransportIndustryGoods(uint tile)
 
static void TransportIndustryGoods(TileIndex tile)
 
{
 
	Industry *i;
 
	int type;
 
	uint cw, am;
 
	byte m5;
 

	
 
	i = GetIndustry(_map2[tile]);
 

	
 
	type = i->type;
 
	cw = min(i->cargo_waiting[0], 255);
 
	if (cw > _industry_min_cargo[type]/* && i->produced_cargo[0] != 0xFF*/) {
 
		i->cargo_waiting[0] -= cw;
 
@@ -493,25 +493,25 @@ static void TransportIndustryGoods(uint 
 
		i->cargo_waiting[1] -= cw;
 

	
 
		if (_economy.fluct <= 0) cw = (cw + 1) >> 1;
 

	
 
		i->last_mo_production[1] += cw;
 

	
 
		am = MoveGoodsToStation(i->xy, i->width, i->height, i->produced_cargo[1], cw);
 
		i->last_mo_transported[1] += am;
 
	}
 
}
 

	
 

	
 
static void AnimateTile_Industry(uint tile)
 
static void AnimateTile_Industry(TileIndex tile)
 
{
 
	byte m,n;
 

	
 
	switch(_map5[tile]) {
 
	case 174:
 
		if ((_tick_counter & 1) == 0) {
 
			m = _map3_lo[tile] + 1;
 

	
 
			switch(m & 7) {
 
			case 2:	SndPlayTileFx(SND_2D_RIP_2, tile); break;
 
			case 6: SndPlayTileFx(SND_29_RIP, tile); break;
 
			}
 
@@ -652,32 +652,32 @@ static void AnimateTile_Industry(uint ti
 
				if (state & i)
 
					return;
 

	
 
				m = (_map_owner[tile] & 0xBF) - 1;
 
				if (m < 0x80) m = 0x82;
 
				_map_owner[tile] = m;
 
				MarkTileDirtyByTile(tile);
 
			}
 
		} break;
 
	}
 
}
 

	
 
static void MakeIndustryTileBiggerCase8(uint tile)
 
static void MakeIndustryTileBiggerCase8(TileIndex tile)
 
{
 
	TileInfo ti;
 
	FindLandscapeHeight(&ti, TileX(tile) * 16, TileY(tile) * 16);
 
	CreateEffectVehicle(ti.x + 15, ti.y + 14, ti.z + 59 + (ti.tileh != 0 ? 8 : 0), EV_CHIMNEY_SMOKE);
 
}
 

	
 
static void MakeIndustryTileBigger(uint tile, byte size)
 
static void MakeIndustryTileBigger(TileIndex tile, byte size)
 
{
 
	byte b = (byte)((size + (1<<2)) & (3<<2));
 

	
 
	if (b != 0) {
 
		_map_owner[tile] = b | (size & 3);
 
		return;
 
	}
 

	
 
	size = (size + 1) & 3;
 
	if (size == 3) size |= 0x80;
 
	_map_owner[tile] = size | b;
 

	
 
@@ -704,50 +704,50 @@ static void MakeIndustryTileBigger(uint 
 
		_map3_hi[tile] = 0;
 
		break;
 

	
 
	case 148: case 149: case 150: case 151:
 
	case 152: case 153: case 154: case 155:
 
		AddAnimatedTile(tile);
 
		break;
 
	}
 
}
 

	
 

	
 

	
 
static void TileLoopIndustryCase161(uint tile)
 
static void TileLoopIndustryCase161(TileIndex tile)
 
{
 
	int dir;
 
	Vehicle *v;
 
	static const int8 _tileloop_ind_case_161[12] = {
 
		11, 0, -4, -14,
 
		-4, -10, -4, 1,
 
		49, 59, 60, 65,
 
	};
 

	
 
	SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
 

	
 
	dir = Random() & 3;
 

	
 
	v = CreateEffectVehicleAbove(
 
		TileX(tile) * 16 + _tileloop_ind_case_161[dir + 0],
 
		TileY(tile) * 16 + _tileloop_ind_case_161[dir + 4],
 
		_tileloop_ind_case_161[dir + 8],
 
		EV_BUBBLE
 
	);
 

	
 
	if (v != NULL)
 
		v->u.special.unk2 = dir;
 
}
 

	
 
static void TileLoop_Industry(uint tile)
 
static void TileLoop_Industry(TileIndex tile)
 
{
 
	byte n;
 

	
 
	if (!(_map_owner[tile] & 0x80)) {
 
		MakeIndustryTileBigger(tile, _map_owner[tile]);
 
		return;
 
	}
 

	
 
	if (_game_mode == GM_EDITOR)
 
		return;
 

	
 
	TransportIndustryGoods(tile);
 
@@ -838,42 +838,42 @@ static void TileLoop_Industry(uint tile)
 
		AddAnimatedTile(tile);
 
		break;
 

	
 
	case 174:
 
		if (CHANCE16(1,3)) {
 
			AddAnimatedTile(tile);
 
		}
 
		break;
 
	}
 
}
 

	
 

	
 
static void ClickTile_Industry(uint tile)
 
static void ClickTile_Industry(TileIndex tile)
 
{
 
	ShowIndustryViewWindow(_map2[tile]);
 
}
 

	
 
static uint32 GetTileTrackStatus_Industry(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_Industry(TileIndex tile, TransportType mode)
 
{
 
	return 0;
 
}
 

	
 
static void GetProducedCargo_Industry(uint tile, byte *b)
 
static void GetProducedCargo_Industry(TileIndex tile, byte *b)
 
{
 
	Industry *i = GetIndustry(_map2[tile]);
 
	b[0] = i->produced_cargo[0];
 
	b[1] = i->produced_cargo[1];
 
}
 

	
 
static void ChangeTileOwner_Industry(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Industry(TileIndex tile, byte old_player, byte new_player)
 
{
 
	/* not used */
 
}
 

	
 
void DeleteIndustry(Industry *i)
 
{
 
	BEGIN_TILE_LOOP(tile_cur, i->width, i->height, i->xy);
 
		if (IsTileType(tile_cur, MP_INDUSTRY)) {
 
			if (_map2[tile_cur] == i->index) {
 
				DoClearSquare(tile_cur);
 
			}
 
		} else if (IsTileType(tile_cur, MP_STATION) && _map5[tile_cur] == 0x4B) {
 
@@ -913,50 +913,50 @@ static bool IsBadFarmFieldTile2(TileInde
 
			byte m5 = _map5[tile] & 0x1C;
 
			return m5 == 0x10;
 
		}
 

	
 
		case MP_TREES:
 
			return false;
 

	
 
		default:
 
			return true;
 
	}
 
}
 

	
 
static void SetupFarmFieldFence(uint tile, int size, byte type, int direction)
 
static void SetupFarmFieldFence(TileIndex tile, int size, byte type, int direction)
 
{
 
	byte or, and;
 

	
 
	do {
 
		tile = TILE_MASK(tile);
 

	
 
		if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
 

	
 
			or = type;
 
			if (or == 1 && (uint16)Random() <= 9362) or = 2;
 

	
 
			or <<= 2;
 
			and = (byte)~0x1C;
 
			if (direction) {
 
				or <<= 3;
 
				and = (byte)~0xE0;
 
			}
 
			_map3_hi[tile] = (_map3_hi[tile] & and) | or;
 
		}
 

	
 
		tile += direction ? TILE_XY(0,1) : TILE_XY(1,0);
 
	} while (--size);
 
}
 

	
 
static void PlantFarmField(uint tile)
 
static void PlantFarmField(TileIndex tile)
 
{
 
	uint size_x, size_y;
 
	uint32 r;
 
	int count;
 
	int type, type2;
 

	
 
	if (_opt.landscape == LT_HILLY) {
 
		if (GetTileZ(tile) + 16 >= _opt.snow_line)
 
			return;
 
	}
 

	
 
	/* determine field size */
 
@@ -999,44 +999,45 @@ static void PlantFarmField(uint tile)
 
	if (_opt.landscape != LT_HILLY && _opt.landscape != LT_DESERT) {
 
		type = _plantfarmfield_type[Random() & 0xF];
 
	}
 

	
 
	SetupFarmFieldFence(tile-TILE_XY(1,0), size_y, type, 1);
 
	SetupFarmFieldFence(tile-TILE_XY(0,1), size_x, type, 0);
 
	SetupFarmFieldFence(tile+TILE_XY(1,0) * (size_x-1), size_y, type, 1);
 
	SetupFarmFieldFence(tile+TILE_XY(0,1) * (size_y-1), size_x, type, 0);
 
}
 

	
 
static void MaybePlantFarmField(Industry *i)
 
{
 
	uint tile;
 
	TileIndex tile;
 

	
 
	if (CHANCE16(1,8)) {
 
		int x = (i->width>>1) + Random() % 31 - 16;
 
		int y = (i->height>>1) + Random() % 31 - 16;
 
		tile = TileAddWrap(i->xy, x, y);
 
		if (tile != INVALID_TILE)
 
			PlantFarmField(tile);
 
	}
 
}
 

	
 
static void ChopLumberMillTrees(Industry *i)
 
{
 
	static const TileIndexDiffC _chop_dir[] = {
 
		{ 0,  1},
 
		{ 1,  0},
 
		{ 0, -1},
 
		{-1,  0}
 
	};
 

	
 
	uint tile = i->xy;
 
	TileIndex tile = i->xy;
 
	int dir, a, j;
 

	
 
	if ((_map_owner[tile] & 0x80) == 0)
 
		return;
 

	
 
	/* search outwards as a rectangular spiral */
 
	for(a=1; a!=41; a+=2) {
 
		for(dir=0; dir!=4; dir++) {
 
			j = a;
 
			do {
 
				tile = TILE_MASK(tile);
 
				if (IsTileType(tile, MP_TREES)) {
 
@@ -1148,132 +1149,132 @@ void OnTick_Industry(void)
 
	}
 

	
 
	if (_game_mode == GM_EDITOR)
 
		return;
 

	
 
	FOR_ALL_INDUSTRIES(i) {
 
		if (i->xy != 0)
 
			ProduceIndustryGoods(i);
 
	}
 
}
 

	
 

	
 
static bool CheckNewIndustry_NULL(uint tile, int type)
 
static bool CheckNewIndustry_NULL(TileIndex tile, int type)
 
{
 
	return true;
 
}
 

	
 
static bool CheckNewIndustry_Forest(uint tile, int type)
 
static bool CheckNewIndustry_Forest(TileIndex tile, int type)
 
{
 
	if (_opt.landscape == LT_HILLY) {
 
		if (GetTileZ(tile) < _opt.snow_line + 16U) {
 
			_error_message = STR_4831_FOREST_CAN_ONLY_BE_PLANTED;
 
			return false;
 
		}
 
	}
 
	return true;
 
}
 

	
 
extern bool _ignore_restrictions;
 

	
 
/* Oil Rig and Oil Refinery */
 
static bool CheckNewIndustry_Oil(uint tile, int type)
 
static bool CheckNewIndustry_Oil(TileIndex tile, int type)
 
{
 
	if(_ignore_restrictions && _game_mode == GM_EDITOR)
 
		return true;
 

	
 
	if (type != IT_OIL_RIG && _game_mode == GM_EDITOR)
 
		return true;
 

	
 
	if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < 16)
 
		return true;
 

	
 
	_error_message = STR_483B_CAN_ONLY_BE_POSITIONED;
 
	return false;
 
}
 

	
 
static bool CheckNewIndustry_Farm(uint tile, int type)
 
static bool CheckNewIndustry_Farm(TileIndex tile, int type)
 
{
 
	if (_opt.landscape == LT_HILLY) {
 
		if (GetTileZ(tile) + 16 >= _opt.snow_line) {
 
			_error_message = STR_0239_SITE_UNSUITABLE;
 
			return false;
 
		}
 
	}
 
	return true;
 
}
 

	
 
static bool CheckNewIndustry_Plantation(uint tile, int type)
 
static bool CheckNewIndustry_Plantation(TileIndex tile, int type)
 
{
 
	if (GetMapExtraBits(tile) == 1) {
 
		_error_message = STR_0239_SITE_UNSUITABLE;
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
static bool CheckNewIndustry_Water(uint tile, int type)
 
static bool CheckNewIndustry_Water(TileIndex tile, int type)
 
{
 
	if (GetMapExtraBits(tile) != 1) {
 
		_error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT;
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
static bool CheckNewIndustry_Lumbermill(uint tile, int type)
 
static bool CheckNewIndustry_Lumbermill(TileIndex tile, int type)
 
{
 
	if (GetMapExtraBits(tile) != 2) {
 
		_error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST;
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
static bool CheckNewIndustry_BubbleGen(uint tile, int type)
 
static bool CheckNewIndustry_BubbleGen(TileIndex tile, int type)
 
{
 
	if (GetTileZ(tile) > 32) {
 
		return false;
 
	}
 
	return true;
 
}
 

	
 
typedef bool CheckNewIndustryProc(uint tile, int type);
 
typedef bool CheckNewIndustryProc(TileIndex tile, int type);
 
static CheckNewIndustryProc * const _check_new_industry_procs[] = {
 
	CheckNewIndustry_NULL,
 
	CheckNewIndustry_Forest,
 
	CheckNewIndustry_Oil,
 
	CheckNewIndustry_Farm,
 
	CheckNewIndustry_Plantation,
 
	CheckNewIndustry_Water,
 
	CheckNewIndustry_Lumbermill,
 
	CheckNewIndustry_BubbleGen,
 
};
 

	
 
static bool CheckSuitableIndustryPos(uint tile)
 
static bool CheckSuitableIndustryPos(TileIndex tile)
 
{
 
	uint x = TileX(tile);
 
	uint y = TileY(tile);
 

	
 
	if ( x < 2 || y < 2 || x > MapMaxX() - 3 || y > MapMaxY() - 3) {
 
		_error_message = STR_0239_SITE_UNSUITABLE;
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
static Town *CheckMultipleIndustryInTown(uint tile, int type)
 
static Town *CheckMultipleIndustryInTown(TileIndex tile, int type)
 
{
 
	Town *t;
 
	Industry *i;
 

	
 
	t = ClosestTownFromTile(tile, (uint)-1);
 

	
 
	if (_patches.multiple_industry_per_town)
 
		return t;
 

	
 
	FOR_ALL_INDUSTRIES(i) {
 
		if (i->xy != 0 &&
 
				i->type == (byte)type &&
 
@@ -1302,33 +1303,33 @@ static const byte _industry_map5_bits[] 
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16, 16,
 
	16, 16, 16, 16, 16, 16, 16,
 
};
 

	
 
static bool CheckIfIndustryTilesAreFree(uint tile, const IndustryTileTable *it, int type, Town *t)
 
static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, int type, Town *t)
 
{
 
	TileInfo ti;
 
	uint cur_tile;
 

	
 
	_error_message = STR_0239_SITE_UNSUITABLE;
 

	
 
	do {
 
		cur_tile = tile + ToTileIndexDiff(it->ti);
 
		TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
 

	
 
		if (!IsValidTile(cur_tile)) {
 
			if (it->map5 == 0xff)
 
				continue;
 
			return false;
 
		}
 

	
 
		FindLandscapeHeightByTile(&ti, cur_tile);
 

	
 
		if (it->map5 == 0xFF) {
 
			if (ti.type != MP_WATER || ti.tileh != 0)
 
				return false;
 
		} else {
 
@@ -1388,25 +1389,25 @@ static bool CheckIfIndustryTilesAreFree(
 
				} else {
 
do_clear:
 
					if (DoCommandByTile(cur_tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) == CMD_ERROR)
 
						return false;
 
				}
 
			}
 
		}
 
	} while ((++it)->ti.x != -0x80);
 

	
 
	return true;
 
}
 

	
 
static bool CheckIfTooCloseToIndustry(uint tile, int type)
 
static bool CheckIfTooCloseToIndustry(TileIndex tile, int type)
 
{
 
	Industry *i;
 
	const IndustrySpec *spec;
 
	spec = &_industry_spec[type];
 

	
 
	// accepting industries won't be close, not even with patch
 
	if (_patches.same_industry_close && (spec->accepts_cargo[0] == 0xFF) )
 
		return true;
 

	
 
	FOR_ALL_INDUSTRIES(i) {
 
		// check if an industry that accepts the same goods is nearby
 
		if (i->xy != 0 &&
 
@@ -1447,29 +1448,28 @@ static Industry *AllocateIndustry(void)
 

	
 
			return i;
 
		}
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
	if (AddBlockToPool(&_industry_pool))
 
		return AllocateIndustry();
 

	
 
	return NULL;
 
}
 

	
 
static void DoCreateNewIndustry(Industry *i, uint tile, int type, const IndustryTileTable *it, Town *t, byte owner)
 
static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const IndustryTileTable *it, Town *t, byte owner)
 
{
 
	const IndustrySpec *spec;
 
	uint32 r;
 
	uint cur_tile;
 
	int j;
 

	
 
	i->xy = tile;
 
	i->width = i->height = 0;
 
	i->type = type;
 

	
 
	spec = &_industry_spec[type];
 

	
 
	i->produced_cargo[0] = spec->produced_cargo[0];
 
	i->produced_cargo[1] = spec->produced_cargo[1];
 
	i->accepts_cargo[0] = spec->accepts_cargo[0];
 
	i->accepts_cargo[1] = spec->accepts_cargo[1];
 
@@ -1500,25 +1500,25 @@ static void DoCreateNewIndustry(Industry
 
	i->total_transported[1] = 0;
 
	i->was_cargo_delivered = false;
 
	i->last_prod_year = _cur_year;
 
	i->total_production[0] = i->production_rate[0] * 8;
 
	i->total_production[1] = i->production_rate[1] * 8;
 

	
 
	if (_generating_world == 0)
 
		i->total_production[0] = i->total_production[1] = 0;
 

	
 
	i->prod_level = 0x10;
 

	
 
	do {
 
		cur_tile = tile + ToTileIndexDiff(it->ti);
 
		TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
 

	
 
		if (it->map5 != 0xFF) {
 
			byte size;
 

	
 
			size = it->ti.x;
 
			if (size > i->width) i->width = size;
 
			size = it->ti.y;
 
			if (size > i->height)i->height = size;
 

	
 
			DoCommandByTile(cur_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 

	
 
			SetTileType(cur_tile, MP_INDUSTRY);
 
@@ -1527,25 +1527,26 @@ static void DoCreateNewIndustry(Industry
 
			_map_owner[cur_tile] = _generating_world ? 0x1E : 0; /* maturity */
 
		}
 
	} while ((++it)->ti.x != -0x80);
 

	
 
	i->width++;
 
	i->height++;
 

	
 
	if (i->type == IT_FARM || i->type == IT_FARM_2) {
 
		tile = i->xy + TILE_XY((i->width >> 1), (i->height >> 1));
 
		for(j=0; j!=50; j++) {
 
			int x = Random() % 31 - 16;
 
			int y = Random() % 31 - 16;
 
			uint new_tile = TileAddWrap(tile, x, y);
 
			TileIndex  new_tile = TileAddWrap(tile, x, y);
 

	
 
			if (new_tile != INVALID_TILE)
 
				PlantFarmField(new_tile);
 
		}
 
	}
 
	_industry_sort_dirty = true;
 
	InvalidateWindow(WC_INDUSTRY_DIRECTORY, 0);
 
}
 

	
 
/** Build/Fund an industry
 
 * @param x,y coordinates where industry is built
 
 * @param p1 industry type @see build_industry.h and @see industry.h
 
 * @param p2 unused
 
@@ -1595,25 +1596,25 @@ int32 CmdBuildIndustry(int x, int y, uin
 

	
 
	if (!CheckIfTooCloseToIndustry(tile, p1)) return CMD_ERROR;
 

	
 
	if ( (i = AllocateIndustry()) == NULL) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC)
 
		DoCreateNewIndustry(i, tile, p1, it, t, OWNER_NONE);
 

	
 
	return (_price.build_industry >> 5) * _industry_type_costs[p1];
 
}
 

	
 

	
 
Industry *CreateNewIndustry(uint tile, int type)
 
Industry *CreateNewIndustry(TileIndex tile, int type)
 
{
 
	Town *t;
 
	const IndustryTileTable *it;
 
	Industry *i;
 

	
 
	const IndustrySpec *spec;
 

	
 
	if (!CheckSuitableIndustryPos(tile))
 
		return NULL;
 

	
 
	spec = &_industry_spec[type];
 

	
main_gui.c
Show inline comments
 
@@ -135,25 +135,25 @@ bool HandlePlacePushButton(Window *w, in
 
	if (w->click_state & mask) {
 
		ResetObjectToPlace();
 
		return false;
 
	}
 

	
 
	SetObjectToPlace(cursor, mode, w->window_class, w->window_number);
 
	w->click_state |= mask;
 
	_place_proc = placeproc;
 
	return true;
 
}
 

	
 

	
 
void CcPlaySound10(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcPlaySound10(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) SndPlayTileFx(SND_12_EXPLOSION, tile);
 
}
 

	
 

	
 
typedef void ToolbarButtonProc(Window *w);
 

	
 
static void ToolbarPauseClick(Window *w)
 
{
 
	if (_networking && !_network_server) { return;} // only server can pause the game
 

	
 
	if (DoCommandP(0, _pause ? 0 : 1, 0, NULL, CMD_PAUSE))
 
@@ -1140,25 +1140,25 @@ static void AskResetLandscape(uint mode)
 
	AllocateWindowDescFront(&_ask_reset_landscape_desc, mode);
 
}
 

	
 
// TODO - Incorporate into game itself to allow for ingame raising/lowering of
 
// larger chunks at the same time OR remove altogether, as we have 'level land' ?
 
/**
 
 * Raise/Lower a bigger chunk of land at the same time in the editor. When
 
 * raising get the lowest point, when lowering the highest point, and set all
 
 * tiles in the selection to that height.
 
 * @param tile The top-left tile where the terraforming will start
 
 * @param mode 1 for raising, 0 for lowering land
 
 */
 
static void CommonRaiseLowerBigLand(uint tile, int mode)
 
static void CommonRaiseLowerBigLand(TileIndex tile, int mode)
 
{
 
	int sizex, sizey;
 
	byte h;
 

	
 
	_error_message_2 = mode ? STR_0808_CAN_T_RAISE_LAND_HERE : STR_0809_CAN_T_LOWER_LAND_HERE;
 

	
 
	_generating_world = true; // used to create green terraformed land
 

	
 
	if (_terraform_size == 1) {
 
		DoCommandP(tile, 8, (uint32)mode, CcTerraform, CMD_TERRAFORM_LAND | CMD_AUTO | CMD_MSG(_error_message_2));
 
	} else {
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
@@ -1185,56 +1185,56 @@ static void CommonRaiseLowerBigLand(uint
 
		}
 

	
 
		BEGIN_TILE_LOOP(tile2, sizex, sizey, tile) {
 
			if (TileHeight(tile2) == h) {
 
				DoCommandP(tile2, 8, (uint32)mode, NULL, CMD_TERRAFORM_LAND | CMD_AUTO);
 
			}
 
		} END_TILE_LOOP(tile2, sizex, sizey, tile)
 
	}
 

	
 
	_generating_world = false;
 
}
 

	
 
static void PlaceProc_RaiseBigLand(uint tile)
 
static void PlaceProc_RaiseBigLand(TileIndex tile)
 
{
 
	CommonRaiseLowerBigLand(tile, 1);
 
}
 

	
 
static void PlaceProc_LowerBigLand(uint tile)
 
static void PlaceProc_LowerBigLand(TileIndex tile)
 
{
 
	CommonRaiseLowerBigLand(tile, 0);
 
}
 

	
 
static void PlaceProc_RockyArea(uint tile)
 
static void PlaceProc_RockyArea(TileIndex tile)
 
{
 
	if (!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES))
 
		return;
 

	
 
	ModifyTile(tile, MP_SETTYPE(MP_CLEAR) | MP_MAP5, (_map5[tile] & ~0x1C) | 0xB);
 
	SndPlayTileFx(SND_1F_SPLAT, tile);
 
}
 

	
 
static void PlaceProc_LightHouse(uint tile)
 
static void PlaceProc_LightHouse(TileIndex tile)
 
{
 
	TileInfo ti;
 

	
 
	FindLandscapeHeightByTile(&ti, tile);
 
	if (ti.type != MP_CLEAR || (ti.tileh & 0x10))
 
		return;
 

	
 
	ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAP5, 1);
 
	SndPlayTileFx(SND_1F_SPLAT, tile);
 
}
 

	
 
static void PlaceProc_Transmitter(uint tile)
 
static void PlaceProc_Transmitter(TileIndex tile)
 
{
 
	TileInfo ti;
 

	
 
	FindLandscapeHeightByTile(&ti, tile);
 
	if (ti.type != MP_CLEAR || (ti.tileh & 0x10))
 
		return;
 

	
 
	ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAP5, 0);
 
	SndPlayTileFx(SND_1F_SPLAT, tile);
 
}
 

	
 
static void PlaceProc_DesertArea(TileIndex tile)
 
@@ -1446,33 +1446,33 @@ static inline void ShowEditorTerraformTo
 
{
 
	AllocateWindowDescFront(&_scen_edit_land_gen_desc, 0);
 
}
 

	
 
static void ToolbarScenGenLand(Window *w)
 
{
 
	HandleButtonClick(w, 11);
 
	SndPlayFx(SND_15_BEEP);
 

	
 
	ShowEditorTerraformToolBar();
 
}
 

	
 
void CcBuildTown(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildTown(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
		ResetObjectToPlace();
 
	}
 
}
 

	
 
static void PlaceProc_Town(uint tile)
 
static void PlaceProc_Town(TileIndex tile)
 
{
 
	DoCommandP(tile, 0, 0, CcBuildTown, CMD_BUILD_TOWN | CMD_MSG(STR_0236_CAN_T_BUILD_TOWN_HERE));
 
}
 

	
 

	
 
static const Widget _scen_edit_town_gen_widgets[] = {
 
{    WWT_TEXTBTN,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5,                 STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,     7,    11,   147,     0,    13, STR_0233_TOWN_GENERATION, STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{  WWT_STICKYBOX,   RESIZE_NONE,     7,   148,   159,     0,    13, 0x0,                      STR_STICKY_BUTTON},
 
{     WWT_IMGBTN,   RESIZE_NONE,     7,     0,   159,    14,    81, 0x0,                      STR_NULL},
 
{    WWT_TEXTBTN,   RESIZE_NONE,    14,     2,   157,    16,    27, STR_0234_NEW_TOWN,        STR_0235_CONSTRUCT_NEW_TOWN},
 
{    WWT_TEXTBTN,   RESIZE_NONE,    14,     2,   157,    29,    40, STR_023D_RANDOM_TOWN,     STR_023E_BUILD_TOWN_IN_RANDOM_LOCATION},
 
@@ -1646,25 +1646,25 @@ static const Widget _scenedit_industry_c
 
int _industry_type_to_place;
 

	
 
static bool AnyTownExists(void)
 
{
 
	Town *t;
 
	FOR_ALL_TOWNS(t) {
 
		if (t->xy)
 
			return true;
 
	}
 
	return false;
 
}
 

	
 
extern Industry *CreateNewIndustry(uint tile, int type);
 
extern Industry *CreateNewIndustry(TileIndex tile, int type);
 

	
 
static bool TryBuildIndustry(TileIndex tile, int type)
 
{
 
	int n;
 

	
 
	if (CreateNewIndustry(tile, type)) return true;
 

	
 
	n = 100;
 
	do {
 
		if (CreateNewIndustry(AdjustTileCoordRandomly(tile, 1), type)) return true;
 
	} while (--n);
 

	
map.h
Show inline comments
 
@@ -36,25 +36,25 @@ uint ScaleByMapSize(uint); // Scale rela
 
uint ScaleByMapSize1D(uint); // Scale relative to the circumference of the map
 

	
 
typedef uint32 TileIndex;
 

	
 
typedef enum {
 
	OWNER_TOWN			= 0xf,	// a town owns the tile
 
	OWNER_NONE			= 0x10,	// nobody owns the tile
 
	OWNER_WATER			= 0x11,	// "water" owns the tile
 
	OWNER_SPECTATOR	= 0xff,	// spectator in MP or in scenario editor
 
} Owner;
 

	
 
enum {
 
	INVALID_TILE = (uint32) -1
 
	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();
misc_gui.c
Show inline comments
 
@@ -21,25 +21,25 @@
 

	
 
#include "hal.h" // for file list
 

	
 
static bool _fios_path_changed;
 
static bool _savegame_sort_dirty;
 

	
 
bool _query_string_active;
 

	
 
typedef struct LandInfoData {
 
	Town *town;
 
	int32 costclear;
 
	AcceptedCargo ac;
 
	uint tile;
 
	TileIndex tile;
 
	TileDesc td;
 
} LandInfoData;
 

	
 
static void LandInfoWndProc(Window *w, WindowEvent *e)
 
{
 
	LandInfoData *lid;
 
	StringID str;
 

	
 
	if (e->event == WE_PAINT) {
 
		int idx = 0;
 
		int i;
 

	
 
@@ -106,25 +106,25 @@ static const Widget _land_info_widgets[]
 
{     WWT_IMGBTN,   RESIZE_NONE,    14,     0,   279,    14,    92, 0x0,				STR_NULL},
 
{    WIDGETS_END},
 
};
 

	
 
static const WindowDesc _land_info_desc = {
 
	-1, -1, 280, 93,
 
	WC_LAND_INFO,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
 
	_land_info_widgets,
 
	LandInfoWndProc
 
};
 

	
 
static void Place_LandInfo(uint tile)
 
static void Place_LandInfo(TileIndex tile)
 
{
 
	Player *p;
 
	static LandInfoData lid;
 
	Window *w;
 
	int64 old_money;
 

	
 
	DeleteWindowById(WC_LAND_INFO, 0);
 

	
 
	w = AllocateWindowDesc(&_land_info_desc);
 
	WP(w,void_d).data = &lid;
 

	
 
	lid.tile = tile;
network_data.c
Show inline comments
 
@@ -387,25 +387,25 @@ void NetworkAddCommandQueue(NetworkClien
 
	*new_cp = *cp;
 

	
 
	if (cs->command_queue == NULL)
 
		cs->command_queue = new_cp;
 
	else {
 
		CommandPacket *c = cs->command_queue;
 
		while (c->next != NULL) c = c->next;
 
		c->next = new_cp;
 
	}
 
}
 

	
 
// Prepare a DoCommand to be send over the network
 
void NetworkSend_Command(uint32 tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
 
void NetworkSend_Command(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallback *callback)
 
{
 
	CommandPacket *c = malloc(sizeof(CommandPacket));
 
	byte temp_callback;
 

	
 
	c->player = _local_player;
 
	c->next = NULL;
 
	c->tile = tile;
 
	c->p1 = p1;
 
	c->p2 = p2;
 
	c->cmd = cmd;
 
	c->callback = 0;
 

	
network_data.h
Show inline comments
 
@@ -28,25 +28,25 @@ typedef struct Packet {
 
	struct Packet *next;
 
	PacketSize size;
 
	PacketSize pos;
 
	byte buffer[SEND_MTU];
 
} Packet;
 

	
 
typedef struct CommandPacket {
 
	struct CommandPacket *next;
 
	byte player;   /// player that is executing the command (PlayerID)
 
	uint32 cmd;    /// command being executed
 
	uint32 p1;     /// parameter p1
 
	uint32 p2;     /// parameter p2
 
	uint32 tile;   /// tile command being executed on ; always make it uint32, so it is bigmap compatible (TileIndex)
 
	TileIndex tile; /// tile command being executed on
 
	char text[80];
 
	uint32 frame;  /// the frame in which this packet is executed
 
	byte callback; /// any callback function executed upon successful completion of the command
 
} CommandPacket;
 

	
 
typedef enum {
 
	STATUS_INACTIVE,
 
	STATUS_AUTH, // This means that the client is authorized
 
	STATUS_MAP_WAIT, // This means that the client is put on hold because someone else is getting the map
 
	STATUS_MAP,
 
	STATUS_DONE_MAP,
 
	STATUS_PRE_ACTIVE,
openttd.c
Show inline comments
 
@@ -1172,25 +1172,25 @@ void BeforeSaveGame(void)
 
{
 
	Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
 

	
 
	if (w != NULL) {
 
		_saved_scrollpos_x = WP(w,vp_d).scrollpos_x;
 
		_saved_scrollpos_y = WP(w,vp_d).scrollpos_y;
 
		_saved_scrollpos_zoom = w->viewport->zoom;
 
	}
 
}
 

	
 
static void ConvertTownOwner(void)
 
{
 
	uint tile;
 
	TileIndex tile;
 

	
 
	for (tile = 0; tile != MapSize(); tile++) {
 
		if (IsTileType(tile, MP_STREET)) {
 
			if (IsLevelCrossing(tile) && _map3_lo[tile] & 0x80)
 
				_map3_lo[tile] = OWNER_TOWN;
 

	
 
			if (_map_owner[tile] & 0x80) SetTileOwner(tile, OWNER_TOWN);
 
		} else if (IsTileType(tile, MP_TUNNELBRIDGE)) {
 
			if (_map_owner[tile] & 0x80) SetTileOwner(tile, OWNER_TOWN);
 
		}
 
	}
 
}
openttd.h
Show inline comments
 
@@ -107,25 +107,25 @@ typedef enum TransportTypes {
 
	TRANSPORT_ROAD = 1,
 
	TRANSPORT_WATER,	// = 2
 
	TRANSPORT_END,
 
	INVALID_TRANSPORT = 0xff,
 
} TransportType;
 

	
 
typedef struct TileInfo {
 
	uint x;
 
	uint y;
 
	uint tileh;
 
	uint type;
 
	uint map5;
 
	uint tile;
 
	TileIndex tile;
 
	uint z;
 
} TileInfo;
 

	
 
enum {
 
	NG_EDGE = 1,
 
};
 

	
 
/* Display Options */
 
enum {
 
	DO_SHOW_TOWN_NAMES =    1 << 0,
 
	DO_SHOW_STATION_NAMES = 1 << 1,
 
	DO_SHOW_SIGNS =         1 << 2,
 
@@ -279,58 +279,58 @@ typedef struct TileDesc {
 
} TileDesc;
 

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

	
 
typedef int32 CommandProc(int x, int y, uint32 flags, uint32 p1, uint32 p2);
 

	
 
typedef void DrawTileProc(TileInfo *ti);
 
typedef uint GetSlopeZProc(TileInfo *ti);
 
typedef int32 ClearTileProc(uint tile, byte flags);
 
typedef void GetAcceptedCargoProc(uint tile, AcceptedCargo res);
 
typedef void GetTileDescProc(uint tile, TileDesc *td);
 
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).
 
 *
 
 * The result (let's call it ts) is often used as follows:
 
 * tracks = (byte)(ts | ts >>8)
 
 * This effectively converts the present part of the result (ccdd) to a
 
 * track bitmask, which disregards directions. Normally, this is the same as just
 
 * doing (byte)ts I think, although I am not really sure
 
 *
 
 * A trackdir is combination of a track and a dir, where the lower three bits
 
 * are a track, the fourth bit is the direction. these give 12 (or 14)
 
 * possible options: 0-5 and 8-13, so we need 14 bits for a trackdir bitmask
 
 * above.
 
 */
 
typedef uint32 GetTileTrackStatusProc(uint tile, TransportType mode);
 
typedef void GetProducedCargoProc(uint tile, byte *b);
 
typedef void ClickTileProc(uint tile);
 
typedef void AnimateTileProc(uint tile);
 
typedef void TileLoopProc(uint tile);
 
typedef void ChangeTileOwnerProc(uint tile, byte old_player, byte new_player);
 
typedef uint32 GetTileTrackStatusProc(TileIndex tile, TransportType mode);
 
typedef void GetProducedCargoProc(TileIndex tile, byte *b);
 
typedef void ClickTileProc(TileIndex tile);
 
typedef void AnimateTileProc(TileIndex tile);
 
typedef void TileLoopProc(TileIndex tile);
 
typedef void ChangeTileOwnerProc(TileIndex tile, byte old_player, byte new_player);
 
/* Return value has bit 0x2 set, when the vehicle enters a station. Then,
 
 * result << 8 contains the id of the station entered. If the return value has
 
 * bit 0x8 set, the vehicle could not and did not enter the tile. Are there
 
 * other bits that can be set? */
 
typedef uint32 VehicleEnterTileProc(Vehicle *v, uint tile, int x, int y);
 
typedef void VehicleLeaveTileProc(Vehicle *v, uint tile, int x, int y);
 
typedef uint32 VehicleEnterTileProc(Vehicle *v, TileIndex tile, int x, int y);
 
typedef void VehicleLeaveTileProc(Vehicle *v, TileIndex tile, int x, int y);
 
typedef uint GetSlopeTilehProc(TileInfo *ti);
 

	
 
typedef struct {
 
	DrawTileProc *draw_tile_proc;
 
	GetSlopeZProc *get_slope_z_proc;
 
	ClearTileProc *clear_tile_proc;
 
	GetAcceptedCargoProc *get_accepted_cargo_proc;
 
	GetTileDescProc *get_tile_desc_proc;
 
	GetTileTrackStatusProc *get_tile_track_status_proc;
 
	ClickTileProc *click_tile_proc;
 
	AnimateTileProc *animate_tile_proc;
 
	TileLoopProc *tile_loop_proc;
 
@@ -512,25 +512,25 @@ enum SpecialStrings {
 
	// reserve 32 strings for various screen resolutions
 
	SPECSTR_RESOLUTION_START = 0x7120,
 
	SPECSTR_RESOLUTION_END = 0x713f,
 

	
 
	// reserve 32 strings for screenshot formats
 
	SPECSTR_SCREENSHOT_START = 0x7140,
 
	SPECSTR_SCREENSHOT_END = 0x715F,
 

	
 
	STR_SPEC_SCREENSHOT_NAME = 0xF800,
 
	STR_SPEC_USERSTRING = 0xF801,
 
};
 

	
 
typedef void PlaceProc(uint tile);
 
typedef void PlaceProc(TileIndex tile);
 

	
 
VARDEF byte _savegame_sort_order;
 

	
 
#define INVALID_UINT_TILE (uint)0xFFFFFFFF
 
#define INVALID_STRING_ID 0xFFFF
 

	
 
enum {
 
	MAX_SCREEN_WIDTH = 2048,
 
	MAX_SCREEN_HEIGHT = 1200,
 
};
 

	
 
/* In certain windows you navigate with the arrow keys. Do not scroll the
order_gui.c
Show inline comments
 
@@ -174,25 +174,25 @@ static void DrawOrdersWindow(Window *w)
 

	
 
		i++;
 
		order = order->next;
 
	}
 

	
 
	if (i - w->vscroll.pos < w->vscroll.cap) {
 
		str = shared_orders ? STR_END_OF_SHARED_ORDERS : STR_882A_END_OF_ORDERS;
 
		color = (i == WP(w,order_d).sel) ? 0xC : 0x10;
 
		DrawString(2, y, str, color);
 
	}
 
}
 

	
 
static Order GetOrderCmdFromTile(Vehicle *v, uint tile)
 
static Order GetOrderCmdFromTile(Vehicle *v, TileIndex tile)
 
{
 
	Order order;
 
	Station *st;
 
	int st_index;
 

	
 
	// check depot first
 
	if (_patches.gotodepot) {
 
		switch (GetTileType(tile)) {
 
		case MP_RAILWAY:
 
			if (v->type == VEH_Train && IsTileOwner(tile, _local_player)) {
 
				if ((_map5[tile]&0xFC)==0xC0) {
 
					order.type = OT_GOTO_DEPOT;
 
@@ -292,25 +292,25 @@ static bool HandleOrderVehClick(Vehicle 
 
	// obviously if you press CTRL on a non-empty orders vehicle you know what you are doing
 
	if (v->num_orders != 0 && _ctrl_pressed == 0) {return false;}
 

	
 
	if (DoCommandP(v->tile, v->index | (u->index << 16), _ctrl_pressed ? 0 : 1, NULL,
 
		_ctrl_pressed ? CMD_CLONE_ORDER | CMD_MSG(STR_CANT_SHARE_ORDER_LIST) : CMD_CLONE_ORDER | CMD_MSG(STR_CANT_COPY_ORDER_LIST))) {
 
		WP(w,order_d).sel = -1;
 
		ResetObjectToPlace();
 
	}
 

	
 
	return true;
 
}
 

	
 
static void OrdersPlaceObj(Vehicle *v, uint tile, Window *w)
 
static void OrdersPlaceObj(Vehicle *v, TileIndex tile, Window *w)
 
{
 
	Order cmd;
 
	Vehicle *u;
 

	
 
	// check if we're clicking on a vehicle first.. clone orders in that case.
 
	u = CheckMouseOverVehicle();
 
	if (u && HandleOrderVehClick(v, u, w))
 
		return;
 

	
 
	cmd = GetOrderCmdFromTile(v, tile);
 
	if (cmd.type == OT_NOTHING) return;
 

	
pathfind.c
Show inline comments
 
#include "stdafx.h"
 
#include "openttd.h"
 
#include "map.h"
 
#include "tile.h"
 
#include "pathfind.h"
 

	
 
// remember which tiles we have already visited so we don't visit them again.
 
static bool TPFSetTileBit(TrackPathFinder *tpf, uint tile, int dir)
 
static bool TPFSetTileBit(TrackPathFinder *tpf, TileIndex tile, int dir)
 
{
 
	uint hash, val, offs;
 
	TrackPathFinderLink *link, *new_link;
 
	uint bits = 1 << dir;
 

	
 
	if (tpf->disable_tile_hash)
 
		return true;
 

	
 
	hash = PATHFIND_HASH_TILE(tile);
 

	
 
	val = tpf->hash_head[hash];
 

	
 
@@ -111,29 +111,29 @@ static const byte _tpf_prev_direction[14
 
	2,3,2,3,0,3,
 
};
 

	
 

	
 
static const byte _otherdir_mask[4] = {
 
	0x10,
 
	0,
 
	0x5,
 
	0x2A,
 
};
 

	
 
#ifdef DEBUG_TILE_PUSH
 
extern void dbg_push_tile(uint tile, int track);
 
extern void dbg_push_tile(TileIndex tile, int track);
 
extern void dbg_pop_tile();
 
#endif
 

	
 
static void TPFMode2(TrackPathFinder *tpf, uint tile, int direction)
 
static void TPFMode2(TrackPathFinder *tpf, TileIndex tile, int direction)
 
{
 
	uint bits;
 
	int i;
 
	RememberData rd;
 
	int owner = -1;
 

	
 
	if (tpf->tracktype == TRANSPORT_RAIL) {
 
		if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE)) {
 
			owner = GetTileOwner(tile);
 
			/* Check if we are on the middle of a bridge (has no owner) */
 
			if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xC0) == 0xC0)
 
				owner = -1;
 
@@ -202,25 +202,25 @@ continue_here:;
 
#endif
 

	
 
		tpf->rd = rd;
 
	} while (++i, bits>>=1);
 

	
 
}
 

	
 
static const int8 _get_tunlen_inc[5] = { -16, 0, 16, 0, -16 };
 

	
 
/* Returns the end tile and the length of a tunnel. The length does not
 
 * include the starting tile (entry), it does include the end tile (exit).
 
 */
 
FindLengthOfTunnelResult FindLengthOfTunnel(uint tile, int direction)
 
FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, int direction)
 
{
 
	FindLengthOfTunnelResult flotr;
 
	int x,y;
 
	byte z;
 

	
 
	flotr.length = 0;
 

	
 
	x = TileX(tile) * 16;
 
	y = TileY(tile) * 16;
 

	
 
	z = GetSlopeZ(x+8, y+8);
 

	
 
@@ -237,25 +237,26 @@ FindLengthOfTunnelResult FindLengthOfTun
 
				//((_map5[tile]>>2)&3) == type &&		// rail/road-tunnel <-- This is not necesary to check, right?
 
				((_map5[tile] & 3)^2) == direction &&	// entrance towards: 0 = NE, 1 = SE, 2 = SW, 3 = NW
 
				GetSlopeZ(x+8, y+8) == z)
 
					break;
 
	}
 

	
 
	flotr.tile = tile;
 
	return flotr;
 
}
 

	
 
static const uint16 _tpfmode1_and[4] = { 0x1009, 0x16, 0x520, 0x2A00 };
 

	
 
static uint SkipToEndOfTunnel(TrackPathFinder *tpf, uint tile, int direction) {
 
static uint SkipToEndOfTunnel(TrackPathFinder *tpf, TileIndex tile, int direction)
 
{
 
	FindLengthOfTunnelResult flotr;
 
	TPFSetTileBit(tpf, tile, 14);
 
	flotr = FindLengthOfTunnel(tile, direction);
 
	tpf->rd.cur_length += flotr.length;
 
	TPFSetTileBit(tpf, flotr.tile, 14);
 
	return flotr.tile;
 
}
 

	
 
const byte _ffb_64[128] = {
 
0,0,1,0,2,0,1,0,
 
3,0,1,0,2,0,1,0,
 
4,0,1,0,2,0,1,0,
 
@@ -266,30 +267,30 @@ 4,0,1,0,2,0,1,0,
 
3,0,1,0,2,0,1,0,
 

	
 
0,0,0,2,0,4,4,6,
 
0,8,8,10,8,12,12,14,
 
0,16,16,18,16,20,20,22,
 
16,24,24,26,24,28,28,30,
 
0,32,32,34,32,36,36,38,
 
32,40,40,42,40,44,44,46,
 
32,48,48,50,48,52,52,54,
 
48,56,56,58,56,60,60,62,
 
};
 

	
 
static void TPFMode1(TrackPathFinder *tpf, uint tile, int direction)
 
static void TPFMode1(TrackPathFinder *tpf, TileIndex tile, int direction)
 
{
 
	uint bits;
 
	int i;
 
	RememberData rd;
 
	uint tile_org = tile;
 
	TileIndex tile_org = tile;
 

	
 
	if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xF0) == 0) {
 
		if ((_map5[tile] & 3) != direction || ((_map5[tile]>>2)&3) != tpf->tracktype)
 
			return;
 
		tile = SkipToEndOfTunnel(tpf, tile, direction);
 
	}
 
	tile += TileOffsByDir(direction);
 

	
 
	/* Check in case of rail if the owner is the same */
 
	if (tpf->tracktype == TRANSPORT_RAIL) {
 
		if (IsTileType(tile_org, MP_RAILWAY) || IsTileType(tile_org, MP_STATION) || IsTileType(tile_org, MP_TUNNELBRIDGE))
 
			if (IsTileType(tile, MP_RAILWAY) || IsTileType(tile, MP_STATION) || IsTileType(tile, MP_TUNNELBRIDGE))
 
@@ -361,25 +362,25 @@ static void TPFMode1(TrackPathFinder *tp
 
		bits = KILL_FIRST_BIT(bits);
 

	
 
		tpf->the_dir = (_otherdir_mask[direction] & (byte)(1 << i)) ? (i+8) : i;
 
		rd = tpf->rd;
 
		if (TPFSetTileBit(tpf, tile, tpf->the_dir) &&
 
				!tpf->enum_proc(tile, tpf->userdata, tpf->the_dir, tpf->rd.cur_length, &tpf->rd.pft_var6) ) {
 
			TPFMode1(tpf, tile, _tpf_new_direction[tpf->the_dir]);
 
		}
 
		tpf->rd = rd;
 
	} while (bits != 0);
 
}
 

	
 
void FollowTrack(uint tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
 
void FollowTrack(TileIndex tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data)
 
{
 
	TrackPathFinder tpf;
 

	
 
	assert(direction < 4);
 

	
 
	/* initialize path finder variables */
 
	tpf.userdata = data;
 
	tpf.enum_proc = enum_proc;
 
	tpf.new_link = tpf.links;
 
	tpf.num_links_left = lengthof(tpf.links);
 

	
 
	tpf.rd.cur_length = 0;
 
@@ -504,25 +505,25 @@ static inline void HeapifyDown(NewTrackP
 
		if (ARR(i).cur_length <= ARR(j).cur_length)
 
			break; // base elem smaller than smallest, done!
 

	
 
		// swap parent with the child
 
		si = ARR(i); ARR(i) = ARR(j); ARR(j) = si;
 
		i = j;
 
	}
 
}
 

	
 
// mark a tile as visited and store the length of the path.
 
// if we already had a better path to this tile, return false.
 
// otherwise return true.
 
static bool NtpVisit(NewTrackPathFinder *tpf, uint tile, uint dir, uint length)
 
static bool NtpVisit(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length)
 
{
 
	uint hash,head;
 
	HashLink *link, *new_link;
 

	
 
	assert(length < 1024);
 

	
 
	hash = PATHFIND_HASH_TILE(tile);
 

	
 
	// never visited before?
 
	if ((head=tpf->hash_head[hash]) == 0) {
 
		tpf->hash_tile[hash] = tile;
 
		tpf->hash_head[hash] = dir | (length << 2);
 
@@ -578,25 +579,25 @@ static bool NtpVisit(NewTrackPathFinder 
 
	new_link = tpf->new_link++;
 

	
 
	/* then fill the link with the new info, and establish a ptr from the old
 
	 * link to the new one */
 
	new_link->tile = (TileIndex)tile;
 
	new_link->typelength = dir | (length << 2);
 
	new_link->next = 0xFFFF;
 

	
 
	link->next = NTP_GET_LINK_OFFS(tpf, new_link);
 
	return true;
 
}
 

	
 
static bool NtpCheck(NewTrackPathFinder *tpf, uint tile, uint dir, uint length)
 
static bool NtpCheck(NewTrackPathFinder *tpf, TileIndex tile, uint dir, uint length)
 
{
 
	uint hash,head,offs;
 
	HashLink *link;
 

	
 
	hash = PATHFIND_HASH_TILE(tile);
 
	head=tpf->hash_head[hash];
 
	assert(head);
 

	
 
	if (head != 0xffff) {
 
		assert( tpf->hash_tile[hash] == tile && (head & 3) == dir);
 
		assert( (head >> 2) <= length);
 
		return length == (head >> 2);
 
@@ -608,25 +609,25 @@ static bool NtpCheck(NewTrackPathFinder 
 
		link = NTP_GET_LINK_PTR(tpf, offs);
 
		if ( (TileIndex)tile == link->tile && (uint)(link->typelength & 0x3) == dir) {
 
			assert( (uint)(link->typelength >> 2) <= length);
 
			return length == (uint)(link->typelength >> 2);
 
		}
 
		offs = link->next;
 
		assert(offs != 0xffff);
 
	}
 
}
 

	
 

	
 
// new more optimized pathfinder for trains...
 
static void NTPEnum(NewTrackPathFinder *tpf, uint tile, uint direction)
 
static void NTPEnum(NewTrackPathFinder *tpf, TileIndex tile, uint direction)
 
{
 
	uint bits, tile_org;
 
	int i;
 
	StackedItem si;
 
	FindLengthOfTunnelResult flotr;
 

	
 
	si.cur_length = 0;
 
	si.depth = 0;
 
	si.state = 0;
 

	
 
restart:
 
	if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xF0) == 0) {
 
@@ -735,25 +736,25 @@ popnext:
 
		HeapifyDown(tpf);
 
	} while (
 
		!NtpCheck(tpf, tile, _tpf_prev_direction[si.track], si.cur_length) || // already have better path to that tile?
 
		tpf->enum_proc(tile, tpf->userdata, si.track, si.cur_length, &si.state)
 
	);
 

	
 
	direction = _tpf_new_direction[si.track];
 
	goto restart;
 
}
 

	
 

	
 
// new pathfinder for trains. better and faster.
 
void NewTrainPathfind(uint tile, byte direction, TPFEnumProc *enum_proc, void *data, byte *cache)
 
void NewTrainPathfind(TileIndex tile, byte direction, TPFEnumProc *enum_proc, void *data, byte *cache)
 
{
 
	if (!_patches.new_pathfinding) {
 
		FollowTrack(tile, 0x3000 | TRANSPORT_RAIL, direction, enum_proc, NULL, data);
 
	} else {
 
		NewTrackPathFinder tpf;
 
		tpf.userdata = data;
 
		tpf.enum_proc = enum_proc;
 
		tpf.tracktype = 0;
 
		tpf.maxlength = _patches.pf_maxlength;
 
		tpf.nstack = 0;
 
		tpf.new_link = tpf.links;
 
		tpf.num_links_left = lengthof(tpf.links);
pathfind.h
Show inline comments
 
#ifndef PATHFIND_H
 
#define PATHFIND_H
 

	
 
//#define PF_BENCH // perform simple benchmarks on the train pathfinder (not
 
//supported on all archs)
 

	
 
typedef struct TrackPathFinder TrackPathFinder;
 
typedef bool TPFEnumProc(uint tile, void *data, int track, uint length, byte *state);
 
typedef bool TPFEnumProc(TileIndex tile, void *data, int track, uint length, byte *state);
 
typedef void TPFAfterProc(TrackPathFinder *tpf);
 

	
 

	
 
#define PATHFIND_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links)
 
#define PATHFIND_GET_LINK_PTR(tpf, link_offs) (TrackPathFinderLink*)((byte*)tpf->links + (link_offs))
 

	
 
/* y7 y6 y5 y4 y3 y2 y1 y0 x7 x6 x5 x4 x3 x2 x1 x0
 
 * y7 y6 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0  0  0  0
 
 *  0  0 y7 y6 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0  0
 
 *  0  0  0  0 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0  0
 
 */
 
#define PATHFIND_HASH_TILE(tile) (TileX(tile) & 0x1F) + ((TileY(tile) & 0x1F) << 5)
 
@@ -46,23 +46,23 @@ struct TrackPathFinder {
 

	
 
	byte tracktype;
 
	byte var2;
 
	bool disable_tile_hash;
 
	bool hasbit_13;
 

	
 
	uint16 hash_head[0x400];
 
	TileIndex hash_tile[0x400]; /* stores the link index when multi link. */
 

	
 
	TrackPathFinderLink links[0x400]; /* hopefully, this is enough. */
 
};
 

	
 
void FollowTrack(uint tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data);
 
void FollowTrack(TileIndex tile, uint16 flags, byte direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data);
 

	
 
typedef struct {
 
	uint tile;
 
	TileIndex tile;
 
	int length;
 
} FindLengthOfTunnelResult;
 
FindLengthOfTunnelResult FindLengthOfTunnel(uint tile, int direction);
 
FindLengthOfTunnelResult FindLengthOfTunnel(TileIndex tile, int direction);
 

	
 
void NewTrainPathfind(uint tile, byte direction, TPFEnumProc *enum_proc, void *data, byte *cache);
 
void NewTrainPathfind(TileIndex tile, byte direction, TPFEnumProc *enum_proc, void *data, byte *cache);
 

	
 
#endif /* PATHFIND_H */
player.h
Show inline comments
 
@@ -178,25 +178,25 @@ typedef struct Player {
 

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

	
 
	int64 yearly_expenses[3][13];
 
	PlayerEconomyEntry cur_economy;
 
	PlayerEconomyEntry old_economy[24];
 
} Player;
 

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

	
 
#define MAX_PLAYERS 8
 
VARDEF Player _players[MAX_PLAYERS];
 

	
 
static inline Player* GetPlayer(uint i)
 
{
 
  assert(i < lengthof(_players));
players.c
Show inline comments
 
@@ -239,25 +239,25 @@ void SubtractMoneyFromPlayerFract(byte p
 

	
 
// 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(byte owner, uint tile)
 
void GetNameOfOwner(byte owner, TileIndex tile)
 
{
 
	SetDParam(2, owner);
 

	
 
	if (owner != OWNER_TOWN) {
 
		if (owner >= 8)
 
			SetDParam(0, STR_0150_SOMEONE);
 
		else {
 
			Player *p = GetPlayer(owner);
 
			SetDParam(0, p->name_1);
 
			SetDParam(1, p->name_2);
 
		}
 
	} else {
 
@@ -270,43 +270,43 @@ void GetNameOfOwner(byte owner, uint til
 

	
 
bool CheckOwnership(byte owner)
 
{
 
	assert(owner <= OWNER_WATER);
 

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

	
 
bool CheckTileOwnership(uint tile)
 
bool CheckTileOwnership(TileIndex tile)
 
{
 
	byte owner = GetTileOwner(tile);
 

	
 
	assert(owner <= OWNER_WATER);
 

	
 
	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 (_current_player == _local_player)
 
		GetNameOfOwner(owner, tile);
 
	return false;
 
}
 

	
 
static void GenerateCompanyName(Player *p)
 
{
 
	uint tile;
 
	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;
rail_cmd.c
Show inline comments
 
@@ -12,25 +12,25 @@
 
#include "pathfind.h"
 
#include "engine.h"
 
#include "town.h"
 
#include "sound.h"
 
#include "station.h"
 
#include "sprite.h"
 
#include "depot.h"
 
#include "waypoint.h"
 
#include "rail.h"
 

	
 
extern uint16 _custom_sprites_base;
 

	
 
void ShowTrainDepotWindow(uint tile);
 
void ShowTrainDepotWindow(TileIndex tile);
 

	
 
/* Format of rail map5 byte.
 
 * 00 abcdef  => Normal rail
 
 * 01 abcdef  => Rail with signals
 
 * 10 ??????  => Unused
 
 * 11 ????dd  => Depot
 
 *
 
 * abcdef is a bitmask, which contains ones for all present tracks. Below the
 
 * value for each track is given.
 
 */
 

	
 
/*         4
 
@@ -943,47 +943,47 @@ int32 CmdRemoveSingleSignal(int x, int y
 
	return _price.remove_signals;
 
}
 

	
 
/** Remove signals on a stretch of track.
 
 * Stub for the unified signal builder/remover
 
 * @see CmdSignalTrackHelper
 
 */
 
int32 CmdRemoveSignalTrack(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	return CmdSignalTrackHelper(x, y, flags, p1, SETBIT(p2, 0));
 
}
 

	
 
typedef int32 DoConvertRailProc(uint tile, uint totype, bool exec);
 
typedef int32 DoConvertRailProc(TileIndex tile, uint totype, bool exec);
 

	
 
static int32 DoConvertRail(uint tile, uint totype, bool exec)
 
static int32 DoConvertRail(TileIndex tile, uint totype, bool exec)
 
{
 
	if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile))
 
		return CMD_ERROR;
 

	
 
	// tile is already of requested type?
 
	if ( GetRailType(tile) == totype)
 
		return CMD_ERROR;
 

	
 
	// change type.
 
	if (exec) {
 
		_map3_lo[tile] = (_map3_lo[tile] & 0xF0) + totype;
 
		MarkTileDirtyByTile(tile);
 
	}
 

	
 
	return _price.build_rail / 2;
 
}
 

	
 
extern int32 DoConvertStationRail(uint tile, uint totype, bool exec);
 
extern int32 DoConvertStreetRail(uint tile, uint totype, bool exec);
 
extern int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec);
 
extern int32 DoConvertStationRail(TileIndex tile, uint totype, bool exec);
 
extern int32 DoConvertStreetRail(TileIndex tile, uint totype, bool exec);
 
extern int32 DoConvertTunnelBridgeRail(TileIndex tile, uint totype, bool exec);
 

	
 
/** Convert one rail type to the other. You can convert normal rail to
 
 * monorail/maglev easily or vice-versa.
 
 * @param ex,ey end tile of rail conversion drag
 
 * @param p1 start tile of drag
 
 * @param p2 new railtype to convert to
 
 */
 
int32 CmdConvertRail(int ex, int ey, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	int32 ret, cost, money;
 
	int sx, sy, x, y;
 

	
 
@@ -1017,25 +1017,25 @@ int32 CmdConvertRail(int ex, int ey, uin
 
			cost += ret;
 

	
 
			if (flags & DC_EXEC) {
 
				if ( (money -= ret) < 0) { _additional_cash_required = ret; return cost - ret; }
 
				proc(tile, p2, true);
 
			}
 
		}
 
	}
 

	
 
	return (cost == 0) ? CMD_ERROR : cost;
 
}
 

	
 
static int32 RemoveTrainDepot(uint tile, uint32 flags)
 
static int32 RemoveTrainDepot(TileIndex tile, uint32 flags)
 
{
 
	if (!CheckTileOwnership(tile) && _current_player != OWNER_WATER)
 
		return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicle(tile))
 
		return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		int track = TrackdirToTrack(DiagdirToDiagTrackdir(GetDepotDirection(tile, TRANSPORT_RAIL)));
 

	
 
		DoDeleteDepot(tile);
 
		SetSignalsOnBothDir(tile, track);
 
@@ -1528,25 +1528,25 @@ typedef struct SetSignalsData {
 
	int presignal_exits_free;
 

	
 
	// these are used to keep track of the signals that change.
 
	byte bit[NUM_SSD_ENTRY];
 
	TileIndex tile[NUM_SSD_ENTRY];
 

	
 
	// these are used to keep track of the stack that modifies presignals recursively
 
	TileIndex next_tile[NUM_SSD_STACK];
 
	byte next_dir[NUM_SSD_STACK];
 

	
 
} SetSignalsData;
 

	
 
static bool SetSignalsEnumProc(uint tile, SetSignalsData *ssd, int track, uint length, byte *state)
 
static bool SetSignalsEnumProc(TileIndex tile, SetSignalsData *ssd, int track, uint length, byte *state)
 
{
 
	// the tile has signals?
 
	if (IsTileType(tile, MP_RAILWAY)) {
 
		if (HasSignalOnTrack(tile, TrackdirToTrack(track))) {
 
			if ((_map3_lo[tile] & _signals_table[track]) != 0) {
 
				// yes, add the signal to the list of signals
 
				if (ssd->cur != NUM_SSD_ENTRY) {
 
					ssd->tile[ssd->cur] = tile; // remember the tile index
 
					ssd->bit[ssd->cur] = track; // and the controlling bit number
 
					ssd->cur++;
 
				}
 

	
 
@@ -1688,25 +1688,25 @@ static const byte _dir_from_track[14] = 
 
static void ChangeSignalStates(SetSignalsData *ssd)
 
{
 
	int i;
 

	
 
	// thinking about presignals...
 
	// the presignal is green if,
 
	//   if no train is in the segment AND
 
	//   there is at least one green exit signal OR
 
	//   there are no exit signals in the segment
 

	
 
	// then mark the signals in the segment accordingly
 
	for(i=0; i!=ssd->cur; i++) {
 
		uint tile = ssd->tile[i];
 
		TileIndex tile = ssd->tile[i];
 
		byte bit = _signals_table[ssd->bit[i]];
 
		uint16 m2 = _map2[tile];
 

	
 
		// presignals don't turn green if there is at least one presignal exit and none are free
 
		if (_map3_hi[tile] & 1) {
 
			int ex = ssd->presignal_exits, exfree = ssd->presignal_exits_free;
 

	
 
			// subtract for dual combo signals so they don't count themselves
 
			if (_map3_hi[tile]&2 && _map3_lo[tile]&_signals_table_other[ssd->bit[i]]) {
 
				ex--;
 
				if ((_map2[tile]&_signals_table_other[ssd->bit[i]]) != 0) exfree--;
 
			}
 
@@ -1736,25 +1736,25 @@ make_red:
 
			} else {
 
				printf("NUM_SSD_STACK too small\n"); /// @todo WTF is this???
 
			}
 
		}
 

	
 
		// it changed, so toggle it
 
		_map2[tile] = m2 ^ bit;
 
		MarkTileDirtyByTile(tile);
 
	}
 
}
 

	
 

	
 
bool UpdateSignalsOnSegment(uint tile, byte direction)
 
bool UpdateSignalsOnSegment(TileIndex tile, byte direction)
 
{
 
	SetSignalsData ssd;
 
	int result = -1;
 

	
 
	ssd.cur_stack = 0;
 
	direction>>=1;
 

	
 
	for(;;) {
 
		// go through one segment and update all signals pointing into that segment.
 
		ssd.cur = ssd.presignal_exits = ssd.presignal_exits_free = 0;
 
		ssd.has_presignal = false;
 

	
 
@@ -1767,25 +1767,25 @@ bool UpdateSignalsOnSegment(uint tile, b
 
		// if any exit signals were changed, we need to keep going to modify the stuff behind those.
 
		if(!ssd.cur_stack)
 
			break;
 

	
 
		// one or more exit signals were changed, so we need to update another segment too.
 
		tile = ssd.next_tile[--ssd.cur_stack];
 
		direction = ssd.next_dir[ssd.cur_stack];
 
	}
 

	
 
	return (bool)result;
 
}
 

	
 
void SetSignalsOnBothDir(uint tile, byte track)
 
void SetSignalsOnBothDir(TileIndex tile, byte track)
 
{
 
	static const byte _search_dir_1[6] = {1, 3, 1, 3, 5, 3};
 
	static const byte _search_dir_2[6] = {5, 7, 7, 5, 7, 1};
 

	
 
	UpdateSignalsOnSegment(tile, _search_dir_1[track]);
 
	UpdateSignalsOnSegment(tile, _search_dir_2[track]);
 
}
 

	
 
static uint GetSlopeZ_Track(TileInfo *ti)
 
{
 
	uint z = ti->z;
 
	int th = ti->tileh;
 
@@ -1824,35 +1824,35 @@ static uint GetSlopeTileh_Track(TileInfo
 
				}
 
				// inclined foundation
 
				return _inclined_tileh[f - 15];
 
			}
 
		} else if ((ti->map5 & 0xC0) == 0xC0) {
 
			// depot or waypoint
 
			return 0;
 
		}
 
	}
 
	return ti->tileh;
 
}
 

	
 
static void GetAcceptedCargo_Track(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Track(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* not used */
 
}
 

	
 
static void AnimateTile_Track(uint tile)
 
static void AnimateTile_Track(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void TileLoop_Track(uint tile)
 
static void TileLoop_Track(TileIndex tile)
 
{
 
	byte a2;
 
	byte rail;
 
	uint16 m2;
 
	byte owner;
 

	
 
	m2 = _map2[tile] & 0xF;
 

	
 
	/* special code for alps landscape */
 
	if (_opt.landscape == LT_HILLY) {
 
		/* convert into snow? */
 
		if (GetTileZ(tile) > _opt.snow_line) {
 
@@ -1920,25 +1920,26 @@ static void TileLoop_Track(uint tile)
 
		}
 
	}
 

	
 
modify_me:;
 
	/* tile changed? */
 
	if ( m2 != a2) {
 
		_map2[tile] = (_map2[tile] & ~RAIL_MAP2LO_GROUND_MASK) | a2;
 
		MarkTileDirtyByTile(tile);
 
	}
 
}
 

	
 

	
 
static uint32 GetTileTrackStatus_Track(uint tile, TransportType mode) {
 
static uint32 GetTileTrackStatus_Track(TileIndex tile, TransportType mode)
 
{
 
	byte m5, a;
 
	uint16 b;
 
	uint32 ret;
 

	
 
	if (mode != TRANSPORT_RAIL)
 
		return 0;
 

	
 
	m5 = _map5[tile];
 

	
 
	if (!(m5 & RAIL_TYPE_SPECIAL)) {
 
		ret = (m5 | (m5 << 8)) & 0x3F3F;
 
		if (!(m5 & RAIL_TYPE_SIGNALS)) {
 
@@ -1965,25 +1966,25 @@ static uint32 GetTileTrackStatus_Track(u
 
			if ( (b & 0x20) == 0)	ret |= 0x20080000;
 
			if ( (b & 0x10) == 0)	ret |= 0x8200000;
 
		}
 
	} else if (m5 & 0x40) {
 
		static const byte _train_spec_tracks[6] = {1,2,1,2,1,2};
 
		m5 = _train_spec_tracks[m5 & 0x3F];
 
		ret = (m5 << 8) + m5;
 
	} else
 
		return 0;
 
	return ret;
 
}
 

	
 
static void ClickTile_Track(uint tile)
 
static void ClickTile_Track(TileIndex tile)
 
{
 
	if (IsTileDepotType(tile, TRANSPORT_RAIL))
 
		ShowTrainDepotWindow(tile);
 
	else if (IsRailWaypoint(_map5[tile]))
 
		ShowRenameWaypointWindow(GetWaypointByTile(tile));
 

	
 
}
 

	
 
static void GetTileDesc_Track(TileIndex tile, TileDesc *td)
 
{
 
	td->owner = GetTileOwner(tile);
 
	switch (GetRailTileType(tile)) {
 
@@ -2002,46 +2003,46 @@ static void GetTileDesc_Track(TileIndex 
 
			td->str = signal_type[_map3_hi[tile] & 0x03];
 
			break;
 
		}
 

	
 
		case RAIL_TYPE_DEPOT_WAYPOINT:
 
		default:
 
			td->str = ((_map5[tile] & RAIL_SUBTYPE_MASK) == RAIL_SUBTYPE_DEPOT) ?
 
				STR_1023_RAILROAD_TRAIN_DEPOT : STR_LANDINFO_WAYPOINT;
 
			break;
 
	}
 
}
 

	
 
static void ChangeTileOwner_Track(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Track(TileIndex tile, byte old_player, byte new_player)
 
{
 
	if (!IsTileOwner(tile, old_player)) return;
 

	
 
	if (new_player != 255) {
 
		SetTileOwner(tile, new_player);
 
	}	else {
 
		DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 
	}
 
}
 

	
 
static const byte _fractcoords_behind[4] = { 0x8F, 0x8, 0x80, 0xF8 };
 
static const byte _fractcoords_enter[4] = { 0x8A, 0x48, 0x84, 0xA8 };
 
static const byte _deltacoord_leaveoffset[8] = {
 
	-1,  0,  1,  0, /* x */
 
	 0,  1,  0, -1  /* y */
 
};
 
static const byte _enter_directions[4] = {5, 7, 1, 3};
 
static const byte _leave_directions[4] = {1, 3, 5, 7};
 
static const byte _depot_track_mask[4] = {1, 2, 1, 2};
 

	
 
static uint32 VehicleEnter_Track(Vehicle *v, uint tile, int x, int y)
 
static uint32 VehicleEnter_Track(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	byte fract_coord;
 
	byte fract_coord_leave;
 
	int dir;
 
	int length;
 

	
 
	// this routine applies only to trains in depot tiles
 
	if (v->type != VEH_Train || !IsTileDepotType(tile, TRANSPORT_RAIL))
 
		return 0;
 

	
 
	/* depot direction */
 
	dir = GetDepotDirection(tile, TRANSPORT_RAIL);
rail_gui.c
Show inline comments
 
@@ -20,146 +20,146 @@ static bool _remove_button_clicked;
 
static byte _build_depot_direction;
 
static byte _waypoint_count=1;
 
static byte _cur_waypoint_type;
 

	
 
struct {
 
	byte orientation;
 
	byte numtracks;
 
	byte platlength;
 
	bool dragdrop;
 
} _railstation;
 

	
 

	
 
static void HandleStationPlacement(uint start, uint end);
 
static void HandleStationPlacement(TileIndex start, TileIndex end);
 
static void ShowBuildTrainDepotPicker(void);
 
static void ShowBuildWaypointPicker(void);
 
static void ShowStationBuilder(void);
 

	
 
typedef void OnButtonClick(Window *w);
 

	
 
void CcPlaySound1E(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcPlaySound1E(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) SndPlayTileFx(SND_20_SPLAT_2, tile);
 
}
 

	
 
static void GenericPlaceRail(uint tile, int cmd)
 
static void GenericPlaceRail(TileIndex tile, int cmd)
 
{
 
	DoCommandP(tile, _cur_railtype, cmd, CcPlaySound1E,
 
		_remove_button_clicked ?
 
		CMD_REMOVE_SINGLE_RAIL | CMD_MSG(STR_1012_CAN_T_REMOVE_RAILROAD_TRACK) | CMD_AUTO | CMD_NO_WATER :
 
		CMD_BUILD_SINGLE_RAIL | CMD_MSG(STR_1011_CAN_T_BUILD_RAILROAD_TRACK) | CMD_AUTO | CMD_NO_WATER
 
		);
 
}
 

	
 
static void PlaceRail_N(uint tile)
 
static void PlaceRail_N(TileIndex tile)
 
{
 
	int cmd = _tile_fract_coords.x > _tile_fract_coords.y ? 4 : 5;
 
	GenericPlaceRail(tile, cmd);
 
}
 

	
 
static void PlaceRail_NE(uint tile)
 
static void PlaceRail_NE(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_FIX_Y);
 
}
 

	
 
static void PlaceRail_E(uint tile)
 
static void PlaceRail_E(TileIndex tile)
 
{
 
	int cmd = _tile_fract_coords.x + _tile_fract_coords.y <= 15 ? 2 : 3;
 
	GenericPlaceRail(tile, cmd);
 
}
 

	
 
static void PlaceRail_NW(uint tile)
 
static void PlaceRail_NW(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_FIX_X);
 
}
 

	
 
static void PlaceRail_AutoRail(uint tile)
 
static void PlaceRail_AutoRail(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_RAILDIRS);
 
}
 

	
 
static void PlaceExtraDepotRail(uint tile, uint16 extra)
 
static void PlaceExtraDepotRail(TileIndex tile, uint16 extra)
 
{
 
	byte b = _map5[tile];
 

	
 
	if (b & 0xC0 || !(b & (extra >> 8)))
 
		return;
 

	
 
	DoCommandP(tile, _cur_railtype, extra & 0xFF, NULL, CMD_BUILD_SINGLE_RAIL | CMD_AUTO | CMD_NO_WATER);
 
}
 

	
 
static const uint16 _place_depot_extra[12] = {
 
	0x604,		0x2102,		0x1202,		0x505,
 
	0x2400,		0x2801,		0x1800,		0x1401,
 
	0x2203,		0x904,		0x0A05,		0x1103,
 
};
 

	
 

	
 
void CcRailDepot(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcRailDepot(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		int dir = p2;
 

	
 
		SndPlayTileFx(SND_20_SPLAT_2, tile);
 
		ResetObjectToPlace();
 

	
 
		tile += TileOffsByDir(dir);
 

	
 
		if (IsTileType(tile, MP_RAILWAY)) {
 
			PlaceExtraDepotRail(tile, _place_depot_extra[dir]);
 
			PlaceExtraDepotRail(tile, _place_depot_extra[dir + 4]);
 
			PlaceExtraDepotRail(tile, _place_depot_extra[dir + 8]);
 
		}
 
	}
 
}
 

	
 
static void PlaceRail_Depot(uint tile)
 
static void PlaceRail_Depot(TileIndex tile)
 
{
 
	DoCommandP(tile, _cur_railtype, _build_depot_direction, CcRailDepot,
 
		CMD_BUILD_TRAIN_DEPOT | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_100E_CAN_T_BUILD_TRAIN_DEPOT));
 
}
 

	
 
static void PlaceRail_Waypoint(uint tile)
 
static void PlaceRail_Waypoint(TileIndex tile)
 
{
 
	if (!_remove_button_clicked) {
 
		DoCommandP(tile, (_waypoint_count > 0) ? (0x100 + _cur_waypoint_type) : 0, 0, CcPlaySound1E, CMD_BUILD_TRAIN_WAYPOINT | CMD_MSG(STR_CANT_BUILD_TRAIN_WAYPOINT));
 
	} else {
 
		DoCommandP(tile, 0, 0, CcPlaySound1E, CMD_REMOVE_TRAIN_WAYPOINT | CMD_MSG(STR_CANT_REMOVE_TRAIN_WAYPOINT));
 
	}
 
}
 

	
 
void CcStation(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcStation(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_20_SPLAT_2, tile);
 
		ResetObjectToPlace();
 
	}
 
}
 

	
 
static void PlaceRail_Station(uint tile)
 
static void PlaceRail_Station(TileIndex tile)
 
{
 
	if(_remove_button_clicked)
 
		DoCommandP(tile, 0, 0, CcPlaySound1E, CMD_REMOVE_FROM_RAILROAD_STATION | CMD_MSG(STR_CANT_REMOVE_PART_OF_STATION));
 
	else if (_railstation.dragdrop) {
 
		VpStartPlaceSizing(tile, VPM_X_AND_Y_LIMITED);
 
		VpSetPlaceSizingLimit(_patches.station_spread);
 
	} else {
 
		// TODO: Custom station selector GUI. Now we just try using first custom station
 
		// (and fall back to normal stations if it isn't available).
 
		DoCommandP(tile, _railstation.orientation | (_railstation.numtracks<<8) | (_railstation.platlength<<16),_cur_railtype|1<<4, CcStation,
 
				CMD_BUILD_RAILROAD_STATION | CMD_NO_WATER | CMD_AUTO | CMD_MSG(STR_100F_CAN_T_BUILD_RAILROAD_STATION));
 
	}
 
}
 

	
 
static void GenericPlaceSignals(uint tile)
 
static void GenericPlaceSignals(TileIndex tile)
 
{
 
	uint trackstat;
 
	int i;
 

	
 
	trackstat = (byte)GetTileTrackStatus(tile, TRANSPORT_RAIL);
 

	
 
	if ((trackstat & 0x30)) // N-S direction
 
		trackstat = (_tile_fract_coords.x <= _tile_fract_coords.y) ? 0x20 : 0x10;
 

	
 
	if ((trackstat & 0x0C)) // E-W direction
 
		trackstat = (_tile_fract_coords.x + _tile_fract_coords.y <= 15) ? 4 : 8;
 

	
 
@@ -167,56 +167,56 @@ static void GenericPlaceSignals(uint til
 
	i = 0;
 
	if (trackstat != 0) {	while (!(trackstat & 1)) { i++; trackstat >>= 1; }}
 

	
 
	if (!_remove_button_clicked) {
 
		DoCommandP(tile, i + (_ctrl_pressed ? 8 : 0), 0, CcPlaySound1E,
 
			CMD_BUILD_SIGNALS | CMD_AUTO | CMD_MSG(STR_1010_CAN_T_BUILD_SIGNALS_HERE));
 
	} else {
 
		DoCommandP(tile, i, 0, CcPlaySound1E,
 
			CMD_REMOVE_SIGNALS | CMD_AUTO | CMD_MSG(STR_1013_CAN_T_REMOVE_SIGNALS_FROM));
 
	}
 
}
 

	
 
static void PlaceRail_Bridge(uint tile)
 
static void PlaceRail_Bridge(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_OR_Y);
 
}
 

	
 
void CcBuildRailTunnel(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildRailTunnel(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_20_SPLAT_2, tile);
 
		ResetObjectToPlace();
 
	} else {
 
		SetRedErrorSquare(_build_tunnel_endtile);
 
	}
 
}
 

	
 
static void PlaceRail_Tunnel(uint tile)
 
static void PlaceRail_Tunnel(TileIndex tile)
 
{
 
	DoCommandP(tile, _cur_railtype, 0, CcBuildRailTunnel,
 
		CMD_BUILD_TUNNEL | CMD_AUTO | CMD_MSG(STR_5016_CAN_T_BUILD_TUNNEL_HERE));
 
}
 

	
 
void PlaceProc_BuyLand(uint tile)
 
void PlaceProc_BuyLand(TileIndex tile)
 
{
 
	DoCommandP(tile, 0, 0, CcPlaySound1E, CMD_PURCHASE_LAND_AREA | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_5806_CAN_T_PURCHASE_THIS_LAND));
 
}
 

	
 
static void PlaceRail_ConvertRail(uint tile)
 
static void PlaceRail_ConvertRail(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_AND_Y | GUI_PlaceProc_ConvertRailArea);
 
}
 

	
 
static void PlaceRail_AutoSignals(uint tile)
 
static void PlaceRail_AutoSignals(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_SIGNALDIRS);
 
}
 

	
 
static void BuildRailClick_N(Window *w)
 
{
 
	HandlePlacePushButton(w, 4, _cur_railtype*4 + SPR_CURSOR_NS_TRACK, 1, PlaceRail_N);
 
}
 

	
 
static void BuildRailClick_NE(Window *w)
 
{
 
	HandlePlacePushButton(w, 5, _cur_railtype*4 + SPR_CURSOR_SWNE_TRACK, 1, PlaceRail_NE);
 
@@ -464,25 +464,26 @@ static void BuildRailToolbWndProc(Window
 

	
 
	case WE_ABORT_PLACE_OBJ:
 
		UnclickWindowButtons(w);
 
		SetWindowDirty(w);
 

	
 
		w = FindWindowById(WC_BUILD_STATION, 0);
 
		if (w != NULL) WP(w,def_d).close=true;
 
		w = FindWindowById(WC_BUILD_DEPOT, 0);
 
		if (w != NULL) WP(w,def_d).close=true;
 
		break;
 

	
 
	case WE_PLACE_PRESIZE: {
 
		uint tile = e->place.tile;
 
		TileIndex tile = e->place.tile;
 

	
 
		DoCommandByTile(tile, 0, 0, DC_AUTO, CMD_BUILD_TUNNEL);
 
		VpSetPresizeRange(tile, _build_tunnel_endtile==0?tile:_build_tunnel_endtile);
 
	} break;
 
	}
 
}
 

	
 

	
 
static const Widget _build_railroad_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5,STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,     7,    11,   359,     0,    13, STR_100A_RAILROAD_CONSTRUCTION, STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{  WWT_STICKYBOX,   RESIZE_NONE,     7,   360,   371,     0,    13, 0x0,     STR_STICKY_BUTTON},
 

	
 
@@ -610,25 +611,25 @@ void ShowBuildRailToolbar(int index, int
 
		DeleteWindowById(WC_BUILD_TOOLBAR, 0);
 
		_cur_railtype = (byte)index;
 
		w = AllocateWindowDesc(_build_rr_desc[index]);
 
	}
 

	
 
	_remove_button_clicked = false;
 
	if (w != NULL && button >= 0) _build_railroad_button_proc[button](w);
 
}
 

	
 
/* TODO: For custom stations, respect their allowed platforms/lengths bitmasks!
 
 * --pasky */
 

	
 
static void HandleStationPlacement(uint start, uint end)
 
static void HandleStationPlacement(TileIndex start, TileIndex end)
 
{
 
	uint sx = TileX(start);
 
	uint sy = TileY(start);
 
	uint ex = TileX(end);
 
	uint ey = TileY(end);
 
	uint w,h;
 

	
 
	if (sx > ex) uintswap(sx,ex);
 
	if (sy > ey) uintswap(sy,ey);
 
	w = ex - sx + 1;
 
	h = ey - sy + 1;
 
	if (!_railstation.orientation) uintswap(w,h);
road_cmd.c
Show inline comments
 
@@ -12,25 +12,25 @@
 
#include "sound.h"
 
#include "depot.h"
 

	
 
/* When true, GetTrackStatus for roads will treat roads under reconstruction
 
 * as normal roads instead of impassable. This is used when detecting whether
 
 * a road can be removed. This is of course ugly, but I don't know a better
 
 * solution just like that... */
 
static bool _road_special_gettrackstatus;
 

	
 
void RoadVehEnterDepot(Vehicle *v);
 

	
 

	
 
static bool HasTileRoadAt(uint tile, int i)
 
static bool HasTileRoadAt(TileIndex tile, int i)
 
{
 
	int mask;
 
	byte b;
 

	
 
	switch (GetTileType(tile)) {
 
	case MP_STREET:
 
		b = _map5[tile];
 

	
 
		if ((b & 0xF0) == 0) {
 
		} else if (IsLevelCrossing(tile)) {
 
			b = (b&8)?5:10;
 
		} else if ((b & 0xF0) == 0x20) {
 
@@ -49,25 +49,25 @@ static bool HasTileRoadAt(uint tile, int
 
		mask = GetRoadBitsByTile(tile);
 
		b = 10; if (mask & 1) break;
 
		b = 5;  if (mask & 2) break;
 
		return false;
 

	
 
	default:
 
		return false;
 
	}
 

	
 
	return HASBIT(b, i);
 
}
 

	
 
static bool CheckAllowRemoveRoad(uint tile, uint br, bool *edge_road)
 
static bool CheckAllowRemoveRoad(TileIndex tile, uint br, bool *edge_road)
 
{
 
	int blocks;
 
	byte owner;
 
	uint n;
 
	*edge_road = true;
 

	
 
	if (_game_mode == GM_EDITOR)
 
		return true;
 

	
 
	blocks = GetRoadBitsByTile(tile);
 
	if (blocks == 0)
 
		return true;
 
@@ -470,25 +470,25 @@ do_clear:;
 
			_map5[tile] = 0;
 
			_map2[tile] = p2;
 
			SetTileOwner(tile, _current_player);
 
		}
 

	
 
		_map5[tile] |= (byte)pieces;
 

	
 
		MarkTileDirtyByTile(tile);
 
	}
 
	return cost;
 
}
 

	
 
int32 DoConvertStreetRail(uint tile, uint totype, bool exec)
 
int32 DoConvertStreetRail(TileIndex tile, uint totype, bool exec)
 
{
 
	// not a railroad crossing?
 
	if (!IsLevelCrossing(tile)) return CMD_ERROR;
 

	
 
	// not owned by me?
 
	if (!CheckTileOwnership(tile) || !EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
	// tile is already of requested type?
 
	if ( (uint)(_map3_hi[tile] & 0xF) == totype) return CMD_ERROR;
 

	
 
	if (exec) {
 
		// change type.
 
@@ -668,25 +668,26 @@ static int32 RemoveRoadDepot(uint tile, 
 
	if (!EnsureNoVehicle(tile))
 
		return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DoDeleteDepot(tile);
 
	}
 

	
 
	return _price.remove_road_depot;
 
}
 

	
 
#define M(x) (1<<(x))
 

	
 
static int32 ClearTile_Road(uint tile, byte flags) {
 
static int32 ClearTile_Road(TileIndex tile, byte flags)
 
{
 
	int32 ret;
 
	byte m5 = _map5[tile];
 

	
 
	if ( (m5 & 0xF0) == 0) {
 
		byte b = m5 & 0xF;
 

	
 
		if (! ((1 << b) & (M(1)|M(2)|M(4)|M(8))) ) {
 
			if ((!(flags & DC_AI_BUILDING) || !IsTileOwner(tile, OWNER_TOWN)) && flags & DC_AUTO)
 
				return_cmd_error(STR_1801_MUST_REMOVE_ROAD_FIRST);
 
		}
 
		return DoCommandByTile(tile, b, 0, flags, CMD_REMOVE_ROAD);
 
	} else if ( (m5 & 0xE0) == 0) {
 
@@ -920,54 +921,54 @@ static uint GetSlopeTileh_Road(TileInfo 
 
				}
 
				// inclined foundation
 
				return _inclined_tileh[f - 15];
 
			}
 
		} else if ((ti->map5 & 0xF0) == 0x20) {
 
			// depot
 
			return 0;
 
		}
 
	}
 
	return ti->tileh;
 
}
 

	
 
static void GetAcceptedCargo_Road(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Road(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* not used */
 
}
 

	
 
static void AnimateTile_Road(uint tile)
 
static void AnimateTile_Road(TileIndex tile)
 
{
 
	if (IsLevelCrossing(tile)) {
 
		MarkTileDirtyByTile(tile);
 
	}
 
}
 

	
 
static const byte _town_road_types[5][2] = {
 
	{1,1},
 
	{2,2},
 
	{2,2},
 
	{5,5},
 
	{3,2},
 
};
 

	
 
static const byte _town_road_types_2[5][2] = {
 
	{1,1},
 
	{2,2},
 
	{3,2},
 
	{3,2},
 
	{3,2},
 
};
 

	
 

	
 
static void TileLoop_Road(uint tile)
 
static void TileLoop_Road(TileIndex tile)
 
{
 
	Town *t;
 
	int grp;
 

	
 
	if (_opt.landscape == LT_HILLY) {
 
		// Fix snow style if the road is above the snowline
 
		if ((_map3_hi[tile] & 0x80) != ((GetTileZ(tile) > _opt.snow_line) ? 0x80 : 0x00)) {
 
			_map3_hi[tile] ^= 0x80;
 
			MarkTileDirtyByTile(tile);
 
		}
 
	} else if (_opt.landscape == LT_DESERT) {
 
		// Fix desert style
 
@@ -1032,38 +1033,39 @@ static void TileLoop_Road(uint tile)
 
		//keep roadworks running for 16 loops
 
		//lower 4 bits of map3_hi store the counter now
 
		if ((b & 0xF) != 0xF) {
 
			_map3_hi[tile] = b + 1;
 
			return;
 
		}
 
		//roadworks finished
 
		_map3_hi[tile] = ((((b& 0x70) >> 4)== 6) ? 1 : 2) << 4;
 
		MarkTileDirtyByTile(tile);
 
	}
 
}
 

	
 
void ShowRoadDepotWindow(uint tile);
 
void ShowRoadDepotWindow(TileIndex tile);
 

	
 
static void ClickTile_Road(uint tile)
 
static void ClickTile_Road(TileIndex tile)
 
{
 
	if ((_map5[tile] & 0xF0) == 0x20) {
 
		ShowRoadDepotWindow(tile);
 
	}
 
}
 

	
 
static const byte _road_trackbits[16] = {
 
	0x0, 0x0, 0x0, 0x10, 0x0, 0x2, 0x8, 0x1A, 0x0, 0x4, 0x1, 0x15, 0x20, 0x26, 0x29, 0x3F,
 
};
 

	
 
static uint32 GetTileTrackStatus_Road(uint tile, TransportType mode)	{
 
static uint32 GetTileTrackStatus_Road(TileIndex tile, TransportType mode)
 
{
 
	if (mode == TRANSPORT_RAIL) {
 
		if (!IsLevelCrossing(tile))
 
			return 0;
 
		return _map5[tile] & 8 ? 0x101 : 0x202;
 
	} else if  (mode == TRANSPORT_ROAD) {
 
		byte b = _map5[tile];
 
		if ((b & 0xF0) == 0) {
 
			/* Ordinary road */
 
			if (!_road_special_gettrackstatus && ((_map3_hi[tile]&0x70) >> 4) >= 6)
 
				return 0;
 
			return _road_trackbits[b&0xF] * 0x101;
 
		} else if (IsLevelCrossing(tile)) {
 
@@ -1085,67 +1087,67 @@ static const StringID _road_tile_strings
 
	STR_1817_ROAD_VEHICLE_DEPOT,
 

	
 
	STR_1814_ROAD,
 
	STR_1814_ROAD,
 
	STR_1814_ROAD,
 
	STR_1815_ROAD_WITH_STREETLIGHTS,
 
	STR_1814_ROAD,
 
	STR_1816_TREE_LINED_ROAD,
 
	STR_1814_ROAD,
 
	STR_1814_ROAD,
 
};
 

	
 
static void GetTileDesc_Road(uint tile, TileDesc *td)
 
static void GetTileDesc_Road(TileIndex tile, TileDesc *td)
 
{
 
	int i = (_map5[tile] >> 4);
 
	if (i == 0)
 
		i = ((_map3_hi[tile] & 0x70) >> 4) + 3;
 
	td->str = _road_tile_strings[i - 1];
 
	td->owner = GetTileOwner(tile);
 
}
 

	
 
static const byte _roadveh_enter_depot_unk0[4] = {
 
	8, 9, 0, 1
 
};
 

	
 
static uint32 VehicleEnter_Road(Vehicle *v, uint tile, int x, int y)
 
static uint32 VehicleEnter_Road(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	if (IsLevelCrossing(tile)) {
 
		if (v->type == VEH_Train && (_map5[tile] & 4) == 0) {
 
			/* train crossing a road */
 
			SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
 
			_map5[tile] |= 4;
 
			MarkTileDirtyByTile(tile);
 
		}
 
	} else if ((_map5[tile]&0xF0) ==  0x20){
 
		if (v->type == VEH_Road && v->u.road.frame == 11) {
 
			if (_roadveh_enter_depot_unk0[_map5[tile]&3] == v->u.road.state) {
 
				RoadVehEnterDepot(v);
 
				return 4;
 
			}
 
		}
 
	}
 
	return 0;
 
}
 

	
 
static void VehicleLeave_Road(Vehicle *v, uint tile, int x, int y)
 
static void VehicleLeave_Road(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	if (IsLevelCrossing(tile) && v->type == VEH_Train && v->next == NULL) {
 
		// Turn off level crossing lights
 
		_map5[tile] &= ~4;
 
		MarkTileDirtyByTile(tile);
 
	}
 
}
 

	
 
static void ChangeTileOwner_Road(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Road(TileIndex tile, byte old_player, byte new_player)
 
{
 
	byte b;
 

	
 
	// road/rail crossing where the road is owned by the current player?
 
	if (old_player == _map3_lo[tile] && IsLevelCrossing(tile)) {
 
		_map3_lo[tile] = (new_player == 0xFF) ? OWNER_NONE : new_player;
 
	}
 

	
 
	if (!IsTileOwner(tile, old_player)) return;
 

	
 
	if (new_player != 255) {
 
		SetTileOwner(tile, new_player);
road_gui.c
Show inline comments
 
@@ -17,97 +17,97 @@
 
static void ShowBusStationPicker(void);
 
static void ShowTruckStationPicker(void);
 
static void ShowRoadDepotPicker(void);
 

	
 
static bool _remove_button_clicked;
 
static bool _build_road_flag;
 

	
 
static byte _place_road_flag;
 

	
 
static byte _road_depot_orientation;
 
static byte _road_station_picker_orientation;
 

	
 
void CcPlaySound1D(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcPlaySound1D(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) SndPlayTileFx(SND_1F_SPLAT, tile);
 
}
 

	
 
static void PlaceRoad_NE(uint tile)
 
static void PlaceRoad_NE(TileIndex tile)
 
{
 
	_place_road_flag = (_tile_fract_coords.y >= 8) + 4;
 
	VpStartPlaceSizing(tile, VPM_FIX_X);
 
}
 

	
 
static void PlaceRoad_NW(uint tile)
 
static void PlaceRoad_NW(TileIndex tile)
 
{
 
	_place_road_flag = (_tile_fract_coords.x >= 8) + 0;
 
	VpStartPlaceSizing(tile, VPM_FIX_Y);
 
}
 

	
 
static void PlaceRoad_Bridge(uint tile)
 
static void PlaceRoad_Bridge(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_OR_Y);
 
}
 

	
 

	
 
void CcBuildRoadTunnel(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildRoadTunnel(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_20_SPLAT_2, tile);
 
		ResetObjectToPlace();
 
	} else {
 
		SetRedErrorSquare(_build_tunnel_endtile);
 
	}
 
}
 

	
 
static void PlaceRoad_Tunnel(uint tile)
 
static void PlaceRoad_Tunnel(TileIndex tile)
 
{
 
	DoCommandP(tile, 0x200, 0, CcBuildRoadTunnel, CMD_BUILD_TUNNEL | CMD_AUTO | CMD_MSG(STR_5016_CAN_T_BUILD_TUNNEL_HERE));
 
}
 

	
 
static void BuildRoadOutsideStation(uint tile, int direction)
 
static void BuildRoadOutsideStation(TileIndex tile, int direction)
 
{
 
	static const byte _roadbits_by_dir[4] = {2,1,8,4};
 
	tile += TileOffsByDir(direction);
 
	// if there is a roadpiece just outside of the station entrance, build a connecting route
 
	if (IsTileType(tile, MP_STREET) && !(_map5[tile] & 0x20)) {
 
		DoCommandP(tile, _roadbits_by_dir[direction], 0, NULL, CMD_BUILD_ROAD);
 
	}
 
}
 

	
 
void CcRoadDepot(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcRoadDepot(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
		ResetObjectToPlace();
 
		BuildRoadOutsideStation(tile, (int)p1);
 
	}
 
}
 

	
 
static void PlaceRoad_Depot(uint tile)
 
static void PlaceRoad_Depot(TileIndex tile)
 
{
 
	DoCommandP(tile, _road_depot_orientation, 0, CcRoadDepot, CMD_BUILD_ROAD_DEPOT | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_1807_CAN_T_BUILD_ROAD_VEHICLE));
 
}
 

	
 
static void PlaceRoad_BusStation(uint tile)
 
static void PlaceRoad_BusStation(TileIndex tile)
 
{
 
	DoCommandP(tile, _road_station_picker_orientation, RS_BUS, CcRoadDepot, CMD_BUILD_ROAD_STOP | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_1808_CAN_T_BUILD_BUS_STATION));
 
}
 

	
 
static void PlaceRoad_TruckStation(uint tile)
 
static void PlaceRoad_TruckStation(TileIndex tile)
 
{
 
	DoCommandP(tile, _road_station_picker_orientation, RS_TRUCK, CcRoadDepot, CMD_BUILD_ROAD_STOP | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_1809_CAN_T_BUILD_TRUCK_STATION));
 
}
 

	
 
static void PlaceRoad_DemolishArea(uint tile)
 
static void PlaceRoad_DemolishArea(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, 4);
 
}
 

	
 
typedef void OnButtonClick(Window *w);
 

	
 
static void BuildRoadClick_NE(Window *w)
 
{
 
	_build_road_flag = 0;
 
	HandlePlacePushButton(w, 3, SPR_CURSOR_ROAD_NESW, 1, PlaceRoad_NE);
 
}
 

	
 
@@ -244,42 +244,44 @@ static void BuildRoadToolbWndProc(Window
 
		} else if (e->place.userdata == 4) {
 
			sel_method = VPM_X_AND_Y;
 
		} else {
 
			sel_method = VPM_X_OR_Y;
 
		}
 

	
 
		VpSelectTilesWithMethod(e->place.pt.x, e->place.pt.y, sel_method);
 
		return;
 
	}
 

	
 
	case WE_PLACE_MOUSEUP:
 
		if (e->place.pt.x != -1) {
 
			uint start_tile = e->place.starttile;
 
			uint end_tile = e->place.tile;
 
			TileIndex start_tile = e->place.starttile;
 
			TileIndex end_tile = e->place.tile;
 

	
 
			if (e->place.userdata == 0) {
 
				ResetObjectToPlace();
 
				ShowBuildBridgeWindow(start_tile, end_tile, 0x80);
 
			} else if (e->place.userdata != 4) {
 
				DoCommandP(end_tile, start_tile, _place_road_flag, CcPlaySound1D,
 
					_remove_button_clicked ?
 
					CMD_REMOVE_LONG_ROAD | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_1805_CAN_T_REMOVE_ROAD_FROM) :
 
					CMD_BUILD_LONG_ROAD | CMD_AUTO | CMD_NO_WATER | CMD_MSG(STR_1804_CAN_T_BUILD_ROAD_HERE));
 
			} else {
 
				DoCommandP(end_tile, start_tile, _place_road_flag, CcPlaySound10, CMD_CLEAR_AREA | CMD_MSG(STR_00B5_CAN_T_CLEAR_THIS_AREA));
 
			}
 
		}
 
		break;
 

	
 
	case WE_PLACE_PRESIZE: {
 
		uint tile = e->place.tile;
 
		TileIndex tile = e->place.tile;
 

	
 
		DoCommandByTile(tile, 0x200, 0, DC_AUTO, CMD_BUILD_TUNNEL);
 
		VpSetPresizeRange(tile, _build_tunnel_endtile==0?tile:_build_tunnel_endtile);
 
		break;
 
	}
 
	}
 
}
 

	
 
static const Widget _build_road_widgets[] = {
 
{   WWT_CLOSEBOX,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5, STR_018B_CLOSE_WINDOW},
 
{    WWT_CAPTION,   RESIZE_NONE,     7,    11,   227,     0,    13, STR_1802_ROAD_CONSTRUCTION, STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{  WWT_STICKYBOX,   RESIZE_NONE,     7,   228,   239,     0,    13, 0x0,                   STR_STICKY_BUTTON},
 

	
roadveh_cmd.c
Show inline comments
 
@@ -273,52 +273,52 @@ int32 CmdSellRoadVeh(int x, int y, uint3
 
		InvalidateWindow(WC_COMPANY, v->owner);
 
		DeleteWindowById(WC_VEHICLE_VIEW, v->index);
 
		ClearSlot(v, v->u.road.slot);
 
		DeleteVehicle(v);
 
	}
 
	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Road); // updates the replace Road window
 

	
 
	return -(int32)v->value;
 
}
 

	
 
typedef struct RoadFindDepotData {
 
	uint best_length;
 
	uint tile;
 
	TileIndex tile;
 
	byte owner;
 
} RoadFindDepotData;
 

	
 
static const byte _road_pf_directions[16] = {
 
	0, 1, 0, 1, 2, 1, 255, 255,
 
	2, 3, 3, 2, 3, 0, 255, 255,
 
};
 

	
 
static bool EnumRoadSignalFindDepot(uint tile, RoadFindDepotData *rfdd, int track, uint length, byte *state)
 
static bool EnumRoadSignalFindDepot(TileIndex tile, RoadFindDepotData *rfdd, int track, uint length, byte *state)
 
{
 
	tile += TileOffsByDir(_road_pf_directions[track]);
 

	
 
	if (IsTileType(tile, MP_STREET) &&
 
			(_map5[tile] & 0xF0) == 0x20 &&
 
			IsTileOwner(tile, rfdd->owner)) {
 

	
 
		if (length < rfdd->best_length) {
 
			rfdd->best_length = length;
 
			rfdd->tile = tile;
 
		}
 
	}
 
	return false;
 
}
 

	
 
static Depot *FindClosestRoadDepot(Vehicle *v)
 
{
 
	uint tile = v->tile;
 
	TileIndex tile = v->tile;
 
	int i;
 

	
 
	if (v->u.road.state == 255) { tile = GetVehicleOutOfTunnelTile(v); }
 

	
 
	if (_patches.new_pathfinding_all) {
 
		NPFFoundTargetData ftd;
 
		/* See where we are now */
 
		Trackdir trackdir = GetVehicleTrackdir(v);
 

	
 
		ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_ROAD, v->owner);
 
		if (ftd.best_bird_dist == 0)
 
			return GetDepotByTile(ftd.node.tile); /* Target found */
 
@@ -463,25 +463,25 @@ static void UpdateRoadVehDeltaXY(Vehicle
 
		MKIT(7, 3, -3, -1),
 
	};
 
#undef MKIT
 
	uint32 x = _delta_xy_table[v->direction];
 
	v->x_offs = (byte)x;
 
	v->y_offs = (byte)(x>>=8);
 
	v->sprite_width = (byte)(x>>=8);
 
	v->sprite_height = (byte)(x>>=8);
 
}
 

	
 
static void ClearCrashedStation(Vehicle *v)
 
{
 
	uint tile = v->tile;
 
	TileIndex tile = v->tile;
 
	byte *b, bb;
 

	
 
	RoadStop *rs = GetRoadStopByTile(tile, GetRoadStopType(tile));
 
	b = &rs->status;
 

	
 
	bb = *b;
 

	
 
	// mark station as not busy
 
	bb &= ~0x80;
 

	
 
	// free parking bay
 
	bb |= (v->u.road.state&0x02)?2:1;
 
@@ -575,25 +575,25 @@ static void RoadVehCrash(Vehicle *v)
 

	
 
	AddNewsItem(STR_9031_ROAD_VEHICLE_CRASH_DRIVER+(pass!=1),
 
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
 
		v->index,
 
		0);
 

	
 
	ModifyStationRatingAround(v->tile, v->owner, -160, 22);
 
	SndPlayVehicleFx(SND_12_EXPLOSION, v);
 
}
 

	
 
static void RoadVehCheckTrainCrash(Vehicle *v)
 
{
 
	uint tile;
 
	TileIndex tile;
 

	
 
	if (v->u.road.state == 255)
 
		return;
 

	
 
	tile = v->tile;
 

	
 
	// Make sure it's a road/rail crossing
 
	if (!IsTileType(tile, MP_STREET) || !IsLevelCrossing(tile))
 
		return;
 

	
 
	if (VehicleFromPos(tile, v, (VehicleFromPosProc*)EnumCheckRoadVehCrashTrain) != NULL)
 
		RoadVehCrash(v);
 
@@ -892,25 +892,25 @@ static byte RoadVehGetSlidingDirection(V
 
	if (b == d) return d;
 
	d = (d+1)&7;
 
	if (b==d) return d;
 
	d = (d-2)&7;
 
	if (b==d) return d;
 
	if (b==((d-1)&7)) return d;
 
	if (b==((d-2)&7)) return d;
 
	return (d+2)&7;
 
}
 

	
 
typedef struct OvertakeData {
 
	Vehicle *u, *v;
 
	uint tile;
 
	TileIndex tile;
 
	byte tilebits;
 
} OvertakeData;
 

	
 
static void *EnumFindVehToOvertake(Vehicle *v, OvertakeData *od)
 
{
 
	if (v->tile != (TileIndex)od->tile ||
 
			v->type != VEH_Road ||
 
			v == od->u ||
 
			v == od->v)
 
				return NULL;
 
	return v;
 
}
 
@@ -1003,45 +1003,45 @@ static int PickRandomBit(uint bits)
 
	num = ((uint16)Random() * num >> 16);
 

	
 
	for(i=0; !((bits & 1) && ((int)--num) < 0); bits>>=1,i++);
 
	return i;
 
}
 

	
 
typedef struct {
 
	TileIndex dest;
 
	uint maxtracklen;
 
	uint mindist;
 
} FindRoadToChooseData;
 

	
 
static bool EnumRoadTrackFindDist(uint tile, FindRoadToChooseData *frd, int track, uint length, byte *state)
 
static bool EnumRoadTrackFindDist(TileIndex tile, FindRoadToChooseData *frd, int track, uint length, byte *state)
 
{
 
	uint dist = DistanceManhattan(tile, frd->dest);
 
	if (dist <= frd->mindist) {
 
		if (dist != frd->mindist || length < frd->maxtracklen) {
 
			frd->maxtracklen = length;
 
		}
 
		frd->mindist = dist;
 
	}
 
	return false;
 
}
 

	
 
// Returns direction to choose
 
// or -1 if the direction is currently blocked
 
static int RoadFindPathToDest(Vehicle *v, uint tile, int enterdir)
 
static int RoadFindPathToDest(Vehicle *v, TileIndex tile, int enterdir)
 
{
 
#define return_track(x) {best_track = x; goto found_best_track; }
 

	
 
	uint16 signal;
 
	uint bitmask;
 
	uint desttile;
 
	TileIndex desttile;
 
	FindRoadToChooseData frd;
 
	int best_track;
 
	uint best_dist, best_maxlen;
 
	uint i;
 
	byte m5;
 

	
 
	{
 
		uint32 r;
 
		r = GetTileTrackStatus(tile, TRANSPORT_ROAD);
 
		signal = (uint16)(r >> 16);
 
		bitmask = (uint16)r;
 
	}
 
@@ -1308,25 +1308,25 @@ static void RoadVehController(Vehicle *v
 
		}
 

	
 
		v->x_pos = gp.x;
 
		v->y_pos = gp.y;
 
		VehiclePositionChanged(v);
 
		return;
 
	}
 

	
 
	rd = _road_drive_data[(v->u.road.state + (_opt.road_side<<4)) ^ v->u.road.overtaking][v->u.road.frame+1];
 

	
 
// switch to another tile
 
	if (rd.x & 0x80) {
 
		uint tile = v->tile + TileOffsByDir(rd.x & 3);
 
		TileIndex tile = v->tile + TileOffsByDir(rd.x & 3);
 
		int dir = RoadFindPathToDest(v, tile, rd.x&3);
 
		int tmp;
 
		uint32 r;
 
		byte newdir;
 
		const RoadDriveEntry *rdp;
 

	
 
		if (dir == -1) {
 
			v->cur_speed = 0;
 
			return;
 
		}
 

	
 
again:
roadveh_gui.c
Show inline comments
 
@@ -401,25 +401,25 @@ static void DrawNewRoadVehWindow(Window 
 
				}
 
				sel--;
 
			}
 
		} while (++engine_id, ++e,--num);
 

	
 
		WP(w,buildtrain_d).sel_engine = selected_id;
 
		if (selected_id != -1) {
 
			DrawRoadVehPurchaseInfo(2, w->widget[4].top + 1, selected_id);
 
		}
 
	}
 
}
 

	
 
void CcBuildRoadVeh(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildRoadVeh(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!success) return;
 

	
 
	v = GetVehicle(_new_roadveh_id);
 
	if (v->tile == _backup_orders_tile) {
 
		_backup_orders_tile = 0;
 
		RestoreVehicleOrders(v, _backup_orders_data);
 
	}
 
	ShowRoadVehViewWindow(v);
 
}
 
@@ -517,25 +517,25 @@ static void ShowBuildRoadVehWindow(TileI
 
	w->resize.step_height = 14;
 
	w->resize.height = w->height - 14 * 4; /* Minimum of 4 vehicles in the display */
 

	
 
	if (tile != 0) {
 
		w->caption_color = GetTileOwner(tile);
 
	} else {
 
		w->caption_color = _local_player;
 
	}
 
}
 

	
 
static void DrawRoadDepotWindow(Window *w)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	Vehicle *v;
 
	int num,x,y;
 
	Depot *depot;
 

	
 
	tile = w->window_number;
 

	
 
	/* setup disabled buttons */
 
	w->disabled_state =
 
		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 7));
 

	
 
	/* determine amount of items for scroller */
 
	num = 0;
 
@@ -744,25 +744,25 @@ static const Widget _road_depot_widgets[
 
{  WWT_RESIZEBOX,   RESIZE_LRTB,    14,   303,   314,    56,    67, 0x0,													STR_RESIZE_BUTTON},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _road_depot_desc = {
 
	-1, -1, 315, 68,
 
	WC_VEHICLE_DEPOT,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
 
	_road_depot_widgets,
 
	RoadDepotWndProc
 
};
 

	
 
void ShowRoadDepotWindow(uint tile)
 
void ShowRoadDepotWindow(TileIndex tile)
 
{
 
	Window *w;
 

	
 
	w = AllocateWindowDescFront(&_road_depot_desc, tile);
 
	if (w) {
 
		w->caption_color = GetTileOwner(w->window_number);
 
		w->hscroll.cap = 5;
 
		w->vscroll.cap = 3;
 
		w->resize.step_width = 56;
 
		w->resize.step_height = 14;
 
		WP(w,traindepot_d).sel = INVALID_VEHICLE;
 
		_backup_orders_tile = 0;
 
@@ -900,25 +900,25 @@ static void PlayerRoadVehWndProc(Window 
 

	
 
				if (id_v >= vl->list_length) return; // click out of list bound
 

	
 
				v	= GetVehicle(vl->sort_list[id_v].index);
 

	
 
				assert(v->type == VEH_Road && v->owner == owner);
 

	
 
				ShowRoadVehViewWindow(v);
 
			}
 
		} break;
 

	
 
		case 9: { /* Build new Vehicle */
 
			uint tile;
 
			TileIndex tile;
 

	
 
			if (!IsWindowOfPrototype(w, _player_roadveh_widgets))
 
				break;
 

	
 
			tile = _last_built_road_depot_tile;
 
			do {
 
				if (IsTileDepotType(tile, TRANSPORT_ROAD) && IsTileOwner(tile, _local_player)) {
 
					ShowRoadDepotWindow(tile);
 
					ShowBuildRoadVehWindow(tile);
 
					return;
 
				}
 

	
ship_cmd.c
Show inline comments
 
@@ -10,25 +10,26 @@
 
#include "gfx.h"
 
#include "news.h"
 
#include "engine.h"
 
#include "gui.h"
 
#include "player.h"
 
#include "sound.h"
 
#include "npf.h"
 
#include "depot.h"
 

	
 
static const uint16 _ship_sprites[] = {0x0E5D, 0x0E55, 0x0E65, 0x0E6D};
 
static const byte _ship_sometracks[4] = {0x19, 0x16, 0x25, 0x2A};
 

	
 
static byte GetTileShipTrackStatus(uint tile) {
 
static byte GetTileShipTrackStatus(TileIndex tile)
 
{
 
	uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER);
 
	return r | r >> 8;
 
}
 

	
 
void DrawShipEngine(int x, int y, int engine, uint32 image_ormod)
 
{
 
	int spritenum = ShipVehInfo(engine)->image_index;
 

	
 
	if (is_custom_sprite(spritenum)) {
 
		int sprite = GetCustomVehicleIcon(engine, 6);
 

	
 
		if (sprite) {
 
@@ -59,26 +60,28 @@ int GetShipImage(const Vehicle *v, byte 
 
		int sprite = GetCustomVehicleSprite(v, direction);
 

	
 
		if (sprite) return sprite;
 
		spritenum = _engine_original_sprites[v->engine_type];
 
	}
 
	return _ship_sprites[spritenum] + direction;
 
}
 

	
 
static Depot *FindClosestShipDepot(Vehicle *v)
 
{
 
	Depot *depot;
 
	Depot *best_depot = NULL;
 
	uint tile, dist, best_dist = (uint)-1;
 
	uint tile2 = v->tile;
 
	uint dist;
 
	uint best_dist = (uint)-1;
 
	TileIndex tile;
 
	TileIndex tile2 = v->tile;
 

	
 
	if (_patches.new_pathfinding_all) {
 
		NPFFoundTargetData ftd;
 
		byte trackdir = GetVehicleTrackdir(v);
 
		ftd = NPFRouteToDepotTrialError(v->tile, trackdir, TRANSPORT_WATER, v->owner);
 
		if (ftd.best_bird_dist == 0)
 
			best_depot = GetDepotByTile(ftd.node.tile); /* Found target */
 
		else
 
			best_depot = NULL; /* Did not find target */
 
	} else {
 
		FOR_ALL_DEPOTS(depot) {
 
			tile = depot->xy;
 
@@ -333,25 +336,25 @@ static void RecalcShipStuff(Vehicle *v)
 
	v->cur_image = GetShipImage(v, v->direction);
 
	MarkShipDirty(v);
 
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
 
}
 

	
 
static const TileIndexDiffC _ship_leave_depot_offs[] = {
 
	{-1,  0},
 
	{ 0, -1}
 
};
 

	
 
static void CheckShipLeaveDepot(Vehicle *v)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	int d;
 
	uint m;
 

	
 
	if (v->u.ship.state != 0x80)
 
		return;
 

	
 
	tile = v->tile;
 
	d = (_map5[tile]&2) ? 1 : 0;
 

	
 
	// Check first side
 
	if (_ship_sometracks[d] & GetTileShipTrackStatus(TILE_ADD(tile, ToTileIndexDiff(_ship_leave_depot_offs[d])))) {
 
		m = (d==0) ? 0x101 : 0x207;
 
@@ -457,33 +460,33 @@ static void ShipArrivesAt(Vehicle *v, St
 
		st->had_vehicle_of_type |= HVOT_SHIP;
 
		SetDParam(0, st->index);
 
		flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
 
		AddNewsItem(
 
			STR_9833_CITIZENS_CELEBRATE_FIRST,
 
			flags,
 
			v->index,
 
			0);
 
	}
 
}
 

	
 
typedef struct {
 
	uint skiptile;
 
	uint dest_coords;
 
	TileIndex skiptile;
 
	TileIndex dest_coords;
 
	uint best_bird_dist;
 
	uint best_length;
 
} PathFindShip;
 

	
 
//extern void dbg_store_path();
 

	
 
static bool ShipTrackFollower(uint tile, PathFindShip *pfs, int track, uint length, byte *state)
 
static bool ShipTrackFollower(TileIndex tile, PathFindShip *pfs, int track, uint length, byte *state)
 
{
 
	// Found dest?
 
	if (tile == pfs->dest_coords) {
 
		pfs->best_bird_dist = 0;
 

	
 
//		if (length < pfs->best_length)
 
//			dbg_store_path();
 

	
 
		pfs->best_length = minu(pfs->best_length, length);
 
		return true;
 
	}
 

	
 
@@ -497,25 +500,25 @@ static bool ShipTrackFollower(uint tile,
 

	
 
static const byte _ship_search_directions[6][4] = {
 
	{ 0, 9, 2, 9 },
 
	{ 9, 1, 9, 3 },
 
	{ 9, 0, 3, 9 },
 
	{ 1, 9, 9, 2 },
 
	{ 3, 2, 9, 9 },
 
	{ 9, 9, 1, 0 },
 
};
 

	
 
static const byte _pick_shiptrack_table[6] = {1, 3, 2, 2, 0, 0};
 

	
 
static uint FindShipTrack(Vehicle *v, uint tile, int dir, uint bits, uint skiptile, int *track)
 
static uint FindShipTrack(Vehicle *v, TileIndex tile, int dir, uint bits, TileIndex skiptile, int *track)
 
{
 
	PathFindShip pfs;
 
	int i, best_track;
 
	uint best_bird_dist = 0;
 
	uint best_length    = 0;
 
	uint r;
 
	byte ship_dir = v->direction & 3;
 

	
 
	pfs.dest_coords = v->dest_tile;
 
	pfs.skiptile = skiptile;
 

	
 
	best_track = -1;
 
@@ -552,52 +555,52 @@ good:;
 
		best_length = pfs.best_length;
 
bad:;
 

	
 
	} while (bits != 0);
 

	
 
	*track = best_track;
 
	return best_bird_dist;
 
}
 

	
 
/* returns the track to choose on the next tile, or -1 when it's better to
 
 * reverse. The tile given is the tile we are about to enter, enterdir is the
 
 * direction in which we are entering the tile */
 
static int ChooseShipTrack(Vehicle *v, uint tile, int enterdir, uint tracks)
 
static int ChooseShipTrack(Vehicle *v, TileIndex tile, int enterdir, uint tracks)
 
{
 
	assert(enterdir>=0 && enterdir<=3);
 

	
 
	if (_patches.new_pathfinding_all) {
 
		NPFFindStationOrTileData fstd;
 
		NPFFoundTargetData ftd;
 
		uint src_tile = TILE_ADD(tile, TileOffsByDir(ReverseDiagdir(enterdir)));
 
		TileIndex src_tile = TILE_ADD(tile, TileOffsByDir(ReverseDiagdir(enterdir)));
 
		byte trackdir = GetVehicleTrackdir(v);
 
		assert (trackdir != 0xFF); /* Check that we are not in a depot */
 

	
 
		NPFFillWithOrderData(&fstd, v);
 

	
 
		ftd = NPFRouteToStationOrTile(src_tile, trackdir, &fstd, TRANSPORT_WATER, v->owner);
 

	
 
		if (ftd.best_trackdir != 0xff)
 
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
 
			the direction we need to take to get there, if ftd.best_bird_dist is not 0,
 
			we did not find our target, but ftd.best_trackdir contains the direction leading
 
			to the tile closest to our target. */
 
			return ftd.best_trackdir & 7; /* TODO: Wrapper function? */
 
		else
 
			return -1; /* Already at target, reverse? */
 
	} else {
 
		uint b;
 
		uint tot_dist, dist;
 
		int track;
 
		uint tile2;
 
		TileIndex tile2;
 

	
 
		tile2 = TILE_ADD(tile, -TileOffsByDir(enterdir));
 
		tot_dist = (uint)-1;
 

	
 
		/* Let's find out how far it would be if we would reverse first */
 
		b = GetTileShipTrackStatus(tile2) & _ship_sometracks[ReverseDiagdir(enterdir)] & v->u.ship.state;
 
		if (b != 0) {
 
			dist = FindShipTrack(v, tile2, ReverseDiagdir(enterdir), b, tile, &track);
 
			if (dist != (uint)-1)
 
				tot_dist = dist + 1;
 
		}
 
		/* And if we would not reverse? */
 
@@ -606,40 +609,40 @@ static int ChooseShipTrack(Vehicle *v, u
 
			/* We could better reverse */
 
			return -1;
 
		return track;
 
	}
 
}
 

	
 
static const byte _new_vehicle_direction_table[11] = {
 
	0, 7, 6, 0,
 
	1, 0, 5, 0,
 
	2, 3, 4,
 
};
 

	
 
static int ShipGetNewDirectionFromTiles(uint new_tile, uint old_tile)
 
static int ShipGetNewDirectionFromTiles(TileIndex new_tile, TileIndex old_tile)
 
{
 
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
 
							TileX(new_tile) - TileX(old_tile) + 1;
 
	assert(offs < 11 && offs != 3 && offs != 7);
 
	return _new_vehicle_direction_table[offs];
 
}
 

	
 
static int ShipGetNewDirection(Vehicle *v, int x, int y)
 
{
 
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
 
	assert(offs < 11 && offs != 3 && offs != 7);
 
	return _new_vehicle_direction_table[offs];
 
}
 

	
 
static int GetAvailShipTracks(uint tile, int dir)
 
static int GetAvailShipTracks(TileIndex tile, int dir)
 
{
 
	uint32 r = GetTileTrackStatus(tile, TRANSPORT_WATER);
 
	return (byte) ((r | r >> 8)) & _ship_sometracks[dir];
 
}
 

	
 
static const byte _ship_subcoord[4][6][3] = {
 
	{
 
		{15, 8, 1},
 
		{ 0, 0, 0},
 
		{ 0, 0, 0},
 
		{15, 8, 2},
 
		{15, 7, 0},
ship_gui.c
Show inline comments
 
@@ -293,25 +293,25 @@ static const WindowDesc _ship_details_de
 
static void ShowShipDetailsWindow(Vehicle *v)
 
{
 
	Window *w;
 
	VehicleID veh = v->index;
 
	DeleteWindowById(WC_VEHICLE_ORDERS, veh);
 
	DeleteWindowById(WC_VEHICLE_DETAILS, veh);
 
	_alloc_wnd_parent_num = veh;
 
	w = AllocateWindowDesc(&_ship_details_desc);
 
	w->window_number = veh;
 
	w->caption_color = v->owner;
 
}
 

	
 
void CcBuildShip(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildShip(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	if (!success) return;
 

	
 
	v = GetVehicle(_new_ship_id);
 
	if (v->tile == _backup_orders_tile) {
 
		_backup_orders_tile = 0;
 
		RestoreVehicleOrders(v, _backup_orders_data);
 
	}
 
	ShowShipViewWindow(v);
 
}
 

	
 
@@ -593,25 +593,25 @@ void ShowShipViewWindow(Vehicle *v)
 
{
 
	Window *w;
 

	
 
	w = AllocateWindowDescFront(&_ship_view_desc, v->index);
 
	if (w) {
 
		w->caption_color = v->owner;
 
		AssignWindowViewport(w, 3, 17, 0xE2, 0x54, w->window_number | (1 << 31), 0);
 
	}
 
}
 

	
 
static void DrawShipDepotWindow(Window *w)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	Vehicle *v;
 
	int num,x,y;
 
	Depot *depot;
 

	
 
	tile = w->window_number;
 

	
 
	/* setup disabled buttons */
 
	w->disabled_state =
 
		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 7));
 

	
 
	/* determine amount of items for scroller */
 
	num = 0;
 
@@ -816,25 +816,25 @@ static const Widget _ship_depot_widgets[
 
{  WWT_RESIZEBOX,   RESIZE_LRTB,    14,   293,   304,    62,    73, 0x0,										STR_RESIZE_BUTTON},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _ship_depot_desc = {
 
	-1, -1, 305, 74,
 
	WC_VEHICLE_DEPOT,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
 
	_ship_depot_widgets,
 
	ShipDepotWndProc
 
};
 

	
 
void ShowShipDepotWindow(uint tile)
 
void ShowShipDepotWindow(TileIndex tile)
 
{
 
	Window *w;
 

	
 
	w = AllocateWindowDescFront(&_ship_depot_desc,tile);
 
	if (w) {
 
		w->caption_color = GetTileOwner(w->window_number);
 
		w->vscroll.cap = 2;
 
		w->hscroll.cap = 3;
 
		w->resize.step_width = 90;
 
		w->resize.step_height = 24;
 
		WP(w,traindepot_d).sel = INVALID_VEHICLE;
 
		_backup_orders_tile = 0;
 
@@ -1001,25 +1001,25 @@ static void PlayerShipsWndProc(Window *w
 

	
 
				if (id_v >= vl->list_length) return; // click out of list bound
 

	
 
				v	= GetVehicle(vl->sort_list[id_v].index);
 

	
 
				assert(v->type == VEH_Ship);
 

	
 
				ShowShipViewWindow(v);
 
			}
 
		} break;
 

	
 
		case 9: { /* Build new Vehicle */
 
			uint tile;
 
			TileIndex tile;
 

	
 
			if (!IsWindowOfPrototype(w, _player_ships_widgets))
 
				break;
 

	
 

	
 
			tile = _last_built_ship_depot_tile;
 
			do {
 
				if (IsTileDepotType(tile, TRANSPORT_WATER) && IsTileOwner(tile, _local_player)) {
 
					ShowShipDepotWindow(tile);
 
					ShowBuildShipWindow(tile);
 
					return;
 
				}
signs.c
Show inline comments
 
@@ -177,39 +177,39 @@ int32 CmdRenameSign(int x, int y, uint32
 
			_sign_sort_dirty = true;
 
		}
 
	}
 

	
 
	return 0;
 
}
 

	
 
/**
 
 *
 
 * Callback function that is called after a sign is placed
 
 *
 
 */
 
void CcPlaceSign(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcPlaceSign(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		ShowRenameSignWindow(_new_sign_struct);
 
		ResetObjectToPlace();
 
	}
 
}
 

	
 
/**
 
 *
 
 * PlaceProc function, called when someone pressed the button if the
 
 *  sign-tool is selected
 
 *
 
 */
 
void PlaceProc_Sign(uint tile)
 
void PlaceProc_Sign(TileIndex tile)
 
{
 
	DoCommandP(tile, _current_player, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
 
}
 

	
 
/**
 
 *
 
 * Initialize the signs
 
 *
 
 */
 
void InitializeSigns(void)
 
{
 
	CleanPool(&_sign_pool);
signs.h
Show inline comments
 
@@ -46,18 +46,18 @@ static inline bool IsSignIndex(uint inde
 
	return index < GetSignPoolSize();
 
}
 

	
 
#define FOR_ALL_SIGNS_FROM(ss, start) for (ss = GetSign(start); ss != NULL; ss = (ss->index + 1 < GetSignPoolSize()) ? GetSign(ss->index + 1) : NULL)
 
#define FOR_ALL_SIGNS(ss) FOR_ALL_SIGNS_FROM(ss, 0)
 

	
 
VARDEF SignStruct *_new_sign_struct;
 

	
 
VARDEF bool _sign_sort_dirty;
 
VARDEF uint16 *_sign_sort;
 

	
 
void UpdateAllSignVirtCoords(void);
 
void PlaceProc_Sign(uint tile);
 
void PlaceProc_Sign(TileIndex tile);
 

	
 
/* misc.c */
 
void ShowRenameSignWindow(SignStruct *ss);
 

	
 
#endif /* SIGNS_H */
station.h
Show inline comments
 
@@ -178,25 +178,25 @@ static inline uint16 GetRoadStopPoolSize
 

	
 
#define FOR_ALL_ROADSTOPS_FROM(rs, start) for (rs = GetRoadStop(start); rs != NULL; rs = (rs->index + 1 < GetRoadStopPoolSize()) ? GetRoadStop(rs->index + 1) : NULL)
 
#define FOR_ALL_ROADSTOPS(rs) FOR_ALL_ROADSTOPS_FROM(rs, 0)
 

	
 
/* End of stuff for ROADSTOPS */
 

	
 

	
 
VARDEF bool _station_sort_dirty[MAX_PLAYERS];
 
VARDEF bool _global_station_sort_dirty;
 

	
 
void GetProductionAroundTiles(AcceptedCargo produced, TileIndex tile, int w, int h, int rad);
 
void GetAcceptanceAroundTiles(AcceptedCargo accepts, TileIndex tile, int w, int h, int rad);
 
uint GetStationPlatforms(Station *st, uint tile);
 
uint GetStationPlatforms(Station *st, TileIndex tile);
 

	
 

	
 
/* Station layout for given dimensions - it is a two-dimensional array
 
 * where index is computed as (x * platforms) + platform. */
 
typedef byte *StationLayout;
 

	
 
typedef enum StationClass {
 
	STAT_CLASS_NONE, // unused station slot or so
 
	STAT_CLASS_DFLT, // default station class
 
	STAT_CLASS_WAYP, // waypoints
 

	
 
	/* TODO: When we actually support custom classes, they are
 
@@ -269,25 +269,26 @@ int GetCustomStationsCount(StationClass 
 

	
 
RoadStop * GetRoadStopByTile(TileIndex tile, RoadStopType type);
 
static inline int GetRoadStopType(TileIndex tile)
 
{
 
	return (_map5[tile] < 0x47) ? RS_TRUCK : RS_BUS;
 
}
 

	
 
uint GetNumRoadStops(const Station *st, RoadStopType type);
 
RoadStop * GetPrimaryRoadStop(const Station *st, RoadStopType type);
 
RoadStop * AllocateRoadStop( void );
 
void ClearSlot(Vehicle *v, RoadStop *rs);
 

	
 
static inline bool IsTrainStationTile(uint tile) {
 
static inline bool IsTrainStationTile(TileIndex tile)
 
{
 
	return IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8);
 
}
 

	
 
static inline bool IsCompatibleTrainStationTile(TileIndex tile, TileIndex ref)
 
{
 
	assert(IsTrainStationTile(ref));
 
	return
 
		IsTrainStationTile(tile) &&
 
		(_map3_lo[tile] & 0x0F) == (_map3_lo[ref] & 0x0F) && // same rail type?
 
		(_map5[tile] & 0x01) == (_map5[ref] & 0x01); // same direction?
 
}
 

	
 
@@ -305,18 +306,19 @@ static inline bool IsValidStation(const 
 

	
 
static inline bool IsBuoy(const Station* st)
 
{
 
	return st->had_vehicle_of_type & HVOT_BUOY; /* XXX: We should really ditch this ugly coding and switch to something sane... */
 
}
 

	
 
static inline bool IsBuoyTile(TileIndex tile) {
 
	return IsTileType(tile, MP_STATION) && _map5[tile] == 0x52;
 
}
 

	
 
/* Get's the direction the station exit points towards. Ie, returns 0 for a
 
 * station with the exit NE. */
 
static inline byte GetRoadStationDir(uint tile) {
 
static inline byte GetRoadStationDir(TileIndex tile)
 
{
 
	assert(IsRoadStationTile(tile));
 
	return (_map5[tile] - 0x43) & 3;
 
}
 

	
 
#endif /* STATION_H */
station_cmd.c
Show inline comments
 
@@ -53,25 +53,25 @@ static void RoadStopPoolNewBlock(uint st
 
}
 

	
 
/* Initialize the station-pool and roadstop-pool */
 
MemoryPool _station_pool = { "Stations", STATION_POOL_MAX_BLOCKS, STATION_POOL_BLOCK_SIZE_BITS, sizeof(Station), &StationPoolNewBlock, 0, 0, NULL };
 
MemoryPool _roadstop_pool = { "RoadStop", ROADSTOP_POOL_MAX_BLOCKS, ROADSTOP_POOL_BLOCK_SIZE_BITS, sizeof(RoadStop), &RoadStopPoolNewBlock, 0, 0, NULL };
 

	
 

	
 
// FIXME -- need to be embedded into Airport variable. Is dynamically
 
// deducteable from graphics-tile array, so will not be needed
 
const byte _airport_size_x[] = {4, 6, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
 
const byte _airport_size_y[] = {3, 6, 1, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
 

	
 
void ShowAircraftDepotWindow(uint tile);
 
void ShowAircraftDepotWindow(TileIndex tile);
 
extern void UpdateAirplanesOnNewStation(Station *st);
 

	
 
static void MarkStationDirty(Station *st)
 
{
 
	if (st->sign.width_1 != 0) {
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, 1);
 

	
 
		MarkAllViewportsDirty(
 
			st->sign.left - 6,
 
			st->sign.top,
 
			st->sign.left + (st->sign.width_1 << 2) + 12,
 
			st->sign.top + 48);
 
@@ -163,25 +163,25 @@ static byte FindCatchmentRadius(Station 
 
			case AT_HELIPORT:      ret = max(ret, CA_AIR_HELIPORT); break;
 
			case AT_LARGE:         ret = max(ret, CA_AIR_LARGE);    break;
 
			case AT_METROPOLITAN:  ret = max(ret, CA_AIR_METRO);    break;
 
			case AT_INTERNATIONAL: ret = max(ret, CA_AIR_INTER);    break;
 
		}
 
	}
 

	
 
	return ret;
 
}
 

	
 
#define CHECK_STATIONS_ERR ((Station*)-1)
 

	
 
static Station *GetStationAround(uint tile, int w, int h, int closest_station)
 
static Station *GetStationAround(TileIndex tile, int w, int h, int closest_station)
 
{
 
	// check around to see if there's any stations there
 
	BEGIN_TILE_LOOP(tile_cur, w + 2, h + 2, tile - TILE_XY(1,1))
 
		if (IsTileType(tile_cur, MP_STATION)) {
 
			int t;
 
			t = _map2[tile_cur];
 
			{
 
				Station *st = GetStation(t);
 
				// you cannot take control of an oilrig!!
 
				if (st->airport_type == AT_OILRIG && st->facilities == (FACIL_AIRPORT|FACIL_DOCK))
 
					continue;
 
			}
 
@@ -212,25 +212,25 @@ TileIndex GetStationTileForVehicle(const
 
			} else {
 
				if (st->truck_stops != NULL)
 
					return st->truck_stops->xy;
 
				else
 
					return 0;
 
			}
 
		default:
 
			assert(false);
 
			return 0;
 
	}
 
}
 

	
 
static bool CheckStationSpreadOut(Station *st, uint tile, int w, int h)
 
static bool CheckStationSpreadOut(Station *st, TileIndex tile, int w, int h)
 
{
 
	StationID station_index = st->index;
 
	uint i;
 
	uint x1 = TileX(tile);
 
	uint y1 = TileY(tile);
 
	uint x2 = x1 + w - 1;
 
	uint y2 = y1 + h - 1;
 
	uint t;
 

	
 
	for (i = 0; i != MapSize(); i++) {
 
		if (IsTileType(i, MP_STATION) && _map2[i] == station_index) {
 
			t = TileX(i);
 
@@ -266,50 +266,51 @@ static Station *AllocateStation(void)
 
		}
 
	}
 

	
 
	/* Check if we can add a block to the pool */
 
	if (AddBlockToPool(&_station_pool))
 
		return AllocateStation();
 

	
 
	_error_message = STR_3008_TOO_MANY_STATIONS_LOADING;
 
	return NULL;
 
}
 

	
 

	
 
static int CountMapSquareAround(uint tile, byte type, byte min, byte max) {
 
static int CountMapSquareAround(TileIndex tile, byte type, byte min, byte max)
 
{
 
	static const TileIndexDiffC _count_square_table[] = {
 
		{-3, -3}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
 
		{-6,  1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
 
		{-6,  1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
 
		{-6,  1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
 
		{-6,  1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
 
		{-6,  1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0},
 
		{-6,  1}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}, {1, 0}
 
	};
 
	const TileIndexDiffC *p;
 
	int num = 0;
 

	
 
	for (p = _count_square_table; p != endof(_count_square_table); ++p) {
 
		tile = TILE_MASK(tile + ToTileIndexDiff(*p));
 

	
 
		if (IsTileType(tile, type) && _map5[tile] >= min && _map5[tile] <= max)
 
			num++;
 
	}
 

	
 
	return num;
 
}
 

	
 
#define M(x) ((x) - STR_SV_STNAME)
 

	
 
static bool GenerateStationName(Station *st, uint tile, int flag)
 
static bool GenerateStationName(Station *st, TileIndex tile, int flag)
 
{
 
	static const uint32 _gen_station_name_bits[] = {
 
		0,                                      /* 0 */
 
		1 << M(STR_SV_STNAME_AIRPORT),          /* 1 */
 
		1 << M(STR_SV_STNAME_OILFIELD),         /* 2 */
 
		1 << M(STR_SV_STNAME_DOCKS),            /* 3 */
 
		0x1FF << M(STR_SV_STNAME_BUOY_1),       /* 4 */
 
		1 << M(STR_SV_STNAME_HELIPORT),         /* 5 */
 
	};
 

	
 
	Town *t = st->town;
 
	uint32 free_names = (uint32)-1;
 
@@ -734,29 +735,29 @@ static void UpdateStationAcceptance(Stat
 
}
 

	
 
// This is called right after a station was deleted.
 
// It checks if the whole station is free of substations, and if so, the station will be
 
// deleted after a little while.
 
static void DeleteStationIfEmpty(Station *st) {
 
	if (st->facilities == 0) {
 
		st->delete_ctr = 0;
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
	}
 
}
 

	
 
static int32 ClearTile_Station(uint tile, byte flags);
 
static int32 ClearTile_Station(TileIndex tile, byte flags);
 

	
 
// Tries to clear the given area. Returns the cost in case of success.
 
// Or an error code if it failed.
 
int32 CheckFlatLandBelow(uint tile, uint w, uint h, uint flags, uint invalid_dirs, int *station)
 
int32 CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, int *station)
 
{
 
	int32 cost = 0, ret;
 

	
 
	uint tileh;
 
	uint z;
 
	int allowed_z = -1;
 
	int flat_z;
 

	
 
	BEGIN_TILE_LOOP(tile_cur, w, h, tile)
 
		if (!EnsureNoVehicle(tile_cur))
 
			return CMD_ERROR;
 

	
 
@@ -811,25 +812,25 @@ int32 CheckFlatLandBelow(uint tile, uint
 
			ret = DoCommandByTile(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
			if (ret == CMD_ERROR) return CMD_ERROR;
 
			cost += ret;
 
		}
 
	END_TILE_LOOP(tile_cur, w, h, tile)
 

	
 
	return cost;
 
}
 

	
 
static bool CanExpandRailroadStation(Station *st, uint *fin, int direction)
 
{
 
	uint curw = st->trainst_w, curh = st->trainst_h;
 
	uint tile = fin[0];
 
	TileIndex tile = fin[0];
 
	uint w = fin[1];
 
	uint h = fin[2];
 

	
 
	if (_patches.nonuniform_stations) {
 
		// determine new size of train station region..
 
		int x = min(TileX(st->train_tile), TileX(tile));
 
		int y = min(TileY(st->train_tile), TileY(tile));
 
		curw = max(TileX(st->train_tile) + curw, TileX(tile) + w) - x;
 
		curh = max(TileY(st->train_tile) + curh, TileY(tile) + h) - y;
 
		tile = TILE_XY(x,y);
 
	} else {
 
		// check so the direction is the same
 
@@ -1010,25 +1011,25 @@ int32 CmdBuildRailroadStation(int x, int
 
		st->town = ClosestTownFromTile(tile_org, (uint)-1);
 
		if (_current_player < MAX_PLAYERS && flags&DC_EXEC)
 
			SETBIT(st->town->have_ratings, _current_player);
 

	
 
		if (!GenerateStationName(st, tile_org, 0))
 
			return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC)
 
			StationInitialize(st, tile_org);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		int tile_delta;
 
		TileIndexDiff tile_delta;
 
		byte *layout_ptr;
 
		StationID station_index = st->index;
 
		StationSpec *statspec;
 

	
 
		// Now really clear the land below the station
 
		// It should never return CMD_ERROR.. but you never know ;)
 
		//  (a bit strange function name for it, but it really does clear the land, when DC_EXEC is in flags)
 
		if (CmdFailed(CheckFlatLandBelow(tile_org, w_org, h_org, flags, 5 << direction, _patches.nonuniform_stations ? &est : NULL))) return CMD_ERROR;
 

	
 
		st->train_tile = finalvalues[0];
 
		if (!st->facilities) st->xy = finalvalues[0];
 
		st->facilities |= FACIL_TRAIN;
 
@@ -1063,34 +1064,34 @@ int32 CmdBuildRailroadStation(int x, int
 
			} while (--w);
 
			tile_org += tile_delta ^ TILE_XY(1,1);
 
		} while (--numtracks);
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
	}
 

	
 
	return cost;
 
}
 

	
 
static bool TileBelongsToRailStation(Station *st, uint tile)
 
static bool TileBelongsToRailStation(Station *st, TileIndex tile)
 
{
 
	return IsTileType(tile, MP_STATION) && _map2[tile] == st->index && _map5[tile] < 8;
 
}
 

	
 
static void MakeRailwayStationAreaSmaller(Station *st)
 
{
 
	uint w = st->trainst_w;
 
	uint h = st->trainst_h;
 
	uint tile = st->train_tile;
 
	TileIndex tile = st->train_tile;
 
	uint i;
 

	
 
restart:
 

	
 
	// too small?
 
	if (w != 0 && h != 0) {
 
		// check the left side, x = constant, y changes
 
		for(i=0; !TileBelongsToRailStation(st, tile + TILE_XY(0,i)) ;)
 
			// the left side is unused?
 
			if (++i==h) { tile += TILE_XY(1, 0); w--; goto restart; }
 

	
 
		// check the right side, x = constant, y changes
 
@@ -1143,25 +1144,25 @@ int32 CmdRemoveFromRailroadStation(int x
 

	
 
		// if we deleted the whole station, delete the train facility.
 
		if (st->train_tile == 0) {
 
			st->facilities &= ~FACIL_TRAIN;
 
			UpdateStationVirtCoordDirty(st);
 
			DeleteStationIfEmpty(st);
 
		}
 
	}
 
	return _price.remove_rail_station;
 
}
 

	
 
// determine the number of platforms for the station
 
uint GetStationPlatforms(Station *st, uint tile)
 
uint GetStationPlatforms(Station *st, TileIndex tile)
 
{
 
	uint t;
 
	int dir,delta;
 
	int len;
 
	assert(TileBelongsToRailStation(st, tile));
 

	
 
	len = 0;
 
	dir = _map5[tile]&1;
 
	delta = dir ? TILE_XY(0,1) : TILE_XY(1,0);
 

	
 
	// find starting tile..
 
	t = tile;
 
@@ -1380,25 +1381,25 @@ static int32 RemoveRailroadStation(Stati
 

	
 
	if (flags & DC_EXEC) {
 
		st->train_tile = 0;
 
		st->facilities &= ~FACIL_TRAIN;
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		DeleteStationIfEmpty(st);
 
	}
 

	
 
	return cost;
 
}
 

	
 
int32 DoConvertStationRail(uint tile, uint totype, bool exec)
 
int32 DoConvertStationRail(TileIndex tile, uint totype, bool exec)
 
{
 
	const Station *st = GetStation(_map2[tile]);
 
	if (!CheckOwnership(st->owner) || !EnsureNoVehicle(tile)) return CMD_ERROR;
 

	
 
	// tile is not a railroad station?
 
	if (_map5[tile] >= 8) return CMD_ERROR;
 

	
 
	// tile is already of requested type?
 
	if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR;
 

	
 
	if (exec) {
 
		// change type.
 
@@ -1777,25 +1778,25 @@ END_TILE_LOOP(tile_cur,w,h,tile)
 
		}
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
	}
 

	
 
	return cost;
 
}
 

	
 
static int32 RemoveAirport(Station *st, uint32 flags)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	int w,h;
 
	int32 cost;
 

	
 
	if (_current_player != OWNER_WATER && !CheckOwnership(st->owner))
 
		return CMD_ERROR;
 

	
 
	tile = st->airport_tile;
 

	
 
	w = _airport_size_x[st->airport_type];
 
	h = _airport_size_y[st->airport_type];
 

	
 
	cost = w * h * _price.remove_airport;
 
@@ -1892,25 +1893,25 @@ static bool CheckShipsOnBuoy(Station *st
 
			FOR_VEHICLE_ORDERS(v, order) {
 
				if (order->type == OT_GOTO_STATION && order->station == st->index) {
 
					return true;
 
				}
 
			}
 
		}
 
	}
 
	return false;
 
}
 

	
 
static int32 RemoveBuoy(Station *st, uint32 flags)
 
{
 
	uint tile;
 
	TileIndex tile;
 

	
 
	if (_current_player >= MAX_PLAYERS) {
 
		/* XXX: strange stuff */
 
		return_cmd_error(INVALID_STRING_ID);
 
	}
 

	
 
	tile = st->dock_tile;
 

	
 
	if (CheckShipsOnBuoy(st))
 
		return_cmd_error(STR_BUOY_IS_IN_USE);
 

	
 
	if (!EnsureNoVehicle(tile))
 
@@ -2049,25 +2050,26 @@ int32 CmdBuildDock(int x, int y, uint32 
 
			st->index,
 
			(direction&1) + 0x50);
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
	}
 
	return _price.build_dock;
 
}
 

	
 
static int32 RemoveDock(Station *st, uint32 flags)
 
{
 
	uint tile1, tile2;
 
	TileIndex tile1;
 
	TileIndex tile2;
 

	
 
	if (!CheckOwnership(st->owner))
 
		return CMD_ERROR;
 

	
 
	tile1 = st->dock_tile;
 
	tile2 = tile1 + TileOffsByDir(_map5[tile1] - 0x4C);
 

	
 
	if (!EnsureNoVehicle(tile1))
 
		return CMD_ERROR;
 

	
 
	if (!EnsureNoVehicle(tile2))
 
		return CMD_ERROR;
 
@@ -2185,51 +2187,52 @@ static uint GetSlopeZ_Station(TileInfo *
 
{
 
	uint z = ti->z;
 
	if (ti->tileh != 0)
 
		z += 8;
 
	return z;
 
}
 

	
 
static uint GetSlopeTileh_Station(TileInfo *ti)
 
{
 
	return 0;
 
}
 

	
 
static void GetAcceptedCargo_Station(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Station(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* not used */
 
}
 

	
 
static void GetTileDesc_Station(uint tile, TileDesc *td)
 
static void GetTileDesc_Station(TileIndex tile, TileDesc *td)
 
{
 
	byte m5;
 
	StringID str;
 

	
 
	td->owner = GetTileOwner(tile);
 
	td->build_date = GetStation(_map2[tile])->build_date;
 

	
 
	m5 = _map5[tile];
 
	(str=STR_305E_RAILROAD_STATION, m5 < 8) ||
 
	(str=STR_305F_AIRCRAFT_HANGAR, m5==32 || m5==45) || // hangars
 
	(str=STR_3060_AIRPORT, m5 < 0x43 || (m5 >= 83 && m5 <= 114)) ||
 
	(str=STR_3061_TRUCK_LOADING_AREA, m5 < 0x47) ||
 
	(str=STR_3062_BUS_STATION, m5 < 0x4B) ||
 
	(str=STR_4807_OIL_RIG, m5 == 0x4B) ||
 
	(str=STR_3063_SHIP_DOCK, m5 != 0x52) ||
 
	(str=STR_3069_BUOY, true);
 
	td->str = str;
 
}
 

	
 

	
 
static uint32 GetTileTrackStatus_Station(uint tile, TransportType mode) {
 
static uint32 GetTileTrackStatus_Station(TileIndex tile, TransportType mode)
 
{
 
	uint i = _map5[tile];
 
	uint j = 0;
 

	
 
	switch (mode) {
 
		case TRANSPORT_RAIL:
 
			if (i < 8) {
 
				const byte tile_track_status_rail[8] = { 1, 2, 1, 2, 1, 2, 1, 2 };
 
				j = tile_track_status_rail[i];
 
			}
 
			j += (j << 8);
 
			break;
 

	
 
@@ -2238,47 +2241,47 @@ static uint32 GetTileTrackStatus_Station
 
			// (0x3F, all tracks available)
 
			if (i == 0x52) j = 0x3F;
 
			j += (j << 8);
 
			break;
 

	
 
		default:
 
			break;
 
	}
 

	
 
	return j;
 
}
 

	
 
static void TileLoop_Station(uint tile)
 
static void TileLoop_Station(TileIndex tile)
 
{
 
  //FIXME -- GetTileTrackStatus_Station -> animated stationtiles
 
  // hardcoded.....not good
 
  // 0x27 - large big airport (39)
 
  // 0x66 - radar metropolitan airport (102)
 
  // 0x5A - radar international airport (90)
 
  // 0x3A - flag small airport (58)
 
	if (_map5[tile] == 39 || _map5[tile] == 58 || _map5[tile] == 90 || _map5[tile] == 102)
 
		AddAnimatedTile(tile);
 

	
 
	// treat a bouy tile as water.
 
	else if (_map5[tile] == 0x52)
 
		TileLoop_Water(tile);
 

	
 
	// treat a oilrig (the station part) as water
 
	else if (_map5[tile] == 0x4B)
 
		TileLoop_Water(tile);
 

	
 
}
 

	
 

	
 
static void AnimateTile_Station(uint tile)
 
static void AnimateTile_Station(TileIndex tile)
 
{
 
	byte m5 = _map5[tile];
 
	//FIXME -- AnimateTile_Station -> not nice code, lots of things double
 
  // again hardcoded...was a quick hack
 

	
 
  // turning radar / windsack on airport
 
	if (m5 >= 39 && m5 <= 50) { // turning radar (39 - 50)
 
		if (_tick_counter & 3)
 
			return;
 

	
 
		if (++m5 == 50+1)
 
			m5 = 39;
 
@@ -2301,40 +2304,40 @@ static void AnimateTile_Station(uint til
 
	} else if (m5 >= 0x3A && m5 <= 0x3D) {  // windsack (58 - 61)
 
		if (_tick_counter & 1)
 
			return;
 

	
 
		if (++m5 == 0x3D+1)
 
			m5 = 0x3A;
 

	
 
		_map5[tile] = m5;
 
		MarkTileDirtyByTile(tile);
 
	}
 
}
 

	
 
static void ClickTile_Station(uint tile)
 
static void ClickTile_Station(TileIndex tile)
 
{
 
  // 0x20 - hangar large airport (32)
 
  // 0x41 - hangar small airport (65)
 
	if (_map5[tile] == 32 || _map5[tile] == 65) {
 
		ShowAircraftDepotWindow(tile);
 
	} else {
 
		ShowStationViewWindow(_map2[tile]);
 
	}
 
}
 

	
 
static const byte _enter_station_speedtable[12] = {
 
	215, 195, 175, 155, 135, 115, 95, 75, 55, 35, 15, 0
 
};
 

	
 
static uint32 VehicleEnter_Station(Vehicle *v, uint tile, int x, int y)
 
static uint32 VehicleEnter_Station(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	StationID station_id;
 
	byte dir;
 
	uint16 spd;
 

	
 
	if (v->type == VEH_Train) {
 
		if (IS_BYTE_INSIDE(_map5[tile], 0, 8) && v->subtype == TS_Front_Engine &&
 
				!IsCompatibleTrainStationTile(tile + TileOffsByDir(v->direction >> 1), tile)) {
 

	
 
			station_id = _map2[tile];
 
			if ((!(v->current_order.flags & OF_NON_STOP) && !_patches.new_nonstop) ||
 
					(v->current_order.type == OT_GOTO_STATION && v->current_order.station == station_id)) {
 
@@ -2667,25 +2670,25 @@ int32 CmdRenameStation(int x, int y, uin
 
		UpdateStationVirtCoord(st);
 
		DeleteName(old_str);
 
		_station_sort_dirty[st->owner] = true; // rename a station
 
		MarkWholeScreenDirty();
 
	} else {
 
		DeleteName(str);
 
	}
 

	
 
	return 0;
 
}
 

	
 

	
 
uint MoveGoodsToStation(uint tile, int w, int h, int type, uint amount)
 
uint MoveGoodsToStation(TileIndex tile, int w, int h, int type, uint amount)
 
{
 
	Station *around_ptr[8];
 
	StationID around[8];
 
	StationID st_index;
 
	int i;
 
	Station *st;
 
	uint moved;
 
	uint best_rating, best_rating2;
 
	Station *st1, *st2;
 
	int t;
 
	int rad=0;
 
	int w_prod=0, h_prod=0; //width and height of the "producer" of the cargo
 
@@ -2804,25 +2807,25 @@ uint MoveGoodsToStation(uint tile, int w
 
		amount -= t;
 
		UpdateStationWaiting(st1, type, moved);
 
	}
 

	
 
	if (amount != 0) {
 
		moved += (amount = (amount * best_rating2 >> 8) + 1);
 
		UpdateStationWaiting(st2, type, amount);
 
	}
 

	
 
	return moved;
 
}
 

	
 
void BuildOilRig(uint tile)
 
void BuildOilRig(TileIndex tile)
 
{
 
	Station *st;
 
	int j;
 

	
 
	FOR_ALL_STATIONS(st) {
 
		if (st->xy == 0) {
 
			st->town = ClosestTownFromTile(tile, (uint)-1);
 
			st->sign.width_1 = 0;
 
			if (!GenerateStationName(st, tile, 2))
 
				return;
 

	
 
			SetTileType(tile, MP_STATION);
 
@@ -2855,54 +2858,55 @@ void BuildOilRig(uint tile)
 
				st->goods[j].rating = 175;
 
				st->goods[j].last_speed = 0;
 
				st->goods[j].last_age = 255;
 
			}
 

	
 
			UpdateStationVirtCoordDirty(st);
 
			UpdateStationAcceptance(st, false);
 
			return;
 
		}
 
	}
 
}
 

	
 
void DeleteOilRig(uint tile)
 
void DeleteOilRig(TileIndex tile)
 
{
 
	Station *st = GetStation(_map2[tile]);
 

	
 
	DoClearSquare(tile);
 

	
 
	st->dock_tile = 0;
 
	st->airport_tile = 0;
 
	st->facilities &= ~(FACIL_AIRPORT | FACIL_DOCK);
 
	st->airport_flags = 0;
 
	UpdateStationVirtCoordDirty(st);
 
	DeleteStation(st);
 
}
 

	
 
static void ChangeTileOwner_Station(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Station(TileIndex tile, byte old_player, byte new_player)
 
{
 
	if (!IsTileOwner(tile, old_player)) return;
 

	
 
	if (new_player != 255) {
 
		Station *st = GetStation(_map2[tile]);
 
		SetTileOwner(tile, new_player);
 
		st->owner = new_player;
 
		_global_station_sort_dirty = true; // transfer ownership of station to another player
 
		InvalidateWindowClasses(WC_STATION_LIST);
 
	} else {
 
		DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
 
	}
 
}
 

	
 
static int32 ClearTile_Station(uint tile, byte flags) {
 
static int32 ClearTile_Station(TileIndex tile, byte flags)
 
{
 
	byte m5 = _map5[tile];
 
	Station *st;
 

	
 
	if (flags & DC_AUTO) {
 
		if (m5 < 8) return_cmd_error(STR_300B_MUST_DEMOLISH_RAILROAD);
 
		if (m5 < 0x43 || (m5 >= 83 && m5 <= 114)) return_cmd_error(STR_300E_MUST_DEMOLISH_AIRPORT_FIRST);
 
		if (m5 < 0x47) return_cmd_error(STR_3047_MUST_DEMOLISH_TRUCK_STATION);
 
		if (m5 < 0x4B) return_cmd_error(STR_3046_MUST_DEMOLISH_BUS_STATION);
 
		if (m5 == 0x52) return_cmd_error(STR_306A_BUOY_IN_THE_WAY);
 
		if (m5 != 0x4B && m5 < 0x53) return_cmd_error(STR_304D_MUST_DEMOLISH_DOCK_FIRST);
 
		SetDParam(0, STR_4807_OIL_RIG);
 
		return_cmd_error(STR_4800_IN_THE_WAY);
terraform_gui.c
Show inline comments
 
@@ -3,34 +3,34 @@
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
#include "tile.h"
 
#include "window.h"
 
#include "gui.h"
 
#include "viewport.h"
 
#include "gfx.h"
 
#include "sound.h"
 
#include "command.h"
 
#include "vehicle.h"
 
#include "signs.h"
 

	
 
void CcTerraform(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcTerraform(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	if (success) {
 
		SndPlayTileFx(SND_1F_SPLAT, tile);
 
	} else {
 
		SetRedErrorSquare(_terraform_err_tile);
 
	}
 
}
 

	
 
static void GenericRaiseLowerLand(uint tile, int mode)
 
static void GenericRaiseLowerLand(TileIndex tile, int mode)
 
{
 
	if (mode) {
 
		DoCommandP(tile, 8, (uint32)mode, CcTerraform, CMD_TERRAFORM_LAND | CMD_AUTO | CMD_MSG(STR_0808_CAN_T_RAISE_LAND_HERE));
 
	} else {
 
		DoCommandP(tile, 8, (uint32)mode, CcTerraform, CMD_TERRAFORM_LAND | CMD_AUTO | CMD_MSG(STR_0809_CAN_T_LOWER_LAND_HERE));
 
	}
 
}
 

	
 
/** Scenario editor command that generates desert areas */
 
static void GenerateDesertArea(TileIndex end, TileIndex start)
 
{
 
	int size_x, size_y;
 
@@ -92,45 +92,45 @@ bool GUIPlaceProcDragXY(const WindowEven
 
typedef void OnButtonClick(Window *w);
 

	
 
static const uint16 _terraform_keycodes[] = {
 
	'Q',
 
	'W',
 
	'E',
 
	'D',
 
	'U',
 
	'I',
 
	'O',
 
};
 

	
 
void PlaceProc_DemolishArea(uint tile)
 
void PlaceProc_DemolishArea(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_AND_Y | GUI_PlaceProc_DemolishArea);
 
}
 

	
 
void PlaceProc_RaiseLand(uint tile)
 
void PlaceProc_RaiseLand(TileIndex tile)
 
{
 
	GenericRaiseLowerLand(tile, 1);
 
}
 

	
 
void PlaceProc_LowerLand(uint tile)
 
void PlaceProc_LowerLand(TileIndex tile)
 
{
 
	GenericRaiseLowerLand(tile, 0);
 
}
 

	
 
void PlaceProc_LevelLand(uint tile)
 
void PlaceProc_LevelLand(TileIndex tile)
 
{
 
	VpStartPlaceSizing(tile, VPM_X_AND_Y | GUI_PlaceProc_LevelArea);
 
}
 

	
 
static void PlaceProc_PlantTree(uint tile) {}
 
static void PlaceProc_PlantTree(TileIndex tile) {}
 

	
 
static void TerraformClick_Lower(Window *w)
 
{
 
	HandlePlacePushButton(w, 4, ANIMCURSOR_LOWERLAND, 2, PlaceProc_LowerLand);
 
}
 

	
 
static void TerraformClick_Raise(Window *w)
 
{
 
	HandlePlacePushButton(w, 5, ANIMCURSOR_RAISELAND, 2, PlaceProc_RaiseLand);
 
}
 

	
 
static void TerraformClick_Level(Window *w)
texteff.c
Show inline comments
 
@@ -311,59 +311,59 @@ void DrawTextEffects(DrawPixelInfo *dpi)
 
			/* intersection? */
 
			if (dpi->left > te->right*2 -  te->x ||
 
					dpi->top > te->bottom*2 - te->y ||
 
					(dpi->left + dpi->width) <= te->x ||
 
					(dpi->top + dpi->height) <= te->y)
 
						continue;
 
			AddStringToDraw(te->x, te->y, (StringID)(te->string_id-1), te->params_1, te->params_2, 0);
 
		}
 

	
 
	}
 
}
 

	
 
void DeleteAnimatedTile(uint tile)
 
void DeleteAnimatedTile(TileIndex tile)
 
{
 
	TileIndex *ti;
 

	
 
	for(ti=_animated_tile_list; ti!=endof(_animated_tile_list); ti++) {
 
		if ( (TileIndex)tile == *ti) {
 
		if (tile == *ti) {
 
			/* remove the hole */
 
			memmove(ti, ti+1, endof(_animated_tile_list) - 1 - ti);
 
			/* and clear last item */
 
			endof(_animated_tile_list)[-1] = 0;
 
			MarkTileDirtyByTile(tile);
 
			return;
 
		}
 
	}
 
}
 

	
 
bool AddAnimatedTile(uint tile)
 
bool AddAnimatedTile(TileIndex tile)
 
{
 
	TileIndex *ti;
 

	
 
	for(ti=_animated_tile_list; ti!=endof(_animated_tile_list); ti++) {
 
		if ( (TileIndex)tile == *ti || *ti == 0) {
 
		if (tile == *ti || *ti == 0) {
 
			*ti = tile;
 
			MarkTileDirtyByTile(tile);
 
			return true;
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 
void AnimateAnimatedTiles(void)
 
{
 
	TileIndex *ti;
 
	uint tile;
 
	TileIndex tile;
 

	
 
	for(ti=_animated_tile_list; ti!=endof(_animated_tile_list) && (tile=*ti) != 0; ti++) {
 
		AnimateTile(tile);
 
	}
 
}
 

	
 
void InitializeAnimatedTiles(void)
 
{
 
	memset(_animated_tile_list, 0, sizeof(_animated_tile_list));
 
}
 

	
 
static void SaveLoad_ANIT(void)
town.h
Show inline comments
 
@@ -118,25 +118,25 @@ enum {
 
	RATING_INDUSTRY_DOWN_STEP = -1500,
 
	RATING_INDUSTRY_MINIMUM = RATING_MINIMUM,
 

	
 
	RATING_ROAD_DOWN_STEP = -50,
 
	RATING_ROAD_MINIMUM = -100,
 
	RATING_HOUSE_MINIMUM = RATING_MINIMUM,
 

	
 
	RATING_BRIBE_UP_STEP = 200,
 
	RATING_BRIBE_MAXIMUM = 800,
 
	RATING_BRIBE_DOWN_TO = -50 					// XXX SHOULD BE SOMETHING LOWER?
 
};
 

	
 
bool CheckforTownRating(uint tile, uint32 flags, Town *t, byte type);
 
bool CheckforTownRating(TileIndex tile, uint32 flags, Town *t, byte type);
 

	
 
VARDEF uint16 *_town_sort;
 

	
 
extern MemoryPool _town_pool;
 

	
 
/**
 
 * Check if a Town really exists.
 
 */
 
static inline bool IsValidTown(Town* town)
 
{
 
	return town->xy != 0; /* XXX: Replace by INVALID_TILE someday */
 
}
town_cmd.c
Show inline comments
 
@@ -43,27 +43,27 @@ MemoryPool _town_pool = { "Towns", TOWN_
 
 * one town should get grown per tick. The other numbers of towns
 
 * are then scaled based on that. */
 
#define TOWN_GROWTH_FREQUENCY 23
 

	
 
enum {
 
	TOWN_HAS_CHURCH     = 0x02,
 
	TOWN_HAS_STADIUM    = 0x04
 
};
 

	
 
// Local
 
static int _grow_town_result;
 

	
 
static bool BuildTownHouse(Town *t, uint tile);
 
static void ClearTownHouse(Town *t, uint tile);
 
static void DoBuildTownHouse(Town *t, uint tile);
 
static bool BuildTownHouse(Town *t, TileIndex tile);
 
static void ClearTownHouse(Town *t, TileIndex tile);
 
static void DoBuildTownHouse(Town *t, TileIndex tile);
 

	
 
typedef struct DrawTownTileStruct {
 
	uint32 sprite_1;
 
	uint32 sprite_2;
 

	
 
	byte subtile_x:4;
 
	byte subtile_y:4;
 
	byte width:4;
 
	byte height:4;
 
	byte dz;
 
	byte proc;
 
} DrawTownTileStruct;
 
@@ -141,25 +141,25 @@ static void DrawTile_Town(TileInfo *ti)
 
static uint GetSlopeZ_Town(TileInfo *ti)
 
{
 
	uint z = GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z;
 
	if (ti->tileh != 0) z = (z & ~7) + 4;
 
	return (uint16) z;
 
}
 

	
 
static uint GetSlopeTileh_Town(TileInfo *ti)
 
{
 
	return ti->tileh;
 
}
 

	
 
static void AnimateTile_Town(uint tile)
 
static void AnimateTile_Town(TileIndex tile)
 
{
 
	int old;
 
	int i;
 
	int a,b;
 

	
 
	if (_tick_counter & 3)
 
		return;
 

	
 
	if (_map3_hi[tile] != 4 && _map3_hi[tile] != 5)
 
		return;
 

	
 
	if (!((old=_map_owner[tile])&0x80)) {
 
@@ -179,25 +179,25 @@ static void AnimateTile_Town(uint tile)
 

	
 
	if (a == b) {
 
		_map5[tile] &= 0x40;
 
		_map_owner[tile] &= 0x7F;
 
		DeleteAnimatedTile(tile);
 
	}
 

	
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
static void UpdateTownRadius(Town *t);
 

	
 
static bool IsCloseToTown(uint tile, uint dist)
 
static bool IsCloseToTown(TileIndex tile, uint dist)
 
{
 
	Town *t;
 

	
 
	FOR_ALL_TOWNS(t) {
 
		if (t->xy != 0 && DistanceManhattan(tile, t->xy) < dist)
 
			return true;
 
	}
 
	return false;
 
}
 

	
 
static void MarkTownSignDirty(Town *t)
 
{
 
@@ -233,57 +233,57 @@ static void ChangePopulation(Town *t, in
 

	
 
uint32 GetWorldPopulation(void)
 
{
 
	uint32 pop;
 
	Town *t;
 
	pop = 0;
 
	FOR_ALL_TOWNS(t) {
 
			pop += t->population;
 
	}
 
	return pop;
 
}
 

	
 
static void MakeSingleHouseBigger(uint tile)
 
static void MakeSingleHouseBigger(TileIndex tile)
 
{
 
	byte b;
 

	
 
	assert(IsTileType(tile, MP_HOUSE));
 

	
 
	b = _map5[tile];
 
	if (b & 0x80)
 
		return;
 

	
 
	_map5[tile] = (b & 0xC0) | ((b+1)&7);
 

	
 
	if ((_map5[tile]&7) != 0)
 
		return;
 

	
 
	_map3_lo[tile] = _map3_lo[tile] + 0x40;
 

	
 
	if ( (_map3_lo[tile] & 0xC0) == 0xC0) {
 
		ChangePopulation(GetTown(_map2[tile]), _housetype_population[_map3_hi[tile]]);
 
	}
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
static void MakeTownHouseBigger(uint tile)
 
static void MakeTownHouseBigger(TileIndex tile)
 
{
 
	uint flags = _house_more_flags[_map3_hi[tile]];
 
	if (flags & 8) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0));
 
	if (flags & 4) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1));
 
	if (flags & 2) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0));
 
	if (flags & 1) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1));
 
}
 

	
 
static void TileLoop_Town(uint tile)
 
static void TileLoop_Town(TileIndex tile)
 
{
 
	int house;
 
	Town *t;
 
	uint32 r;
 

	
 
	if ((_map3_lo[tile] & 0xC0) != 0xC0) {
 
		MakeTownHouseBigger(tile);
 
		return;
 
	}
 

	
 
	house = _map3_hi[tile];
 
	if (_housetype_extra_flags[house] & 0x20 &&
 
@@ -321,30 +321,30 @@ static void TileLoop_Town(uint tile)
 

	
 
		ClearTownHouse(t, tile);
 

	
 
		// rebuild with another house?
 
		if ( (byte) (r >> 8) >= 12) {
 
			DoBuildTownHouse(t, tile);
 
		}
 

	
 
		_current_player = OWNER_NONE;
 
	}
 
}
 

	
 
static void ClickTile_Town(uint tile)
 
static void ClickTile_Town(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static int32 ClearTile_Town(uint tile, byte flags)
 
static int32 ClearTile_Town(TileIndex tile, byte flags)
 
{
 
	int house, rating;
 
	int32 cost;
 
	Town *t;
 

	
 
	// safety checks
 
	if (!EnsureNoVehicle(tile)) return CMD_ERROR;
 
	if (flags&DC_AUTO && !(flags&DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
 

	
 
	house = _map3_hi[tile];
 
	cost = _price.remove_house * _housetype_remove_cost[house] >> 8;
 

	
 
@@ -358,52 +358,52 @@ static int32 ClearTile_Town(uint tile, b
 
			return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
 
		}
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM);
 
		ClearTownHouse(t, tile);
 
	}
 

	
 
	return cost;
 
}
 

	
 
static void GetAcceptedCargo_Town(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac)
 
{
 
	int type = _map3_hi[tile];
 

	
 
	ac[CT_PASSENGERS] = _housetype_cargo_passengers[type];
 
	ac[CT_MAIL] = _housetype_cargo_mail[type];
 
	ac[CT_GOODS] = _housetype_cargo_goods[type];
 
	ac[CT_FOOD] = _housetype_cargo_food[type];
 
}
 

	
 
static void GetTileDesc_Town(uint tile, TileDesc *td)
 
static void GetTileDesc_Town(TileIndex tile, TileDesc *td)
 
{
 
	td->str = _town_tile_names[_map3_hi[tile]];
 
	if ((_map3_lo[tile] & 0xC0) != 0xC0) {
 
		SetDParamX(td->dparam, 0, td->str);
 
		td->str = STR_2058_UNDER_CONSTRUCTION;
 
	}
 

	
 
	td->owner = OWNER_TOWN;
 
}
 

	
 
static uint32 GetTileTrackStatus_Town(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_Town(TileIndex tile, TransportType mode)
 
{
 
	/* not used */
 
	return 0;
 
}
 

	
 
static void ChangeTileOwner_Town(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Town(TileIndex tile, byte old_player, byte new_player)
 
{
 
	/* not used */
 
}
 

	
 

	
 
static const TileIndexDiffC _roadblock_tileadd[] = {
 
	{ 0, -1},
 
	{ 1,  0},
 
	{ 0,  1},
 
	{-1,  0},
 

	
 
	// Store the first 3 elements again.
 
@@ -457,25 +457,25 @@ static byte GetTownRoadMask(TileIndex ti
 
{
 
	byte b = GetRoadBitsByTile(tile);
 
	byte r=0;
 
	if (b&1) r|=10;
 
	if (b&2) r|=5;
 
	if (b&4) r|=9;
 
	if (b&8) r|=6;
 
	if (b&16) r|=3;
 
	if (b&32) r|=12;
 
	return r;
 
}
 

	
 
static bool IsRoadAllowedHere(uint tile, int dir)
 
static bool IsRoadAllowedHere(TileIndex tile, int dir)
 
{
 
	uint k;
 
	uint slope;
 

	
 
	// If this assertion fails, it might be because the world contains
 
	//  land at the edges. This is not ok.
 
	TILE_ASSERT(tile);
 

	
 
	for(;;) {
 
		// Check if there already is a road at this point?
 
		if (GetRoadBitsByTile(tile) == 0) {
 
			// No, try to build one in the direction.
 
@@ -519,65 +519,65 @@ no_slope:
 
					// We can consider building on the slope, though.
 
					goto no_slope;
 
			}
 
			return false;
 
		}
 

	
 
		/* Can somebody explain for what this is needed? :s */
 
		// tile = TILE_ADD(tile, _roadblock_tileadd[dir]);
 
		return true;
 
	}
 
}
 

	
 
static bool TerraformTownTile(uint tile, int edges, int dir)
 
static bool TerraformTownTile(TileIndex tile, int edges, int dir)
 
{
 
	int32 r;
 

	
 
	TILE_ASSERT(tile);
 

	
 
	r = DoCommandByTile(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
 
	if (r == CMD_ERROR || r >= 126*16)
 
		return false;
 
	DoCommandByTile(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND);
 
	return true;
 
}
 

	
 
static void LevelTownLand(uint tile)
 
static void LevelTownLand(TileIndex tile)
 
{
 
	TileInfo ti;
 

	
 
	TILE_ASSERT(tile);
 

	
 
	// Don't terraform if land is plain or if there's a house there.
 
	FindLandscapeHeightByTile(&ti, tile);
 
	if (ti.tileh == 0 || ti.type == MP_HOUSE)
 
		return;
 

	
 
	// First try up, then down
 
	if (!TerraformTownTile(tile, ~ti.tileh & 0xF, 1)) {
 
		TerraformTownTile(tile, ti.tileh & 0xF, 0);
 
	}
 
}
 

	
 
#define IS_WATER_TILE(t) (IsTileType((t), MP_WATER) && _map5[(t)] == 0)
 

	
 
static void GrowTownInTile(uint *tile_ptr, uint mask, int block, Town *t1)
 
static void GrowTownInTile(TileIndex *tile_ptr, uint mask, int block, Town *t1)
 
{
 
	uint16 r;
 
	int a,b,rcmd;
 
	uint tmptile;
 
	TileIndex tmptile;
 
	TileInfo ti;
 
	int i;
 
	int j;
 
	uint tile = *tile_ptr;
 
	TileIndex tile = *tile_ptr;
 

	
 
	TILE_ASSERT(tile);
 

	
 
	if (mask == 0) {
 
		// Tile has no road. First reset the status counter
 
		// to say that this is the last iteration.
 
		_grow_town_result = 0;
 

	
 
		// Remove hills etc
 
		LevelTownLand(tile);
 

	
 
		// Is a road allowed here?
 
@@ -702,25 +702,25 @@ build_road_and_exit:
 
					_grow_town_result = -1;
 

	
 
				// obviously, if building any bridge would fail, there is no need to try other bridge-types
 
				return;
 
			}
 
		} while(--j != 0);
 
	}
 
}
 
#undef IS_WATER_TILE
 

	
 

	
 
// Returns true if a house was built, or no if the build failed.
 
static int GrowTownAtRoad(Town *t, uint tile)
 
static int GrowTownAtRoad(Town *t, TileIndex tile)
 
{
 
	uint mask;
 
	int block = 5; // special case
 

	
 
	TILE_ASSERT(tile);
 

	
 
	// Number of times to search.
 
	_grow_town_result = 10 + t->num_houses * 4 / 9;
 

	
 
	do {
 
		// Get a bitmask of the road blocks on a tile
 
		mask = GetTownRoadMask(tile);
 
@@ -763,25 +763,25 @@ static int GrowTownAtRoad(Town *t, uint 
 
static int GenRandomRoadBits(void)
 
{
 
	uint32 r = Random();
 
	int a = r&3, b = (r >> 8) & 3;
 
	if (a == b) b ^= 2;
 
	return (1<<a)+(1<<b);
 
}
 

	
 
// Grow the town
 
// Returns true if a house was built, or no if the build failed.
 
bool GrowTown(Town *t)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	const TileIndexDiffC *ptr;
 
	TileInfo ti;
 
	byte old_player;
 

	
 
	static const TileIndexDiffC _town_coord_mod[] = {
 
		{-1,  0},
 
		{ 1,  1},
 
		{ 1, -1},
 
		{-1, -1},
 
		{-1,  0},
 
		{ 0,  2},
 
		{ 2,  0},
 
@@ -1056,25 +1056,25 @@ int32 CmdBuildTown(int x, int y, uint32 
 

	
 
	// Create the town
 
	if (flags & DC_EXEC) {
 
		_generating_world = true;
 
		DoCreateTown(t, tile, townnameparts);
 
		_generating_world = false;
 
	}
 
	return 0;
 
}
 

	
 
Town *CreateRandomTown(uint attempts)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	TileInfo ti;
 
	Town *t;
 
	uint32 townnameparts;
 

	
 
	do {
 
		// Generate a tile index not too close from the edge
 
		tile = TILE_MASK(Random());
 
		if (DistanceFromEdge(tile) < 20)
 
			continue;
 

	
 
		// Make sure the tile is plain
 
		FindLandscapeHeightByTile(&ti, tile);
 
@@ -1112,89 +1112,90 @@ void GenerateTowns(void)
 

	
 
	do {
 
		if (CreateRandomTown(20) != NULL) 	//try 20 times for the first loop
 
			num++;
 
	} while (--n);
 

	
 
	if (num == 0 && CreateRandomTown(10000) == NULL) {
 
		//XXX can we handle that more gracefully?
 
		error("Could not generate any town");
 
	}
 
}
 

	
 
static bool CheckBuildHouseMode(Town *t1, uint tile, uint tileh, int mode) {
 
static bool CheckBuildHouseMode(Town *t1, TileIndex tile, uint tileh, int mode)
 
{
 
	int b;
 
	uint slope;
 

	
 
	static const byte _masks[8] = {
 
		0xC,0x3,0x9,0x6,
 
		0x3,0xC,0x6,0x9,
 
	};
 

	
 
	slope = GetTileSlope(tile, NULL);
 
	if (slope & 0x10)
 
		return false;
 

	
 
	b = 0;
 
	if ((slope & 0xF && ~slope & _masks[mode])) b = ~b;
 
	if ((tileh & 0xF && ~tileh & _masks[mode+4])) b = ~b;
 
	if (b)
 
		return false;
 

	
 
	return DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR) != CMD_ERROR;
 
}
 

	
 
int GetTownRadiusGroup(Town *t, uint tile)
 
int GetTownRadiusGroup(Town *t, TileIndex tile)
 
{
 
	uint dist;
 
	int i,smallest;
 

	
 
	dist = DistanceSquare(tile, t->xy);
 
	if (t->fund_buildings_months && dist <= 25)
 
		return 4;
 

	
 
	smallest = 0;
 
	for(i=0; i!=lengthof(t->radius); i++) {
 
		if (dist < t->radius[i])
 
			smallest = i;
 
	}
 

	
 
	return smallest;
 
}
 

	
 
static bool CheckFree2x2Area(Town *t1, uint tile)
 
static bool CheckFree2x2Area(Town *t1, TileIndex tile)
 
{
 
	int i;
 

	
 
	static const TileIndexDiffC _tile_add[] = {
 
		{0    , 0    },
 
		{0 - 0, 1 - 0},
 
		{1 - 0, 0 - 1},
 
		{1 - 1, 1 - 0}
 
	};
 

	
 
	for(i=0; i!=4; i++) {
 
		tile += ToTileIndexDiff(_tile_add[i]);
 

	
 
		if (GetTileSlope(tile, NULL))
 
			return false;
 

	
 
		if (DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_FORCETEST, CMD_LANDSCAPE_CLEAR) == CMD_ERROR)
 
			return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
static void DoBuildTownHouse(Town *t, uint tile)
 
static void DoBuildTownHouse(Town *t, TileIndex tile)
 
{
 
	int i;
 
	uint bitmask;
 
	int house;
 
	uint slope;
 
	uint z;
 
	uint oneof;
 

	
 
	// Above snow?
 
	slope = GetTileSlope(tile, &z);
 

	
 
	// Get the town zone type
 
@@ -1349,48 +1350,49 @@ static void DoBuildTownHouse(Town *t, ui
 
				t->index,
 
				m3lo,			/* map3_lo */
 
				++house,	/* map3_hi */
 
				0,				/* map_owner */
 
				m5				/* map5 */
 
			);
 
		}
 
	}
 

	
 
	// ENDING
 
}
 

	
 
static bool BuildTownHouse(Town *t, uint tile)
 
static bool BuildTownHouse(Town *t, TileIndex tile)
 
{
 
	int32 r;
 

	
 
	// make sure it's possible
 
	if (!EnsureNoVehicle(tile)) return false;
 
	if (GetTileSlope(tile, NULL) & 0x10) return false;
 

	
 
	r = DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
 
	if (r == CMD_ERROR) return false;
 

	
 
	DoBuildTownHouse(t, tile);
 
	return true;
 
}
 

	
 

	
 
static void DoClearTownHouseHelper(uint tile)
 
static void DoClearTownHouseHelper(TileIndex tile)
 
{
 
	assert(IsTileType(tile, MP_HOUSE));
 
	DoClearSquare(tile);
 
	DeleteAnimatedTile(tile);
 
}
 

	
 
static void ClearTownHouse(Town *t, uint tile) {
 
static void ClearTownHouse(Town *t, TileIndex tile)
 
{
 
	uint house = _map3_hi[tile];
 
	uint eflags;
 

	
 
	assert(IsTileType(tile, MP_HOUSE));
 

	
 
	// need to align the tile to point to the upper left corner of the house
 
	if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks.
 
		if (_housetype_extra_flags[house-1] & 0x04) {
 
			house--;
 
			tile += TILE_XY(-1,0);
 
		} else if (_housetype_extra_flags[house-1] & 0x18) {
 
			house--;
 
@@ -1555,25 +1557,25 @@ static void TownActionRoadRebuild(Town *
 
	t->road_build_months = 6;
 

	
 
	SetDParam(0, t->index);
 

	
 
	p = GetPlayer(_current_player);
 
	SetDParam(1, p->name_1);
 
	SetDParam(2, p->name_2);
 

	
 
	AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING,
 
		NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_GENERAL, 0), t->xy, 0);
 
}
 

	
 
static bool DoBuildStatueOfCompany(uint tile)
 
static bool DoBuildStatueOfCompany(TileIndex tile)
 
{
 
	TileInfo ti;
 
	byte old;
 
	int32 r;
 

	
 
	FindLandscapeHeightByTile(&ti, tile);
 
	if (ti.tileh != 0)
 
		return false;
 

	
 
	if (ti.type != MP_HOUSE && ti.type != MP_CLEAR && ti.type != MP_TREES)
 
		return false;
 

	
 
@@ -1607,25 +1609,25 @@ static void TownActionBuildStatue(Town *
 
		{ 0,-1}, { 0,-1}, { 0,-1}, { 0,-1},
 
		{-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0},
 
		{ 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1},
 
		{ 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0},
 
		{ 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1},
 
		{-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0},
 
		{ 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1},
 
		{ 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0},
 
		{ 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1},
 
		{-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0},
 
		{ 0, 0}
 
	};
 
	uint tile = t->xy;
 
	TileIndex tile = t->xy;
 
	const TileIndexDiffC *p;
 

	
 
	SETBIT(t->statues, _current_player);
 

	
 
	for (p = _statue_tiles; p != endof(_statue_tiles); ++p) {
 
		if (DoBuildStatueOfCompany(tile))
 
			return;
 
		tile = TILE_ADD(tile, ToTileIndexDiff(*p));
 
	}
 
}
 

	
 
static void TownActionFundBuildings(Town *t, int action)
 
@@ -1802,46 +1804,46 @@ static void UpdateTownAmounts(Town *t)
 
}
 

	
 
static void UpdateTownUnwanted(Town *t)
 
{
 
	Player *p;
 

	
 
	FOR_ALL_PLAYERS(p) {
 
		if (t->unwanted[p->index] > 0)
 
			t->unwanted[p->index]--;
 
	}
 
}
 

	
 
bool CheckIfAuthorityAllows(uint tile)
 
bool CheckIfAuthorityAllows(TileIndex tile)
 
{
 
	Town *t;
 

	
 
	if (_current_player >= MAX_PLAYERS)
 
		return true;
 

	
 
	t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 
	if (t == NULL)
 
		return true;
 

	
 
	if (t->ratings[_current_player] > -200)
 
		return true;
 

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

	
 
	return false;
 
}
 

	
 

	
 
Town *ClosestTownFromTile(uint tile, uint threshold)
 
Town *ClosestTownFromTile(TileIndex tile, uint threshold)
 
{
 
	Town *t;
 
	uint dist, best = threshold;
 
	Town *best_town = NULL;
 

	
 
	// XXX - Fix this so for a given tiletype the owner of the type is in the same variable
 
	if (IsTileType(tile, MP_HOUSE) || (
 
				IsTileType(tile, MP_STREET) &&
 
				(IsLevelCrossing(tile) ? _map3_lo[tile] : GetTileOwner(tile)) == OWNER_TOWN
 
			))
 
		return GetTown(_map2[tile]);
 

	
 
@@ -1883,25 +1885,25 @@ void ChangeTownRating(Town *t, int add, 
 
	}
 
	t->ratings[_current_player] = rating;
 
}
 

	
 
/*	penalty for removing town-owned stuff */
 
static const int _default_rating_settings [3][3] = {
 
	// ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE
 
	{  0, 128, 384},	// Permissive
 
	{ 48, 192, 480},	// Neutral
 
	{ 96, 384, 768},	// Hostile
 
};
 

	
 
bool CheckforTownRating(uint tile, uint32 flags, Town *t, byte type)
 
bool CheckforTownRating(TileIndex tile, uint32 flags, Town *t, byte type)
 
{
 
	int modemod;
 

	
 
	//	if magic_bulldozer cheat is active, town doesn't restrict your destructive actions
 
	if (t == NULL || _current_player >= MAX_PLAYERS || _cheats.magic_bulldozer.value)
 
		return true;
 

	
 
	/*	check if you're allowed to remove the street/bridge/tunnel/industry
 
	 *	owned by a town	no removal if rating is lower than ... depends now on
 
	 *	difficulty setting. Minimum town rating selected by difficulty level
 
	 */
 
	modemod = _default_rating_settings[_opt.diff.town_council_tolerance][type];
train_cmd.c
Show inline comments
 
@@ -406,25 +406,25 @@ void DrawTrainEngineInfo(int engine, int
 
	SetDParam(4, (rvi->running_cost_base * _price.running_rail[rvi->engclass] >> 8) << multihead);
 

	
 
	cap = rvi->capacity;
 
	SetDParam(5, STR_8838_N_A);
 
	if (cap != 0) {
 
		SetDParam(6, cap << multihead);
 
		SetDParam(5, _cargoc.names_long_p[rvi->cargo_type]);
 
	}
 
	DrawStringMultiCenter(x, y, STR_885B_COST_WEIGHT_T_SPEED_POWER, maxw);
 
}
 

	
 

	
 
static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags)
 
static int32 CmdBuildRailWagon(uint engine, TileIndex tile, uint32 flags)
 
{
 
	int32 value;
 
	Vehicle *v;
 
	const RailVehicleInfo *rvi;
 
	int dir;
 
	const Engine *e;
 
	int x,y;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 

	
 
	rvi = RailVehInfo(engine);
 
	value = (rvi->base_cost * _price.build_railwagon) >> 8;
 
@@ -1497,34 +1497,34 @@ int32 CmdRefitRailVehicle(int x, int y, 
 
			}
 
		}
 
	// SkipStoppedInDepotCheck is called by CmdReplace and it should only apply to the single car it is called for
 
	} while ( (v=v->next) != NULL || SkipStoppedInDepotCheck );
 

	
 
	_returned_refit_amount = num;
 

	
 
	return cost;
 
}
 

	
 
typedef struct TrainFindDepotData {
 
	uint best_length;
 
	uint tile;
 
	TileIndex tile;
 
	byte owner;
 
	/**
 
	 * true if reversing is necesarry for the train to get to this depot This
 
	 * value is unused when new depot finding and NPF are both disabled
 
	 */
 
	bool reverse;
 
} TrainFindDepotData;
 

	
 
static bool TrainFindDepotEnumProc(uint tile, TrainFindDepotData *tfdd, int track, uint length, byte *state)
 
static bool TrainFindDepotEnumProc(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length, byte *state)
 
{
 
	if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, tfdd->owner)) {
 
		if ((_map5[tile] & ~0x3) == 0xC0) {
 
			if (length < tfdd->best_length) {
 
				tfdd->best_length = length;
 
				tfdd->tile = tile;
 
			}
 
			return true;
 
		}
 

	
 
		// make sure the train doesn't run against a oneway signal
 
		if ((_map5[tile] & 0xC0) == 0x40) {
 
@@ -1534,25 +1534,25 @@ static bool TrainFindDepotEnumProc(uint 
 
	}
 

	
 
	// stop  searching if we've found a destination that is closer already.
 
	return length >= tfdd->best_length;
 
}
 

	
 
// returns the tile of a depot to goto to. The given vehicle must not be
 
// crashed!
 
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
 
{
 
	int i;
 
	TrainFindDepotData tfdd;
 
	uint tile = v->tile;
 
	TileIndex tile = v->tile;
 

	
 
	assert(!(v->vehstatus & VS_CRASHED));
 

	
 
	tfdd.owner = v->owner;
 
	tfdd.best_length = (uint)-1;
 
	tfdd.reverse = false;
 

	
 
	if (IsTileDepotType(tile, TRANSPORT_RAIL)){
 
		tfdd.tile = tile;
 
		tfdd.best_length = 0;
 
		return tfdd;
 
	}
 
@@ -1798,25 +1798,26 @@ static bool CheckTrainStayInDepot(Vehicl
 
	return false;
 
}
 

	
 
/* Check for station tiles */
 
typedef struct TrainTrackFollowerData {
 
	TileIndex dest_coords;
 
	StationID station_index; // station index we're heading for
 
	uint best_bird_dist;
 
	uint best_track_dist;
 
	byte best_track;
 
} TrainTrackFollowerData;
 

	
 
static bool TrainTrackFollower(uint tile, TrainTrackFollowerData *ttfd, int track, uint length, byte *state){
 
static bool TrainTrackFollower(TileIndex tile, TrainTrackFollowerData *ttfd, int track, uint length, byte *state)
 
{
 
	if (IsTileType(tile, MP_RAILWAY) && (_map5[tile]&0xC0) == 0x40) {
 
		// the tile has a signal
 
		byte m3 = _map3_lo[tile];
 
		if (!(m3 & SignalAlongTrackdir(track))) {
 
			// if one way signal not pointing towards us, stop going in this direction.
 
			if (m3 & SignalAgainstTrackdir(track))
 
				return true;
 
		} else if (_map2[tile] & SignalAlongTrackdir(track)) {
 
			// green signal in our direction. either one way or two way.
 
			*state = true;
 
		} else if (m3 & SignalAgainstTrackdir(track)) {
 
			// two way signal. unless we passed another green signal on the way,
 
@@ -1898,25 +1899,25 @@ static const byte _pick_track_table[6] =
 
#if PF_BENCHMARK
 
unsigned int rdtsc()
 
{
 
     unsigned int high, low;
 

	
 
     __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
 
     return low;
 
}
 
#endif
 

	
 

	
 
/* choose a track */
 
static byte ChooseTrainTrack(Vehicle *v, uint tile, int enterdir, TrackdirBits trackdirbits)
 
static byte ChooseTrainTrack(Vehicle *v, TileIndex tile, int enterdir, TrackdirBits trackdirbits)
 
{
 
	TrainTrackFollowerData fd;
 
	uint best_track;
 
#if PF_BENCHMARK
 
	int time = rdtsc();
 
	static float f;
 
#endif
 

	
 
	assert( (trackdirbits & ~0x3F) == 0);
 

	
 
	/* quick return in case only one possible track is available */
 
	if (KILL_FIRST_BIT(trackdirbits) == 0)
 
@@ -2375,25 +2376,25 @@ static byte AfterSetTrainPos(Vehicle *v,
 

	
 
	VehiclePositionChanged(v);
 
	EndVehicleMove(v);
 
	return old_z;
 
}
 

	
 
static const byte _new_vehicle_direction_table[11] = {
 
	0, 7, 6, 0,
 
	1, 0, 5, 0,
 
	2, 3, 4,
 
};
 

	
 
static int GetNewVehicleDirectionByTile(uint new_tile, uint old_tile)
 
static int GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
 
{
 
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
 
							TileX(new_tile) - TileX(old_tile) + 1;
 
	assert(offs < 11);
 
	return _new_vehicle_direction_table[offs];
 
}
 

	
 
static int GetNewVehicleDirection(Vehicle *v, int x, int y)
 
{
 
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
 
	assert(offs < 11);
 
	return _new_vehicle_direction_table[offs];
 
@@ -2497,25 +2498,25 @@ static void AffectSpeedByZChange(Vehicle
 
	} else {
 
		uint16 spd = v->cur_speed + rsp->z_down;
 
		if (spd <= v->max_speed)
 
			v->cur_speed = spd;
 
	}
 
}
 

	
 
static const byte _otherside_signal_directions[14] = {
 
	1, 3, 1, 3, 5, 3, 0, 0,
 
	5, 7, 7, 5, 7, 1,
 
};
 

	
 
static void TrainMovedChangeSignals(uint tile, int dir)
 
static void TrainMovedChangeSignals(TileIndex tile, int dir)
 
{
 
	int i;
 
	if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40) {
 
		i = FindFirstBit2x64((_map5[tile]+(_map5[tile]<<8)) & _reachable_tracks[dir]);
 
		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
 
	}
 
}
 

	
 

	
 
typedef struct TrainCollideChecker {
 
	const Vehicle *v;
 
	const Vehicle *v_skip;
 
@@ -2996,25 +2997,25 @@ static void HandleBrokenTrain(Vehicle *v
 
			v->breakdown_ctr = 0;
 
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
 
		}
 
	}
 
}
 

	
 
static const byte _breakdown_speeds[16] = {
 
	225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
 
};
 

	
 
static bool TrainCheckIfLineEnds(Vehicle *v)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	uint x,y;
 
	int t;
 
	uint32 ts;
 

	
 
	if ((uint)(t=v->breakdown_ctr) > 1) {
 
		v->vehstatus |= VS_TRAIN_SLOWING;
 

	
 
		t = _breakdown_speeds[ ((~t) >> 4) & 0xF];
 
		if ((uint16)t <= v->cur_speed)
 
			v->cur_speed = t;
 
	} else {
 
		v->vehstatus &= ~VS_TRAIN_SLOWING;
 
@@ -3199,25 +3200,25 @@ void Train_Tick(Vehicle *v)
 
}
 

	
 

	
 
static const byte _depot_track_ind[4] = {0,1,0,1};
 

	
 
// Validation for the news item "Train is waiting in depot"
 
static bool ValidateTrainInDepot( uint data_a, uint data_b )
 
{
 
	Vehicle *v = GetVehicle(data_a);
 
	return  (v->u.rail.track == 0x80 && (v->vehstatus | VS_STOPPED));
 
}
 

	
 
void TrainEnterDepot(Vehicle *v, uint tile)
 
void TrainEnterDepot(Vehicle *v, TileIndex tile)
 
{
 
	SetSignalsOnBothDir(tile, _depot_track_ind[_map5[tile]&3]);
 

	
 
	if (v->subtype != TS_Front_Engine)
 
		v = GetFirstVehicleInChain(v);
 

	
 
	VehicleServiceInDepot(v);
 

	
 
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 

	
 
	v->load_unload_time_rem = 0;
 
	v->cur_speed = 0;
train_gui.c
Show inline comments
 
@@ -99,25 +99,25 @@ void DrawTrainWagonPurchaseInfo(int x, i
 
	SetDParam(1, (_cargoc.weights[rvi->cargo_type] * rvi->capacity >> 4) + rvi->weight);
 
	DrawString(x, y, STR_PURCHASE_INFO_WEIGHT_CWEIGHT, 0);
 
	y += 10;
 

	
 
	/* Cargo type + capacity */
 
	SetDParam(0, _cargoc.names_long_p[rvi->cargo_type]);
 
	SetDParam(1, rvi->capacity);
 
	SetDParam(2, refittable ? STR_9842_REFITTABLE : STR_EMPTY);
 
	DrawString(x, y, STR_PURCHASE_INFO_CAPACITY, 0);
 
	y += 10;
 
}
 

	
 
void CcBuildWagon(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildWagon(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v,*found;
 

	
 
	if (!success)
 
		return;
 

	
 
	// find a locomotive in the depot.
 
	found = NULL;
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_Train && v->subtype == TS_Front_Engine &&
 
				v->tile == tile &&
 
				v->u.rail.track == 0x80) {
 
@@ -127,25 +127,25 @@ void CcBuildWagon(bool success, uint til
 
		}
 
	}
 

	
 
	// if we found a loco,
 
	if (found != NULL) {
 
		found = GetLastVehicleInChain(found);
 
		// put the new wagon at the end of the loco.
 
		DoCommandP(0, _new_wagon_id | (found->index<<16), 0, NULL, CMD_MOVE_RAIL_VEHICLE);
 
		RebuildVehicleLists();
 
	}
 
}
 

	
 
void CcBuildLoco(bool success, uint tile, uint32 p1, uint32 p2)
 
void CcBuildLoco(bool success, TileIndex tile, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!success)
 
		return;
 

	
 
	v = GetVehicle(_new_train_id);
 
	if (tile == _backup_orders_tile) {
 
		_backup_orders_tile = 0;
 
		RestoreVehicleOrders(v, _backup_orders_data);
 
	}
 
	ShowTrainViewWindow(v);
 
@@ -295,25 +295,25 @@ static const Widget _new_rail_vehicle_wi
 
{  WWT_RESIZEBOX,     RESIZE_TB,    14,   216,   227,   198,   209, 0x0,											STR_RESIZE_BUTTON},
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _new_rail_vehicle_desc = {
 
	-1, -1, 228, 210,
 
	WC_BUILD_VEHICLE,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_RESIZABLE,
 
	_new_rail_vehicle_widgets,
 
	NewRailVehicleWndProc
 
};
 

	
 
static void ShowBuildTrainWindow(uint tile)
 
static void ShowBuildTrainWindow(TileIndex tile)
 
{
 
	Window *w;
 

	
 
	DeleteWindowById(WC_BUILD_VEHICLE, tile);
 

	
 
	w = AllocateWindowDesc(&_new_rail_vehicle_desc);
 
	w->window_number = tile;
 
	w->vscroll.cap = 8;
 
	w->widget[2].unkA = (w->vscroll.cap << 8) + 1;
 

	
 
	w->resize.step_height = 14;
 
	w->resize.height = w->height - 14 * 4; /* Minimum of 4 vehicles in the display */
 
@@ -345,25 +345,25 @@ static void DrawTrainImage(const Vehicle
 
					DrawFrameRect(x - 1, y - 1, x + width + 4, y + 12, 15, FR_BORDERONLY);
 
			}
 
			x += width + 5;
 
		}
 

	
 
		if (!(v = v->next))
 
			break;
 
	} while (x < max_x);
 
}
 

	
 
static void DrawTrainDepotWindow(Window *w)
 
{
 
	uint tile;
 
	TileIndex tile;
 
	Vehicle *v, *u;
 
	int num,x,y,i, hnum;
 
	Depot *depot;
 

	
 
	tile = w->window_number;
 

	
 
	/* setup disabled buttons */
 
	w->disabled_state =
 
		IsTileOwner(tile, _local_player) ? 0 : ((1 << 4) | (1 << 5) | (1 << 8));
 

	
 
	/* determine amount of items for scroller */
 
	num = 0;
 
@@ -692,25 +692,25 @@ static const Widget _train_depot_widgets
 
{   WIDGETS_END},
 
};
 

	
 
static const WindowDesc _train_depot_desc = {
 
	-1, -1, 361, 122,
 
	WC_VEHICLE_DEPOT,0,
 
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS | WDF_STICKY_BUTTON | WDF_RESIZABLE,
 
	_train_depot_widgets,
 
	TrainDepotWndProc
 
};
 

	
 

	
 
void ShowTrainDepotWindow(uint tile)
 
void ShowTrainDepotWindow(TileIndex tile)
 
{
 
	Window *w;
 

	
 
	w = AllocateWindowDescFront(&_train_depot_desc, tile);
 
	if (w) {
 
		w->caption_color = GetTileOwner(w->window_number);
 
		w->vscroll.cap = 6;
 
		w->hscroll.cap = 10;
 
		w->resize.step_width = 29;
 
		w->resize.step_height = 14;
 
		WP(w,traindepot_d).sel = INVALID_VEHICLE;
 
		_backup_orders_tile = 0;
 
@@ -1382,25 +1382,25 @@ static void PlayerTrainsWndProc(Window *
 

	
 
				if (id_v >= vl->list_length) return; // click out of list bound
 

	
 
				v = GetVehicle(vl->sort_list[id_v].index);
 

	
 
				assert(v->type == VEH_Train && v->subtype == TS_Front_Engine && v->owner == owner);
 

	
 
				ShowTrainViewWindow(v);
 
			}
 
		} break;
 

	
 
		case 9: { /* Build new Vehicle */
 
			uint tile;
 
			TileIndex tile;
 

	
 
			if (!IsWindowOfPrototype(w, _player_trains_widgets))
 
				break;
 

	
 
			tile = _last_built_train_depot_tile;
 
			do {
 
				if (IsTileDepotType(tile, TRANSPORT_RAIL) && IsTileOwner(tile, _local_player)) {
 
					ShowTrainDepotWindow(tile);
 
					ShowBuildTrainWindow(tile);
 
					return;
 
				}
 

	
tree_cmd.c
Show inline comments
 
@@ -26,25 +26,25 @@ static int GetRandomTreeType(TileIndex t
 
				case 1:
 
					return (seed > 12) ? -1 : 27;
 

	
 
				default:
 
					return (seed * 7 >> 8) + 20;
 
			}
 

	
 
		default:
 
			return (seed * 9 >> 8) + 32;
 
	}
 
}
 

	
 
static void PlaceTree(uint tile, uint32 r, byte m5_or)
 
static void PlaceTree(TileIndex tile, uint32 r, byte m5_or)
 
{
 
	int tree = GetRandomTreeType(tile, (r >> 24));
 
	byte m5;
 

	
 
	if (tree >= 0) {
 
		m5 = (byte)(r >> 16);
 
		if ((m5 & 0x7) == 7) m5--; // there is no growth state 7
 

	
 
		_map5[tile] = m5 & 0x07;	// growth state;
 
		_map5[tile] |=  m5 & 0xC0;	// amount of trees
 

	
 
		_map3_lo[tile] = tree;		// set type of tree
 
@@ -57,33 +57,34 @@ static void PlaceTree(uint tile, uint32 
 
		}
 
		else
 
		{
 
			_map2[tile] = (byte)(r >> 24)&0x1F; // randomize counter and ground
 
		}
 

	
 

	
 
		// make it tree class
 
		SetTileType(tile, MP_TREES);
 
	}
 
}
 

	
 
static void DoPlaceMoreTrees(uint tile)
 
static void DoPlaceMoreTrees(TileIndex tile)
 
{
 
	int i = 1000;
 
	int x,y;
 
	uint cur_tile;
 
	int dist;
 

	
 
	do {
 
		uint32 r = Random();
 
		TileIndex cur_tile;
 

	
 
		x = (r & 0x1F) - 16;
 
		y = ((r>>8) & 0x1F) - 16;
 

	
 
		dist = myabs(x) + myabs(y);
 

	
 
		cur_tile = TILE_MASK(tile + TILE_XY(x,y));
 

	
 
		/* Only on tiles within 13 squares from tile,
 
		    on clear tiles, and NOT on farm-tiles or rocks */
 
		if (dist <= 13 && IsTileType(cur_tile, MP_CLEAR) &&
 
			 (_map5[cur_tile] & 0x1F) != 0x0F && (_map5[cur_tile] & 0x1C) != 8) {
 
			PlaceTree(cur_tile, r, dist <= 6 ? 0xC0 : 0);
 
@@ -94,25 +95,25 @@ static void DoPlaceMoreTrees(uint tile)
 
static void PlaceMoreTrees(void)
 
{
 
	int i = ScaleByMapSize((Random() & 0x1F) + 25);
 
	do {
 
		DoPlaceMoreTrees(TILE_MASK(Random()));
 
	} while (--i);
 
}
 

	
 
void PlaceTreesRandomly(void)
 
{
 
	int i;
 
	uint32 r;
 
	uint tile;
 
	TileIndex tile;
 

	
 
	i = ScaleByMapSize(1000);
 
	do {
 
		r = Random();
 
		tile = TILE_MASK(r);
 
		/* Only on clear tiles, and NOT on farm-tiles or rocks */
 
		if (IsTileType(tile, MP_CLEAR) && (_map5[tile] & 0x1F) != 0x0F && (_map5[tile] & 0x1C) != 8) {
 
			PlaceTree(tile, r, 0);
 
		}
 
	} while (--i);
 

	
 
	/* place extra trees at rainforest area */
 
@@ -358,94 +359,95 @@ static void DrawTile_Trees(TileInfo *ti)
 
	EndSpriteCombine();
 
}
 

	
 

	
 
static uint GetSlopeZ_Trees(TileInfo *ti) {
 
	return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z;
 
}
 

	
 
static uint GetSlopeTileh_Trees(TileInfo *ti) {
 
	return ti->tileh;
 
}
 

	
 
static int32 ClearTile_Trees(uint tile, byte flags) {
 
static int32 ClearTile_Trees(TileIndex tile, byte flags)
 
{
 
	int num;
 

	
 
	if (flags & DC_EXEC && _current_player < MAX_PLAYERS) {
 
		Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority);
 
		if (t != NULL)
 
			ChangeTownRating(t, RATING_TREE_DOWN_STEP, RATING_TREE_MINIMUM);
 
	}
 

	
 
	num = (_map5[tile] >> 6) + 1;
 
	if ( (byte)(_map3_lo[tile]-0x14) <= (0x1A-0x14))
 
		num <<= 2;
 

	
 
	if (flags & DC_EXEC)
 
		DoClearSquare(tile);
 

	
 
	return num * _price.remove_trees;
 
}
 

	
 
static void GetAcceptedCargo_Trees(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Trees(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* not used */
 
}
 

	
 
static void GetTileDesc_Trees(uint tile, TileDesc *td)
 
static void GetTileDesc_Trees(TileIndex tile, TileDesc *td)
 
{
 
	byte b;
 
	StringID str;
 

	
 
	td->owner = GetTileOwner(tile);
 

	
 
	b = _map3_lo[tile];
 
	(str=STR_2810_CACTUS_PLANTS, b==0x1B) ||
 
	(str=STR_280F_RAINFOREST, IS_BYTE_INSIDE(b, 0x14, 0x1A+1)) ||
 
	(str=STR_280E_TREES, true);
 
	td->str = str;
 
}
 

	
 
static void AnimateTile_Trees(uint tile)
 
static void AnimateTile_Trees(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static SoundFx _desert_sounds[] = {
 
	SND_42_LOON_BIRD,
 
	SND_43_LION,
 
	SND_44_MONKEYS,
 
	SND_48_DISTANT_BIRD
 
};
 

	
 
static void TileLoopTreesDesert(uint tile)
 
static void TileLoopTreesDesert(TileIndex tile)
 
{
 
	byte b;
 

	
 
	if ((b=GetMapExtraBits(tile)) == 2) {
 
		uint32 r;
 

	
 
		if (CHANCE16I(1,200,r=Random())) {
 
			SndPlayTileFx(_desert_sounds[(r >> 16) & 3], tile);
 
		}
 
	} else if (b == 1) {
 
		if ((_map2[tile] & 0x30) != 0x20) {
 
			_map2[tile] &= 0xF;
 
			_map2[tile] |= 0xE0;
 
			MarkTileDirtyByTile(tile);
 
		}
 
	}
 
}
 

	
 
static void TileLoopTreesAlps(uint tile)
 
static void TileLoopTreesAlps(TileIndex tile)
 
{
 
	byte tmp, m2;
 
	int k;
 

	
 
	/* distance from snow line, in steps of 8 */
 
	k = GetTileZ(tile) - _opt.snow_line;
 

	
 
	tmp = _map5[tile] & 0xF0;
 

	
 
	if (k < -8) {
 
		/* snow_m2_down */
 
		if ((tmp&0x30) != 0x20)
 
@@ -475,25 +477,25 @@ static void TileLoopTreesAlps(uint tile)
 
			}
 
			return;
 
		} else {
 
			m2 = 0xE0;
 
		}
 
	}
 

	
 
	_map2[tile] &= 0xF;
 
	_map2[tile] |= m2;
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
static void TileLoop_Trees(uint tile)
 
static void TileLoop_Trees(TileIndex tile)
 
{
 
	byte m5;
 
	uint16 m2;
 

	
 
	static const TileIndexDiffC _tileloop_trees_dir[] = {
 
		{-1, -1},
 
		{ 0, -1},
 
		{ 1, -1},
 
		{-1,  0},
 
		{ 1,  0},
 
		{-1,  1},
 
		{ 0,  1},
 
@@ -585,25 +587,25 @@ static void TileLoop_Trees(uint tile)
 
	} else {
 
		/* in the middle of a transition, change to next */
 
		m5++;
 
	}
 

	
 
	_map5[tile] = m5;
 
	MarkTileDirtyByTile(tile);
 
}
 

	
 
void OnTick_Trees(void)
 
{
 
	uint32 r;
 
	uint tile;
 
	TileIndex tile;
 
	byte m;
 
	int tree;
 

	
 
	/* place a tree at a random rainforest spot */
 
	if (_opt.landscape == LT_DESERT &&
 
			(r=Random(),tile=TILE_MASK(r),GetMapExtraBits(tile)==2) &&
 
			IsTileType(tile, MP_CLEAR) &&
 
			(m=_map5[tile]&0x1C, m<=4) &&
 
			(tree=GetRandomTreeType(tile, r>>24)) >= 0) {
 

	
 
		ModifyTile(tile,
 
			MP_SETTYPE(MP_TREES) |
 
@@ -637,35 +639,35 @@ void OnTick_Trees(void)
 

	
 
		ModifyTile(tile,
 
			MP_SETTYPE(MP_TREES) |
 
			MP_MAP2 | MP_MAP3LO | MP_MAP3HI | MP_MAP5,
 
			m2,
 
			tree,
 
			_map3_hi[tile] & ~3,
 
			0
 
		);
 
	}
 
}
 

	
 
static void ClickTile_Trees(uint tile)
 
static void ClickTile_Trees(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static uint32 GetTileTrackStatus_Trees(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_Trees(TileIndex tile, TransportType mode)
 
{
 
	return 0;
 
}
 

	
 
static void ChangeTileOwner_Trees(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Trees(TileIndex tile, byte old_player, byte new_player)
 
{
 
	/* not used */
 
}
 

	
 
void InitializeTrees(void)
 
{
 
	_trees_tick_ctr = 0;
 
}
 

	
 

	
 
const TileTypeProcs _tile_type_trees_procs = {
 
	DrawTile_Trees,						/* draw_tile_proc */
tunnelbridge_cmd.c
Show inline comments
 
@@ -2,25 +2,25 @@
 
#include "openttd.h"
 
#include "table/sprites.h"
 
#include "table/strings.h"
 
#include "map.h"
 
#include "tile.h"
 
#include "vehicle.h"
 
#include "viewport.h"
 
#include "command.h"
 
#include "player.h"
 
#include "town.h"
 
#include "sound.h"
 

	
 
extern void DrawCanalWater(uint tile);
 
extern void DrawCanalWater(TileIndex tile);
 

	
 
static const byte _bridge_available_year[MAX_BRIDGES] = {
 
	0, 0, 10, 0, 10, 10, 10, 10, 10, 10, 75, 85, 90
 
};
 

	
 
static const byte _bridge_minlen[MAX_BRIDGES] = {
 
	0, 0, 0, 2, 3, 3, 3, 3, 3, 0, 2, 2, 2
 
};
 

	
 
static const byte _bridge_maxlen[MAX_BRIDGES] = {
 
	16, 2, 5, 10, 16, 16, 7, 8, 9, 2, 16, 32, 32,
 
};
 
@@ -391,45 +391,43 @@ not_valid_below:;
 
	if (!(flags & DC_QUERY_COST)) {
 
		bridge_len += 2;	// begin and end tiles/ramps
 

	
 
		if (_current_player < MAX_PLAYERS && !(_is_ai_player && !_patches.ainew_active))
 
			bridge_len = CalcBridgeLenCostFactor(bridge_len);
 

	
 
		cost += ((int64)bridge_len * _price.build_bridge * _bridge_type_price_mod[bridge_type]) >> 8;
 
	}
 

	
 
	return cost;
 
}
 

	
 
static bool DoCheckTunnelInWay(uint tile, uint z, uint dir)
 
static bool DoCheckTunnelInWay(TileIndex tile, uint z, uint dir)
 
{
 
	TileIndexDiff delta = TileOffsByDir(dir);
 
	TileInfo ti;
 
	int delta;
 

	
 
	delta = TileOffsByDir(dir);
 

	
 
	do {
 
		tile -= delta;
 
		FindLandscapeHeightByTile(&ti, tile);
 
	} while (z < ti.z);
 

	
 
	if (z == ti.z && ti.type == MP_TUNNELBRIDGE && (ti.map5&0xF0) == 0 && (ti.map5&3) == dir) {
 
		_error_message = STR_5003_ANOTHER_TUNNEL_IN_THE_WAY;
 
		return false;
 
	}
 

	
 
	return true;
 
}
 

	
 
bool CheckTunnelInWay(uint tile, int z)
 
bool CheckTunnelInWay(TileIndex tile, int z)
 
{
 
	return DoCheckTunnelInWay(tile,z,0) &&
 
		DoCheckTunnelInWay(tile,z,1) &&
 
		DoCheckTunnelInWay(tile,z,2) &&
 
		DoCheckTunnelInWay(tile,z,3);
 
}
 

	
 
static byte _build_tunnel_bh;
 
static byte _build_tunnel_railtype;
 

	
 
static int32 DoBuildTunnel(int x, int y, int x2, int y2, uint32 flags, uint exc_tile)
 
{
 
@@ -584,49 +582,49 @@ int32 CmdBuildTunnel(int x, int y, uint3
 
		excavated_tile = 1;
 
	}
 

	
 
	return DoBuildTunnel(x, y, tiorg.x, tiorg.y, flags, excavated_tile);
 
}
 

	
 
TileIndex CheckTunnelBusy(TileIndex tile, uint *length)
 
{
 
	uint z = GetTileZ(tile);
 
	byte m5 = _map5[tile];
 
	int delta = TileOffsByDir(m5 & 3);
 
	uint len = 0;
 
	uint starttile = tile;
 
	TileIndex starttile = tile;
 
	Vehicle *v;
 

	
 
	do {
 
		tile += delta;
 
		len++;
 
	} while (
 
		!IsTileType(tile, MP_TUNNELBRIDGE) ||
 
		(_map5[tile] & 0xF0) != 0 ||
 
		(byte)(_map5[tile] ^ 2) != m5 ||
 
		GetTileZ(tile) != z
 
	);
 

	
 
	v = FindVehicleBetween(starttile, tile, z);
 
	if (v != NULL) {
 
		_error_message = v->type == VEH_Train ?
 
			STR_5000_TRAIN_IN_TUNNEL : STR_5001_ROAD_VEHICLE_IN_TUNNEL;
 
		return INVALID_TILE;
 
	}
 

	
 
	if (length != NULL) *length = len;
 
	return tile;
 
}
 

	
 
static int32 DoClearTunnel(uint tile, uint32 flags)
 
static int32 DoClearTunnel(TileIndex tile, uint32 flags)
 
{
 
	Town *t;
 
	TileIndex endtile;
 
	uint length;
 
	static const byte _updsignals_tunnel_dir[4] = { 5, 7, 1, 3};
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	// in scenario editor you can always destroy tunnels
 
	if (_game_mode != GM_EDITOR && !CheckTileOwnership(tile)) {
 
		if (!(_patches.extra_dynamite || _cheats.magic_bulldozer.value) || !IsTileOwner(tile, OWNER_TOWN))
 
			return CMD_ERROR;
 
@@ -653,53 +651,53 @@ static int32 DoClearTunnel(uint tile, ui
 
		byte tile_dir = _map5[tile]&3;
 
		byte endtile_dir = _map5[endtile]&3;
 
		DoClearSquare(tile);
 
		DoClearSquare(endtile);
 
		UpdateSignalsOnSegment(tile, _updsignals_tunnel_dir[tile_dir]);
 
		UpdateSignalsOnSegment(endtile, _updsignals_tunnel_dir[endtile_dir]);
 
		if (IsTileOwner(tile, OWNER_TOWN) && _game_mode != GM_EDITOR)
 
			ChangeTownRating(t, RATING_TUNNEL_BRIDGE_DOWN_STEP, RATING_TUNNEL_BRIDGE_MINIMUM);
 
	}
 
	return  _price.clear_tunnel * (length + 1);
 
}
 

	
 
static uint FindEdgesOfBridge(uint tile, uint *endtile)
 
static TileIndex FindEdgesOfBridge(TileIndex tile, TileIndex *endtile)
 
{
 
	int direction = _map5[tile] & 1;
 
	uint start;
 
	TileIndex start;
 

	
 
	// find start of bridge
 
	for(;;) {
 
		if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0x80)
 
			break;
 
		tile += direction ? TILE_XY(0,-1) : TILE_XY(-1,0);
 
	}
 

	
 
	start = tile;
 

	
 
	// find end of bridge
 
	for(;;) {
 
		if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile] & 0xE0) == 0xA0)
 
			break;
 
		tile += direction ? TILE_XY(0,1) : TILE_XY(1,0);
 
	}
 

	
 
	*endtile = tile;
 

	
 
	return start;
 
}
 

	
 
static int32 DoClearBridge(uint tile, uint32 flags)
 
static int32 DoClearBridge(TileIndex tile, uint32 flags)
 
{
 
	uint endtile;
 
	TileIndex endtile;
 
	Vehicle *v;
 
	Town *t;
 
	int direction;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	direction = _map5[tile]&1;
 

	
 
	/* delete stuff under the middle part if there's a transport route there..? */
 
	if ((_map5[tile] & 0xE0) == 0xE0) {
 
		int32 cost;
 

	
 
@@ -802,43 +800,44 @@ clear_it:;
 
			}
 
			c += direction ? TILE_XY(0,1) : TILE_XY(1,0);
 
		} while (c <= endtile);
 

	
 
		SetSignalsOnBothDir(tile, direction);
 
		SetSignalsOnBothDir(endtile, direction);
 

	
 
	}
 

	
 
	return ((((endtile - tile) >> (direction?8:0))&0xFF)+1) * _price.clear_bridge;
 
}
 

	
 
static int32 ClearTile_TunnelBridge(uint tile, byte flags) {
 
static int32 ClearTile_TunnelBridge(TileIndex tile, byte flags)
 
{
 
	byte m5 = _map5[tile];
 

	
 
	if ((m5 & 0xF0) == 0) {
 
		if (flags & DC_AUTO)
 
			return_cmd_error(STR_5006_MUST_DEMOLISH_TUNNEL_FIRST);
 

	
 
		return DoClearTunnel(tile, flags);
 
	} else if (m5 & 0x80) {
 
		if (flags & DC_AUTO)
 
			return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
		return DoClearBridge(tile, flags);
 
	}
 

	
 
	return CMD_ERROR;
 
}
 

	
 
int32 DoConvertTunnelBridgeRail(uint tile, uint totype, bool exec)
 
int32 DoConvertTunnelBridgeRail(TileIndex tile, uint totype, bool exec)
 
{
 
	TileIndex endtile;
 
	uint length;
 
	Vehicle *v;
 

	
 
	if ((_map5[tile] & 0xFC) == 0x00) {
 
		// railway tunnel
 
		if (!CheckTileOwnership(tile)) return CMD_ERROR;
 

	
 
		if ( (uint)(_map3_lo[tile] & 0xF) == totype) return CMD_ERROR;
 

	
 
		endtile = CheckTunnelBusy(tile, &length);
 
@@ -1220,25 +1219,25 @@ static uint GetSlopeZ_TunnelBridge(TileI
 

	
 
	// default case
 
	z = ti->z;
 
	return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + z;
 
}
 

	
 
static uint GetSlopeTileh_TunnelBridge(TileInfo *ti) {
 
	// not accurate, but good enough for slope graphics drawing
 
	return 0;
 
}
 

	
 

	
 
static void GetAcceptedCargo_TunnelBridge(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_TunnelBridge(TileIndex tile, AcceptedCargo ac)
 
{
 
	/* not used */
 
}
 

	
 
static const StringID _bridge_tile_str[(MAX_BRIDGES + 3) + (MAX_BRIDGES + 3)] = {
 
	STR_501F_WOODEN_RAIL_BRIDGE,
 
	STR_5020_CONCRETE_RAIL_BRIDGE,
 
	STR_501C_STEEL_GIRDER_RAIL_BRIDGE,
 
	STR_501E_REINFORCED_CONCRETE_SUSPENSION,
 
	STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,
 
	STR_501B_STEEL_SUSPENSION_RAIL_BRIDGE,
 
	STR_501D_STEEL_CANTILEVER_RAIL_BRIDGE,
 
@@ -1257,80 +1256,80 @@ static const StringID _bridge_tile_str[(
 
	STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE,
 
	STR_5021_STEEL_SUSPENSION_ROAD_BRIDGE,
 
	STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
 
	STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
 
	STR_5023_STEEL_CANTILEVER_ROAD_BRIDGE,
 
	STR_5022_STEEL_GIRDER_ROAD_BRIDGE,
 
	STR_5028_TUBULAR_ROAD_BRIDGE,
 
	STR_5028_TUBULAR_ROAD_BRIDGE,
 
	STR_5028_TUBULAR_ROAD_BRIDGE,
 
	0,0,0,
 
};
 

	
 
static void GetTileDesc_TunnelBridge(uint tile, TileDesc *td)
 
static void GetTileDesc_TunnelBridge(TileIndex tile, TileDesc *td)
 
{
 
	int delta;
 

	
 
	if ((_map5[tile] & 0x80) == 0) {
 
		td->str = STR_5017_RAILROAD_TUNNEL + ((_map5[tile] >> 2) & 3);
 
	} else {
 
		td->str = _bridge_tile_str[ (_map2[tile] >> 4) + (((_map5[tile]>>1)&3)<<4) ];
 

	
 
		/* scan to the end of the bridge, that's where the owner is stored */
 
		if (_map5[tile] & 0x40) {
 
			delta = _map5[tile] & 1 ? TILE_XY(0,-1) : TILE_XY(-1,0);
 
			do tile += delta; while (_map5[tile] & 0x40);
 
		}
 
	}
 
	td->owner = GetTileOwner(tile);
 
}
 

	
 

	
 
static void AnimateTile_TunnelBridge(uint tile)
 
static void AnimateTile_TunnelBridge(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void TileLoop_TunnelBridge(uint tile)
 
static void TileLoop_TunnelBridge(TileIndex tile)
 
{
 
	if (_opt.landscape == LT_HILLY) {
 
		if ( GetTileZ(tile) > _opt.snow_line) {
 
			if (!(_map3_hi[tile] & 0x80)) {
 
				_map3_hi[tile] |= 0x80;
 
				MarkTileDirtyByTile(tile);
 
			}
 
		} else {
 
			if (_map3_hi[tile] & 0x80) {
 
				_map3_hi[tile] &= ~0x80;
 
				MarkTileDirtyByTile(tile);
 
			}
 
		}
 
	} else if (_opt.landscape == LT_DESERT) {
 
		if (GetMapExtraBits(tile) == 1 && !(_map3_hi[tile]&0x80)) {
 
			_map3_hi[tile] |= 0x80;
 
			MarkTileDirtyByTile(tile);
 
		}
 
	}
 

	
 
	// if it's a bridge with water below, call tileloop_water on it.
 
	if ((_map5[tile] & 0xF8) == 0xC8) TileLoop_Water(tile);
 
}
 

	
 
static void ClickTile_TunnelBridge(uint tile)
 
static void ClickTile_TunnelBridge(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 

	
 
static uint32 GetTileTrackStatus_TunnelBridge(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_TunnelBridge(TileIndex tile, TransportType mode)
 
{
 
	uint32 result;
 
	byte m5 = _map5[tile];
 

	
 
	if ((m5 & 0xF0) == 0) {
 
		/* This is a tunnel */
 
		if (((m5 & 0xCU) >> 2) == mode) {
 
			/* Tranport in the tunnel is compatible */
 
			return m5&1 ? 0x202 : 0x101;
 
		}
 
	} else if (m5 & 0x80) {
 
		/* This is a bridge */
 
@@ -1359,25 +1358,25 @@ static uint32 GetTileTrackStatus_TunnelB
 
			 * transport or water beneath, so we can add it to
 
			 * result */
 
			/* Why is this xor'd ? Can't it just be or'd? */
 
			result ^= m5&1 ? 0x101 : 0x202;
 
		}
 
		return result;
 
	} else {
 
		assert(0); /* This should never occur */
 
	}
 
	return 0;
 
}
 

	
 
static void ChangeTileOwner_TunnelBridge(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_TunnelBridge(TileIndex tile, byte old_player, byte new_player)
 
{
 
	if (!IsTileOwner(tile, old_player)) return;
 

	
 
	if (new_player != 255) {
 
		SetTileOwner(tile, new_player);
 
	}	else {
 
		if((_map5[tile] & 0xC0)==0xC0) {
 
			// the stuff BELOW the middle part is owned by the deleted player.
 
			if (!(_map5[tile] & (1 << 4 | 1 << 3))) {
 
				// convert railway into grass.
 
				_map5[tile] &= ~(1 << 5 | 1 << 4 | 1 << 3); // no transport route under bridge anymore..
 
			} else {
 
@@ -1395,25 +1394,25 @@ static const byte _tunnel_fractcoord_1[4
 
static const byte _tunnel_fractcoord_2[4] = {0x81,0x98,0x87,0x38};
 
static const byte _tunnel_fractcoord_3[4] = {0x82,0x88,0x86,0x48};
 
static const byte _exit_tunnel_track[4] = {1,2,1,2};
 

	
 
static const byte _road_exit_tunnel_state[4] = {8, 9, 0, 1};
 
static const byte _road_exit_tunnel_frame[4] = {2, 7, 9, 4};
 

	
 
static const byte _tunnel_fractcoord_4[4] = {0x52, 0x85, 0x98, 0x29};
 
static const byte _tunnel_fractcoord_5[4] = {0x92, 0x89, 0x58, 0x25};
 
static const byte _tunnel_fractcoord_6[4] = {0x92, 0x89, 0x56, 0x45};
 
static const byte _tunnel_fractcoord_7[4] = {0x52, 0x85, 0x96, 0x49};
 

	
 
static uint32 VehicleEnter_TunnelBridge(Vehicle *v, uint tile, int x, int y)
 
static uint32 VehicleEnter_TunnelBridge(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	int z;
 
	int dir, vdir;
 
	byte fc;
 

	
 
	if ((_map5[tile] & 0xF0) == 0) {
 
		z = GetSlopeZ(x, y) - v->z_pos;
 
		if (myabs(z) > 2)
 
			return 8;
 

	
 
		if (v->type == VEH_Train) {
 
			fc = (x&0xF)+(y<<4);
unmovable_cmd.c
Show inline comments
 
@@ -49,25 +49,25 @@ int32 DestroyCompanyHQ(TileIndex tile, u
 
			InvalidateWindow(WC_COMPANY, (int)p->index);
 
		}
 

	
 
	// cost of relocating company is 1% of company value
 
		return CalculateCompanyValue(p) / 100;
 
}
 

	
 
/** Build or relocate the HQ. This depends if the HQ is already built or not
 
 * @param x,y the coordinates where the HQ will be built or relocated to
 
 * @param p1 relocate HQ (set to some value, usually 1 or true)
 
 * @param p2 unused
 
 */
 
 extern int32 CheckFlatLandBelow(uint tile, uint w, uint h, uint flags, uint invalid_dirs, int *);
 
extern int32 CheckFlatLandBelow(TileIndex tile, uint w, uint h, uint flags, uint invalid_dirs, int *);
 
int32 CmdBuildCompanyHQ(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	TileIndex tile = TILE_FROM_XY(x,y);
 
	Player *p = GetPlayer(_current_player);
 
	int cost;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_PROPERTY);
 

	
 
	cost = CheckFlatLandBelow(tile, 2, 2, flags, 0, NULL);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 

	
 
	if (p1) { /* Moving HQ */
 
@@ -184,48 +184,48 @@ static void DrawTile_Unmovable(TileInfo 
 
}
 

	
 
static uint GetSlopeZ_Unmovable(TileInfo *ti)
 
{
 
	return GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z;
 
}
 

	
 
static uint GetSlopeTileh_Unmovable(TileInfo *ti)
 
{
 
	return 0;
 
}
 

	
 
static int32 ClearTile_Unmovable(uint tile, byte flags)
 
static int32 ClearTile_Unmovable(TileIndex tile, byte flags)
 
{
 
	byte m5 = _map5[tile];
 

	
 
	if (m5 & 0x80) {
 
		if (_current_player == OWNER_WATER) return DestroyCompanyHQ(tile, DC_EXEC);
 
		return_cmd_error(STR_5804_COMPANY_HEADQUARTERS_IN);
 
	}
 

	
 
	if (m5 == 3)	// company owned land
 
		return DoCommandByTile(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
 

	
 
	// checks if you're allowed to remove unmovable things
 
	if (_game_mode != GM_EDITOR && _current_player != OWNER_WATER && ((flags & DC_AUTO || !_cheats.magic_bulldozer.value)) )
 
		return_cmd_error(STR_5800_OBJECT_IN_THE_WAY);
 

	
 
	if (flags & DC_EXEC) {
 
		DoClearSquare(tile);
 
	}
 

	
 
	return 0;
 
}
 

	
 
static void GetAcceptedCargo_Unmovable(uint tile, AcceptedCargo ac)
 
static void GetAcceptedCargo_Unmovable(TileIndex tile, AcceptedCargo ac)
 
{
 
	byte m5 = _map5[tile];
 
	uint level; // HQ level (depends on company performance) in the range 1..5.
 

	
 
	if (!(m5 & 0x80)) {
 
		/* not used */
 
		return;
 
	}
 

	
 
	/* HQ accepts passenger and mail; but we have to divide the values
 
	 * between 4 tiles it occupies! */
 

	
 
@@ -241,38 +241,38 @@ static void GetAcceptedCargo_Unmovable(u
 
	// correspondence per physical visitor.
 
	ac[CT_MAIL] = max(1, level / 2);
 
}
 

	
 
static const StringID _unmovable_tile_str[] = {
 
	STR_5803_COMPANY_HEADQUARTERS,
 
	STR_5801_TRANSMITTER,
 
	STR_5802_LIGHTHOUSE,
 
	STR_2016_STATUE,
 
	STR_5805_COMPANY_OWNED_LAND,
 
};
 

	
 
static void GetTileDesc_Unmovable(uint tile, TileDesc *td)
 
static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
 
{
 
	int i = _map5[tile];
 
	if (i & 0x80) i = -1;
 
	td->str = _unmovable_tile_str[i + 1];
 
	td->owner = GetTileOwner(tile);
 
}
 

	
 
static void AnimateTile_Unmovable(uint tile)
 
static void AnimateTile_Unmovable(TileIndex tile)
 
{
 
	/* not used */
 
}
 

	
 
static void TileLoop_Unmovable(uint tile)
 
static void TileLoop_Unmovable(TileIndex tile)
 
{
 
	byte m5 = _map5[tile];
 
	byte level; // HQ level (depends on company performance) in the range 1..5.
 
	uint32 r;
 

	
 
	if (!(m5 & 0x80)) {
 
		/* not used */
 
		return;
 
	}
 

	
 
	/* HQ accepts passenger and mail; but we have to divide the values
 
	 * between 4 tiles it occupies! */
 
@@ -291,62 +291,62 @@ static void TileLoop_Unmovable(uint tile
 
	r >>= 8;
 
	// Top town building generates 90, HQ can make up to 196. The
 
	// proportion passengers:mail is about the same as in the acceptance
 
	// equations.
 
	if ((byte) r < (196 / 4 / (6 - level))) {
 
		uint amt = ((byte) r >> 3) / 4 + 1;
 
		if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
 
		MoveGoodsToStation(tile, 2, 2, CT_MAIL, amt);
 
	}
 
}
 

	
 

	
 
static uint32 GetTileTrackStatus_Unmovable(uint tile, TransportType mode)
 
static uint32 GetTileTrackStatus_Unmovable(TileIndex tile, TransportType mode)
 
{
 
	return 0;
 
}
 

	
 
static void ClickTile_Unmovable(uint tile)
 
static void ClickTile_Unmovable(TileIndex tile)
 
{
 
	if (_map5[tile] & 0x80) {
 
		ShowPlayerCompany(GetTileOwner(tile));
 
	}
 
}
 

	
 
static const TileIndexDiffC _tile_add[] = {
 
	{ 1,  0},
 
	{ 0,  1},
 
	{-1,  0},
 
	{ 0, -1}
 
};
 

	
 
/* checks, if a radio tower is within a 9x9 tile square around tile */
 
static bool checkRadioTowerNearby(uint tile)
 
static bool checkRadioTowerNearby(TileIndex tile)
 
{
 
	uint tile_s;
 
	TileIndex tile_s;
 

	
 
	tile_s = TILE_XY(TileX(tile) - 4, TileY(tile) - 4);
 

	
 
	BEGIN_TILE_LOOP(tile, 9, 9, tile_s)
 
		// already a radio tower here?
 
		if (IsTileType(tile, MP_UNMOVABLE) && _map5[tile] == 0)
 
			return false;
 
	END_TILE_LOOP(tile, 9, 9, tile_s)
 
	return true;
 
}
 

	
 
void GenerateUnmovables(void)
 
{
 
	int i,j;
 
	uint tile;
 
	TileIndex tile;
 
	uint32 r;
 
	int dir;
 
	uint h;
 

	
 
	if (_opt.landscape == LT_CANDY)
 
		return;
 

	
 
	/* add radio tower */
 
	i = ScaleByMapSize(1000);
 
	j = ScaleByMapSize(40); // maximum number of radio towers on the map
 
	do {
 
		r = Random();
 
@@ -385,25 +385,25 @@ restart:
 
				goto restart;
 
			tile = TILE_MASK(tile + ToTileIndexDiff(_tile_add[dir]));
 
		} while (!(IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == 0 && h <= 16));
 

	
 
		assert(tile == TILE_MASK(tile));
 

	
 
		SetTileType(tile, MP_UNMOVABLE);
 
		_map5[tile] = 1;
 
		SetTileOwner(tile, OWNER_NONE);
 
	} while (--i);
 
}
 

	
 
static void ChangeTileOwner_Unmovable(uint tile, byte old_player, byte new_player)
 
static void ChangeTileOwner_Unmovable(TileIndex tile, byte old_player, byte new_player)
 
{
 
	if (!IsTileOwner(tile, old_player)) return;
 

	
 
	if (_map5[tile]==3 && new_player != 255) {
 
		SetTileOwner(tile, new_player);
 
	}	else {
 
		DoClearSquare(tile);
 
	}
 
}
 

	
 
const TileTypeProcs _tile_type_unmovable_procs = {
 
	DrawTile_Unmovable,             /* draw_tile_proc */
variables.h
Show inline comments
 
@@ -304,25 +304,25 @@ VARDEF byte _dirkeys;				// 1=left, 2=up
 

	
 
VARDEF bool _fullscreen;
 
VARDEF bool _double_size;
 
VARDEF uint _display_hz;
 
VARDEF bool _force_full_redraw;
 
VARDEF uint _fullscreen_bpp;
 
VARDEF bool _fast_forward;
 
VARDEF bool _rightclick_emulate;
 

	
 
// IN/OUT parameters to commands
 
VARDEF byte _yearly_expenses_type;
 
VARDEF TileIndex _terraform_err_tile;
 
VARDEF uint _build_tunnel_endtile;
 
VARDEF TileIndex _build_tunnel_endtile;
 
VARDEF bool _generating_world;
 
VARDEF int _new_town_size;
 
VARDEF uint _returned_refit_amount;
 

	
 
// Deals with the type of the savegame, independent of extension
 
typedef struct {
 
	int mode;						// savegame/scenario type (old, new)
 
	char name[MAX_PATH];	// name
 
} SmallFiosItem;
 

	
 
// Used when switching from the intro menu.
 
VARDEF byte _switch_mode;
vehicle.c
Show inline comments
 
@@ -1766,27 +1766,27 @@ Trackdir GetVehicleTrackdir(const Vehicl
 
				return DiagdirToDiagTrackdir(GetRoadStationDir(v->tile)); /* Road vehicle in a station */
 

	
 
			return DiagdirToDiagTrackdir((v->direction >> 1) & 3);
 

	
 
		/* case VEH_Aircraft: case VEH_Special: case VEH_Disaster: */
 
		default: return 0xFF;
 
	}
 
}
 
/* Return value has bit 0x2 set, when the vehicle enters a station. Then,
 
 * result << 8 contains the id of the station entered. If the return value has
 
 * bit 0x8 set, the vehicle could not and did not enter the tile. Are there
 
 * other bits that can be set? */
 
uint32 VehicleEnterTile(Vehicle *v, uint tile, int x, int y)
 
uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
 
{
 
	uint old_tile = v->tile;
 
	TileIndex old_tile = v->tile;
 
	uint32 result = _tile_type_procs[GetTileType(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
 

	
 
	/* When vehicle_enter_tile_proc returns 8, that apparently means that
 
	 * we cannot enter the tile at all. In that case, don't call
 
	 * leave_tile. */
 
	if (!(result & 8) && old_tile != tile) {
 
		VehicleLeaveTileProc *proc = _tile_type_procs[GetTileType(old_tile)]->vehicle_leave_tile_proc;
 
		if (proc != NULL)
 
			proc(v, old_tile, x, y);
 
	}
 
	return result;
 
}
vehicle.h
Show inline comments
 
@@ -266,46 +266,46 @@ void DeleteVehicle(Vehicle *v);
 
void DeleteVehicleChain(Vehicle *v);
 
void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc);
 
void CallVehicleTicks(void);
 
Vehicle *FindVehicleOnTileZ(TileIndex tile, byte z);
 

	
 
void InitializeTrains(void);
 

	
 
bool CanFillVehicle(Vehicle *v);
 
bool CanRefitTo(const Vehicle *v, CargoID cid_to);
 

	
 
void ViewportAddVehicles(DrawPixelInfo *dpi);
 

	
 
void TrainEnterDepot(Vehicle *v, uint tile);
 
void TrainEnterDepot(Vehicle *v, TileIndex tile);
 

	
 
void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building) ;
 

	
 
/* train_cmd.h */
 
int GetTrainImage(const Vehicle *v, byte direction);
 
int GetAircraftImage(const Vehicle *v, byte direction);
 
int GetRoadVehImage(const Vehicle *v, byte direction);
 
int GetShipImage(const Vehicle *v, byte direction);
 

	
 
Vehicle *CreateEffectVehicle(int x, int y, int z, EffectVehicle type);
 
Vehicle *CreateEffectVehicleAbove(int x, int y, int z, EffectVehicle type);
 
Vehicle *CreateEffectVehicleRel(const Vehicle *v, int x, int y, int z, EffectVehicle type);
 

	
 
uint32 VehicleEnterTile(Vehicle *v, uint tile, int x, int y);
 
uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y);
 

	
 
void VehicleInTheWayErrMsg(Vehicle *v);
 
Vehicle *FindVehicleBetween(TileIndex from, TileIndex to, byte z);
 
TileIndex GetVehicleOutOfTunnelTile(const Vehicle *v);
 

	
 
bool UpdateSignalsOnSegment(uint tile, byte direction);
 
void SetSignalsOnBothDir(uint tile, byte track);
 
bool UpdateSignalsOnSegment(TileIndex tile, byte direction);
 
void SetSignalsOnBothDir(TileIndex tile, byte track);
 

	
 
Vehicle *CheckClickOnVehicle(ViewPort *vp, int x, int y);
 
//uint GetVehicleWeight(Vehicle *v);
 

	
 
void DecreaseVehicleValue(Vehicle *v);
 
void CheckVehicleBreakdown(Vehicle *v);
 
void AgeVehicle(Vehicle *v);
 
void MaybeReplaceVehicle(Vehicle *v);
 

	
 
void BeginVehicleMove(Vehicle *v);
 
void EndVehicleMove(Vehicle *v);
 

	
 
@@ -317,26 +317,26 @@ UnitID GetFreeUnitNumber(byte type);
 
int LoadUnloadVehicle(Vehicle *v);
 

	
 
void TrainConsistChanged(Vehicle *v);
 
void UpdateTrainAcceleration(Vehicle *v);
 
int32 GetTrainRunningCost(const Vehicle *v);
 

	
 
int CheckTrainStoppedInDepot(const Vehicle *v);
 

	
 
bool VehicleNeedsService(const Vehicle *v);
 

	
 
typedef struct GetNewVehiclePosResult {
 
	int x,y;
 
	uint old_tile;
 
	uint new_tile;
 
	TileIndex old_tile;
 
	TileIndex new_tile;
 
} GetNewVehiclePosResult;
 

	
 
/**
 
 * Returns the Trackdir on which the vehicle is currently located.
 
 * Works for trains and ships.
 
 * Currently works only sortof for road vehicles, since they have a fuzzy
 
 * concept of being "on" a trackdir. Dunno really what it returns for a road
 
 * vehicle that is halfway a tile, never really understood that part. For road
 
 * vehicles that are at the beginning or end of the tile, should just return
 
 * the diagonal trackdir on which they are driving. I _think_.
 
 * For other vehicles types, or vehicles with no clear trackdir (such as those
 
 * in depots), returns 0xFF.
viewport.c
Show inline comments
 
@@ -540,40 +540,40 @@ void *AddStringToDraw(int x, int y, Stri
 
	ss->width = 0;
 

	
 
	*vd->last_string = ss;
 
	vd->last_string = &ss->next;
 

	
 
	return ss;
 
}
 

	
 
/* Debugging code */
 

	
 
#ifdef DEBUG_TILE_PUSH
 
static int _num_push;
 
static uint _pushed_tile[200];
 
static TileIndex _pushed_tile[200];
 
static int _pushed_track[200];
 

	
 
static uint _stored_tile[200];
 
static TileIndex _stored_tile[200];
 
static int _stored_track[200];
 
static int _num_stored;
 

	
 
void dbg_store_path(void)
 
{
 
	memcpy(_stored_tile, _pushed_tile, sizeof(_stored_tile));
 
	memcpy(_stored_track, _pushed_track, sizeof(_stored_tile));
 
	_num_stored = _num_push;
 
	MarkWholeScreenDirty();
 
}
 

	
 
void dbg_push_tile(uint tile, int track)
 
void dbg_push_tile(TileIndex tile, int track)
 
{
 
	_pushed_tile[_num_push] = tile;
 
	_pushed_track[_num_push++] = track;
 
	dbg_store_path();
 
}
 

	
 
void dbg_pop_tile(void)
 
{
 
	_num_push--;
 
}
 

	
 
static const uint16 _dbg_track_sprite[] = {
 
@@ -675,25 +675,25 @@ static void DrawTileSelection(const Tile
 
			}
 
			DrawGroundSpriteAt(_cur_dpi->zoom != 2 ? SPR_DOT : SPR_DOT_SMALL, ti->x, ti->y, z);
 
		} else if (_thd.drawstyle & HT_RAIL /*&& _thd.place_mode == VHM_RAIL*/) { // autorail highlight piece under cursor
 
			int type = _thd.drawstyle & 0xF;
 
			assert(type<=5);
 
			image = SPR_AUTORAIL_BASE + AutorailTilehSprite[ ti->tileh ][ AutorailType[type][0] ];
 

	
 
			if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED;
 
			DrawSelectionSprite(image, ti);
 

	
 
		} else if (IsPartOfAutoLine(ti->x, ti->y)) { // autorail highlighting long line
 
				int dir = _thd.drawstyle & ~0xF0;
 
				uint start = TILE_FROM_XY(_thd.selstart.x, _thd.selstart.y);
 
				TileIndex start = TILE_FROM_XY(_thd.selstart.x, _thd.selstart.y);
 
				int diffx, diffy;
 
				int side;
 

	
 
				diffx = myabs(TileX(start)-TileX(ti->tile));
 
				diffy = myabs(TileY(start)-TileY(ti->tile));
 

	
 
				side = myabs( diffx-diffy );
 
				if(dir<2) side = 0;
 

	
 
				image = SPR_AUTORAIL_BASE + AutorailTilehSprite[ ti->tileh ][ AutorailType[dir][side] ];
 

	
 
				if (_thd.make_square_red) image |= PALETTE_SEL_TILE_RED;
 
@@ -1689,28 +1689,26 @@ static bool CheckClickOnWaypoint(ViewPor
 
				return true;
 
			}
 
		}
 
	}
 

	
 
	return false;
 
}
 

	
 

	
 
static void CheckClickOnLandscape(ViewPort *vp, int x, int y)
 
{
 
	Point pt = TranslateXYToTileCoord(vp,x,y);
 
	if (pt.x != -1) {
 
		uint tile = TILE_FROM_XY(pt.x, pt.y);
 
		ClickTile(tile);
 
	}
 

	
 
	if (pt.x != -1) ClickTile(TILE_FROM_XY(pt.x, pt.y));
 
}
 

	
 
void HandleClickOnTrain(Vehicle *v);
 
void HandleClickOnRoadVeh(Vehicle *v);
 
void HandleClickOnAircraft(Vehicle *v);
 
void HandleClickOnShip(Vehicle *v);
 
static void HandleClickOnSpecialVeh(Vehicle *v) {}
 
void HandleClickOnDisasterVeh(Vehicle *v);
 
typedef void OnVehicleClickProc(Vehicle *v);
 
static OnVehicleClickProc * const _on_vehicle_click_proc[6] = {
 
	HandleClickOnTrain,
 
	HandleClickOnRoadVeh,
 
@@ -1933,25 +1931,25 @@ void UpdateTileSelection(void)
 
		_thd.drawstyle = _thd.new_drawstyle;
 
		_thd.pos = _thd.new_pos;
 
		_thd.size = _thd.new_size;
 
		_thd.outersize = _thd.new_outersize;
 
		_thd.dirty = 0xff;
 

	
 
		// draw the new selection?
 
		if (_thd.new_drawstyle) SetSelectionTilesDirty();
 
	}
 
}
 

	
 
// highlighting tiles while only going over them with the mouse
 
void VpStartPlaceSizing(uint tile, int user)
 
void VpStartPlaceSizing(TileIndex tile, int user)
 
{
 
	_thd.userdata = user;
 
	_thd.selend.x = TileX(tile) * 16;
 
	_thd.selstart.x = TileX(tile) * 16;
 
	_thd.selend.y = TileY(tile) * 16;
 
	_thd.selstart.y = TileY(tile) * 16;
 
	if (_thd.place_mode == VHM_RECT) {
 
		_thd.place_mode = VHM_SPECIAL;
 
		_thd.next_drawstyle = HT_RECT;
 
	} else if (_thd.place_mode == VHM_RAIL) { // autorail one piece
 
		_thd.place_mode = VHM_SPECIAL;
 
		_thd.next_drawstyle = _thd.drawstyle;
viewport.h
Show inline comments
 
@@ -31,25 +31,25 @@ void *AddStringToDraw(int x, int y, Stri
 
void AddChildSpriteScreen(uint32 image, int x, int y);
 

	
 

	
 
void StartSpriteCombine(void);
 
void EndSpriteCombine(void);
 

	
 
void HandleViewportClicked(ViewPort *vp, int x, int y);
 
void PlaceObject(void);
 
void SetRedErrorSquare(TileIndex tile);
 
void SetTileSelectSize(int w, int h);
 
void SetTileSelectBigSize(int ox, int oy, int sx, int sy);
 

	
 
void VpStartPlaceSizing(uint tile, int user);
 
void VpStartPlaceSizing(TileIndex tile, int user);
 
void VpStartPreSizing(void);
 
void VpSetPresizeRange(uint from, uint to);
 
void VpSetPlaceSizingLimit(int limit);
 

	
 
Vehicle *CheckMouseOverVehicle(void);
 

	
 
enum {
 
	VPM_X_OR_Y = 0,
 
	VPM_FIX_X = 1,
 
	VPM_FIX_Y = 2,
 
	VPM_RAILDIRS = 3,
 
	VPM_X_AND_Y = 4,

Changeset was too big and was cut off... Show full diff anyway

0 comments (0 inline, 0 general)