2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "ttd.h"
|
2004-11-25 10:47:30 +00:00
|
|
|
#include "table/strings.h"
|
2004-12-15 22:18:54 +00:00
|
|
|
#include "map.h"
|
2005-01-29 12:19:05 +00:00
|
|
|
#include "tile.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "vehicle.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "pathfind.h"
|
2005-01-31 11:23:10 +00:00
|
|
|
#include "npf.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "station.h"
|
|
|
|
#include "table/train_cmd.h"
|
|
|
|
#include "gfx.h"
|
|
|
|
#include "news.h"
|
|
|
|
#include "engine.h"
|
|
|
|
#include "player.h"
|
2004-11-05 23:12:33 +00:00
|
|
|
#include "sound.h"
|
2005-02-06 10:18:47 +00:00
|
|
|
#include "depot.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
#define is_firsthead_sprite(spritenum) \
|
|
|
|
(is_custom_sprite(spritenum) \
|
|
|
|
? is_custom_firsthead_sprite(spritenum) \
|
|
|
|
: _engine_sprite_add[spritenum] == 0)
|
|
|
|
|
2004-12-21 16:00:14 +00:00
|
|
|
static bool TrainCheckIfLineEnds(Vehicle *v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static const byte _vehicle_initial_x_fract[4] = {10,8,4,8};
|
|
|
|
static const byte _vehicle_initial_y_fract[4] = {8,4,8,10};
|
|
|
|
static const byte _state_dir_table[4] = { 0x20, 8, 0x10, 4 };
|
|
|
|
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* These two arrays are used for realistic acceleration. XXX: How should they
|
|
|
|
* be interpreted? */
|
2005-01-26 12:51:04 +00:00
|
|
|
static const byte _curve_neighbours45[8][2] = {
|
|
|
|
{7, 1},
|
|
|
|
{0, 2},
|
|
|
|
{1, 3},
|
|
|
|
{2, 4},
|
|
|
|
{3, 5},
|
|
|
|
{4, 6},
|
|
|
|
{5, 7},
|
|
|
|
{6, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _curve_neighbours90[8][2] = {
|
|
|
|
{6, 2},
|
|
|
|
{7, 3},
|
|
|
|
{0, 4},
|
|
|
|
{1, 5},
|
|
|
|
{2, 6},
|
|
|
|
{3, 7},
|
|
|
|
{4, 0},
|
|
|
|
{5, 1},
|
|
|
|
};
|
|
|
|
|
|
|
|
enum AccelType {
|
|
|
|
AM_ACCEL,
|
|
|
|
AM_BRAKE
|
|
|
|
};
|
|
|
|
|
2005-01-30 19:51:39 +00:00
|
|
|
static bool TrainShouldStop(Vehicle *v, TileIndex tile)
|
|
|
|
{
|
|
|
|
Order *o = &v->current_order;
|
|
|
|
assert(v->type == VEH_Train);
|
|
|
|
assert(IsTileType(v->tile, MP_STATION));
|
|
|
|
//When does a train drive through a station
|
|
|
|
//first we deal with the "new nonstop handling"
|
|
|
|
if ( _patches.new_nonstop && o->flags & OF_NON_STOP && _map2[tile] == o->station )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (v->last_station_visited == _map2[tile])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( _map2[tile] != o->station && (o->flags & OF_NON_STOP || _patches.new_nonstop))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-01-26 12:51:04 +00:00
|
|
|
//new acceleration
|
|
|
|
static int GetTrainAcceleration(Vehicle *v, bool mode)
|
|
|
|
{
|
|
|
|
Vehicle *u = v;
|
|
|
|
int num = 0; //number of vehicles, change this into the number of axles later
|
|
|
|
int power = 0;
|
|
|
|
int mass = 0;
|
|
|
|
int max_speed = 2000;
|
|
|
|
int area = 120;
|
|
|
|
int friction = 35; //[1e-3]
|
|
|
|
int drag_coeff = 20; //[1e-4]
|
|
|
|
int incl = 0;
|
|
|
|
int resistance;
|
|
|
|
int speed = v->cur_speed; //[mph]
|
|
|
|
int force = 0x3FFFFFFF;
|
|
|
|
int pos = 0;
|
|
|
|
int lastpos = -1;
|
|
|
|
int curvecount[2] = {0, 0};
|
|
|
|
int *dist = NULL;
|
|
|
|
int sum = 0;
|
|
|
|
int numcurve = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
speed *= 10;
|
|
|
|
speed /= 16;
|
|
|
|
|
|
|
|
//first find the curve speed limit
|
|
|
|
for (; u->next != NULL; u = u->next, pos++) {
|
|
|
|
int dir = u->direction;
|
|
|
|
int ndir = u->next->direction;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if ( _curve_neighbours45[dir][i] == ndir) {
|
|
|
|
curvecount[i]++;
|
|
|
|
if (lastpos != -1) {
|
|
|
|
dist = realloc(dist, sizeof(int) * ++numcurve);
|
|
|
|
dist[numcurve - 1] = pos - lastpos;
|
|
|
|
if (pos - lastpos == 1) {
|
|
|
|
max_speed = 88;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastpos = pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//if we have a 90 degree turn, fix the speed limit to 60
|
|
|
|
if ( _curve_neighbours90[dir][0] == ndir || _curve_neighbours90[dir][1] == ndir) {
|
|
|
|
max_speed = 61;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0; i < numcurve; i++) {
|
|
|
|
sum += dist[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (numcurve > 0) {
|
|
|
|
sum /= numcurve;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((curvecount[0] != 0 || curvecount[1] != 0) && (max_speed > 88)) {
|
|
|
|
int total = curvecount[0] + curvecount[1];
|
|
|
|
if (curvecount[0] == 1 && curvecount[1] == 1) {
|
|
|
|
max_speed = 0xFFFF;
|
|
|
|
} else if (total > 1) {
|
|
|
|
max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
max_speed += (max_speed / 2) * v->u.rail.railtype;
|
|
|
|
|
|
|
|
if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) {
|
|
|
|
static const TileIndexDiffC _station_dir_from_vdir[] = {
|
|
|
|
{0, 0}, {-1, 0}, {0, 0}, {0, 1}, {0, 0}, {1, 0}, {0, 0}, {0, -1}
|
|
|
|
};
|
|
|
|
|
2005-01-30 19:51:39 +00:00
|
|
|
if (TrainShouldStop(v, v->tile)) {
|
2005-01-26 12:51:04 +00:00
|
|
|
int station_length = 0;
|
|
|
|
TileIndex tile = v->tile;
|
|
|
|
int delta_v;
|
|
|
|
|
|
|
|
max_speed = 120;
|
|
|
|
do {
|
|
|
|
station_length++;
|
|
|
|
tile = TILE_ADD(tile, ToTileIndexDiff(_station_dir_from_vdir[v->direction]));
|
|
|
|
} while (IsTileType(tile, MP_STATION));
|
|
|
|
|
|
|
|
delta_v = v->cur_speed / (station_length + 1);
|
|
|
|
if (v->max_speed > (v->cur_speed - delta_v))
|
|
|
|
max_speed = v->cur_speed - (delta_v / 10);
|
|
|
|
|
|
|
|
max_speed = max(max_speed, 25 * station_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (u = v; u != NULL; u = u->next) {
|
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
|
|
|
|
int vmass;
|
|
|
|
|
|
|
|
num++;
|
|
|
|
power += rvi->power * 746; //[W]
|
|
|
|
drag_coeff += 3;
|
|
|
|
|
|
|
|
if (rvi->max_speed != 0)
|
|
|
|
max_speed = min(rvi->max_speed, max_speed);
|
|
|
|
|
|
|
|
if (u->u.rail.track == 0x80)
|
|
|
|
max_speed = 61;
|
|
|
|
|
|
|
|
vmass = rvi->weight; //[t]
|
|
|
|
vmass += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16;
|
|
|
|
mass += vmass; //[t]
|
|
|
|
|
|
|
|
if (!IsTileType(u->tile, MP_TUNNELBRIDGE)) {
|
|
|
|
if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) {
|
|
|
|
incl += vmass * 60; //3% slope, quite a bit actually
|
|
|
|
} else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) {
|
|
|
|
incl -= vmass * 60;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// these are shown in the UI
|
|
|
|
v->u.rail.cached_weight = mass;
|
|
|
|
v->u.rail.cached_power = power / 746;
|
|
|
|
v->max_speed = max_speed;
|
|
|
|
|
|
|
|
|
|
|
|
if (v->u.rail.railtype != 2) {
|
|
|
|
resistance = 13 * mass / 10;
|
|
|
|
resistance += 60 * num;
|
|
|
|
resistance += friction * mass * speed / 1000;
|
|
|
|
resistance += (area * drag_coeff * speed * speed) / 10000;
|
|
|
|
} else
|
|
|
|
resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
|
|
|
|
resistance += incl;
|
|
|
|
resistance *= 4; //[N]
|
|
|
|
|
|
|
|
if (speed > 0) {
|
|
|
|
switch (v->u.rail.railtype) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
force = power / speed; //[N]
|
|
|
|
force *= 22;
|
|
|
|
force /= 10;
|
|
|
|
} break;
|
|
|
|
case 2:
|
|
|
|
force = power / 25;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
//"kickoff" acceleration
|
|
|
|
force = resistance * 10;
|
|
|
|
|
|
|
|
if (force <= 0) force = 10000;
|
|
|
|
|
|
|
|
if (v->u.rail.railtype != 2)
|
|
|
|
force = min(force, mass * 10 * 200);
|
|
|
|
|
|
|
|
if (mode == AM_ACCEL) {
|
|
|
|
return (force - resistance) / (mass * 4);
|
|
|
|
} else {
|
|
|
|
return min((-force - resistance) /(mass * 4), (10000 / (mass * 4)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
void UpdateTrainAcceleration(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint acc, power=0, max_speed=5000, weight=0;
|
|
|
|
Vehicle *u = v;
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
assert(v->subtype == TS_Front_Engine);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// compute stuff like max speed, power, and weight.
|
|
|
|
do {
|
2004-12-04 07:41:37 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// power is sum of the power for all engines
|
|
|
|
power += rvi->power;
|
|
|
|
|
|
|
|
// limit the max speed to the speed of the slowest vehicle.
|
|
|
|
if (rvi->max_speed && rvi->max_speed <= max_speed) max_speed = rvi->max_speed;
|
|
|
|
|
|
|
|
// weight is the sum of the weight of the wagon and the weight of the cargo.
|
|
|
|
weight += rvi->weight;
|
|
|
|
weight += (_cargoc.weights[u->cargo_type] * u->cargo_count) >> 4;
|
|
|
|
|
|
|
|
} while ( (u=u->next) != NULL);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// these are shown in the UI
|
|
|
|
v->u.rail.cached_weight = weight;
|
|
|
|
v->u.rail.cached_power = power;
|
|
|
|
v->max_speed = max_speed;
|
|
|
|
|
|
|
|
assert(weight != 0);
|
|
|
|
|
|
|
|
// compute acceleration
|
|
|
|
acc = power / weight * 4;
|
|
|
|
|
|
|
|
if (acc >= 255) acc=255;
|
|
|
|
if (acc == 0) acc++;
|
|
|
|
|
|
|
|
v->acceleration = (byte)acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetTrainImage(Vehicle *v, byte direction)
|
|
|
|
{
|
|
|
|
int img = v->spritenum;
|
|
|
|
int base;
|
|
|
|
|
|
|
|
if (is_custom_sprite(img)) {
|
|
|
|
base = GetCustomVehicleSprite(v, direction + 4 * is_custom_secondhead_sprite(img));
|
|
|
|
if (base) return base;
|
|
|
|
img = _engine_original_sprites[v->engine_type];
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
|
|
|
|
|
|
|
|
if (v->cargo_count >= (v->cargo_cap >> 1))
|
|
|
|
base += _wagon_full_adder[img];
|
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTrainEngine(int x, int y, int engine, uint32 image_ormod)
|
|
|
|
{
|
2004-12-04 07:41:37 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(engine);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
int img = rvi->image_index;
|
|
|
|
uint32 image = 0;
|
|
|
|
|
|
|
|
if (is_custom_sprite(img)) {
|
2004-11-12 17:16:13 +00:00
|
|
|
image = GetCustomVehicleIcon(engine, 6);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!image) img = _engine_original_sprites[engine];
|
|
|
|
}
|
|
|
|
if (!image) {
|
|
|
|
image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rvi->flags & RVI_MULTIHEAD) {
|
|
|
|
DrawSprite(image | image_ormod, x-14, y);
|
|
|
|
x += 15;
|
|
|
|
image = 0;
|
|
|
|
if (is_custom_sprite(img)) {
|
2004-11-12 17:16:13 +00:00
|
|
|
image = GetCustomVehicleIcon(engine, 2);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!image) img = _engine_original_sprites[engine];
|
|
|
|
}
|
|
|
|
if (!image) {
|
2004-09-10 19:02:27 +00:00
|
|
|
image = ((6 + _engine_sprite_add[img+1]) & _engine_sprite_and[img+1]) + _engine_sprite_base[img+1];
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DrawSprite(image | image_ormod, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawTrainEngineInfo(int engine, int x, int y, int maxw)
|
|
|
|
{
|
2004-12-04 07:41:37 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(engine);
|
2004-08-09 17:04:08 +00:00
|
|
|
int cap;
|
|
|
|
uint multihead = ((rvi->flags & RVI_MULTIHEAD) ? 1 : 0);
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5);
|
|
|
|
SetDParam(2, rvi->max_speed * 10 >> 4);
|
|
|
|
SetDParam(3, rvi->power << multihead);
|
|
|
|
SetDParam(1, rvi->weight << multihead);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(4, (rvi->running_cost_base * _price.running_rail[rvi->engclass] >> 8) << multihead);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
cap = rvi->capacity;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(5, STR_8838_N_A);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (cap != 0) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(6, cap << multihead);
|
|
|
|
SetDParam(5, _cargoc.names_long_p[rvi->cargo_type]);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
DrawStringMultiCenter(x, y, STR_885B_COST_WEIGHT_T_SPEED_POWER, maxw);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int32 value;
|
|
|
|
Vehicle *v;
|
|
|
|
const RailVehicleInfo *rvi;
|
|
|
|
int dir;
|
|
|
|
const Engine *e;
|
|
|
|
int x,y;
|
|
|
|
|
2005-01-04 17:11:03 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2004-12-04 07:41:37 +00:00
|
|
|
rvi = RailVehInfo(engine);
|
2004-08-09 17:04:08 +00:00
|
|
|
value = (rvi->base_cost * _price.build_railwagon) >> 8;
|
|
|
|
|
|
|
|
if (!(flags & DC_QUERY_COST)) {
|
|
|
|
_error_message = STR_00E1_TOO_MANY_VEHICLES_IN_GAME;
|
|
|
|
|
|
|
|
v = AllocateVehicle();
|
|
|
|
if (v == NULL)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
byte img = rvi->image_index;
|
2005-01-06 22:31:58 +00:00
|
|
|
Vehicle *u, *w;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->spritenum = img;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
u = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
FOR_ALL_VEHICLES(w) {
|
|
|
|
if (w->type == VEH_Train && w->tile == (TileIndex)tile &&
|
2005-01-19 19:15:03 +00:00
|
|
|
w->subtype == TS_Free_Car && w->engine_type == engine) {
|
2005-01-06 22:31:58 +00:00
|
|
|
u = GetLastVehicleInChain(w);
|
2004-08-09 17:04:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->engine_type = engine;
|
|
|
|
|
|
|
|
dir = _map5[tile] & 3;
|
|
|
|
|
|
|
|
v->direction = (byte)(dir*2+1);
|
|
|
|
v->tile = (TileIndex)tile;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-07 17:02:43 +00:00
|
|
|
x = TileX(tile) * 16 | _vehicle_initial_x_fract[dir];
|
|
|
|
y = TileY(tile) * 16 | _vehicle_initial_y_fract[dir];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
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 = 0x80;
|
|
|
|
v->vehstatus = VS_HIDDEN | VS_DEFPAL;
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
v->subtype = TS_Free_Car;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (u != NULL) {
|
|
|
|
u->next = v;
|
2005-01-19 19:15:03 +00:00
|
|
|
v->subtype = TS_Not_First;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.rail.first_engine = u->u.rail.first_engine;
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->u.rail.first_engine == 0xffff && u->subtype == TS_Front_Engine )
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.rail.first_engine = u->engine_type;
|
|
|
|
} else {
|
|
|
|
v->u.rail.first_engine = 0xffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->cargo_type = rvi->cargo_type;
|
|
|
|
v->cargo_cap = rvi->capacity;
|
|
|
|
v->value = value;
|
|
|
|
// v->day_counter = 0;
|
|
|
|
|
|
|
|
e = &_engines[engine];
|
|
|
|
v->u.rail.railtype = e->railtype;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->build_year = _cur_year;
|
|
|
|
v->type = VEH_Train;
|
|
|
|
v->cur_image = 0xAC2;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_new_wagon_id = v->index;
|
|
|
|
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move all free vehicles in the depot to the train
|
|
|
|
static void NormalizeTrainVehInDepot(Vehicle *u)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->type == VEH_Train && v->subtype == TS_Free_Car &&
|
2004-08-09 17:04:08 +00:00
|
|
|
v->tile == u->tile &&
|
|
|
|
v->u.rail.track == 0x80) {
|
|
|
|
if (DoCommandByTile(0,v->index | (u->index<<16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _railveh_unk1[] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 1, 1, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 1, 0, 1, 0,
|
|
|
|
0, 1, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 1,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _railveh_score[] = {
|
|
|
|
1, 4, 7, 19, 20, 30, 31, 19,
|
|
|
|
20, 21, 22, 10, 11, 30, 31, 32,
|
|
|
|
33, 34, 35, 29, 45, 32, 50, 40,
|
|
|
|
41, 51, 52, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 60, 62,
|
|
|
|
63, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 70, 71, 72, 73,
|
|
|
|
74, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-12-27 18:18:44 +00:00
|
|
|
int32 EstimateTrainCost(const RailVehicleInfo *rvi)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5;
|
|
|
|
}
|
|
|
|
|
2005-01-23 13:09:35 +00:00
|
|
|
void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building)
|
2005-01-18 23:27:06 +00:00
|
|
|
{
|
|
|
|
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 = 0x80;
|
|
|
|
v->u.rail.first_engine = 0xffff;
|
|
|
|
u->vehstatus = v->vehstatus & ~VS_STOPPED;
|
2005-01-19 19:15:03 +00:00
|
|
|
u->subtype = TS_Not_First;
|
2005-01-18 23:27:06 +00:00
|
|
|
u->spritenum = v->spritenum + 1;
|
|
|
|
u->cargo_type = v->cargo_type;
|
|
|
|
u->cargo_cap = v->cargo_cap;
|
|
|
|
u->u.rail.railtype = v->u.rail.railtype;
|
|
|
|
if (building) v->next = u;
|
|
|
|
u->engine_type = v->engine_type;
|
|
|
|
u->build_year = v->build_year;
|
2005-01-23 13:09:35 +00:00
|
|
|
if (building)
|
2005-01-18 23:27:06 +00:00
|
|
|
v->value = u->value = v->value >> 1;
|
|
|
|
else
|
|
|
|
u->value = v->value;
|
|
|
|
u->type = VEH_Train;
|
|
|
|
u->cur_image = 0xAC2;
|
|
|
|
VehiclePositionChanged(u);
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Build a railroad vehicle
|
|
|
|
* p1 = vehicle type id
|
|
|
|
*/
|
|
|
|
|
|
|
|
int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
const RailVehicleInfo *rvi;
|
|
|
|
int value,dir;
|
|
|
|
Vehicle *v, *u;
|
2005-02-04 14:24:23 +00:00
|
|
|
UnitID unit_num;
|
2004-08-09 17:04:08 +00:00
|
|
|
Engine *e;
|
2005-02-06 22:36:08 +00:00
|
|
|
TileIndex tile = TILE_FROM_XY(x,y);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-27 21:18:03 +00:00
|
|
|
if (!IsEngineBuildable(p1, VEH_Train)) return CMD_ERROR;
|
2005-01-27 21:00:05 +00:00
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
|
2005-01-29 23:58:07 +00:00
|
|
|
|
2005-01-30 13:03:32 +00:00
|
|
|
if (_map_owner[tile] != _current_player) return CMD_ERROR;
|
2005-01-29 23:58:07 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
_cmd_build_rail_veh_var1 = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2004-12-04 07:41:37 +00:00
|
|
|
rvi = RailVehInfo(p1);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (rvi->flags & RVI_WAGON) {
|
|
|
|
return CmdBuildRailWagon(p1, tile, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
value = EstimateTrainCost(rvi);
|
|
|
|
|
|
|
|
if (!(flags & DC_QUERY_COST)) {
|
|
|
|
v = AllocateVehicle();
|
2005-01-15 19:06:22 +00:00
|
|
|
if (v == NULL || IsOrderPoolFull())
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
unit_num = GetFreeUnitNumber(VEH_Train);
|
|
|
|
if (unit_num > _patches.max_trains)
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->unitnumber = unit_num;
|
|
|
|
|
|
|
|
dir = _map5[tile] & 3;
|
|
|
|
|
|
|
|
v->direction = (byte)(dir*2+1);
|
|
|
|
v->tile = (TileIndex)tile;
|
|
|
|
v->owner = _current_player;
|
|
|
|
v->x_pos = (x |= _vehicle_initial_x_fract[dir]);
|
|
|
|
v->y_pos = (y |= _vehicle_initial_y_fract[dir]);
|
|
|
|
v->z_pos = GetSlopeZ(x,y);
|
|
|
|
v->z_height = 6;
|
|
|
|
v->u.rail.track = 0x80;
|
|
|
|
v->u.rail.first_engine = 0xffff;
|
|
|
|
v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
|
|
|
|
v->spritenum = rvi->image_index;
|
|
|
|
v->cargo_type = rvi->cargo_type;
|
|
|
|
v->cargo_cap = rvi->capacity;
|
|
|
|
v->max_speed = rvi->max_speed;
|
|
|
|
v->value = value;
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->dest_tile = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->engine_type = (byte)p1;
|
|
|
|
e = &_engines[p1];
|
|
|
|
|
|
|
|
v->reliability = e->reliability;
|
|
|
|
v->reliability_spd_dec = e->reliability_spd_dec;
|
|
|
|
v->max_age = e->lifelength * 366;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->string_id = STR_SV_TRAIN_NAME;
|
|
|
|
v->u.rail.railtype = e->railtype;
|
|
|
|
_new_train_id = v->index;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->service_interval = _patches.servint_trains;
|
|
|
|
v->date_of_last_service = _date;
|
|
|
|
v->build_year = _cur_year;
|
|
|
|
v->type = VEH_Train;
|
|
|
|
v->cur_image = 0xAC2;
|
|
|
|
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
|
2005-01-18 23:27:06 +00:00
|
|
|
if (rvi->flags&RVI_MULTIHEAD && (u = AllocateVehicle()) != NULL)
|
|
|
|
AddRearEngineToMultiheadedTrain(v, u, true);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
UpdateTrainAcceleration(v);
|
|
|
|
NormalizeTrainVehInDepot(v);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, tile);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_cmd_build_rail_veh_var1 = _railveh_unk1[p1];
|
|
|
|
_cmd_build_rail_veh_score = _railveh_score[p1];
|
2005-01-23 22:01:51 +00:00
|
|
|
|
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
return value;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static bool IsTunnelTile(TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
return IsTileType(tile, MP_TUNNELBRIDGE) &&
|
2004-08-09 17:04:08 +00:00
|
|
|
(_map5[tile]&0x80) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-06 10:18:47 +00:00
|
|
|
int CheckTrainStoppedInDepot(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
TileIndex tile = v->tile;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* check if stopped in a depot */
|
2005-02-06 22:36:08 +00:00
|
|
|
if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
errmsg:
|
|
|
|
_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
do {
|
|
|
|
count++;
|
2004-09-10 19:02:27 +00:00
|
|
|
if (v->u.rail.track != 0x80 || v->tile != (TileIndex)tile ||
|
2005-01-19 19:15:03 +00:00
|
|
|
(v->subtype == TS_Front_Engine && !(v->vehstatus&VS_STOPPED)))
|
2004-08-09 17:04:08 +00:00
|
|
|
goto errmsg;
|
|
|
|
} while ( (v=v->next) != NULL);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlink a rail wagon from the linked list.
|
|
|
|
// returns the new value of first
|
|
|
|
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
|
|
|
|
{
|
|
|
|
// unlinking the first vehicle of the chain?
|
|
|
|
v->u.rail.first_engine = 0xffff;
|
|
|
|
if (v == first) {
|
|
|
|
Vehicle *u;
|
|
|
|
if ((v=v->next) == NULL) return NULL;
|
|
|
|
for (u=v; u; u=u->next) u->u.rail.first_engine = v->engine_type;
|
2005-01-19 19:15:03 +00:00
|
|
|
v->subtype = TS_Free_Car;
|
2004-08-09 17:04:08 +00:00
|
|
|
return v;
|
|
|
|
} else {
|
|
|
|
Vehicle *u;
|
|
|
|
for(u=first; u->next!=v; u=u->next) {}
|
|
|
|
u->next = v->next;
|
|
|
|
return first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Vehicle *FindGoodVehiclePos(Vehicle *src)
|
|
|
|
{
|
|
|
|
Vehicle *dst;
|
|
|
|
uint16 eng = src->engine_type;
|
|
|
|
TileIndex tile = src->tile;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(dst) {
|
2005-01-19 19:15:03 +00:00
|
|
|
if (dst->type == VEH_Train && dst->subtype == TS_Free_Car && dst->tile==tile) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// check so all vehicles in the line have the same engine.
|
|
|
|
Vehicle *v = dst;
|
|
|
|
while (v->engine_type == eng) {
|
|
|
|
if ((v = v->next) == NULL) return dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p1 & 0xffff= source vehicle index
|
|
|
|
p1 & 0xffff0000 = what wagon to put the wagon AFTER, 0xffff0000 to make a new line
|
|
|
|
p2 & 1 = move all vehicles following the vehicle..
|
|
|
|
*/
|
|
|
|
|
|
|
|
int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *src, *dst, *src_head, *dst_head;
|
|
|
|
bool is_loco;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1 & 0xFFFF)) return CMD_ERROR;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
src = GetVehicle(p1 & 0xFFFF);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (src->type != VEH_Train) return CMD_ERROR;
|
|
|
|
|
2004-12-04 07:41:37 +00:00
|
|
|
is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON)
|
2004-08-09 17:04:08 +00:00
|
|
|
&& is_firsthead_sprite(src->spritenum);
|
|
|
|
|
|
|
|
// if nothing is selected as destination, try and find a matching vehicle to drag to.
|
2004-09-10 19:02:27 +00:00
|
|
|
if (((int32)p1 >> 16) == -1) {
|
2004-08-09 17:04:08 +00:00
|
|
|
dst = NULL;
|
|
|
|
if (!is_loco) dst = FindGoodVehiclePos(src);
|
|
|
|
} else {
|
2005-01-06 22:31:58 +00:00
|
|
|
dst = GetVehicle(((int32)p1 >> 16));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't move the same vehicle..
|
|
|
|
if (src == dst)
|
|
|
|
return 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* the player must be the owner */
|
|
|
|
if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner)))
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
/* locate the head of the two chains */
|
|
|
|
src_head = GetFirstVehicleInChain(src);
|
|
|
|
dst_head = NULL;
|
|
|
|
if (dst != NULL) dst_head = GetFirstVehicleInChain(dst);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* check if all vehicles in the source train are stopped */
|
2005-02-06 10:18:47 +00:00
|
|
|
if (CheckTrainStoppedInDepot(src_head) < 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
/* check if all the vehicles in the dest train are stopped,
|
|
|
|
* and that the length of the dest train is no longer than XXX vehicles */
|
|
|
|
if (dst_head != NULL) {
|
2005-02-06 10:18:47 +00:00
|
|
|
int num = CheckTrainStoppedInDepot(dst_head);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (num < 0)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (num > (_patches.mammoth_trains ? 100 : 9) && dst_head->subtype == TS_Front_Engine )
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_8819_TRAIN_TOO_LONG);
|
|
|
|
|
|
|
|
// if it's a multiheaded vehicle we're dragging to, drag to the vehicle before..
|
|
|
|
while (is_custom_secondhead_sprite(dst->spritenum)
|
|
|
|
|| (!is_custom_sprite(dst->spritenum) && _engine_sprite_add[dst->spritenum] != 0)) {
|
|
|
|
Vehicle *v = GetPrevVehicleInChain(dst);
|
|
|
|
if (!v || src == v) break;
|
|
|
|
dst = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(dst_head->tile == src_head->tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
// when moving all wagons, we can't have the same src_head and dst_head
|
|
|
|
if (p2 & 1 && src_head == dst_head)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// moving a loco to a new line?, then we need to assign a unitnumber.
|
2005-01-19 19:15:03 +00:00
|
|
|
if (dst == NULL && src->subtype != TS_Front_Engine && is_loco) {
|
2005-02-04 14:24:23 +00:00
|
|
|
UnitID unit_num = GetFreeUnitNumber(VEH_Train);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (unit_num > _patches.max_trains)
|
|
|
|
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
src->unitnumber = unit_num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* do it? */
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (p2 & 1) {
|
|
|
|
// unlink ALL wagons
|
|
|
|
if (src != src_head) {
|
|
|
|
Vehicle *v = src_head;
|
|
|
|
while (v->next != src) v=v->next;
|
|
|
|
v->next = NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// unlink single wagon from linked list
|
|
|
|
UnlinkWagon(src, src_head);
|
|
|
|
src->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst == NULL) {
|
|
|
|
// move the train to an empty line. for locomotives, we set the type to 0. for wagons, 4.
|
|
|
|
if (is_loco) {
|
2005-01-19 19:15:03 +00:00
|
|
|
if (src->subtype != TS_Front_Engine) {
|
2005-01-15 19:06:22 +00:00
|
|
|
// setting the type to 0 also involves setting up the orders field.
|
2005-01-19 19:15:03 +00:00
|
|
|
src->subtype = TS_Front_Engine;
|
2005-01-15 19:06:22 +00:00
|
|
|
assert(src->orders == NULL);
|
2004-08-09 17:04:08 +00:00
|
|
|
src->num_orders = 0;
|
|
|
|
}
|
|
|
|
dst_head = src;
|
|
|
|
} else {
|
2005-01-19 19:15:03 +00:00
|
|
|
src->subtype = TS_Free_Car;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
src->u.rail.first_engine = 0xffff;
|
|
|
|
} else {
|
2005-01-19 19:15:03 +00:00
|
|
|
if (src->subtype == TS_Front_Engine) {
|
2005-01-15 19:06:22 +00:00
|
|
|
// the vehicle was previously a loco. need to free the order list and delete vehicle windows etc.
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, src->index);
|
2005-01-15 19:06:22 +00:00
|
|
|
DeleteVehicleOrders(src);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
src->subtype = TS_Not_First;
|
2004-08-09 17:04:08 +00:00
|
|
|
src->unitnumber = 0; // doesn't occupy a unitnumber anymore.
|
|
|
|
|
|
|
|
// setup first_engine
|
|
|
|
src->u.rail.first_engine = dst->u.rail.first_engine;
|
2005-01-19 19:15:03 +00:00
|
|
|
if (src->u.rail.first_engine == 0xffff && dst->subtype == TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
src->u.rail.first_engine = dst->engine_type;
|
|
|
|
|
|
|
|
// link in the wagon(s) in the chain.
|
|
|
|
{
|
|
|
|
Vehicle *v = src;
|
|
|
|
while (v->next != NULL) {
|
|
|
|
v->next->u.rail.first_engine = v->u.rail.first_engine;
|
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
v->next = dst->next;
|
|
|
|
}
|
|
|
|
dst->next = src;
|
|
|
|
}
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (src_head->subtype == TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTrainAcceleration(src_head);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index);
|
|
|
|
|
|
|
|
if (dst_head) {
|
2005-01-19 19:15:03 +00:00
|
|
|
if (dst_head->subtype == TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTrainAcceleration(dst_head);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
|
2004-12-20 18:11:22 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p1 = train to start / stop */
|
|
|
|
int32 CmdStartStopTrain(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->u.rail.days_since_order_progr = 0;
|
|
|
|
v->vehstatus ^= VS_STOPPED;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// p1 = wagon/loco index
|
|
|
|
// p2 = mode
|
|
|
|
// 0: sell just the vehicle
|
|
|
|
// 1: sell the vehicle and all vehicles that follow it in the chain
|
|
|
|
// 2: when selling attached locos, rearrange all vehicles after it to separate lines.
|
|
|
|
int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v, *first,*last;
|
|
|
|
int32 cost;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 18:51:08 +00:00
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// get first vehicle in chain
|
|
|
|
first = v;
|
2005-01-19 19:15:03 +00:00
|
|
|
if (first->subtype != TS_Front_Engine) {
|
2004-08-09 17:04:08 +00:00
|
|
|
first = GetFirstVehicleInChain(first);
|
2004-09-14 08:13:15 +00:00
|
|
|
last = GetLastVehicleInChain(first);
|
|
|
|
//now if:
|
|
|
|
// 1) we delete a whole a chain, and
|
2004-09-14 14:26:12 +00:00
|
|
|
// 2) we don't actually try to delete the last engine
|
|
|
|
// 3) the first and the last vehicle of that train are of the same type, and
|
|
|
|
// 4) the first and the last vehicle of the chain are not identical
|
|
|
|
// 5) and of "engine" type (i.e. not a carriage)
|
2004-09-14 08:13:15 +00:00
|
|
|
// then let the last vehicle live
|
2005-01-19 19:15:03 +00:00
|
|
|
if ( (p2 == 1) && (v != last) && ( last->engine_type == first->engine_type ) && (last != first) && (first->subtype == TS_Front_Engine) )
|
2004-09-14 08:13:15 +00:00
|
|
|
last = GetPrevVehicleInChain(last);
|
|
|
|
else
|
|
|
|
last = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
if (p2 != 1) {
|
|
|
|
// sell last part of multiheaded?
|
|
|
|
last = GetLastVehicleInChain(v);
|
2004-08-17 09:29:26 +00:00
|
|
|
// Check if the end-part is the same engine and check if it is the rear-end
|
|
|
|
if (last->engine_type != first->engine_type || is_firsthead_sprite(last->spritenum))
|
2004-08-09 17:04:08 +00:00
|
|
|
last = NULL;
|
|
|
|
} else {
|
|
|
|
last = NULL;
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// make sure the vehicle is stopped in the depot
|
2005-02-06 10:18:47 +00:00
|
|
|
if (CheckTrainStoppedInDepot(first) < 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
// always redraw the depot. maybe redraw train list
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
|
2005-01-19 19:15:03 +00:00
|
|
|
if (first->subtype == TS_Front_Engine) {
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-09-06 18:15:13 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
// when selling an attached locomotive. we need to delete its window.
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype == TS_Front_Engine) {
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// rearrange all vehicles that follow to separate lines.
|
|
|
|
if (p2 == 2) {
|
|
|
|
Vehicle *u,*tmp;
|
|
|
|
u = v->next;
|
|
|
|
while (u != last) {
|
|
|
|
tmp = u;
|
|
|
|
u = u->next;
|
2004-09-10 19:02:27 +00:00
|
|
|
DoCommandByTile(tmp->tile, tmp->index | ((-1)<<16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete the vehicles
|
|
|
|
cost = 0;
|
|
|
|
for(;;) {
|
|
|
|
Vehicle *tmp;
|
|
|
|
|
|
|
|
assert(first);
|
|
|
|
first = UnlinkWagon(v, first);
|
|
|
|
cost -= v->value;
|
|
|
|
tmp = v;
|
|
|
|
DeleteVehicle(tmp);
|
2004-09-14 14:26:12 +00:00
|
|
|
if ( v == last ) {
|
|
|
|
last = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( (v=v->next) == last || p2 != 1) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// delete last vehicle of multiheaded train?
|
|
|
|
if (last) {
|
|
|
|
first = UnlinkWagon(last, first);
|
|
|
|
cost -= last->value;
|
|
|
|
DeleteVehicle(last);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// an attached train changed?
|
2005-01-19 19:15:03 +00:00
|
|
|
if (first && first->subtype == TS_Front_Engine) {
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTrainAcceleration(first);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cost = 0;
|
|
|
|
for(;;) {
|
|
|
|
cost -= v->value;
|
2004-09-14 14:26:12 +00:00
|
|
|
if ( v == last ) {
|
|
|
|
last = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( (v=v->next) == last || p2 != 1) break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
if (last) cost -= last->value;
|
|
|
|
}
|
2005-01-23 22:01:51 +00:00
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateTrainDeltaXY(Vehicle *v, int direction)
|
|
|
|
{
|
|
|
|
#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[direction];
|
|
|
|
|
|
|
|
v->x_offs = (byte)x;
|
|
|
|
v->y_offs = (byte)(x>>=8);
|
|
|
|
v->sprite_width = (byte)(x>>=8);
|
|
|
|
v->sprite_height = (byte)(x>>=8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UpdateVarsAfterSwap(Vehicle *v)
|
|
|
|
{
|
|
|
|
UpdateTrainDeltaXY(v, v->direction);
|
|
|
|
v->cur_image = GetTrainImage(v, v->direction);
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
static void SetLastSpeed(Vehicle *v, int spd) {
|
2004-08-09 17:04:08 +00:00
|
|
|
int old = v->u.rail.last_speed;
|
|
|
|
if (spd != old) {
|
|
|
|
v->u.rail.last_speed = spd;
|
|
|
|
if (_patches.vehicle_speed || !old != !spd)
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2)
|
|
|
|
{
|
|
|
|
byte flag1, flag2;
|
|
|
|
|
|
|
|
flag1 = *swap_flag1;
|
|
|
|
flag2 = *swap_flag2;
|
|
|
|
|
|
|
|
/* Clear the flags */
|
|
|
|
CLRBIT(*swap_flag1, VRF_GOINGUP);
|
|
|
|
CLRBIT(*swap_flag1, VRF_GOINGDOWN);
|
|
|
|
CLRBIT(*swap_flag2, VRF_GOINGUP);
|
|
|
|
CLRBIT(*swap_flag2, VRF_GOINGDOWN);
|
|
|
|
|
|
|
|
/* Reverse the rail-flags (if needed) */
|
|
|
|
if (HASBIT(flag1, VRF_GOINGUP)) {
|
|
|
|
SETBIT(*swap_flag2, VRF_GOINGDOWN);
|
|
|
|
} else if (HASBIT(flag1, VRF_GOINGDOWN)) {
|
|
|
|
SETBIT(*swap_flag2, VRF_GOINGUP);
|
|
|
|
}
|
|
|
|
if (HASBIT(flag2, VRF_GOINGUP)) {
|
|
|
|
SETBIT(*swap_flag1, VRF_GOINGDOWN);
|
|
|
|
} else if (HASBIT(flag2, VRF_GOINGDOWN)) {
|
|
|
|
SETBIT(*swap_flag1, VRF_GOINGUP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
|
|
|
|
{
|
|
|
|
Vehicle *a, *b;
|
|
|
|
|
|
|
|
/* locate vehicles to swap */
|
|
|
|
for(a=v; l!=0; l--) { a = a->next; }
|
|
|
|
for(b=v; r!=0; r--) { b = b->next; }
|
|
|
|
|
|
|
|
if (a != b) {
|
|
|
|
/* swap the hidden bits */
|
|
|
|
{
|
|
|
|
uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN);
|
|
|
|
b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN);
|
|
|
|
a->vehstatus = tmp;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* swap variables */
|
|
|
|
swap_byte(&a->u.rail.track, &b->u.rail.track);
|
|
|
|
swap_byte(&a->direction, &b->direction);
|
|
|
|
|
|
|
|
/* toggle direction */
|
|
|
|
if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
|
|
|
|
if (!(b->u.rail.track & 0x80)) b->direction ^= 4;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* swap more variables */
|
2005-01-25 21:43:57 +00:00
|
|
|
swap_int32(&a->x_pos, &b->x_pos);
|
|
|
|
swap_int32(&a->y_pos, &b->y_pos);
|
2004-08-09 17:04:08 +00:00
|
|
|
swap_tile(&a->tile, &b->tile);
|
|
|
|
swap_byte(&a->z_pos, &b->z_pos);
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* update other vars */
|
|
|
|
UpdateVarsAfterSwap(a);
|
|
|
|
UpdateVarsAfterSwap(b);
|
|
|
|
} else {
|
|
|
|
if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
|
2004-09-10 19:02:27 +00:00
|
|
|
UpdateVarsAfterSwap(a);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-21 16:17:27 +00:00
|
|
|
/* Check if the vehicle is a train and is on the tile we are testing */
|
|
|
|
static void *TestTrainOnCrossing(Vehicle *v, void *data)
|
|
|
|
{
|
|
|
|
if (v->tile != *(const TileIndex*)data || v->type != VEH_Train)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2005-01-23 10:40:54 +00:00
|
|
|
static void DisableTrainCrossing(TileIndex tile)
|
|
|
|
{
|
|
|
|
/* Test if we have a rail/road-crossing */
|
|
|
|
if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) {
|
|
|
|
/* Check if there is a train on the tile itself */
|
|
|
|
if (VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL) {
|
|
|
|
/* If light is on, switch light off */
|
|
|
|
if (_map5[tile] & 4) {
|
|
|
|
_map5[tile] &= ~4;
|
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void ReverseTrainDirection(Vehicle *v)
|
|
|
|
{
|
|
|
|
int l = 0, r = -1;
|
|
|
|
Vehicle *u;
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
if (IsTileDepotType(v->tile, TRANSPORT_RAIL))
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
|
2004-12-21 16:02:14 +00:00
|
|
|
/* Check if we were approaching a rail/road-crossing */
|
|
|
|
{
|
|
|
|
TileIndex tile = v->tile;
|
|
|
|
int t;
|
|
|
|
/* Determine the non-diagonal direction in which we will exit this tile */
|
|
|
|
t = v->direction >> 1;
|
|
|
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
|
|
|
|
t = (t - 1) & 3;
|
|
|
|
}
|
|
|
|
/* Calculate next tile */
|
2005-01-05 13:32:03 +00:00
|
|
|
tile += TileOffsByDir(t);
|
2005-01-23 10:40:54 +00:00
|
|
|
|
|
|
|
/* Check if the train left a rail/road-crossing */
|
|
|
|
DisableTrainCrossing(tile);
|
2004-12-21 16:02:14 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// count number of vehicles
|
|
|
|
u = v;
|
|
|
|
do r++; while ( (u = u->next) != NULL );
|
|
|
|
|
|
|
|
/* swap start<>end, start+1<>end-1, ... */
|
|
|
|
do {
|
|
|
|
ReverseTrainSwapVeh(v, l++, r--);
|
|
|
|
} while (l <= r);
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
if (IsTileDepotType(v->tile, TRANSPORT_RAIL))
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
CLRBIT(v->u.rail.flags, VRF_REVERSING);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* p1 = vehicle */
|
|
|
|
int32 CmdReverseTrainDirection(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
_error_message = STR_EMPTY;
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
// if (v->u.rail.track & 0x80 || IsTileDepotType(v->tile, TRANSPORT_RAIL))
|
2004-08-09 17:04:08 +00:00
|
|
|
// return CMD_ERROR;
|
|
|
|
|
|
|
|
if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0)
|
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
if (_patches.realistic_acceleration && v->cur_speed != 0) {
|
2005-01-09 16:02:06 +00:00
|
|
|
TOGGLEBIT(v->u.rail.flags, VRF_REVERSING);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
SetLastSpeed(v, 0);
|
|
|
|
ReverseTrainDirection(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 CmdForceTrainProceed(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC)
|
|
|
|
v->u.rail.force_proceed = 0x50;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// p1 = vehicle to refit
|
2005-01-02 17:23:04 +00:00
|
|
|
// p2 = new cargo (0xFF)
|
|
|
|
// p2 = skip check for stopped in hanger (0x0100)
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
int32 cost;
|
|
|
|
uint num;
|
|
|
|
|
2005-01-02 17:23:04 +00:00
|
|
|
byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8;
|
|
|
|
|
|
|
|
p2 = p2 & 0xFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
v = GetVehicle(p1);
|
2005-01-30 20:50:06 +00:00
|
|
|
|
2005-02-06 10:18:47 +00:00
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner) || ((CheckTrainStoppedInDepot(v) < 0) && !(SkipStoppedInDepotCheck)))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
cost = 0;
|
|
|
|
num = 0;
|
2005-01-06 18:45:28 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2004-11-22 23:05:34 +00:00
|
|
|
/* XXX: We also refit all the attached wagons en-masse if they
|
|
|
|
* can be refitted. This is how TTDPatch does it. TODO: Have
|
|
|
|
* some nice [Refit] button near each wagon. --pasky */
|
2004-12-04 07:41:37 +00:00
|
|
|
if ((!(RailVehInfo(v->engine_type)->flags & RVI_WAGON)
|
2004-11-22 23:05:34 +00:00
|
|
|
|| (_engine_refit_masks[v->engine_type] & (1 << p2)))
|
|
|
|
&& (byte) p2 != v->cargo_type && v->cargo_cap != 0) {
|
2004-08-10 21:28:43 +00:00
|
|
|
cost += (_price.build_railvehicle >> 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
num += v->cargo_cap;
|
|
|
|
if (flags & DC_EXEC) {
|
2005-01-02 17:23:04 +00:00
|
|
|
//autorefitted train cars wants to keep the cargo
|
|
|
|
//it will be checked if the cargo is valid in CmdReplaceVehicle
|
|
|
|
if (!(SkipStoppedInDepotCheck))
|
|
|
|
v->cargo_count = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cargo_type = (byte)p2;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2005-01-02 17:23:04 +00:00
|
|
|
// SkipStoppedInDepotCheck is called by CmdReplace and it should only apply to the single car it is called for
|
|
|
|
} while ( (v=v->next) != NULL || SkipStoppedInDepotCheck );
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
_returned_refit_amount = num;
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct TrainFindDepotData {
|
|
|
|
uint best_length;
|
|
|
|
uint tile;
|
|
|
|
byte owner;
|
|
|
|
} TrainFindDepotData;
|
|
|
|
|
|
|
|
static bool TrainFindDepotEnumProc(uint tile, TrainFindDepotData *tfdd, int track, uint length, byte *state)
|
|
|
|
{
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) && _map_owner[tile] == tfdd->owner) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((_map5[tile] & ~0x3) == 0xC0) {
|
|
|
|
if (length < tfdd->best_length) {
|
|
|
|
tfdd->best_length = length;
|
|
|
|
tfdd->tile = tile;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make sure the train doesn't run against a oneway signal
|
|
|
|
if ((_map5[tile] & 0xC0) == 0x40) {
|
2005-01-31 11:23:10 +00:00
|
|
|
if (!(_map3_lo[tile] & _signal_along_trackdir[track]) && _map3_lo[tile] & _signal_against_trackdir[track])
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// stop searching if we've found a destination that is closer already.
|
|
|
|
return length >= tfdd->best_length;
|
|
|
|
}
|
|
|
|
|
2004-09-23 21:20:38 +00:00
|
|
|
// returns the tile of a depot to goto to.
|
|
|
|
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
TrainFindDepotData tfdd;
|
|
|
|
uint tile = v->tile;
|
|
|
|
|
|
|
|
tfdd.owner = v->owner;
|
|
|
|
tfdd.best_length = (uint)-1;
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
if (IsTileDepotType(tile, TRANSPORT_RAIL)){
|
2004-09-23 21:20:38 +00:00
|
|
|
tfdd.tile = tile;
|
|
|
|
tfdd.best_length = 0;
|
|
|
|
return tfdd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->u.rail.track == 0x40) { tile = GetVehicleOutOfTunnelTile(v); }
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (_patches.new_pathfinding_all) {
|
|
|
|
NPFFoundTargetData ftd;
|
|
|
|
byte trackdir = _track_direction_to_trackdir[FIND_FIRST_BIT(v->u.rail.track)][v->direction];
|
2005-02-06 22:36:08 +00:00
|
|
|
ftd = NPFRouteToDepotBreadthFirst(v->tile, trackdir, TRANSPORT_RAIL, v->owner);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (ftd.best_bird_dist == 0) {
|
|
|
|
/* Found target */
|
|
|
|
tfdd.tile = ftd.node.tile;
|
|
|
|
tfdd.best_length = ftd.best_path_dist;
|
|
|
|
}
|
|
|
|
} else if (!_patches.new_depot_finding) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// search in all directions
|
|
|
|
for(i=0; i!=4; i++)
|
|
|
|
NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL);
|
|
|
|
} else {
|
|
|
|
// search in the forward direction first.
|
|
|
|
i = v->direction >> 1;
|
|
|
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; }
|
|
|
|
NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL);
|
2004-09-23 21:20:38 +00:00
|
|
|
if (tfdd.best_length == (uint)-1){
|
|
|
|
// search in backwards direction
|
|
|
|
i = (v->direction^4) >> 1;
|
|
|
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; }
|
|
|
|
NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-09-23 21:20:38 +00:00
|
|
|
return tfdd;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-29 12:01:36 +00:00
|
|
|
/* Send a train to the nearest depot
|
|
|
|
p1 = index of the train
|
|
|
|
p2 = bit 0 = do not stop in depot
|
|
|
|
bit 1 = set v->set_for_replacement
|
|
|
|
bit 2 = clear v->set_for_replacement */
|
2004-08-09 17:04:08 +00:00
|
|
|
int32 CmdTrainGotoDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2005-01-30 20:50:06 +00:00
|
|
|
Vehicle *v;
|
2004-09-23 21:20:38 +00:00
|
|
|
TrainFindDepotData tfdd;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
|
|
|
|
|
|
|
v = GetVehicle(p1);
|
|
|
|
|
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner))
|
2005-01-29 12:01:36 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (HASBIT(p2, 0)) v->set_for_replacement = true;
|
|
|
|
if (HASBIT(p2, 2)) v->set_for_replacement = false;
|
|
|
|
|
|
|
|
if (HASBIT(p2, 1) || HASBIT(p2, 2)) return CMD_ERROR; // vehicle has a depot in schedule. It just needed to alter set_for_replacement
|
|
|
|
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.flags & OF_UNLOAD) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.rail.days_since_order_progr = 0;
|
|
|
|
v->cur_order_index++;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-09-23 21:20:38 +00:00
|
|
|
tfdd = FindClosestTrainDepot(v);
|
|
|
|
if (tfdd.best_length == (uint)-1)
|
2004-08-09 17:04:08 +00:00
|
|
|
return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2004-09-23 21:20:38 +00:00
|
|
|
v->dest_tile = tfdd.tile;
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
2005-01-29 12:01:36 +00:00
|
|
|
v->current_order.flags = HASBIT(p2, 0) ? 0 : OF_NON_STOP | OF_FULL_LOAD;
|
2005-02-06 10:18:47 +00:00
|
|
|
v->current_order.station = GetDepotByTile(tfdd.tile)->index;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* p1 = vehicle
|
|
|
|
* p2 = new service int
|
|
|
|
*/
|
|
|
|
int32 CmdChangeTrainServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2)
|
|
|
|
{
|
2005-01-30 20:50:06 +00:00
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
if (!IsVehicleIndex(p1)) return CMD_ERROR;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-30 20:50:06 +00:00
|
|
|
v = GetVehicle(p1);
|
|
|
|
|
|
|
|
if (v->type != VEH_Train || !CheckOwnership(v->owner))
|
2004-08-09 17:04:08 +00:00
|
|
|
return CMD_ERROR;
|
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
v->service_interval = (uint16)p2;
|
|
|
|
InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 8);
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void OnTick_Train(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int8 _vehicle_smoke_pos[16] = {
|
|
|
|
-4, -4, -4, 0, 4, 4, 4, 0,
|
|
|
|
-4, 0, 4, 4, 4, 0,-4,-4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void HandleLocomotiveSmokeCloud(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *u;
|
|
|
|
|
|
|
|
if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
u = v;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int engtype = v->engine_type;
|
|
|
|
|
|
|
|
// no smoke?
|
2004-12-04 07:41:37 +00:00
|
|
|
if (RailVehInfo(engtype)->flags & 2
|
2004-08-09 17:04:08 +00:00
|
|
|
|| _engines[engtype].railtype > 0
|
|
|
|
|| (v->vehstatus&VS_HIDDEN) || (v->u.rail.track & 0xC0) )
|
|
|
|
continue;
|
|
|
|
|
2004-12-04 07:41:37 +00:00
|
|
|
switch (RailVehInfo(engtype)->engclass) {
|
2004-08-09 17:04:08 +00:00
|
|
|
case 0:
|
|
|
|
// steam smoke.
|
2005-02-06 22:36:08 +00:00
|
|
|
if ( (v->tick_counter&0xF) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile)) {
|
2004-09-10 19:02:27 +00:00
|
|
|
CreateEffectVehicleRel(v,
|
2004-08-09 17:04:08 +00:00
|
|
|
(_vehicle_smoke_pos[v->direction]),
|
|
|
|
(_vehicle_smoke_pos[v->direction+8]),
|
|
|
|
10,
|
|
|
|
EV_STEAM_SMOKE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// diesel smoke
|
2005-02-06 22:36:08 +00:00
|
|
|
if (u->cur_speed <= 40 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) {
|
2004-08-09 17:04:08 +00:00
|
|
|
CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_3);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// blue spark
|
2005-02-06 22:36:08 +00:00
|
|
|
if ( (v->tick_counter&0x3) == 0 && !IsTileDepotType(v->tile, TRANSPORT_RAIL) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_2);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ( (v = v->next) != NULL );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TrainPlayLeaveStationSound(Vehicle *v)
|
|
|
|
{
|
2004-12-04 09:26:39 +00:00
|
|
|
static const SoundFx sfx[] = {
|
|
|
|
SND_04_TRAIN,
|
|
|
|
SND_0A_TRAIN_HORN,
|
|
|
|
SND_0A_TRAIN_HORN
|
|
|
|
};
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
int engtype = v->engine_type;
|
|
|
|
|
|
|
|
switch (_engines[engtype].railtype) {
|
2004-11-05 23:12:33 +00:00
|
|
|
case 0:
|
2004-12-04 07:41:37 +00:00
|
|
|
SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v);
|
2004-11-05 23:12:33 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
SndPlayVehicleFx(SND_47_MAGLEV_2, v);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
SndPlayVehicleFx(SND_41_MAGLEV, v);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CheckTrainStayInDepot(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *u;
|
2004-11-23 20:25:18 +00:00
|
|
|
|
|
|
|
// bail out if not all wagons are in the same depot or not in a depot at all
|
2004-12-21 16:00:14 +00:00
|
|
|
for (u = v; u != NULL; u = u->next)
|
2004-11-23 20:25:18 +00:00
|
|
|
if (u->u.rail.track != 0x80 || u->tile != v->tile)
|
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (v->u.rail.force_proceed == 0) {
|
2005-01-24 22:24:47 +00:00
|
|
|
if (++v->load_unload_time_rem < 37) {
|
|
|
|
InvalidateWindowClasses(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
2005-01-24 22:24:47 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->load_unload_time_rem = 0;
|
|
|
|
|
2005-01-24 22:24:47 +00:00
|
|
|
if (UpdateSignalsOnSegment(v->tile, v->direction)) {
|
|
|
|
InvalidateWindowClasses(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
2005-01-24 22:24:47 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
2005-01-24 22:24:47 +00:00
|
|
|
InvalidateWindowClasses(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
TrainPlayLeaveStationSound(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.rail.track = 1;
|
|
|
|
if (v->direction & 2)
|
|
|
|
v->u.rail.track = 2;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus &= ~VS_HIDDEN;
|
|
|
|
v->cur_speed = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
UpdateTrainDeltaXY(v, v->direction);
|
|
|
|
v->cur_image = GetTrainImage(v, v->direction);
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
UpdateSignalsOnSegment(v->tile, v->direction);
|
|
|
|
UpdateTrainAcceleration(v);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Check for station tiles */
|
2004-08-09 17:04:08 +00:00
|
|
|
typedef struct TrainTrackFollowerData {
|
|
|
|
TileIndex dest_coords;
|
|
|
|
int station_index; // station index we're heading for
|
|
|
|
uint best_bird_dist;
|
|
|
|
uint best_track_dist;
|
|
|
|
byte best_track;
|
|
|
|
} TrainTrackFollowerData;
|
|
|
|
|
|
|
|
static bool TrainTrackFollower(uint tile, TrainTrackFollowerData *ttfd, int track, uint length, byte *state){
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) && (_map5[tile]&0xC0) == 0x40) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// the tile has a signal
|
|
|
|
byte m3 = _map3_lo[tile];
|
2005-01-31 11:23:10 +00:00
|
|
|
if (!(m3 & _signal_along_trackdir[track])) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// if one way signal not pointing towards us, stop going in this direction.
|
2005-01-31 11:23:10 +00:00
|
|
|
if (m3 & _signal_against_trackdir[track])
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else if (_map2[tile] & _signal_along_trackdir[track]) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// green signal in our direction. either one way or two way.
|
|
|
|
*state = true;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else if (m3 & _signal_against_trackdir[track]) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// two way signal. unless we passed another green signal on the way,
|
|
|
|
// stop going in this direction.
|
|
|
|
if (!*state) return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// heading for nowhere?
|
|
|
|
if (ttfd->dest_coords == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// did we reach the final station?
|
2004-09-06 18:33:51 +00:00
|
|
|
if ((ttfd->station_index == -1 && tile == ttfd->dest_coords) ||
|
2005-01-16 11:24:58 +00:00
|
|
|
(IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8) && _map2[tile] == ttfd->station_index)) {
|
2004-09-06 18:33:51 +00:00
|
|
|
/* We do not check for dest_coords if we have a station_index,
|
|
|
|
* because in that case the dest_coords are just an
|
|
|
|
* approximation of where the station is */
|
2004-08-09 17:04:08 +00:00
|
|
|
// found station
|
|
|
|
ttfd->best_bird_dist = 0;
|
|
|
|
if (length < ttfd->best_track_dist) {
|
|
|
|
ttfd->best_track_dist = length;
|
|
|
|
ttfd->best_track = state[1];
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
uint dist;
|
|
|
|
|
|
|
|
// we've actually found the destination already. no point searching in directions longer than this.
|
|
|
|
if (ttfd->best_track_dist != (uint)-1)
|
|
|
|
return length >= ttfd->best_track_dist;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// didn't find station
|
2005-01-31 07:23:15 +00:00
|
|
|
dist = DistanceManhattan(tile, ttfd->dest_coords);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (dist < ttfd->best_bird_dist) {
|
|
|
|
ttfd->best_bird_dist = dist;
|
|
|
|
ttfd->best_track = state[1];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FillWithStationData(TrainTrackFollowerData *fd, Vehicle *v)
|
|
|
|
{
|
2004-08-21 23:56:01 +00:00
|
|
|
fd->dest_coords = v->dest_tile;
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION)
|
|
|
|
fd->station_index = v->current_order.station;
|
2004-08-21 23:56:01 +00:00
|
|
|
else
|
|
|
|
fd->station_index = -1;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _initial_tile_subcoord[6][4][3] = {
|
|
|
|
{{ 15, 8, 1 },{ 0, 0, 0 },{ 0, 8, 5 },{ 0, 0, 0 }},
|
|
|
|
{{ 0, 0, 0 },{ 8, 0, 3 },{ 0, 0, 0 },{ 8,15, 7 }},
|
|
|
|
{{ 0, 0, 0 },{ 7, 0, 2 },{ 0, 7, 6 },{ 0, 0, 0 }},
|
|
|
|
{{ 15, 8, 2 },{ 0, 0, 0 },{ 0, 0, 0 },{ 8,15, 6 }},
|
|
|
|
{{ 15, 7, 0 },{ 8, 0, 4 },{ 0, 0, 0 },{ 0, 0, 0 }},
|
|
|
|
{{ 0, 0, 0 },{ 0, 0, 0 },{ 0, 8, 4 },{ 7,15, 0 }},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const uint32 _reachable_tracks[4] = {
|
|
|
|
0x10091009,
|
|
|
|
0x00160016,
|
|
|
|
0x05200520,
|
|
|
|
0x2A002A00,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _search_directions[6][4] = {
|
|
|
|
{ 0, 9, 2, 9 }, // track 1
|
|
|
|
{ 9, 1, 9, 3 }, // track 2
|
|
|
|
{ 9, 0, 3, 9 }, // track upper
|
|
|
|
{ 1, 9, 9, 2 }, // track lower
|
|
|
|
{ 3, 2, 9, 9 }, // track left
|
|
|
|
{ 9, 9, 1, 0 }, // track right
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0};
|
2005-01-31 11:23:10 +00:00
|
|
|
#if PF_BENCHMARK
|
|
|
|
unsigned int rdtsc()
|
|
|
|
{
|
|
|
|
unsigned int high, low;
|
|
|
|
|
|
|
|
__asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
|
|
|
|
return low;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* choose a track */
|
2005-01-31 11:23:10 +00:00
|
|
|
static byte ChooseTrainTrack(Vehicle *v, uint tile, int enterdir, byte trackbits)
|
2004-09-10 19:02:27 +00:00
|
|
|
{
|
2004-08-09 17:04:08 +00:00
|
|
|
TrainTrackFollowerData fd;
|
|
|
|
int bits = trackbits;
|
|
|
|
uint best_track;
|
2005-01-31 11:23:10 +00:00
|
|
|
#if PF_BENCHMARK
|
2004-08-09 17:04:08 +00:00
|
|
|
int time = rdtsc();
|
|
|
|
static float f;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
assert( (bits & ~0x3F) == 0);
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* quick return in case only one possible track is available */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (KILL_FIRST_BIT(bits) == 0)
|
|
|
|
return FIND_FIRST_BIT(bits);
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
|
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
NPFFoundTargetData ftd;
|
|
|
|
byte trackdir;
|
|
|
|
|
|
|
|
NPFFillWithOrderData(&fstd, v);
|
|
|
|
/* The enterdir for the new tile, is the exitdir for the old tile */
|
|
|
|
trackdir = _track_exitdir_to_trackdir[FIND_FIRST_BIT(v->u.rail.track)][enterdir];
|
|
|
|
assert(trackdir != 0xff);
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
ftd = NPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (ftd.best_bird_dist != 0 || ftd.best_trackdir == 0xff) {
|
|
|
|
/* Not found, or we are already there. Just do something */
|
|
|
|
//TODO: maybe display error?
|
|
|
|
//TODO: go straight ahead if possible?
|
2004-08-09 17:04:08 +00:00
|
|
|
best_track = FIND_FIRST_BIT(bits);
|
|
|
|
} else {
|
2005-01-31 11:23:10 +00:00
|
|
|
/* Discard enterdir information, making it a normal track */
|
|
|
|
best_track = ftd.best_trackdir & 7; /* TODO: Wrapper function? */
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
FillWithStationData(&fd, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (_patches.new_pathfinding) {
|
|
|
|
/* New train pathfinding */
|
2004-08-09 17:04:08 +00:00
|
|
|
fd.best_bird_dist = (uint)-1;
|
|
|
|
fd.best_track_dist = (uint)-1;
|
2005-01-31 11:23:10 +00:00
|
|
|
fd.best_track = 0xFF;
|
|
|
|
NewTrainPathfind(tile - TileOffsByDir(enterdir), enterdir, (TPFEnumProc*)TrainTrackFollower, &fd, NULL);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
// printf("Train %d %s\n", v->unitnumber, fd.best_track_dist == -1 ? "NOTFOUND" : "FOUND");
|
|
|
|
|
|
|
|
if (fd.best_track == 0xff) {
|
|
|
|
// blaha
|
|
|
|
best_track = FIND_FIRST_BIT(bits);
|
|
|
|
} else {
|
|
|
|
best_track = fd.best_track & 7;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Original pathfinding */
|
|
|
|
int i, r;
|
|
|
|
uint best_bird_dist = 0;
|
|
|
|
uint best_track_dist = 0;
|
|
|
|
byte train_dir = v->direction & 3;
|
|
|
|
|
|
|
|
|
|
|
|
best_track = (uint)-1;
|
|
|
|
|
|
|
|
do {
|
|
|
|
i = FIND_FIRST_BIT(bits);
|
|
|
|
bits = KILL_FIRST_BIT(bits);
|
|
|
|
|
|
|
|
fd.best_bird_dist = (uint)-1;
|
|
|
|
fd.best_track_dist = (uint)-1;
|
|
|
|
|
|
|
|
NewTrainPathfind(tile, _search_directions[i][enterdir], (TPFEnumProc*)TrainTrackFollower, &fd, NULL);
|
|
|
|
if (best_track != (uint)-1) {
|
|
|
|
if (best_track_dist == (uint)-1) {
|
|
|
|
if (fd.best_track_dist == (uint)-1) {
|
|
|
|
/* neither reached the destination, pick the one with the smallest bird dist */
|
|
|
|
if (fd.best_bird_dist > best_bird_dist) goto bad;
|
|
|
|
if (fd.best_bird_dist < best_bird_dist) goto good;
|
|
|
|
} else {
|
|
|
|
/* we found the destination for the first time */
|
|
|
|
goto good;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-31 11:23:10 +00:00
|
|
|
if (fd.best_track_dist == (uint)-1) {
|
|
|
|
/* didn't find destination, but we've found the destination previously */
|
|
|
|
goto bad;
|
|
|
|
} else {
|
|
|
|
/* both old & new reached the destination, compare track length */
|
|
|
|
if (fd.best_track_dist > best_track_dist) goto bad;
|
|
|
|
if (fd.best_track_dist < best_track_dist) goto good;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* if we reach this position, there's two paths of equal value so far.
|
|
|
|
* pick one randomly. */
|
|
|
|
r = (byte)Random();
|
|
|
|
if (_pick_track_table[i] == train_dir) r += 80;
|
|
|
|
if (_pick_track_table[best_track] == train_dir) r -= 80;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (r <= 127) goto bad;
|
|
|
|
}
|
|
|
|
good:;
|
|
|
|
best_track = i;
|
|
|
|
best_bird_dist = fd.best_bird_dist;
|
|
|
|
best_track_dist = fd.best_track_dist;
|
|
|
|
bad:;
|
|
|
|
} while (bits != 0);
|
|
|
|
// printf("Train %d %s\n", v->unitnumber, best_track_dist == -1 ? "NOTFOUND" : "FOUND");
|
|
|
|
assert(best_track != (uint)-1);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
#if PF_BENCHMARK
|
2004-08-09 17:04:08 +00:00
|
|
|
time = rdtsc() - time;
|
|
|
|
f = f * 0.99 + 0.01 * time;
|
|
|
|
printf("PF time = %d %f\n", time, f);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return best_track;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool CheckReverseTrain(Vehicle *v)
|
|
|
|
{
|
|
|
|
TrainTrackFollowerData fd;
|
|
|
|
int i, r;
|
|
|
|
int best_track;
|
|
|
|
uint best_bird_dist = 0;
|
|
|
|
uint best_track_dist = 0;
|
|
|
|
uint reverse, reverse_best;
|
|
|
|
|
|
|
|
if (_opt.diff.line_reverse_mode != 0 ||
|
|
|
|
v->u.rail.track & 0xC0 ||
|
|
|
|
!(v->direction & 1))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
FillWithStationData(&fd, v);
|
|
|
|
|
|
|
|
best_track = -1;
|
|
|
|
reverse_best = reverse = 0;
|
|
|
|
|
|
|
|
assert(v->u.rail.track);
|
|
|
|
|
|
|
|
i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][v->direction>>1];
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
|
|
|
|
NPFFindStationOrTileData fstd;
|
|
|
|
NPFFoundTargetData ftd;
|
|
|
|
byte trackdir, trackdir_rev;
|
|
|
|
Vehicle* last = GetLastVehicleInChain(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
NPFFillWithOrderData(&fstd, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
trackdir = _track_direction_to_trackdir[FIND_FIRST_BIT(v->u.rail.track)][v->direction];
|
|
|
|
trackdir_rev = REVERSE_TRACKDIR(_track_direction_to_trackdir[FIND_FIRST_BIT(last->u.rail.track)][last->direction]);
|
|
|
|
assert(trackdir != 0xff);
|
|
|
|
assert(trackdir_rev != 0xff);
|
|
|
|
|
2005-02-06 22:36:08 +00:00
|
|
|
ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, v->owner);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (ftd.best_bird_dist != 0) {
|
|
|
|
/* We didn't find anything, just keep on going straight ahead */
|
|
|
|
reverse_best = false;
|
|
|
|
} else {
|
|
|
|
if (ftd.node.user_data[NPF_NODE_FLAGS] & NPF_FLAG_REVERSE)
|
|
|
|
reverse_best = true;
|
|
|
|
else
|
|
|
|
reverse_best = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while(true) {
|
|
|
|
fd.best_bird_dist = (uint)-1;
|
|
|
|
fd.best_track_dist = (uint)-1;
|
|
|
|
|
|
|
|
NewTrainPathfind(v->tile, reverse ^ i, (TPFEnumProc*)TrainTrackFollower, &fd, NULL);
|
|
|
|
|
|
|
|
if (best_track != -1) {
|
|
|
|
if (best_bird_dist != 0) {
|
|
|
|
if (fd.best_bird_dist != 0) {
|
|
|
|
/* neither reached the destination, pick the one with the smallest bird dist */
|
|
|
|
if (fd.best_bird_dist > best_bird_dist) goto bad;
|
|
|
|
if (fd.best_bird_dist < best_bird_dist) goto good;
|
|
|
|
} else {
|
|
|
|
/* we found the destination for the first time */
|
|
|
|
goto good;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-31 11:23:10 +00:00
|
|
|
if (fd.best_bird_dist != 0) {
|
|
|
|
/* didn't find destination, but we've found the destination previously */
|
|
|
|
goto bad;
|
|
|
|
} else {
|
|
|
|
/* both old & new reached the destination, compare track length */
|
|
|
|
if (fd.best_track_dist > best_track_dist) goto bad;
|
|
|
|
if (fd.best_track_dist < best_track_dist) goto good;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
/* if we reach this position, there's two paths of equal value so far.
|
|
|
|
* pick one randomly. */
|
|
|
|
r = (byte)Random();
|
|
|
|
if (_pick_track_table[i] == (v->direction & 3)) r += 80;
|
|
|
|
if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
|
|
|
|
if (r <= 127) goto bad;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
good:;
|
2005-01-31 11:23:10 +00:00
|
|
|
best_track = i;
|
|
|
|
best_bird_dist = fd.best_bird_dist;
|
|
|
|
best_track_dist = fd.best_track_dist;
|
|
|
|
reverse_best = reverse;
|
2004-08-09 17:04:08 +00:00
|
|
|
bad:;
|
2005-01-31 11:23:10 +00:00
|
|
|
if (reverse != 0)
|
|
|
|
break;
|
|
|
|
reverse = 2;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return reverse_best != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ProcessTrainOrder(Vehicle *v)
|
|
|
|
{
|
2005-01-15 19:06:22 +00:00
|
|
|
const Order *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
bool result;
|
|
|
|
|
|
|
|
// These are un-interruptible
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type >= OT_GOTO_DEPOT &&
|
|
|
|
v->current_order.type <= OT_LEAVESTATION) {
|
2005-01-15 19:06:22 +00:00
|
|
|
// Let a depot order in the orderlist interrupt.
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT ||
|
|
|
|
!(v->current_order.flags & OF_UNLOAD))
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
|
|
|
(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) == (OF_UNLOAD | OF_FULL_LOAD) &&
|
2005-01-29 12:01:36 +00:00
|
|
|
!VehicleNeedsService(v) && !v->set_for_replacement) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_order_index++;
|
|
|
|
}
|
|
|
|
|
2004-11-14 13:07:07 +00:00
|
|
|
// check if we've reached the waypoint?
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_order_index++;
|
|
|
|
}
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
// check if we've reached a non-stop station while TTDPatch nonstop is enabled..
|
2004-12-05 12:43:04 +00:00
|
|
|
if (_patches.new_nonstop && v->current_order.flags & OF_NON_STOP &&
|
2005-02-07 12:47:29 +00:00
|
|
|
v->current_order.station == _map2[v->tile] && IsTileType(v->tile, MP_STATION) ) {
|
2004-09-10 19:02:27 +00:00
|
|
|
v->cur_order_index++;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current order
|
|
|
|
if (v->cur_order_index >= v->num_orders)
|
|
|
|
v->cur_order_index = 0;
|
2005-01-15 19:06:22 +00:00
|
|
|
|
|
|
|
order = GetVehicleOrder(v, v->cur_order_index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
// If no order, do nothing.
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order == NULL) {
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
v->dest_tile = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it is unchanged, keep it.
|
2005-01-15 19:06:22 +00:00
|
|
|
if (order->type == v->current_order.type &&
|
|
|
|
order->flags == v->current_order.flags &&
|
|
|
|
order->station == v->current_order.station)
|
2004-08-09 17:04:08 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Otherwise set it, and determine the destination tile.
|
2005-01-15 19:06:22 +00:00
|
|
|
v->current_order = *order;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->dest_tile = 0;
|
|
|
|
|
|
|
|
result = false;
|
2005-01-15 19:06:22 +00:00
|
|
|
switch (order->type) {
|
|
|
|
case OT_GOTO_STATION:
|
|
|
|
if (order->station == v->last_station_visited)
|
2005-02-02 16:16:43 +00:00
|
|
|
v->last_station_visited = INVALID_STATION;
|
2005-01-15 19:06:22 +00:00
|
|
|
v->dest_tile = GetStation(order->station)->xy;
|
|
|
|
result = CheckReverseTrain(v);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_GOTO_DEPOT:
|
2005-02-06 10:18:47 +00:00
|
|
|
v->dest_tile = GetDepot(order->station)->xy;
|
2005-01-15 19:06:22 +00:00
|
|
|
result = CheckReverseTrain(v);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OT_GOTO_WAYPOINT:
|
|
|
|
v->dest_tile = _waypoints[order->station].xy;
|
|
|
|
result = CheckReverseTrain(v);
|
|
|
|
break;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MarkTrainDirty(Vehicle *v)
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
v->cur_image = GetTrainImage(v, v->direction);
|
|
|
|
MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
|
|
|
|
} while ( (v=v->next) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleTrainLoading(Vehicle *v, bool mode)
|
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_NOTHING)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type != OT_DUMMY) {
|
|
|
|
if (v->current_order.type != OT_LOADING)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (mode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// don't mark the train as lost if we're loading on the final station.
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.flags & OF_NON_STOP)
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.rail.days_since_order_progr = 0;
|
|
|
|
|
|
|
|
if (--v->load_unload_time_rem)
|
|
|
|
return;
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) {
|
2005-01-04 00:54:12 +00:00
|
|
|
v->u.rail.days_since_order_progr = 0; /* Prevent a train lost message for full loading trains */
|
2004-08-09 17:04:08 +00:00
|
|
|
SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
|
|
|
|
if (LoadUnloadVehicle(v)) {
|
|
|
|
InvalidateWindow(WC_TRAINS_LIST, v->owner);
|
|
|
|
MarkTrainDirty(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// need to update acceleration since the goods on the train changed.
|
|
|
|
UpdateTrainAcceleration(v);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
TrainPlayLeaveStationSound(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-12-05 12:43:04 +00:00
|
|
|
Order b = v->current_order;
|
|
|
|
v->current_order.type = OT_LEAVESTATION;
|
|
|
|
v->current_order.flags = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-29 12:01:36 +00:00
|
|
|
if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
|
|
|
|
// only the vehicle owner needs to calculate the rest (locally)
|
|
|
|
if ((_autoreplace_array[v->engine_type] != v->engine_type) ||
|
|
|
|
(_patches.autorenew && v->age - v->max_age > (_patches.autorenew_months * 30))) {
|
|
|
|
byte flags = 1;
|
|
|
|
// the flags means, bit 0 = needs to go to depot, bit 1 = have depot in orders
|
|
|
|
if (VehicleHasDepotOrders(v)) SETBIT(flags, 1);
|
|
|
|
if (!(HASBIT(flags, 1) && v->set_for_replacement)) {
|
|
|
|
_current_player = _local_player;
|
|
|
|
DoCommandP(v->tile, v->index, flags, NULL, CMD_TRAIN_GOTO_DEPOT | CMD_SHOW_NO_ERROR);
|
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
}
|
|
|
|
} else { // no need to go to a depot
|
|
|
|
if (v->set_for_replacement) {
|
|
|
|
// it seems that the user clicked "Stop replacing"
|
|
|
|
_current_player = _local_player;
|
|
|
|
DoCommandP(v->tile, v->index, 1 | (1 << 2), NULL, CMD_TRAIN_GOTO_DEPOT | CMD_SHOW_NO_ERROR);
|
|
|
|
_current_player = OWNER_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
// If this was not the final order, don't remove it from the list.
|
2004-12-05 12:43:04 +00:00
|
|
|
if (!(b.flags & OF_NON_STOP))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
v->u.rail.days_since_order_progr = 0;
|
|
|
|
v->cur_order_index++;
|
2005-01-15 19:06:22 +00:00
|
|
|
InvalidateVehicleOrder(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int UpdateTrainSpeed(Vehicle *v)
|
|
|
|
{
|
|
|
|
uint spd;
|
|
|
|
uint accel;
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) {
|
2005-01-26 12:51:04 +00:00
|
|
|
if (_patches.realistic_acceleration)
|
|
|
|
accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
|
|
|
|
else
|
|
|
|
accel = v->acceleration * -2;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2005-01-26 12:51:04 +00:00
|
|
|
if (_patches.realistic_acceleration)
|
|
|
|
accel = GetTrainAcceleration(v, AM_ACCEL);
|
|
|
|
else
|
|
|
|
accel = v->acceleration;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
spd = v->subspeed + accel * 2;
|
|
|
|
v->subspeed = (byte)spd;
|
2005-01-26 12:51:04 +00:00
|
|
|
{
|
|
|
|
int tempmax = v->max_speed;
|
|
|
|
if (v->cur_speed > v->max_speed)
|
|
|
|
tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
|
|
|
|
v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(v->direction & 1)) spd = spd * 3 >> 2;
|
|
|
|
|
|
|
|
spd += v->progress;
|
|
|
|
v->progress = (byte)spd;
|
|
|
|
return (spd >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TrainEnterStation(Vehicle *v, int station)
|
|
|
|
{
|
|
|
|
Station *st;
|
|
|
|
uint32 flags;
|
|
|
|
|
|
|
|
v->last_station_visited = station;
|
|
|
|
|
|
|
|
/* check if a train ever visited this station before */
|
2005-01-06 22:31:58 +00:00
|
|
|
st = GetStation(station);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
|
|
|
|
st->had_vehicle_of_type |= HVOT_TRAIN;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, st->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
flags = (v->owner == _local_player) ? NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_PLAYER, 0) : NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ARRIVAL_OTHER, 0);
|
|
|
|
AddNewsItem(
|
|
|
|
STR_8801_CITIZENS_CELEBRATE_FIRST,
|
|
|
|
flags,
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Did we reach the final destination?
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION &&
|
|
|
|
v->current_order.station == (byte)station) {
|
2004-08-09 17:04:08 +00:00
|
|
|
// Yeah, keep the load/unload flags
|
|
|
|
// Non Stop now means if the order should be increased.
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_LOADING;
|
|
|
|
v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD;
|
|
|
|
v->current_order.flags |= OF_NON_STOP;
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
|
|
|
// No, just do a simple load
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_LOADING;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.station = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
|
|
|
|
if (LoadUnloadVehicle(v) != 0) {
|
|
|
|
InvalidateWindow(WC_TRAINS_LIST, v->owner);
|
|
|
|
MarkTrainDirty(v);
|
|
|
|
UpdateTrainAcceleration(v);
|
|
|
|
}
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
byte new_z, old_z;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// need this hint so it returns the right z coordinate on bridges.
|
|
|
|
_get_z_hint = v->z_pos;
|
|
|
|
new_z = GetSlopeZ(v->x_pos, v->y_pos);
|
|
|
|
_get_z_hint = 0;
|
|
|
|
|
|
|
|
old_z = v->z_pos;
|
|
|
|
v->z_pos = new_z;
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
if (new_tile) {
|
|
|
|
CLRBIT(v->u.rail.flags, VRF_GOINGUP);
|
|
|
|
CLRBIT(v->u.rail.flags, VRF_GOINGDOWN);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
if (new_z != old_z) {
|
|
|
|
SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VehiclePositionChanged(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
return old_z;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _new_vehicle_direction_table[11] = {
|
|
|
|
0, 7, 6, 0,
|
|
|
|
1, 0, 5, 0,
|
|
|
|
2, 3, 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int GetNewVehicleDirectionByTile(uint new_tile, uint old_tile)
|
|
|
|
{
|
2005-01-07 17:02:43 +00:00
|
|
|
uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
|
|
|
|
TileX(new_tile) - TileX(old_tile) + 1;
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(offs < 11);
|
|
|
|
return _new_vehicle_direction_table[offs];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetNewVehicleDirection(Vehicle *v, int x, int y)
|
|
|
|
{
|
|
|
|
uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
|
|
|
|
assert(offs < 11);
|
|
|
|
return _new_vehicle_direction_table[offs];
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetDirectionToVehicle(Vehicle *v, int x, int y)
|
|
|
|
{
|
|
|
|
byte offs;
|
|
|
|
|
|
|
|
x -= v->x_pos;
|
|
|
|
if (x >= 0) {
|
|
|
|
offs = (x > 2) ? 0 : 1;
|
|
|
|
} else {
|
|
|
|
offs = (x < -2) ? 2 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
y -= v->y_pos;
|
|
|
|
if (y >= 0) {
|
|
|
|
offs += ((y > 2) ? 0 : 1) * 4;
|
|
|
|
} else {
|
|
|
|
offs += ((y < -2) ? 2 : 1) * 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(offs < 11);
|
|
|
|
return _new_vehicle_direction_table[offs];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the vehicle is compatible with the specified tile */
|
2005-01-17 09:41:46 +00:00
|
|
|
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-01-29 15:12:40 +00:00
|
|
|
switch (GetTileType(tile)) {
|
2005-01-17 09:41:46 +00:00
|
|
|
case MP_RAILWAY:
|
|
|
|
case MP_STATION:
|
|
|
|
// normal tracks, jump to owner check
|
|
|
|
break;
|
2004-09-21 21:28:42 +00:00
|
|
|
|
2005-01-17 09:41:46 +00:00
|
|
|
case MP_TUNNELBRIDGE:
|
|
|
|
if ((_map5[tile] & 0xC0) == 0xC0) { // is bridge middle part?
|
2005-01-27 12:52:20 +00:00
|
|
|
uint height;
|
|
|
|
uint tileh = GetTileSlope(tile, &height);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-17 09:41:46 +00:00
|
|
|
// correct Z position of a train going under a bridge on slopes
|
2005-01-27 12:52:20 +00:00
|
|
|
if (CORRECT_Z(tileh)) height += 8;
|
2005-01-17 09:41:46 +00:00
|
|
|
|
2005-01-27 12:52:20 +00:00
|
|
|
if (v->z_pos != height) return true; // train is going over bridge
|
2005-01-17 09:41:46 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_STREET:
|
|
|
|
// tracks over roads, do owner check of tracks (_map_owner[tile])
|
|
|
|
return
|
|
|
|
_map_owner[tile] == v->owner &&
|
2005-01-19 19:15:03 +00:00
|
|
|
(v->subtype != TS_Front_Engine || (_map3_hi[tile] & 0xF) == v->u.rail.railtype);
|
2005-01-17 09:41:46 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
return true;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-17 09:41:46 +00:00
|
|
|
return
|
|
|
|
_map_owner[tile] == v->owner &&
|
2005-01-19 19:15:03 +00:00
|
|
|
(v->subtype != TS_Front_Engine || (_map3_lo[tile] & 0xF) == v->u.rail.railtype);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
byte small_turn, large_turn;
|
|
|
|
byte z_up; // fraction to remove when moving up
|
|
|
|
byte z_down; // fraction to remove when moving down
|
|
|
|
} RailtypeSlowdownParams;
|
|
|
|
|
|
|
|
static const RailtypeSlowdownParams _railtype_slowdown[3] = {
|
|
|
|
// normal accel
|
|
|
|
{256/4, 256/2, 256/4, 2}, // normal
|
|
|
|
{256/4, 256/2, 256/4, 2}, // monorail
|
|
|
|
{0, 256/2, 256/4, 2}, // maglev
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Modify the speed of the vehicle due to a turn */
|
|
|
|
static void AffectSpeedByDirChange(Vehicle *v, byte new_dir)
|
|
|
|
{
|
|
|
|
byte diff;
|
|
|
|
const RailtypeSlowdownParams *rsp;
|
|
|
|
|
|
|
|
if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rsp = &_railtype_slowdown[v->u.rail.railtype];
|
|
|
|
v->cur_speed -= ((diff == 1 || diff == 7) ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Modify the speed of the vehicle due to a change in altitude */
|
|
|
|
static void AffectSpeedByZChange(Vehicle *v, byte old_z)
|
|
|
|
{
|
|
|
|
const RailtypeSlowdownParams *rsp;
|
|
|
|
if (old_z == v->z_pos || _patches.realistic_acceleration)
|
|
|
|
return;
|
|
|
|
|
|
|
|
rsp = &_railtype_slowdown[v->u.rail.railtype];
|
|
|
|
|
|
|
|
if (old_z < v->z_pos) {
|
|
|
|
v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
|
|
|
|
} else {
|
|
|
|
uint16 spd = v->cur_speed + rsp->z_down;
|
|
|
|
if (spd <= v->max_speed)
|
|
|
|
v->cur_speed = spd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _otherside_signal_directions[14] = {
|
|
|
|
1, 3, 1, 3, 5, 3, 0, 0,
|
|
|
|
5, 7, 7, 5, 7, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void TrainMovedChangeSignals(uint tile, int dir)
|
|
|
|
{
|
|
|
|
int i;
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40) {
|
2004-08-09 17:04:08 +00:00
|
|
|
i = FindFirstBit2x64((_map5[tile]+(_map5[tile]<<8)) & _reachable_tracks[dir]);
|
|
|
|
UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct TrainCollideChecker {
|
|
|
|
Vehicle *v, *v_skip;
|
|
|
|
|
|
|
|
} TrainCollideChecker;
|
|
|
|
|
2005-01-22 22:47:58 +00:00
|
|
|
static void *FindTrainCollideEnum(Vehicle *v, TrainCollideChecker *tcc)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
if (v == tcc->v || v == tcc->v_skip || v->type != VEH_Train || v->u.rail.track==0x80)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if ( myabs(v->z_pos - tcc->v->z_pos) > 6 ||
|
|
|
|
myabs(v->x_pos - tcc->v->x_pos) >= 6 ||
|
|
|
|
myabs(v->y_pos - tcc->v->y_pos) >= 6)
|
|
|
|
return NULL;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SetVehicleCrashed(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *u;
|
|
|
|
|
|
|
|
if (v->u.rail.crash_anim_pos != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
v->u.rail.crash_anim_pos++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
u = v;
|
|
|
|
BEGIN_ENUM_WAGONS(v)
|
|
|
|
v->vehstatus |= VS_CRASHED;
|
|
|
|
END_ENUM_WAGONS(v)
|
|
|
|
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int CountPassengersInTrain(Vehicle *v)
|
|
|
|
{
|
2004-09-10 19:02:27 +00:00
|
|
|
int num = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
BEGIN_ENUM_WAGONS(v)
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->cargo_type == CT_PASSENGERS) num += v->cargo_count;
|
2004-08-09 17:04:08 +00:00
|
|
|
END_ENUM_WAGONS(v)
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/*
|
|
|
|
* Checks whether the specified tried has a collision with another vehicle. If
|
2005-01-19 19:15:03 +00:00
|
|
|
* so, destroys this vehicle, and the other vehicle if its subtype is 0 (TS_Front_Engine).
|
2004-08-11 22:07:08 +00:00
|
|
|
* Reports the incident in a flashy news item, modifies station ratings and
|
|
|
|
* plays a sound.
|
|
|
|
*/
|
2004-08-09 17:04:08 +00:00
|
|
|
static void CheckTrainCollision(Vehicle *v)
|
|
|
|
{
|
|
|
|
TrainCollideChecker tcc;
|
2004-08-21 09:57:02 +00:00
|
|
|
Vehicle *coll,*realcoll;
|
2004-08-09 17:04:08 +00:00
|
|
|
int num;
|
|
|
|
|
|
|
|
/* can't collide in depot */
|
|
|
|
if (v->u.rail.track == 0x80)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
if ( !(v->u.rail.track == 0x40) )
|
2004-08-21 09:57:02 +00:00
|
|
|
assert((uint)TILE_FROM_XY(v->x_pos, v->y_pos) == v->tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
tcc.v = v;
|
|
|
|
tcc.v_skip = v->next;
|
|
|
|
|
|
|
|
/* find colliding vehicle */
|
2004-08-21 09:57:02 +00:00
|
|
|
realcoll = coll = VehicleFromPos(TILE_FROM_XY(v->x_pos, v->y_pos), &tcc, (VehicleFromPosProc*)FindTrainCollideEnum);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (coll == NULL)
|
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
coll = GetFirstVehicleInChain(coll);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* it can't collide with its own wagons */
|
2004-08-21 09:57:02 +00:00
|
|
|
if ( (v == coll) || ( (v->u.rail.track & 0x40) && ( (v->direction & 2) != (realcoll->direction & 2) ) ) )
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2004-09-10 19:02:27 +00:00
|
|
|
//two drivers + passangers killed in train v
|
2004-08-09 17:04:08 +00:00
|
|
|
num = 2 + CountPassengersInTrain(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
if(!(coll->vehstatus&VS_CRASHED))
|
2004-08-09 17:04:08 +00:00
|
|
|
//two drivers + passangers killed in train coll (if it was not crashed already)
|
|
|
|
num += 2 + CountPassengersInTrain(coll);
|
|
|
|
|
|
|
|
SetVehicleCrashed(v);
|
2005-01-19 19:15:03 +00:00
|
|
|
if (coll->subtype == TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
SetVehicleCrashed(coll);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, num);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
|
|
|
|
NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
|
|
|
|
ModifyStationRatingAround(v->tile, v->owner, -160, 30);
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_13_BIG_CRASH, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *CheckVehicleAtSignal(Vehicle *v, void *data)
|
|
|
|
{
|
|
|
|
uint32 d = (uint32)data;
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->type == VEH_Train && v->subtype == TS_Front_Engine && v->tile == (TileIndex)(d >> 8)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte diff = (v->direction - (byte)d + 2) & 7;
|
|
|
|
if (diff == 2 || (v->cur_speed <= 5 && diff <= 4))
|
|
|
|
return (void*)1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TrainController(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *prev = NULL;
|
|
|
|
GetNewVehiclePosResult gp;
|
|
|
|
uint32 r, tracks,ts;
|
2005-01-31 11:23:10 +00:00
|
|
|
int i, enterdir, newdir, dir;
|
2004-08-09 17:04:08 +00:00
|
|
|
byte chosen_dir;
|
|
|
|
byte chosen_track;
|
|
|
|
byte old_z;
|
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/* For every vehicle after and including the given vehicle */
|
2004-08-09 17:04:08 +00:00
|
|
|
for(;;) {
|
|
|
|
BeginVehicleMove(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->u.rail.track != 0x40) {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Not inside tunnel */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (GetNewVehiclePos(v, &gp)) {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Staying in the old tile */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->u.rail.track == 0x80) {
|
|
|
|
/* inside depot */
|
|
|
|
gp.x = v->x_pos;
|
|
|
|
gp.y = v->y_pos;
|
|
|
|
} else {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* is not inside depot */
|
2004-12-21 16:00:14 +00:00
|
|
|
|
|
|
|
if (!TrainCheckIfLineEnds(v))
|
|
|
|
return;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (r & 0x8) {
|
|
|
|
//debug("%x & 0x8", r);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto invalid_rail;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
if (r & 0x2) {
|
|
|
|
TrainEnterStation(v, r >> 8);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_LEAVESTATION) {
|
|
|
|
v->current_order.type = OT_NOTHING;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* A new tile is about to be entered. */
|
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
byte bits;
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Determine what direction we're entering the new tile from */
|
|
|
|
dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
|
2005-01-31 11:23:10 +00:00
|
|
|
enterdir = dir >> 1;
|
|
|
|
assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Get the status of the tracks in the new tile and mask
|
|
|
|
* away the bits that aren't reachable. */
|
2005-01-31 11:23:10 +00:00
|
|
|
ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[enterdir];
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Combine the from & to directions.
|
|
|
|
* Now, the lower byte contains the track status, and the byte at bit 16 contains
|
|
|
|
* the signal status. */
|
|
|
|
tracks = ts|(ts >> 8);
|
2005-01-31 11:23:10 +00:00
|
|
|
bits = tracks & 0xFF;
|
|
|
|
if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL)
|
|
|
|
/* We allow wagons to make 90 deg turns, because forbid_90_deg
|
|
|
|
* can be switched on halfway a turn */
|
|
|
|
bits &= ~_track_crosses_tracks[FIND_FIRST_BIT(v->u.rail.track)];
|
|
|
|
|
|
|
|
if ( bits == 0) {
|
|
|
|
//debug("%x == 0", bits);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto invalid_rail;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Check if the new tile contrains tracks that are compatible
|
|
|
|
* with the current train, if not, bail out. */
|
2005-01-31 11:23:10 +00:00
|
|
|
if (!CheckCompatibleRail(v, gp.new_tile)) {
|
|
|
|
//debug("!CheckCompatibleRail(%p, %x)", v, gp.new_tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto invalid_rail;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (prev == NULL) {
|
|
|
|
/* Currently the locomotive is active. Determine which one of the
|
|
|
|
* available tracks to choose */
|
2005-01-31 11:23:10 +00:00
|
|
|
chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, enterdir, bits);
|
|
|
|
assert(chosen_track & tracks);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
/* Check if it's a red signal and that force proceed is not clicked. */
|
|
|
|
if ( (tracks>>16)&chosen_track && v->u.rail.force_proceed == 0) goto red_light;
|
|
|
|
} else {
|
|
|
|
static byte _matching_tracks[8] = {0x30, 1, 0xC, 2, 0x30, 1, 0xC, 2};
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* The wagon is active, simply follow the prev vehicle. */
|
2005-01-31 11:23:10 +00:00
|
|
|
chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* make sure chosen track is a valid track */
|
|
|
|
assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32);
|
|
|
|
|
|
|
|
/* Update XY to reflect the entrance to the new tile, and select the direction to use */
|
|
|
|
{
|
2005-01-31 11:23:10 +00:00
|
|
|
const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
|
2004-08-09 17:04:08 +00:00
|
|
|
gp.x = (gp.x & ~0xF) | b[0];
|
|
|
|
gp.y = (gp.y & ~0xF) | b[1];
|
|
|
|
chosen_dir = b[2];
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* Call the landscape function and tell it that the vehicle entered the tile */
|
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
2005-01-31 11:23:10 +00:00
|
|
|
if (r&0x8){
|
|
|
|
//debug("%x & 0x8", r);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto invalid_rail;
|
2005-01-31 11:23:10 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype == TS_Front_Engine) v->load_unload_time_rem = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(r&0x4)) {
|
|
|
|
v->tile = gp.new_tile;
|
|
|
|
v->u.rail.track = chosen_track;
|
2005-02-06 22:36:08 +00:00
|
|
|
assert(v->u.rail.track);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype == TS_Front_Engine)
|
2005-01-31 11:23:10 +00:00
|
|
|
TrainMovedChangeSignals(gp.new_tile, enterdir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Signals can only change when the first
|
|
|
|
* (above) or the last vehicle moves. */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (v->next == NULL)
|
2005-01-31 11:23:10 +00:00
|
|
|
TrainMovedChangeSignals(gp.old_tile, (enterdir) ^ 2);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (prev == NULL) {
|
|
|
|
AffectSpeedByDirChange(v, chosen_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
v->direction = chosen_dir;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* in tunnel */
|
|
|
|
GetNewVehiclePos(v, &gp);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) &&
|
2004-08-09 17:04:08 +00:00
|
|
|
!(_map5[gp.new_tile] & 0xF0)) {
|
|
|
|
r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
|
|
|
|
if (r & 0x4) goto common;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
VehiclePositionChanged(v);
|
2004-08-21 09:57:02 +00:00
|
|
|
if (prev == NULL)
|
|
|
|
CheckTrainCollision(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
goto next_vehicle;
|
|
|
|
}
|
|
|
|
common:;
|
|
|
|
|
|
|
|
/* update image of train, as well as delta XY */
|
2005-01-31 11:23:10 +00:00
|
|
|
newdir = GetNewVehicleDirection(v, gp.x, gp.y);
|
|
|
|
UpdateTrainDeltaXY(v, newdir);
|
|
|
|
v->cur_image = GetTrainImage(v, newdir);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
v->x_pos = gp.x;
|
|
|
|
v->y_pos = gp.y;
|
|
|
|
|
|
|
|
/* update the Z position of the vehicle */
|
2005-01-09 16:02:06 +00:00
|
|
|
old_z = AfterSetTrainPos(v, (gp.new_tile != gp.old_tile));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (prev == NULL) {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* This is the first vehicle in the train */
|
2004-08-09 17:04:08 +00:00
|
|
|
AffectSpeedByZChange(v, old_z);
|
|
|
|
CheckTrainCollision(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
next_vehicle:;
|
2004-08-11 22:07:08 +00:00
|
|
|
/* continue with next vehicle */
|
2004-08-09 17:04:08 +00:00
|
|
|
prev = v;
|
|
|
|
if ((v=v->next) == NULL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid_rail:
|
2004-08-11 22:07:08 +00:00
|
|
|
/* We've reached end of line?? */
|
2004-08-09 17:04:08 +00:00
|
|
|
if (prev != NULL) {
|
|
|
|
error("!Disconnecting train");
|
|
|
|
}
|
|
|
|
goto reverse_train_direction;
|
|
|
|
|
|
|
|
red_light: {
|
2004-08-11 22:07:08 +00:00
|
|
|
/* We're in front of a red signal ?? */
|
2004-08-09 17:04:08 +00:00
|
|
|
/* find the first set bit in ts. need to do it in 2 steps, since
|
|
|
|
* FIND_FIRST_BIT only handles 6 bits at a time. */
|
|
|
|
i = FindFirstBit2x64(ts);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-31 11:23:10 +00:00
|
|
|
if (!(_map3_lo[gp.new_tile] & _signal_against_trackdir[i])) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 255-100;
|
|
|
|
if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20)
|
|
|
|
return;
|
2005-01-31 11:23:10 +00:00
|
|
|
} else if (_map3_lo[gp.new_tile] & _signal_along_trackdir[i]){
|
2004-08-09 17:04:08 +00:00
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
v->progress = 255-10;
|
|
|
|
if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) {
|
2005-01-31 11:23:10 +00:00
|
|
|
uint o_tile = gp.new_tile + TileOffsByDir(enterdir);
|
2004-08-09 17:04:08 +00:00
|
|
|
/* check if a train is waiting on the other side */
|
|
|
|
if (VehicleFromPos(o_tile, (void*)( (o_tile<<8) | (dir^4)), (VehicleFromPosProc*)CheckVehicleAtSignal) == NULL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
reverse_train_direction:
|
|
|
|
v->load_unload_time_rem = 0;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2004-08-21 09:57:02 +00:00
|
|
|
extern uint CheckTunnelBusy(uint tile, int *length);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
static void DeleteLastWagon(Vehicle *v)
|
|
|
|
{
|
|
|
|
Vehicle *u = v;
|
|
|
|
int t;
|
|
|
|
|
|
|
|
while (v->next != NULL) {
|
|
|
|
u = v;
|
|
|
|
v = v->next;
|
|
|
|
}
|
|
|
|
u->next = NULL;
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
DeleteWindowById(WC_VEHICLE_VIEW, v->index);
|
2004-12-10 18:16:08 +00:00
|
|
|
RebuildVehicleLists();
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_COMPANY, v->owner);
|
|
|
|
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
EndVehicleMove(v);
|
|
|
|
DeleteVehicle(v);
|
|
|
|
|
|
|
|
if (!((t=v->u.rail.track) & 0xC0)) {
|
|
|
|
SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(t));
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-23 10:40:54 +00:00
|
|
|
/* Check if the wagon was on a road/rail-crossing and disable it if no others are on it */
|
|
|
|
DisableTrainCrossing(v->tile);
|
|
|
|
|
2004-08-21 09:57:02 +00:00
|
|
|
if (v->u.rail.track == 0x40) {
|
|
|
|
int length;
|
|
|
|
TileIndex endtile = CheckTunnelBusy(v->tile, &length);
|
|
|
|
if ((v->direction == 1) || (v->direction == 5) )
|
|
|
|
SetSignalsOnBothDir(v->tile,0);
|
|
|
|
SetSignalsOnBothDir(endtile,0);
|
|
|
|
if ((v->direction == 3) || (v->direction == 7) )
|
|
|
|
SetSignalsOnBothDir(v->tile,1);
|
|
|
|
SetSignalsOnBothDir(endtile,1);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ChangeTrainDirRandomly(Vehicle *v)
|
|
|
|
{
|
|
|
|
static int8 _random_dir_change[4] = { -1, 0, 0, 1};
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2004-08-21 09:57:02 +00:00
|
|
|
//I need to buffer the train direction
|
2004-09-18 16:40:06 +00:00
|
|
|
if (!(v->u.rail.track & 0x40))
|
2005-01-24 14:39:22 +00:00
|
|
|
v->direction = (v->direction + _random_dir_change[Random()&3]) & 7;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
BeginVehicleMove(v);
|
|
|
|
UpdateTrainDeltaXY(v, v->direction);
|
|
|
|
v->cur_image = GetTrainImage(v, v->direction);
|
2005-01-09 16:02:06 +00:00
|
|
|
AfterSetTrainPos(v, false);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} while ( (v=v->next) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleCrashedTrain(Vehicle *v)
|
|
|
|
{
|
|
|
|
int state = ++v->u.rail.crash_anim_pos, index;
|
|
|
|
uint32 r;
|
|
|
|
Vehicle *u;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-21 09:57:02 +00:00
|
|
|
if ( (state == 4) && (v->u.rail.track != 0x40) ) {
|
2004-08-09 17:04:08 +00:00
|
|
|
CreateEffectVehicleRel(v, 4, 4, 8, EV_CRASHED_SMOKE);
|
|
|
|
}
|
|
|
|
|
2005-01-24 14:39:22 +00:00
|
|
|
if (state <= 200 && (uint16)(r=Random()) <= 0x2492) {
|
2004-08-09 17:04:08 +00:00
|
|
|
index = (r * 10 >> 16);
|
|
|
|
|
|
|
|
u = v;
|
|
|
|
do {
|
|
|
|
if (--index < 0) {
|
2005-01-24 14:39:22 +00:00
|
|
|
r = Random();
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
CreateEffectVehicleRel(u,
|
|
|
|
2 + ((r>>8)&7),
|
|
|
|
2 + ((r>>16)&7),
|
|
|
|
5 + (r&7),
|
|
|
|
EV_DEMOLISH);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while ( (u=u->next) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state <= 240 && !(v->tick_counter&3)) {
|
|
|
|
ChangeTrainDirRandomly(v);
|
|
|
|
}
|
|
|
|
|
2005-01-23 22:01:51 +00:00
|
|
|
if (state >= 4440 && !(v->tick_counter&0x1F)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteLastWagon(v);
|
2005-01-23 22:01:51 +00:00
|
|
|
InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleBrokenTrain(Vehicle *v)
|
|
|
|
{
|
|
|
|
if (v->breakdown_ctr != 1) {
|
|
|
|
v->breakdown_ctr = 1;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
|
|
|
|
if (v->breakdowns_since_last_service != 255)
|
|
|
|
v->breakdowns_since_last_service++;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx((_opt.landscape != LT_CANDY) ?
|
|
|
|
SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
if (!(v->vehstatus & VS_HIDDEN)) {
|
|
|
|
Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
|
|
|
|
if (u)
|
|
|
|
u->u.special.unk0 = v->breakdown_delay * 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(v->tick_counter & 3)) {
|
|
|
|
if (!--v->breakdown_delay) {
|
|
|
|
v->breakdown_ctr = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const byte _breakdown_speeds[16] = {
|
|
|
|
225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
|
|
|
|
};
|
|
|
|
|
2004-12-21 16:00:14 +00:00
|
|
|
static bool TrainCheckIfLineEnds(Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
uint tile;
|
|
|
|
uint x,y;
|
|
|
|
int t;
|
|
|
|
uint32 ts;
|
|
|
|
|
|
|
|
if ((uint)(t=v->breakdown_ctr) > 1) {
|
|
|
|
v->vehstatus |= VS_TRAIN_SLOWING;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
t = _breakdown_speeds[ ((~t) >> 4) & 0xF];
|
|
|
|
if ((uint16)t <= v->cur_speed)
|
|
|
|
v->cur_speed = t;
|
|
|
|
} else {
|
|
|
|
v->vehstatus &= ~VS_TRAIN_SLOWING;
|
|
|
|
}
|
|
|
|
|
|
|
|
// exit if inside a tunnel
|
|
|
|
if (v->u.rail.track & 0x40)
|
2004-12-21 16:00:14 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
tile = v->tile;
|
|
|
|
|
|
|
|
// tunnel entrance?
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_TUNNELBRIDGE) &&
|
2004-08-09 17:04:08 +00:00
|
|
|
(_map5[tile] & 0xF0) == 0 && (byte)((_map5[tile] & 3)*2+1) == v->direction)
|
2004-12-21 16:00:14 +00:00
|
|
|
return true;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// depot?
|
2004-12-21 16:00:14 +00:00
|
|
|
/* XXX -- When enabled, this makes it possible to crash trains of others
|
|
|
|
(by building a depot right against a station) */
|
2005-01-16 11:24:58 +00:00
|
|
|
/* if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xFC) == 0xC0)
|
2004-12-21 16:00:14 +00:00
|
|
|
return true;*/
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Determine the non-diagonal direction in which we will exit this tile */
|
2004-08-09 17:04:08 +00:00
|
|
|
t = v->direction >> 1;
|
|
|
|
if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
|
|
|
|
t = (t - 1) & 3;
|
|
|
|
}
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Calculate next tile */
|
2005-01-05 13:32:03 +00:00
|
|
|
tile += TileOffsByDir(t);
|
2004-08-11 22:07:08 +00:00
|
|
|
// determine the track status on the next tile.
|
2005-01-24 22:24:47 +00:00
|
|
|
ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[t];
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-11 22:07:08 +00:00
|
|
|
/* Calc position within the current tile ?? */
|
2004-08-09 17:04:08 +00:00
|
|
|
x = v->x_pos & 0xF;
|
|
|
|
y = v->y_pos & 0xF;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
switch(v->direction) {
|
|
|
|
case 0:
|
|
|
|
x = (~x) + (~y) + 24;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
x = y;
|
|
|
|
/* fall through */
|
|
|
|
case 1:
|
|
|
|
x = (~x) + 16;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
x = (~x) + y + 8;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
x = y;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
x = x + y - 8;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
x = (~y) + x + 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (uint16)ts != 0) {
|
2004-12-21 16:00:14 +00:00
|
|
|
/* If we approach a rail-piece which we can't enter, don't enter it! */
|
|
|
|
if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
if ((ts &= (ts >> 16)) == 0) {
|
|
|
|
// make a rail/road crossing red
|
2005-01-16 11:24:58 +00:00
|
|
|
if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) {
|
2004-08-09 17:04:08 +00:00
|
|
|
if (!(_map5[tile] & 4)) {
|
|
|
|
_map5[tile] |= 4;
|
2004-12-04 09:26:39 +00:00
|
|
|
SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
|
2004-08-09 17:04:08 +00:00
|
|
|
MarkTileDirtyByTile(tile);
|
|
|
|
}
|
|
|
|
}
|
2004-12-21 16:00:14 +00:00
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} else if (x + 4 > 15) {
|
|
|
|
v->cur_speed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
2004-12-21 16:00:14 +00:00
|
|
|
return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// slow down
|
|
|
|
v->vehstatus |= VS_TRAIN_SLOWING;
|
|
|
|
t = _breakdown_speeds[x & 0xF];
|
|
|
|
if (!(v->direction&1)) t>>=1;
|
|
|
|
if ((uint16)t < v->cur_speed)
|
|
|
|
v->cur_speed = t;
|
2004-12-21 16:00:14 +00:00
|
|
|
|
|
|
|
return true;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TrainLocoHandler(Vehicle *v, bool mode)
|
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* train has crashed? */
|
|
|
|
if (v->u.rail.crash_anim_pos != 0) {
|
|
|
|
if (!mode) HandleCrashedTrain(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v->u.rail.force_proceed != 0)
|
|
|
|
v->u.rail.force_proceed--;
|
|
|
|
|
|
|
|
/* train is broken down? */
|
|
|
|
if (v->breakdown_ctr != 0) {
|
|
|
|
if (v->breakdown_ctr <= 2) {
|
|
|
|
HandleBrokenTrain(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
v->breakdown_ctr--;
|
|
|
|
}
|
|
|
|
|
2005-01-09 16:02:06 +00:00
|
|
|
if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) {
|
2004-08-09 17:04:08 +00:00
|
|
|
ReverseTrainDirection(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exit if train is stopped */
|
|
|
|
if (v->vehstatus & VS_STOPPED && v->cur_speed == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
if (ProcessTrainOrder(v)) {
|
|
|
|
v->load_unload_time_rem = 0;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
v->subspeed = 0;
|
|
|
|
ReverseTrainDirection(v);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleTrainLoading(v, mode);
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_LOADING)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (CheckTrainStayInDepot(v))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!mode) HandleLocomotiveSmokeCloud(v);
|
|
|
|
|
|
|
|
j = UpdateTrainSpeed(v);
|
|
|
|
if (j == 0) {
|
|
|
|
// if the vehicle has speed 0, update the last_speed field.
|
|
|
|
if (v->cur_speed != 0)
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
TrainCheckIfLineEnds(v);
|
|
|
|
|
|
|
|
do {
|
|
|
|
TrainController(v);
|
|
|
|
if (v->cur_speed <= 0x100)
|
|
|
|
break;
|
|
|
|
} while (--j != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastSpeed(v, v->cur_speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Train_Tick(Vehicle *v)
|
|
|
|
{
|
|
|
|
if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff)
|
|
|
|
v->cargo_days++;
|
|
|
|
|
|
|
|
v->tick_counter++;
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype == TS_Front_Engine) {
|
2004-08-09 17:04:08 +00:00
|
|
|
TrainLocoHandler(v, false);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// make sure vehicle wasn't deleted.
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->type == VEH_Train && v->subtype == TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
TrainLocoHandler(v, true);
|
2005-01-24 00:36:22 +00:00
|
|
|
} else if (v->subtype == TS_Free_Car && HASBITS(v->vehstatus, VS_CRASHED)) {
|
|
|
|
// Delete flooded standalone wagon
|
|
|
|
if (++v->u.rail.crash_anim_pos >= 4400)
|
|
|
|
DeleteVehicle(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const byte _depot_track_ind[4] = {0,1,0,1};
|
|
|
|
|
2004-12-19 09:39:19 +00:00
|
|
|
// Validation for the news item "Train is waiting in depot"
|
2005-01-22 22:47:58 +00:00
|
|
|
static bool ValidateTrainInDepot( uint data_a, uint data_b )
|
2004-12-19 09:39:19 +00:00
|
|
|
{
|
2005-01-06 22:31:58 +00:00
|
|
|
Vehicle *v = GetVehicle(data_a);
|
2005-01-19 10:00:37 +00:00
|
|
|
return (v->u.rail.track == 0x80 && (v->vehstatus | VS_STOPPED));
|
2004-12-19 09:39:19 +00:00
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
void TrainEnterDepot(Vehicle *v, uint tile)
|
|
|
|
{
|
|
|
|
SetSignalsOnBothDir(tile, _depot_track_ind[_map5[tile]&3]);
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype != TS_Front_Engine)
|
2004-08-09 17:04:08 +00:00
|
|
|
v = GetFirstVehicleInChain(v);
|
|
|
|
|
2004-12-09 21:46:56 +00:00
|
|
|
VehicleServiceInDepot(v);
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
|
|
|
|
v->load_unload_time_rem = 0;
|
|
|
|
v->cur_speed = 0;
|
|
|
|
|
2005-01-02 17:23:04 +00:00
|
|
|
MaybeReplaceVehicle(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-17 08:52:47 +00:00
|
|
|
TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
|
|
|
Order t;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
InvalidateWindow(WC_VEHICLE_VIEW, v->index);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
t = v->current_order;
|
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (t.flags & OF_UNLOAD) { // Part of the orderlist?
|
2004-12-05 12:43:04 +00:00
|
|
|
v->u.rail.days_since_order_progr = 0;
|
|
|
|
v->cur_order_index++;
|
|
|
|
} else if (t.flags & OF_FULL_LOAD) { // User initiated?
|
2004-08-09 17:04:08 +00:00
|
|
|
v->vehstatus |= VS_STOPPED;
|
|
|
|
if (v->owner == _local_player) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, v->unitnumber);
|
2004-12-19 09:39:19 +00:00
|
|
|
AddValidatedNewsItem(
|
2004-08-09 17:04:08 +00:00
|
|
|
STR_8814_TRAIN_IS_WAITING_IN_DEPOT,
|
|
|
|
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
|
|
|
|
v->index,
|
2004-12-19 09:39:19 +00:00
|
|
|
0,
|
|
|
|
ValidateTrainInDepot);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-01-24 22:24:47 +00:00
|
|
|
InvalidateWindowClasses(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckIfTrainNeedsService(Vehicle *v)
|
|
|
|
{
|
2005-02-06 10:18:47 +00:00
|
|
|
Depot *depot;
|
2004-09-23 21:20:38 +00:00
|
|
|
TrainFindDepotData tfdd;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-01-29 12:01:36 +00:00
|
|
|
if (_patches.servint_trains == 0 && !v->set_for_replacement)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2005-01-29 12:01:36 +00:00
|
|
|
if (!VehicleNeedsService(v) && !v->set_for_replacement)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (v->vehstatus & VS_STOPPED)
|
|
|
|
return;
|
|
|
|
|
2005-01-15 19:06:22 +00:00
|
|
|
if (_patches.gotodepot && VehicleHasDepotOrders(v))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// Don't interfere with a depot visit scheduled by the user, or a
|
|
|
|
// depot visit by the order list.
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
|
|
|
(v->current_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) != 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2004-09-23 21:20:38 +00:00
|
|
|
tfdd = FindClosestTrainDepot(v);
|
|
|
|
/* Only go to the depot if it is not too far out of our way. */
|
|
|
|
if (tfdd.best_length == (uint)-1 || tfdd.best_length > 16 ) {
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT) {
|
2004-09-23 21:20:38 +00:00
|
|
|
/* If we were already heading for a depot but it has
|
|
|
|
* suddenly moved farther away, we continue our normal
|
|
|
|
* schedule? */
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_DUMMY;
|
|
|
|
v->current_order.flags = 0;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-09-23 21:20:38 +00:00
|
|
|
depot = GetDepotByTile(tfdd.tile);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_DEPOT &&
|
2005-02-06 10:18:47 +00:00
|
|
|
v->current_order.station != depot->index &&
|
2004-12-05 12:43:04 +00:00
|
|
|
!CHANCE16(3,16))
|
2004-08-09 17:04:08 +00:00
|
|
|
return;
|
|
|
|
|
2004-12-05 12:43:04 +00:00
|
|
|
v->current_order.type = OT_GOTO_DEPOT;
|
|
|
|
v->current_order.flags = OF_NON_STOP;
|
2005-02-06 10:18:47 +00:00
|
|
|
v->current_order.station = depot->index;
|
2004-09-23 21:20:38 +00:00
|
|
|
v->dest_tile = tfdd.tile;
|
2004-12-21 23:27:58 +00:00
|
|
|
InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 GetTrainRunningCost(Vehicle *v)
|
|
|
|
{
|
|
|
|
int32 cost = 0;
|
|
|
|
|
|
|
|
do {
|
2004-12-04 07:41:37 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
|
2004-08-09 17:04:08 +00:00
|
|
|
if (rvi->running_cost_base)
|
|
|
|
cost += rvi->running_cost_base * _price.running_rail[rvi->engclass];
|
|
|
|
} while ( (v=v->next) != NULL );
|
|
|
|
|
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnNewDay_Train(Vehicle *v)
|
|
|
|
{
|
|
|
|
TileIndex tile;
|
|
|
|
|
|
|
|
if ((++v->day_counter & 7) == 0)
|
|
|
|
DecreaseVehicleValue(v);
|
|
|
|
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype == TS_Front_Engine) {
|
2004-08-09 17:04:08 +00:00
|
|
|
CheckVehicleBreakdown(v);
|
|
|
|
AgeVehicle(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
CheckIfTrainNeedsService(v);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// check if train hasn't advanced in its order list for a set number of days
|
2005-01-18 16:01:35 +00:00
|
|
|
if (_patches.lost_train_days && v->num_orders && !(v->vehstatus & (VS_STOPPED | VS_CRASHED) ) && ++v->u.rail.days_since_order_progr >= _patches.lost_train_days && v->owner == _local_player) {
|
2004-08-09 17:04:08 +00:00
|
|
|
v->u.rail.days_since_order_progr = 0;
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(0, v->unitnumber);
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem(
|
|
|
|
STR_TRAIN_IS_LOST,
|
|
|
|
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2005-01-17 21:54:45 +00:00
|
|
|
CheckOrders(v->index, OC_INIT);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
/* update destination */
|
2004-12-05 12:43:04 +00:00
|
|
|
if (v->current_order.type == OT_GOTO_STATION &&
|
2005-01-06 22:31:58 +00:00
|
|
|
(tile = GetStation(v->current_order.station)->train_tile) != 0)
|
2004-08-09 17:04:08 +00:00
|
|
|
v->dest_tile = tile;
|
|
|
|
|
|
|
|
if ((v->vehstatus & VS_STOPPED) == 0) {
|
|
|
|
/* running costs */
|
|
|
|
int32 cost = GetTrainRunningCost(v) / 364;
|
|
|
|
|
|
|
|
v->profit_this_year -= cost >> 8;
|
|
|
|
|
|
|
|
SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
|
|
|
|
SubtractMoneyFromPlayerFract(v->owner, cost);
|
|
|
|
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
2005-01-24 22:24:47 +00:00
|
|
|
InvalidateWindowClasses(WC_TRAINS_LIST);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void TrainsYearlyLoop(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->type == VEH_Train && v->subtype == TS_Front_Engine) {
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
// show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list)
|
|
|
|
if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) {
|
2004-12-02 22:53:07 +00:00
|
|
|
SetDParam(1, v->profit_this_year);
|
|
|
|
SetDParam(0, v->unitnumber);
|
2004-08-09 17:04:08 +00:00
|
|
|
AddNewsItem(
|
|
|
|
STR_TRAIN_IS_UNPROFITABLE,
|
|
|
|
NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
|
|
|
|
v->index,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
v->profit_last_year = v->profit_this_year;
|
|
|
|
v->profit_this_year = 0;
|
|
|
|
InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extern void ShowTrainViewWindow(Vehicle *v);
|
|
|
|
|
|
|
|
void HandleClickOnTrain(Vehicle *v)
|
|
|
|
{
|
2005-01-19 19:15:03 +00:00
|
|
|
if (v->subtype != TS_Front_Engine) v = GetFirstVehicleInChain(v);
|
2004-08-09 17:04:08 +00:00
|
|
|
ShowTrainViewWindow(v);
|
|
|
|
}
|
|
|
|
|
2005-01-22 20:23:18 +00:00
|
|
|
void InitializeTrains(void)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
_age_cargo_skip_counter = 1;
|
|
|
|
}
|