Changeset - r1718:0c13f342c6f1
[Not reviewed]
master
0 4 0
tron - 19 years ago 2005-04-22 05:41:09
tron@openttd.org
(svn r2222) Check the parameters of Cmd{Insert,Delete,Modify,Skip}Order() and CmdRestoreOrderIndex():
- Check if the vehicle exists
- Check if the vehicle belongs to the correct player
- Check if the new order is valid (type, destination, flags) (CmdInsertOrder)
4 files changed with 183 insertions and 8 deletions:
0 comments (0 inline, 0 general)
depot.h
Show inline comments
 
@@ -25,12 +25,17 @@ static inline Depot *GetDepot(uint index
 
 */
 
static inline uint16 GetDepotPoolSize(void)
 
{
 
	return _depot_pool.total_items;
 
}
 

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

	
 
#define FOR_ALL_DEPOTS_FROM(d, start) for (d = GetDepot(start); d != NULL; d = (d->index + 1 < GetDepotPoolSize()) ? GetDepot(d->index + 1) : NULL)
 
#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
 
@@ -41,13 +46,13 @@ VARDEF TileIndex _last_built_road_depot_
 
VARDEF TileIndex _last_built_aircraft_depot_tile;
 
VARDEF TileIndex _last_built_ship_depot_tile;
 

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

	
 
/**
 
 * Check if a tile is a depot of the given type.
order_cmd.c
Show inline comments
 
#include "stdafx.h"
 
#include "ttd.h"
 
#include "airport.h"
 
#include "depot.h"
 
#include "table/strings.h"
 
#include "vehicle.h"
 
#include "waypoint.h"
 
#include "command.h"
 
#include "station.h"
 
#include "player.h"
 
#include "news.h"
 
#include "saveload.h"
 

	
 
@@ -144,16 +147,152 @@ void AssignOrder(Order *order, Order dat
 
 *                       If the lastone is given, order will be inserted above thatone
 
 * @param packed_order Packed order to insert
 
 *
 
 */
 
int32 CmdInsertOrder(int x, int y, uint32 flags, uint32 veh_sel, uint32 packed_order)
 
{
 
	Vehicle *v      = GetVehicle(veh_sel & 0xFFFF);
 
	Vehicle *v;
 
	int sel         = veh_sel >> 16;
 
	Order new_order = UnpackOrder(packed_order);
 

	
 
	if (!IsVehicleIndex(veh_sel & 0xFFFF)) return CMD_ERROR;
 
	v = GetVehicle(veh_sel & 0xFFFF);
 
	if (v->type == 0 || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	switch (new_order.type) {
 
		case OT_GOTO_STATION: {
 
			const Station* st;
 

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

	
 
			if (!IsValidStation(st) ||
 
					(st->airport_type != AT_OILRIG && !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 {
 
						if (!(st->facilities & FACIL_TRUCK_STOP)) return CMD_ERROR;
 
					}
 
					break;
 

	
 
				case VEH_Ship:
 
					if (!(st->facilities & FACIL_DOCK)) return CMD_ERROR;
 
					break;
 

	
 
				case VEH_Aircraft:
 
					if (!(st->facilities & FACIL_AIRPORT)) return CMD_ERROR;
 
					break;
 

	
 
				default:
 
					return CMD_ERROR;
 
			}
 

	
 
			switch (new_order.flags) {
 
				case 0:
 
				case OF_FULL_LOAD:
 
				case OF_UNLOAD:
 
				case OF_NON_STOP:
 
				case OF_NON_STOP | OF_FULL_LOAD:
 
				case OF_NON_STOP | OF_UNLOAD:
 
					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;
 
				st = GetStation(new_order.station);
 

	
 
				if (!IsValidStation(st) ||
 
						(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;
 
				dp = GetDepot(new_order.station);
 

	
 
				if (!IsValidDepot(dp) ||
 
						!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;
 
						break;
 

	
 
					default:
 
						return CMD_ERROR;
 
				}
 
			}
 

	
 
			switch (new_order.flags) {
 
				case OF_PART_OF_ORDERS:
 
				case OF_PART_OF_ORDERS | OF_HALT_IN_DEPOT:
 
				case OF_NON_STOP | OF_PART_OF_ORDERS:
 
				case OF_NON_STOP | OF_PART_OF_ORDERS | OF_HALT_IN_DEPOT:
 
					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;
 
			wp = GetWaypoint(new_order.station);
 

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

	
 
			switch (new_order.flags) {
 
				case 0:
 
				case OF_NON_STOP:
 
					break;
 

	
 
				default:
 
					return CMD_ERROR;
 
			}
 
			break;
 
		}
 

	
 
		default:
 
			return CMD_ERROR;
 
	}
 

	
 
	if (sel > v->num_orders)
 
		return_cmd_error(STR_EMPTY);
 

	
 
	if (IsOrderPoolFull())
 
		return_cmd_error(STR_8831_NO_MORE_SPACE_FOR_ORDERS);
 

	
 
@@ -167,13 +306,13 @@ int32 CmdInsertOrder(int x, int y, uint3
 
	if (v->type == VEH_Ship && IS_HUMAN_PLAYER(v->owner) &&
 
		sel != 0 && GetVehicleOrder(v, sel - 1)->type == OT_GOTO_STATION
 
		&& !_patches.new_pathfinding_all) {
 

	
 
		int dist = DistanceManhattan(
 
			GetStation(GetVehicleOrder(v, sel - 1)->station)->xy,
 
			GetStation(new_order.station)->xy
 
			GetStation(new_order.station)->xy // XXX type != OT_GOTO_STATION?
 
		);
 
		if (dist >= 130)
 
			return_cmd_error(STR_0210_TOO_FAR_FROM_PREVIOUS_DESTINATIO);
 
	}
 

	
 
	if (flags & DC_EXEC) {
 
@@ -266,16 +405,21 @@ static int32 DecloneOrder(Vehicle *dst, 
 
 * @param vehicle_id The ID of the vehicle
 
 * @param selected   The order to delete
 
 *
 
 */
 
int32 CmdDeleteOrder(int x, int y, uint32 flags, uint32 vehicle_id, uint32 selected)
 
{
 
	Vehicle *v = GetVehicle(vehicle_id), *u;
 
	Vehicle *v;
 
	Vehicle *u;
 
	uint sel   = selected;
 
	Order *order;
 

	
 
	if (!IsVehicleIndex(vehicle_id)) return CMD_ERROR;
 
	v = GetVehicle(vehicle_id);
 
	if (v->type == 0 || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	/* XXX -- Why is this here? :s */
 
	_error_message = STR_EMPTY;
 

	
 
	/* If we did not select an order, we maybe want to de-clone the orders */
 
	if (sel >= v->num_orders)
 
		return DecloneOrder(v, flags);
 
@@ -344,13 +488,17 @@ int32 CmdDeleteOrder(int x, int y, uint3
 
 *
 
 * @param vehicle_id The ID of the vehicle
 
 *
 
 */
 
int32 CmdSkipOrder(int x, int y, uint32 flags, uint32 vehicle_id, uint32 not_used)
 
{
 
	Vehicle *v = GetVehicle(vehicle_id);
 
	Vehicle *v;
 

	
 
	if (!IsVehicleIndex(vehicle_id)) return CMD_ERROR;
 
	v = GetVehicle(vehicle_id);
 
	if (v->type == 0 || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		/* Goto next order */
 
		{
 
			byte b = v->cur_order_index + 1;
 
			if (b >= v->num_orders)
 
@@ -391,16 +539,20 @@ int32 CmdSkipOrder(int x, int y, uint32 
 
 *                       If the lastone is given, order will be inserted above thatone
 
 * @param mode         Mode to change the order to
 
 *
 
 */
 
int32 CmdModifyOrder(int x, int y, uint32 flags, uint32 veh_sel, uint32 mode)
 
{
 
	Vehicle *v = GetVehicle(veh_sel & 0xFFFF);
 
	Vehicle *v;
 
	byte sel = veh_sel >> 16;
 
	Order *order;
 

	
 
	if (!IsVehicleIndex(veh_sel & 0xFFFF)) return CMD_ERROR;
 
	v = GetVehicle(veh_sel & 0xFFFF);
 
	if (v->type == 0 || !CheckOwnership(v->owner)) return CMD_ERROR;
 

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

	
 
	order = GetVehicleOrder(v, sel);
 
	if (order->type != OT_GOTO_STATION &&
 
@@ -419,12 +571,15 @@ int32 CmdModifyOrder(int x, int y, uint3
 
			TOGGLEBIT(order->flags, OFB_UNLOAD);
 
			CLRBIT(order->flags, OFB_FULL_LOAD);
 
			break;
 
		case OFB_NON_STOP:
 
			TOGGLEBIT(order->flags, OFB_NON_STOP);
 
			break;
 

	
 
			default:
 
				return CMD_ERROR;
 
		}
 

	
 
		/* Update the windows, also for vehicles that share the same order list */
 
		{
 
			Vehicle *u = GetFirstVehicleFromSharedList(v);
 
			while (u != NULL) {
 
@@ -650,14 +805,19 @@ void RestoreVehicleOrders(Vehicle *v, Ba
 
 * @param data       First 16 bits are the current-order-index
 
 *                   The last 16 bits are the service-interval
 
 *
 
 */
 
int32 CmdRestoreOrderIndex(int x, int y, uint32 flags, uint32 vehicle_id, uint32 data)
 
{
 
	Vehicle* v;
 

	
 
	if (!IsVehicleIndex(vehicle_id)) return CMD_ERROR;
 
	v = GetVehicle(vehicle_id);
 
	if (v->type == 0 || !CheckOwnership(v->owner)) return CMD_ERROR;
 

	
 
	if (flags & DC_EXEC) {
 
		Vehicle *v = GetVehicle(vehicle_id);
 
		v->service_interval = data >> 16;
 
		v->cur_order_index = data & 0xFFFF;
 
	}
 

	
 
	return 0;
 
}
station.h
Show inline comments
 
@@ -143,12 +143,17 @@ static inline Station *GetStation(Statio
 
 */
 
static inline uint16 GetStationPoolSize(void)
 
{
 
	return _station_pool.total_items;
 
}
 

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

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

	
 

	
 
/* Stuff for ROADSTOPS */
 

	
 
@@ -289,13 +294,13 @@ static inline bool IsRoadStationTile(uin
 
	return IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0x43, 0x4B);
 
}
 

	
 
/**
 
 * Check if a station really exists.
 
 */
 
static inline bool IsValidStation(Station* station)
 
static inline bool IsValidStation(const Station* station)
 
{
 
	return station->xy != 0; /* XXX: Replace by INVALID_TILE someday */
 
}
 

	
 
/* Get's the direction the station exit points towards. Ie, returns 0 for a
 
 * station with the exit NE. */
waypoint.h
Show inline comments
 
@@ -37,12 +37,17 @@ static inline Waypoint *GetWaypoint(uint
 
 */
 
static inline uint16 GetWaypointPoolSize(void)
 
{
 
	return _waypoint_pool.total_items;
 
}
 

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

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

	
 
static inline bool IsRailWaypoint(byte m5)
 
{
 
	return (m5 & 0xFC) == 0xC4;
0 comments (0 inline, 0 general)