Changeset - r6558:469828caa298
[Not reviewed]
master
0 12 0
rubidium - 17 years ago 2007-05-01 16:35:14
rubidium@openttd.org
(svn r9760) -Codechange: remove the need for saving some vehicle variables.
12 files changed with 140 insertions and 101 deletions:
0 comments (0 inline, 0 general)
src/aircraft.h
Show inline comments
 
@@ -129,9 +129,10 @@ struct Aircraft : public Vehicle {
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~Aircraft() {}
 

	
 
	const char *GetTypeString() { return "aircraft"; }
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
};
 

	
 
#endif /* AIRCRAFT_H */
src/aircraft_cmd.cpp
Show inline comments
 
@@ -30,12 +30,41 @@
 
#include "newgrf_text.h"
 
#include "newgrf_sound.h"
 
#include "date.h"
 
#include "spritecache.h"
 
#include "cargotype.h"
 

	
 
void Aircraft::UpdateDeltaXY(Direction direction)
 
{
 
	uint32 x;
 
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
 
	switch (this->subtype) {
 
		default: NOT_REACHED();
 
		case AIR_AIRCRAFT:
 
		case AIR_HELICOPTER:
 
			switch (this->u.air.state) {
 
				case ENDTAKEOFF:
 
				case LANDING:
 
				case HELILANDING:
 
				case FLYING:     x = MKIT(24, 24, -1, -1); break;
 
				default:         x = MKIT( 2,  2, -1, -1); break;
 
			}
 
			this->z_height = 5;
 
			break;
 
		case AIR_SHADOW:     this->z_height = 1; x = MKIT(2,  2,  0,  0); break;
 
		case AIR_ROTOR:      this->z_height = 1; x = MKIT(2,  2, -1, -1); break;
 
	}
 
#undef MKIT
 

	
 
	this->x_offs        = GB(x,  0, 8);
 
	this->y_offs        = GB(x,  8, 8);
 
	this->sprite_width  = GB(x, 16, 8);
 
	this->sprite_height = GB(x, 24, 8);
 
}
 

	
 

	
 
/** this maps the terminal to its corresponding state and block flag
 
 *  currently set for 10 terms, 4 helipads */
 
static const byte _airport_terminal_state[] = {2, 3, 4, 5, 6, 7, 19, 20, 0, 0, 8, 9, 21, 22};
 
static const byte _airport_terminal_flag[] =  {0, 1, 2, 3, 4, 5, 22, 23, 0, 0, 6, 7, 24, 25};
 

	
 
static bool AirportMove(Vehicle *v, const AirportFTAClass *apc);
 
@@ -284,21 +313,14 @@ int32 CmdBuildAircraft(TileIndex tile, u
 
		v->x_pos = u->x_pos = x;
 
		v->y_pos = u->y_pos = y;
 

	
 
		u->z_pos = GetSlopeZ(x, y);
 
		v->z_pos = u->z_pos + 1;
 

	
 
		v->x_offs = v->y_offs = -1;
 
//		u->delta_x = u->delta_y = 0;
 

	
 
		v->sprite_width = v->sprite_height = 2;
 
		v->z_height = 5;
 

	
 
		u->sprite_width = u->sprite_height = 2;
 
		u->z_height = 1;
 

	
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 
		u->vehstatus = VS_HIDDEN | VS_UNCLICKABLE | VS_SHADOW;
 

	
 
		v->spritenum = avi->image_index;
 
//		v->cargo_count = u->number_of_pieces = 0;
 

	
 
@@ -320,15 +342,17 @@ int32 CmdBuildAircraft(TileIndex tile, u
 

	
 
		v->max_speed = avi->max_speed;
 
		v->acceleration = avi->acceleration;
 
		v->engine_type = p1;
 

	
 
		v->subtype = (avi->subtype & AIR_CTOL ? AIR_AIRCRAFT : AIR_HELICOPTER);
 
		v->UpdateDeltaXY(INVALID_DIR);
 
		v->value = value;
 

	
 
		u->subtype = AIR_SHADOW;
 
		u->UpdateDeltaXY(INVALID_DIR);
 

	
 
		/* Danger, Will Robinson!
 
		 * If the aircraft is refittable, but cannot be refitted to
 
		 * passengers, we select the cargo type from the refit mask.
 
		 * This is a fairly nasty hack to get around the fact that TTD
 
		 * has no default cargo type specifier for planes... */
 
@@ -408,22 +432,20 @@ int32 CmdBuildAircraft(TileIndex tile, u
 
			w = new (w) Aircraft();
 
			w->direction = DIR_N;
 
			w->owner = _current_player;
 
			w->x_pos = v->x_pos;
 
			w->y_pos = v->y_pos;
 
			w->z_pos = v->z_pos + 5;
 
			w->x_offs = w->y_offs = -1;
 
			w->sprite_width = w->sprite_height = 2;
 
			w->z_height = 1;
 
			w->vehstatus = VS_HIDDEN | VS_UNCLICKABLE;
 
			w->spritenum = 0xFF;
 
			w->subtype = AIR_ROTOR;
 
			w->cur_image = SPR_ROTOR_STOPPED;
 
			w->random_bits = VehicleRandomBits();
 
			/* Use rotor's air.state to store the rotor animation frame */
 
			w->u.air.state = HRS_ROTOR_STOPPED;
 
			w->UpdateDeltaXY(INVALID_DIR);
 
			VehiclePositionChanged(w);
 
		}
 

	
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 
		RebuildVehicleLists();
 
		InvalidateWindow(WC_COMPANY, v->owner);
 
@@ -1507,20 +1529,16 @@ static void AircraftEntersTerminal(Vehic
 
			0);
 
	}
 

	
 
	v->BeginLoading();
 
}
 

	
 
static void AircraftLand(Vehicle *v)
 
{
 
	v->sprite_width = v->sprite_height = 2;
 
}
 

	
 
static void AircraftLandAirplane(Vehicle *v)
 
{
 
	AircraftLand(v);
 
	v->UpdateDeltaXY(INVALID_DIR);
 

	
 
	if (!PlayVehicleSound(v, VSE_TOUCHDOWN)) {
 
		SndPlayVehicleFx(SND_17_SKID_PLANE, v);
 
	}
 
	MaybeCrashAirplane(v);
 
}
 

	
 
@@ -1673,28 +1691,29 @@ static void AircraftEventHandler_TakeOff
 
	PlayAircraftSound(v); // play takeoffsound for airplanes
 
	v->u.air.state = STARTTAKEOFF;
 
}
 

	
 
static void AircraftEventHandler_StartTakeOff(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	v->sprite_width = v->sprite_height = 24; // ??? no idea what this is
 
	v->u.air.state = ENDTAKEOFF;
 
	v->UpdateDeltaXY(INVALID_DIR);
 
}
 

	
 
static void AircraftEventHandler_EndTakeOff(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	v->u.air.state = FLYING;
 
	/* get the next position to go to, differs per airport */
 
	AircraftNextAirportPos_and_Order(v);
 
}
 

	
 
static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	const Player* p = GetPlayer(v->owner);
 
	v->sprite_width = v->sprite_height = 24; // ??? no idea what this is
 
	v->u.air.state = FLYING;
 
	v->UpdateDeltaXY(INVALID_DIR);
 

	
 
	/* get the next position to go to, differs per airport */
 
	AircraftNextAirportPos_and_Order(v);
 

	
 
	/* check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
 
	 * unless it is due for renewal but the engine is no longer available */
 
	if (v->owner == _local_player && (
 
@@ -1746,14 +1765,15 @@ static void AircraftEventHandler_Flying(
 
	v->u.air.state = FLYING;
 
	v->u.air.pos = apc->layout[v->u.air.pos].next_position;
 
}
 

	
 
static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	v->u.air.state = ENDLANDING;
 
	AircraftLandAirplane(v);  // maybe crash airplane
 
	v->u.air.state = ENDLANDING;
 

	
 
	/* check if the aircraft needs to be replaced or renewed and send it to a hangar if needed */
 
	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
 
		/* only the vehicle owner needs to calculate the rest (locally) */
 
		const Player* p = GetPlayer(v->owner);
 
		if (EngineHasReplacementForPlayer(p, v->engine_type) ||
 
			(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
 
@@ -1764,14 +1784,14 @@ static void AircraftEventHandler_Landing
 
		}
 
	}
 
}
 

	
 
static void AircraftEventHandler_HeliLanding(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	AircraftLand(v); // helicopters don't crash
 
	v->u.air.state = HELIENDLANDING;
 
	v->UpdateDeltaXY(INVALID_DIR);
 
}
 

	
 
static void AircraftEventHandler_EndLanding(Vehicle *v, const AirportFTAClass *apc)
 
{
 
	/* next block busy, don't do a thing, just wait */
 
	if (AirportHasBlock(v, &apc->layout[v->u.air.pos], apc)) return;
src/disaster_cmd.cpp
Show inline comments
 
@@ -113,29 +113,24 @@ static void DisasterVehicleUpdateImage(V
 
{
 
	SpriteID img = v->u.disaster.image_override;
 
	if (img == 0) img = _disaster_images[v->subtype][v->direction];
 
	v->cur_image = img;
 
}
 

	
 

	
 
/** Initialize a disaster vehicle. These vehicles are of type VEH_DISASTER, are unclickable
 
 * and owned by nobody */
 
static void InitializeDisasterVehicle(Vehicle *v, int x, int y, byte z, Direction direction, byte subtype)
 
{
 
	v = new (v) DisasterVehicle();
 
	v->x_pos = x;
 
	v->y_pos = y;
 
	v->z_pos = z;
 
	v->tile = TileVirtXY(x, y);
 
	v->direction = direction;
 
	v->subtype = subtype;
 
	v->x_offs = -1;
 
	v->y_offs = -1;
 
	v->sprite_width = 2;
 
	v->sprite_height = 2;
 
	v->z_height = 5;
 
	v->UpdateDeltaXY(INVALID_DIR);
 
	v->owner = OWNER_NONE;
 
	v->vehstatus = VS_UNCLICKABLE;
 
	v->u.disaster.image_override = 0;
 
	v->current_order.Free();
 

	
 
	DisasterVehicleUpdateImage(v);
 
@@ -1062,6 +1057,15 @@ void DisasterDailyLoop()
 
}
 

	
 
void StartupDisasters()
 
{
 
	ResetDisasterDelay();
 
}
 

	
 
void DisasterVehicle::UpdateDeltaXY(Direction direction)
 
{
 
	this->x_offs        = -1;
 
	this->y_offs        = -1;
 
	this->sprite_width  =  2;
 
	this->sprite_height =  2;
 
	this->z_height      =  5;
 
}
src/players.cpp
Show inline comments
 
@@ -1106,13 +1106,13 @@ static const SaveLoad _player_desc[] = {
 
	SLE_CONDVAR(Player, money64,               SLE_INT64, 1, SL_MAX_VERSION),
 

	
 
	    SLE_VAR(Player, current_loan,          SLE_INT32),
 

	
 
	    SLE_VAR(Player, player_color,          SLE_UINT8),
 
	    SLE_VAR(Player, player_money_fraction, SLE_UINT8),
 
	    SLE_VAR(Player, avail_railtypes,       SLE_UINT8),
 
	SLE_CONDVAR(Player, avail_railtypes,       SLE_UINT8,                   0, 57),
 
	    SLE_VAR(Player, block_preview,         SLE_UINT8),
 

	
 
	    SLE_VAR(Player, cargo_types,           SLE_UINT16),
 
	SLE_CONDVAR(Player, location_of_house,     SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
 
	SLE_CONDVAR(Player, location_of_house,     SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Player, last_build_coordinate, SLE_FILE_U16 | SLE_VAR_U32,  0,  5),
src/roadveh.h
Show inline comments
 
@@ -37,9 +37,10 @@ struct RoadVehicle : public Vehicle {
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~RoadVehicle() {}
 

	
 
	const char *GetTypeString() { return "road vehicle"; }
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
};
 

	
 
#endif /* ROADVEH_H */
src/roadveh_cmd.cpp
Show inline comments
 
@@ -166,13 +166,12 @@ int32 CmdBuildRoadVeh(TileIndex tile, ui
 
		v->tile = tile;
 
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		v->x_pos = x;
 
		v->y_pos = y;
 
		v->z_pos = GetSlopeZ(x, y);
 
		v->z_height = 6;
 

	
 
		v->u.road.state = RVSB_IN_DEPOT;
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 

	
 
		v->spritenum = rvi->image_index;
 
		v->cargo_type = rvi->cargo_type;
 
@@ -489,31 +488,33 @@ int32 CmdTurnRoadVeh(TileIndex tile, uin
 
void RoadVehicle::MarkDirty()
 
{
 
	this->cur_image = GetRoadVehImage(this, this->direction);
 
	MarkAllViewportsDirty(this->left_coord, this->top_coord, this->right_coord + 1, this->bottom_coord + 1);
 
}
 

	
 
static void UpdateRoadVehDeltaXY(Vehicle *v)
 
void RoadVehicle::UpdateDeltaXY(Direction direction)
 
{
 
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
 
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
 
	static const uint32 _delta_xy_table[8] = {
 
		MKIT(3, 3, -1, -1),
 
		MKIT(3, 7, -1, -3),
 
		MKIT(3, 3, -1, -1),
 
		MKIT(7, 3, -3, -1),
 
		MKIT(3, 3, -1, -1),
 
		MKIT(3, 7, -1, -3),
 
		MKIT(3, 3, -1, -1),
 
		MKIT(7, 3, -3, -1),
 
	};
 
#undef MKIT
 
	uint32 x = _delta_xy_table[v->direction];
 
	v->x_offs        = GB(x,  0, 8);
 
	v->y_offs        = GB(x,  8, 8);
 
	v->sprite_width  = GB(x, 16, 8);
 
	v->sprite_height = GB(x, 24, 8);
 

	
 
	uint32 x = _delta_xy_table[direction];
 
	this->x_offs        = GB(x,  0, 8);
 
	this->y_offs        = GB(x,  8, 8);
 
	this->sprite_width  = GB(x, 16, 8);
 
	this->sprite_height = GB(x, 24, 8);
 
	this->z_height      = 6;
 
}
 

	
 
static void ClearCrashedStation(Vehicle *v)
 
{
 
	RoadStop *rs = GetRoadStopByTile(v->tile, GetRoadStopType(v->tile));
 

	
 
@@ -563,13 +564,13 @@ static void RoadVehSetRandomDirection(Ve
 
	};
 

	
 
	uint32 r = Random();
 

	
 
	v->direction = ChangeDir(v->direction, delta[r & 3]);
 
	BeginVehicleMove(v);
 
	UpdateRoadVehDeltaXY(v);
 
	v->UpdateDeltaXY(v->direction);
 
	v->cur_image = GetRoadVehImage(v, v->direction);
 
	SetRoadVehPosition(v, v->x_pos, v->y_pos);
 
}
 

	
 
static void RoadVehIsCrashed(Vehicle *v)
 
{
 
@@ -1342,13 +1343,13 @@ static void RoadVehController(Vehicle *v
 

	
 
		v->vehstatus &= ~VS_HIDDEN;
 
		v->u.road.state = tdir;
 
		v->u.road.frame = RVC_DEPOT_START_FRAME;
 

	
 
		v->cur_image = GetRoadVehImage(v, v->direction);
 
		UpdateRoadVehDeltaXY(v);
 
		v->UpdateDeltaXY(v->direction);
 
		SetRoadVehPosition(v,x,y);
 

	
 
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 
		return;
 
	}
 

	
 
@@ -1378,13 +1379,13 @@ static void RoadVehController(Vehicle *v
 
			return;
 
		}
 

	
 
		if ((IsTunnelTile(gp.new_tile) || IsBridgeTile(gp.new_tile)) && HASBIT(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y), VETS_ENTERED_WORMHOLE)) {
 
			/* Vehicle has just entered a bridge or tunnel */
 
			v->cur_image = GetRoadVehImage(v, v->direction);
 
			UpdateRoadVehDeltaXY(v);
 
			v->UpdateDeltaXY(v->direction);
 
			SetRoadVehPosition(v,gp.x,gp.y);
 
			return;
 
		}
 

	
 
		v->x_pos = gp.x;
 
		v->y_pos = gp.y;
 
@@ -1466,13 +1467,13 @@ again:
 
		if (newdir != v->direction) {
 
			v->direction = newdir;
 
			v->cur_speed -= v->cur_speed >> 2;
 
		}
 

	
 
		v->cur_image = GetRoadVehImage(v, newdir);
 
		UpdateRoadVehDeltaXY(v);
 
		v->UpdateDeltaXY(v->direction);
 
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 
		return;
 
	}
 

	
 
	if (rd.x & RDE_TURNED) {
 
		/* Vehicle has finished turning around, it will now head back onto the same tile */
 
@@ -1506,13 +1507,13 @@ again:
 
		if (newdir != v->direction) {
 
			v->direction = newdir;
 
			v->cur_speed -= v->cur_speed >> 2;
 
		}
 

	
 
		v->cur_image = GetRoadVehImage(v, newdir);
 
		UpdateRoadVehDeltaXY(v);
 
		v->UpdateDeltaXY(v->direction);
 
		RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 
		return;
 
	}
 

	
 
	/* Calculate new position for the vehicle */
 
	x = (v->x_pos & ~15) + (rd.x & 15);
 
@@ -1536,13 +1537,13 @@ again:
 
	if (new_dir != old_dir) {
 
		v->direction = new_dir;
 
		v->cur_speed -= (v->cur_speed >> 2);
 
		if (old_dir != v->u.road.state) {
 
			/* The vehicle is in a road stop */
 
			v->cur_image = GetRoadVehImage(v, new_dir);
 
			UpdateRoadVehDeltaXY(v);
 
			v->UpdateDeltaXY(v->direction);
 
			SetRoadVehPosition(v, v->x_pos, v->y_pos);
 
			/* Note, return here means that the frame counter is not incremented
 
			 * for vehicles changing direction in a road stop. This causes frames to
 
			 * be repeated. (XXX) Is this intended? */
 
			return;
 
		}
 
@@ -1656,13 +1657,13 @@ again:
 

	
 
	/* Move to next frame unless vehicle arrived at a stop position
 
	 * in a depot or entered a tunnel/bridge */
 
	if (!HASBIT(r, VETS_ENTERED_WORMHOLE)) v->u.road.frame++;
 

	
 
	v->cur_image = GetRoadVehImage(v, v->direction);
 
	UpdateRoadVehDeltaXY(v);
 
	v->UpdateDeltaXY(v->direction);
 
	RoadZPosAffectSpeed(v, SetRoadVehPosition(v, x, y));
 
}
 

	
 
static void AgeRoadVehCargo(Vehicle *v)
 
{
 
	if (_age_cargo_skip_counter != 0) return;
src/ship.h
Show inline comments
 
@@ -38,9 +38,10 @@ struct Ship: public Vehicle {
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~Ship() {}
 

	
 
	const char *GetTypeString() { return "ship"; }
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
};
 

	
 
#endif /* SHIP_H */
src/ship_cmd.cpp
Show inline comments
 
@@ -331,36 +331,38 @@ static void HandleShipLoading(Vehicle *v
 
	}
 

	
 
	v->cur_order_index++;
 
	InvalidateVehicleOrder(v);
 
}
 

	
 
static void UpdateShipDeltaXY(Vehicle *v, int dir)
 
void Ship::UpdateDeltaXY(Direction direction)
 
{
 
#define MKIT(d,c,b,a) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
 
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
 
	static const uint32 _delta_xy_table[8] = {
 
		MKIT( -3,  -3,  6,  6),
 
		MKIT(-16,  -3, 32,  6),
 
		MKIT( -3,  -3,  6,  6),
 
		MKIT( -3, -16,  6, 32),
 
		MKIT( -3,  -3,  6,  6),
 
		MKIT(-16,  -3, 32,  6),
 
		MKIT( -3,  -3,  6,  6),
 
		MKIT( -3, -16,  6, 32),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT( 6, 32,  -3, -16),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT(32,  6, -16,  -3),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT( 6, 32,  -3, -16),
 
		MKIT( 6,  6,  -3,  -3),
 
		MKIT(32,  6, -16,  -3),
 
	};
 
#undef MKIT
 
	uint32 x = _delta_xy_table[dir];
 
	v->x_offs        = GB(x,  0, 8);
 
	v->y_offs        = GB(x,  8, 8);
 
	v->sprite_width  = GB(x, 16, 8);
 
	v->sprite_height = GB(x, 24, 8);
 

	
 
	uint32 x = _delta_xy_table[direction];
 
	this->x_offs        = GB(x,  0, 8);
 
	this->y_offs        = GB(x,  8, 8);
 
	this->sprite_width  = GB(x, 16, 8);
 
	this->sprite_height = GB(x, 24, 8);
 
	this->z_height      = 6;
 
}
 

	
 
void RecalcShipStuff(Vehicle *v)
 
{
 
	UpdateShipDeltaXY(v, v->direction);
 
	v->UpdateDeltaXY(v->direction);
 
	v->cur_image = GetShipImage(v, v->direction);
 
	v->MarkDirty();
 
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
 
}
 

	
 
static const TileIndexDiffC _ship_leave_depot_offs[] = {
 
@@ -785,13 +787,13 @@ static void ShipController(Vehicle *v)
 
	dir = ShipGetNewDirection(v, gp.x, gp.y);
 
	v->x_pos = gp.x;
 
	v->y_pos = gp.y;
 
	v->z_pos = GetSlopeZ(gp.x, gp.y);
 

	
 
getout:
 
	UpdateShipDeltaXY(v, dir);
 
	v->UpdateDeltaXY(dir);
 
	v->cur_image = GetShipImage(v, dir);
 
	VehiclePositionChanged(v);
 
	EndVehicleMove(v);
 
	return;
 

	
 
reverse_direction:
 
@@ -870,17 +872,13 @@ int32 CmdBuildShip(TileIndex tile, uint3
 
		x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
 
		v->x_pos = x;
 
		v->y_pos = y;
 
		v->z_pos = GetSlopeZ(x, y);
 

	
 
		v->z_height = 6;
 
		v->sprite_width = 6;
 
		v->sprite_height = 6;
 
		v->x_offs = -3;
 
		v->y_offs = -3;
 
		v->UpdateDeltaXY(v->direction);
 
		v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 

	
 
		v->spritenum = svi->image_index;
 
		v->cargo_type = svi->cargo_type;
 
		v->cargo_subtype = 0;
 
		v->cargo_cap = svi->capacity;
src/train.h
Show inline comments
 
@@ -242,9 +242,10 @@ struct Train : public Vehicle {
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~Train() {}
 

	
 
	const char *GetTypeString() { return "train"; }
 
	void MarkDirty();
 
	void UpdateDeltaXY(Direction direction);
 
};
 

	
 
#endif /* TRAIN_H */
src/train_cmd.cpp
Show inline comments
 
@@ -532,13 +532,12 @@ static void AddArticulatedParts(Vehicle 
 
		u->direction = v->direction;
 
		u->owner = v->owner;
 
		u->tile = v->tile;
 
		u->x_pos = v->x_pos;
 
		u->y_pos = v->y_pos;
 
		u->z_pos = v->z_pos;
 
		u->z_height = v->z_height;
 
		u->u.rail.track = v->u.rail.track;
 
		u->u.rail.railtype = v->u.rail.railtype;
 
		u->build_year = v->build_year;
 
		u->vehstatus = v->vehstatus & ~VS_STOPPED;
 
		u->u.rail.first_engine = v->engine_type;
 

	
 
@@ -605,13 +604,12 @@ static int32 CmdBuildRailWagon(EngineID 
 
			int y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
 

	
 
			v->x_pos = x;
 
			v->y_pos = y;
 
			v->z_pos = GetSlopeZ(x, y);
 
			v->owner = _current_player;
 
			v->z_height = 6;
 
			v->u.rail.track = TRACK_BIT_DEPOT;
 
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
 

	
 
			v->subtype = 0;
 
			SetTrainWagon(v);
 
			if (u != NULL) {
 
@@ -678,13 +676,12 @@ static void AddRearEngineToMultiheadedTr
 
	u->direction = v->direction;
 
	u->owner = v->owner;
 
	u->tile = v->tile;
 
	u->x_pos = v->x_pos;
 
	u->y_pos = v->y_pos;
 
	u->z_pos = v->z_pos;
 
	u->z_height = 6;
 
	u->u.rail.track = TRACK_BIT_DEPOT;
 
	u->vehstatus = v->vehstatus & ~VS_STOPPED;
 
	u->subtype = 0;
 
	SetMultiheaded(u);
 
	u->spritenum = v->spritenum + 1;
 
	u->cargo_type = v->cargo_type;
 
@@ -760,13 +757,12 @@ int32 CmdBuildRailVehicle(TileIndex tile
 
			v->direction = DiagDirToDir(dir);
 
			v->tile = tile;
 
			v->owner = _current_player;
 
			v->x_pos = x;
 
			v->y_pos = y;
 
			v->z_pos = GetSlopeZ(x, y);
 
			v->z_height = 6;
 
			v->u.rail.track = TRACK_BIT_DEPOT;
 
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
 
			v->spritenum = rvi->image_index;
 
			v->cargo_type = rvi->cargo_type;
 
			v->cargo_subtype = 0;
 
			v->cargo_cap = rvi->capacity;
 
@@ -1460,15 +1456,15 @@ int32 CmdSellRailWagon(TileIndex tile, u
 
			}
 
		} break;
 
	}
 
	return cost;
 
}
 

	
 
static void UpdateTrainDeltaXY(Vehicle *v, Direction direction)
 
void Train::UpdateDeltaXY(Direction direction)
 
{
 
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
 
#define MKIT(a, b, c, d) ((a & 0xFF) << 24) | ((b & 0xFF) << 16) | ((c & 0xFF) << 8) | ((d & 0xFF) << 0)
 
	static const uint32 _delta_xy_table[8] = {
 
		MKIT(3, 3, -1, -1),
 
		MKIT(3, 7, -1, -3),
 
		MKIT(3, 3, -1, -1),
 
		MKIT(7, 3, -3, -1),
 
		MKIT(3, 3, -1, -1),
 
@@ -1476,22 +1472,22 @@ static void UpdateTrainDeltaXY(Vehicle *
 
		MKIT(3, 3, -1, -1),
 
		MKIT(7, 3, -3, -1),
 
	};
 
#undef MKIT
 

	
 
	uint32 x = _delta_xy_table[direction];
 

	
 
	v->x_offs        = GB(x,  0, 8);
 
	v->y_offs        = GB(x,  8, 8);
 
	v->sprite_width  = GB(x, 16, 8);
 
	v->sprite_height = GB(x, 24, 8);
 
	this->x_offs        = GB(x,  0, 8);
 
	this->y_offs        = GB(x,  8, 8);
 
	this->sprite_width  = GB(x, 16, 8);
 
	this->sprite_height = GB(x, 24, 8);
 
	this->z_height      = 6;
 
}
 

	
 
static void UpdateVarsAfterSwap(Vehicle *v)
 
{
 
	UpdateTrainDeltaXY(v, v->direction);
 
	v->UpdateDeltaXY(v->direction);
 
	v->cur_image = GetTrainImage(v, v->direction);
 
	BeginVehicleMove(v);
 
	VehiclePositionChanged(v);
 
	EndVehicleMove(v);
 
}
 

	
 
@@ -2146,13 +2142,13 @@ static bool CheckTrainStayInDepot(Vehicl
 
	v->u.rail.track = TRACK_BIT_X;
 
	if (v->direction & 2) v->u.rail.track = TRACK_BIT_Y;
 

	
 
	v->vehstatus &= ~VS_HIDDEN;
 
	v->cur_speed = 0;
 

	
 
	UpdateTrainDeltaXY(v, v->direction);
 
	v->UpdateDeltaXY(v->direction);
 
	v->cur_image = GetTrainImage(v, v->direction);
 
	VehiclePositionChanged(v);
 
	UpdateSignalsOnSegment(v->tile, DirToDiagDir(v->direction));
 
	UpdateTrainAcceleration(v);
 
	InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
 

	
 
@@ -3058,13 +3054,13 @@ static void TrainController(Vehicle *v, 
 
				continue;
 
			}
 
		}
 

	
 
		/* update image of train, as well as delta XY */
 
		Direction newdir = GetNewVehicleDirection(v, gp.x, gp.y);
 
		UpdateTrainDeltaXY(v, newdir);
 
		v->UpdateDeltaXY(newdir);
 
		if (update_image) v->cur_image = GetTrainImage(v, newdir);
 

	
 
		v->x_pos = gp.x;
 
		v->y_pos = gp.y;
 

	
 
		/* update the Z position of the vehicle */
 
@@ -3154,13 +3150,13 @@ static void ChangeTrainDirRandomly(Vehic
 

	
 
	do {
 
		/* We don't need to twist around vehicles if they're not visible */
 
		if (!(v->vehstatus & VS_HIDDEN)) {
 
			v->direction = ChangeDir(v->direction, delta[GB(Random(), 0, 2)]);
 
			BeginVehicleMove(v);
 
			UpdateTrainDeltaXY(v, v->direction);
 
			v->UpdateDeltaXY(v->direction);
 
			v->cur_image = GetTrainImage(v, v->direction);
 
			/* Refrain from updating the z position of the vehicle when on
 
			   a bridge, because AfterSetTrainPos will put the vehicle under
 
			   the bridge in that case */
 
			if (v->u.rail.track != TRACK_BIT_WORMHOLE) AfterSetTrainPos(v, false);
 
		}
src/vehicle.cpp
Show inline comments
 
@@ -221,12 +221,14 @@ void VehiclePositionChanged(Vehicle *v)
 
/** Called after load to update coordinates */
 
void AfterLoadVehicles()
 
{
 
	Vehicle *v;
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		v->UpdateDeltaXY(v->direction);
 

	
 
		v->first = NULL;
 
		if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE;
 
	}
 

	
 
	FOR_ALL_VEHICLES(v) {
 
		if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v)))
 
@@ -1440,15 +1442,14 @@ Vehicle *CreateEffectVehicle(int x, int 
 
	if (v != NULL) {
 
		v = new (v) SpecialVehicle();
 
		v->subtype = type;
 
		v->x_pos = x;
 
		v->y_pos = y;
 
		v->z_pos = z;
 
		v->z_height = v->sprite_width = v->sprite_height = 1;
 
		v->x_offs = v->y_offs = 0;
 
		v->tile = 0;
 
		v->UpdateDeltaXY(INVALID_DIR);
 
		v->vehstatus = VS_UNCLICKABLE;
 

	
 
		_effect_init_procs[type](v);
 

	
 
		VehiclePositionChanged(v);
 
		BeginVehicleMove(v);
 
@@ -2641,19 +2642,19 @@ extern const SaveLoad _common_veh_desc[]
 
	SLE_CONDVAR(Vehicle, x_pos,                SLE_UINT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Vehicle, y_pos,                SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
 
	SLE_CONDVAR(Vehicle, y_pos,                SLE_UINT32,                  6, SL_MAX_VERSION),
 
	    SLE_VAR(Vehicle, z_pos,                SLE_UINT8),
 
	    SLE_VAR(Vehicle, direction,            SLE_UINT8),
 

	
 
	    SLE_VAR(Vehicle, cur_image,            SLE_UINT16),
 
	SLE_CONDVAR(Vehicle, cur_image,            SLE_UINT16,                  0, 57),
 
	    SLE_VAR(Vehicle, spritenum,            SLE_UINT8),
 
	    SLE_VAR(Vehicle, sprite_width,         SLE_UINT8),
 
	    SLE_VAR(Vehicle, sprite_height,        SLE_UINT8),
 
	    SLE_VAR(Vehicle, z_height,             SLE_UINT8),
 
	    SLE_VAR(Vehicle, x_offs,               SLE_INT8),
 
	    SLE_VAR(Vehicle, y_offs,               SLE_INT8),
 
	SLE_CONDVAR(Vehicle, sprite_width,         SLE_UINT8,                   0, 57),
 
	SLE_CONDVAR(Vehicle, sprite_height,        SLE_UINT8,                   0, 57),
 
	SLE_CONDVAR(Vehicle, z_height,             SLE_UINT8,                   0, 57),
 
	SLE_CONDVAR(Vehicle, x_offs,               SLE_INT8,                    0, 57),
 
	SLE_CONDVAR(Vehicle, y_offs,               SLE_INT8,                    0, 57),
 
	    SLE_VAR(Vehicle, engine_type,          SLE_UINT16),
 

	
 
	    SLE_VAR(Vehicle, max_speed,            SLE_UINT16),
 
	    SLE_VAR(Vehicle, cur_speed,            SLE_UINT16),
 
	    SLE_VAR(Vehicle, subspeed,             SLE_UINT8),
 
	    SLE_VAR(Vehicle, acceleration,         SLE_UINT8),
 
@@ -2815,17 +2816,17 @@ static const SaveLoad _special_desc[] = 
 
	SLE_CONDVAR(Vehicle, x_pos,         SLE_INT32,                  6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
 
	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                  6, SL_MAX_VERSION),
 
	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
 

	
 
	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
 
	    SLE_VAR(Vehicle, sprite_width,  SLE_UINT8),
 
	    SLE_VAR(Vehicle, sprite_height, SLE_UINT8),
 
	    SLE_VAR(Vehicle, z_height,      SLE_UINT8),
 
	    SLE_VAR(Vehicle, x_offs,        SLE_INT8),
 
	    SLE_VAR(Vehicle, y_offs,        SLE_INT8),
 
	SLE_CONDVAR(Vehicle, sprite_width,  SLE_UINT8,                  0, 57),
 
	SLE_CONDVAR(Vehicle, sprite_height, SLE_UINT8,                  0, 57),
 
	SLE_CONDVAR(Vehicle, z_height,      SLE_UINT8,                  0, 57),
 
	SLE_CONDVAR(Vehicle, x_offs,        SLE_INT8,                   0, 57),
 
	SLE_CONDVAR(Vehicle, y_offs,        SLE_INT8,                   0, 57),
 
	    SLE_VAR(Vehicle, progress,      SLE_UINT8),
 
	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
 

	
 
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleSpecial, unk0), SLE_UINT16),
 
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleSpecial, unk2), SLE_UINT8),
 

	
 
@@ -2850,17 +2851,17 @@ static const SaveLoad _disaster_desc[] =
 
	SLE_CONDVAR(Vehicle, x_pos,         SLE_INT32,                   6, SL_MAX_VERSION),
 
	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32,  0, 5),
 
	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                   6, SL_MAX_VERSION),
 
	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
 
	    SLE_VAR(Vehicle, direction,     SLE_UINT8),
 

	
 
	    SLE_VAR(Vehicle, x_offs,        SLE_INT8),
 
	    SLE_VAR(Vehicle, y_offs,        SLE_INT8),
 
	    SLE_VAR(Vehicle, sprite_width,  SLE_UINT8),
 
	    SLE_VAR(Vehicle, sprite_height, SLE_UINT8),
 
	    SLE_VAR(Vehicle, z_height,      SLE_UINT8),
 
	SLE_CONDVAR(Vehicle, x_offs,        SLE_INT8,                    0, 57),
 
	SLE_CONDVAR(Vehicle, y_offs,        SLE_INT8,                    0, 57),
 
	SLE_CONDVAR(Vehicle, sprite_width,  SLE_UINT8,                   0, 57),
 
	SLE_CONDVAR(Vehicle, sprite_height, SLE_UINT8,                   0, 57),
 
	SLE_CONDVAR(Vehicle, z_height,      SLE_UINT8,                   0, 57),
 
	    SLE_VAR(Vehicle, owner,         SLE_UINT8),
 
	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
 
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
 
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16,                5, SL_MAX_VERSION),
 

	
 
	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
 
@@ -2997,6 +2998,16 @@ void Vehicle::LeaveStation()
 
{
 
	assert(current_order.type == OT_LOADING);
 
	current_order.type = OT_LEAVESTATION;
 
	current_order.flags = 0;
 
	GetStation(this->last_station_visited)->loading_vehicles.remove(this);
 
}
 

	
 

	
 
void SpecialVehicle::UpdateDeltaXY(Direction direction)
 
{
 
	this->x_offs        = 0;
 
	this->y_offs        = 0;
 
	this->sprite_width  = 1;
 
	this->sprite_height = 1;
 
	this->z_height      = 1;
 
}
src/vehicle.h
Show inline comments
 
@@ -355,12 +355,19 @@ struct Vehicle {
 
	virtual const char* GetTypeString() = 0;
 

	
 
	/**
 
	 * Marks the vehicles to be redrawn and updates cached variables
 
	 */
 
	virtual void MarkDirty() {}
 

	
 
	/**
 
	 * Updates the x and y offsets and the size of the sprite used
 
	 * for this vehicle.
 
	 * @param direction the direction the vehicle is facing
 
	 */
 
	virtual void UpdateDeltaXY(Direction direction) {}
 
};
 

	
 
/**
 
 * This class 'wraps' Vehicle; you do not actually instantiate this class.
 
 * You create a Vehicle using AllocateVehicle, so it is added to the pool
 
 * and you reinitialize that to a Train using:
 
@@ -380,12 +387,13 @@ struct SpecialVehicle : public Vehicle {
 
	SpecialVehicle() { this->type = VEH_SPECIAL; }
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~SpecialVehicle() {}
 

	
 
	const char *GetTypeString() { return "special vehicle"; }
 
	void UpdateDeltaXY(Direction direction);
 
};
 

	
 
/**
 
 * This class 'wraps' Vehicle; you do not actually instantiate this class.
 
 * You create a Vehicle using AllocateVehicle, so it is added to the pool
 
 * and you reinitialize that to a Train using:
 
@@ -398,34 +406,31 @@ struct DisasterVehicle : public Vehicle 
 
	DisasterVehicle() { this->type = VEH_DISASTER; }
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~DisasterVehicle() {}
 

	
 
	const char *GetTypeString() { return "disaster vehicle"; }
 
	void UpdateDeltaXY(Direction direction);
 
};
 

	
 
/**
 
 * This class 'wraps' Vehicle; you do not actually instantiate this class.
 
 * You create a Vehicle using AllocateVehicle, so it is added to the pool
 
 * and you reinitialize that to a Train using:
 
 *   v = new (v) Train();
 
 *
 
 * As side-effect the vehicle type is set correctly.
 
 *
 
 * An invalid vehicle must never be used; all (virtual) functions from
 
 * Vehicle should assert (NOT_REACHED).
 
 */
 
struct InvalidVehicle : public Vehicle {
 
	/** Initializes the Vehicle to a invalid vehicle */
 
	InvalidVehicle() { this->type = VEH_INVALID; }
 

	
 
	/** We want to 'destruct' the right class. */
 
	virtual ~InvalidVehicle() {}
 

	
 
	const char *GetTypeString() { return "invalid vehicle"; }
 
	void MarkDirty() { NOT_REACHED(); }
 
};
 

	
 
#define is_custom_sprite(x) (x >= 0xFD)
 
#define IS_CUSTOM_FIRSTHEAD_SPRITE(x) (x == 0xFD)
 
#define IS_CUSTOM_SECONDHEAD_SPRITE(x) (x == 0xFE)
 

	
0 comments (0 inline, 0 general)