Changeset - r8964:d804b28173fd
[Not reviewed]
master
0 2 0
smatz - 16 years ago 2008-04-17 20:03:28
smatz@openttd.org
(svn r12756) -Cleanup: variable scope and coding style in station*
2 files changed with 56 insertions and 63 deletions:
0 comments (0 inline, 0 general)
src/station_cmd.cpp
Show inline comments
 
@@ -228,70 +228,70 @@ static bool CMSAForest(TileIndex tile)
 

	
 
#define M(x) ((x) - STR_SV_STNAME)
 

	
 
enum StationNaming {
 
	STATIONNAMING_RAIL = 0,
 
	STATIONNAMING_ROAD = 0,
 
	STATIONNAMING_AIRPORT,
 
	STATIONNAMING_OILRIG,
 
	STATIONNAMING_DOCK,
 
	STATIONNAMING_BUOY,
 
	STATIONNAMING_HELIPORT,
 
};
 

	
 
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;
 
	int found;
 
	unsigned long tmp;
 
	const Town *t = st->town;
 
	uint32 free_names = UINT32_MAX;
 

	
 
	{
 
		Station *s;
 
		const Station *s;
 

	
 
		FOR_ALL_STATIONS(s) {
 
			if (s != st && s->town == t) {
 
				uint str = M(s->string_id);
 
				if (str <= 0x20) {
 
					if (str == M(STR_SV_STNAME_FOREST))
 
					if (str == M(STR_SV_STNAME_FOREST)) {
 
						str = M(STR_SV_STNAME_WOODS);
 
					}
 
					ClrBit(free_names, str);
 
				}
 
			}
 
		}
 
	}
 

	
 
	/* check default names */
 
	tmp = free_names & _gen_station_name_bits[flag];
 
	uint32 tmp = free_names & _gen_station_name_bits[flag];
 
	int found;
 
	if (tmp != 0) {
 
		found = FindFirstBit(tmp);
 
		goto done;
 
	}
 

	
 
	/* check mine? */
 
	if (HasBit(free_names, M(STR_SV_STNAME_MINES))) {
 
		if (CountMapSquareAround(tile, CMSAMine) >= 2) {
 
			found = M(STR_SV_STNAME_MINES);
 
			goto done;
 
		}
 
	}
 

	
 
	/* check close enough to town to get central as name? */
 
	if (DistanceMax(tile, t->xy) < 8) {
 
		found = M(STR_SV_STNAME);
 
		if (HasBit(free_names, M(STR_SV_STNAME))) goto done;
 

	
 
		found = M(STR_SV_STNAME_CENTRAL);
 
		if (HasBit(free_names, M(STR_SV_STNAME_CENTRAL))) goto done;
 
	}
 

	
 
	/* Check lakeside */
 
	if (HasBit(free_names, M(STR_SV_STNAME_LAKESIDE)) &&
 
@@ -543,103 +543,99 @@ static inline void MergePoint(ottd_Recta
 
	if (rect->max_y < y) rect->max_y = y;
 
}
 

	
 
/** Update the acceptance for a station.
 
 * @param st Station to update
 
 * @param show_msg controls whether to display a message that acceptance was changed.
 
 */
 
static void UpdateStationAcceptance(Station *st, bool show_msg)
 
{
 
	/* Don't update acceptance for a buoy */
 
	if (st->IsBuoy()) return;
 

	
 
	ottd_Rectangle rect;
 
	rect.min_x = MapSizeX();
 
	rect.min_y = MapSizeY();
 
	rect.max_x = 0;
 
	rect.max_y = 0;
 

	
 
	/* old accepted goods types */
 
	uint old_acc = GetAcceptanceMask(st);
 

	
 
	/* Put all the tiles that span an area in the table. */
 
	if (st->train_tile != 0) {
 
		MergePoint(&rect, st->train_tile);
 
		MergePoint(&rect,
 
			st->train_tile + TileDiffXY(st->trainst_w - 1, st->trainst_h - 1)
 
		);
 
		MergePoint(&rect, st->train_tile + TileDiffXY(st->trainst_w - 1, st->trainst_h - 1));
 
	}
 

	
 
	if (st->airport_tile != 0) {
 
		const AirportFTAClass* afc = st->Airport();
 

	
 
		MergePoint(&rect, st->airport_tile);
 
		MergePoint(&rect,
 
			st->airport_tile + TileDiffXY(afc->size_x - 1, afc->size_y - 1)
 
		);
 
		MergePoint(&rect, st->airport_tile + TileDiffXY(afc->size_x - 1, afc->size_y - 1));
 
	}
 

	
 
	if (st->dock_tile != 0) MergePoint(&rect, st->dock_tile);
 

	
 
	for (const RoadStop *rs = st->bus_stops; rs != NULL; rs = rs->next) {
 
		MergePoint(&rect, rs->xy);
 
	}
 

	
 
	for (const RoadStop *rs = st->truck_stops; rs != NULL; rs = rs->next) {
 
		MergePoint(&rect, rs->xy);
 
	}
 

	
 
	/* And retrieve the acceptance. */
 
	AcceptedCargo accepts;
 
	if (rect.max_x >= rect.min_x) {
 
		GetAcceptanceAroundTiles(
 
			accepts,
 
			TileXY(rect.min_x, rect.min_y),
 
			rect.max_x - rect.min_x + 1,
 
			rect.max_y - rect.min_y + 1,
 
			_patches.modified_catchment ? FindCatchmentRadius(st) : (uint)CA_UNMODIFIED
 
		);
 
	} else {
 
		memset(accepts, 0, sizeof(accepts));
 
	}
 

	
 
	/* Adjust in case our station only accepts fewer kinds of goods */
 
	for (CargoID i = 0; i < NUM_CARGO; i++) {
 
		uint amt = min(accepts[i], 15);
 

	
 
		/* Make sure the station can accept the goods type. */
 
		bool is_passengers = IsCargoInClass(i, CC_PASSENGERS);
 
		if ((!is_passengers && !(st->facilities & (byte)~FACIL_BUS_STOP)) ||
 
				(is_passengers && !(st->facilities & (byte)~FACIL_TRUCK_STOP)))
 
				(is_passengers && !(st->facilities & (byte)~FACIL_TRUCK_STOP))) {
 
			amt = 0;
 
		}
 

	
 
		SB(st->goods[i].acceptance_pickup, GoodsEntry::ACCEPTANCE, 1, amt >= 8);
 
	}
 

	
 
	/* Only show a message in case the acceptance was actually changed. */
 
	uint new_acc = GetAcceptanceMask(st);
 
	if (old_acc == new_acc)
 
		return;
 
	if (old_acc == new_acc) return;
 

	
 
	/* show a message to report that the acceptance was changed? */
 
	if (show_msg && st->owner == _local_player && st->facilities) {
 
		/* List of accept and reject strings for different number of
 
		 * cargo types */
 
		static const StringID accept_msg[] = {
 
			STR_3040_NOW_ACCEPTS,
 
			STR_3041_NOW_ACCEPTS_AND,
 
		};
 
		static const StringID reject_msg[] = {
 
			STR_303E_NO_LONGER_ACCEPTS,
 
			STR_303F_NO_LONGER_ACCEPTS_OR,
 
		};
 

	
 
		/* Array of accepted and rejected cargo types */
 
		CargoID accepts[2] = { CT_INVALID, CT_INVALID };
 
		CargoID rejects[2] = { CT_INVALID, CT_INVALID };
 
		uint num_acc = 0;
 
		uint num_rej = 0;
 

	
 
		/* Test each cargo type to see if its acceptange has changed */
 
		for (CargoID i = 0; i < NUM_CARGO; i++) {
 
			if (HasBit(new_acc, i)) {
 
				if (!HasBit(old_acc, i) && num_acc < lengthof(accepts)) {
 
@@ -878,81 +874,80 @@ static void GetStationLayout(byte *layou
 
		numtracks >>= 1;
 

	
 
		while (--numtracks >= 0) {
 
			layout = CreateMulti(layout, plat_len, 4);
 
			layout = CreateMulti(layout, plat_len, 6);
 
		}
 
	}
 
}
 

	
 
/** Build railroad station
 
 * @param tile_org starting position of station dragging/placement
 
 * @param flags operation to perform
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0)    - orientation (Axis)
 
 * - p1 = (bit  8-15) - number of tracks
 
 * - p1 = (bit 16-23) - platform length
 
 * - p1 = (bit 24)    - allow stations directly adjacent to other stations.
 
 * @param p2 various bitstuffed elements
 
 * - p2 = (bit  0- 3) - railtype (p2 & 0xF)
 
 * - p2 = (bit  8-15) - custom station class
 
 * - p2 = (bit 16-23) - custom station id
 
 */
 
CommandCost CmdBuildRailroadStation(TileIndex tile_org, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	int w_org, h_org;
 
	CommandCost ret;
 

	
 
	/* Does the authority allow this? */
 
	if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile_org)) return CMD_ERROR;
 
	if (!ValParamRailtype((RailType)(p2 & 0xF))) return CMD_ERROR;
 

	
 
	/* unpack parameters */
 
	Axis axis = Extract<Axis, 0>(p1);
 
	uint numtracks = GB(p1,  8, 8);
 
	uint plat_len  = GB(p1, 16, 8);
 

	
 
	int w_org, h_org;
 
	if (axis == AXIS_X) {
 
		w_org = plat_len;
 
		h_org = numtracks;
 
	} else {
 
		h_org = plat_len;
 
		w_org = numtracks;
 
	}
 

	
 
	if (h_org > _patches.station_spread || w_org > _patches.station_spread) return CMD_ERROR;
 

	
 
	/* these values are those that will be stored in train_tile and station_platforms */
 
	uint finalvalues[3];
 
	finalvalues[0] = tile_org;
 
	finalvalues[1] = w_org;
 
	finalvalues[2] = h_org;
 

	
 
	/* Make sure the area below consists of clear tiles. (OR tiles belonging to a certain rail station) */
 
	StationID est = INVALID_STATION;
 
	/* If DC_EXEC is in flag, do not want to pass it to CheckFlatLandBelow, because of a nice bug
 
	 * for detail info, see:
 
	 * https://sourceforge.net/tracker/index.php?func=detail&aid=1029064&group_id=103924&atid=636365 */
 
	ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags & ~DC_EXEC, 5 << axis, _patches.nonuniform_stations ? &est : NULL);
 
	CommandCost ret = CheckFlatLandBelow(tile_org, w_org, h_org, flags & ~DC_EXEC, 5 << axis, _patches.nonuniform_stations ? &est : NULL);
 
	if (CmdFailed(ret)) return ret;
 
	CommandCost cost(EXPENSES_CONSTRUCTION, ret.GetCost() + (numtracks * _price.train_station_track + _price.train_station_length) * plat_len);
 

	
 
	Station *st = NULL;
 
	bool check_surrounding = true;
 

	
 
	if (_patches.adjacent_stations) {
 
		if (est != INVALID_STATION) {
 
			if (HasBit(p1, 24)) {
 
				/* You can't build an adjacent station over the top of one that
 
				 * already exists. */
 
				return_cmd_error(STR_MUST_REMOVE_RAILWAY_STATION_FIRST);
 
			} else {
 
				/* Extend the current station, and don't check whether it will
 
				 * be near any other stations. */
 
				st = GetStation(est);
 
				check_surrounding = false;
 
			}
 
		} else {
 
			/* There's no station here. Don't check the tiles surrounding this
 
			 * one if the player wanted to build an adjacent station. */
 
			if (HasBit(p1, 24)) check_surrounding = false;
 
		}
 
	}
 
@@ -1218,54 +1213,54 @@ CommandCost CmdRemoveFromRailroadStation
 
			MakeRailwayStationAreaSmaller(st);
 
			st->MarkTilesDirty(false);
 
			UpdateStationSignCoord(st);
 

	
 
			/* if we deleted the whole station, delete the train facility. */
 
			if (st->train_tile == 0) {
 
				st->facilities &= ~FACIL_TRAIN;
 
				InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_TRAINS);
 
				UpdateStationVirtCoordDirty(st);
 
				DeleteStationIfEmpty(st);
 
			}
 
		}
 
	} END_TILE_LOOP(tile2, size_x, size_y, tile)
 

	
 
	/* If we've not removed any tiles, give an error */
 
	if (quantity == 0) return CMD_ERROR;
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_rail_station * quantity);
 
}
 

	
 

	
 
static CommandCost RemoveRailroadStation(Station *st, TileIndex tile, uint32 flags)
 
{
 
	/* if there is flooding and non-uniform stations are enabled, remove platforms tile by tile */
 
	if (_current_player == OWNER_WATER && _patches.nonuniform_stations)
 
	if (_current_player == OWNER_WATER && _patches.nonuniform_stations) {
 
		return DoCommand(tile, 0, 0, DC_EXEC, CMD_REMOVE_FROM_RAILROAD_STATION);
 
	}
 

	
 
	/* Current player owns the station? */
 
	if (_current_player != OWNER_WATER && !CheckOwnership(st->owner))
 
		return CMD_ERROR;
 
	if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) return CMD_ERROR;
 

	
 
	/* determine width and height of platforms */
 
	tile = st->train_tile;
 
	int w = st->trainst_w;
 
	int h = st->trainst_h;
 

	
 
	assert(w != 0 && h != 0);
 

	
 
	CommandCost cost(EXPENSES_CONSTRUCTION);
 
	/* clear all areas of the station */
 
	do {
 
		int w_bak = w;
 
		do {
 
			/* for nonuniform stations, only remove tiles that are actually train station tiles */
 
			if (st->TileBelongsToRailStation(tile)) {
 
				if (!EnsureNoVehicleOnGround(tile))
 
					return CMD_ERROR;
 
				cost.AddCost(_price.remove_rail_station);
 
				if (flags & DC_EXEC) {
 
					/* read variables before the station tile is removed */
 
					Track track = GetRailStationTrack(tile);
 
					Owner owner = GetTileOwner(tile); // _current_player can be OWNER_WATER
 
					DoClearSquare(tile);
 
					AddTrackToSignalBuffer(tile, track, owner);
 
@@ -1327,75 +1322,74 @@ static RoadStop **FindRoadStopSpot(bool 
 
 *           bit 5: allow stations directly adjacent to other stations.
 
 */
 
CommandCost CmdBuildRoadStop(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	bool type = HasBit(p2, 0);
 
	bool is_drive_through = HasBit(p2, 1);
 
	bool build_over_road  = is_drive_through && IsNormalRoadTile(tile);
 
	bool town_owned_road  = build_over_road && IsTileOwner(tile, OWNER_TOWN);
 
	RoadTypes rts = (RoadTypes)GB(p2, 2, 3);
 

	
 
	if (!AreValidRoadTypes(rts) || !HasRoadTypesAvail(_current_player, rts)) return CMD_ERROR;
 

	
 
	/* Trams only have drive through stops */
 
	if (!is_drive_through && HasBit(rts, ROADTYPE_TRAM)) return CMD_ERROR;
 

	
 
	/* Saveguard the parameters */
 
	if (!IsValidDiagDirection((DiagDirection)p1)) return CMD_ERROR;
 
	/* If it is a drive-through stop check for valid axis */
 
	if (is_drive_through && !IsValidAxis((Axis)p1)) return CMD_ERROR;
 
	/* Road bits in the wrong direction */
 
	if (build_over_road && (GetAllRoadBits(tile) & ((Axis)p1 == AXIS_X ? ROAD_Y : ROAD_X)) != 0) return_cmd_error(STR_DRIVE_THROUGH_ERROR_DIRECTION);
 

	
 
	if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) return CMD_ERROR;
 

	
 
	CommandCost cost;
 

	
 
	/* Not allowed to build over this road */
 
	if (build_over_road) {
 
		if (IsTileOwner(tile, OWNER_TOWN) && !_patches.road_stop_on_town_road) return_cmd_error(STR_DRIVE_THROUGH_ERROR_ON_TOWN_ROAD);
 

	
 
		RoadTypes cur_rts = GetRoadTypes(tile);
 

	
 
		/* there is a road, check if we can build road+tram stop over it */
 
		if (HasBit(cur_rts, ROADTYPE_ROAD)) {
 
			Owner road_owner = GetRoadOwner(tile, ROADTYPE_ROAD);
 
			if (road_owner != OWNER_TOWN && road_owner != OWNER_NONE && !CheckOwnership(road_owner)) return CMD_ERROR;
 
		}
 

	
 
		/* there is a tram, check if we can build road+tram stop over it */
 
		if (HasBit(cur_rts, ROADTYPE_TRAM)) {
 
			Owner tram_owner = GetRoadOwner(tile, ROADTYPE_TRAM);
 
			if (tram_owner != OWNER_NONE && !CheckOwnership(tram_owner)) return CMD_ERROR;
 
		}
 

	
 
		/* Don't allow building the roadstop when vehicles are already driving on it */
 
		if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
 

	
 
		/* Do not remove roadtypes! */
 
		rts |= cur_rts;
 
	}
 
	cost = CheckFlatLandBelow(tile, 1, 1, flags, is_drive_through ? 5 << p1 : 1 << p1, NULL, !build_over_road);
 

	
 
	CommandCost cost = CheckFlatLandBelow(tile, 1, 1, flags, is_drive_through ? 5 << p1 : 1 << p1, NULL, !build_over_road);
 
	if (CmdFailed(cost)) return cost;
 

	
 
	Station *st = NULL;
 

	
 
	if (!_patches.adjacent_stations || !HasBit(p2, 5)) {
 
		st = GetStationAround(tile, 1, 1, INVALID_STATION);
 
		if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
 
	}
 

	
 
	/* Find a station close to us */
 
	if (st == NULL) st = GetClosestStationFromTile(tile);
 

	
 
	/* give us a road stop in the list, and check if something went wrong */
 
	RoadStop *road_stop = new RoadStop(tile);
 
	if (road_stop == NULL) {
 
		return_cmd_error(type ? STR_TOO_MANY_TRUCK_STOPS : STR_TOO_MANY_BUS_STOPS);
 
	}
 

	
 
	/* ensure that in case of error (or no DC_EXEC) the new road stop gets deleted upon return */
 
	AutoPtrT<RoadStop> rs_auto_delete(road_stop);
 

	
 
	if (st != NULL &&
 
			GetNumRoadStopsInStation(st, ROADSTOP_BUS) + GetNumRoadStopsInStation(st, ROADSTOP_TRUCK) >= RoadStop::LIMIT) {
 
		return_cmd_error(type ? STR_TOO_MANY_TRUCK_STOPS : STR_TOO_MANY_BUS_STOPS);
 
@@ -1649,101 +1643,103 @@ static const byte * const _airport_secti
 
	_airport_sections_country,           // Country Airfield (small)
 
	_airport_sections_town,              // City Airport (large)
 
	_airport_sections_heliport,          // Heliport
 
	_airport_sections_metropolitan,      // Metropolitain Airport (large)
 
	_airport_sections_international,     // International Airport (xlarge)
 
	_airport_sections_commuter,          // Commuter Airport (small)
 
	_airport_sections_helidepot,         // Helidepot
 
	_airport_sections_intercontinental,  // Intercontinental Airport (xxlarge)
 
	_airport_sections_helistation        // Helistation
 
};
 

	
 
/** Place an Airport.
 
 * @param tile tile where airport will be built
 
 * @param flags operation to perform
 
 * @param p1 airport type, @see airport.h
 
 * @param p2 (bit 0) - allow airports directly adjacent to other airports.
 
 */
 
CommandCost CmdBuildAirport(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	bool airport_upgrade = true;
 

	
 
	/* Check if a valid, buildable airport was chosen for construction */
 
	if (p1 > lengthof(_airport_sections) || !HasBit(GetValidAirports(), p1)) return CMD_ERROR;
 

	
 
	if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile))
 
	if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) {
 
		return CMD_ERROR;
 

	
 
	Town *t = ClosestTownFromTile(tile, (uint)-1);
 
	}
 

	
 
	Town *t = ClosestTownFromTile(tile, UINT_MAX);
 

	
 
	/* Check if local auth refuses a new airport */
 
	{
 
		uint num = 0;
 
		const Station *st;
 
		FOR_ALL_STATIONS(st) {
 
			if (st->town == t && st->facilities&FACIL_AIRPORT && st->airport_type != AT_OILRIG)
 
				num++;
 
			if (st->town == t && st->facilities & FACIL_AIRPORT && st->airport_type != AT_OILRIG) num++;
 
		}
 
		if (num >= 2) {
 
			SetDParam(0, t->index);
 
			return_cmd_error(STR_2035_LOCAL_AUTHORITY_REFUSES);
 
		}
 
	}
 

	
 
	const AirportFTAClass *afc = GetAirport(p1);
 
	int w = afc->size_x;
 
	int h = afc->size_y;
 

	
 
	CommandCost cost = CheckFlatLandBelow(tile, w, h, flags, 0, NULL);
 
	if (CmdFailed(cost)) return cost;
 

	
 
	Station *st = NULL;
 

	
 
	if (!_patches.adjacent_stations || !HasBit(p2, 0)) {
 
		st = GetStationAround(tile, w, h, INVALID_STATION);
 
		if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
 
	}
 

	
 
	/* Find a station close to us */
 
	if (st == NULL) st = GetClosestStationFromTile(tile);
 

	
 
	if (w > _patches.station_spread || h > _patches.station_spread) {
 
		_error_message = STR_306C_STATION_TOO_SPREAD_OUT;
 
		return CMD_ERROR;
 
	}
 

	
 
	/* In case of new station if DC_EXEC is NOT set we still need to create the station
 
	 * to test if everything is OK. In this case we need to delete it before return. */
 
	AutoPtrT<Station> st_auto_delete;
 

	
 
	if (st != NULL) {
 
		if (st->owner != _current_player)
 
		if (st->owner != _current_player) {
 
			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
 
		}
 

	
 
		if (!st->rect.BeforeAddRect(tile, w, h, StationRect::ADD_TEST)) return CMD_ERROR;
 

	
 
		if (st->airport_tile != 0)
 
		if (st->airport_tile != 0) {
 
			return_cmd_error(STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT);
 
		}
 
	} else {
 
		airport_upgrade = false;
 

	
 
		/* allocate and initialize new station */
 
		st = new Station(tile);
 
		if (st == NULL) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING);
 

	
 
		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
 
		st_auto_delete = st;
 

	
 
		st->town = t;
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
 
			SetBit(t->have_ratings, _current_player);
 
		}
 

	
 
		st->sign.width_1 = 0;
 

	
 
		/* If only helicopters may use the airport generate a helicopter related (5)
 
		 * station name, otherwise generate a normal airport name (1) */
 
		if (!GenerateStationName(st, tile, !(afc->flags & AirportFTAClass::AIRPLANES) ? STATIONNAMING_HELIPORT : STATIONNAMING_AIRPORT)) {
 
			return CMD_ERROR;
 
		}
 
	}
 
@@ -1769,60 +1765,61 @@ CommandCost CmdBuildAirport(TileIndex ti
 

	
 
		{
 
			const byte *b = _airport_sections[p1];
 

	
 
			BEGIN_TILE_LOOP(tile_cur, w, h, tile) {
 
				MakeAirport(tile_cur, st->owner, st->index, *b - ((*b < 67) ? 8 : 24));
 
				b++;
 
			} END_TILE_LOOP(tile_cur, w, h, tile)
 
		}
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_PLANES);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.Detach();
 
	}
 

	
 
	return cost;
 
}
 

	
 
static CommandCost RemoveAirport(Station *st, uint32 flags)
 
{
 
	if (_current_player != OWNER_WATER && !CheckOwnership(st->owner))
 
	if (_current_player != OWNER_WATER && !CheckOwnership(st->owner)) {
 
		return CMD_ERROR;
 
	}
 

	
 
	TileIndex tile = st->airport_tile;
 

	
 
	const AirportFTAClass *afc = st->Airport();
 
	int w = afc->size_x;
 
	int h = afc->size_y;
 

	
 
	CommandCost cost(EXPENSES_CONSTRUCTION, w * h * _price.remove_airport);
 

	
 
	Vehicle *v;
 
	const Vehicle *v;
 
	FOR_ALL_VEHICLES(v) {
 
		if (!(v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) continue;
 

	
 
		if (v->u.air.targetairport == st->index && v->u.air.state != FLYING) return CMD_ERROR;
 
	}
 

	
 
	BEGIN_TILE_LOOP(tile_cur, w, h, tile) {
 
		if (!EnsureNoVehicleOnGround(tile_cur)) return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) {
 
			DeleteAnimatedTile(tile_cur);
 
			DoClearSquare(tile_cur);
 
		}
 
	} END_TILE_LOOP(tile_cur, w, h, tile)
 

	
 
	if (flags & DC_EXEC) {
 
		for (uint i = 0; i < afc->nof_depots; ++i) {
 
			DeleteWindowById(
 
				WC_VEHICLE_DEPOT, tile + ToTileIndexDiff(afc->airport_depots[i])
 
			);
 
		}
 

	
 
		st->rect.AfterRemoveRect(st, tile, w, h);
 

	
 
@@ -1937,142 +1934,140 @@ static CommandCost RemoveBuoy(Station *s
 
		UpdateStationVirtCoordDirty(st);
 
		DeleteStationIfEmpty(st);
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.remove_truck_station);
 
}
 

	
 
static const TileIndexDiffC _dock_tileoffs_chkaround[] = {
 
	{-1,  0},
 
	{ 0,  0},
 
	{ 0,  0},
 
	{ 0, -1}
 
};
 
static const byte _dock_w_chk[4] = { 2, 1, 2, 1 };
 
static const byte _dock_h_chk[4] = { 1, 2, 1, 2 };
 

	
 
/** Build a dock/haven.
 
 * @param tile tile where dock will be built
 
 * @param flags operation to perform
 
 * @param p1 (bit 0) - allow docks directly adjacent to other docks.
 
 * @param p2 unused
 
 */
 
CommandCost CmdBuildDock(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	CommandCost cost;
 

	
 
	DiagDirection direction = GetInclinedSlopeDirection(GetTileSlope(tile, NULL));
 
	if (direction == INVALID_DIAGDIR) return_cmd_error(STR_304B_SITE_UNSUITABLE);
 
	direction = ReverseDiagDir(direction);
 

	
 
	/* Docks cannot be placed on rapids */
 
	if (IsWaterTile(tile)) return_cmd_error(STR_304B_SITE_UNSUITABLE);
 

	
 
	if (!(flags & DC_NO_TOWN_RATING) && !CheckIfAuthorityAllows(tile)) return CMD_ERROR;
 

	
 
	if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 
	if (CmdFailed(DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR))) return CMD_ERROR;
 

	
 
	TileIndex tile_cur = tile + TileOffsByDiagDir(direction);
 

	
 
	if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_304B_SITE_UNSUITABLE);
 
	}
 

	
 
	if (MayHaveBridgeAbove(tile_cur) && IsBridgeAbove(tile_cur)) return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
 

	
 
	/* Get the water class of the water tile before it is cleared.*/
 
	WaterClass wc = GetWaterClass(tile_cur);
 

	
 
	cost = DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
	if (CmdFailed(cost)) return CMD_ERROR;
 
	if (CmdFailed(DoCommand(tile_cur, 0, 0, flags, CMD_LANDSCAPE_CLEAR))) return CMD_ERROR;
 

	
 
	tile_cur += TileOffsByDiagDir(direction);
 
	if (!IsTileType(tile_cur, MP_WATER) || GetTileSlope(tile_cur, NULL) != SLOPE_FLAT) {
 
		return_cmd_error(STR_304B_SITE_UNSUITABLE);
 
	}
 

	
 
	/* middle */
 
	Station *st = NULL;
 

	
 
	if (!_patches.adjacent_stations || !HasBit(p1, 0)) {
 
		st = GetStationAround(
 
				tile + ToTileIndexDiff(_dock_tileoffs_chkaround[direction]),
 
				_dock_w_chk[direction], _dock_h_chk[direction], INVALID_STATION);
 
		if (st == CHECK_STATIONS_ERR) return CMD_ERROR;
 
	}
 

	
 
	/* Find a station close to us */
 
	if (st == NULL) st = GetClosestStationFromTile(tile);
 

	
 
	/* In case of new station if DC_EXEC is NOT set we still need to create the station
 
	 * to test if everything is OK. In this case we need to delete it before return. */
 
	AutoPtrT<Station> st_auto_delete;
 

	
 
	if (st != NULL) {
 
		if (st->owner != _current_player)
 
		if (st->owner != _current_player) {
 
			return_cmd_error(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION);
 
		}
 

	
 
		if (!st->rect.BeforeAddRect(tile, _dock_w_chk[direction], _dock_h_chk[direction], StationRect::ADD_TEST)) return CMD_ERROR;
 

	
 
		if (st->dock_tile != 0) return_cmd_error(STR_304C_TOO_CLOSE_TO_ANOTHER_DOCK);
 
	} else {
 
		/* allocate and initialize new station */
 
		st = new Station(tile);
 
		if (st == NULL) return_cmd_error(STR_3008_TOO_MANY_STATIONS_LOADING);
 

	
 
		/* ensure that in case of error (or no DC_EXEC) the station gets deleted upon return */
 
		st_auto_delete = st;
 

	
 
		Town *t = st->town = ClosestTownFromTile(tile, (uint)-1);
 

	
 
		if (IsValidPlayer(_current_player) && (flags & DC_EXEC) != 0) {
 
			SetBit(t->have_ratings, _current_player);
 
		}
 

	
 
		st->sign.width_1 = 0;
 

	
 
		if (!GenerateStationName(st, tile, STATIONNAMING_DOCK)) return CMD_ERROR;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
		st->dock_tile = tile;
 
		st->AddFacility(FACIL_DOCK, tile);
 

	
 
		st->rect.BeforeAddRect(tile, _dock_w_chk[direction], _dock_h_chk[direction], StationRect::ADD_TRY);
 

	
 
		MakeDock(tile, st->owner, st->index, direction, wc);
 

	
 
		UpdateStationVirtCoordDirty(st);
 
		UpdateStationAcceptance(st, false);
 
		RebuildStationLists();
 
		InvalidateWindow(WC_STATION_LIST, st->owner);
 
		InvalidateWindowWidget(WC_STATION_VIEW, st->index, SVW_SHIPS);
 
		/* success, so don't delete the new station */
 
		st_auto_delete.Detach();
 
	}
 

	
 
	return CommandCost(EXPENSES_CONSTRUCTION, _price.build_dock);
 
}
 

	
 
static CommandCost RemoveDock(Station *st, uint32 flags)
 
{
 
	if (!CheckOwnership(st->owner)) return CMD_ERROR;
 

	
 
	TileIndex tile1 = st->dock_tile;
 
	TileIndex tile2 = tile1 + TileOffsByDiagDir(GetDockDirection(tile1));
 

	
 
	if (!EnsureNoVehicleOnGround(tile1)) return CMD_ERROR;
 
	if (!EnsureNoVehicleOnGround(tile2)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DoClearSquare(tile1);
 
		MakeWaterKeepingClass(tile2, st->owner);
 

	
 
		st->rect.AfterRemoveTile(st, tile1);
 
		st->rect.AfterRemoveTile(st, tile2);
 

	
 
		MarkTileDirtyByTile(tile2);
 

	
 
		st->dock_tile = 0;
 
		st->facilities &= ~FACIL_DOCK;
 
@@ -2110,50 +2105,48 @@ static void DrawTile_Station(TileInfo *t
 
		custom_ground_offset = 0;
 
	}
 
	uint32 relocation = 0;
 
	const Station *st = NULL;
 
	const StationSpec *statspec = NULL;
 
	PlayerID owner = GetTileOwner(ti->tile);
 

	
 
	SpriteID palette;
 
	if (IsValidPlayer(owner)) {
 
		palette = PLAYER_SPRITE_COLOR(owner);
 
	} else {
 
		/* Some stations are not owner by a player, namely oil rigs */
 
		palette = PALETTE_TO_GREY;
 
	}
 

	
 
	/* don't show foundation for docks */
 
	if (ti->tileh != SLOPE_FLAT && !IsDock(ti->tile))
 
		DrawFoundation(ti, FOUNDATION_LEVELED);
 

	
 
	if (IsCustomStationSpecIndex(ti->tile)) {
 
		/* look for customization */
 
		st = GetStationByTile(ti->tile);
 
		statspec = st->speclist[GetCustomStationSpecIndex(ti->tile)].spec;
 

	
 
		//debug("Cust-o-mized %p", statspec);
 

	
 
		if (statspec != NULL) {
 
			uint tile = GetStationGfx(ti->tile);
 

	
 
			relocation = GetCustomStationRelocation(statspec, st, ti->tile);
 

	
 
			if (HasBit(statspec->callbackmask, CBM_STATION_SPRITE_LAYOUT)) {
 
				uint16 callback = GetStationCallback(CBID_STATION_SPRITE_LAYOUT, 0, 0, statspec, st, ti->tile);
 
				if (callback != CALLBACK_FAILED) tile = (callback & ~1) + GetRailStationAxis(ti->tile);
 
			}
 

	
 
			/* Ensure the chosen tile layout is valid for this custom station */
 
			if (statspec->renderdata != NULL) {
 
				t = &statspec->renderdata[tile < statspec->tiles ? tile : (uint)GetRailStationAxis(ti->tile)];
 
			}
 
		}
 
	}
 

	
 
	if (t == NULL || t->seq == NULL) t = &_station_display_datas[GetStationType(ti->tile)][GetStationGfx(ti->tile)];
 

	
 

	
 
	if (IsBuoy(ti->tile) || IsDock(ti->tile)) {
 
		if (ti->tileh == SLOPE_FLAT) {
 
			DrawWaterClassGround(ti);
 
		} else {
 
@@ -2463,49 +2456,53 @@ static VehicleEnterTileStatus VehicleEnt
 
				SetBit(v->u.road.state, RVS_IN_ROAD_STOP);
 

	
 
				/* Allocate a bay and update the road state */
 
				uint bay_nr = rs->AllocateBay();
 
				SB(v->u.road.state, RVS_USING_SECOND_BAY, 1, bay_nr);
 

	
 
				/* Mark the station entrace as busy */
 
				rs->SetEntranceBusy(true);
 
			}
 
		}
 
	}
 

	
 
	return VETSB_CONTINUE;
 
}
 

	
 
/* this function is called for one station each tick */
 
static void StationHandleBigTick(Station *st)
 
{
 
	UpdateStationAcceptance(st, true);
 

	
 
	if (st->facilities == 0 && ++st->delete_ctr >= 8) delete st;
 

	
 
}
 

	
 
static inline void byte_inc_sat(byte *p) { byte b = *p + 1; if (b != 0) *p = b; }
 
static inline void byte_inc_sat(byte *p)
 
{
 
	byte b = *p + 1;
 
	if (b != 0) *p = b;
 
}
 

	
 
static void UpdateStationRating(Station *st)
 
{
 
	bool waiting_changed = false;
 

	
 
	byte_inc_sat(&st->time_since_load);
 
	byte_inc_sat(&st->time_since_unload);
 

	
 
	GoodsEntry *ge = st->goods;
 
	do {
 
		/* Slowly increase the rating back to his original level in the case we
 
		 *  didn't deliver cargo yet to this station. This happens when a bribe
 
		 *  failed while you didn't moved that cargo yet to a station. */
 
		if (!HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP) && ge->rating < INITIAL_STATION_RATING) {
 
			ge->rating++;
 
		}
 

	
 
		/* Only change the rating if we are moving this cargo */
 
		if (HasBit(ge->acceptance_pickup, GoodsEntry::PICKUP)) {
 
			byte_inc_sat(&ge->days_since_pickup);
 

	
 
			int rating = 0;
 

	
 
			{
 
@@ -2601,48 +2598,49 @@ static void StationHandleSmallTick(Stati
 
	if (st->facilities == 0) return;
 

	
 
	byte b = st->delete_ctr + 1;
 
	if (b >= 185) b = 0;
 
	st->delete_ctr = b;
 

	
 
	if (b == 0) UpdateStationRating(st);
 
}
 

	
 
void OnTick_Station()
 
{
 
	if (_game_mode == GM_EDITOR) return;
 

	
 
	uint i = _station_tick_ctr;
 
	if (++_station_tick_ctr > GetMaxStationIndex()) _station_tick_ctr = 0;
 

	
 
	if (IsValidStationID(i)) StationHandleBigTick(GetStation(i));
 

	
 
	Station *st;
 
	FOR_ALL_STATIONS(st) StationHandleSmallTick(st);
 
}
 

	
 
void StationMonthlyLoop()
 
{
 
	/* not used */
 
}
 

	
 

	
 
void ModifyStationRatingAround(TileIndex tile, PlayerID owner, int amount, uint radius)
 
{
 
	Station *st;
 

	
 
	FOR_ALL_STATIONS(st) {
 
		if (st->owner == owner &&
 
				DistanceManhattan(tile, st->xy) <= radius) {
 
			for (CargoID i = 0; i < NUM_CARGO; i++) {
 
				GoodsEntry* ge = &st->goods[i];
 

	
 
				if (ge->acceptance_pickup != 0) {
 
					ge->rating = Clamp(ge->rating + amount, 0, 255);
 
				}
 
			}
 
		}
 
	}
 
}
 

	
 
static void UpdateStationWaiting(Station *st, CargoID type, uint amount)
 
{
 
	st->goods[type].cargo.Append(new CargoPacket(st->index, amount));
 
@@ -2974,49 +2972,48 @@ static CommandCost ClearTile_Station(Til
 
			return RemoveRoadStop(st, flags, tile);
 
		case STATION_BUS:
 
			if (IsDriveThroughStopTile(tile) && !CanRemoveRoadWithStop(tile))
 
				return_cmd_error(STR_3046_MUST_DEMOLISH_BUS_STATION);
 
			return RemoveRoadStop(st, flags, tile);
 
		case STATION_BUOY:    return RemoveBuoy(st, flags);
 
		case STATION_DOCK:    return RemoveDock(st, flags);
 
		default: break;
 
	}
 

	
 
	return CMD_ERROR;
 
}
 

	
 
void InitializeStations()
 
{
 
	/* Clean the station pool and create 1 block in it */
 
	_Station_pool.CleanPool();
 
	_Station_pool.AddBlockToPool();
 

	
 
	/* Clean the roadstop pool and create 1 block in it */
 
	_RoadStop_pool.CleanPool();
 
	_RoadStop_pool.AddBlockToPool();
 

	
 
	_station_tick_ctr = 0;
 

	
 
}
 

	
 

	
 
void AfterLoadStations()
 
{
 
	/* Update the speclists of all stations to point to the currently loaded custom stations. */
 
	Station *st;
 
	FOR_ALL_STATIONS(st) {
 
		for (uint i = 0; i < st->num_specs; i++) {
 
			if (st->speclist[i].grfid == 0) continue;
 

	
 
			st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx);
 
		}
 

	
 
		for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();
 
	}
 
}
 

	
 
static CommandCost TerraformTile_Station(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
 
{
 
	if (_patches.build_on_slopes && AutoslopeEnabled()) {
 
		/* TODO: If you implement newgrf callback 149 'land slope check', you have to decide what to do with it here.
 
		 *       TTDP does not call it.
 
		 */
 
@@ -3067,85 +3064,83 @@ extern const TileTypeProcs _tile_type_st
 
	TerraformTile_Station,      /* terraform_tile_proc */
 
};
 

	
 
static const SaveLoad _roadstop_desc[] = {
 
	SLE_VAR(RoadStop,xy,           SLE_UINT32),
 
	SLE_CONDNULL(1, 0, 44),
 
	SLE_VAR(RoadStop,status,       SLE_UINT8),
 
	/* Index was saved in some versions, but this is not needed */
 
	SLE_CONDNULL(4, 0, 8),
 
	SLE_CONDNULL(2, 0, 44),
 
	SLE_CONDNULL(1, 0, 25),
 

	
 
	SLE_REF(RoadStop,next,         REF_ROADSTOPS),
 
	SLE_CONDNULL(2, 0, 44),
 

	
 
	SLE_CONDNULL(4, 0, 24),
 
	SLE_CONDNULL(1, 25, 25),
 

	
 
	SLE_END()
 
};
 

	
 
static const SaveLoad _station_desc[] = {
 
	SLE_CONDVAR(Station, xy,                         SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
 
	SLE_CONDVAR(Station, xy,                         SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDNULL(4, 0, 5), // bus/lorry tile
 
	SLE_CONDNULL(4, 0, 5),  ///< bus/lorry tile
 
	SLE_CONDVAR(Station, train_tile,                 SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
 
	SLE_CONDVAR(Station, train_tile,                 SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Station, airport_tile,               SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
 
	SLE_CONDVAR(Station, airport_tile,               SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Station, dock_tile,                  SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
 
	SLE_CONDVAR(Station, dock_tile,                  SLE_UINT32,                  6, SL_MAX_VERSION),
 
	    SLE_REF(Station, town,                       REF_TOWN),
 
	    SLE_VAR(Station, trainst_w,                  SLE_UINT8),
 
	SLE_CONDVAR(Station, trainst_h,                  SLE_UINT8,                   2, SL_MAX_VERSION),
 

	
 
	/* alpha_order was stored here in savegame format 0 - 3 */
 
	SLE_CONDNULL(1, 0, 3),
 
	SLE_CONDNULL(1, 0, 3),  ///< alpha_order
 

	
 
	    SLE_VAR(Station, string_id,                  SLE_STRINGID),
 
	SLE_CONDSTR(Station, name,                       SLE_STR, 0,                 84, SL_MAX_VERSION),
 
	    SLE_VAR(Station, had_vehicle_of_type,        SLE_UINT16),
 

	
 
	    SLE_VAR(Station, time_since_load,            SLE_UINT8),
 
	    SLE_VAR(Station, time_since_unload,          SLE_UINT8),
 
	    SLE_VAR(Station, delete_ctr,                 SLE_UINT8),
 
	    SLE_VAR(Station, owner,                      SLE_UINT8),
 
	    SLE_VAR(Station, facilities,                 SLE_UINT8),
 
	    SLE_VAR(Station, airport_type,               SLE_UINT8),
 

	
 
	SLE_CONDNULL(2, 0, 5), // Truck/bus stop status
 
	SLE_CONDNULL(1, 0, 4), // Blocked months
 
	SLE_CONDNULL(2, 0, 5),  ///< Truck/bus stop status
 
	SLE_CONDNULL(1, 0, 4),  ///< Blocked months
 

	
 
	SLE_CONDVAR(Station, airport_flags,              SLE_VAR_U64 | SLE_FILE_U16,  0,  2),
 
	SLE_CONDVAR(Station, airport_flags,              SLE_VAR_U64 | SLE_FILE_U32,  3, 45),
 
	SLE_CONDVAR(Station, airport_flags,              SLE_UINT64,                 46, SL_MAX_VERSION),
 

	
 
	SLE_CONDNULL(2, 0, 25), /* Ex last-vehicle */
 
	SLE_CONDNULL(2, 0, 25), ///< last-vehicle
 
	SLE_CONDVAR(Station, last_vehicle_type,          SLE_UINT8,                  26, SL_MAX_VERSION),
 

	
 
	/* Was custom station class and id */
 
	SLE_CONDNULL(2, 3, 25),
 
	SLE_CONDNULL(2, 3, 25), ///< custom station class and id
 
	SLE_CONDVAR(Station, build_date,                 SLE_FILE_U16 | SLE_VAR_I32,  3, 30),
 
	SLE_CONDVAR(Station, build_date,                 SLE_INT32,                  31, SL_MAX_VERSION),
 

	
 
	SLE_CONDREF(Station, bus_stops,                  REF_ROADSTOPS,               6, SL_MAX_VERSION),
 
	SLE_CONDREF(Station, truck_stops,                REF_ROADSTOPS,               6, SL_MAX_VERSION),
 

	
 
	/* Used by newstations for graphic variations */
 
	SLE_CONDVAR(Station, random_bits,                SLE_UINT16,                 27, SL_MAX_VERSION),
 
	SLE_CONDVAR(Station, waiting_triggers,           SLE_UINT8,                  27, SL_MAX_VERSION),
 
	SLE_CONDVAR(Station, num_specs,                  SLE_UINT8,                  27, SL_MAX_VERSION),
 

	
 
	SLE_CONDLST(Station, loading_vehicles,           REF_VEHICLE,                57, SL_MAX_VERSION),
 

	
 
	/* reserve extra space in savegame here. (currently 32 bytes) */
 
	SLE_CONDNULL(32, 2, SL_MAX_VERSION),
 

	
 
	SLE_END()
 
};
 

	
 
static uint16 _waiting_acceptance;
 
static uint16 _cargo_source;
 
static uint32 _cargo_source_xy;
 
static uint16 _cargo_days;
 
static Money  _cargo_feeder_share;
src/station_gui.cpp
Show inline comments
 
@@ -81,71 +81,70 @@ static void StationsWndShowStationRating
 
	/* Draw green/red ratings bar (fits into 14 pixels) */
 
	y += 8;
 
	GfxFillRect(x + 1, y, x + 14, y, 0xB8);
 
	rating = minu(rating, rating_full) / 16;
 
	if (rating != 0) GfxFillRect(x + 1, y, x + rating, y, 0xD0);
 
}
 

	
 
const StringID _station_sort_listing[] = {
 
	STR_SORT_BY_DROPDOWN_NAME,
 
	STR_SORT_BY_FACILITY,
 
	STR_SORT_BY_WAITING,
 
	STR_SORT_BY_RATING_MAX,
 
	INVALID_STRING_ID
 
};
 

	
 
static char _bufcache[64];
 
static const Station* _last_station;
 
static int _internal_sort_order;
 

	
 
static int CDECL StationNameSorter(const void *a, const void *b)
 
{
 
	const Station* st1 = *(const Station**)a;
 
	const Station* st2 = *(const Station**)b;
 
	char buf1[64];
 
	int r;
 

	
 
	SetDParam(0, st1->index);
 
	GetString(buf1, STR_STATION, lastof(buf1));
 

	
 
	if (st2 != _last_station) {
 
		_last_station = st2;
 
		SetDParam(0, st2->index);
 
		GetString(_bufcache, STR_STATION, lastof(_bufcache));
 
	}
 

	
 
	r = strcmp(buf1, _bufcache); // sort by name
 
	int r = strcmp(buf1, _bufcache); // sort by name
 
	return (_internal_sort_order & 1) ? -r : r;
 
}
 

	
 
static int CDECL StationTypeSorter(const void *a, const void *b)
 
{
 
	const Station* st1 = *(const Station**)a;
 
	const Station* st2 = *(const Station**)b;
 
	return (_internal_sort_order & 1) ? st2->facilities - st1->facilities : st1->facilities - st2->facilities;
 
}
 

	
 
static const uint32 _cargo_filter_max = ~0;
 
static const uint32 _cargo_filter_max = UINT32_MAX;
 
static uint32 _cargo_filter = _cargo_filter_max;
 

	
 
static int CDECL StationWaitingSorter(const void *a, const void *b)
 
{
 
	const Station* st1 = *(const Station**)a;
 
	const Station* st2 = *(const Station**)b;
 
	Money sum1 = 0, sum2 = 0;
 

	
 
	for (CargoID j = 0; j < NUM_CARGO; j++) {
 
		if (!HasBit(_cargo_filter, j)) continue;
 
		if (!st1->goods[j].cargo.Empty()) sum1 += GetTransportedGoodsIncome(st1->goods[j].cargo.Count(), 20, 50, j);
 
		if (!st2->goods[j].cargo.Empty()) sum2 += GetTransportedGoodsIncome(st2->goods[j].cargo.Count(), 20, 50, j);
 
	}
 

	
 
	return (_internal_sort_order & 1) ? ClampToI32(sum2 - sum1) : ClampToI32(sum1 - sum2);
 
}
 

	
 
/**
 
 * qsort-compatible version of sorting two stations by maximum rating
 
 * @param a   First object to be sorted, must be of type (const Station *)
 
 * @param b   Second object to be sorted, must be of type (const Station *)
 
 * @return    The sort order
 
 * @retval >0 a should come before b in the list
 
 * @retval <0 b should come before a in the list
 
@@ -729,51 +728,48 @@ assert_compile(WINDOW_CUSTOM_SIZE >= siz
 

	
 
struct CargoData {
 
	CargoID cargo;
 
	StationID source;
 
	uint count;
 

	
 
	CargoData(CargoID cargo, StationID source, uint count) :
 
		cargo(cargo),
 
		source(source),
 
		count(count)
 
	{ }
 
};
 

	
 
typedef std::list<CargoData> CargoDataList;
 

	
 
/**
 
 * Redraws whole StationView window
 
 *
 
 * @param w pointer to window
 
 */
 
static void DrawStationViewWindow(Window *w)
 
{
 
	StationID station_id = w->window_number;
 
	const Station* st = GetStation(station_id);
 
	int x, y;     ///< coordinates used for printing waiting/accepted/rating of cargo
 
	int pos;      ///< = w->vscroll.pos
 
	StringID str;
 
	CargoDataList cargolist;
 
	uint32 transfers = 0;
 

	
 
	/* count types of cargos waiting in station */
 
	for (CargoID i = 0; i < NUM_CARGO; i++) {
 
		if (st->goods[i].cargo.Empty()) {
 
			WP(w, stationview_d).cargo_rows[i] = 0;
 
		} else {
 
			/* Add an entry for total amount of cargo of this type waiting. */
 
			cargolist.push_back(CargoData(i, INVALID_STATION, st->goods[i].cargo.Count()));
 

	
 
			/* Set the row for this cargo entry for the expand/hide button */
 
			WP(w, stationview_d).cargo_rows[i] = cargolist.size();
 

	
 
			/* Add an entry for each distinct cargo source. */
 
			const CargoList::List *packets = st->goods[i].cargo.Packets();
 
			for (CargoList::List::const_iterator it = packets->begin(); it != packets->end(); it++) {
 
				const CargoPacket *cp = *it;
 
				if (cp->source != station_id) {
 
					bool added = false;
 

	
 
					/* Enable the expand/hide button for this cargo type */
 
					SetBit(transfers, i);
 

	
 
@@ -787,55 +783,57 @@ static void DrawStationViewWindow(Window
 
							cd->count += cp->count;
 
							added = true;
 
							break;
 
						}
 
					}
 

	
 
					if (!added) cargolist.push_back(CargoData(i, cp->source, cp->count));
 
				}
 
			}
 
		}
 
	}
 
	SetVScrollCount(w, cargolist.size() + 1); // update scrollbar
 

	
 
	/* disable some buttons */
 
	w->SetWidgetDisabledState(SVW_RENAME,   st->owner != _local_player);
 
	w->SetWidgetDisabledState(SVW_TRAINS,   !(st->facilities & FACIL_TRAIN));
 
	w->SetWidgetDisabledState(SVW_ROADVEHS, !(st->facilities & FACIL_TRUCK_STOP) && !(st->facilities & FACIL_BUS_STOP));
 
	w->SetWidgetDisabledState(SVW_PLANES,   !(st->facilities & FACIL_AIRPORT));
 
	w->SetWidgetDisabledState(SVW_SHIPS,    !(st->facilities & FACIL_DOCK));
 

	
 
	SetDParam(0, st->index);
 
	SetDParam(1, st->facilities);
 
	DrawWindowWidgets(w);
 

	
 
	x = 2;
 
	y = 15;
 
	pos = w->vscroll.pos;
 
	int x = 2;  ///< coordinates used for printing waiting/accepted/rating of cargo
 
	int y = 15;
 
	int pos = w->vscroll.pos; ///< = w->vscroll.pos
 

	
 
	uint width = w->widget[SVW_WAITING].right - w->widget[SVW_WAITING].left - 4;
 
	int maxrows = w->vscroll.cap;
 

	
 
	StringID str;
 

	
 
	if (--pos < 0) {
 
		str = STR_00D0_NOTHING;
 
		for (CargoID i = 0; i < NUM_CARGO; i++) {
 
			if (!st->goods[i].cargo.Empty()) str = STR_EMPTY;
 
		}
 
		SetDParam(0, str);
 
		DrawString(x, y, STR_0008_WAITING, TC_FROMSTRING);
 
		y += 10;
 
	}
 

	
 
	for (CargoDataList::const_iterator it = cargolist.begin(); it != cargolist.end() && pos > -maxrows; ++it) {
 
		if (--pos < 0) {
 
			const CargoData *cd = &(*it);
 
			if (cd->source == INVALID_STATION) {
 
				/* Heading */
 
				DrawCargoIcons(cd->cargo, cd->count, x, y, width);
 
				SetDParam(0, cd->cargo);
 
				SetDParam(1, cd->count);
 
				if (HasBit(transfers, cd->cargo)) {
 
					/* This cargo has transfers waiting so show the expand or shrink 'button' */
 
					const char *sym = HasBit(WP(w, stationview_d).cargo, cd->cargo) ? "-" : "+";
 
					DrawStringRightAligned(x + width - 8, y, STR_0009, TC_FROMSTRING);
 
					DoDrawString(sym, x + width - 6, y, TC_YELLOW);
 
				} else {
0 comments (0 inline, 0 general)