Changeset - r4352:460a517b040f
[Not reviewed]
master
0 17 0
truelight - 18 years ago 2006-08-22 18:15:17
truelight@openttd.org
(svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
-Codechange: IsValidXXXID now also checks if XXX is really valid, not if the number is within range
Both changes again in preperation of the new mem-pool system, which requires this.
IsValidXXXID is not a bit less pretty, but that will be cleaned up after the new mem-pool system
17 files changed with 88 insertions and 87 deletions:
0 comments (0 inline, 0 general)
aircraft_cmd.c
Show inline comments
 
@@ -417,25 +417,25 @@ static void DoDeleteAircraft(Vehicle *v)
 
	InvalidateWindowClasses(WC_AIRCRAFT_LIST);
 
}
 

	
 
/** Sell an aircraft.
 
 * @param tile unused
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
int32 CmdSellAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 

	
 
	if (flags & DC_EXEC) {
 
		// Invalidate depot
 
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
 
		DoDeleteAircraft(v);
 
@@ -447,25 +447,25 @@ int32 CmdSellAircraft(TileIndex tile, ui
 
}
 

	
 
/** Start/Stop an aircraft.
 
 * @param tile unused
 
 * @param p1 aircraft ID to start/stop
 
 * @param p2 unused
 
 */
 
int32 CmdStartStopAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	uint16 callback;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	// cannot stop airplane when in flight, or when taking off / landing
 
	if (v->u.air.state >= STARTTAKEOFF && v->u.air.state < TERM7)
 
		return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
 

	
 
	/* Check if this aircraft can be started/stopped. The callback will fail or
 
	 * return 0xFF if it can. */
 
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 
@@ -491,25 +491,25 @@ int32 CmdStartStopAircraft(TileIndex til
 
/** Send an aircraft to the hangar.
 
 * @param tile unused
 
 * @param p1 vehicle ID to send to the hangar
 
 * @param p2 various bitmasked elements
 
 * - p2 = 0      - aircraft goes to the depot and stays there (user command)
 
 * - p2 (bit 16) - aircraft will try to goto a depot, but not stop there (eg autorenew or autoreplace)
 
 * - p2 (bit 17) - aircraft will try to goto a depot at the airport specified by low word of p2 XXX - Not Used
 
 */
 
int32 CmdSendAircraftToHangar(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->current_order.type == OT_GOTO_DEPOT && p2 == 0) {
 
		if (flags & DC_EXEC) {
 
			if (v->current_order.flags & OF_UNLOAD) v->cur_order_index++;
 
			v->current_order.type = OT_DUMMY;
 
			v->current_order.flags = 0;
 
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
 
		}
 
@@ -559,25 +559,25 @@ int32 CmdSendAircraftToHangar(TileIndex 
 
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
 
 */
 
int32 CmdRefitAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	int pass, mail;
 
	int32 cost;
 
	CargoID new_cid = GB(p2, 0, 8);
 
	byte new_subtype = GB(p2, 8, 8);
 
	const AircraftVehicleInfo *avi;
 
	uint16 callback = CALLBACK_FAILED;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Aircraft || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!IsAircraftInHangarStopped(v)) return_cmd_error(STR_A01B_AIRCRAFT_MUST_BE_STOPPED);
 

	
 
	avi = AircraftVehInfo(v->engine_type);
 

	
 
	/* Check cargo */
 
	if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_AIRCRAFT_RUN);
depot.h
Show inline comments
 
@@ -26,37 +26,37 @@ static inline Depot *GetDepot(uint index
 
{
 
	return (Depot*)GetItemFromPool(&_depot_pool, index);
 
}
 

	
 
/**
 
 * Get the current size of the DepotPool
 
 */
 
static inline uint16 GetDepotPoolSize(void)
 
{
 
	return _depot_pool.total_items;
 
}
 

	
 
static inline bool IsDepotIndex(uint index)
 
{
 
	return index < GetDepotPoolSize();
 
}
 

	
 
/**
 
 * Check if a depot really exists.
 
 */
 
static inline bool IsValidDepot(const Depot* depot)
 
{
 
	return depot->xy != 0;
 
}
 

	
 
static inline bool IsValidDepotID(uint index)
 
{
 
	return index < GetDepotPoolSize() && IsValidDepot(GetDepot(index));
 
}
 

	
 
#define FOR_ALL_DEPOTS_FROM(d, start) for (d = GetDepot(start); d != NULL; d = (d->index + 1 < GetDepotPoolSize()) ? GetDepot(d->index + 1) : NULL) if (IsValidDepot(d))
 
#define FOR_ALL_DEPOTS(d) FOR_ALL_DEPOTS_FROM(d, 0)
 

	
 
#define MIN_SERVINT_PERCENT  5
 
#define MAX_SERVINT_PERCENT 90
 
#define MIN_SERVINT_DAYS    30
 
#define MAX_SERVINT_DAYS   800
 

	
 
/** Get the service interval domain.
 
 * Get the new proposed service interval for the vehicle is indeed, clamped
 
 * within the given bounds. @see MIN_SERVINT_PERCENT ,etc.
 
 * @param index proposed service interval
order_cmd.c
Show inline comments
 
@@ -170,39 +170,40 @@ static void DeleteOrderWarnings(const Ve
 
 * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given,
 
 *                        the order will be inserted before that one
 
 *                        only the first 8 bits used currently (bit 16 - 23) (max 255)
 
 * @param p2 packed order to insert
 
 */
 
int32 CmdInsertOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	VehicleID veh   = GB(p1,  0, 16);
 
	OrderID sel_ord = GB(p1, 16, 16);
 
	Order new_order = UnpackOrder(p2);
 

	
 
	if (!IsVehicleIndex(veh)) return CMD_ERROR;
 
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
 

	
 
	v = GetVehicle(veh);
 
	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* Check if the inserted order is to the correct destination (owner, type),
 
	 * and has the correct flags if any */
 
	switch (new_order.type) {
 
		case OT_GOTO_STATION: {
 
			const Station *st;
 

	
 
			if (!IsStationIndex(new_order.station)) return CMD_ERROR;
 
			if (!IsValidStationID(new_order.station)) return CMD_ERROR;
 
			st = GetStation(new_order.station);
 

	
 
			if (!IsValidStation(st) ||
 
					(st->airport_type != AT_OILRIG && !IsBuoy(st) && !CheckOwnership(st->owner))) {
 
			if (st->airport_type != AT_OILRIG && !IsBuoy(st) && !CheckOwnership(st->owner)) {
 
				return CMD_ERROR;
 
			}
 

	
 
			switch (v->type) {
 
				case VEH_Train:
 
					if (!(st->facilities & FACIL_TRAIN)) return CMD_ERROR;
 
					break;
 

	
 
				case VEH_Road:
 
					if (v->cargo_type == CT_PASSENGERS) {
 
						if (!(st->facilities & FACIL_BUS_STOP)) return CMD_ERROR;
 
					} else {
 
@@ -242,41 +243,39 @@ int32 CmdInsertOrder(TileIndex tile, uin
 
					if (v->type != VEH_Train) return CMD_ERROR;
 
					break;
 

	
 
				default: return CMD_ERROR;
 
			}
 
			break;
 
		}
 

	
 
		case OT_GOTO_DEPOT: {
 
			if (v->type == VEH_Aircraft) {
 
				const Station* st;
 

	
 
				if (!IsStationIndex(new_order.station)) return CMD_ERROR;
 
				if (!IsValidStationID(new_order.station)) return CMD_ERROR;
 
				st = GetStation(new_order.station);
 

	
 
				if (!IsValidStation(st) ||
 
						(st->airport_type != AT_OILRIG && !CheckOwnership(st->owner)) ||
 
				if ((st->airport_type != AT_OILRIG && !CheckOwnership(st->owner)) ||
 
						!(st->facilities & FACIL_AIRPORT) ||
 
						GetAirport(st->airport_type)->nof_depots == 0) {
 
					return CMD_ERROR;
 
				}
 
			} else {
 
				const Depot* dp;
 

	
 
				if (!IsDepotIndex(new_order.station)) return CMD_ERROR;
 
				if (!IsValidDepotID(new_order.station)) return CMD_ERROR;
 
				dp = GetDepot(new_order.station);
 

	
 
				if (!IsValidDepot(dp) || !CheckOwnership(GetTileOwner(dp->xy)))
 
					return CMD_ERROR;
 
				if (!CheckOwnership(GetTileOwner(dp->xy))) return CMD_ERROR;
 

	
 
				switch (v->type) {
 
					case VEH_Train:
 
						if (!IsTileDepotType(dp->xy, TRANSPORT_RAIL)) return CMD_ERROR;
 
						break;
 

	
 
					case VEH_Road:
 
						if (!IsTileDepotType(dp->xy, TRANSPORT_ROAD)) return CMD_ERROR;
 
						break;
 

	
 
					case VEH_Ship:
 
						if (!IsTileDepotType(dp->xy, TRANSPORT_WATER)) return CMD_ERROR;
 
@@ -300,25 +299,25 @@ int32 CmdInsertOrder(TileIndex tile, uin
 
					break;
 

	
 
				default: return CMD_ERROR;
 
			}
 
			break;
 
		}
 

	
 
		case OT_GOTO_WAYPOINT: {
 
			const Waypoint* wp;
 

	
 
			if (v->type != VEH_Train) return CMD_ERROR;
 

	
 
			if (!IsWaypointIndex(new_order.station)) return CMD_ERROR;
 
			if (!IsValidWaypointID(new_order.station)) return CMD_ERROR;
 
			wp = GetWaypoint(new_order.station);
 

	
 
			if (!CheckOwnership(GetTileOwner(wp->xy))) return CMD_ERROR;
 

	
 
			/* Order flags can be any of the following for waypoints:
 
			 * [non-stop]
 
			 * non-stop orders (if any) are only valid for trains */
 
			switch (new_order.flags) {
 
				case 0: break;
 

	
 
				case OF_NON_STOP:
 
					if (v->type != VEH_Train) return CMD_ERROR;
 
@@ -433,27 +432,29 @@ static int32 DecloneOrder(Vehicle *dst, 
 
/** Delete an order from the orderlist of a vehicle.
 
 * @param tile unused
 
 * @param p1 the ID of the vehicle
 
 * @param p2 the order to delete (max 255)
 
 */
 
int32 CmdDeleteOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v, *u;
 
	VehicleID veh_id = p1;
 
	OrderID sel_ord = p2;
 
	Order *order;
 

	
 
	if (!IsVehicleIndex(veh_id)) return CMD_ERROR;
 
	if (!IsValidVehicleID(veh_id)) return CMD_ERROR;
 

	
 
	v = GetVehicle(veh_id);
 
	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* If we did not select an order, we maybe want to de-clone the orders */
 
	if (sel_ord >= v->num_orders)
 
		return DecloneOrder(v, flags);
 

	
 
	order = GetVehicleOrder(v, sel_ord);
 
	if (order == NULL) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		if (GetVehicleOrder(v, sel_ord - 1) == NULL) {
 
			if (GetVehicleOrder(v, sel_ord + 1) != NULL) {
 
				/* First item, but not the last, so we need to alter v->orders
 
@@ -505,27 +506,29 @@ int32 CmdDeleteOrder(TileIndex tile, uin
 
}
 

	
 
/** Goto next order of order-list.
 
 * @param tile unused
 
 * @param p1 The ID of the vehicle which order is skipped
 
 * @param p2 unused
 
 */
 
int32 CmdSkipOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	VehicleID veh_id = p1;
 

	
 
	if (!IsVehicleIndex(veh_id)) return CMD_ERROR;
 
	if (!IsValidVehicleID(veh_id)) return CMD_ERROR;
 

	
 
	v = GetVehicle(veh_id);
 
	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		/* Goto next order */
 
		OrderID b = v->cur_order_index + 1;
 
		if (b >= v->num_orders) b = 0;
 

	
 
		v->cur_order_index = b;
 

	
 
		if (v->type == VEH_Train) v->u.rail.days_since_order_progr = 0;
 

	
 
		if (v->type == VEH_Road) ClearSlot(v);
 

	
 
@@ -552,29 +555,30 @@ int32 CmdSkipOrder(TileIndex tile, uint3
 
 * - p1 = (bit 16 - 31) - the selected order (if any). If the last order is given,
 
 *                        the order will be inserted before that one
 
 *                        only the first 8 bits used currently (bit 16 - 23) (max 255)
 
 * @param p2 mode to change the order to (always set)
 
 */
 
int32 CmdModifyOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	Order *order;
 
	OrderID sel_ord = GB(p1, 16, 16); // XXX - automatically truncated to 8 bits.
 
	VehicleID veh   = GB(p1,  0, 16);
 

	
 
	if (!IsVehicleIndex(veh)) return CMD_ERROR;
 
	if (!IsValidVehicleID(veh)) return CMD_ERROR;
 
	if (p2 != OFB_FULL_LOAD && p2 != OFB_UNLOAD && p2 != OFB_NON_STOP && p2 != OFB_TRANSFER) return CMD_ERROR;
 

	
 
	v = GetVehicle(veh);
 
	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* Is it a valid order? */
 
	if (sel_ord >= v->num_orders) return CMD_ERROR;
 

	
 
	order = GetVehicleOrder(v, sel_ord);
 
	if (order->type != OT_GOTO_STATION &&
 
			(order->type != OT_GOTO_DEPOT    || p2 == OFB_UNLOAD) &&
 
			(order->type != OT_GOTO_WAYPOINT || p2 != OFB_NON_STOP)) {
 
		return CMD_ERROR;
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
@@ -619,40 +623,40 @@ int32 CmdModifyOrder(TileIndex tile, uin
 
/** Clone/share/copy an order-list of an other vehicle.
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0-15) - destination vehicle to clone orders to (p1 & 0xFFFF)
 
 * - p1 = (bit 16-31) - source vehicle to clone orders from, if any (none for CO_UNSHARE)
 
 * @param p2 mode of cloning: CO_SHARE, CO_COPY, or CO_UNSHARE
 
 */
 
int32 CmdCloneOrder(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *dst;
 
	VehicleID veh_src = GB(p1, 16, 16);
 
	VehicleID veh_dst = GB(p1,  0, 16);
 

	
 
	if (!IsVehicleIndex(veh_dst)) return CMD_ERROR;
 
	if (!IsValidVehicleID(veh_dst)) return CMD_ERROR;
 

	
 
	dst = GetVehicle(veh_dst);
 

	
 
	if (!IsValidVehicle(dst) || !CheckOwnership(dst->owner)) return CMD_ERROR;
 
	if (!CheckOwnership(dst->owner)) return CMD_ERROR;
 

	
 
	switch (p2) {
 
		case CO_SHARE: {
 
			Vehicle *src;
 

	
 
			if (!IsVehicleIndex(veh_src)) return CMD_ERROR;
 
			if (!IsValidVehicleID(veh_src)) return CMD_ERROR;
 

	
 
			src = GetVehicle(veh_src);
 

	
 
			/* Sanity checks */
 
			if (!IsValidVehicle(src) || !CheckOwnership(src->owner) || dst->type != src->type || dst == src)
 
			if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src)
 
				return CMD_ERROR;
 

	
 
			/* Trucks can't share orders with busses (and visa versa) */
 
			if (src->type == VEH_Road) {
 
				if (src->cargo_type != dst->cargo_type && (src->cargo_type == CT_PASSENGERS || dst->cargo_type == CT_PASSENGERS))
 
					return CMD_ERROR;
 
			}
 

	
 
			/* Is the vehicle already in the shared list? */
 
			{
 
				const Vehicle* u;
 

	
 
@@ -676,30 +680,30 @@ int32 CmdCloneOrder(TileIndex tile, uint
 

	
 
				InvalidateVehicleOrder(dst);
 
				InvalidateVehicleOrder(src);
 

	
 
				RebuildVehicleLists();
 
			}
 
		} break;
 

	
 
		case CO_COPY: {
 
			Vehicle *src;
 
			int delta;
 

	
 
			if (!IsVehicleIndex(veh_src)) return CMD_ERROR;
 
			if (!IsValidVehicleID(veh_src)) return CMD_ERROR;
 

	
 
			src = GetVehicle(veh_src);
 

	
 
			/* Sanity checks */
 
			if (!IsValidVehicle(src) || !CheckOwnership(src->owner) || dst->type != src->type || dst == src)
 
			if (!CheckOwnership(src->owner) || dst->type != src->type || dst == src)
 
				return CMD_ERROR;
 

	
 
			/* Trucks can't copy all the orders from busses (and visa versa) */
 
			if (src->type == VEH_Road) {
 
				const Order *order;
 
				TileIndex required_dst = INVALID_TILE;
 

	
 
				FOR_VEHICLE_ORDERS(src, order) {
 
					if (order->type == OT_GOTO_STATION) {
 
						const Station *st = GetStation(order->station);
 
						if (dst->cargo_type == CT_PASSENGERS) {
 
							if (st->bus_stops != NULL) required_dst = st->bus_stops->xy;
 
@@ -835,29 +839,30 @@ void RestoreVehicleOrders(const Vehicle*
 
 * as far as I can see. We can store it in BackuppedOrders, and restore it, but
 
 * but we have no way of seeing it has been tampered with or not, as we have no
 
 * legit way of knowing what that ID was.@n
 
 * If we do want to backup/restore it, just add UnitID uid to BackuppedOrders, and
 
 * restore it as parameter 'y' (ugly hack I know) for example. "v->unitnumber = y;"
 
 */
 
int32 CmdRestoreOrderIndex(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	OrderID cur_ord = GB(p2,  0, 16);
 
	uint16 serv_int = GB(p2, 16, 16);
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	/* Check the vehicle type and ownership, and if the service interval and order are in range */
 
	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (serv_int != GetServiceIntervalClamped(serv_int) || cur_ord >= v->num_orders) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		v->cur_order_index = cur_ord;
 
		v->service_interval = serv_int;
 
	}
 

	
 
	return 0;
 
}
 

	
 

	
 
static TileIndex GetStationTileForVehicle(const Vehicle* v, const Station* st)
road_cmd.c
Show inline comments
 
@@ -284,25 +284,25 @@ static uint32 CheckRoadSlope(Slope tileh
 
int32 CmdBuildRoad(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	int32 cost = 0;
 
	int32 ret;
 
	RoadBits existing = 0;
 
	RoadBits pieces;
 
	Slope tileh;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
 

	
 
	/* Road pieces are max 4 bitset values (NE, NW, SE, SW) and town can only be non-zero
 
	 * if a non-player is building the road */
 
	if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsTownIndex(p2)) return CMD_ERROR;
 
	if ((p1 >> 4) || (_current_player < MAX_PLAYERS && p2 != 0) || !IsValidTownID(p2)) return CMD_ERROR;
 
	pieces = p1;
 

	
 
	tileh = GetTileSlope(tile, NULL);
 

	
 
	switch (GetTileType(tile)) {
 
		case MP_STREET:
 
			switch (GetRoadTileType(tile)) {
 
				case ROAD_TILE_NORMAL:
 
					if (HasRoadWorks(tile)) return_cmd_error(STR_ROAD_WORKS_IN_PROGRESS);
 

	
 
					existing = GetRoadBits(tile);
 
					if ((existing & pieces) == pieces) {
roadveh_cmd.c
Show inline comments
 
@@ -203,25 +203,25 @@ int32 CmdBuildRoadVeh(TileIndex tile, ui
 
}
 

	
 
/** Start/Stop a road vehicle.
 
 * @param tile unused
 
 * @param p1 road vehicle ID to start/stop
 
 * @param p2 unused
 
 */
 
int32 CmdStartStopRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	uint16 callback;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* Check if this road veh can be started/stopped. The callback will fail or
 
	 * return 0xFF if it can. */
 
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
 
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
 
		return_cmd_error(error);
 
	}
 
@@ -253,25 +253,25 @@ void ClearSlot(Vehicle *v)
 
	DEBUG(ms, 3) ("Multistop: Clearing slot at 0x%X", rs->xy);
 
}
 

	
 
/** Sell a road vehicle.
 
 * @param tile unused
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
int32 CmdSellRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 

	
 
	if (!IsRoadVehInDepotStopped(v)) {
 
		return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
@@ -355,25 +355,25 @@ static const Depot* FindClosestRoadDepot
 
}
 

	
 
/** Send a road vehicle to the depot.
 
 * @param tile unused
 
 * @param p1 vehicle ID to send to the depot
 
 * @param p2 unused
 
 */
 
int32 CmdSendRoadVehToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	const Depot *dep;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
 

	
 
	/* If the current orders are already goto-depot */
 
	if (v->current_order.type == OT_GOTO_DEPOT) {
 
		if (flags & DC_EXEC) {
 
			/* If the orders to 'goto depot' are in the orders list (forced servicing),
 
			 * then skip to the next order; effectively cancelling this forced service */
 
@@ -402,25 +402,25 @@ int32 CmdSendRoadVehToDepot(TileIndex ti
 
	return 0;
 
}
 

	
 
/** Turn a roadvehicle around.
 
 * @param tile unused
 
 * @param p1 vehicle ID to turn
 
 * @param p2 unused
 
 */
 
int32 CmdTurnRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->vehstatus & (VS_HIDDEN | VS_STOPPED) ||
 
			v->u.road.crashed_ctr != 0 ||
 
			v->breakdown_ctr != 0 ||
 
			v->u.road.overtaking != 0 ||
 
			v->cur_speed < 5) {
 
		return CMD_ERROR;
 
	}
 
@@ -1748,25 +1748,25 @@ void RoadVehiclesYearlyLoop(void)
 
 * @param p2 Bitstuffed elements
 
 * - p2 = (bit 0-7) - the new cargo type to refit to
 
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
 
 */
 
int32 CmdRefitRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	int32 cost;
 
	CargoID new_cid = GB(p2, 0, 8);
 
	byte new_subtype = GB(p2, 8, 8);
 
	uint16 capacity = CALLBACK_FAILED;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Road || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!IsRoadVehInDepotStopped(v)) return_cmd_error(STR_9013_MUST_BE_STOPPED_INSIDE);
 

	
 
	if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_ROADVEH_RUN);
 

	
 
	if (HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_REFIT_CAPACITY)) {
 
		/* Back up the cargo type */
ship_cmd.c
Show inline comments
 
@@ -920,25 +920,25 @@ int32 CmdBuildShip(TileIndex tile, uint3
 
	return value;
 
}
 

	
 
/** Sell a ship.
 
 * @param tile unused
 
 * @param p1 vehicle ID to be sold
 
 * @param p2 unused
 
 */
 
int32 CmdSellShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 

	
 
	if (!IsShipInDepotStopped(v)) {
 
		return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
@@ -955,25 +955,25 @@ int32 CmdSellShip(TileIndex tile, uint32
 
}
 

	
 
/** Start/Stop a ship.
 
 * @param tile unused
 
 * @param p1 ship ID to start/stop
 
 * @param p2 unused
 
 */
 
int32 CmdStartStopShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	uint16 callback;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* Check if this ship can be started/stopped. The callback will fail or
 
	 * return 0xFF if it can. */
 
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
 
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
 
		return_cmd_error(error);
 
	}
 
@@ -993,25 +993,25 @@ int32 CmdStartStopShip(TileIndex tile, u
 
}
 

	
 
/** Send a ship to the depot.
 
 * @param tile unused
 
 * @param p1 vehicle ID to send to the depot
 
 * @param p2 unused
 
 */
 
int32 CmdSendShipToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	const Depot *dep;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
 

	
 
	/* If the current orders are already goto-depot */
 
	if (v->current_order.type == OT_GOTO_DEPOT) {
 
		if (flags & DC_EXEC) {
 
			/* If the orders to 'goto depot' are in the orders list (forced servicing),
 
			 * then skip to the next order; effectively cancelling this forced service */
 
@@ -1047,25 +1047,25 @@ int32 CmdSendShipToDepot(TileIndex tile,
 
 * @param p2 various bitstuffed elements
 
 * - p2 = (bit 0-7) - the new cargo type to refit to (p2 & 0xFF)
 
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
 
 */
 
int32 CmdRefitShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	int32 cost;
 
	CargoID new_cid = GB(p2, 0, 8); //gets the cargo number
 
	byte new_subtype = GB(p2, 8, 8);
 
	uint16 capacity = CALLBACK_FAILED;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Ship || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (!IsShipInDepotStopped(v)) {
 
		return_cmd_error(STR_980B_SHIP_MUST_BE_STOPPED_IN);
 
	}
 

	
 
	/* Check cargo */
 
	if (!ShipVehInfo(v->engine_type)->refittable) return CMD_ERROR;
 
	if (new_cid > NUM_CARGO || !CanRefitTo(v->engine_type, new_cid)) return CMD_ERROR;
signs.c
Show inline comments
 
@@ -138,25 +138,25 @@ int32 CmdPlaceSign(TileIndex tile, uint3
 
	return 0;
 
}
 

	
 
/** Rename a sign. If the new name of the sign is empty, we assume
 
 * the user wanted to delete it. So delete it. Ownership of signs
 
 * has no meaning/effect whatsoever except for eyecandy
 
 * @param tile unused
 
 * @param p1 index of the sign to be renamed/removed
 
 * @param p2 unused
 
 */
 
int32 CmdRenameSign(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	if (!IsSignIndex(p1)) return CMD_ERROR;
 
	if (!IsValidSignID(p1)) return CMD_ERROR;
 

	
 
	/* If _cmd_text 0 means the new text for the sign is non-empty.
 
	 * So rename the sign. If it is empty, it has no name, so delete it */
 
	if (_cmd_text[0] != '\0') {
 
		/* Create the name */
 
		StringID str = AllocateName(_cmd_text, 0);
 
		if (str == 0) return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) {
 
			Sign *si = GetSign(p1);
 

	
 
			/* Delete the old name */
signs.h
Show inline comments
 
@@ -25,37 +25,37 @@ static inline Sign *GetSign(SignID index
 
{
 
	return (Sign *)GetItemFromPool(&_sign_pool, index);
 
}
 

	
 
/**
 
 * Get the current size of the SignPool
 
 */
 
static inline uint16 GetSignPoolSize(void)
 
{
 
	return _sign_pool.total_items;
 
}
 

	
 
static inline bool IsSignIndex(uint index)
 
{
 
	return index < GetSignPoolSize();
 
}
 

	
 
/**
 
 * Check if a Sign really exists.
 
 */
 
static inline bool IsValidSign(const Sign *si)
 
{
 
	return si->str != STR_NULL;
 
}
 

	
 
static inline bool IsValidSignID(uint index)
 
{
 
	return index < GetSignPoolSize() && IsValidSign(GetSign(index));
 
}
 

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

	
 
VARDEF bool _sign_sort_dirty;
 
VARDEF SignID *_sign_sort;
 

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

	
 
/* misc.c */
 
void ShowRenameSignWindow(const Sign *si);
 

	
station.h
Show inline comments
 
@@ -152,37 +152,37 @@ static inline Station *GetStation(Statio
 
{
 
	return (Station*)GetItemFromPool(&_station_pool, index);
 
}
 

	
 
/**
 
 * Get the current size of the StationPool
 
 */
 
static inline uint16 GetStationPoolSize(void)
 
{
 
	return _station_pool.total_items;
 
}
 

	
 
static inline bool IsStationIndex(StationID index)
 
{
 
	return index < GetStationPoolSize();
 
}
 

	
 
/**
 
 * Check if a station really exists.
 
 */
 
static inline bool IsValidStation(const Station *st)
 
{
 
	return st->xy != 0;
 
}
 

	
 
static inline bool IsValidStationID(StationID index)
 
{
 
	return index < GetStationPoolSize() && IsValidStation(GetStation(index));
 
}
 

	
 
#define FOR_ALL_STATIONS_FROM(st, start) for (st = GetStation(start); st != NULL; st = (st->index + 1 < GetStationPoolSize()) ? GetStation(st->index + 1) : NULL) if (IsValidStation(st))
 
#define FOR_ALL_STATIONS(st) FOR_ALL_STATIONS_FROM(st, 0)
 

	
 

	
 
/* Stuff for ROADSTOPS */
 

	
 
extern MemoryPool _roadstop_pool;
 

	
 
/**
 
 * Get the pointer to the roadstop with index 'index'
 
 */
 
static inline RoadStop *GetRoadStop(uint index)
station_cmd.c
Show inline comments
 
@@ -2564,26 +2564,25 @@ static void StationHandleSmallTick(Stati
 
}
 

	
 
void OnTick_Station(void)
 
{
 
	uint i;
 
	Station *st;
 

	
 
	if (_game_mode == GM_EDITOR) return;
 

	
 
	i = _station_tick_ctr;
 
	if (++_station_tick_ctr == GetStationPoolSize()) _station_tick_ctr = 0;
 

	
 
	st = GetStation(i);
 
	if (IsValidStation(st)) StationHandleBigTick(st);
 
	if (IsValidStationID(i)) StationHandleBigTick(GetStation(i));
 

	
 
	FOR_ALL_STATIONS(st) {
 
		StationHandleSmallTick(st);
 
	}
 
}
 

	
 
void StationMonthlyLoop(void)
 
{
 
}
 

	
 

	
 
void ModifyStationRatingAround(TileIndex tile, PlayerID owner, int amount, uint radius)
 
@@ -2618,28 +2617,28 @@ static void UpdateStationWaiting(Station
 
}
 

	
 
/** Rename a station
 
 * @param tile unused
 
 * @param p1 station ID that is to be renamed
 
 * @param p2 unused
 
 */
 
int32 CmdRenameStation(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	StringID str;
 
	Station *st;
 

	
 
	if (!IsStationIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
 
	if (!IsValidStationID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
 
	st = GetStation(p1);
 

	
 
	if (!IsValidStation(st) || !CheckOwnership(st->owner)) return CMD_ERROR;
 
	if (!CheckOwnership(st->owner)) return CMD_ERROR;
 

	
 
	str = AllocateNameUnique(_cmd_text, 6);
 
	if (str == 0) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		StringID old_str = st->string_id;
 

	
 
		st->string_id = str;
 
		UpdateStationVirtCoord(st);
 
		DeleteName(old_str);
 
		ResortStationLists();
 
		MarkWholeScreenDirty();
town.h
Show inline comments
 
@@ -170,27 +170,27 @@ static inline Town *GetTown(uint index)
 
{
 
	return (Town*)GetItemFromPool(&_town_pool, index);
 
}
 

	
 
/**
 
 * Get the current size of the TownPool
 
 */
 
static inline uint16 GetTownPoolSize(void)
 
{
 
	return _town_pool.total_items;
 
}
 

	
 
static inline bool IsTownIndex(uint index)
 
static inline bool IsValidTownID(uint index)
 
{
 
	return index < GetTownPoolSize();
 
	return index < GetTownPoolSize() && IsValidTown(GetTown(index));
 
}
 

	
 
#define FOR_ALL_TOWNS_FROM(t, start) for (t = GetTown(start); t != NULL; t = (t->index + 1 < GetTownPoolSize()) ? GetTown(t->index + 1) : NULL) if (IsValidTown(t))
 
#define FOR_ALL_TOWNS(t) FOR_ALL_TOWNS_FROM(t, 0)
 

	
 
VARDEF uint _total_towns; // For the AI: the amount of towns active
 

	
 
VARDEF bool _town_sort_dirty;
 
VARDEF byte _town_sort_order;
 

	
 
VARDEF Town *_cleared_town;
 
VARDEF int _cleared_town_rating;
town_cmd.c
Show inline comments
 
@@ -400,32 +400,29 @@ static void TownTickHandler(Town *t)
 
}
 

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

	
 
	/* Make sure each town's tickhandler invocation frequency is about the
 
	 * same - TOWN_GROWTH_FREQUENCY - independent on the number of towns. */
 
	for (_cur_town_iter += GetTownPoolSize();
 
	     _cur_town_iter >= TOWN_GROWTH_FREQUENCY;
 
	     _cur_town_iter -= TOWN_GROWTH_FREQUENCY) {
 
		uint32 i = _cur_town_ctr;
 
		Town *t;
 

	
 
		if (++_cur_town_ctr >= GetTownPoolSize())
 
			_cur_town_ctr = 0;
 

	
 
		t = GetTown(i);
 

	
 
		if (IsValidTown(t)) TownTickHandler(t);
 
		if (IsValidTownID(i)) TownTickHandler(GetTown(i));
 
	}
 
}
 

	
 
static RoadBits GetTownRoadMask(TileIndex tile)
 
{
 
	TrackBits b = GetAnyRoadTrackBits(tile);
 
	RoadBits r = 0;
 

	
 
	if (b & TRACK_BIT_X)     r |= ROAD_X;
 
	if (b & TRACK_BIT_Y)     r |= ROAD_Y;
 
	if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW;
 
	if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW;
 
@@ -1339,25 +1336,25 @@ static void ClearTownHouse(Town *t, Tile
 
}
 

	
 
/** Rename a town (server-only).
 
 * @param tile unused
 
 * @param p1 town ID to rename
 
 * @param p2 unused
 
 */
 
int32 CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	StringID str;
 
	Town *t;
 

	
 
	if (!IsTownIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
 
	if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
 

	
 
	t = GetTown(p1);
 

	
 
	str = AllocateNameUnique(_cmd_text, 4);
 
	if (str == 0) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		DeleteName(t->townnametype);
 
		t->townnametype = str;
 

	
 
		UpdateTownVirtCoord(t);
 
		_town_sort_dirty = true;
 
@@ -1596,25 +1593,25 @@ extern uint GetMaskOfTownActions(int *nu
 
/** Do a town action.
 
 * This performs an action such as advertising, building a statue, funding buildings,
 
 * but also bribing the town-council
 
 * @param tile unused
 
 * @param p1 town to do the action at
 
 * @param p2 action to perform, @see _town_action_proc for the list of available actions
 
 */
 
int32 CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	int32 cost;
 
	Town *t;
 

	
 
	if (!IsTownIndex(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
 
	if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR;
 

	
 
	t = GetTown(p1);
 

	
 
	if (!HASBIT(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_OTHER);
 

	
 
	cost = (_price.build_industry >> 8) * _town_action_costs[p2];
 

	
 
	if (flags & DC_EXEC) {
 
		_town_action_proc[p2](t);
 
		InvalidateWindow(WC_TOWN_AUTHORITY, p1);
train_cmd.c
Show inline comments
 
@@ -942,25 +942,25 @@ static void NormaliseTrainConsist(Vehicl
 
 * @param tile unused
 
 * @param p1 various bitstuffed elements
 
 * - p1 (bit  0 - 15) source vehicle index
 
 * - p1 (bit 16 - 31) what wagon to put the source wagon AFTER, XXX - INVALID_VEHICLE to make a new line
 
 * @param p2 (bit 0) move all vehicles following the source vehicle
 
 */
 
int32 CmdMoveRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	VehicleID s = GB(p1, 0, 16);
 
	VehicleID d = GB(p1, 16, 16);
 
	Vehicle *src, *dst, *src_head, *dst_head;
 

	
 
	if (!IsVehicleIndex(s)) return CMD_ERROR;
 
	if (!IsValidVehicleID(s)) return CMD_ERROR;
 

	
 
	src = GetVehicle(s);
 

	
 
	if (src->type != VEH_Train) return CMD_ERROR;
 

	
 
	// if nothing is selected as destination, try and find a matching vehicle to drag to.
 
	if (d == INVALID_VEHICLE) {
 
		dst = IsTrainEngine(src) ? NULL : FindGoodVehiclePos(src);
 
	} else {
 
		dst = GetVehicle(d);
 
	}
 

	
 
@@ -1221,25 +1221,25 @@ int32 CmdMoveRailVehicle(TileIndex tile,
 
}
 

	
 
/** Start/Stop a train.
 
 * @param tile unused
 
 * @param p1 train to start/stop
 
 * @param p2 unused
 
 */
 
int32 CmdStartStopTrain(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	uint16 callback;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* Check if this train can be started/stopped. The callback will fail or
 
	 * return 0xFF if it can. */
 
	callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
 
	if (callback != CALLBACK_FAILED && callback != 0xFF) {
 
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
 
		return_cmd_error(error);
 
	}
 
@@ -1266,25 +1266,25 @@ int32 CmdStartStopTrain(TileIndex tile, 
 
 * - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
 
 * - p2 = 1: sell the vehicle and all vehicles following it in the chain
 
             if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
 
 * - p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines;
 
 *           all wagons of the same type will go on the same line. Used by the AI currently
 
 */
 
int32 CmdSellRailWagon(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v, *tmp, *first;
 
	Vehicle *new_f = NULL;
 
	int32 cost = 0;
 

	
 
	if (!IsVehicleIndex(p1) || p2 > 2) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1) || p2 > 2) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 

	
 
	while (IsArticulatedPart(v)) v = GetPrevVehicleInChain(v);
 
	first = GetFirstVehicleInChain(v);
 

	
 
	// make sure the vehicle is stopped in the depot
 
	if (CheckTrainStoppedInDepot(first) < 0) {
 
@@ -1662,25 +1662,25 @@ static void ReverseTrainDirection(Vehicl
 
	CLRBIT(v->u.rail.flags, VRF_REVERSING);
 
}
 

	
 
/** Reverse train.
 
 * @param tile unused
 
 * @param p1 train to reverse
 
 * @param p2 if true, reverse a unit in a train (needs to be in a depot)
 
 */
 
int32 CmdReverseTrainDirection(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (p2) {
 
		// turn a single unit around
 
		Vehicle *front;
 

	
 
		if (IsMultiheaded(v) || HASBIT(EngInfo(v->engine_type)->callbackmask, CBM_ARTIC_ENGINE)) {
 
			return_cmd_error(STR_ONLY_TURN_SINGLE_UNIT);
 
		}
 
@@ -1711,25 +1711,25 @@ int32 CmdReverseTrainDirection(TileIndex
 
	return 0;
 
}
 

	
 
/** Force a train through a red signal
 
 * @param tile unused
 
 * @param p1 train to ignore the red signal
 
 * @param p2 unused
 
 */
 
int32 CmdForceTrainProceed(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) v->u.rail.force_proceed = 0x50;
 

	
 
	return 0;
 
}
 

	
 
/** Refits a train to the specified cargo type.
 
 * @param tile unused
 
@@ -1737,25 +1737,25 @@ int32 CmdForceTrainProceed(TileIndex til
 
 * param p2 various bitstuffed elements
 
 * - p2 = (bit 0-7) - the new cargo type to refit to
 
 * - p2 = (bit 8-15) - the new cargo subtype to refit to
 
 */
 
int32 CmdRefitRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	CargoID new_cid = GB(p2, 0, 8);
 
	byte new_subtype = GB(p2, 8, 8);
 
	Vehicle *v;
 
	int32 cost;
 
	uint num;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
 

	
 
	/* Check cargo */
 
	if (new_cid > NUM_CARGO) return CMD_ERROR;
 

	
 
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
 

	
 
	cost = 0;
 
@@ -1921,25 +1921,25 @@ static TrainFindDepotData FindClosestTra
 
}
 

	
 
/** Send a train to a depot
 
 * @param tile unused
 
 * @param p1 train to send to the depot
 
 * @param p2 unused
 
 */
 
int32 CmdSendTrainToDepot(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	TrainFindDepotData tfdd;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
 

	
 
	if (v->current_order.type == OT_GOTO_DEPOT) {
 
		if (flags & DC_EXEC) {
 
			if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
 
				v->u.rail.days_since_order_progr = 0;
 
				v->cur_order_index++;
vehicle.c
Show inline comments
 
@@ -1517,25 +1517,25 @@ void AgeVehicle(Vehicle *v)
 
/** Clone a vehicle. If it is a train, it will clone all the cars too
 
* @param tile tile of the depot where the cloned vehicle is build
 
* @param p1 the original vehicle's index
 
* @param p2 1 = shared orders, else copied orders
 
*/
 
int32 CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v_front, *v;
 
	Vehicle *w_front, *w, *w_rear;
 
	int cost, total_cost = 0;
 
	uint32 build_argument = 2;
 

	
 
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
 
	v = GetVehicle(p1);
 
	v_front = v;
 
	w = NULL;
 
	w_front = NULL;
 
	w_rear = NULL;
 

	
 

	
 
	/*
 
	 * v_front is the front engine in the original vehicle
 
	 * v is the car/vehicle of the original vehicle, that is currently being copied
 
	 * w_front is the front engine of the cloned vehicle
 
	 * w is the car/vehicle currently being cloned
 
@@ -1898,25 +1898,25 @@ static void MaybeReplaceVehicle(Vehicle 
 

	
 

	
 
/** Give a custom name to your vehicle
 
 * @param tile unused
 
 * @param p1 vehicle ID to name
 
 * @param p2 unused
 
 */
 
int32 CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle *v;
 
	StringID str;
 

	
 
	if (!IsVehicleIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
 
	if (!IsValidVehicleID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	str = AllocateNameUnique(_cmd_text, 2);
 
	if (str == 0) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		StringID old_str = v->string_id;
 
		v->string_id = str;
 
		DeleteName(old_str);
 
@@ -1931,29 +1931,29 @@ int32 CmdNameVehicle(TileIndex tile, uin
 

	
 

	
 
/** Change the service interval of a vehicle
 
 * @param tile unused
 
 * @param p1 vehicle ID that is being service-interval-changed
 
 * @param p2 new service interval
 
 */
 
int32 CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Vehicle* v;
 
	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
 

	
 
	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
 
	if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;
 

	
 
	v = GetVehicle(p1);
 

	
 
	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
 
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		v->service_interval = serv_int;
 
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
 
	}
 

	
 
	return 0;
 
}
 

	
 

	
 
static Rect _old_vehicle_coords;
 

	
vehicle.h
Show inline comments
 
@@ -366,27 +366,27 @@ static inline bool IsValidVehicle(const 
 
{
 
	return v->type != 0;
 
}
 

	
 
#define FOR_ALL_VEHICLES_FROM(v, start) for (v = GetVehicle(start); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) if (IsValidVehicle(v))
 
#define FOR_ALL_VEHICLES(v) FOR_ALL_VEHICLES_FROM(v, 0)
 

	
 
/**
 
 * Check if an index is a vehicle-index (so between 0 and max-vehicles)
 
 *
 
 * @return Returns true if the vehicle-id is in range
 
 */
 
static inline bool IsVehicleIndex(uint index)
 
static inline bool IsValidVehicleID(uint index)
 
{
 
	return index < GetVehiclePoolSize();
 
	return index < GetVehiclePoolSize() && IsValidVehicle(GetVehicle(index));
 
}
 

	
 
/* Returns order 'index' of a vehicle or NULL when it doesn't exists */
 
static inline Order *GetVehicleOrder(const Vehicle *v, int index)
 
{
 
	Order *order = v->orders;
 

	
 
	if (index < 0) return NULL;
 

	
 
	while (order != NULL && index-- > 0)
 
		order = order->next;
 

	
waypoint.c
Show inline comments
 
@@ -310,25 +310,25 @@ int32 CmdRemoveTrainWaypoint(TileIndex t
 
	return RemoveTrainWaypoint(tile, flags, true);
 
}
 

	
 
/** Rename a waypoint.
 
 * @param tile unused
 
 * @param p1 id of waypoint
 
 * @param p2 unused
 
 */
 
int32 CmdRenameWaypoint(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 
{
 
	Waypoint *wp;
 

	
 
	if (!IsWaypointIndex(p1)) return CMD_ERROR;
 
	if (!IsValidWaypointID(p1)) return CMD_ERROR;
 

	
 
	if (_cmd_text[0] != '\0') {
 
		StringID str = AllocateNameUnique(_cmd_text, 0);
 

	
 
		if (str == 0) return CMD_ERROR;
 

	
 
		if (flags & DC_EXEC) {
 
			wp = GetWaypoint(p1);
 
			if (wp->string != STR_NULL) DeleteName(wp->string);
 

	
 
			wp->string = str;
 
			wp->town_cn = 0;
waypoint.h
Show inline comments
 
@@ -33,37 +33,37 @@ static inline Waypoint *GetWaypoint(uint
 
{
 
	return (Waypoint*)GetItemFromPool(&_waypoint_pool, index);
 
}
 

	
 
/**
 
 * Get the current size of the WaypointPool
 
 */
 
static inline uint16 GetWaypointPoolSize(void)
 
{
 
	return _waypoint_pool.total_items;
 
}
 

	
 
static inline bool IsWaypointIndex(uint index)
 
{
 
	return index < GetWaypointPoolSize();
 
}
 

	
 
/**
 
 * Check if a Waypoint really exists.
 
 */
 
static inline bool IsValidWaypoint(const Waypoint *wp)
 
{
 
	return wp->xy != 0;
 
}
 

	
 
static inline bool IsValidWaypointID(uint index)
 
{
 
	return index < GetWaypointPoolSize() && IsValidWaypoint(GetWaypoint(index));
 
}
 

	
 
#define FOR_ALL_WAYPOINTS_FROM(wp, start) for (wp = GetWaypoint(start); wp != NULL; wp = (wp->index + 1 < GetWaypointPoolSize()) ? GetWaypoint(wp->index + 1) : NULL) if (IsValidWaypoint(wp))
 
#define FOR_ALL_WAYPOINTS(wp) FOR_ALL_WAYPOINTS_FROM(wp, 0)
 

	
 

	
 
/**
 
 * Fetch a waypoint by tile
 
 * @param tile Tile of waypoint
 
 * @return Waypoint
 
 */
 
static inline Waypoint *GetWaypointByTile(TileIndex tile)
 
{
 
	assert(IsTileType(tile, MP_RAILWAY) && IsRailWaypoint(tile));
0 comments (0 inline, 0 general)