train_cmd.c
author darkvater
Sun, 30 Jan 2005 14:32:52 +0000
changeset 1225 16a44e26efa5
parent 1223 228277b970ca
child 1234 8688fec47517
permissions -rw-r--r--
(svn r1729) - Fix: [ 1112469 ] Clearing land for free by reallocating HQ
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     1
#include "stdafx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     2
#include "ttd.h"
507
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 495
diff changeset
     3
#include "table/strings.h"
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 593
diff changeset
     4
#include "map.h"
1209
2e00193652b2 (svn r1713) Split off several functions which query/set information about a single tile from map.h and put them into a seperate file tile.h
tron
parents: 1208
diff changeset
     5
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     6
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     7
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     8
#include "pathfind.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "table/train_cmd.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "gfx.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "player.h"
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
    15
#include "sound.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#define is_firsthead_sprite(spritenum) \
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
	(is_custom_sprite(spritenum) \
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
		? is_custom_firsthead_sprite(spritenum) \
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
		: _engine_sprite_add[spritenum] == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
    22
static bool TrainCheckIfLineEnds(Vehicle *v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
static const byte _vehicle_initial_x_fract[4] = {10,8,4,8};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
static const byte _vehicle_initial_y_fract[4] = {8,4,8,10};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
static const byte _state_dir_table[4] = { 0x20, 8, 0x10, 4 };
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
static const byte _signal_onedir[14] = {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
    29
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10, 0, 0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
static const byte _signal_otherdir[14] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
	0x40, 0x40, 0x40, 0x10, 0x80, 0x20, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
	0x80, 0x80, 0x80, 0x20, 0x40, 0x10
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    38
static const byte _curve_neighbours45[8][2] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    39
	{7, 1},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    40
	{0, 2},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    41
	{1, 3},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    42
	{2, 4},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    43
	{3, 5},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    44
	{4, 6},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    45
	{5, 7},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    46
	{6, 0},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    47
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    48
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    49
static const byte _curve_neighbours90[8][2] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    50
	{6, 2},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    51
	{7, 3},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    52
	{0, 4},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    53
	{1, 5},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    54
	{2, 6},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    55
	{3, 7},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    56
	{4, 0},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    57
	{5, 1},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    58
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    59
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    60
enum AccelType {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    61
	AM_ACCEL,
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    62
	AM_BRAKE
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    63
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    64
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    65
//new acceleration
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    66
static int GetTrainAcceleration(Vehicle *v, bool mode)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    67
{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    68
	Vehicle *u = v;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    69
	int num = 0;	//number of vehicles, change this into the number of axles later
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    70
	int power = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    71
	int mass = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    72
	int max_speed = 2000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    73
	int area = 120;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    74
	int friction = 35; //[1e-3]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    75
	int drag_coeff = 20;	//[1e-4]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    76
	int incl = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    77
	int resistance;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    78
	int speed = v->cur_speed; //[mph]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    79
	int force = 0x3FFFFFFF;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    80
	int pos = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    81
	int lastpos = -1;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    82
	int curvecount[2] = {0, 0};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    83
	int *dist = NULL;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    84
	int sum = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    85
	int numcurve = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    86
	int i;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    87
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    88
	speed *= 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    89
	speed /= 16;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    90
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    91
	//first find the curve speed limit
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    92
	for (; u->next != NULL; u = u->next, pos++) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    93
		int dir = u->direction;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    94
		int ndir = u->next->direction;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    95
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    96
		for (i = 0; i < 2; i++) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    97
			if ( _curve_neighbours45[dir][i] == ndir) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    98
				curvecount[i]++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
    99
				if (lastpos != -1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   100
					dist = realloc(dist, sizeof(int) * ++numcurve);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   101
					dist[numcurve - 1] = pos - lastpos;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   102
					if (pos - lastpos == 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   103
						max_speed = 88;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   104
					}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   105
				}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   106
				lastpos = pos;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   107
			}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   108
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   109
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   110
		//if we have a 90 degree turn, fix the speed limit to 60
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   111
		if ( _curve_neighbours90[dir][0] == ndir || _curve_neighbours90[dir][1] == ndir) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   112
			max_speed = 61;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   113
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   114
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   115
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   116
	for(i = 0; i < numcurve; i++) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   117
		sum += dist[i];
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   118
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   119
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   120
	if (numcurve > 0) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   121
		sum /= numcurve;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   122
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   123
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   124
	if ((curvecount[0] != 0 || curvecount[1] != 0) && (max_speed > 88)) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   125
		int total = curvecount[0] + curvecount[1];
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   126
		if (curvecount[0] == 1 && curvecount[1] == 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   127
			max_speed = 0xFFFF;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   128
		} else if (total > 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   129
			max_speed = 232 - (13 - clamp(sum, 1, 12)) * (13 - clamp(sum, 1, 12));
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   130
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   131
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   132
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   133
	max_speed += (max_speed / 2) * v->u.rail.railtype;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   134
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   135
	if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   136
		static const TileIndexDiffC _station_dir_from_vdir[] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   137
			{0, 0}, {-1, 0}, {0, 0}, {0, 1}, {0, 0}, {1, 0}, {0, 0}, {0, -1}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   138
		};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   139
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   140
		if (((v->current_order.station == _map2[v->tile]) || !(v->current_order.flags & OF_NON_STOP)) && v->last_station_visited != _map2[v->tile]) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   141
			int station_length = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   142
			TileIndex tile = v->tile;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   143
			int delta_v;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   144
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   145
			max_speed = 120;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   146
			do {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   147
				station_length++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   148
				tile = TILE_ADD(tile, ToTileIndexDiff(_station_dir_from_vdir[v->direction]));
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   149
			} while (IsTileType(tile, MP_STATION));
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   150
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   151
			delta_v = v->cur_speed / (station_length + 1);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   152
			if (v->max_speed > (v->cur_speed - delta_v))
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   153
				max_speed = v->cur_speed - (delta_v / 10);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   154
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   155
			max_speed = max(max_speed, 25 * station_length);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   156
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   157
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   158
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   159
	for (u = v; u != NULL; u = u->next) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   160
		const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   161
		int vmass;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   162
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   163
		num++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   164
		power += rvi->power * 746;	//[W]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   165
		drag_coeff += 3;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   166
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   167
		if (rvi->max_speed != 0)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   168
			max_speed = min(rvi->max_speed, max_speed);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   169
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   170
		if (u->u.rail.track == 0x80)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   171
			max_speed = 61;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   172
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   173
		vmass = rvi->weight;  //[t]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   174
		vmass += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   175
		mass += vmass; //[t]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   176
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   177
		if (!IsTileType(u->tile, MP_TUNNELBRIDGE)) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   178
			if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   179
				incl += vmass * 60;		//3% slope, quite a bit actually
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   180
			} else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   181
				incl -= vmass * 60;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   182
			}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   183
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   184
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   185
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   186
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   187
	// these are shown in the UI
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   188
	v->u.rail.cached_weight = mass;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   189
	v->u.rail.cached_power = power / 746;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   190
	v->max_speed = max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   191
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   192
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   193
	if (v->u.rail.railtype != 2) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   194
		resistance = 13 * mass / 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   195
		resistance += 60 * num;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   196
		resistance += friction * mass * speed / 1000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   197
		resistance += (area * drag_coeff * speed * speed) / 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   198
	} else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   199
		resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   200
	resistance += incl;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   201
	resistance *= 4; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   202
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   203
	if (speed > 0) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   204
		switch (v->u.rail.railtype) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   205
			case 0:
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   206
			case 1:
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   207
			{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   208
				force = power / speed; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   209
				force *= 22;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   210
				force /= 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   211
			} break;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   212
			case 2:
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   213
				force = power / 25;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   214
			break;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   215
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   216
	} else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   217
		//"kickoff" acceleration
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   218
		force = resistance * 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   219
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   220
	if (force <= 0) force = 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   221
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   222
	if (v->u.rail.railtype != 2)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   223
		force = min(force, mass * 10 * 200);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   224
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   225
	if (mode == AM_ACCEL) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   226
		return (force - resistance) / (mass * 4);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   227
	} else {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   228
		return min((-force - resistance) /(mass * 4), (10000 / (mass * 4)));
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   229
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   230
}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   231
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
void UpdateTrainAcceleration(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
	uint acc, power=0, max_speed=5000, weight=0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
	Vehicle *u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   237
	assert(v->subtype == TS_Front_Engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
	// compute stuff like max speed, power, and weight.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
	do {
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   241
		const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   242
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
		// power is sum of the power for all engines
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
		power += rvi->power;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
		// limit the max speed to the speed of the slowest vehicle.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
		if (rvi->max_speed && rvi->max_speed <= max_speed) max_speed = rvi->max_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
		// weight is the sum of the weight of the wagon and the weight of the cargo.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
		weight += rvi->weight;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
		weight += (_cargoc.weights[u->cargo_type] * u->cargo_count) >> 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
	} while ( (u=u->next) != NULL);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   254
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
	// these are shown in the UI
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
	v->u.rail.cached_weight = weight;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
	v->u.rail.cached_power = power;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
	v->max_speed = max_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
	assert(weight != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
	// compute acceleration
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
	acc = power / weight * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   265
	if (acc >= 255) acc=255;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
	if (acc == 0) acc++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
	v->acceleration = (byte)acc;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
int GetTrainImage(Vehicle *v, byte direction)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
	int img = v->spritenum;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
	int base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	if (is_custom_sprite(img)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
		base = GetCustomVehicleSprite(v, direction + 4 * is_custom_secondhead_sprite(img));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
		if (base) return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   279
		img = _engine_original_sprites[v->engine_type];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   281
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
	base = _engine_sprite_base[img] + ((direction + _engine_sprite_add[img]) & _engine_sprite_and[img]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
	if (v->cargo_count >= (v->cargo_cap >> 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
		base += _wagon_full_adder[img];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
	return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   288
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
void DrawTrainEngine(int x, int y, int engine, uint32 image_ormod)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
{
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   291
	const RailVehicleInfo *rvi = RailVehInfo(engine);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   292
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
	int img = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	uint32 image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
	if (is_custom_sprite(img)) {
358
da81e0ef68cf (svn r546) -newgrf: Use GetCustomVehicleIcon() for fetching sprites for vehicle selection dialogs. (Idea by octo, done by pasky).
darkvater
parents: 337
diff changeset
   297
		image = GetCustomVehicleIcon(engine, 6);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
		if (!image) img = _engine_original_sprites[engine];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	if (!image) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
		image = (6 & _engine_sprite_and[img]) + _engine_sprite_base[img];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	if (rvi->flags & RVI_MULTIHEAD) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
		DrawSprite(image | image_ormod, x-14, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
		x += 15;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
		image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
		if (is_custom_sprite(img)) {
358
da81e0ef68cf (svn r546) -newgrf: Use GetCustomVehicleIcon() for fetching sprites for vehicle selection dialogs. (Idea by octo, done by pasky).
darkvater
parents: 337
diff changeset
   309
			image = GetCustomVehicleIcon(engine, 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
			if (!image) img = _engine_original_sprites[engine];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
		if (!image) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   313
			image = ((6 + _engine_sprite_add[img+1]) & _engine_sprite_and[img+1]) + _engine_sprite_base[img+1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
	DrawSprite(image | image_ormod, x, y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
void DrawTrainEngineInfo(int engine, int x, int y, int maxw)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
{
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   321
	const RailVehicleInfo *rvi = RailVehInfo(engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
	int cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
	uint multihead = ((rvi->flags & RVI_MULTIHEAD) ? 1 : 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   324
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   325
	SetDParam(0, ((_price.build_railvehicle >> 3) * rvi->base_cost) >> 5);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   326
	SetDParam(2, rvi->max_speed * 10 >> 4);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   327
	SetDParam(3, rvi->power << multihead);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   328
	SetDParam(1, rvi->weight << multihead);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   329
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   330
	SetDParam(4, (rvi->running_cost_base * _price.running_rail[rvi->engclass] >> 8) << multihead);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   331
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
	cap = rvi->capacity;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   333
	SetDParam(5, STR_8838_N_A);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   334
	if (cap != 0) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   335
		SetDParam(6, cap << multihead);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
   336
		SetDParam(5, _cargoc.names_long_p[rvi->cargo_type]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
	DrawStringMultiCenter(x, y, STR_885B_COST_WEIGHT_T_SPEED_POWER, maxw);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   340
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 395
diff changeset
   342
static int32 CmdBuildRailWagon(uint engine, uint tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
	int32 value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
	const RailVehicleInfo *rvi;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
	int dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
	const Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
	int x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
889
64d2f7d5f02b (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 882
diff changeset
   351
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
64d2f7d5f02b (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 882
diff changeset
   352
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   353
	rvi = RailVehInfo(engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   354
	value = (rvi->base_cost * _price.build_railwagon) >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
	if (!(flags & DC_QUERY_COST)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
		_error_message = STR_00E1_TOO_MANY_VEHICLES_IN_GAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
		v = AllocateVehicle();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
		if (v == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
			return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
		if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
			byte img = rvi->image_index;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   365
			Vehicle *u, *w;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
			v->spritenum = img;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   369
			u = NULL;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   370
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   371
			FOR_ALL_VEHICLES(w) {
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   372
				if (w->type == VEH_Train && w->tile == (TileIndex)tile &&
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   373
				    w->subtype == TS_Free_Car && w->engine_type == engine) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   374
					u = GetLastVehicleInChain(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
			v->engine_type = engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   381
			dir = _map5[tile] & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   383
			v->direction = (byte)(dir*2+1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
			v->tile = (TileIndex)tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   385
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   386
			x = TileX(tile) * 16 | _vehicle_initial_x_fract[dir];
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
   387
			y = TileY(tile) * 16 | _vehicle_initial_y_fract[dir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
			v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
			v->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   391
			v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   392
			v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   393
			v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
			v->u.rail.track = 0x80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   397
			v->subtype = TS_Free_Car;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
			if (u != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
				u->next = v;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   400
				v->subtype = TS_Not_First;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
				v->u.rail.first_engine = u->u.rail.first_engine;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   402
				if (v->u.rail.first_engine == 0xffff && u->subtype == TS_Front_Engine )
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   403
					v->u.rail.first_engine = u->engine_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
				v->u.rail.first_engine = 0xffff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
			v->cargo_type = rvi->cargo_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
			v->value = value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
//			v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
			e = &_engines[engine];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
			v->u.rail.railtype = e->railtype;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   415
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
			v->build_year = _cur_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
			v->type = VEH_Train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
			v->cur_image = 0xAC2;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   419
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   420
			_new_wagon_id = v->index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   421
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   422
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   423
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   426
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   427
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   428
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   429
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   430
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   431
// Move all free vehicles in the depot to the train
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   432
static void NormalizeTrainVehInDepot(Vehicle *u)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   433
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   434
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
	FOR_ALL_VEHICLES(v) {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   436
		if (v->type == VEH_Train && v->subtype == TS_Free_Car &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
				v->tile == u->tile &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   438
				v->u.rail.track == 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
			if (DoCommandByTile(0,v->index | (u->index<<16), 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   441
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   442
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   444
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
static const byte _railveh_unk1[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
	0, 0, 0, 1, 1, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
	0, 0, 0, 0, 1, 0, 1, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
	0, 1, 1, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
	0, 0, 0, 0, 0, 0, 0, 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
	0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
static const byte _railveh_score[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
	1, 4, 7, 19, 20, 30, 31, 19,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
	20, 21, 22, 10, 11, 30, 31, 32,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
	33, 34, 35, 29, 45, 32, 50, 40,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
	41, 51, 52, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
	0, 0, 0, 0, 0, 0, 60, 62,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	63, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   473
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	0, 0, 0, 0, 70, 71, 72, 73,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
	74, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   477
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   478
	0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   479
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   480
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   481
812
65ecc321b3db (svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents: 755
diff changeset
   482
int32 EstimateTrainCost(const RailVehicleInfo *rvi)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   483
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   484
	return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   485
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   486
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1103
diff changeset
   487
void AddRearEngineToMultiheadedTrain(Vehicle *v, Vehicle *u, bool building)
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   488
{
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   489
	u->direction = v->direction;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   490
	u->owner = v->owner;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   491
	u->tile = v->tile;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   492
	u->x_pos = v->x_pos;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   493
	u->y_pos = v->y_pos;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   494
	u->z_pos = v->z_pos;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   495
	u->z_height = 6;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   496
	u->u.rail.track = 0x80;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   497
	v->u.rail.first_engine = 0xffff;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   498
	u->vehstatus = v->vehstatus & ~VS_STOPPED;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   499
	u->subtype = TS_Not_First;
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   500
	u->spritenum = v->spritenum + 1;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   501
	u->cargo_type = v->cargo_type;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   502
	u->cargo_cap = v->cargo_cap;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   503
	u->u.rail.railtype = v->u.rail.railtype;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   504
	if (building) v->next = u;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   505
	u->engine_type = v->engine_type;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   506
	u->build_year = v->build_year;
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1103
diff changeset
   507
	if (building)
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   508
		v->value = u->value = v->value >> 1;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   509
	else
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   510
		u->value = v->value;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   511
	u->type = VEH_Train;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   512
	u->cur_image = 0xAC2;
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   513
	VehiclePositionChanged(u);
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   514
}
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   515
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   516
/* Build a railroad vehicle
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   517
 * p1 = vehicle type id
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   519
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   520
int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   521
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   522
	const RailVehicleInfo *rvi;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
	int value,dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
	Vehicle *v, *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
	byte unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
	Engine *e;
1221
ce638de43f3a (svn r1725) - Fix: now trains can only be built in depots and you have to own the depot too (hacked client protection)
bjarni
parents: 1214
diff changeset
   527
	uint tile = TILE_FROM_XY(x,y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
1197
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
   529
	if (!IsEngineBuildable(p1, VEH_Train)) return CMD_ERROR;
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1192
diff changeset
   530
1223
228277b970ca (svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents: 1221
diff changeset
   531
	if (!IsTrainDepotTile((TileIndex)tile)) return CMD_ERROR;
228277b970ca (svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents: 1221
diff changeset
   532
228277b970ca (svn r1727) - Fix: now CmdBuildRailVehicle() behaves correctly if DC_EXEC is set
bjarni
parents: 1221
diff changeset
   533
	if (_map_owner[tile] != _current_player) return CMD_ERROR;
1221
ce638de43f3a (svn r1725) - Fix: now trains can only be built in depots and you have to own the depot too (hacked client protection)
bjarni
parents: 1214
diff changeset
   534
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   535
	_cmd_build_rail_veh_var1 = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   536
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   537
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   539
	rvi = RailVehInfo(p1);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   540
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
	if (rvi->flags & RVI_WAGON) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
		return CmdBuildRailWagon(p1, tile, flags);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
	value = EstimateTrainCost(rvi);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   547
	if (!(flags & DC_QUERY_COST)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
		v = AllocateVehicle();
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
   549
		if (v == NULL || IsOrderPoolFull())
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   550
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   551
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   552
		unit_num = GetFreeUnitNumber(VEH_Train);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
		if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
			v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
			dir = _map5[tile] & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
			v->direction = (byte)(dir*2+1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
			v->tile = (TileIndex)tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
			v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
			v->x_pos = (x |= _vehicle_initial_x_fract[dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			v->y_pos = (y |= _vehicle_initial_y_fract[dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
			v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
			v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
			v->u.rail.track = 0x80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
			v->u.rail.first_engine = 0xffff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
			v->spritenum = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
			v->cargo_type = rvi->cargo_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
			v->max_speed = rvi->max_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
			v->value = value;
817
238bbdaa228b (svn r1288) -Codechange: changed _map2 to an uint16. It is still saved and loaded as
truelight
parents: 812
diff changeset
   576
			v->last_station_visited = 0xFFFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
			v->dest_tile = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   578
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
			v->engine_type = (byte)p1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
			e = &_engines[p1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
			v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
			v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
			v->max_age = e->lifelength * 366;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   585
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
			v->string_id = STR_SV_TRAIN_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
			v->u.rail.railtype = e->railtype;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
			_new_train_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   589
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
			v->service_interval = _patches.servint_trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
			v->date_of_last_service = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
			v->build_year = _cur_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
			v->type = VEH_Train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   594
			v->cur_image = 0xAC2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   598
			if (rvi->flags&RVI_MULTIHEAD && (u = AllocateVehicle()) != NULL)
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   599
				AddRearEngineToMultiheadedTrain(v, u, true);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   600
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
			UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
			NormalizeTrainVehInDepot(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
			InvalidateWindow(WC_VEHICLE_DEPOT, tile);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   605
			RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
			InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   607
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   608
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   609
	_cmd_build_rail_veh_var1 = _railveh_unk1[p1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	_cmd_build_rail_veh_score = _railveh_score[p1];
1128
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
   611
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
   612
	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
   613
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   614
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
bool IsTrainDepotTile(TileIndex tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
{
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
   620
	return IsTileType(tile, MP_RAILWAY) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
					(_map5[tile] & 0xFC) == 0xC0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 395
diff changeset
   624
static bool IsTunnelTile(TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
{
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
   626
	return IsTileType(tile, MP_TUNNELBRIDGE) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
				 (_map5[tile]&0x80) == 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
int CheckStoppedInDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   635
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
	/* check if stopped in a depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
	if (!IsTrainDepotTile(tile) || v->cur_speed != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
errmsg:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
		_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
		return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
	count = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
		count++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   646
		if (v->u.rail.track != 0x80 || v->tile != (TileIndex)tile ||
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   647
				(v->subtype == TS_Front_Engine && !(v->vehstatus&VS_STOPPED)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
			goto errmsg;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
	} while ( (v=v->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
	return count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
// unlink a rail wagon from the linked list.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
// returns the new value of first
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
	// unlinking the first vehicle of the chain?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
	v->u.rail.first_engine = 0xffff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
	if (v == first) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
		Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
		if ((v=v->next) == NULL) return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   663
		for (u=v; u; u=u->next) u->u.rail.first_engine = v->engine_type;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   664
		v->subtype = TS_Free_Car;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
		return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
		Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   668
		for(u=first; u->next!=v; u=u->next) {}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   669
		u->next = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   670
		return first;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   672
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   673
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
static Vehicle *FindGoodVehiclePos(Vehicle *src)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   675
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   676
	Vehicle *dst;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   677
	uint16 eng = src->engine_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   678
	TileIndex tile = src->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   679
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   680
	FOR_ALL_VEHICLES(dst) {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   681
		if (dst->type == VEH_Train && dst->subtype == TS_Free_Car && dst->tile==tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   682
			// check so all vehicles in the line have the same engine.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   683
			Vehicle *v = dst;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   684
			while (v->engine_type == eng) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
				if ((v = v->next) == NULL) return dst;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   687
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   688
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   689
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   690
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   691
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   692
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
/* p1 & 0xffff= source vehicle index
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
   p1 & 0xffff0000 = what wagon to put the wagon AFTER, 0xffff0000 to make a new line
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
	 p2 & 1 = move all vehicles following the vehicle..
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   696
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
int32 CmdMoveRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   699
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   700
	Vehicle *src, *dst, *src_head, *dst_head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   701
	bool is_loco;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   702
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   703
	src = GetVehicle(p1 & 0xFFFF);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	if (src->type != VEH_Train) return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   706
	is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
	          && is_firsthead_sprite(src->spritenum);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   708
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   709
	// if nothing is selected as destination, try and find a matching vehicle to drag to.
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   710
	if (((int32)p1 >> 16) == -1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
		dst = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
		if (!is_loco) dst = FindGoodVehiclePos(src);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
	} else {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   714
		dst = GetVehicle(((int32)p1 >> 16));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   716
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   717
	// don't move the same vehicle..
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
	if (src == dst)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
		return 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   720
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   721
	/* the player must be the owner */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   722
	if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner)))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   723
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
	/* locate the head of the two chains */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
	src_head = GetFirstVehicleInChain(src);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
	dst_head = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
	if (dst != NULL) dst_head = GetFirstVehicleInChain(dst);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   729
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
	/* check if all vehicles in the source train are stopped */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
	if (CheckStoppedInDepot(src_head) < 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
	/* check if all the vehicles in the dest train are stopped,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
	 * and that the length of the dest train is no longer than XXX vehicles */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
	if (dst_head != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
		int num = CheckStoppedInDepot(dst_head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   738
		if (num < 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
			return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   741
		if (num > (_patches.mammoth_trains ? 100 : 9) && dst_head->subtype == TS_Front_Engine )
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
			return_cmd_error(STR_8819_TRAIN_TOO_LONG);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   744
		// if it's a multiheaded vehicle we're dragging to, drag to the vehicle before..
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
		while (is_custom_secondhead_sprite(dst->spritenum)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
		       || (!is_custom_sprite(dst->spritenum) && _engine_sprite_add[dst->spritenum] != 0)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
			Vehicle *v = GetPrevVehicleInChain(dst);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   748
			if (!v || src == v) break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
			dst = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
		assert(dst_head->tile == src_head->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
	// when moving all wagons, we can't have the same src_head and dst_head
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
	if (p2 & 1 && src_head == dst_head)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   757
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
	// moving a loco to a new line?, then we need to assign a unitnumber.
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   760
	if (dst == NULL && src->subtype != TS_Front_Engine && is_loco) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   761
		uint unit_num = GetFreeUnitNumber(VEH_Train);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   765
		if (flags & DC_EXEC)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
			src->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   769
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   770
	/* do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   771
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   772
		if (p2 & 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
			// unlink ALL wagons
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
			if (src != src_head) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   775
				Vehicle *v = src_head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
				while (v->next != src) v=v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   777
				v->next = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   778
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   779
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
			// unlink single wagon from linked list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
			UnlinkWagon(src, src_head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
			src->next = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   783
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   784
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
		if (dst == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
			// move the train to an empty line. for locomotives, we set the type to 0. for wagons, 4.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
			if (is_loco) {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   788
				if (src->subtype != TS_Front_Engine) {
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
   789
					// setting the type to 0 also involves setting up the orders field.
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   790
					src->subtype = TS_Front_Engine;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
   791
					assert(src->orders == NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
					src->num_orders = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
				dst_head = src;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
			} else {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   796
				src->subtype = TS_Free_Car;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   797
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
			src->u.rail.first_engine = 0xffff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
		} else {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   800
			if (src->subtype == TS_Front_Engine) {
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
   801
				// the vehicle was previously a loco. need to free the order list and delete vehicle windows etc.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
				DeleteWindowById(WC_VEHICLE_VIEW, src->index);
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
   803
				DeleteVehicleOrders(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   805
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   806
			src->subtype = TS_Not_First;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
			src->unitnumber = 0; // doesn't occupy a unitnumber anymore.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   808
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   809
			// setup first_engine
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   810
			src->u.rail.first_engine = dst->u.rail.first_engine;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   811
			if (src->u.rail.first_engine == 0xffff && dst->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
				src->u.rail.first_engine = dst->engine_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   813
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   814
			// link in the wagon(s) in the chain.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   815
			{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
				Vehicle *v = src;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
				while (v->next != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
					v->next->u.rail.first_engine = v->u.rail.first_engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
					v = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
				v->next = dst->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   823
			dst->next = src;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   824
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   826
		if (src_head->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
			UpdateTrainAcceleration(src_head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
		InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
		if (dst_head) {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   831
			if (dst_head->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
				UpdateTrainAcceleration(dst_head);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
			InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   835
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
		InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
737
05082439a150 (svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents: 715
diff changeset
   837
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   842
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
/* p1 = train to start / stop */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
int32 CmdStartStopTrain(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   846
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   848
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
	if (!CheckOwnership(v->owner))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   853
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
		v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
		v->vehstatus ^= VS_STOPPED;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
   856
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
// p1 = wagon/loco index
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
// p2 = mode
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   864
//   0: sell just the vehicle
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
//   1: sell the vehicle and all vehicles that follow it in the chain
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
//   2: when selling attached locos, rearrange all vehicles after it to separate lines.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
int32 CmdSellRailWagon(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
	Vehicle *v, *first,*last;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   871
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   874
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   875
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
	if (!CheckOwnership(v->owner))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   877
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   878
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
	// get first vehicle in chain
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
	first = v;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   881
	if (first->subtype != TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
		first = GetFirstVehicleInChain(first);
242
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   883
		last = GetLastVehicleInChain(first);
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   884
		//now if:
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   885
		// 1) we delete a whole a chain, and
244
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   886
		// 2) we don't actually try to delete the last engine
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   887
		// 3) the first and the last vehicle of that train are of the same type, and
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   888
		// 4) the first and the last vehicle of the chain are not identical
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   889
		// 5) and of "engine" type (i.e. not a carriage)
242
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   890
		// then let the last vehicle live
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   891
		if ( (p2 == 1) && (v != last) && ( last->engine_type == first->engine_type ) && (last != first) && (first->subtype == TS_Front_Engine) )
242
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   892
			last = GetPrevVehicleInChain(last);
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   893
		else
f4894a2cdfe8 (svn r243) -Fix: [1022689] When destroying a part of a train with a 2 side engine the second engine was also deleted [Celestar]
signde
parents: 193
diff changeset
   894
			last = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   895
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   896
		if (p2 != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
			// sell last part of multiheaded?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
			last = GetLastVehicleInChain(v);
73
34d5f66cfad7 (svn r74) -Fix: [1009631] Wrong multihead selling (Bodewes)
truelight
parents: 22
diff changeset
   899
			// Check if the end-part is the same engine and check if it is the rear-end
34d5f66cfad7 (svn r74) -Fix: [1009631] Wrong multihead selling (Bodewes)
truelight
parents: 22
diff changeset
   900
			if (last->engine_type != first->engine_type || is_firsthead_sprite(last->spritenum))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
				last = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
			last = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   906
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
	// make sure the vehicle is stopped in the depot
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
	if (CheckStoppedInDepot(first) < 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
		// always redraw the depot. maybe redraw train list
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   915
		if (first->subtype == TS_Front_Engine) {
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   916
			RebuildVehicleLists();
164
0cbdf3c9bde1 (svn r165) -Feature: Option to sort vehicles in vehicle-list window by different criteria. Total independent sort for all types and players. Periodic resort of list every 10 TTD days. Thank you for your graphical inspiration follow and buxo (since none of you provided any code).
darkvater
parents: 159
diff changeset
   917
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   918
		// when selling an attached locomotive. we need to delete its window.
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   919
		if (v->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
			DeleteWindowById(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   921
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
			// rearrange all vehicles that follow to separate lines.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
			if (p2 == 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
				Vehicle *u,*tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
				u = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
				while (u != last) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
					tmp = u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
					u = u->next;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   929
					DoCommandByTile(tmp->tile, tmp->index | ((-1)<<16), 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
		// delete the vehicles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
		cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   936
		for(;;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
			Vehicle *tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   938
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
			assert(first);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
			first = UnlinkWagon(v, first);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
			cost -= v->value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
			tmp = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
			DeleteVehicle(tmp);
244
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   944
			if ( v == last ) {
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   945
				last = NULL;
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   946
				break;
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   947
			}
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   948
			if ( (v=v->next) == last || p2 != 1) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   949
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
		// delete last vehicle of multiheaded train?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
		if (last) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
			first = UnlinkWagon(last, first);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
			cost -= last->value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
			DeleteVehicle(last);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   957
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
		// an attached train changed?
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   959
		if (first && first->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
			UpdateTrainAcceleration(first);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
			InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
		cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
		for(;;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
			cost -= v->value;
244
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   967
			if ( v == last ) {
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   968
				last = NULL;
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   969
				break;
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   970
			}
57a3922a029c (svn r245) -Fix: [1022689] Bug when dragging a part of a multiheaded engine to 'sell-whole-train'. The rear-head of the multiheaded train will now not be sold automatically. You have to drag it manually (Celestar)
darkvater
parents: 242
diff changeset
   971
			if ( (v=v->next) == last || p2 != 1) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
		if (last) cost -= last->value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
	}
1128
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
   975
	InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
static void UpdateTrainDeltaXY(Vehicle *v, int direction)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
#define MKIT(a,b,c,d) ((a&0xFF)<<24) | ((b&0xFF)<<16) | ((c&0xFF)<<8) | ((d&0xFF)<<0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
#undef MKIT
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
	uint32 x = _delta_xy_table[direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
	v->x_offs = (byte)x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
	v->y_offs = (byte)(x>>=8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
	v->sprite_width = (byte)(x>>=8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
	v->sprite_height = (byte)(x>>=8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
static void UpdateVarsAfterSwap(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
	v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1012
static void SetLastSpeed(Vehicle *v, int spd) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
	int old = v->u.rail.last_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
	if (spd != old) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
		v->u.rail.last_speed = spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
		if (_patches.vehicle_speed || !old != !spd)
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1017
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1021
static void SwapTrainFlags(byte *swap_flag1, byte *swap_flag2)
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1022
{
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1023
	byte flag1, flag2;
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1024
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1025
	flag1 = *swap_flag1;
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1026
	flag2 = *swap_flag2;
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1027
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1028
	/* Clear the flags */
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1029
	CLRBIT(*swap_flag1, VRF_GOINGUP);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1030
	CLRBIT(*swap_flag1, VRF_GOINGDOWN);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1031
	CLRBIT(*swap_flag2, VRF_GOINGUP);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1032
	CLRBIT(*swap_flag2, VRF_GOINGDOWN);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1033
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1034
	/* Reverse the rail-flags (if needed) */
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1035
	if (HASBIT(flag1, VRF_GOINGUP)) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1036
		SETBIT(*swap_flag2, VRF_GOINGDOWN);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1037
	} else if (HASBIT(flag1, VRF_GOINGDOWN)) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1038
		SETBIT(*swap_flag2, VRF_GOINGUP);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1039
	}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1040
	if (HASBIT(flag2, VRF_GOINGUP)) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1041
		SETBIT(*swap_flag1, VRF_GOINGDOWN);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1042
	} else if (HASBIT(flag2, VRF_GOINGDOWN)) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1043
		SETBIT(*swap_flag1, VRF_GOINGUP);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1044
	}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1045
}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1046
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
	Vehicle *a, *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
	/* locate vehicles to swap */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
	for(a=v; l!=0; l--) { a = a->next; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	for(b=v; r!=0; r--) { b = b->next; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
	if (a != b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
		/* swap the hidden bits */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
			uint16 tmp = (a->vehstatus & ~VS_HIDDEN) | (b->vehstatus&VS_HIDDEN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
			b->vehstatus = (b->vehstatus & ~VS_HIDDEN) | (a->vehstatus&VS_HIDDEN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
			a->vehstatus = tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1062
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
		/* swap variables */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
		swap_byte(&a->u.rail.track, &b->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
		swap_byte(&a->direction, &b->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1066
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1067
		/* toggle direction */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
		if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
		if (!(b->u.rail.track & 0x80)) b->direction ^= 4;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1070
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
		/* swap more variables */
1174
6a5e747f3ba6 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1151
diff changeset
  1072
		swap_int32(&a->x_pos, &b->x_pos);
6a5e747f3ba6 (svn r1676) Increase the size of TileIndex and TileIndexDiff to 32bits and adapt the save/load data and some other parts of the code to that change
tron
parents: 1151
diff changeset
  1073
		swap_int32(&a->y_pos, &b->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1074
		swap_tile(&a->tile, &b->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
		swap_byte(&a->z_pos, &b->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1077
		SwapTrainFlags(&a->u.rail.flags, &b->u.rail.flags);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1078
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
		/* update other vars */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1080
		UpdateVarsAfterSwap(a);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
		UpdateVarsAfterSwap(b);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
		if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1084
		UpdateVarsAfterSwap(a);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
744
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1088
/* Check if the vehicle is a train and is on the tile we are testing */
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1089
static void *TestTrainOnCrossing(Vehicle *v, void *data)
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1090
{
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1091
	if (v->tile != *(const TileIndex*)data || v->type != VEH_Train)
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1092
		return NULL;
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1093
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1094
	return v;
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1095
}
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1096
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1097
static void DisableTrainCrossing(TileIndex tile)
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1098
{
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1099
	/* Test if we have a rail/road-crossing */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1100
	if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) {
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1101
		/* Check if there is a train on the tile itself */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1102
		if (VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL) {
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1103
			/* If light is on, switch light off */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1104
			if (_map5[tile] & 4) {
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1105
				_map5[tile] &= ~4;
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1106
				MarkTileDirtyByTile(tile);
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1107
			}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1108
		}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1109
	}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1110
}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1111
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1112
static void ReverseTrainDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1113
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1114
	int l = 0, r = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1115
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1116
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
	if (IsTrainDepotTile(v->tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1118
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1120
	/* Check if we were approaching a rail/road-crossing */
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1121
	{
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1122
		TileIndex tile = v->tile;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1123
		int t;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1124
		/* Determine the non-diagonal direction in which we will exit this tile */
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1125
		t = v->direction >> 1;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1126
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1127
			t = (t - 1) & 3;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1128
		}
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1129
		/* Calculate next tile */
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 889
diff changeset
  1130
		tile += TileOffsByDir(t);
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1131
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1132
		/* Check if the train left a rail/road-crossing */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1133
		DisableTrainCrossing(tile);
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1134
	}
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1135
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1136
	// count number of vehicles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1138
	do r++; while ( (u = u->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1139
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1140
	/* swap start<>end, start+1<>end-1, ... */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1141
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1142
		ReverseTrainSwapVeh(v, l++, r--);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1143
	} while (l <= r);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1144
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1145
	if (IsTrainDepotTile(v->tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1146
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1147
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1148
	CLRBIT(v->u.rail.flags, VRF_REVERSING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1149
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1150
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1151
/* p1 = vehicle */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1152
int32 CmdReverseTrainDirection(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1153
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1156
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1158
	if (!CheckOwnership(v->owner))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1159
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1160
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1161
	_error_message = STR_EMPTY;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1162
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1163
//	if (v->u.rail.track & 0x80 || IsTrainDepotTile(v->tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1164
//		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1165
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1166
	if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1167
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1168
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1169
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1170
		if (_patches.realistic_acceleration && v->cur_speed != 0) {
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1171
			TOGGLEBIT(v->u.rail.flags, VRF_REVERSING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1172
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1173
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1174
			SetLastSpeed(v, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1175
			ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1176
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1177
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1178
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1179
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1180
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1181
int32 CmdForceTrainProceed(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1182
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1183
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1184
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1185
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1186
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1187
	if (!CheckOwnership(v->owner))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1188
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1189
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1190
	if (flags & DC_EXEC)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1191
		v->u.rail.force_proceed = 0x50;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1192
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1193
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1194
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1195
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1196
// p1 = vehicle to refit
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1197
// p2 = new cargo (0xFF)
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1198
// p2 = skip check for stopped in hanger (0x0100)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1199
int32 CmdRefitRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1200
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1201
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1202
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1203
	uint num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1204
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1205
	byte SkipStoppedInDepotCheck = (p2 & 0x100) >> 8;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1206
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1207
	p2 = p2 & 0xFF;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1208
889
64d2f7d5f02b (svn r1375) -Fix: [1050990] Buying trains sometimes accounted for incorrectly. Was the result of the cost getting reset in a recursive call of docommand. That is fixed. In addition all cost-commands are typed explicitely. Please do not forget to do so or your costs will be credited to construction if you are unlucky.
darkvater
parents: 882
diff changeset
  1209
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1210
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1211
	v = GetVehicle(p1);
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1212
	if (!CheckOwnership(v->owner) || ((CheckStoppedInDepot(v) < 0) && !(SkipStoppedInDepotCheck)))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1213
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1214
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1215
	cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1216
	num = 0;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1217
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1218
	do {
491
6527219352cd (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1219
		/* XXX: We also refit all the attached wagons en-masse if they
6527219352cd (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1220
		 * can be refitted. This is how TTDPatch does it.  TODO: Have
6527219352cd (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1221
		 * some nice [Refit] button near each wagon. --pasky */
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  1222
		if ((!(RailVehInfo(v->engine_type)->flags & RVI_WAGON)
491
6527219352cd (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1223
		     || (_engine_refit_masks[v->engine_type] & (1 << p2)))
6527219352cd (svn r773) When refitting a train engine, refit also all the attached wagons which can be refitted (applicable to DBSetXL, as shown in http://pasky.or.cz/~pasky/dev/openttd/screenshots/wagrefit.png). This is how TTDPatch does it, pointed out by Bjarni.
pasky
parents: 445
diff changeset
  1224
		    && (byte) p2 != v->cargo_type && v->cargo_cap != 0) {
15
255d0f685325 (svn r16) Fix: Train refit cost based on _price.ship_base
dominik
parents: 11
diff changeset
  1225
			cost += (_price.build_railvehicle >> 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1226
			num += v->cargo_cap;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1227
			if (flags & DC_EXEC) {
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1228
		//autorefitted train cars wants to keep the cargo
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1229
		//it will be checked if the cargo is valid in CmdReplaceVehicle
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1230
				if (!(SkipStoppedInDepotCheck))
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1231
					v->cargo_count = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1232
				v->cargo_type = (byte)p2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1233
				InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1234
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1235
		}
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1236
	// SkipStoppedInDepotCheck is called by CmdReplace and it should only apply to the single car it is called for
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1237
	} while ( (v=v->next) != NULL || SkipStoppedInDepotCheck );
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1239
	_returned_refit_amount = num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1240
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1241
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1242
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1243
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1244
int GetDepotByTile(uint tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1245
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1246
	Depot *d;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
	int i=0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
	for(d=_depots; d->xy != (TileIndex)tile; d++) { i++; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1249
	return i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
typedef struct TrainFindDepotData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
	uint best_length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
	uint tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
	byte owner;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
} TrainFindDepotData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
static bool TrainFindDepotEnumProc(uint tile, TrainFindDepotData *tfdd, int track, uint length, byte *state)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
{
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1260
	if (IsTileType(tile, MP_RAILWAY) && _map_owner[tile] == tfdd->owner) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
		if ((_map5[tile] & ~0x3) == 0xC0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
			if (length < tfdd->best_length) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
				tfdd->best_length = length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
				tfdd->tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
		// make sure the train doesn't run against a oneway signal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
		if ((_map5[tile] & 0xC0) == 0x40) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1271
			if (!(_map3_lo[tile] & _signal_onedir[track]) && _map3_lo[tile] & _signal_otherdir[track])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
	// stop  searching if we've found a destination that is closer already.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
	return length >= tfdd->best_length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1280
// returns the tile of a depot to goto to.
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1281
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
	TrainFindDepotData tfdd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1285
	uint tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1287
	tfdd.owner = v->owner;
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1288
	tfdd.best_length = (uint)-1;
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1289
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1290
	if (IsTrainDepotTile(tile)){
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1291
		tfdd.tile = tile;
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1292
		tfdd.best_length = 0;
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1293
		return tfdd;
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1294
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1296
	if (v->u.rail.track == 0x40) { tile = GetVehicleOutOfTunnelTile(v); }
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1297
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1298
	if (!_patches.new_depot_finding) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1299
		// search in all directions
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1300
		for(i=0; i!=4; i++)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1301
			NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1302
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
		// search in the forward direction first.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1304
		i = v->direction >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1305
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; }
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1306
		NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL);
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1307
		if (tfdd.best_length == (uint)-1){
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1308
			// search in backwards direction
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1309
			i = (v->direction^4) >> 1;
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1310
			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; }
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1311
			NewTrainPathfind(tile, i, (TPFEnumProc*)TrainFindDepotEnumProc, &tfdd, NULL);
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1312
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1313
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1314
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1315
	return tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1316
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1317
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1318
/*  Send a train to the nearest depot
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1319
	p1 = index of the train
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1320
	p2 = bit 0 = do not stop in depot
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1321
		 bit 1 = set v->set_for_replacement
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1322
		 bit 2 = clear v->set_for_replacement */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1323
int32 CmdTrainGotoDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1324
{
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1325
	Vehicle *v = GetVehicle(p1);
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1326
	TrainFindDepotData tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1327
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1328
	if (!CheckOwnership(v->owner))
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1329
		return CMD_ERROR;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1330
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1331
	if (HASBIT(p2, 0)) v->set_for_replacement = true;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1332
	if (HASBIT(p2, 2)) v->set_for_replacement = false;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1333
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1334
	if (HASBIT(p2, 1) || HASBIT(p2, 2)) return CMD_ERROR;   // vehicle has a depot in schedule. It just needed to alter set_for_replacement
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1335
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1336
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1337
	if (v->current_order.type == OT_GOTO_DEPOT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
		if (flags & DC_EXEC) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1339
			if (v->current_order.flags & OF_UNLOAD) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
				v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
				v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1343
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1344
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1345
			v->current_order.flags = 0;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1346
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1349
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1351
	tfdd = FindClosestTrainDepot(v);
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1352
	if (tfdd.best_length == (uint)-1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
		return_cmd_error(STR_883A_UNABLE_TO_FIND_ROUTE_TO);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1354
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
	if (flags & DC_EXEC) {
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  1356
		v->dest_tile = tfdd.tile;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1357
		v->current_order.type = OT_GOTO_DEPOT;
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1358
		v->current_order.flags = HASBIT(p2, 0) ? 0 : OF_NON_STOP | OF_FULL_LOAD;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1359
		v->current_order.station = GetDepotByTile(tfdd.tile);
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  1360
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1361
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1362
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1365
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1366
/* p1 = vehicle
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1367
 * p2 = new service int
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1368
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
int32 CmdChangeTrainServiceInt(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
{
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1371
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1372
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1373
	if (!CheckOwnership(v->owner))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1375
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1376
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1377
		v->service_interval = (uint16)p2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1378
		InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1379
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1380
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1381
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1382
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1383
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1087
diff changeset
  1384
void OnTick_Train(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1385
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1386
	_age_cargo_skip_counter = (_age_cargo_skip_counter == 0) ? 184 : (_age_cargo_skip_counter - 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1387
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1388
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1389
static const int8 _vehicle_smoke_pos[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1390
	-4, -4, -4, 0, 4, 4, 4, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1391
	-4,  0,  4, 4, 4, 0,-4,-4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1392
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1393
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1394
static void HandleLocomotiveSmokeCloud(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1395
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1396
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1397
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1398
	if (v->vehstatus & VS_TRAIN_SLOWING || v->load_unload_time_rem != 0 || v->cur_speed < 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1399
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1400
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1401
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1402
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1403
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1404
		int engtype = v->engine_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1405
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1406
		// no smoke?
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  1407
		if (RailVehInfo(engtype)->flags & 2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1408
		    || _engines[engtype].railtype > 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1409
		    || (v->vehstatus&VS_HIDDEN) || (v->u.rail.track & 0xC0) )
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1410
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1411
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  1412
		switch (RailVehInfo(engtype)->engclass) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1413
		case 0:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1414
			// steam smoke.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1415
			if ( (v->tick_counter&0xF) == 0 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile)) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1416
				CreateEffectVehicleRel(v,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1417
					(_vehicle_smoke_pos[v->direction]),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1418
					(_vehicle_smoke_pos[v->direction+8]),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1419
					10,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1420
					EV_STEAM_SMOKE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1421
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1422
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1423
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1424
		case 1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1425
			// diesel smoke
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1426
			if (u->cur_speed <= 40 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x1E00) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1427
				CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_3);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1428
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1429
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1430
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1431
		case 2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1432
			// blue spark
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1433
			if ( (v->tick_counter&0x3) == 0 && !IsTrainDepotTile(v->tile) && !IsTunnelTile(v->tile) && (uint16)Random() <= 0x5B0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1434
				CreateEffectVehicleRel(v, 0,0,10, EV_SMOKE_2);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1435
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1436
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1437
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1438
	} while ( (v = v->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1439
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1440
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1442
static void TrainPlayLeaveStationSound(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1443
{
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1444
	static const SoundFx sfx[] = {
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1445
		SND_04_TRAIN,
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1446
		SND_0A_TRAIN_HORN,
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1447
		SND_0A_TRAIN_HORN
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1448
	};
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1449
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1450
	int engtype = v->engine_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1451
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1452
	switch (_engines[engtype].railtype) {
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1453
		case 0:
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  1454
			SndPlayVehicleFx(sfx[RailVehInfo(engtype)->engclass], v);
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1455
			break;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1456
		case 1:
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1457
			SndPlayVehicleFx(SND_47_MAGLEV_2, v);
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1458
			break;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1459
		case 2:
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1460
			SndPlayVehicleFx(SND_41_MAGLEV, v);
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1461
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1462
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1463
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1464
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1465
static bool CheckTrainStayInDepot(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1466
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
	Vehicle *u;
495
6b3e2b4fa7c9 (svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents: 491
diff changeset
  1468
6b3e2b4fa7c9 (svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents: 491
diff changeset
  1469
	// bail out if not all wagons are in the same depot or not in a depot at all
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  1470
	for (u = v; u != NULL; u = u->next)
495
6b3e2b4fa7c9 (svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents: 491
diff changeset
  1471
		if (u->u.rail.track != 0x80 || u->tile != v->tile)
6b3e2b4fa7c9 (svn r785) -Fix: A train can leave and enter the same depot at the same time, then the trai simply got stuck
tron
parents: 491
diff changeset
  1472
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1473
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1474
	if (v->u.rail.force_proceed == 0) {
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1475
		if (++v->load_unload_time_rem < 37) {
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1476
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1477
			return true;
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1478
		}
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1479
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1480
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1481
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1482
		if (UpdateSignalsOnSegment(v->tile, v->direction)) {
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1483
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1484
			return true;
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1485
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1486
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1487
578
1e66514eb621 (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  1488
	VehicleServiceInDepot(v);
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1489
	InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1490
	TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1491
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1492
	v->u.rail.track = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1493
	if (v->direction & 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1494
		v->u.rail.track = 2;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1495
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1496
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1497
	v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1498
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1499
	UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1500
	v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1501
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1502
	UpdateSignalsOnSegment(v->tile, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1503
	UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1504
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1505
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1506
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1507
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1508
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1509
typedef struct TrainTrackFollowerData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1510
	TileIndex dest_coords;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1511
	int station_index; // station index we're heading for
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1512
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1513
	uint best_track_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1514
	byte best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1515
} TrainTrackFollowerData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1516
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1517
static bool TrainTrackFollower(uint tile, TrainTrackFollowerData *ttfd, int track, uint length, byte *state){
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1518
	if (IsTileType(tile, MP_RAILWAY) && (_map5[tile]&0xC0) == 0x40) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1519
		// the tile has a signal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1520
		byte m3 = _map3_lo[tile];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1521
		if (!(m3 & _signal_onedir[track])) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1522
			// if one way signal not pointing towards us, stop going in this direction.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1523
			if (m3 & _signal_otherdir[track])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
				return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1525
		} else if (_map2[tile] & _signal_onedir[track]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
			// green signal in our direction. either one way or two way.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
			*state = true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1528
		} else if (m3 & _signal_otherdir[track]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1529
			// two way signal. unless we passed another green signal on the way,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1530
			// stop going in this direction.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1531
			if (!*state) return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1532
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1533
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1534
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1535
	// heading for nowhere?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1536
	if (ttfd->dest_coords == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1537
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1538
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1539
	// did we reach the final station?
166
683136f1e8a8 (svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents: 164
diff changeset
  1540
 if ((ttfd->station_index == -1 && tile == ttfd->dest_coords) ||
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  1541
  (IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_map5[tile], 0, 8) && _map2[tile] == ttfd->station_index)) {
166
683136f1e8a8 (svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents: 164
diff changeset
  1542
  /* We do not check for dest_coords if we have a station_index,
683136f1e8a8 (svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents: 164
diff changeset
  1543
   * because in that case the dest_coords are just an
683136f1e8a8 (svn r167) -Fix: [972247] bug in '[a] weird non-uniform stations handling'
truelight
parents: 164
diff changeset
  1544
   * approximation of where the station is */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
		// found station
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
		ttfd->best_bird_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
		if (length < ttfd->best_track_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1548
			ttfd->best_track_dist = length;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1549
			ttfd->best_track = state[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1550
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
		uint dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
		// we've actually found the destination already. no point searching in directions longer than this.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1556
		if (ttfd->best_track_dist != (uint)-1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1557
			return length >= ttfd->best_track_dist;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1558
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
		// didn't find station
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
		dist = GetTileDist(tile, ttfd->dest_coords);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1561
		if (dist < ttfd->best_bird_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
			ttfd->best_bird_dist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
			ttfd->best_track = state[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1564
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1565
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1566
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1567
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1569
static void FillWithStationData(TrainTrackFollowerData *fd, Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
{
103
3af99b9373a1 (svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents: 98
diff changeset
  1571
        fd->dest_coords = v->dest_tile;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1572
        if (v->current_order.type == OT_GOTO_STATION)
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1573
                fd->station_index = v->current_order.station;
103
3af99b9373a1 (svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents: 98
diff changeset
  1574
        else
3af99b9373a1 (svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents: 98
diff changeset
  1575
                fd->station_index = -1;
3af99b9373a1 (svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents: 98
diff changeset
  1576
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1577
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1578
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1579
static const byte _initial_tile_subcoord[6][4][3] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
{{ 15, 8, 1 },{ 0, 0, 0 },{ 0, 8, 5 },{ 0, 0, 0 }},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1581
{{  0, 0, 0 },{ 8, 0, 3 },{ 0, 0, 0 },{ 8,15, 7 }},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
{{  0, 0, 0 },{ 7, 0, 2 },{ 0, 7, 6 },{ 0, 0, 0 }},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
{{ 15, 8, 2 },{ 0, 0, 0 },{ 0, 0, 0 },{ 8,15, 6 }},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1584
{{ 15, 7, 0 },{ 8, 0, 4 },{ 0, 0, 0 },{ 0, 0, 0 }},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
{{  0, 0, 0 },{ 0, 0, 0 },{ 0, 8, 4 },{ 7,15, 0 }},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1588
static const uint32 _reachable_tracks[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
	0x10091009,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
	0x00160016,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
	0x05200520,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1592
	0x2A002A00,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1593
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1594
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1595
static const byte _search_directions[6][4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1596
	{ 0, 9, 2, 9 }, // track 1
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1597
	{ 9, 1, 9, 3 }, // track 2
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1598
	{ 9, 0, 3, 9 }, // track upper
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1599
	{ 1, 9, 9, 2 }, // track lower
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1600
	{ 3, 2, 9, 9 }, // track left
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1601
	{ 9, 9, 1, 0 }, // track right
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1602
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1603
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1604
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1606
/* choose a track */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1607
static byte ChooseTrainTrack(Vehicle *v, uint tile, int direction, byte trackbits)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1608
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1609
	TrainTrackFollowerData fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1610
	int bits = trackbits;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1611
	uint best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1612
#if 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1613
	int time = rdtsc();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1614
	static float f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1615
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1616
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1617
	assert( (bits & ~0x3F) == 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1618
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1619
	/* quick return in case only one possible direction is available */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1620
	if (KILL_FIRST_BIT(bits) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1621
		return FIND_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1622
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1623
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1625
	if (_patches.new_pathfinding) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1626
		fd.best_bird_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
		fd.best_track_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
		fd.best_track = 0xFF;
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 889
diff changeset
  1629
		NewTrainPathfind(tile - TileOffsByDir(direction), direction, (TPFEnumProc*)TrainTrackFollower, &fd, NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1630
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1631
//		printf("Train %d %s\n", v->unitnumber, fd.best_track_dist == -1 ? "NOTFOUND" : "FOUND");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1632
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1633
		if (fd.best_track == 0xff) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1634
			// blaha
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1635
			best_track = FIND_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1636
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1637
			best_track = fd.best_track & 7;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1638
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1639
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
		int i, r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1641
		uint best_bird_dist  = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1642
		uint best_track_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
		byte train_dir = v->direction & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1645
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1646
		best_track = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1647
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1648
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1649
			i = FIND_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1650
			bits = KILL_FIRST_BIT(bits);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1651
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1652
			fd.best_bird_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1653
			fd.best_track_dist = (uint)-1;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1654
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1655
			NewTrainPathfind(tile, _search_directions[i][direction], (TPFEnumProc*)TrainTrackFollower, &fd, NULL);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1656
			if (best_track != (uint)-1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1657
				if (best_track_dist == (uint)-1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1658
					if (fd.best_track_dist == (uint)-1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1659
						/* neither reached the destination, pick the one with the smallest bird dist */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1660
						if (fd.best_bird_dist > best_bird_dist) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1661
						if (fd.best_bird_dist < best_bird_dist) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1662
					} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1663
						/* we found the destination for the first time */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1664
						goto good;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1665
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1666
				} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1667
					if (fd.best_track_dist == (uint)-1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1668
						/* didn't find destination, but we've found the destination previously */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1669
						goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1670
					} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1671
						/* both old & new reached the destination, compare track length */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1672
						if (fd.best_track_dist > best_track_dist) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1673
						if (fd.best_track_dist < best_track_dist) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1674
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1675
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1676
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1677
				/* if we reach this position, there's two paths of equal value so far.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1678
				 * pick one randomly. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1679
				r = (byte)Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1680
				if (_pick_track_table[i] == train_dir) r += 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
				if (_pick_track_table[best_track] == train_dir) r -= 80;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1682
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1683
				if (r <= 127) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1684
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1685
	good:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1686
			best_track = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1687
			best_bird_dist = fd.best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1688
			best_track_dist = fd.best_track_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1689
	bad:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
		} while (bits != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
//		printf("Train %d %s\n", v->unitnumber, best_track_dist == -1 ? "NOTFOUND" : "FOUND");
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1692
		assert(best_track != (uint)-1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1694
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1695
#if 0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
	time = rdtsc() - time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1697
	f = f * 0.99 + 0.01 * time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1698
	printf("PF time = %d %f\n", time, f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1699
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1700
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1701
	return best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1702
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1703
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1704
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1705
static bool CheckReverseTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1706
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
	TrainTrackFollowerData fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1708
	int i, r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1709
	int best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1710
	uint best_bird_dist  = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1711
	uint best_track_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1712
	uint reverse, reverse_best;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1713
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1714
	if (_opt.diff.line_reverse_mode != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1715
			v->u.rail.track & 0xC0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1716
			!(v->direction & 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1719
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1720
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
	best_track = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1722
	reverse_best = reverse = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1723
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1724
	assert(v->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1725
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1726
	i = _search_directions[FIND_FIRST_BIT(v->u.rail.track)][v->direction>>1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1728
	while(true) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
		fd.best_bird_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1730
		fd.best_track_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1731
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1732
		NewTrainPathfind(v->tile, reverse ^ i, (TPFEnumProc*)TrainTrackFollower, &fd, NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1733
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1734
		if (best_track != -1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1735
			if (best_bird_dist != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1736
				if (fd.best_bird_dist != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1737
					/* neither reached the destination, pick the one with the smallest bird dist */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1738
					if (fd.best_bird_dist > best_bird_dist) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1739
					if (fd.best_bird_dist < best_bird_dist) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1740
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
					/* we found the destination for the first time */
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1742
					goto good;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1743
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1744
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1745
				if (fd.best_bird_dist != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1746
					/* didn't find destination, but we've found the destination previously */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1747
					goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1748
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1749
					/* both old & new reached the destination, compare track length */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1750
					if (fd.best_track_dist > best_track_dist) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
					if (fd.best_track_dist < best_track_dist) goto good;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1754
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1755
			/* if we reach this position, there's two paths of equal value so far.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1756
			 * pick one randomly. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1757
			r = (byte)Random();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1758
			if (_pick_track_table[i] == (v->direction & 3)) r += 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1759
			if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1760
			if (r <= 127) goto bad;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1761
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1762
good:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1763
		best_track = i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1764
		best_bird_dist = fd.best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
		best_track_dist = fd.best_track_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1766
		reverse_best = reverse;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
bad:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1768
		if (reverse != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1769
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
		reverse = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1771
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
	return reverse_best != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1775
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1776
static bool ProcessTrainOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1777
{
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1778
	const Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1779
	bool result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1780
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1781
	// These are un-interruptible
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1782
	if (v->current_order.type >= OT_GOTO_DEPOT &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1783
			v->current_order.type <= OT_LEAVESTATION) {
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1784
		// Let a depot order in the orderlist interrupt.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1785
		if (v->current_order.type != OT_GOTO_DEPOT ||
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1786
				!(v->current_order.flags & OF_UNLOAD))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1787
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1788
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1790
	if (v->current_order.type == OT_GOTO_DEPOT &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1791
			(v->current_order.flags & (OF_UNLOAD | OF_FULL_LOAD)) ==  (OF_UNLOAD | OF_FULL_LOAD) &&
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1792
			!VehicleNeedsService(v) && !v->set_for_replacement) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
		v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1794
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1795
395
788a9bba0889 (svn r587) -newgrf: Rename all /Checkpoint/i tokens to 'Waypoint's. The name actually makes some sense and is also compatible with TTDPatch (pasky).
darkvater
parents: 358
diff changeset
  1796
	// check if we've reached the waypoint?
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1797
	if (v->current_order.type == OT_GOTO_WAYPOINT && v->tile == v->dest_tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1798
		v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1801
	// check if we've reached a non-stop station while TTDPatch nonstop is enabled..
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1802
	if (_patches.new_nonstop && v->current_order.flags & OF_NON_STOP &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1803
			v->current_order.station == _map2[v->tile]) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1804
		v->cur_order_index++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1806
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1807
	// Get the current order
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1808
	if (v->cur_order_index >= v->num_orders)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
		v->cur_order_index = 0;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1810
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1811
	order = GetVehicleOrder(v, v->cur_order_index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1813
	// If no order, do nothing.
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1814
	if (order == NULL) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1815
		v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1816
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1817
		v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1818
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1819
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1820
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1821
	// If it is unchanged, keep it.
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1822
	if (order->type    == v->current_order.type &&
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1823
			order->flags   == v->current_order.flags &&
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1824
			order->station == v->current_order.station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1825
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1826
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
	// Otherwise set it, and determine the destination tile.
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1828
	v->current_order = *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1830
	v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1831
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1832
	result = false;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1833
	switch (order->type) {
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1834
		case OT_GOTO_STATION:
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1835
			if (order->station == v->last_station_visited)
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1836
				v->last_station_visited = 0xFFFF;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1837
			v->dest_tile = GetStation(order->station)->xy;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1838
			result = CheckReverseTrain(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1839
			break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1840
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1841
		case OT_GOTO_DEPOT:
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1842
			v->dest_tile = _depots[order->station].xy;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1843
			result = CheckReverseTrain(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1844
			break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1845
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1846
		case OT_GOTO_WAYPOINT:
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1847
			v->dest_tile = _waypoints[order->station].xy;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1848
			result = CheckReverseTrain(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1849
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1850
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1851
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1852
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1854
	return result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1855
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1856
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1857
static void MarkTrainDirty(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1858
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1859
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1860
		v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
		MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
	} while ( (v=v->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1863
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
static void HandleTrainLoading(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1866
{
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1867
	if (v->current_order.type == OT_NOTHING)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1868
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1869
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1870
	if (v->current_order.type != OT_DUMMY) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1871
		if (v->current_order.type != OT_LOADING)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1873
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1874
		if (mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1875
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1876
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1877
		// don't mark the train as lost if we're loading on the final station.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1878
		if (v->current_order.flags & OF_NON_STOP)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1879
			v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1880
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1881
		if (--v->load_unload_time_rem)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1882
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1883
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1884
		if (v->current_order.flags & OF_FULL_LOAD && CanFillVehicle(v)) {
882
64665fd87871 (svn r1367) -Fix: Full-Loading trains no longer get "lost" after a while (Hackykid)
matthijs
parents: 842
diff changeset
  1885
			v->u.rail.days_since_order_progr = 0; /* Prevent a train lost message for full loading trains */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1886
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1887
			if (LoadUnloadVehicle(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1888
				InvalidateWindow(WC_TRAINS_LIST, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1889
				MarkTrainDirty(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1890
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1891
				// need to update acceleration since the goods on the train changed.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1892
				UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1893
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1894
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1895
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1896
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1897
		TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1898
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
		{
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1900
			Order b = v->current_order;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1901
			v->current_order.type = OT_LEAVESTATION;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1902
			v->current_order.flags = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1903
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1904
			if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1905
				// only the vehicle owner needs to calculate the rest (locally)
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1906
				if ((_autoreplace_array[v->engine_type] != v->engine_type) ||
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1907
					(_patches.autorenew && v->age - v->max_age > (_patches.autorenew_months * 30))) {
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1908
					byte flags = 1;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1909
					// the flags means, bit 0 = needs to go to depot, bit 1 = have depot in orders
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1910
					if (VehicleHasDepotOrders(v)) SETBIT(flags, 1);
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1911
					if (!(HASBIT(flags, 1) && v->set_for_replacement)) {
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1912
						_current_player = _local_player;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1913
						DoCommandP(v->tile, v->index, flags, NULL, CMD_TRAIN_GOTO_DEPOT | CMD_SHOW_NO_ERROR);
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1914
						_current_player = OWNER_NONE;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1915
					}
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1916
				} else { // no need to go to a depot
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1917
					if (v->set_for_replacement) {
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1918
						// it seems that the user clicked "Stop replacing"
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1919
						_current_player = _local_player;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1920
						DoCommandP(v->tile, v->index, 1 | (1 << 2), NULL, CMD_TRAIN_GOTO_DEPOT | CMD_SHOW_NO_ERROR);
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1921
						_current_player = OWNER_NONE;
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1922
					}
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1923
				}
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  1924
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
			// If this was not the final order, don't remove it from the list.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1926
			if (!(b.flags & OF_NON_STOP))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1928
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1929
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1930
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1931
	v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1932
	v->cur_order_index++;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  1933
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1934
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1935
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1936
static int UpdateTrainSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1937
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1938
	uint spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1939
	uint accel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1940
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1941
	if (v->vehstatus & VS_STOPPED || HASBIT(v->u.rail.flags, VRF_REVERSING)) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1942
		if (_patches.realistic_acceleration)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1943
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1944
		else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1945
			accel = v->acceleration * -2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1946
	} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1947
		if (_patches.realistic_acceleration)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1948
			accel = GetTrainAcceleration(v, AM_ACCEL);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1949
		else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1950
			accel = v->acceleration;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1951
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1952
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1953
	spd = v->subspeed + accel * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1954
	v->subspeed = (byte)spd;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1955
	{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1956
		int tempmax = v->max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1957
		if (v->cur_speed > v->max_speed)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1958
			tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1959
		v->cur_speed = spd = clamp(v->cur_speed + ((int)spd >> 8), 0, tempmax);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  1960
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1961
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1962
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1963
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1964
	spd += v->progress;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1965
	v->progress = (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1966
	return (spd >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1967
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1969
static void TrainEnterStation(Vehicle *v, int station)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1970
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1971
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1972
	uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1973
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1974
	v->last_station_visited = station;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1975
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1976
	/* check if a train ever visited this station before */
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1977
	st = GetStation(station);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1978
	if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1979
		st->had_vehicle_of_type |= HVOT_TRAIN;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  1980
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1981
		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);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1982
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1983
			STR_8801_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1984
			flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1985
			v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1986
			0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1987
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1988
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1989
	// Did we reach the final destination?
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1990
	if (v->current_order.type == OT_GOTO_STATION &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1991
			v->current_order.station == (byte)station) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1992
		// Yeah, keep the load/unload flags
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1993
		// Non Stop now means if the order should be increased.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1994
		v->current_order.type = OT_LOADING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1995
		v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1996
		v->current_order.flags |= OF_NON_STOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1997
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1998
		// No, just do a simple load
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1999
		v->current_order.type = OT_LOADING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2000
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2001
	}
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2002
	v->current_order.station = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2003
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2004
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
	if (LoadUnloadVehicle(v) != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
		InvalidateWindow(WC_TRAINS_LIST, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
		MarkTrainDirty(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2008
		UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2009
	}
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2010
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2011
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2012
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2013
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2015
	byte new_z, old_z;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2016
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
	// need this hint so it returns the right z coordinate on bridges.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
	_get_z_hint = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
	new_z = GetSlopeZ(v->x_pos, v->y_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2020
	_get_z_hint = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2022
	old_z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2023
	v->z_pos = new_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2025
	if (new_tile) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2026
		CLRBIT(v->u.rail.flags, VRF_GOINGUP);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2027
		CLRBIT(v->u.rail.flags, VRF_GOINGDOWN);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2028
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2029
		if (new_z != old_z) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2030
			SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2031
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2032
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2035
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2036
	return old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2037
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2038
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2039
static const byte _new_vehicle_direction_table[11] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2040
	0, 7, 6, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2041
	1, 0, 5, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2042
	2, 3, 4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2043
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2044
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2045
static int GetNewVehicleDirectionByTile(uint new_tile, uint old_tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2046
{
926
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
  2047
	uint offs = (TileY(new_tile) - TileY(old_tile) + 1) * 4 +
a6d140a6a4de (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 919
diff changeset
  2048
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2049
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2050
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2051
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2052
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2053
static int GetNewVehicleDirection(Vehicle *v, int x, int y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2054
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2055
	uint offs = (y - v->y_pos + 1) * 4 + (x - v->x_pos + 1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2056
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2057
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2058
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2059
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2060
static int GetDirectionToVehicle(Vehicle *v, int x, int y)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2061
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2062
	byte offs;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2063
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2064
	x -= v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2065
	if (x >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2066
		offs = (x > 2) ? 0 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2067
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2068
		offs = (x < -2) ? 2 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2069
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2070
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2071
	y -= v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2072
	if (y >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2073
		offs += ((y > 2) ? 0 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2074
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2075
		offs += ((y < -2) ? 2 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2076
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2077
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2078
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2082
/* Check if the vehicle is compatible with the specified tile */
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2083
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
{
1214
8262981ac274 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1209
diff changeset
  2085
	switch (GetTileType(tile)) {
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2086
		case MP_RAILWAY:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2087
		case MP_STATION:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2088
			// normal tracks, jump to owner check
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2089
			break;
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2090
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2091
		case MP_TUNNELBRIDGE:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2092
			if ((_map5[tile] & 0xC0) == 0xC0) { // is bridge middle part?
1192
2649bd4e556a (svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents: 1179
diff changeset
  2093
				uint height;
2649bd4e556a (svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents: 1179
diff changeset
  2094
				uint tileh = GetTileSlope(tile, &height);
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2095
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2096
				// correct Z position of a train going under a bridge on slopes
1192
2649bd4e556a (svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents: 1179
diff changeset
  2097
				if (CORRECT_Z(tileh)) height += 8;
2649bd4e556a (svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents: 1179
diff changeset
  2098
2649bd4e556a (svn r1696) Use GetTileSlope() instead of FindLandscapeHeightByTile() where it is sufficient. FindLandscapeHeightByTile() uses GetTileSlope() internally and adds some more info, which is discarded in these cases.
tron
parents: 1179
diff changeset
  2099
				if (v->z_pos != height) return true; // train is going over bridge
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2100
			}
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2101
			break;
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2102
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2103
		case MP_STREET:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2104
			// tracks over roads, do owner check of tracks (_map_owner[tile])
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2105
			return
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2106
				_map_owner[tile] == v->owner &&
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2107
				(v->subtype != TS_Front_Engine || (_map3_hi[tile] & 0xF) == v->u.rail.railtype);
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2108
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2109
		default:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2110
			return true;
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2111
	}
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2112
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2113
	return
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2114
		_map_owner[tile] == v->owner &&
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2115
		(v->subtype != TS_Front_Engine || (_map3_lo[tile] & 0xF) == v->u.rail.railtype);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2116
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2117
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2118
typedef struct {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2119
	byte small_turn, large_turn;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2120
	byte z_up; // fraction to remove when moving up
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2121
	byte z_down; // fraction to remove when moving down
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2122
} RailtypeSlowdownParams;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2123
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2124
static const RailtypeSlowdownParams _railtype_slowdown[3] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2125
	// normal accel
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2126
	{256/4, 256/2, 256/4, 2}, // normal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2127
	{256/4, 256/2, 256/4, 2}, // monorail
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2128
	{0,     256/2, 256/4, 2}, // maglev
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2129
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2131
/* Modify the speed of the vehicle due to a turn */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2132
static void AffectSpeedByDirChange(Vehicle *v, byte new_dir)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2133
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2134
	byte diff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2135
	const RailtypeSlowdownParams *rsp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2136
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2137
	if (_patches.realistic_acceleration || (diff = (v->direction - new_dir) & 7) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2138
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2139
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2140
	rsp = &_railtype_slowdown[v->u.rail.railtype];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2141
	v->cur_speed -= ((diff == 1 || diff == 7) ? rsp->small_turn : rsp->large_turn) * v->cur_speed >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2142
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2143
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2144
/* Modify the speed of the vehicle due to a change in altitude */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2145
static void AffectSpeedByZChange(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2146
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2147
	const RailtypeSlowdownParams *rsp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2148
	if (old_z == v->z_pos || _patches.realistic_acceleration)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2149
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2150
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2151
	rsp = &_railtype_slowdown[v->u.rail.railtype];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2152
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2153
	if (old_z < v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2154
		v->cur_speed -= (v->cur_speed * rsp->z_up >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2155
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2156
		uint16 spd = v->cur_speed + rsp->z_down;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2157
		if (spd <= v->max_speed)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2158
			v->cur_speed = spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2159
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2160
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2161
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2162
static const byte _otherside_signal_directions[14] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2163
	1, 3, 1, 3, 5, 3, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2164
	5, 7, 7, 5, 7, 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2165
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2166
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2167
static void TrainMovedChangeSignals(uint tile, int dir)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2168
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2169
	int i;
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  2170
	if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xC0) == 0x40) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2171
		i = FindFirstBit2x64((_map5[tile]+(_map5[tile]<<8)) & _reachable_tracks[dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2172
		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2173
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2174
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2175
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2176
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2177
typedef struct TrainCollideChecker {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2178
	Vehicle *v, *v_skip;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2179
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2180
} TrainCollideChecker;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2181
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
  2182
static void *FindTrainCollideEnum(Vehicle *v, TrainCollideChecker *tcc)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2183
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2184
	if (v == tcc->v || v == tcc->v_skip || v->type != VEH_Train || v->u.rail.track==0x80)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2185
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2186
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2187
	if ( myabs(v->z_pos - tcc->v->z_pos) > 6 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2188
			 myabs(v->x_pos - tcc->v->x_pos) >= 6 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2189
			 myabs(v->y_pos - tcc->v->y_pos) >= 6)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2190
				return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2191
	return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2192
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2193
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2194
static void SetVehicleCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2195
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2196
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2197
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2198
	if (v->u.rail.crash_anim_pos != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2199
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2200
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2201
	v->u.rail.crash_anim_pos++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2202
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2203
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2204
	BEGIN_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2205
		v->vehstatus |= VS_CRASHED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2206
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2207
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2208
	InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2209
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2210
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2211
static int CountPassengersInTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2212
{
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2213
	int num = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2214
	BEGIN_ENUM_WAGONS(v)
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2215
		if (v->cargo_type == CT_PASSENGERS) num += v->cargo_count;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2216
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2217
	return num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2218
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2219
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2220
/*
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2221
 * Checks whether the specified tried has a collision with another vehicle. If
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2222
 * so, destroys this vehicle, and the other vehicle if its subtype is 0 (TS_Front_Engine).
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2223
 * Reports the incident in a flashy news item, modifies station ratings and
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2224
 * plays a sound.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2225
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2226
static void CheckTrainCollision(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2227
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2228
	TrainCollideChecker tcc;
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2229
	Vehicle *coll,*realcoll;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2230
	int num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2231
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2232
	/* can't collide in depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2233
	if (v->u.rail.track == 0x80)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2234
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2235
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2236
	if ( !(v->u.rail.track == 0x40) )
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2237
		assert((uint)TILE_FROM_XY(v->x_pos, v->y_pos) == v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2239
	tcc.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2240
	tcc.v_skip = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2241
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2242
	/* find colliding vehicle */
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2243
	realcoll = coll = VehicleFromPos(TILE_FROM_XY(v->x_pos, v->y_pos), &tcc, (VehicleFromPosProc*)FindTrainCollideEnum);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2244
	if (coll == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2245
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2246
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2247
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2248
	coll = GetFirstVehicleInChain(coll);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2249
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2250
	/* it can't collide with its own wagons */
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2251
	if ( (v == coll) || ( (v->u.rail.track & 0x40) && ( (v->direction & 2) != (realcoll->direction & 2) ) ) )
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2252
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2253
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2254
	//two drivers + passangers killed in train v
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2255
	num = 2 + CountPassengersInTrain(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2256
	if(!(coll->vehstatus&VS_CRASHED))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2257
		//two drivers + passangers killed in train coll (if it was not crashed already)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2258
		num += 2 + CountPassengersInTrain(coll);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2260
	SetVehicleCrashed(v);
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2261
	if (coll->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2262
		SetVehicleCrashed(coll);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2263
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2264
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2265
	SetDParam(0, num);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2266
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2267
	AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2268
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT|NF_VEHICLE, NT_ACCIDENT, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2269
		v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2270
		0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2271
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2272
	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2273
	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2274
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2275
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2276
static void *CheckVehicleAtSignal(Vehicle *v, void *data)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2277
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2278
	uint32 d = (uint32)data;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2279
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2280
	if (v->type == VEH_Train && v->subtype == TS_Front_Engine && v->tile == (TileIndex)(d >> 8)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2281
		byte diff = (v->direction - (byte)d + 2) & 7;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2282
		if (diff == 2 || (v->cur_speed <= 5 && diff <= 4))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2283
			return (void*)1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2284
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2285
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2286
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2287
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2288
static void TrainController(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2289
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2290
	Vehicle *prev = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2291
	GetNewVehiclePosResult gp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2292
	uint32 r, tracks,ts;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2293
	int dir, i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2294
	byte chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2295
	byte chosen_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2296
	byte old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2297
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2298
	/* For every vehicle after and including the given vehicle */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2299
	for(;;) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2300
		BeginVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2301
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2302
		if (v->u.rail.track != 0x40) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2303
			/* Not inside tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2304
			if (GetNewVehiclePos(v, &gp)) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2305
				/* Staying in the old tile */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2306
				if (v->u.rail.track == 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
					/* inside depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2308
					gp.x = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2309
					gp.y = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2310
				} else {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2311
					/* is not inside depot */
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2312
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2313
					if (!TrainCheckIfLineEnds(v))
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2314
						return;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2315
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2316
					r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2317
					if (r & 0x8)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2318
						goto invalid_rail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2319
					if (r & 0x2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2320
						TrainEnterStation(v, r >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2321
						return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2322
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2323
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2324
					if (v->current_order.type == OT_LEAVESTATION) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2325
						v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2326
						v->current_order.flags = 0;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2327
						InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2328
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2329
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2330
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2331
				/* A new tile is about to be entered. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2332
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2333
				/* Determine what direction we're entering the new tile from */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2334
				dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2335
				assert(dir==1 || dir==3 || dir==5 || dir==7);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2336
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2337
				/* Get the status of the tracks in the new tile and mask
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2338
				 * away the bits that aren't reachable. */
159
139cf78bfb28 (svn r160) -Codechange: made GetTileTrackStatus more readable (blathijs)
truelight
parents: 156
diff changeset
  2339
				ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[dir >> 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2340
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2341
				/* Combine the from & to directions.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2342
				 * Now, the lower byte contains the track status, and the byte at bit 16 contains
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2343
				 * the signal status. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2344
				tracks = ts|(ts >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2345
				if ( (byte) tracks == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2346
					goto invalid_rail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2347
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2348
				/* Check if the new tile contrains tracks that are compatible
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2349
				 * with the current train, if not, bail out. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2350
				if (!CheckCompatibleRail(v, gp.new_tile))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2351
					goto invalid_rail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2352
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2353
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2354
					/* Currently the locomotive is active. Determine which one of the
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2355
					 * available tracks to choose */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2356
					chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, dir>>1, (byte)tracks);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2357
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2358
					/* Check if it's a red signal and that force proceed is not clicked. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2359
					if ( (tracks>>16)&chosen_track && v->u.rail.force_proceed == 0) goto red_light;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2360
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2361
					static byte _matching_tracks[8] = {0x30, 1, 0xC, 2, 0x30, 1, 0xC, 2};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2362
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2363
					/* The wagon is active, simply follow the prev vehicle. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2364
					chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & tracks);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2365
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2366
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2367
				/* make sure chosen track is a valid track */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2368
				assert(chosen_track==1 || chosen_track==2 || chosen_track==4 || chosen_track==8 || chosen_track==16 || chosen_track==32);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2369
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2370
				/* Update XY to reflect the entrance to the new tile, and select the direction to use */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2371
				{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2372
					const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][dir>>1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2373
					gp.x = (gp.x & ~0xF) | b[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2374
					gp.y = (gp.y & ~0xF) | b[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2375
					chosen_dir = b[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2376
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2377
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2378
				/* Call the landscape function and tell it that the vehicle entered the tile */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2379
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2380
				if (r&0x8)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2381
					goto invalid_rail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2382
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2383
				if (v->subtype == TS_Front_Engine) v->load_unload_time_rem = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2384
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2385
				if (!(r&0x4)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2386
					v->tile = gp.new_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
					v->u.rail.track = chosen_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2388
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2389
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2390
				if (v->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2391
					TrainMovedChangeSignals(gp.new_tile, dir>>1);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2392
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2393
				/* Signals can only change when the first
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2394
				 * (above) or the last vehicle moves. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2395
				if (v->next == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2396
					TrainMovedChangeSignals(gp.old_tile, (dir>>1) ^ 2);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2397
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2398
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2399
					AffectSpeedByDirChange(v, chosen_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2400
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2401
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2402
				v->direction = chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2403
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2404
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2405
			/* in tunnel */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2406
			GetNewVehiclePos(v, &gp);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2407
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  2408
			if (IsTileType(gp.new_tile, MP_TUNNELBRIDGE) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2409
					!(_map5[gp.new_tile] & 0xF0)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2410
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2411
				if (r & 0x4) goto common;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2412
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2413
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2414
			v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2415
			v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2416
			VehiclePositionChanged(v);
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2417
			if (prev == NULL)
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2418
				CheckTrainCollision(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2419
			goto next_vehicle;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2420
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2421
common:;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2422
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2423
		/* update image of train, as well as delta XY */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2424
		dir = GetNewVehicleDirection(v, gp.x, gp.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2425
		UpdateTrainDeltaXY(v, dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2426
		v->cur_image = GetTrainImage(v, dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2427
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2428
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2429
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2430
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2431
		/* update the Z position of the vehicle */
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2432
		old_z = AfterSetTrainPos(v, (gp.new_tile != gp.old_tile));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2433
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2434
		if (prev == NULL) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2435
			/* This is the first vehicle in the train */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2436
			AffectSpeedByZChange(v, old_z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2437
			CheckTrainCollision(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2438
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2439
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2440
next_vehicle:;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2441
		/* continue with next vehicle */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2442
		prev = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2443
		if ((v=v->next) == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2444
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2445
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2446
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2447
invalid_rail:
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2448
	/* We've reached end of line?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2449
	if (prev != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2450
		error("!Disconnecting train");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2451
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2452
	goto reverse_train_direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2453
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2454
red_light: {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2455
	/* We're in front of a red signal ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2456
		/* find the first set bit in ts. need to do it in 2 steps, since
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2457
		 * FIND_FIRST_BIT only handles 6 bits at a time. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2458
		i = FindFirstBit2x64(ts);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2459
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2460
		if (!(_map3_lo[gp.new_tile] & _signal_otherdir[i])) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2461
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2462
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2463
			v->progress = 255-100;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2464
			if (++v->load_unload_time_rem < _patches.wait_oneway_signal * 20)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2465
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2466
		} else if (_map3_lo[gp.new_tile] & _signal_onedir[i]){
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2467
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2468
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2469
			v->progress = 255-10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2470
			if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) {
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 889
diff changeset
  2471
				uint o_tile = gp.new_tile + TileOffsByDir(dir >> 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2472
				/* check if a train is waiting on the other side */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2473
				if (VehicleFromPos(o_tile, (void*)( (o_tile<<8) | (dir^4)), (VehicleFromPosProc*)CheckVehicleAtSignal) == NULL)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2474
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2475
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2476
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2477
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2478
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2479
reverse_train_direction:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2480
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2481
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2482
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2483
	ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2484
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2485
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2486
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2487
extern uint CheckTunnelBusy(uint tile, int *length);
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2488
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2489
static void DeleteLastWagon(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2490
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2491
	Vehicle *u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2492
	int t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2493
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2494
	while (v->next != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2495
		u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2496
		v = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2497
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2498
	u->next = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2499
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2500
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2501
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
  2502
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2503
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2504
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2505
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2506
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2507
	DeleteVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2508
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2509
	if (!((t=v->u.rail.track) & 0xC0)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2510
		SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(t));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2511
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2512
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  2513
	/* Check if the wagon was on a road/rail-crossing and disable it if no others are on it */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  2514
	DisableTrainCrossing(v->tile);
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  2515
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2516
	if (v->u.rail.track == 0x40) {
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2517
		int length;
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2518
		TileIndex endtile = CheckTunnelBusy(v->tile, &length);
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2519
		if ((v->direction == 1) || (v->direction == 5) )
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2520
			SetSignalsOnBothDir(v->tile,0);
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2521
			SetSignalsOnBothDir(endtile,0);
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2522
		if ((v->direction == 3) || (v->direction == 7) )
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2523
			SetSignalsOnBothDir(v->tile,1);
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2524
			SetSignalsOnBothDir(endtile,1);
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2525
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2526
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2527
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2528
static void ChangeTrainDirRandomly(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2529
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2530
	static int8 _random_dir_change[4] = { -1, 0, 0, 1};
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2531
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2532
	do {
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2533
		//I need to buffer the train direction
288
72ca19a694dd (svn r294) -Fix: autorail always builds rail, instead of occasional rail removal (on pressing hotkey after pressing 'bulldozer')
darkvater
parents: 244
diff changeset
  2534
		if (!(v->u.rail.track & 0x40))
1137
bce056e230ce (svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents: 1132
diff changeset
  2535
			v->direction = (v->direction + _random_dir_change[Random()&3]) & 7;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2536
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2537
			BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2538
			UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2539
			v->cur_image = GetTrainImage(v, v->direction);
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2540
			AfterSetTrainPos(v, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2541
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2542
	} while ( (v=v->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2543
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2544
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2545
static void HandleCrashedTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2546
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2547
	int state = ++v->u.rail.crash_anim_pos, index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2548
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2549
	Vehicle *u;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2550
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  2551
	if ( (state == 4) && (v->u.rail.track != 0x40) ) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2552
		CreateEffectVehicleRel(v, 4, 4, 8, EV_CRASHED_SMOKE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2553
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2554
1137
bce056e230ce (svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents: 1132
diff changeset
  2555
	if (state <= 200 && (uint16)(r=Random()) <= 0x2492) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2556
		index = (r * 10 >> 16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2557
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2558
		u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2559
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2560
			if (--index < 0) {
1137
bce056e230ce (svn r1638) -Fix: Train crashes should no longer desync the game. This is more of a
celestar
parents: 1132
diff changeset
  2561
				r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2562
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2563
				CreateEffectVehicleRel(u,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2564
					2 + ((r>>8)&7),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2565
					2 + ((r>>16)&7),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2566
					5 + (r&7),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2567
					EV_DEMOLISH);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2568
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2569
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2570
		} while ( (u=u->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2571
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2572
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2573
	if (state <= 240 && !(v->tick_counter&3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2574
		ChangeTrainDirRandomly(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2575
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2576
1128
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
  2577
	if (state >= 4440 && !(v->tick_counter&0x1F)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2578
		DeleteLastWagon(v);
1128
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
  2579
		InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
ca7f860db7ac (svn r1629) added a counter to tell how many engines you have of each type to the autoreplace vehicle windows and made them show only the vehicles you actually have in the left list.
bjarni
parents: 1109
diff changeset
  2580
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2581
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2582
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2583
static void HandleBrokenTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2584
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2585
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2586
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2587
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2588
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2589
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2590
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2591
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2592
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2593
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2594
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2595
		SndPlayVehicleFx((_opt.landscape != LT_CANDY) ?
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2596
			SND_10_TRAIN_BREAKDOWN : SND_3A_COMEDY_BREAKDOWN_2, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2597
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2598
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2599
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2600
			if (u)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2601
				u->u.special.unk0 = v->breakdown_delay * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2602
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2603
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2604
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2605
	if (!(v->tick_counter & 3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2606
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2607
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2608
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2609
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2610
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2611
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2612
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2613
static const byte _breakdown_speeds[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2614
	225, 210, 195, 180, 165, 150, 135, 120, 105, 90, 75, 60, 45, 30, 15, 15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2615
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2616
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2617
static bool TrainCheckIfLineEnds(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2618
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2619
	uint tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2620
	uint x,y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2621
	int t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2622
	uint32 ts;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2623
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2624
	if ((uint)(t=v->breakdown_ctr) > 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2625
		v->vehstatus |= VS_TRAIN_SLOWING;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2626
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2627
		t = _breakdown_speeds[ ((~t) >> 4) & 0xF];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2628
		if ((uint16)t <= v->cur_speed)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2629
			v->cur_speed = t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2630
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2631
		v->vehstatus &= ~VS_TRAIN_SLOWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2632
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2633
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2634
	// exit if inside a tunnel
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2635
	if (v->u.rail.track & 0x40)
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2636
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2637
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2638
	tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2639
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2640
	// tunnel entrance?
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  2641
	if (IsTileType(tile, MP_TUNNELBRIDGE) &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2642
			(_map5[tile] & 0xF0) == 0 && (byte)((_map5[tile] & 3)*2+1) == v->direction)
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2643
				return true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2644
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2645
	// depot?
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2646
	/* XXX -- When enabled, this makes it possible to crash trains of others
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2647
	     (by building a depot right against a station) */
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  2648
/*	if (IsTileType(tile, MP_RAILWAY) && (_map5[tile] & 0xFC) == 0xC0)
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2649
		return true;*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2650
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2651
	/* Determine the non-diagonal direction in which we will exit this tile */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2652
	t = v->direction >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2653
	if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[t]) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2654
		t = (t - 1) & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2655
	}
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2656
	/* Calculate next tile */
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 889
diff changeset
  2657
	tile += TileOffsByDir(t);
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2658
	// determine the track status on the next tile.
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2659
	ts = GetTileTrackStatus(tile, TRANSPORT_RAIL) & _reachable_tracks[t];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2660
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2661
	/* Calc position within the current tile ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2662
	x = v->x_pos & 0xF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2663
	y = v->y_pos & 0xF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2664
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2665
	switch(v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2666
	case 0:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2667
		x = (~x) + (~y) + 24;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2668
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2669
	case 7:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2670
		x = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2671
		/* fall through */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2672
	case 1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2673
		x = (~x) + 16;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2674
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2675
	case 2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2676
		x = (~x) + y + 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2677
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2678
	case 3:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2679
		x = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2680
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2681
	case 4:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2682
		x = x + y - 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2683
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2684
	case 6:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2685
		x = (~y) + x + 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2686
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2687
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2688
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2689
	if ( (uint16)ts != 0) {
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2690
		/* If we approach a rail-piece which we can't enter, don't enter it! */
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2691
		if (x + 4 > 15 && !CheckCompatibleRail(v, tile)) {
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2692
			v->cur_speed = 0;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2693
			ReverseTrainDirection(v);
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2694
			return false;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2695
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2696
		if ((ts &= (ts >> 16)) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2697
			// make a rail/road crossing red
1035
812f837ee03f (svn r1536) Move GET_TILEHEIGHT, GET_TILETYPE and IS_TILETYPE to map.h, turn them into inline functions and add some asserts
tron
parents: 1024
diff changeset
  2698
			if (IsTileType(tile, MP_STREET) && (_map5[tile] & 0xF0) == 0x10) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2699
				if (!(_map5[tile] & 4)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2700
					_map5[tile] |= 4;
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2701
					SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2702
					MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2703
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2704
			}
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2705
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2706
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2707
	} else if (x + 4 > 15) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2708
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2709
		ReverseTrainDirection(v);
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2710
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2711
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2712
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2713
	// slow down
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2714
	v->vehstatus |= VS_TRAIN_SLOWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2715
	t = _breakdown_speeds[x & 0xF];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2716
	if (!(v->direction&1)) t>>=1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2717
	if ((uint16)t < v->cur_speed)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2718
		v->cur_speed = t;
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2719
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2720
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2721
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2722
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2723
static void TrainLocoHandler(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2724
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2725
	int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2726
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2727
	/* train has crashed? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2728
	if (v->u.rail.crash_anim_pos != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2729
		if (!mode) HandleCrashedTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2730
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2731
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2732
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2733
	if (v->u.rail.force_proceed != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2734
		v->u.rail.force_proceed--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2735
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2736
	/* train is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2737
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2738
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2739
			HandleBrokenTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2740
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2741
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2742
		v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2743
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2744
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2745
	if (HASBIT(v->u.rail.flags, VRF_REVERSING) && v->cur_speed == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2746
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2747
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2748
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2749
	/* exit if train is stopped */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2750
	if (v->vehstatus & VS_STOPPED && v->cur_speed == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2751
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2752
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2753
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2754
	if (ProcessTrainOrder(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2755
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2756
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2757
		v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2758
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2759
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2760
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2761
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2762
	HandleTrainLoading(v, mode);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2763
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2764
	if (v->current_order.type == OT_LOADING)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2765
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2766
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2767
	if (CheckTrainStayInDepot(v))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2768
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2769
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2770
	if (!mode) HandleLocomotiveSmokeCloud(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2771
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2772
	j = UpdateTrainSpeed(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2773
	if (j == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2774
		// if the vehicle has speed 0, update the last_speed field.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2775
		if (v->cur_speed != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2776
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2777
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2778
		TrainCheckIfLineEnds(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2779
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2780
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2781
			TrainController(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2782
			if (v->cur_speed <= 0x100)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2783
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2784
		} while (--j != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2785
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2786
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2787
	SetLastSpeed(v, v->cur_speed);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2788
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2789
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2790
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2791
void Train_Tick(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2792
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2793
	if (_age_cargo_skip_counter == 0 && v->cargo_days != 0xff)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2794
		v->cargo_days++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2795
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2796
	v->tick_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2797
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2798
	if (v->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2799
		TrainLocoHandler(v, false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2800
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2801
		// make sure vehicle wasn't deleted.
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2802
		if (v->type == VEH_Train && v->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2803
			TrainLocoHandler(v, true);
1132
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  2804
	} else if (v->subtype == TS_Free_Car && HASBITS(v->vehstatus, VS_CRASHED)) {
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  2805
		// Delete flooded standalone wagon
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  2806
		if (++v->u.rail.crash_anim_pos >= 4400)
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  2807
			DeleteVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2808
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2809
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2810
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2811
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2812
static const byte _depot_track_ind[4] = {0,1,0,1};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2813
715
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2814
// Validation for the news item "Train is waiting in depot"
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
  2815
static bool ValidateTrainInDepot( uint data_a, uint data_b )
715
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2816
{
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  2817
	Vehicle *v = GetVehicle(data_a);
1064
2242cd7419bb (svn r1565) -Fix: [ 1104969 ] Aircraft in hangar messages are now revalidated before
celestar
parents: 1060
diff changeset
  2818
	return  (v->u.rail.track == 0x80 && (v->vehstatus | VS_STOPPED));
715
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2819
}
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2820
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2821
void TrainEnterDepot(Vehicle *v, uint tile)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2822
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2823
	SetSignalsOnBothDir(tile, _depot_track_ind[_map5[tile]&3]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2824
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2825
	if (v->subtype != TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2826
		v = GetFirstVehicleInChain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2827
578
1e66514eb621 (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  2828
	VehicleServiceInDepot(v);
1e66514eb621 (svn r998) now vehicles are serviced both when entering and when leaving depots to prevent that vehicles might need service when leaving after a long stay (ln--)
bjarni
parents: 555
diff changeset
  2829
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2830
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2831
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2832
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2833
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2834
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  2835
	MaybeReplaceVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2836
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 410
diff changeset
  2837
	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 410
diff changeset
  2838
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2839
	if (v->current_order.type == OT_GOTO_DEPOT) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2840
		Order t;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2841
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2842
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2843
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2844
		t = v->current_order;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2845
		v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2846
		v->current_order.flags = 0;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2847
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2848
		if (t.flags & OF_UNLOAD) { // Part of the orderlist?
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2849
			v->u.rail.days_since_order_progr = 0;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2850
			v->cur_order_index++;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2851
		} else if (t.flags & OF_FULL_LOAD) { // User initiated?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2852
			v->vehstatus |= VS_STOPPED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2853
			if (v->owner == _local_player) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2854
				SetDParam(0, v->unitnumber);
715
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2855
				AddValidatedNewsItem(
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2856
					STR_8814_TRAIN_IS_WAITING_IN_DEPOT,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2857
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2858
					v->index,
715
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2859
					0,
28dd55cc55ae (svn r1167) Feature: Added the possibility to add validation functions to NewsItems. This is now done for "Train in depot" messages. Before displaying such a message, it checks if the train really still is in the depot. Can be applied to other news items as well.
dominik
parents: 679
diff changeset
  2860
					ValidateTrainInDepot);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2861
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2862
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2863
	}
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2864
	InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2865
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2866
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2867
static void CheckIfTrainNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2868
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2869
	byte depot;
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2870
	TrainFindDepotData tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2871
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  2872
	if (_patches.servint_trains == 0 && !v->set_for_replacement)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2873
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2874
1208
11d5223ecbcc (svn r1712) - Fix: a hacked client can no longer send other players trains to a depot
bjarni
parents: 1197
diff changeset
  2875
	if (!VehicleNeedsService(v) && !v->set_for_replacement)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2876
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2877
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2878
	if (v->vehstatus & VS_STOPPED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2879
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2880
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2881
	if (_patches.gotodepot && VehicleHasDepotOrders(v))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2882
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2883
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2884
	// Don't interfere with a depot visit scheduled by the user, or a
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2885
	// depot visit by the order list.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2886
	if (v->current_order.type == OT_GOTO_DEPOT &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2887
			(v->current_order.flags & (OF_FULL_LOAD | OF_UNLOAD)) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2888
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2889
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2890
	tfdd = FindClosestTrainDepot(v);
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2891
	/* Only go to the depot if it is not too far out of our way. */
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2892
	if (tfdd.best_length == (uint)-1 || tfdd.best_length > 16 ) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2893
		if (v->current_order.type == OT_GOTO_DEPOT) {
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2894
			/* If we were already heading for a depot but it has
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2895
			 * suddenly moved farther away, we continue our normal
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2896
			 * schedule? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2897
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2898
			v->current_order.flags = 0;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2899
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2900
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2901
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2902
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2903
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2904
	depot = GetDepotByTile(tfdd.tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2905
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2906
	if (v->current_order.type == OT_GOTO_DEPOT &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2907
			v->current_order.station != depot &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2908
			!CHANCE16(3,16))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2909
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2910
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2911
	v->current_order.type = OT_GOTO_DEPOT;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2912
	v->current_order.flags = OF_NON_STOP;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2913
	v->current_order.station = depot;
308
8088f72d10f5 (svn r314) -Fix: [982611] Pathfinding bug; train likes the roundabout. If train needs servicing it will now look 16 tiles along the track instead of 12 tiles manhattan style (blathijs)
darkvater
parents: 300
diff changeset
  2914
	v->dest_tile = tfdd.tile;
755
80091de50044 (svn r1211) -Feature: Introduce sticky windows to all vehicle windows...Just make sure you do not sticky more than 23 at a time. Also a lot of places in the code invalidated the 'status bar' of the vehicle, referring to it as widget 4. This is now widget 5 and has been #definitized
darkvater
parents: 744
diff changeset
  2915
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2916
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2917
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2918
int32 GetTrainRunningCost(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2919
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2920
	int32 cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2921
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2922
	do {
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  2923
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2924
		if (rvi->running_cost_base)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2925
			cost += rvi->running_cost_base * _price.running_rail[rvi->engclass];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2926
	} while ( (v=v->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2927
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2928
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2929
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2930
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2931
void OnNewDay_Train(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2932
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2933
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2934
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2935
	if ((++v->day_counter & 7) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2936
		DecreaseVehicleValue(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2937
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2938
	if (v->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2939
		CheckVehicleBreakdown(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2940
		AgeVehicle(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2941
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2942
		CheckIfTrainNeedsService(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2943
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2944
		// check if train hasn't advanced in its order list for a set number of days
1057
df5d21eec8bb (svn r1558) -Fix: [ 1104350 ] Crashed trains do not initiate the lost vehicle
Celestar
parents: 1053
diff changeset
  2945
		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) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2946
			v->u.rail.days_since_order_progr = 0;
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2947
			SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2948
			AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2949
				STR_TRAIN_IS_LOST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2950
				NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2951
				v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2952
				0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2953
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2954
1053
8d90844ddc2e (svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents: 1049
diff changeset
  2955
		CheckOrders(v->index, OC_INIT);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2956
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2957
		/* update destination */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2958
		if (v->current_order.type == OT_GOTO_STATION &&
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  2959
				(tile = GetStation(v->current_order.station)->train_tile) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2960
					v->dest_tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2961
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2962
		if ((v->vehstatus & VS_STOPPED) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2963
			/* running costs */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2964
			int32 cost = GetTrainRunningCost(v) / 364;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2965
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2966
			v->profit_this_year -= cost >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2967
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2968
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2969
			SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2970
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2971
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
1151
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  2972
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2973
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2974
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2975
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2976
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1087
diff changeset
  2977
void TrainsYearlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2978
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2979
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2980
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2981
	FOR_ALL_VEHICLES(v) {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2982
		if (v->type == VEH_Train && v->subtype == TS_Front_Engine) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2983
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2984
			// show warning if train is not generating enough income last 2 years (corresponds to a red icon in the vehicle list)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2985
			if (_patches.train_income_warn && v->owner == _local_player && v->age >= 730 && v->profit_this_year < 0) {
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2986
				SetDParam(1, v->profit_this_year);
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2987
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2988
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2989
					STR_TRAIN_IS_UNPROFITABLE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2990
					NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2991
					v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2992
					0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2993
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2994
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2995
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2996
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2997
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2998
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2999
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3000
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3001
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3002
extern void ShowTrainViewWindow(Vehicle *v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3003
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3004
void HandleClickOnTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3005
{
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3006
	if (v->subtype != TS_Front_Engine) v = GetFirstVehicleInChain(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3007
	ShowTrainViewWindow(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3008
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3009
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1087
diff changeset
  3010
void InitializeTrains(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3011
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3012
	_age_cargo_skip_counter = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3013
}