Changeset - r5792:292ddd37504e
[Not reviewed]
master
0 3 0
bjarni - 17 years ago 2007-01-22 16:16:52
bjarni@openttd.org
(svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
3 files changed with 59 insertions and 25 deletions:
0 comments (0 inline, 0 general)
src/vehicle.cpp
Show inline comments
 
@@ -32,49 +32,40 @@
 
#include "water_map.h"
 
#include "network/network.h"
 
#include "yapf/yapf.h"
 
#include "date.h"
 
#include "newgrf_callbacks.h"
 
#include "newgrf_engine.h"
 
#include "newgrf_sound.h"
 
#include "helpers.hpp"
 

	
 
#define INVALID_COORD (-0x8000)
 
#define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
 

	
 
/*
 
 * These command macros are used to call vehicle type specific commands with non type specific commands
 
 * it should be used like: DoCommandP(x, y, p1, p2, flags, CMD_STARTSTOP_VEH(v->type))
 
 * that line will start/stop a vehicle nomatter what type it is
 
 * VEH_Train is used as an offset because the vehicle type values doesn't start with 0
 
 */
 

	
 
#define CMD_BUILD_VEH(x) _veh_build_proc_table[ x - VEH_Train]
 
#define CMD_SELL_VEH(x)  _veh_sell_proc_table [ x - VEH_Train]
 
#define CMD_REFIT_VEH(x) _veh_refit_proc_table[ x - VEH_Train]
 

	
 
static const uint32 _veh_build_proc_table[] = {
 

	
 
/* Tables used in vehicle.h to find the right command for a certain vehicle type */
 
const uint32 _veh_build_proc_table[] = {
 
	CMD_BUILD_RAIL_VEHICLE,
 
	CMD_BUILD_ROAD_VEH,
 
	CMD_BUILD_SHIP,
 
	CMD_BUILD_AIRCRAFT,
 
};
 
static const uint32 _veh_sell_proc_table[] = {
 
const uint32 _veh_sell_proc_table[] = {
 
	CMD_SELL_RAIL_WAGON,
 
	CMD_SELL_ROAD_VEH,
 
	CMD_SELL_SHIP,
 
	CMD_SELL_AIRCRAFT,
 
};
 

	
 
static const uint32 _veh_refit_proc_table[] = {
 
const uint32 _veh_refit_proc_table[] = {
 
	CMD_REFIT_RAIL_VEHICLE,
 
	CMD_REFIT_ROAD_VEH,
 
	CMD_REFIT_SHIP,
 
	CMD_REFIT_AIRCRAFT,
 
};
 

	
 
const uint32 _send_to_depot_proc_table[] = {
 
	CMD_SEND_TRAIN_TO_DEPOT,
 
	CMD_SEND_ROADVEH_TO_DEPOT,
 
	CMD_SEND_SHIP_TO_DEPOT,
 
	CMD_SEND_AIRCRAFT_TO_HANGAR,
 
};
 
@@ -1820,38 +1811,38 @@ int32 CmdCloneVehicle(TileIndex tile, ui
 
		}
 
	}
 

	
 
	v = v_front;
 

	
 
	do {
 

	
 
		if (IsMultiheaded(v) && !IsTrainEngine(v)) {
 
			/* we build the rear ends of multiheaded trains with the front ones */
 
			continue;
 
		}
 

	
 
		cost = DoCommand(tile, v->engine_type, build_argument, flags, CMD_BUILD_VEH(v->type));
 
		cost = DoCommand(tile, v->engine_type, build_argument, flags, GetCmdBuildVeh(v));
 
		build_argument = 3; // ensure that we only assign a number to the first engine
 

	
 
		if (CmdFailed(cost)) return cost;
 

	
 
		total_cost += cost;
 

	
 
		if (flags & DC_EXEC) {
 
			w = GetVehicle(_new_vehicle_id);
 

	
 
			if (v->cargo_type != w->cargo_type || v->cargo_subtype != w->cargo_subtype) {
 
				// we can't pay for refitting because we can't estimate refitting costs for a vehicle before it's build
 
				// if we pay for it anyway, the cost and the estimated cost will not be the same and we will have an assert
 
				DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8), flags, CMD_REFIT_VEH(v->type));
 
				DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8), flags, GetCmdRefitVeh(v));
 
			}
 
			if (v->type == VEH_Train && HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
 
				SETBIT(w->u.rail.flags, VRF_REVERSE_DIRECTION);
 
			}
 

	
 
			if (v->type == VEH_Train && !IsFrontEngine(v)) {
 
				// this s a train car
 
				// add this unit to the end of the train
 
				DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
 
			} else {
 
				// this is a front engine or not a train. It need orders
 
				w_front = w;
 
@@ -2015,48 +2006,48 @@ static int32 ReplaceVehicle(Vehicle **w,
 
	Vehicle *new_v = NULL;
 
	char vehicle_name[32];
 
	CargoID replacement_cargo_type;
 

	
 
	new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type);
 
	if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
 

	
 
	replacement_cargo_type = GetNewCargoTypeForReplace(old_v, new_engine_type);
 

	
 
	/* check if we can't refit to the needed type, so no replace takes place to prevent the vehicle from altering cargo type */
 
	if (replacement_cargo_type == CT_INVALID) return 0;
 

	
 
	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, CMD_SELL_VEH(old_v->type));
 
	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
 

	
 
	/* We give the player a loan of the same amount as the sell value.
 
	 * This is needed in case he needs the income from the sale to build the new vehicle.
 
	 * We take it back if building fails or when we really sell the old engine */
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 
	SubtractMoneyFromPlayer(sell_value);
 

	
 
	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, CMD_BUILD_VEH(old_v->type));
 
	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v));
 
	if (CmdFailed(cost)) {
 
		SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 
		SubtractMoneyFromPlayer(-sell_value); // Take back the money we just gave the player
 
		return cost;
 
	}
 

	
 
	if (replacement_cargo_type != CT_NO_REFIT) cost += GetRefitCost(new_engine_type); // add refit cost
 

	
 
	if (flags & DC_EXEC) {
 
		new_v = GetVehicle(_new_vehicle_id);
 
		*w = new_v; //we changed the vehicle, so MaybeReplaceVehicle needs to work on the new one. Now we tell it what the new one is
 

	
 
		/* refit if needed */
 
		if (replacement_cargo_type != CT_NO_REFIT) {
 
			if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, CMD_REFIT_VEH(new_v->type)))) {
 
			if (CmdFailed(DoCommand(0, new_v->index, replacement_cargo_type, DC_EXEC, GetCmdRefitVeh(new_v)))) {
 
				/* Being here shows a failure, which most likely is in GetNewCargoTypeForReplace() or incorrect estimation costs */
 
				error("Autoreplace failed to refit. Replace engine %d to %d and refit to cargo %d", old_v->engine_type, new_v->engine_type, replacement_cargo_type);
 
			}
 
		}
 

	
 
		if (new_v->type == VEH_Train && HASBIT(old_v->u.rail.flags, VRF_REVERSE_DIRECTION) && !IsMultiheaded(new_v) && !(new_v->next != NULL && IsArticulatedPart(new_v->next))) {
 
			// we are autorenewing to a single engine, so we will turn it as the old one was turned as well
 
			SETBIT(new_v->u.rail.flags, VRF_REVERSE_DIRECTION);
 
		}
 

	
 
		if (old_v->type == VEH_Train && !IsFrontEngine(old_v)) {
 
			/* this is a railcar. We need to move the car into the train
 
@@ -2108,25 +2099,25 @@ static int32 ReplaceVehicle(Vehicle **w,
 
			SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 
			SubtractMoneyFromPlayer(-sell_value); // Pay back the loan
 
			return CMD_ERROR;
 
		}
 
	}
 

	
 
	/* Take back the money we just gave the player just before building the vehicle
 
	 * The player will get the same amount now that the sale actually takes place */
 
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
 
	SubtractMoneyFromPlayer(-sell_value);
 

	
 
	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
 
	cost += DoCommand(0, old_v->index, 0, flags, CMD_SELL_VEH(old_v->type));
 
	cost += DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v));
 

	
 
	if (new_front) {
 
		/* now we assign the old unitnumber to the new vehicle */
 
		new_v->unitnumber = cached_unitnumber;
 
	}
 

	
 
	/* Transfer the name of the old vehicle */
 
	if ((flags & DC_EXEC) && vehicle_name[0] != '\0') {
 
		_cmd_text = vehicle_name;
 
		DoCommand(0, new_v->index, 0, DC_EXEC, CMD_NAME_VEHICLE);
 
	}
 

	
 
@@ -2468,25 +2459,25 @@ uint GenerateVehicleSortList(const Vehic
 
 */
 
int32 SendAllVehiclesToDepot(byte type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id)
 
{
 
	const Vehicle **sort_list = NULL;
 
	uint n, i;
 
	uint16 array_length = 0;
 

	
 
	n = GenerateVehicleSortList(&sort_list, &array_length, type, owner, id, vlw_flag);
 

	
 
	/* Send all the vehicles to a depot */
 
	for (i = 0; i < n; i++) {
 
		const Vehicle *v = sort_list[i];
 
		int32 ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, CMD_SEND_TO_DEPOT(type));
 
		int32 ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
 

	
 
		/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
 
			* In this case we know that at least one vehicle can be sent to a depot
 
			* and we will issue the command. We can now safely quit the loop, knowing
 
			* it will succeed at least once. With DC_EXEC we really need to send them to the depot */
 
		if (!CmdFailed(ret) && !(flags & DC_EXEC)) {
 
			free((void*)sort_list);
 
			return 0;
 
		}
 
	}
 

	
 
	free((void*)sort_list);
 
@@ -2551,25 +2542,25 @@ void VehicleEnterDepot(Vehicle *v)
 
		Order t;
 

	
 
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
 

	
 
		t = v->current_order;
 
		v->current_order.type = OT_DUMMY;
 
		v->current_order.flags = 0;
 

	
 
		if (t.refit_cargo < NUM_CARGO) {
 
			int32 cost;
 

	
 
			_current_player = v->owner;
 
			cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, CMD_REFIT_VEH(v->type));
 
			cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, GetCmdRefitVeh(v));
 

	
 
			if (CmdFailed(cost)) {
 
				v->leave_depot_instantly = false; // We ensure that the vehicle stays in the depot
 
				if (v->owner == _local_player) {
 
					/* Notify the user that we stopped the vehicle */
 
					SetDParam(0, _vehicle_type_names[v->type - 0x10]);
 
					SetDParam(1, v->unitnumber);
 
					AddNewsItem(STR_ORDER_REFIT_FAILED, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 
				}
 
			} else if (v->owner == _local_player && cost != 0) {
 
				ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost);
 
			}
src/vehicle.h
Show inline comments
 
@@ -502,16 +502,59 @@ SpriteID GetEnginePalette(EngineID engin
 

	
 
/**
 
 * Get the colour map for a vehicle.
 
 * @param v Vehicle to get colour map for
 
 * @return A ready-to-use palette modifier
 
 */
 
SpriteID GetVehiclePalette(const Vehicle *v);
 

	
 
/* A lot of code calls for the invalidation of the status bar, which is widget 5.
 
 * Best is to have a virtual value for it when it needs to change again */
 
#define STATUS_BAR 5
 

	
 
extern const uint32 _veh_build_proc_table[];
 
extern const uint32 _veh_sell_proc_table[];
 
extern const uint32 _veh_refit_proc_table[];
 
extern const uint32 _send_to_depot_proc_table[];
 
#define CMD_SEND_TO_DEPOT(x) _send_to_depot_proc_table[ x - VEH_Train]
 

	
 
/* Functions to find the right command for certain vehicle type */
 
static inline uint32 GetCmdBuildVeh(byte type)
 
{
 
	return _veh_build_proc_table[VehTypeToIndex(type)];
 
}
 

	
 
static inline uint32 GetCmdBuildVeh(const Vehicle *v)
 
{
 
	return GetCmdBuildVeh(v->type);
 
}
 

	
 
static inline uint32 GetCmdSellVeh(byte type)
 
{
 
	return _veh_sell_proc_table[VehTypeToIndex(type)];
 
}
 

	
 
static inline uint32 GetCmdSellVeh(const Vehicle *v)
 
{
 
	return GetCmdSellVeh(v->type);
 
}
 

	
 
static inline uint32 GetCmdRefitVeh(byte type)
 
{
 
	return _veh_refit_proc_table[VehTypeToIndex(type)];
 
}
 

	
 
static inline uint32 GetCmdRefitVeh(const Vehicle *v)
 
{
 
	return GetCmdRefitVeh(v->type);
 
}
 

	
 
static inline uint32 GetCmdSendToDepot(byte type)
 
{
 
	return _send_to_depot_proc_table[VehTypeToIndex(type)];
 
}
 

	
 
static inline uint32 GetCmdSendToDepot(const Vehicle *v)
 
{
 
	return GetCmdSendToDepot(v->type);
 
}
 

	
 
#endif /* VEHICLE_H */
src/vehicle_gui.cpp
Show inline comments
 
@@ -1768,31 +1768,31 @@ void PlayerVehWndProc(Window *w, WindowE
 
					break;
 
				case VLW_WIDGET_MANAGE_VEHICLES_DROPDOWN:
 
					assert(vl->l.list_length != 0);
 

	
 
					switch (e->we.dropdown.index) {
 
						case 0: /* Replace window */
 
							ShowReplaceVehicleWindow(vl->vehicle_type);
 
							break;
 
						case 1: /* Send for servicing */
 
							DoCommandP(0, GB(w->window_number, 16, 16) /* StationID or OrderID (depending on VLW) */,
 
								(w->window_number & VLW_MASK) | DEPOT_MASS_SEND | DEPOT_SERVICE,
 
								NULL,
 
								CMD_SEND_TO_DEPOT(vl->vehicle_type));
 
								GetCmdSendToDepot(vl->vehicle_type));
 
							break;
 
						case 2: /* Send to Depots */
 
							DoCommandP(0, GB(w->window_number, 16, 16) /* StationID or OrderID (depending on VLW) */,
 
								(w->window_number & VLW_MASK) | DEPOT_MASS_SEND,
 
								NULL,
 
								CMD_SEND_TO_DEPOT(vl->vehicle_type));
 
								GetCmdSendToDepot(vl->vehicle_type));
 
							break;
 

	
 
						default: NOT_REACHED();
 
					}
 
					break;
 
				default: NOT_REACHED();
 
			}
 
			SetWindowDirty(w);
 
			break;
 

	
 
		case WE_DESTROY:
 
			free((void*)vl->sort_list);
0 comments (0 inline, 0 general)