train_cmd.c
author tron
Mon, 14 Nov 2005 08:09:57 +0000
changeset 2635 7ed07303448d
parent 2634 0df9396b0067
child 2637 722dae65c4a8
permissions -rw-r--r--
(svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2182
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     4
#include "openttd.h"
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
     5
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2150
diff changeset
     6
#include "functions.h"
2561
233dafd7cf6c (svn r3098) static, const, uint -> TileIndex, indentation, bracing, unused return values, ... mostly related to the clone vehicle GUI
tron
parents: 2552
diff changeset
     7
#include "gui.h"
507
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 495
diff changeset
     8
#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
     9
#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
    10
#include "tile.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "pathfind.h"
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
    14
#include "npf.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "station.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "table/train_cmd.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
#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
    20
#include "sound.h"
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
    21
#include "depot.h"
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
    22
#include "debug.h"
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1531
diff changeset
    23
#include "waypoint.h"
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
    24
#include "vehicle_gui.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
    26
#define IS_FIRSTHEAD_SPRITE(spritenum) \
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
    27
	(is_custom_sprite(spritenum) ? IS_CUSTOM_FIRSTHEAD_SPRITE(spritenum) : _engine_sprite_add[spritenum] == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
    29
static bool TrainCheckIfLineEnds(Vehicle *v);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
    30
static void TrainController(Vehicle *v);
0
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
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
    33
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
    34
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
    35
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    36
/**
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    37
 * Recalculates the cached weight of a train and its vehicles. Should be called each time the cargo on
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    38
 * the consist changes.
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    39
 * @param v First vehicle of the consist.
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    40
 */
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    41
void TrainCargoChanged(Vehicle *v) {
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    42
	Vehicle *u;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    43
	uint16 weight = 0;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    44
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    45
	for (u = v; u != NULL; u = u->next) {
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    46
		const RailVehicleInfo *rvi = RailVehInfo(u->engine_type);
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    47
		uint16 vweight = 0;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    48
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    49
		vweight += (_cargoc.weights[u->cargo_type] * u->cargo_count) / 16;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    50
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    51
		// Vehicle weight is not added for articulated parts.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    52
		if (u->subtype != TS_Artic_Part) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    53
			// vehicle weight is the sum of the weight of the vehicle and the weight of its cargo
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    54
			vweight += rvi->weight;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    55
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    56
			// powered wagons have extra weight added
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    57
			if (HASBIT(u->u.rail.flags, VRF_POWEREDWAGON))
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    58
				vweight += RailVehInfo(v->engine_type)->pow_wag_weight;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
    59
		}
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    60
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    61
		// consist weight is the sum of the weight of all vehicles in the consist
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    62
		weight += vweight;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    63
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    64
		// store vehicle weight in cache
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    65
		u->u.rail.cached_veh_weight = vweight;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    66
	};
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    67
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    68
	// store consist weight in cache
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    69
	v->u.rail.cached_weight = weight;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    70
}
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    71
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    72
/**
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    73
 * Recalculates the cached stuff of a train. Should be called each time a vehicle is added
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    74
 * to/removed from the chain, and when the game is loaded.
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    75
 * Note: this needs to be called too for 'wagon chains' (in the depot, without an engine)
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    76
 * @param v First vehicle of the chain.
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    77
 */
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    78
void TrainConsistChanged(Vehicle *v) {
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    79
	const RailVehicleInfo *rvi_v;
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    80
	Vehicle *u;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    81
	uint16 max_speed = 0xFFFF;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    82
	uint32 power = 0;
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    83
	EngineID first_engine;
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    84
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    85
	assert(v->type == VEH_Train);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    86
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    87
	assert(v->subtype == TS_Front_Engine || v->subtype == TS_Free_Car);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    88
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    89
	rvi_v = RailVehInfo(v->engine_type);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    90
	first_engine = (v->subtype == TS_Front_Engine) ? v->engine_type : INVALID_VEHICLE;
2587
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2574
diff changeset
    91
	v->u.rail.cached_total_length = 0;
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    92
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
    93
	for (u = v; u != NULL; u = u->next) {
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
    94
		const RailVehicleInfo *rvi_u = RailVehInfo(u->engine_type);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
    95
		uint16 veh_len;
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
    96
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    97
		// update the 'first engine'
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    98
		u->u.rail.first_engine = (v == u) ? INVALID_VEHICLE : first_engine;
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
    99
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   100
		if (rvi_u->visual_effect != 0) {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   101
			u->u.rail.cached_vis_effect = rvi_u->visual_effect;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   102
		} else {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   103
			if (rvi_u->flags & RVI_WAGON || u->subtype == TS_Artic_Part) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   104
				// Wagons and articulated parts have no effect by default
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   105
				u->u.rail.cached_vis_effect = 0x40;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   106
			} else if (rvi_u->engclass == 0) {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   107
				// Steam is offset by -4 units
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   108
				u->u.rail.cached_vis_effect = 4;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   109
			} else {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   110
				// Diesel fumes and sparks come from the centre
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   111
				u->u.rail.cached_vis_effect = 8;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   112
			}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   113
		}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   114
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   115
		if (u->subtype != TS_Artic_Part) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   116
			// power is the sum of the powers of all engines and powered wagons in the consist
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   117
			power += rvi_u->power;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   118
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   119
			// check if its a powered wagon
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   120
			CLRBIT(u->u.rail.flags, VRF_POWEREDWAGON);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   121
			if ((rvi_v->pow_wag_power != 0) && (rvi_u->flags & RVI_WAGON) && UsesWagonOverride(u)) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   122
				if (HASBIT(rvi_u->callbackmask, CBM_WAGON_POWER)) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   123
					uint16 callback = GetCallBackResult(CBID_WAGON_POWER,  u->engine_type, u);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   124
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   125
					if (callback != CALLBACK_FAILED)
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   126
						u->u.rail.cached_vis_effect = callback;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   127
				}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   128
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   129
				if (u->u.rail.cached_vis_effect < 0x40) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   130
					/* wagon is powered */
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   131
					SETBIT(u->u.rail.flags, VRF_POWEREDWAGON); // cache 'powered' status
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   132
					power += rvi_v->pow_wag_power;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   133
				}
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   134
			}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
   135
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   136
			// max speed is the minimum of the speed limits of all vehicles in the consist
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   137
			if (!(rvi_u->flags & RVI_WAGON) || _patches.wagon_speed_limits)
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   138
				if (rvi_u->max_speed != 0 && !UsesWagonOverride(u))
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   139
					max_speed = min(rvi_u->max_speed, max_speed);
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   140
		}
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   141
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   142
		// check the vehicle length (callback)
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   143
		veh_len = CALLBACK_FAILED;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   144
		if (HASBIT(rvi_u->callbackmask, CBM_VEH_LENGTH))
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   145
			veh_len = GetCallBackResult(CBID_VEH_LENGTH,  u->engine_type, u);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   146
		if (veh_len == CALLBACK_FAILED)
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   147
			veh_len = rvi_u->shorten_factor;
1934
b27bc12822cd (svn r2440) - Fix: [newgrf] Fix the spritesorter to handle overlapping sprites properly, this fixes display problems with really short wagons. (algorithm by patchman, ported by therax)
hackykid
parents: 1927
diff changeset
   148
		veh_len = clamp(veh_len, 0, u->next == NULL ? 7 : 5); // the clamp on vehicles not the last in chain is stricter, as too short wagons can break the 'follow next vehicle' code
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   149
		u->u.rail.cached_veh_length = 8 - veh_len;
2587
31647a23da0b (svn r3124) Alter train loading/unloading time to use the actual length of the train instead of the number of wagons. The actual length is cached in the first vehicle of the train.
peter1138
parents: 2574
diff changeset
   150
		v->u.rail.cached_total_length += u->u.rail.cached_veh_length;
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
   151
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   152
	};
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   153
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   154
	// store consist weight/max speed in cache
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   155
	v->u.rail.cached_max_speed = max_speed;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   156
	v->u.rail.cached_power = power;
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   157
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   158
	// recalculate cached weights too (we do this *after* the rest, so it is known which wagons are powered and need extra weight added)
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1905
diff changeset
   159
	TrainCargoChanged(v);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   160
}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   161
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   162
/* These two arrays are used for realistic acceleration. XXX: How should they
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
   163
 * be interpreted? */
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   164
static const byte _curve_neighbours45[8][2] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   165
	{7, 1},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   166
	{0, 2},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   167
	{1, 3},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   168
	{2, 4},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   169
	{3, 5},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   170
	{4, 6},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   171
	{5, 7},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   172
	{6, 0},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   173
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   174
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   175
static const byte _curve_neighbours90[8][2] = {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   176
	{6, 2},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   177
	{7, 3},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   178
	{0, 4},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   179
	{1, 5},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   180
	{2, 6},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   181
	{3, 7},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   182
	{4, 0},
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   183
	{5, 1},
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
enum AccelType {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   187
	AM_ACCEL,
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   188
	AM_BRAKE
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   189
};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   190
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   191
static bool TrainShouldStop(const Vehicle* v, TileIndex tile)
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   192
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   193
	const Order* o = &v->current_order;
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   194
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   195
	assert(v->type == VEH_Train);
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   196
	assert(IsTileType(v->tile, MP_STATION));
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   197
	//When does a train drive through a station
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   198
	//first we deal with the "new nonstop handling"
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   199
	if (_patches.new_nonstop && o->flags & OF_NON_STOP &&
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
   200
			_m[tile].m2 == o->station )
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   201
		return false;
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   202
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
   203
	if (v->last_station_visited == _m[tile].m2)
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   204
		return false;
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   205
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
   206
	if (_m[tile].m2 != o->station &&
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   207
			(o->flags & OF_NON_STOP || _patches.new_nonstop))
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   208
		return false;
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   209
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   210
	return true;
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   211
}
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   212
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   213
//new acceleration
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   214
static int GetTrainAcceleration(Vehicle *v, bool mode)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   215
{
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   216
	const Vehicle *u;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   217
	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
   218
	int power = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   219
	int mass = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   220
	int max_speed = 2000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   221
	int area = 120;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   222
	int friction = 35; //[1e-3]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   223
	int drag_coeff = 20;	//[1e-4]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   224
	int incl = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   225
	int resistance;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   226
	int speed = v->cur_speed; //[mph]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   227
	int force = 0x3FFFFFFF;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   228
	int pos = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   229
	int lastpos = -1;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   230
	int curvecount[2] = {0, 0};
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   231
	int sum = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   232
	int numcurve = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   233
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   234
	speed *= 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   235
	speed /= 16;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   236
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   237
	//first find the curve speed limit
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   238
	for (u = v; u->next != NULL; u = u->next, pos++) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   239
		int dir = u->direction;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   240
		int ndir = u->next->direction;
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   241
		int i;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   242
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   243
		for (i = 0; i < 2; i++) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   244
			if ( _curve_neighbours45[dir][i] == ndir) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   245
				curvecount[i]++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   246
				if (lastpos != -1) {
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   247
					numcurve++;
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   248
					sum += pos - lastpos;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   249
					if (pos - lastpos == 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   250
						max_speed = 88;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   251
					}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   252
				}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   253
				lastpos = pos;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   254
			}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   255
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   256
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   257
		//if we have a 90 degree turn, fix the speed limit to 60
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   258
		if (_curve_neighbours90[dir][0] == ndir ||
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   259
				_curve_neighbours90[dir][1] == ndir) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   260
			max_speed = 61;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   261
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   262
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   263
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   264
	if (numcurve > 0) sum /= numcurve;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   265
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   266
	if ((curvecount[0] != 0 || curvecount[1] != 0) && max_speed > 88) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   267
		int total = curvecount[0] + curvecount[1];
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   268
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   269
		if (curvecount[0] == 1 && curvecount[1] == 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   270
			max_speed = 0xFFFF;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   271
		} else if (total > 1) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   272
			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
   273
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   274
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   275
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   276
	max_speed += (max_speed / 2) * v->u.rail.railtype;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   277
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   278
	if (IsTileType(v->tile, MP_STATION) && v->subtype == TS_Front_Engine) {
1236
928560eaf613 (svn r1740) -Fix: [ 1112342 ] Realistic acceleration works properly with TTDPatch non-stop handling behaviour
celestar
parents: 1235
diff changeset
   279
		if (TrainShouldStop(v, v->tile)) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   280
			int station_length = 0;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   281
			TileIndex tile = v->tile;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   282
			int delta_v;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   283
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   284
			max_speed = 120;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   285
			do {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   286
				station_length++;
1531
4ddd9437139b (svn r2035) - Remove unneeded realloc()
tron
parents: 1530
diff changeset
   287
				tile = TILE_ADD(tile, TileOffsByDir(v->direction / 2));
1685
46783dea9b44 (svn r2189) Introduce and use IsCompatibleTrainStationTile()
tron
parents: 1684
diff changeset
   288
			} while (IsCompatibleTrainStationTile(tile, v->tile));
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   289
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   290
			delta_v = v->cur_speed / (station_length + 1);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   291
			if (v->max_speed > (v->cur_speed - delta_v))
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   292
				max_speed = v->cur_speed - (delta_v / 10);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   293
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   294
			max_speed = max(max_speed, 25 * station_length);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   295
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   296
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   297
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   298
	mass = v->u.rail.cached_weight;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   299
	power = v->u.rail.cached_power * 746;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   300
	max_speed = min(max_speed, v->u.rail.cached_max_speed);
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   301
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   302
	for (u = v; u != NULL; u = u->next) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   303
		num++;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   304
		drag_coeff += 3;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   305
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   306
		if (u->u.rail.track == 0x80)
1882
f7d54d723d71 (svn r2388) - Fix: [realistic accel] Very slow trains no longer get an increase in maximum speed when part of them is in a depot.
hackykid
parents: 1859
diff changeset
   307
			max_speed = min(61, max_speed);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   308
1683
697f47e2bb59 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
   309
		if (HASBIT(u->u.rail.flags, VRF_GOINGUP)) {
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   310
			incl += u->u.rail.cached_veh_weight * 60;		//3% slope, quite a bit actually
1683
697f47e2bb59 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
   311
		} else if (HASBIT(u->u.rail.flags, VRF_GOINGDOWN)) {
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   312
			incl -= u->u.rail.cached_veh_weight * 60;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   313
		}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   314
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   315
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   316
	v->max_speed = max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   317
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   318
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   319
		resistance = 13 * mass / 10;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   320
		resistance += 60 * num;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   321
		resistance += friction * mass * speed / 1000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   322
		resistance += (area * drag_coeff * speed * speed) / 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   323
	} else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   324
		resistance = (area * (drag_coeff / 2) * speed * speed) / 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   325
	resistance += incl;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   326
	resistance *= 4; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   327
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   328
	if (speed > 0) {
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   329
		switch (v->u.rail.railtype) {
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   330
			case RAILTYPE_RAIL:
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   331
			case RAILTYPE_MONO:
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   332
				force = power / speed; //[N]
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   333
				force *= 22;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   334
				force /= 10;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   335
				break;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   336
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   337
			case RAILTYPE_MAGLEV:
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   338
				force = power / 25;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   339
				break;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   340
		}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   341
	} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   342
		//"kickoff" acceleration
1969
b7d014a74c0a (svn r2475) -Fix: [ 1214982 & 1217473 ] realistic acceleration problem. (glx)
celestar
parents: 1961
diff changeset
   343
		force = (mass * 8) + resistance;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   344
	}
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   345
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   346
	if (force <= 0) force = 10000;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   347
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
   348
	if (v->u.rail.railtype != RAILTYPE_MAGLEV) force = min(force, mass * 10 * 200);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   349
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   350
	if (mode == AM_ACCEL) {
1684
3b2df64cc706 (svn r2188) Revert r2185, because trains always accerlated (Requested by Celestar)
tron
parents: 1683
diff changeset
   351
		return (force - resistance) / (mass * 4);
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   352
	} else {
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   353
		return min((-force - resistance) / (mass * 4), 10000 / (mass * 4));
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   354
	}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   355
}
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
   356
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
void UpdateTrainAcceleration(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
{
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   359
	uint power = 0;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   360
	uint weight = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   362
	assert(v->subtype == TS_Front_Engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   363
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   364
	weight = v->u.rail.cached_weight;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   365
	power = v->u.rail.cached_power;
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   366
	v->max_speed = v->u.rail.cached_max_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   367
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   368
	assert(weight != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   370
	v->acceleration = clamp(power / weight * 4, 1, 255);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
1475
c21a6481b735 (svn r1979) Const correctness
tron
parents: 1472
diff changeset
   373
int GetTrainImage(const Vehicle *v, byte direction)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   375
	int img = v->spritenum;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
	int base;
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
	if (is_custom_sprite(img)) {
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   379
		base = GetCustomVehicleSprite(v, direction + 4 * IS_CUSTOM_SECONDHEAD_SPRITE(img));
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   380
		if (base != 0) return base;
2464
e3054f0f0627 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2463
diff changeset
   381
		img = orig_rail_vehicle_info[v->engine_type].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   382
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   383
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   384
	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
   385
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
	if (v->cargo_count >= (v->cargo_cap >> 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
		base += _wagon_full_adder[img];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
	return base;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
2570
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   391
extern int _traininfo_vehicle_pitch;
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   392
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   393
void DrawTrainEngine(int x, int y, EngineID engine, uint32 image_ormod)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
{
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   395
	const RailVehicleInfo *rvi = RailVehInfo(engine);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   396
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   397
	int img = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
	uint32 image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   400
	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
   401
		image = GetCustomVehicleIcon(engine, 6);
2570
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   402
		if (image == 0) {
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   403
			img = orig_rail_vehicle_info[engine].image_index;
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   404
		} else {
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   405
			y += _traininfo_vehicle_pitch;
f86c326fda65 (svn r3107) -NewGRF: Move train y-position offset adjustment to remove code duplication, and make it apply only to vehicles with a custom image.
peter1138
parents: 2564
diff changeset
   406
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
	}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   408
	if (image == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
		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
   410
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
	if (rvi->flags & RVI_MULTIHEAD) {
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   413
		DrawSprite(image | image_ormod, x - 14, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
		x += 15;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
		image = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
		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
   417
			image = GetCustomVehicleIcon(engine, 2);
2464
e3054f0f0627 (svn r2990) Remove _engine_original_sprites and replace all uses with references to our (new) original engine/vehicle tables.
peter1138
parents: 2463
diff changeset
   418
			if (image == 0) img = orig_rail_vehicle_info[engine].image_index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   419
		}
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   420
		if (image == 0) {
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   421
			image =
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   422
				((6 + _engine_sprite_add[img + 1]) & _engine_sprite_and[img + 1]) +
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   423
				_engine_sprite_base[img + 1];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
		}
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
	DrawSprite(image | image_ormod, x, y);
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
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   429
static uint CountArticulatedParts(const RailVehicleInfo *rvi, EngineID engine_type)
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   430
{
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   431
	uint16 callback;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   432
	uint i;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   433
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   434
	if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE))
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   435
		return 0;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   436
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   437
	for (i = 1; i < 10; i++) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   438
		callback = GetCallBackResult(CBID_ARTIC_ENGINE + (i << 8), engine_type, NULL);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   439
		if (callback == CALLBACK_FAILED || callback == 0xFF)
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   440
			break;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   441
	}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   442
2608
c98436848294 (svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents: 2607
diff changeset
   443
	return i - 1;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   444
}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   445
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   446
static void AddArticulatedParts(const RailVehicleInfo *rvi, Vehicle **vl)
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   447
{
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   448
	const RailVehicleInfo *rvi_artic;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   449
	EngineID engine_type;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   450
	Vehicle *v = vl[0];
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   451
	Vehicle *u = v;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   452
	uint16 callback;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   453
	bool flip_image;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   454
	uint i;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   455
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   456
	if (!HASBIT(rvi->callbackmask, CBM_ARTIC_ENGINE))
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   457
		return;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   458
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   459
	for (i = 1; i < 10; i++) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   460
		callback = GetCallBackResult(CBID_ARTIC_ENGINE + (i << 8), v->engine_type, NULL);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   461
		if (callback == CALLBACK_FAILED || callback == 0xFF)
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   462
			return;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   463
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   464
		u->next = vl[i];
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   465
		u = u->next;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   466
2608
c98436848294 (svn r3145) Fix two off-by-one errors in building articulated rail vehicles.
peter1138
parents: 2607
diff changeset
   467
		engine_type = GB(callback, 0, 7);
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   468
		flip_image = HASBIT(callback, 7);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   469
		rvi_artic = RailVehInfo(engine_type);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   470
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   471
		// get common values from first engine
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   472
		u->direction = v->direction;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   473
		u->owner = v->owner;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   474
		u->tile = v->tile;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   475
		u->x_pos = v->x_pos;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   476
		u->y_pos = v->y_pos;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   477
		u->z_pos = v->z_pos;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   478
		u->z_height = v->z_height;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   479
		u->u.rail.track = v->u.rail.track;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   480
		u->u.rail.railtype = v->u.rail.railtype;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   481
		u->build_year = v->build_year;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   482
		u->vehstatus = v->vehstatus & ~VS_STOPPED;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   483
		u->u.rail.first_engine = v->engine_type;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   484
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   485
		// get more settings from rail vehicle info
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   486
		u->spritenum = rvi_artic->image_index;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   487
		if (flip_image) u->spritenum++;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   488
		u->cargo_type = rvi_artic->cargo_type;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   489
		u->cargo_cap = rvi_artic->capacity;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   490
		u->max_speed = 0;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   491
		u->max_age = 0;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   492
		u->engine_type = engine_type;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   493
		u->value = 0;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   494
		u->type = VEH_Train;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   495
		u->subtype = TS_Artic_Part;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   496
		u->cur_image = 0xAC2;
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   497
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   498
		VehiclePositionChanged(u);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   499
	}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   500
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   501
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   502
static int32 CmdBuildRailWagon(EngineID engine, TileIndex tile, uint32 flags)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   503
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   504
	int32 value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   505
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   506
	const RailVehicleInfo *rvi;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   507
	const Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   508
	int x,y;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   509
	uint num_vehicles;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   510
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
   511
	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
   512
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   513
	rvi = RailVehInfo(engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
	value = (rvi->base_cost * _price.build_railwagon) >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   515
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   516
	num_vehicles = 1 + CountArticulatedParts(rvi, engine);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   517
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   518
	if (!(flags & DC_QUERY_COST)) {
2609
3a734c4531ff (svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents: 2608
diff changeset
   519
		Vehicle *vl[11]; // Allow for wagon and upto 10 artic parts.
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   520
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   521
		if (!AllocateVehicles(vl, num_vehicles))
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   522
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   523
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   524
		if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   525
			byte img = rvi->image_index;
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   526
			Vehicle *u, *w;
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   527
			uint dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   529
			v = vl[0];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   530
			v->spritenum = img;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   531
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   532
			u = NULL;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   533
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   534
			FOR_ALL_VEHICLES(w) {
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   535
				if (w->type == VEH_Train && w->tile == tile &&
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   536
				    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
   537
					u = GetLastVehicleInChain(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
					break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   539
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   541
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   542
			v->engine_type = engine;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   543
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   544
			dir = GB(_m[tile].m5, 0, 2);
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   545
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   546
			v->direction = dir * 2 + 1;
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   547
			v->tile = tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   548
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   549
			x = TileX(tile) * TILE_SIZE | _vehicle_initial_x_fract[dir];
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   550
			y = TileY(tile) * TILE_SIZE | _vehicle_initial_y_fract[dir];
0
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
			v->x_pos = x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
			v->y_pos = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
			v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   555
			v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   556
			v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
			v->u.rail.track = 0x80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   558
			v->vehstatus = VS_HIDDEN | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   560
			v->subtype = TS_Free_Car;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
			if (u != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   562
				u->next = v;
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   563
				v->subtype = TS_Not_First;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   564
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   566
			v->cargo_type = rvi->cargo_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
			v->value = value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
//			v->day_counter = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1925
diff changeset
   571
			e = GetEngine(engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
			v->u.rail.railtype = e->railtype;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   573
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
			v->build_year = _cur_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
			v->type = VEH_Train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
			v->cur_image = 0xAC2;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   577
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   578
			AddArticulatedParts(rvi, vl);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   579
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
			_new_wagon_id = v->index;
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   581
			_new_vehicle_id = v->index;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
			VehiclePositionChanged(v);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   584
			TrainConsistChanged(GetFirstVehicleInChain(v));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
			InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   590
	return value;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
// Move all free vehicles in the depot to the train
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   594
static void NormalizeTrainVehInDepot(const Vehicle* u)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   595
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   596
	const Vehicle* v;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   597
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
	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
   599
		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
   600
				v->tile == u->tile &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
				v->u.rail.track == 0x80) {
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   602
			if (DoCommandByTile(0, v->index | (u->index << 16), 1, DC_EXEC,
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   603
					CMD_MOVE_RAIL_VEHICLE) == CMD_ERROR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   604
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   605
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   606
	}
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
static const byte _railveh_score[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   610
	1, 4, 7, 19, 20, 30, 31, 19,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
	20, 21, 22, 10, 11, 30, 31, 32,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   612
	33, 34, 35, 29, 45, 32, 50, 40,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
	41, 51, 52, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
	0, 0, 0, 0, 0, 0, 60, 62,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   617
	63, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   618
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   619
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   620
	0, 0, 0, 0, 70, 71, 72, 73,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   621
	74, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
	0, 0, 0, 0, 0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   624
	0, 0, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   625
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
   628
static int32 EstimateTrainCost(const RailVehicleInfo* rvi)
0
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
	return (rvi->base_cost * (_price.build_railvehicle >> 3)) >> 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
1109
ecb98f43ba2c (svn r1610) Remove trailing whitespace (last time ever, i hope)
tron
parents: 1103
diff changeset
   633
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
   634
{
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   635
	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
   636
	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
   637
	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
   638
	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
   639
	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
   640
	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
   641
	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
   642
	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
   643
	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
   644
	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
   645
	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
   646
	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
   647
	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
   648
	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
   649
	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
   650
	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
   651
	u->build_year = v->build_year;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   652
	if (building) v->value >>= 1;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   653
	u->value = v->value;
1060
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   654
	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
   655
	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
   656
	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
   657
}
ae9a2238bd80 (svn r1561) Fix: autoreplacing a singleheaded engine into a dualheaded engine now adds the the rear engine
bjarni
parents: 1057
diff changeset
   658
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   659
/** Build a railroad vehicle.
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   660
 * @param x,y tile coordinates (depot) where rail-vehicle is built
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   661
 * @param p1 engine type id
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   662
 * @param p2 bit 0 build only one engine, even if it is a dualheaded engine.
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   663
          p2 bit 1 prevents any free cars from being added to the train
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
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
   666
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   667
	const RailVehicleInfo *rvi;
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   668
	int value;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   669
	Vehicle *v;
1282
ea2ae881814c (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   670
	UnitID unit_num;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   671
	Engine *e;
1980
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
   672
	TileIndex tile = TileVirtXY(x, y);
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   673
	uint num_vehicles;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   674
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   675
	/* Check if the engine-type is valid (for the player) */
1197
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
   676
	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
   677
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   678
	/* Check if the train is actually being built in a depot belonging
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   679
	 * to the player. Doesn't matter if only the cost is queried */
1443
2b8e1b123d91 (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1438
diff changeset
   680
	if (!(flags & DC_QUERY_COST)) {
2b8e1b123d91 (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1438
diff changeset
   681
		if (!IsTileDepotType(tile, TRANSPORT_RAIL)) return CMD_ERROR;
1901
03bf9bf99319 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1895
diff changeset
   682
		if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
1443
2b8e1b123d91 (svn r1947) As in r1946, permit DC_QUERY_COST even on non-depot tiles - so that it works for the ai_new. It is de iure not a bug yet but let's be safe against future annoying headaches. Signed-Off-By: TrueLight
pasky
parents: 1438
diff changeset
   683
	}
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
   684
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   685
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   686
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
   687
	rvi = RailVehInfo(p1);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   688
	e = GetEngine(p1);
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   689
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   690
	/* Check if depot and new engine uses the same kind of tracks */
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   691
	if (!IsCompatibleRail(e->railtype, GetRailType(tile))) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   692
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   693
	if (rvi->flags & RVI_WAGON) return CmdBuildRailWagon(p1, tile, flags);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   695
	value = EstimateTrainCost(rvi);
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2244
diff changeset
   696
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   697
	//make sure we only pay for half a dualheaded engine if we only requested half of it
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   698
	if (rvi->flags&RVI_MULTIHEAD && HASBIT(p2,0))
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   699
		value /= 2;
2261
d3554e5d3e86 (svn r2781) Fix some of the issues with variables in .h files.
ludde
parents: 2244
diff changeset
   700
2610
73831453cfbc (svn r3147) Fix crash when building dual-head rail vehicles...
peter1138
parents: 2609
diff changeset
   701
	num_vehicles = (rvi->flags & RVI_MULTIHEAD && !HASBIT(p2, 0)) ? 2 : 1;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   702
	num_vehicles += CountArticulatedParts(rvi, p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   703
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   704
	if (!(flags & DC_QUERY_COST)) {
2609
3a734c4531ff (svn r3146) Avoid use of variable size declared arrays when allocating articulated rail vehicles.
peter1138
parents: 2608
diff changeset
   705
		Vehicle *vl[12]; // Allow for upto 10 artic parts and dual-heads
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   706
		if (!AllocateVehicles(vl, num_vehicles) || IsOrderPoolFull())
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   707
			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
   708
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   709
		v = vl[0];
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   710
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   711
		unit_num = GetFreeUnitNumber(VEH_Train);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   712
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   713
			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
   714
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   715
		if (flags & DC_EXEC) {
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   716
			uint dir;
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   717
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
			v->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   719
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   720
			dir = GB(_m[tile].m5, 0, 2);
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   721
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
   722
			v->direction = dir * 2 + 1;
1986
fcc849a38ae6 (svn r2492) Remove some pointless casts and fix some nearby indentation
tron
parents: 1980
diff changeset
   723
			v->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   724
			v->owner = _current_player;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   725
			v->x_pos = (x |= _vehicle_initial_x_fract[dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   726
			v->y_pos = (y |= _vehicle_initial_y_fract[dir]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   727
			v->z_pos = GetSlopeZ(x,y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   728
			v->z_height = 6;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   729
			v->u.rail.track = 0x80;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   730
			v->vehstatus = VS_HIDDEN | VS_STOPPED | VS_DEFPAL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
			v->spritenum = rvi->image_index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   732
			v->cargo_type = rvi->cargo_type;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
			v->cargo_cap = rvi->capacity;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
			v->max_speed = rvi->max_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
			v->value = value;
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
   736
			v->last_station_visited = INVALID_STATION;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
			v->dest_tile = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   738
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   739
			v->engine_type = p1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   740
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   741
			v->reliability = e->reliability;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   742
			v->reliability_spd_dec = e->reliability_spd_dec;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   743
			v->max_age = e->lifelength * 366;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   744
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   745
			v->string_id = STR_SV_TRAIN_NAME;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
			v->u.rail.railtype = e->railtype;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
			_new_train_id = v->index;
2564
24533c2db978 (svn r3101) -Codechange: added _new_vehicle_id
bjarni
parents: 2561
diff changeset
   748
			_new_vehicle_id = v->index;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   749
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   750
			v->service_interval = _patches.servint_trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   751
			v->date_of_last_service = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
			v->build_year = _cur_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
			v->type = VEH_Train;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
			v->cur_image = 0xAC2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   755
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
   756
			v->u.rail.shortest_platform[0] = 255;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
   757
			v->u.rail.shortest_platform[1] = 0;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
   758
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   759
			VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   761
			if (rvi->flags & RVI_MULTIHEAD && !HASBIT(p2, 0)) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   762
				AddRearEngineToMultiheadedTrain(vl[0], vl[1], true);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   763
			} else {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   764
				AddArticulatedParts(rvi, vl);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   765
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
   767
			TrainConsistChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   768
			UpdateTrainAcceleration(v);
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   769
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
   770
			if (!HASBIT(p2, 1)) {	// check if the cars should be added to the new vehicle
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   771
				NormalizeTrainVehInDepot(v);
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
   772
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   773
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   774
			InvalidateWindow(WC_VEHICLE_DEPOT, tile);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
   775
			RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   776
			InvalidateWindow(WC_COMPANY, v->owner);
2618
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
   777
			if (IsLocalPlayer()) {
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
   778
				InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train); // updates the replace Train window
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
   779
			}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   780
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   781
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   782
	_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
   783
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   784
	return value;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   785
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   786
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   787
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   788
/* Check if all the wagons of the given train are in a depot, returns the
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   789
 * number of cars (including loco) then. If not, sets the error message to
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
   790
 * STR_881A_TRAINS_CAN_ONLY_BE_ALTERED and returns -1 */
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   791
int CheckTrainStoppedInDepot(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   792
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   793
	int count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   794
	TileIndex tile = v->tile;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   795
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
	/* check if stopped in a depot */
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
   797
	if (!IsTileDepotType(tile, TRANSPORT_RAIL) || v->cur_speed != 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
		_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   799
		return -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   801
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
	count = 0;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   803
	for (; v != NULL; v = v->next) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   804
		count++;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   805
		if (v->u.rail.track != 0x80 || v->tile != tile ||
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   806
				(v->subtype == TS_Front_Engine && !(v->vehstatus & VS_STOPPED))) {
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   807
			_error_message = STR_881A_TRAINS_CAN_ONLY_BE_ALTERED;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   808
			return -1;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   809
		}
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   810
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   811
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   812
	return count;
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
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   815
/**
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   816
 * Unlink a rail wagon from the consist.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   817
 * @param v Vehicle to remove.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   818
 * @param first The first vehicle of the consist.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   819
 * @return The first vehicle of the consist.
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   820
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
static Vehicle *UnlinkWagon(Vehicle *v, Vehicle *first)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
{
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   823
	Vehicle *u;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   824
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
	// unlinking the first vehicle of the chain?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
	if (v == first) {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   827
		v = GetNextVehicle(v);
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   828
		if (v == NULL) return NULL;
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   829
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   830
		v->subtype = TS_Free_Car;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   831
		return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
	}
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   833
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   834
	for (u = first; GetNextVehicle(u) != v; u = GetNextVehicle(u)) {}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   835
	GetLastEnginePart(u)->next = GetNextVehicle(v);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   836
	return first;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   839
static Vehicle *FindGoodVehiclePos(const Vehicle *src)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   840
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
	Vehicle *dst;
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
   842
	EngineID eng = src->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
	TileIndex tile = src->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   844
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
	FOR_ALL_VEHICLES(dst) {
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   846
		if (dst->type == VEH_Train && dst->subtype == TS_Free_Car &&
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   847
				dst->tile == tile) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
			// 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
   849
			Vehicle *v = dst;
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   850
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
			while (v->engine_type == eng) {
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   852
				v = v->next;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   853
				if (v == NULL) return dst;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   854
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   855
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   856
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   857
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   861
/** Move a rail vehicle around inside the depot.
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   862
 * @param x,y unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   863
 * @param p1 various bitstuffed elements
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   864
 * - p1 (bit  0 - 15) source vehicle index (p1 & 0xFFFF)
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   865
 * - p1 (bit 16 - 31) what wagon to put the source wagon AFTER (p1 & 0xFFFF0000) XXX - 0xFFFF0000 to make a new line
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   866
 * @param p2 (bit 0) move all vehicles following the source vehicle
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   867
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   868
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
   869
{
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   870
	VehicleID s = GB(p1, 0, 16);
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   871
	VehicleID d = GB(p1, 16, 16);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   872
	Vehicle *src, *dst, *src_head, *dst_head;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   873
	bool is_loco;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   874
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   875
	if (!IsVehicleIndex(s)) return CMD_ERROR;
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   876
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   877
	src = GetVehicle(s);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
   878
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   879
	if (src->type != VEH_Train) return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   881
	is_loco = !(RailVehInfo(src->engine_type)->flags & RVI_WAGON) && IS_FIRSTHEAD_SPRITE(src->spritenum);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
	// if nothing is selected as destination, try and find a matching vehicle to drag to.
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   884
	if (d == INVALID_VEHICLE) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
		dst = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
		if (!is_loco) dst = FindGoodVehiclePos(src);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
	} else {
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
   888
		dst = GetVehicle(d);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   890
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   891
	// if an articulated part is being handled, deal with its parent vehicle
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   892
	while (src->subtype == TS_Artic_Part) src = GetPrevVehicleInChain(src);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   893
	if (dst != NULL) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   894
		while (dst->subtype == TS_Artic_Part) dst = GetPrevVehicleInChain(dst);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   895
	}
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   896
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   897
	// don't move the same vehicle..
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   898
	if (src == dst) return 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   899
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
	/* the player must be the owner */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   901
	if (!CheckOwnership(src->owner) || (dst!=NULL && !CheckOwnership(dst->owner)))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   902
		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   904
	/* locate the head of the two chains */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
	src_head = GetFirstVehicleInChain(src);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
	dst_head = NULL;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   907
	if (dst != NULL) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   908
		dst_head = GetFirstVehicleInChain(dst);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   909
		// Now deal with articulated part of destination wagon
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   910
		dst = GetLastEnginePart(dst);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   911
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
   912
1601
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   913
	/* clear the ->first cache */
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   914
	{
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   915
		Vehicle *u;
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   916
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   917
		for (u = src_head; u != NULL; u = u->next) u->first = NULL;
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   918
		for (u = dst_head; u != NULL; u = u->next) u->first = NULL;
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   919
	}
83fbe3bfb847 (svn r2105) -Codechange: Added a cache for the first vehicle of a chain to increase performance, especially with many long trains
celestar
parents: 1554
diff changeset
   920
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   921
	/* check if all vehicles in the source train are stopped inside a depot */
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   922
	if (CheckTrainStoppedInDepot(src_head) < 0) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
	/* 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
   925
	 * 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
   926
	if (dst_head != NULL) {
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
   927
		int num = CheckTrainStoppedInDepot(dst_head);
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   928
		if (num < 0) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   930
		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
   931
			return_cmd_error(STR_8819_TRAIN_TOO_LONG);
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
		// if it's a multiheaded vehicle we're dragging to, drag to the vehicle before..
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   934
		while (IS_CUSTOM_SECONDHEAD_SPRITE(dst->spritenum) || (
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
   935
			!is_custom_sprite(dst->spritenum) && _engine_sprite_add[dst->spritenum] != 0)
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   936
		) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   937
			Vehicle *v = GetPrevVehicleInChain(dst);
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
   938
			if (v == NULL || src == v) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   939
			dst = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   940
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
		assert(dst_head->tile == src_head->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
	// when moving all wagons, we can't have the same src_head and dst_head
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   946
	if (HASBIT(p2, 0) && src_head == dst_head) return 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
	// 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
   949
	if (dst == NULL && src->subtype != TS_Front_Engine && is_loco) {
1282
ea2ae881814c (svn r1786) -Fix: unitnumber is increased to 16bit, so now you can have up to 5000
truelight
parents: 1266
diff changeset
   950
		UnitID unit_num = GetFreeUnitNumber(VEH_Train);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
		if (unit_num > _patches.max_trains)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   952
			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
   953
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
		if (flags & DC_EXEC)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   955
			src->unitnumber = unit_num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   956
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   957
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   958
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   959
	/* do it? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
	if (flags & DC_EXEC) {
2607
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
   961
		Vehicle *new_front = GetNextVehicle(src);	//used if next in line should make a train on it's own
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
   962
		bool make_new_front = src->subtype == TS_Front_Engine;
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
   963
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
   964
		if (HASBIT(p2, 0)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
			// unlink ALL wagons
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
			if (src != src_head) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
				Vehicle *v = src_head;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   968
				while (GetNextVehicle(v) != src) v = GetNextVehicle(v);
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   969
				GetLastEnginePart(v)->next = NULL;
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   970
			} else {
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   971
				src_head = NULL;
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
		} else {
1921
c20a59372a23 (svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents: 1917
diff changeset
   974
			// if moving within the same chain, dont use dst_head as it may get invalidated
c20a59372a23 (svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents: 1917
diff changeset
   975
			if (src_head == dst_head)
c20a59372a23 (svn r2427) - Fix: CmdMoveRailVehice; Prevent possible assertion failure when moving vehicles within one chain.
hackykid
parents: 1917
diff changeset
   976
				dst_head = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
			// unlink single wagon from linked list
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   978
			src_head = UnlinkWagon(src, src_head);
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
   979
			GetLastEnginePart(src)->next = NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
		}
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
		if (dst == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
			// 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
   984
			if (is_loco) {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   985
				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
   986
					// 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
   987
					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
   988
					assert(src->orders == NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
					src->num_orders = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
			} else {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   992
				src->subtype = TS_Free_Car;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
			}
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
   994
			dst_head = src;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   995
		} else {
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
   996
			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
   997
				// 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
   998
				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
   999
				DeleteVehicleOrders(src);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1001
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  1002
			src->subtype = TS_Not_First;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
			src->unitnumber = 0; // doesn't occupy a unitnumber anymore.
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
			// link in the wagon(s) in the chain.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
			{
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1007
				Vehicle *v;
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1008
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1009
				for (v = src; GetNextVehicle(v) != NULL; v = GetNextVehicle(v));
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1010
				GetLastEnginePart(v)->next = dst->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
			dst->next = src;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
2607
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1015
		/* If there is an engine behind first_engine we moved away, it should become new first_engine
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1016
		 * To do this, CmdMoveRailVehicle must be called once more
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1017
		 * since we set p2 to a condition that makes the statement false, we can't loop forever with this one */
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1018
		if (make_new_front && new_front != NULL && !(HASBIT(p2, 0))) {
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1019
			if (!(RailVehInfo(new_front->engine_type)->flags & RVI_WAGON)) {
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1020
				CmdMoveRailVehicle(x, y, flags, new_front->index | (INVALID_VEHICLE << 16), 1);
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1021
			}
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1022
		}
9915a4829de8 (svn r3144) -Fix: a new train is now made if the front unit is an engine and the former front engine is moved away (Meush)
bjarni
parents: 2602
diff changeset
  1023
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1024
		if (src_head) {
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1025
			TrainConsistChanged(src_head);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1026
			if (src_head->subtype == TS_Front_Engine) {
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1027
				UpdateTrainAcceleration(src_head);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1028
				InvalidateWindow(WC_VEHICLE_DETAILS, src_head->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1029
				/* Update the refit button and window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1030
				InvalidateWindow(WC_VEHICLE_REFIT, src_head->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1031
				InvalidateWindowWidget(WC_VEHICLE_VIEW, src_head->index, 12);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1032
			}
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1033
			/* Update the depot window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1034
			InvalidateWindow(WC_VEHICLE_DEPOT, src_head->tile);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1035
		};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
		if (dst_head) {
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1038
			TrainConsistChanged(dst_head);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1039
			if (dst_head->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1040
				UpdateTrainAcceleration(dst_head);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1041
				InvalidateWindow(WC_VEHICLE_DETAILS, dst_head->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1042
				/* Update the refit button and window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1043
				InvalidateWindowWidget(WC_VEHICLE_VIEW, dst_head->index, 12);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1044
				InvalidateWindow(WC_VEHICLE_REFIT, dst_head->index);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1045
			}
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1046
			/* Update the depot window */
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1047
			InvalidateWindow(WC_VEHICLE_DEPOT, dst_head->tile);
0
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
737
05082439a150 (svn r1189) Fix vehicle list update glitch when moving waggons in depots
tron
parents: 715
diff changeset
  1050
		RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	return 0;
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
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1056
/** Start/Stop a train.
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1057
 * @param x,y unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1058
 * @param p1 train to start/stop
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1059
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1060
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1061
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
  1062
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1063
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1064
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1065
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1066
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1067
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1068
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1069
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1070
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1071
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
		v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1073
		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
  1074
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1075
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1076
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1077
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1080
/**
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1081
 * Search for a matching rear-engine of a dual-headed train.
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1082
 * Do this as if you would find matching parentheses. If a new
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1083
 * engine is 'started', first 'close' that before 'closing' our
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1084
 * searched engine
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1085
 */
2599
ad75080afea4 (svn r3136) -Fix: [autoreplace] all cargo in engines that consists of more than one vehicle will try to move cargo from all vehicles
bjarni
parents: 2595
diff changeset
  1086
Vehicle *GetRearEngine(const Vehicle *v, EngineID engine)
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1087
{
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1088
	Vehicle *u;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1089
	int en_count = 1;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1090
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1091
	for (u = v->next; u != NULL; u = u->next) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1092
		if (u->engine_type == engine) { // find matching engine
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1093
			en_count += (IS_FIRSTHEAD_SPRITE(u->spritenum)) ? +1 : -1;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1094
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1095
			if (en_count == 0) return (Vehicle *)u;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1096
		}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1097
	}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1098
	return NULL;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1099
}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1100
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1101
/** Sell a (single) train wagon/engine.
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1102
 * @param x,y unused
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1103
 * @param p1 the wagon/engine index
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1104
 * @param p2 the selling mode
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1105
 * - p2 = 0: only sell the single dragged wagon/engine (and any belonging rear-engines)
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1106
 * - p2 = 1: sell the vehicle and all vehicles following it in the chain
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1107
             if the wagon is dragged, don't delete the possibly belonging rear-engine to some front
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1108
 * - p2 = 2: when selling attached locos, rearrange all vehicles after it to separate lines;
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1109
 *           all wagons of the same type will go on the same line. Used by the AI currently
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1110
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1111
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
  1112
{
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1113
	Vehicle *v, *tmp, *first;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1114
	int32 cost = 0;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1115
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1116
	if (!IsVehicleIndex(p1) || p2 > 2) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1117
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1118
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1119
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1120
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1121
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1122
	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1123
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1124
	while (v->subtype == TS_Artic_Part) v = GetPrevVehicleInChain(v);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1125
	first = GetFirstVehicleInChain(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1126
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1127
	// make sure the vehicle is stopped in the depot
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1128
	if (CheckTrainStoppedInDepot(first) < 0) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1129
1842
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1130
	if (flags & DC_EXEC) {
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1131
		if (v == first && first->subtype == TS_Front_Engine) {
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1132
			DeleteWindowById(WC_VEHICLE_VIEW, first->index);
2618
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
  1133
		}
9691753519ac (svn r3156) -Fix: removed some cases where autoreplace windows were redrawn when nothing was changed
bjarni
parents: 2617
diff changeset
  1134
		if (IsLocalPlayer() && (p1 == 1 || !(RailVehInfo(v->engine_type)->flags & RVI_WAGON))) {
1842
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1135
			InvalidateWindow(WC_REPLACE_VEHICLE, VEH_Train);
fb9086b04740 (svn r2347) - Fix (regression): depot window did not get redrawn when a non-train-engine was sold.
Darkvater
parents: 1802
diff changeset
  1136
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1137
		InvalidateWindow(WC_VEHICLE_DEPOT, first->tile);
1779
25b3028dc97c (svn r2283) - Fix (regression): [ 1197493 ] train_gui.c:1341: failed assertion ..., somehow RebuildVehicleList() got lost during the rewrite :P
Darkvater
parents: 1777
diff changeset
  1138
		RebuildVehicleLists();
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1139
	}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1140
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1141
	switch (p2) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1142
		case 0: case 2: { /* Delete given wagon */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1143
			bool switch_engine = false;    // update second wagon to engine?
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1144
			byte ori_subtype = v->subtype; // backup subtype of deleted wagon in case DeleteVehicle() changes
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1145
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1146
			/* 1. Delete the engine, if it is dualheaded also delete the matching
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1147
			* rear engine of the loco (from the point of deletion onwards) */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1148
			Vehicle *rear = (RailVehInfo(v->engine_type)->flags & RVI_MULTIHEAD) ? GetRearEngine(v, v->engine_type) : NULL;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1149
			if (rear != NULL) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1150
				cost -= v->value;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1151
				if (flags & DC_EXEC) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1152
					v = UnlinkWagon(rear, v);
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1153
					DeleteVehicle(rear);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1154
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1155
			}
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1156
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1157
			/* 2. We are selling the first engine, some special action might be required
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1158
				* here, so take attention */
1770
18491bba9211 (svn r2274) - Codechange: some comments, parentheses and EngineID typedef for engine_type
Darkvater
parents: 1766
diff changeset
  1159
			if ((flags & DC_EXEC) && v == first) {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1160
				Vehicle *new_f = GetNextVehicle(first);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1161
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1162
				/* 2.1 If the first wagon is sold, update the first-> pointers to NULL */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1163
				for (tmp = first; tmp != NULL; tmp = tmp->next) tmp->first = NULL;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1164
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1165
				/* 2.2 If there are wagons present after the deleted front engine, check
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1166
					* if the second wagon (which will be first) is an engine. If it is one,
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1167
					* promote it as a new train, retaining the unitnumber, orders */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1168
				if (new_f != NULL) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1169
					if (!(RailVehInfo(new_f->engine_type)->flags & RVI_WAGON) && IS_FIRSTHEAD_SPRITE(new_f->spritenum)) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1170
						switch_engine = true;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1171
						/* Copy important data from the front engine */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1172
						new_f->unitnumber = first->unitnumber;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1173
						new_f->current_order = first->current_order;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1174
						new_f->cur_order_index = first->cur_order_index;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1175
						new_f->orders = first->orders;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1176
						new_f->num_orders = first->num_orders;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1177
						first->orders = NULL; // XXX - to not to delete the orders */
2425
e755ee29133a (svn r2951) - Fix: [ 1259345 ] Changing engine in netgame opens train window for everyone
Darkvater
parents: 2360
diff changeset
  1178
						if (IsLocalPlayer()) ShowTrainViewWindow(new_f);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1179
					}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1180
				}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1181
			}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1182
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1183
			/* 3. Delete the requested wagon */
1472
a2abe3b9d7a7 (svn r1976) Cleanups - mostly indentation and fiddling with loops
tron
parents: 1461
diff changeset
  1184
			cost -= v->value;
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1185
			if (flags & DC_EXEC) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1186
				first = UnlinkWagon(v, first);
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1187
				DeleteVehicle(v);
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1188
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1189
				/* 4 If the second wagon was an engine, update it to front_engine
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1190
					* which UnlinkWagon() has changed to TS_Free_Car */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1191
				if (switch_engine) first->subtype = TS_Front_Engine;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1192
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1193
				/* 5. If the train still exists, update its acceleration, window, etc. */
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1194
				if (first != NULL) {
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1195
					TrainConsistChanged(first);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1196
					if (first->subtype == TS_Front_Engine) {
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1197
						InvalidateWindow(WC_VEHICLE_DETAILS, first->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1198
						InvalidateWindow(WC_VEHICLE_REFIT, first->index);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1199
						UpdateTrainAcceleration(first);
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1200
					}
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1201
				}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1202
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1203
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1204
				/* (6.) Borked AI. If it sells an engine it expects all wagons lined
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1205
				* up on a new line to be added to the newly built loco. Replace it is.
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1206
				* Totally braindead cause building a new engine adds all loco-less
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1207
				* engines to its train anyways */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1208
				if (p2 == 2 && ori_subtype == TS_Front_Engine) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1209
					for (v = first; v != NULL; v = tmp) {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1210
						tmp = GetNextVehicle(v);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1211
						DoCommandByTile(v->tile, v->index | INVALID_VEHICLE << 16, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1212
					}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1213
				}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1214
			}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1215
		} break;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1216
		case 1: { /* Delete wagon and all wagons after it given certain criteria */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1217
			/* 1. Count the number for first and rear engines for dualheads
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1218
			* to be able to deduce which ones go with which ones */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1219
			int enf_count = 0;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1220
			int enr_count = 0;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1221
			for (tmp = first; tmp != NULL; tmp = GetNextVehicle(tmp)) {
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1222
				if (RailVehInfo(tmp->engine_type)->flags & RVI_MULTIHEAD)
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1223
					(IS_FIRSTHEAD_SPRITE(tmp->spritenum)) ? enf_count++ : enr_count++;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1224
			}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1225
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1226
			/* 2. Start deleting every vehicle after the selected one
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1227
			* If we encounter a matching rear-engine to a front-engine
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1228
			* earlier in the chain (before deletion), leave it alone */
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1229
			for (; v != NULL; v = tmp) {
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2599
diff changeset
  1230
				tmp = GetNextVehicle(v);
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1231
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1232
				if (RailVehInfo(v->engine_type)->flags & RVI_MULTIHEAD) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1233
					if (IS_FIRSTHEAD_SPRITE(v->spritenum)) {
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1234
						/* Always delete newly encountered front-engines */
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1235
						enf_count--;
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1236
					} else if (enr_count > enf_count) {
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1237
						/* More rear engines than front engines means this rear-engine does
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1238
						 * not belong to any front-engine; delete */
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1239
						enr_count--;
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1240
					} else {
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1241
						/* Otherwise leave it alone */
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1242
						continue;
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1243
					}
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1244
				}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1245
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1246
				cost -= v->value;
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1247
				if (flags & DC_EXEC) {
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1248
					first = UnlinkWagon(v, first);
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1249
					DeleteVehicle(v);
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1250
				}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1251
			}
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1252
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1253
			/* 3. If it is still a valid train after selling, update its acceleration and cached values */
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1254
			if ((flags & DC_EXEC) && first != NULL) {
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1255
				TrainConsistChanged(first);
1917
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1256
				if (first->subtype == TS_Front_Engine)
03e113481a62 (svn r2423) - CodeChange: Include first_engine in the train cache, instead of calculating it all over the place.
hackykid
parents: 1909
diff changeset
  1257
					UpdateTrainAcceleration(first);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  1258
			}
1766
8cdb302ae946 (svn r2270) - Fix/Codechange: fix up selling rail vehicles. Try to be smart about it. When selling dual-headed trains, also sell the rear end; if the now-first wagon is an engine, make it a train. When selling the whole train, try to leave dual-headed setups intact
Darkvater
parents: 1758
diff changeset
  1259
		} break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
static void UpdateTrainDeltaXY(Vehicle *v, int direction)
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
#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
  1267
	static const uint32 _delta_xy_table[8] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1271
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1273
		MKIT(3, 7, -1, -3),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1274
		MKIT(3, 3, -1, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1275
		MKIT(7, 3, -3, -1),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1277
#undef MKIT
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
	uint32 x = _delta_xy_table[direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1281
	v->x_offs        = GB(x,  0, 8);
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1282
	v->y_offs        = GB(x,  8, 8);
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1283
	v->sprite_width  = GB(x, 16, 8);
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  1284
	v->sprite_height = GB(x, 24, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1285
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
static void UpdateVarsAfterSwap(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1288
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1289
	UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
	v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1291
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1292
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1295
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1296
static void SetLastSpeed(Vehicle *v, int spd) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1297
	int old = v->u.rail.last_speed;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1298
	if (spd != old) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1299
		v->u.rail.last_speed = spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1300
		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
  1301
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1302
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1303
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1304
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1305
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
  1306
{
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1307
	byte flag1, flag2;
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1308
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1309
	flag1 = *swap_flag1;
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1310
	flag2 = *swap_flag2;
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1311
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1312
	/* Clear the flags */
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1313
	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
  1314
	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
  1315
	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
  1316
	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
  1317
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1318
	/* 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
  1319
	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
  1320
		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
  1321
	} 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
  1322
		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
  1323
	}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1324
	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
  1325
		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
  1326
	} 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
  1327
		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
  1328
	}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1329
}
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1330
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1331
static void ReverseTrainSwapVeh(Vehicle *v, int l, int r)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1332
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1333
	Vehicle *a, *b;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1334
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1335
	/* locate vehicles to swap */
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1336
	for (a = v; l != 0; l--) a = a->next;
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1337
	for (b = v; r != 0; r--) b = b->next;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1338
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1339
	if (a != b) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1340
		/* swap the hidden bits */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1341
		{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1342
			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
  1343
			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
  1344
			a->vehstatus = tmp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1345
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1346
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1347
		/* swap variables */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1348
		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
  1349
		swap_byte(&a->direction, &b->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1350
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1351
		/* toggle direction */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1352
		if (!(a->u.rail.track & 0x80)) a->direction ^= 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1353
		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
  1354
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1355
		/* 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
  1356
		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
  1357
		swap_int32(&a->y_pos, &b->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1358
		swap_tile(&a->tile, &b->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1359
		swap_byte(&a->z_pos, &b->z_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1360
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1361
		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
  1362
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1363
		/* update other vars */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1364
		UpdateVarsAfterSwap(a);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1365
		UpdateVarsAfterSwap(b);
1554
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1366
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1367
		VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1368
		VehicleEnterTile(b, b->tile, b->x_pos, b->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1369
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1370
		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
  1371
		UpdateVarsAfterSwap(a);
1554
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1372
79809a004e97 (svn r2058) -Fix: hopefully this fixes the reverse-train-in-depot-bugs (plural)
truelight
parents: 1552
diff changeset
  1373
		VehicleEnterTile(a, a->tile, a->x_pos, a->y_pos);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1374
	}
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
744
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1377
/* 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
  1378
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
  1379
{
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1380
	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
  1381
		return NULL;
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1382
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1383
	return v;
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1384
}
ee33af65c436 (svn r1200) -Fix: Fixed bug pointed out by Tron: when a train is on the
truelight
parents: 743
diff changeset
  1385
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1386
static void DisableTrainCrossing(TileIndex tile)
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1387
{
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1388
	/* Test if we have a rail/road-crossing */
1927
1181e7652e2f (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1926
diff changeset
  1389
	if (IsTileType(tile, MP_STREET) && IsLevelCrossing(tile)) {
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1390
		/* Check if there is a train on the tile itself */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1391
		if (VehicleFromPos(tile, &tile, TestTrainOnCrossing) == NULL) {
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1392
			/* If light is on, switch light off */
2493
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2484
diff changeset
  1393
			if (GB(_m[tile].m5, 2, 1) != 0) {
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2484
diff changeset
  1394
				SB(_m[tile].m5, 2, 1, 0);
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1395
				MarkTileDirtyByTile(tile);
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1396
			}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1397
		}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1398
	}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1399
}
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1400
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1401
/**
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1402
 * Advances wagons for train reversing, needed for variable length wagons.
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1403
 * Needs to be called once before the train is reversed, and once after it.
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1404
 * @param v First vehicle in chain
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1405
 * @param before Set to true for the call before reversing, false otherwise
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1406
 */
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1407
static void AdvanceWagons(Vehicle *v, bool before)
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1408
{
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1409
	Vehicle *base, *first, *last, *tempnext;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1410
	int i, length;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1411
	int differential;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1412
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1413
	base = v;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1414
	first = base->next;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1415
	length = CountVehiclesInChain(v);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1416
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1417
	while (length > 2) {
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1418
		// find pairwise matching wagon
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1419
		// start<>end, start+1<>end-1, ... */
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1420
		last = first;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1421
		for (i = length - 3; i; i--) {
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1422
			last = last->next;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1423
		}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1424
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1425
		differential = last->u.rail.cached_veh_length - base->u.rail.cached_veh_length;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1426
		if (before)
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1427
			differential *= -1;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1428
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1429
		if (differential > 0) {
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1430
			// disconnect last car to make sure only this subset moves
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1431
			tempnext = last->next;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1432
			last->next = NULL;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1433
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1434
			for (i = 0; i < differential; i++) {
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1435
				TrainController(first);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1436
			}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1437
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1438
			last->next = tempnext;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1439
		}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1440
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1441
		base = first;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1442
		first = first->next;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1443
		length -= 2;
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1444
	}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1445
}
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1446
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  1447
static TileIndex GetVehicleTileOutOfTunnel(const Vehicle* v, bool reverse)
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1448
{
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1449
	TileIndex tile;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1450
	byte direction = (!reverse) ? DirToDiagdir(v->direction) : ReverseDiagdir(v->direction >> 1);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1451
	TileIndexDiff delta = TileOffsByDir(direction);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1452
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1453
	if (v->u.rail.track != 0x40)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1454
		return v->tile;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1455
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1456
	for (tile = v->tile;; tile += delta) {
2493
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2484
diff changeset
  1457
		if (IsTunnelTile(tile) && GB(_m[tile].m5, 0, 2) != direction && GetTileZ(tile) == v->z_pos)
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1458
 			break;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1459
 	}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1460
 	return tile;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1461
2182
8464b9efd19e (svn r2696) Remove stray semicolons
tron
parents: 2164
diff changeset
  1462
}
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1463
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1464
static void ReverseTrainDirection(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1465
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1466
	int l = 0, r = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1467
	Vehicle *u;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1468
	TileIndex tile;
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1469
	Trackdir trackdir;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1470
	TileIndex pbs_end_tile = v->u.rail.pbs_end_tile; // these may be changed, and we may need
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1471
	Trackdir pbs_end_trackdir = v->u.rail.pbs_end_trackdir; // the old values, so cache them
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1472
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1473
	u = GetLastVehicleInChain(v);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1474
	tile = GetVehicleTileOutOfTunnel(u, false);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1475
	trackdir = ReverseTrackdir(GetVehicleTrackdir(u));
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1476
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1477
	if (PBSTileReserved(tile) & (1 << TrackdirToTrack(trackdir))) {
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1478
		NPFFindStationOrTileData fstd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1479
		NPFFoundTargetData ftd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1480
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1481
		NPFFillWithOrderData(&fstd, v);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1482
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1483
		tile = GetVehicleTileOutOfTunnel(u, true);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1484
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1485
		DEBUG(pbs, 2) ("pbs: (%i) choose reverse (RV), tile:%x, trackdir:%i",v->unitnumber,  u->tile, trackdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1486
		ftd = NPFRouteToStationOrTile(tile, trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_ANY);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1487
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1488
		if (ftd.best_trackdir == 0xFF) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1489
			DEBUG(pbs, 0) ("pbs: (%i) no nodes encountered (RV)", v->unitnumber);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1490
			CLRBIT(v->u.rail.flags, VRF_REVERSING);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1491
			return;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1492
		}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1493
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1494
    // we found a way out of the pbs block
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1495
		if (NPFGetFlag(&ftd.node, NPF_FLAG_PBS_EXIT)) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1496
			if (NPFGetFlag(&ftd.node, NPF_FLAG_PBS_BLOCKED)) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1497
				CLRBIT(v->u.rail.flags, VRF_REVERSING);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1498
				return;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1499
			}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1500
		}
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1501
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1502
		v->u.rail.pbs_end_tile = ftd.node.tile;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1503
		v->u.rail.pbs_end_trackdir = ftd.node.direction;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1504
	}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1505
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1506
	tile = GetVehicleTileOutOfTunnel(v, false);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1507
	trackdir = GetVehicleTrackdir(v);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1508
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1509
	if (v->u.rail.pbs_status == PBS_STAT_HAS_PATH) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1510
		TileIndex tile = AddTileIndexDiffCWrap(v->tile, TileIndexDiffCByDir(TrackdirToExitdir(trackdir)));
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1511
		uint32 ts;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1512
		assert(tile != INVALID_TILE);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1513
		ts = GetTileTrackStatus(tile, TRANSPORT_RAIL);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1514
		ts &= TrackdirReachesTrackdirs(trackdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1515
		assert(ts != 0 && KillFirstBit2x64(ts) == 0);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1516
		trackdir = FindFirstBit2x64(ts);
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1517
		PBSClearPath(tile, trackdir, pbs_end_tile, pbs_end_trackdir);
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1518
		v->u.rail.pbs_status = PBS_STAT_NONE;
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1519
	} else if (PBSTileReserved(tile) & (1 << TrackdirToTrack(trackdir))) {
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1520
		PBSClearPath(tile, trackdir, pbs_end_tile, pbs_end_trackdir);
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1521
		if (v->u.rail.track != 0x40)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1522
			PBSReserveTrack(tile, trackdir & 7);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1523
	};
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1524
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1525
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1526
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1527
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1528
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1529
	/* 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
  1530
	{
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1531
		TileIndex tile = v->tile;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1532
		int t;
1459
19333d7f99b3 (svn r1963) - Add: [NPF] Penalty for a red signal that is the last signal on the path.
matthijs
parents: 1455
diff changeset
  1533
		/* Determine the diagonal direction in which we will exit this tile */
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1534
		t = v->direction >> 1;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1535
		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
  1536
			t = (t - 1) & 3;
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1537
		}
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1538
		/* Calculate next tile */
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 889
diff changeset
  1539
		tile += TileOffsByDir(t);
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1540
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1541
		/* Check if the train left a rail/road-crossing */
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  1542
		DisableTrainCrossing(tile);
743
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1543
	}
da4d304b52d2 (svn r1199) -Fix: [ 958098 ] No longer road/rail crossing signals hang when a train
truelight
parents: 742
diff changeset
  1544
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1545
	// count number of vehicles
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1546
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1547
	do r++; while ( (u = u->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1548
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1549
	AdvanceWagons(v, true);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1550
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1551
	/* swap start<>end, start+1<>end-1, ... */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1552
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1553
		ReverseTrainSwapVeh(v, l++, r--);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1554
	} while (l <= r);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1555
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1556
	AdvanceWagons(v, false);
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  1557
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1558
	if (IsTileDepotType(v->tile, TRANSPORT_RAIL))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1559
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1560
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  1561
	CLRBIT(v->u.rail.flags, VRF_REVERSING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1562
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1563
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1564
/** Reverse train.
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1565
 * @param x,y unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1566
 * @param p1 train to reverse
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1567
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1568
 */
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1569
 int32 CmdReverseTrainDirection(int x, int y, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1570
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1571
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1572
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1573
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1574
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1575
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1576
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1577
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
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
	_error_message = STR_EMPTY;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1580
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1581
//	if (v->u.rail.track & 0x80 || IsTileDepotType(v->tile, TRANSPORT_RAIL))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1582
//		return CMD_ERROR;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1583
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1584
	if (v->u.rail.crash_anim_pos != 0 || v->breakdown_ctr != 0) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1585
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1586
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1587
		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
  1588
			TOGGLEBIT(v->u.rail.flags, VRF_REVERSING);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1589
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1590
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1591
			SetLastSpeed(v, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1592
			ReverseTrainDirection(v);
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
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1596
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1597
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1598
/** Force a train through a red signal
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1599
 * @param x,y unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1600
 * @param p1 train to ignore the red signal
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1601
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1602
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1603
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
  1604
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1605
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1606
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1607
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1608
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1609
	v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1610
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1611
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1612
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1613
	if (flags & DC_EXEC)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1614
		v->u.rail.force_proceed = 0x50;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1615
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1616
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1617
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1618
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1619
/** Refits a train to the specified cargo type.
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1620
 * @param x,y unused
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1621
 * @param p1 vehicle ID of the train to refit
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1622
 * @param p2 the new cargo type to refit to (p2 & 0xFF)
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1623
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1624
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
  1625
{
2635
7ed07303448d (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2634
diff changeset
  1626
	CargoID new_cid = GB(p2, 0, 8);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1627
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1628
	int32 cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1629
	uint num;
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 817
diff changeset
  1630
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1631
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1632
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  1633
	v = GetVehicle(p1);
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1634
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1635
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1636
	if (CheckTrainStoppedInDepot(v) < 0) return_cmd_error(STR_TRAIN_MUST_BE_STOPPED);
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1637
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1638
	/* Check cargo */
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1639
	if (new_cid > NUM_CARGO) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1640
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1641
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1642
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1643
	cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1644
	num = 0;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 900
diff changeset
  1645
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1646
	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
  1647
		/* 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
  1648
		 * 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
  1649
		 * some nice [Refit] button near each wagon. --pasky */
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1650
		if (!CanRefitTo(v, new_cid)) continue;
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1794
diff changeset
  1651
1859
870dcb6fd65b (svn r2365) - Change: [refitting] Make refitting capacities for trains newgrf compatible. Train vehicles can now carry twice as much mail/goods as other cargo, and four times as much passengers.
hackykid
parents: 1842
diff changeset
  1652
		if (v->cargo_cap != 0) {
2463
db029c987bc7 (svn r2989) - Make engine/vehicle information tables constant. Duplicate them so NewGRF data can be loaded without wiping out the default data.
peter1138
parents: 2456
diff changeset
  1653
			const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
1895
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1654
			uint16 amount = CALLBACK_FAILED;
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1655
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1656
			if (HASBIT(rvi->callbackmask, CBM_REFIT_CAP)) {
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1657
				/* Check the 'refit capacity' callback */
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1658
				CargoID temp_cid = v->cargo_type;
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1659
				v->cargo_type = new_cid;
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1660
				amount = GetCallBackResult(CBID_REFIT_CAP, v->engine_type, v);
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1661
				v->cargo_type = temp_cid;
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1662
			}
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1663
0d499b71a782 (svn r2401) - Fix: [newgrf] Finish up callback mechanism, implement 'refit capacity' callback slightly more correct.
hackykid
parents: 1891
diff changeset
  1664
			if (amount == CALLBACK_FAILED) { // callback failed or not used, use default
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1665
				CargoID old_cid = rvi->cargo_type;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1666
				/* normally, the capacity depends on the cargo type, a rail vehicle
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1667
				* can carry twice as much mail/goods as normal cargo,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1668
				* and four times as much passengers */
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1669
				amount = rvi->capacity;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1670
				(old_cid == CT_PASSENGERS) ||
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1671
				(amount <<= 1, old_cid == CT_MAIL || old_cid == CT_GOODS) ||
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1672
				(amount <<= 1, true);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1673
				(new_cid == CT_PASSENGERS) ||
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1674
				(amount >>= 1, new_cid == CT_MAIL || new_cid == CT_GOODS) ||
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1675
				(amount >>= 1, true);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1676
			};
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1677
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1678
			if (amount != 0) {
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1679
				if (new_cid != v->cargo_type)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1680
					cost += (_price.build_railvehicle >> 8);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1681
				num += amount;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1682
				if (flags & DC_EXEC) {
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1683
					v->cargo_count = 0;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1684
					v->cargo_type = new_cid;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1685
					v->cargo_cap = amount;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1686
					InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1687
					InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1882
diff changeset
  1688
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1689
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1690
		}
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2214
diff changeset
  1691
	} while ( (v=v->next) != NULL );
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1693
	_returned_refit_amount = num;
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
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1697
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1698
typedef struct TrainFindDepotData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1699
	uint best_length;
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  1700
	TileIndex tile;
2475
efdcc705cb61 (svn r3001) s/Player*/const Player*/
tron
parents: 2464
diff changeset
  1701
	PlayerID owner;
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1702
	/**
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1703
	 * true if reversing is necesarry for the train to get to this depot This
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1704
	 * value is unused when new depot finding and NPF are both disabled
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1705
	 */
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1706
	bool reverse;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1707
} TrainFindDepotData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1708
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1709
static bool NtpCallbFindDepot(TileIndex tile, TrainFindDepotData *tfdd, int track, uint length)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1710
{
1901
03bf9bf99319 (svn r2407) Use {Get,Is}TileOwner to get/check the owner of a tile and fix some bogus reads of _map_owner
tron
parents: 1895
diff changeset
  1711
	if (IsTileType(tile, MP_RAILWAY) && IsTileOwner(tile, tfdd->owner)) {
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  1712
		if ((_m[tile].m5 & ~0x3) == 0xC0) {
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1713
			tfdd->best_length = length;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1714
			tfdd->tile = tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1715
			return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1716
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1717
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1718
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1719
	return false;
0
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
1758
2158a0938e45 (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1722
// returns the tile of a depot to goto to. The given vehicle must not be
2158a0938e45 (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1723
// crashed!
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
  1724
static TrainFindDepotData FindClosestTrainDepot(Vehicle *v)
0
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
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1727
	TrainFindDepotData tfdd;
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  1728
	TileIndex tile = v->tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1729
1758
2158a0938e45 (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1730
	assert(!(v->vehstatus & VS_CRASHED));
2158a0938e45 (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1731
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
  1732
	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
  1733
	tfdd.best_length = (uint)-1;
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1734
	tfdd.reverse = false;
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
  1735
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  1736
	if (IsTileDepotType(tile, TRANSPORT_RAIL)){
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
  1737
		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
  1738
		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
  1739
		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
  1740
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1741
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1742
	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
  1743
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1744
	if (_patches.new_pathfinding_all) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1745
		NPFFoundTargetData ftd;
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1746
		Vehicle* last = GetLastVehicleInChain(v);
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1747
		Trackdir trackdir = GetVehicleTrackdir(v);
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1748
		Trackdir trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1749
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  1750
		assert (trackdir != INVALID_TRACKDIR);
2006
9d5d7fd428c2 (svn r2514) - Codechange: [NPF] Move the checking of railtype into a funciton IsCompatibleRail().
matthijs
parents: 1998
diff changeset
  1751
		ftd = NPFRouteToDepotBreadthFirstTwoWay(v->tile, trackdir, last->tile, trackdir_rev, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, NPF_INFINITE_PENALTY);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1752
		if (ftd.best_bird_dist == 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1753
			/* Found target */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1754
			tfdd.tile = ftd.node.tile;
1675
dd557a4d108d (svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents: 1615
diff changeset
  1755
			/* Our caller expects a number of tiles, so we just approximate that
dd557a4d108d (svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents: 1615
diff changeset
  1756
			* number by this. It might not be completely what we want, but it will
dd557a4d108d (svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents: 1615
diff changeset
  1757
			* work for now :-) We can possibly change this when the old pathfinder
dd557a4d108d (svn r2179) - Fix: [ 1121437, 1114228 ] Vehicles not entering depots for auto servicing.
matthijs
parents: 1615
diff changeset
  1758
			* is removed. */
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1759
			tfdd.best_length = ftd.best_path_dist / NPF_TILE_LENGTH;
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1760
			if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE))
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1761
				tfdd.reverse = true;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  1762
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1763
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1764
		// search in the forward direction first.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1765
		i = v->direction >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1766
		if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; }
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1767
		NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
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
  1768
		if (tfdd.best_length == (uint)-1){
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1769
			tfdd.reverse = true;
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
  1770
			// 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
  1771
			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
  1772
			if (!(v->direction & 1) && v->u.rail.track != _state_dir_table[i]) { i = (i - 1) & 3; }
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  1773
			NewTrainPathfind(tile, 0, i, (NTPEnumProc*)NtpCallbFindDepot, &tfdd);
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
  1774
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1775
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1776
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
  1777
	return tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1778
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1779
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1780
/** Send a train to a depot
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1781
 * @param x,y unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1782
 * @param p1 train to send to the depot
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1783
 * @param p2 unused
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1784
 */
1794
24b4239f2090 (svn r2298) - CodeChange: removed CmdAbuses: CmdSetTownNameType(), CmdStartNewGame(), CmdCreateScenario(), CmdSetNewLandscapeType() and CmdGenRandomNewGame().
Darkvater
parents: 1790
diff changeset
  1785
int32 CmdSendTrainToDepot(int x, int y, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1786
{
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1787
	Vehicle *v;
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
  1788
	TrainFindDepotData tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1789
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1790
	if (!IsVehicleIndex(p1)) return CMD_ERROR;
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1791
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1792
	v = GetVehicle(p1);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1793
1784
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1794
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1795
d0698aac0c2e (svn r2288) - CodeChange: protected the next batch of commands (41 so far, out of 115).
Darkvater
parents: 1779
diff changeset
  1796
	if (v->vehstatus & VS_CRASHED) return CMD_ERROR;
1757
89f63a75ffd2 (svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents: 1752
diff changeset
  1797
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1798
	if (v->current_order.type == OT_GOTO_DEPOT) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
		if (flags & DC_EXEC) {
1615
24c6310f887b (svn r2119) - Fix: [ 1172878 ] Trains "Go to depot" button: click twice skip to next order (patch by Loic GUILLOUX). I also added short explanation of OF_/OFB_ difference to order.h.
pasky
parents: 1601
diff changeset
  1800
			if (HASBIT(v->current_order.flags, OFB_PART_OF_ORDERS)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1801
				v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1802
				v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
			}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1804
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1805
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1806
			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
  1807
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1808
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1809
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1810
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
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
  1812
	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
  1813
	if (tfdd.best_length == (uint)-1)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1814
		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
  1815
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1816
	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
  1817
		v->dest_tile = tfdd.tile;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  1818
		v->current_order.type = OT_GOTO_DEPOT;
1520
d88442095697 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1507
diff changeset
  1819
		v->current_order.flags = OF_NON_STOP | OF_FULL_LOAD;
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
  1820
		v->current_order.station = GetDepotByTile(tfdd.tile)->index;
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
  1821
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
1777
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1822
		/* If there is no depot in front, reverse automatically */
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1823
		if (tfdd.reverse)
f703cf05b5b9 (svn r2281) - Fix: [ 1115204 ] [NPF] When pressing the goto depot button, trains will now also look behind it if there is no depot in front. If so, the train reverses immediately. This also work anywhere, not just at stations.
matthijs
parents: 1770
diff changeset
  1824
			DoCommandByTile(v->tile, v->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1825
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1826
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1827
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1828
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1829
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1784
diff changeset
  1830
/** Change the service interval for trains.
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1784
diff changeset
  1831
 * @param x,y unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1784
diff changeset
  1832
 * @param p1 vehicle ID that is being service-interval-changed
1790
47963a0cfca3 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  1833
 * @param p2 new service interval
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1834
 */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1835
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
  1836
{
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1837
	Vehicle *v;
1790
47963a0cfca3 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  1838
	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
47963a0cfca3 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  1839
47963a0cfca3 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  1840
	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
1237
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1841
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1842
	v = GetVehicle(p1);
1c7a3f9c94b9 (svn r1741) - Fix: added IsVehicleIndex() so it's possible to protect GetVehicle() from reading an invalid vehicle index
bjarni
parents: 1236
diff changeset
  1843
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1784
diff changeset
  1844
	if (v->type != VEH_Train || !CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1845
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1846
	if (flags & DC_EXEC) {
1790
47963a0cfca3 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  1847
		v->service_interval = serv_int;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1848
		InvalidateWindowWidget(WC_VEHICLE_DETAILS, v->index, 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1849
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  1850
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1851
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1852
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1853
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
  1854
void OnTick_Train(void)
0
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
	_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
  1857
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1858
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1859
static const int8 _vehicle_smoke_pos[8] = {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1860
	1, 1, 1, 0, -1, -1, -1, 0
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1861
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1862
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  1863
static void HandleLocomotiveSmokeCloud(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  1865
	const Vehicle* u;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1866
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1867
	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
  1868
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1869
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1870
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1871
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
	do {
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
  1873
		EngineID engtype = v->engine_type;
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1874
		int effect_offset = GB(v->u.rail.cached_vis_effect, 0, 4) - 8;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1875
		byte effect_type = GB(v->u.rail.cached_vis_effect, 4, 2);
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1876
		bool disable_effect = HASBIT(v->u.rail.cached_vis_effect, 6);
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1877
		int x, y;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1878
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1879
		// no smoke?
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1880
		if ((RailVehInfo(engtype)->flags & RVI_WAGON && effect_type == 0) ||
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1881
				disable_effect ||
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  1882
				GetEngine(engtype)->railtype > RAILTYPE_RAIL ||
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1925
diff changeset
  1883
				(v->vehstatus & VS_HIDDEN) || (v->u.rail.track & 0xC0))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1884
			continue;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1885
2612
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1886
		// No smoke in depots or tunnels
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1887
		if (IsTileDepotType(v->tile, TRANSPORT_RAIL) || IsTunnelTile(v->tile))
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1888
			continue;
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1889
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1890
		if (effect_type == 0) {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1891
			// Use default effect type for engine class.
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1892
			effect_type = RailVehInfo(engtype)->engclass;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1893
		} else {
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1894
			effect_type--;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1895
		}
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1896
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1897
		x = _vehicle_smoke_pos[v->direction] * effect_offset;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1898
		y = _vehicle_smoke_pos[(v->direction + 2) % 8] * effect_offset;
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1899
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1900
		switch (effect_type) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1901
		case 0:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1902
			// steam smoke.
2612
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1903
			if (GB(v->tick_counter, 0, 4) == 0) {
2595
1c5db200577c (svn r3132) -NewGRF: support positioning of rail vehicle visual effects.
peter1138
parents: 2587
diff changeset
  1904
				CreateEffectVehicleRel(v, x, y, 10, EV_STEAM_SMOKE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1905
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1906
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1907
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1908
		case 1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1909
			// diesel smoke
2612
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1910
			if (u->cur_speed <= 40 && GB(Random(), 0, 16) <= 0x1E00) {
1359
52782e5cf7c9 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  1911
				CreateEffectVehicleRel(v, 0, 0, 10, EV_DIESEL_SMOKE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1912
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1913
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1914
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1915
		case 2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1916
			// blue spark
2612
eccc835aaa44 (svn r3150) Unify some code duplication in HandleLocomotiveSmokeCloud()
peter1138
parents: 2610
diff changeset
  1917
			if (GB(v->tick_counter, 0, 2) == 0 && GB(Random(), 0, 16) <= 0x5B0) {
1359
52782e5cf7c9 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  1918
				CreateEffectVehicleRel(v, 0, 0, 10, EV_ELECTRIC_SPARK);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1919
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1920
			break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1921
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1922
	} while ( (v = v->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1923
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1924
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1925
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  1926
static void TrainPlayLeaveStationSound(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1927
{
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1928
	static const SoundFx sfx[] = {
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1929
		SND_04_TRAIN,
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1930
		SND_0A_TRAIN_HORN,
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1931
		SND_0A_TRAIN_HORN
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1932
	};
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  1933
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2475
diff changeset
  1934
	EngineID engtype = v->engine_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1935
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1925
diff changeset
  1936
	switch (GetEngine(engtype)->railtype) {
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  1937
		case RAILTYPE_RAIL:
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  1938
			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
  1939
			break;
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  1940
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  1941
		case RAILTYPE_MONO:
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1942
			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
  1943
			break;
2519
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  1944
beb11a5ac48c (svn r3048) Replace 0/1/2 by RAILTYPE_RAIL/MONO/MAGLEV where appropriate
tron
parents: 2510
diff changeset
  1945
		case RAILTYPE_MAGLEV:
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 308
diff changeset
  1946
			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
  1947
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1948
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1949
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1950
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1951
static bool CheckTrainStayInDepot(Vehicle *v)
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
	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
  1954
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
  1955
	// 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
  1956
	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
  1957
		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
  1958
			return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1959
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1960
	if (v->u.rail.force_proceed == 0) {
2634
0df9396b0067 (svn r3176) Use proper types, not some variants of int
tron
parents: 2630
diff changeset
  1961
		Trackdir trackdir = GetVehicleTrackdir(v);
0df9396b0067 (svn r3176) Use proper types, not some variants of int
tron
parents: 2630
diff changeset
  1962
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
  1963
		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
  1964
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1965
			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
  1966
		}
06c115ce7b7a (svn r1652) Fix: train and road vehicle lists are now redrawn when vehicles enters/leaves a depot (Hackykid)
bjarni
parents: 1137
diff changeset
  1967
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1968
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1969
2164
5523a9711651 (svn r2674) - CodeChange: [pbs] Generalise the PSBISPbsDepot function so it can check if an arbitrary junction is a pbs junction. Preparations for making pbs more safe.
hackykid
parents: 2163
diff changeset
  1970
		if (PBSIsPbsSegment(v->tile, trackdir)) {
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1971
			NPFFindStationOrTileData fstd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1972
			NPFFoundTargetData ftd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1973
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1974
			if (PBSTileUnavail(v->tile) & (1 << trackdir))
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1975
				return true;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1976
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1977
			NPFFillWithOrderData(&fstd, v);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1978
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1979
			DEBUG(pbs, 2) ("pbs: (%i) choose depot (DP), tile:%x, trackdir:%i",v->unitnumber,  v->tile, trackdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1980
			ftd = NPFRouteToStationOrTile(v->tile, trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_GREEN);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1981
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1982
			// we found a way out of the pbs block
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1983
			if (NPFGetFlag(&ftd.node, NPF_FLAG_PBS_EXIT)) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1984
				if (NPFGetFlag(&ftd.node, NPF_FLAG_PBS_BLOCKED) || NPFGetFlag(&ftd.node, NPF_FLAG_PBS_RED))
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1985
					return true;
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1986
				else {
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1987
					v->u.rail.pbs_end_tile = ftd.node.tile;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1988
					v->u.rail.pbs_end_trackdir = ftd.node.direction;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1989
					goto green;
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  1990
				}
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1991
			}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1992
		}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1993
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  1994
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
  1995
		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
  1996
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1997
			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
  1998
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1999
	}
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2000
green:
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
  2001
	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
  2002
	InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2003
	TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2004
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2005
	v->u.rail.track = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2006
	if (v->direction & 2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2007
		v->u.rail.track = 2;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2008
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2009
	v->vehstatus &= ~VS_HIDDEN;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2010
	v->cur_speed = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2011
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2012
	UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2013
	v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2014
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2015
	UpdateSignalsOnSegment(v->tile, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2016
	UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2017
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2018
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2019
	return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2020
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2021
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2022
/* Check for station tiles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2023
typedef struct TrainTrackFollowerData {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2024
	TileIndex dest_coords;
1551
b1c7df6daa63 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2025
	StationID station_index; // station index we're heading for
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2026
	uint best_bird_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2027
	uint best_track_dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2028
	byte best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2029
} TrainTrackFollowerData;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2030
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2031
static bool NtpCallbFindStation(TileIndex tile, TrainTrackFollowerData *ttfd, int track, uint length)
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2032
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2033
	// heading for nowhere?
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2034
	if (ttfd->dest_coords == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2035
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2036
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2037
	// did we reach the final station?
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2038
	if ((ttfd->station_index == INVALID_STATION && tile == ttfd->dest_coords) ||
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  2039
		(IsTileType(tile, MP_STATION) && IS_BYTE_INSIDE(_m[tile].m5, 0, 8) && _m[tile].m2 == ttfd->station_index)) {
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2040
		/* We do not check for dest_coords if we have a station_index,
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2041
		 * because in that case the dest_coords are just an
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2042
		 * approximation of where the station is */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2043
		// found station
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2044
		ttfd->best_track = track;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2045
		return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2046
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2047
		uint dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2048
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2049
		// didn't find station, keep track of the best path so far.
1245
3822f77cbc53 (svn r1749) Move the functions which calculate distances to map.[ch] and give the more meaningful names
tron
parents: 1237
diff changeset
  2050
		dist = DistanceManhattan(tile, ttfd->dest_coords);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2051
		if (dist < ttfd->best_bird_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2052
			ttfd->best_bird_dist = dist;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2053
			ttfd->best_track = track;
0
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
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2056
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2057
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2058
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  2059
static void FillWithStationData(TrainTrackFollowerData* fd, const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2060
{
103
3af99b9373a1 (svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents: 98
diff changeset
  2061
        fd->dest_coords = v->dest_tile;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2062
        if (v->current_order.type == OT_GOTO_STATION)
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2063
                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
  2064
        else
1551
b1c7df6daa63 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2065
                fd->station_index = INVALID_STATION;
103
3af99b9373a1 (svn r104) Fix: wrong pathfinding when northern station tile is missing (blathijs)
dominik
parents: 98
diff changeset
  2066
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2067
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2068
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2069
static const byte _initial_tile_subcoord[6][4][3] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2070
{{ 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
  2071
{{  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
  2072
{{  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
  2073
{{ 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
  2074
{{ 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
  2075
{{  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
  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
static const uint32 _reachable_tracks[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2079
	0x10091009,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2080
	0x00160016,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2081
	0x05200520,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2082
	0x2A002A00,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2083
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2084
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2085
static const byte _search_directions[6][4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2086
	{ 0, 9, 2, 9 }, // track 1
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2087
	{ 9, 1, 9, 3 }, // track 2
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2088
	{ 9, 0, 3, 9 }, // track upper
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2089
	{ 1, 9, 9, 2 }, // track lower
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2090
	{ 3, 2, 9, 9 }, // track left
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2091
	{ 9, 9, 1, 0 }, // track right
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2092
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2093
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2094
static const byte _pick_track_table[6] = {1, 3, 2, 2, 0, 0};
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2095
#if PF_BENCHMARK
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2096
#if !defined(_MSC_VER)
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2477
diff changeset
  2097
unsigned int _rdtsc()
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2098
{
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2099
     unsigned int high, low;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2100
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2101
     __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2102
     return low;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2103
}
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2104
#else
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2477
diff changeset
  2105
#ifndef _M_AMD64
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2477
diff changeset
  2106
static unsigned int _declspec(naked) _rdtsc(void)
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2107
{
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2108
	_asm {
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2109
		rdtsc
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2110
		ret
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2111
	}
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2112
}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2113
#endif
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2114
#endif
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2477
diff changeset
  2115
#endif
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2116
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2117
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2118
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2119
/* choose a track */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2120
static byte ChooseTrainTrack(Vehicle *v, TileIndex tile, int enterdir, TrackdirBits trackdirbits)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2121
{
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2122
	TrainTrackFollowerData fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2123
	uint best_track;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2124
#if PF_BENCHMARK
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2477
diff changeset
  2125
	int time = _rdtsc();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2126
	static float f;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2127
#endif
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2128
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2129
	assert( (trackdirbits & ~0x3F) == 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2130
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2131
	/* quick return in case only one possible track is available */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2132
	if (KILL_FIRST_BIT(trackdirbits) == 0)
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2133
		return FIND_FIRST_BIT(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2134
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2135
	if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2136
		NPFFindStationOrTileData fstd;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2137
		NPFFoundTargetData ftd;
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2138
		Trackdir trackdir;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2139
		uint16 pbs_tracks;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2140
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2141
		NPFFillWithOrderData(&fstd, v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2142
		/* The enterdir for the new tile, is the exitdir for the old tile */
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
  2143
		trackdir = GetVehicleTrackdir(v);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2144
		assert(trackdir != 0xff);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2145
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2146
		pbs_tracks = PBSTileReserved(tile);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2147
		pbs_tracks |= pbs_tracks << 8;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2148
		pbs_tracks &= TrackdirReachesTrackdirs(trackdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2149
		if (pbs_tracks || (v->u.rail.pbs_status == PBS_STAT_NEED_PATH)) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2150
			DEBUG(pbs, 2) ("pbs: (%i) choosefromblock, tile_org:%x tile_dst:%x  trackdir:%i  pbs_tracks:%i",v->unitnumber, tile,tile - TileOffsByDir(enterdir), trackdir, pbs_tracks);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2151
			// clear the currently planned path
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  2152
			if (v->u.rail.pbs_status != PBS_STAT_NEED_PATH) PBSClearPath(tile, FindFirstBit2x64(pbs_tracks), v->u.rail.pbs_end_tile, v->u.rail.pbs_end_trackdir);
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2153
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2154
			// try to find a route to a green exit signal
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2155
			ftd = NPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_ANY);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2156
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  2157
			v->u.rail.pbs_end_tile = ftd.node.tile;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  2158
			v->u.rail.pbs_end_trackdir = ftd.node.direction;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  2159
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2160
		} else
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2161
			ftd = NPFRouteToStationOrTile(tile - TileOffsByDir(enterdir), trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_NONE);
1698
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2162
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2163
		if (ftd.best_trackdir == 0xff) {
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2164
			/* We are already at our target. Just do something */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2165
			//TODO: maybe display error?
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2166
			//TODO: go straight ahead if possible?
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2167
			best_track = FIND_FIRST_BIT(trackdirbits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2168
		} else {
1698
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2169
			/* If ftd.best_bird_dist is 0, we found our target and ftd.best_trackdir contains
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2170
			the direction we need to take to get there, if ftd.best_bird_dist is not 0,
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2171
			we did not find our target, but ftd.best_trackdir contains the direction leading
3f979f9ecf6c (svn r2202) - Fix: [NPF] When a vehicle could not reach its target, it would choose a random direction. It will now try to get as close as possible.
matthijs
parents: 1685
diff changeset
  2172
			to the tile closest to our target. */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2173
			/* Discard enterdir information, making it a normal track */
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2174
			best_track = TrackdirToTrack(ftd.best_trackdir);
0
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
	} else {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2177
		FillWithStationData(&fd, v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2178
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2179
		/* New train pathfinding */
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2180
		fd.best_bird_dist = (uint)-1;
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2181
		fd.best_track_dist = (uint)-1;
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2182
		fd.best_track = 0xFF;
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2183
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2184
		NewTrainPathfind(tile - TileOffsByDir(enterdir), v->dest_tile,
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2185
			enterdir, (NTPEnumProc*)NtpCallbFindStation, &fd);
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2186
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2187
		if (fd.best_track == 0xff) {
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2188
			// blaha
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2189
			best_track = FIND_FIRST_BIT(trackdirbits);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2190
		} else {
2044
df63b9a7dec3 (svn r2553) - Fix: [pathfinding] Remove old-old train pathfinder. Enhanced old pathfinder.
ludde
parents: 2032
diff changeset
  2191
			best_track = fd.best_track & 7;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2192
		}
0
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
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2195
#if PF_BENCHMARK
2482
374f6395847d (svn r3008) [ 1247535 ] Native Support for Win64 (compile&run only) (michi_cc)
Darkvater
parents: 2477
diff changeset
  2196
	time = _rdtsc() - time;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2197
	f = f * 0.99 + 0.01 * time;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2198
	printf("PF time = %d %f\n", time, f);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2199
#endif
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
	return best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2202
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2204
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2205
static bool CheckReverseTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2206
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2207
	TrainTrackFollowerData fd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2208
	int i, r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2209
	int best_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2210
	uint best_bird_dist  = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2211
	uint best_track_dist = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2212
	uint reverse, reverse_best;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2213
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2214
	if (_opt.diff.line_reverse_mode != 0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2215
			v->u.rail.track & 0xC0 ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2216
			!(v->direction & 1))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2217
		return false;
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
	FillWithStationData(&fd, v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2220
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2221
	best_track = -1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2222
	reverse_best = reverse = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2223
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2224
	assert(v->u.rail.track);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2225
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2226
	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
  2227
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2228
	if (_patches.new_pathfinding_all) { /* Use a new pathfinding for everything */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2229
		NPFFindStationOrTileData fstd;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2230
		NPFFoundTargetData ftd;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2231
		byte trackdir, trackdir_rev;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2232
		Vehicle* last = GetLastVehicleInChain(v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2233
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2234
		NPFFillWithOrderData(&fstd, v);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2235
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1698
diff changeset
  2236
		trackdir = GetVehicleTrackdir(v);
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2237
		trackdir_rev = ReverseTrackdir(GetVehicleTrackdir(last));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2238
		assert(trackdir != 0xff);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2239
		assert(trackdir_rev != 0xff);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2240
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2241
		ftd = NPFRouteToStationOrTileTwoWay(v->tile, trackdir, last->tile, trackdir_rev, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_NONE);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2242
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2243
		if (ftd.best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2244
			/* We didn't find anything, just keep on going straight ahead */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2245
			reverse_best = false;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2246
		} else {
1461
699c4d46f59e (svn r1965) - Fix: [NPF] Forgot to update one line where NPFGetFlag() should have been used two commits ago.
matthijs
parents: 1459
diff changeset
  2247
			if (NPFGetFlag(&ftd.node, NPF_FLAG_REVERSE))
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2248
				reverse_best = true;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2249
			else
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2250
				reverse_best = false;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2251
		}
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2252
	} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2253
		while(true) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2254
			fd.best_bird_dist = (uint)-1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2255
			fd.best_track_dist = (uint)-1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2256
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  2257
			NewTrainPathfind(v->tile, v->dest_tile, reverse ^ i, (NTPEnumProc*)NtpCallbFindStation, &fd);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2258
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2259
			if (best_track != -1) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2260
				if (best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2261
					if (fd.best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2262
						/* neither reached the destination, pick the one with the smallest bird dist */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2263
						if (fd.best_bird_dist > best_bird_dist) goto bad;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2264
						if (fd.best_bird_dist < best_bird_dist) goto good;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2265
					} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2266
						/* we found the destination for the first time */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2267
						goto good;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2268
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2269
				} else {
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2270
					if (fd.best_bird_dist != 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2271
						/* didn't find destination, but we've found the destination previously */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2272
						goto bad;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2273
					} else {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2274
						/* both old & new reached the destination, compare track length */
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2275
						if (fd.best_track_dist > best_track_dist) goto bad;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2276
						if (fd.best_track_dist < best_track_dist) goto good;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2277
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2278
				}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2279
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2280
				/* if we reach this position, there's two paths of equal value so far.
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2281
				 * pick one randomly. */
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  2282
				r = GB(Random(), 0, 8);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2283
				if (_pick_track_table[i] == (v->direction & 3)) r += 80;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2284
				if (_pick_track_table[best_track] == (v->direction & 3)) r -= 80;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2285
				if (r <= 127) goto bad;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2286
			}
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2287
good:;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2288
			best_track = i;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2289
			best_bird_dist = fd.best_bird_dist;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2290
			best_track_dist = fd.best_track_dist;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2291
			reverse_best = reverse;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2292
bad:;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2293
			if (reverse != 0)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2294
				break;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2295
			reverse = 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2296
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2297
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2298
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2299
	return reverse_best != 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2300
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2301
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2302
static bool ProcessTrainOrder(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2303
{
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2304
	const Order *order;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2305
	bool result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2306
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2307
	// These are un-interruptible
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2308
	if (v->current_order.type >= OT_GOTO_DEPOT &&
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2309
			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
  2310
		// Let a depot order in the orderlist interrupt.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2311
		if (v->current_order.type != OT_GOTO_DEPOT ||
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2312
				!(v->current_order.flags & OF_UNLOAD))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2313
			return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2314
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2315
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2316
	if (v->current_order.type == OT_GOTO_DEPOT &&
2214
056bbc3c8236 (svn r2732) -Codechange: Cleaned the order flags some more. Hit some inconsistencies while trying to locate bug 1244167
celestar
parents: 2186
diff changeset
  2317
			(v->current_order.flags & (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED)) ==  (OF_PART_OF_ORDERS | OF_SERVICE_IF_NEEDED) &&
1520
d88442095697 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1507
diff changeset
  2318
			!VehicleNeedsService(v)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2319
		v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2320
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2321
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
  2322
	// check if we've reached the waypoint?
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2323
	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
  2324
		v->cur_order_index++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2325
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2326
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2327
	// check if we've reached a non-stop station while TTDPatch nonstop is enabled..
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2328
	if (_patches.new_nonstop &&
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2329
			v->current_order.flags & OF_NON_STOP &&
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2330
			IsTileType(v->tile, MP_STATION) &&
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2331
			v->current_order.station == _m[v->tile].m2) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2332
		v->cur_order_index++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2333
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2334
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2335
	// Get the current order
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2336
	if (v->cur_order_index >= v->num_orders)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2337
		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
  2338
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2339
	order = GetVehicleOrder(v, v->cur_order_index);
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
	// 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
  2342
	if (order == NULL) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2343
		v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2344
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2345
		v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2346
		return false;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2349
	// 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
  2350
	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
  2351
			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
  2352
			order->station == v->current_order.station)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2353
		return false;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2354
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2355
	// 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
  2356
	v->current_order = *order;
0
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
	v->dest_tile = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2359
2617
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2360
	// store the station length if no shorter station was visited this order round
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2361
	if (v->cur_order_index == 0) {
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2362
		if (v->u.rail.shortest_platform[1] != 0 && v->u.rail.shortest_platform[1] != 255) {
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2363
			// we went though a whole round of orders without interruptions, so we store the length of the shortest station
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2364
			v->u.rail.shortest_platform[0] = v->u.rail.shortest_platform[1];
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2365
		}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2366
		// all platforms are shorter than 255, so now we can find the shortest in the next order round. They might have changed size
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2367
		v->u.rail.shortest_platform[1] = 255;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2368
	}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2369
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2370
	if (v->last_station_visited != INVALID_STATION) {
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2371
		Station *st = GetStation(v->last_station_visited);
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2372
		if (TileBelongsToRailStation(st, v->tile)) {
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2373
			byte length = GetStationPlatforms(st, v->tile);
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2374
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2375
			if (length < v->u.rail.shortest_platform[1]) {
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2376
				v->u.rail.shortest_platform[1] = length;
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2377
			}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2378
		}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2379
	}
03120a0b1e39 (svn r3155) -Feature: [autoreplace] autoreplace can now remove cars from too long trains
bjarni
parents: 2612
diff changeset
  2380
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2381
	result = false;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2382
	switch (order->type) {
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2383
		case OT_GOTO_STATION:
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2384
			if (order->station == v->last_station_visited)
1266
eccd576e322f (svn r1770) -Fix: Hopefully last pieces of code that are containing a station-id
truelight
parents: 1247
diff changeset
  2385
				v->last_station_visited = INVALID_STATION;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2386
			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
  2387
			result = CheckReverseTrain(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2388
			break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2389
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2390
		case OT_GOTO_DEPOT:
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
  2391
			v->dest_tile = GetDepot(order->station)->xy;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2392
			result = CheckReverseTrain(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2393
			break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2394
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2395
		case OT_GOTO_WAYPOINT:
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1531
diff changeset
  2396
			v->dest_tile = GetWaypoint(order->station)->xy;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2397
			result = CheckReverseTrain(v);
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2398
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2399
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2400
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2401
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2402
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2403
	return result;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2404
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2405
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2406
static void MarkTrainDirty(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2407
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2408
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2409
		v->cur_image = GetTrainImage(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2410
		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
  2411
	} while ( (v=v->next) != NULL);
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
static void HandleTrainLoading(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2415
{
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2416
	if (v->current_order.type == OT_NOTHING)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2417
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2418
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2419
	if (v->current_order.type != OT_DUMMY) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2420
		if (v->current_order.type != OT_LOADING)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2421
			return;
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
		if (mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2424
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2425
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2426
		// 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
  2427
		if (v->current_order.flags & OF_NON_STOP)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2428
			v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2429
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2430
		if (--v->load_unload_time_rem)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2431
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2432
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2433
		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
  2434
			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
  2435
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2436
			if (LoadUnloadVehicle(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2437
				InvalidateWindow(WC_TRAINS_LIST, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2438
				MarkTrainDirty(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2439
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  2440
				// need to update acceleration and cached values since the goods on the train changed.
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  2441
				TrainCargoChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2442
				UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2443
			}
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
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2446
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2447
		TrainPlayLeaveStationSound(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2448
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2449
		{
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2450
			Order b = v->current_order;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2451
			v->current_order.type = OT_LEAVESTATION;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2452
			v->current_order.flags = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2453
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2454
			// 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
  2455
			if (!(b.flags & OF_NON_STOP))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2456
				return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2457
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2458
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2459
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2460
	v->u.rail.days_since_order_progr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2461
	v->cur_order_index++;
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  2462
	InvalidateVehicleOrder(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2463
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2464
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2465
static int UpdateTrainSpeed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2466
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2467
	uint spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2468
	uint accel;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2469
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2470
	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
  2471
		if (_patches.realistic_acceleration)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2472
			accel = GetTrainAcceleration(v, AM_BRAKE) * 2;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2473
		else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2474
			accel = v->acceleration * -2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2475
	} else {
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2476
		if (_patches.realistic_acceleration)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2477
			accel = GetTrainAcceleration(v, AM_ACCEL);
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2478
		else
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2479
			accel = v->acceleration;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2480
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2481
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2482
	spd = v->subspeed + accel * 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2483
	v->subspeed = (byte)spd;
1179
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2484
	{
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2485
		int tempmax = v->max_speed;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2486
		if (v->cur_speed > v->max_speed)
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2487
			tempmax = v->cur_speed - (v->cur_speed / 10) - 1;
ce0a75460808 (svn r1681) -Feature: New realistic acceleration.
celestar
parents: 1174
diff changeset
  2488
		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
  2489
	}
0
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
	if (!(v->direction & 1)) spd = spd * 3 >> 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2492
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2493
	spd += v->progress;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2494
	v->progress = (byte)spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2495
	return (spd >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2496
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2497
1551
b1c7df6daa63 (svn r2055) -CodeChange: Begun introducting StationID
celestar
parents: 1542
diff changeset
  2498
static void TrainEnterStation(Vehicle *v, StationID station)
0
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
	Station *st;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2501
	uint32 flags;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2502
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2503
	v->last_station_visited = station;
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
	/* 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
  2506
	st = GetStation(station);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2507
	if (!(st->had_vehicle_of_type & HVOT_TRAIN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2508
		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
  2509
		SetDParam(0, st->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2510
		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
  2511
		AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2512
			STR_8801_CITIZENS_CELEBRATE_FIRST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2513
			flags,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2514
			v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2515
			0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2516
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2517
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2518
	// Did we reach the final destination?
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2519
	if (v->current_order.type == OT_GOTO_STATION &&
1552
1da3dae846b7 (svn r2056) -Fix: [ 1166999 ] Strange cast removed. Thanks Tron
celestar
parents: 1551
diff changeset
  2520
			v->current_order.station == station) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2521
		// Yeah, keep the load/unload flags
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2522
		// 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
  2523
		v->current_order.type = OT_LOADING;
1935
164d58b9137c (svn r2441) -Feature: You can now give transfer order to set up feeder systems
celestar
parents: 1934
diff changeset
  2524
		v->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER;
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2525
		v->current_order.flags |= OF_NON_STOP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2526
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2527
		// No, just do a simple load
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2528
		v->current_order.type = OT_LOADING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2529
		v->current_order.flags = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2530
	}
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2531
	v->current_order.station = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2532
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2533
	SET_EXPENSES_TYPE(EXPENSES_TRAIN_INC);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2534
	if (LoadUnloadVehicle(v) != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2535
		InvalidateWindow(WC_TRAINS_LIST, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2536
		MarkTrainDirty(v);
1905
c53f8d71f9b6 (svn r2411) - Codechange: Have trains cache stuff like consist power/weight/max speed instead of recalculating it each time.
hackykid
parents: 1901
diff changeset
  2537
		TrainCargoChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2538
		UpdateTrainAcceleration(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2539
	}
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
  2540
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
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
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2543
static byte AfterSetTrainPos(Vehicle *v, bool new_tile)
0
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
	byte new_z, old_z;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2546
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2547
	// 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
  2548
	_get_z_hint = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2549
	new_z = GetSlopeZ(v->x_pos, v->y_pos);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2550
	_get_z_hint = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2551
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2552
	old_z = v->z_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2553
	v->z_pos = new_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2554
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2555
	if (new_tile) {
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2556
		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
  2557
		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
  2558
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2559
		if (new_z != old_z) {
1980
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
  2560
			TileIndex tile = TileVirtXY(v->x_pos, v->y_pos);
1683
697f47e2bb59 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
  2561
697f47e2bb59 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
  2562
			// XXX workaround, whole UP/DOWN detection needs overhaul
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  2563
			if (!IsTileType(tile, MP_TUNNELBRIDGE) || (_m[tile].m5 & 0x80) != 0)
1683
697f47e2bb59 (svn r2187) Implement a slightly better workaround than r1588 did. (Joint effort with Celestar)
tron
parents: 1681
diff changeset
  2564
				SETBIT(v->u.rail.flags, (new_z > old_z) ? VRF_GOINGUP : VRF_GOINGDOWN);
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  2565
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2566
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2567
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2568
	VehiclePositionChanged(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2569
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2570
	return old_z;
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
static const byte _new_vehicle_direction_table[11] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2574
	0, 7, 6, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2575
	1, 0, 5, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2576
	2, 3, 4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2577
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2578
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2579
static int GetNewVehicleDirectionByTile(TileIndex new_tile, TileIndex old_tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2580
{
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
  2581
	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
  2582
							TileX(new_tile) - TileX(old_tile) + 1;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2583
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2584
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2585
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2586
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
  2587
static int GetNewVehicleDirection(const Vehicle *v, int x, int y)
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
	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
  2590
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2591
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2592
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2593
2436
7d5df545bd5d (svn r2962) - const correctness for all Get* functions and most Draw* functions that don't change their pointer parameters
Darkvater
parents: 2425
diff changeset
  2594
static int GetDirectionToVehicle(const Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2595
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2596
	byte offs;
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
	x -= v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2599
	if (x >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2600
		offs = (x > 2) ? 0 : 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2601
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2602
		offs = (x < -2) ? 2 : 1;
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
	y -= v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2606
	if (y >= 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2607
		offs += ((y > 2) ? 0 : 1) * 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2608
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2609
		offs += ((y < -2) ? 2 : 1) * 4;
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
	assert(offs < 11);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2613
	return _new_vehicle_direction_table[offs];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2614
}
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
/* Check if the vehicle is compatible with the specified tile */
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2617
static bool CheckCompatibleRail(const Vehicle *v, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2618
{
1214
8262981ac274 (svn r1718) Use the enum TileType as parameter/return type for [GS]etTileType() instead of plain int.
tron
parents: 1209
diff changeset
  2619
	switch (GetTileType(tile)) {
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2620
		case MP_RAILWAY:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2621
		case MP_STATION:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2622
			// normal tracks, jump to owner check
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2623
			break;
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2624
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2625
		case MP_TUNNELBRIDGE:
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  2626
			if ((_m[tile].m5 & 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
  2627
				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
  2628
				uint tileh = GetTileSlope(tile, &height);
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2629
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2630
				// correct Z position of a train going under a bridge on slopes
1394
79cb56d80a3a (svn r1898) Remove some unused macros from macros.h and move some others to more appropriate headers
tron
parents: 1387
diff changeset
  2631
				if (CorrectZ(tileh)) height += 8;
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
  2632
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
  2633
				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
  2634
			}
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2635
			break;
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2636
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2637
		case MP_STREET:
2360
4e4ebe18e448 (svn r2886) Rename the "owner" attribute to "m1", because when it stores an owner it is accessed by [GS]etOwner anyway and when it doesn't store an owner, but arbitrary data, accessing a field called "owner" is confusing.
tron
parents: 2261
diff changeset
  2638
			// tracks over roads, do owner check of tracks
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2639
			return
2510
0008aad5d311 (svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents: 2493
diff changeset
  2640
				IsTileOwner(tile, v->owner) && (
0008aad5d311 (svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents: 2493
diff changeset
  2641
					v->subtype != TS_Front_Engine ||
0008aad5d311 (svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents: 2493
diff changeset
  2642
					IsCompatibleRail(v->u.rail.railtype, GB(_m[tile].m4, 0, 4))
0008aad5d311 (svn r3036) Use IsCompatibleRail() to check the rail type of level crossings (not a fix, because the old == method works so far)
tron
parents: 2493
diff changeset
  2643
				);
1048
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2644
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2645
		default:
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2646
			return true;
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2647
	}
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2648
8611c5c02dcb (svn r1549) Clean up some functions:
tron
parents: 1035
diff changeset
  2649
	return
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2650
		IsTileOwner(tile, v->owner) && (
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2651
			v->subtype != TS_Front_Engine ||
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2652
			IsCompatibleRail(v->u.rail.railtype, GetRailType(tile))
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2653
		);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2654
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2655
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2656
typedef struct {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2657
	byte small_turn, large_turn;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2658
	byte z_up; // fraction to remove when moving up
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2659
	byte z_down; // fraction to remove when moving down
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2660
} RailtypeSlowdownParams;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2661
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2662
static const RailtypeSlowdownParams _railtype_slowdown[3] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2663
	// normal accel
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2664
	{256/4, 256/2, 256/4, 2}, // normal
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2665
	{256/4, 256/2, 256/4, 2}, // monorail
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2666
	{0,     256/2, 256/4, 2}, // maglev
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2667
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2668
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2669
/* 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
  2670
static void AffectSpeedByDirChange(Vehicle *v, byte new_dir)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2671
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2672
	byte diff;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2673
	const RailtypeSlowdownParams *rsp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2674
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2675
	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
  2676
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2677
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2678
	rsp = &_railtype_slowdown[v->u.rail.railtype];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2679
	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
  2680
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2681
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2682
/* 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
  2683
static void AffectSpeedByZChange(Vehicle *v, byte old_z)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2684
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2685
	const RailtypeSlowdownParams *rsp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2686
	if (old_z == v->z_pos || _patches.realistic_acceleration)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2687
		return;
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
	rsp = &_railtype_slowdown[v->u.rail.railtype];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2690
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2691
	if (old_z < v->z_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2692
		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
  2693
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2694
		uint16 spd = v->cur_speed + rsp->z_down;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2695
		if (spd <= v->max_speed)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2696
			v->cur_speed = spd;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2697
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2698
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2699
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2700
static const byte _otherside_signal_directions[14] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2701
	1, 3, 1, 3, 5, 3, 0, 0,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2702
	5, 7, 7, 5, 7, 1,
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
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  2705
static void TrainMovedChangeSignals(TileIndex tile, int dir)
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
	int i;
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  2708
	if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xC0) == 0x40) {
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  2709
		i = FindFirstBit2x64((_m[tile].m5+(_m[tile].m5<<8)) & _reachable_tracks[dir]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2710
		UpdateSignalsOnSegment(tile, _otherside_signal_directions[i]);
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2714
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2715
typedef struct TrainCollideChecker {
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2716
	const Vehicle *v;
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2717
	const Vehicle *v_skip;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2718
} TrainCollideChecker;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2719
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2720
static void *FindTrainCollideEnum(Vehicle *v, void *data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2721
{
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2722
	const TrainCollideChecker* tcc = data;
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2723
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2724
	if (v != tcc->v &&
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2725
			v != tcc->v_skip &&
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2726
			v->type == VEH_Train &&
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2727
			v->u.rail.track != 0x80 &&
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2728
			myabs(v->z_pos - tcc->v->z_pos) <= 6 &&
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2729
			myabs(v->x_pos - tcc->v->x_pos) < 6 &&
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2730
			myabs(v->y_pos - tcc->v->y_pos) < 6) {
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2731
		return v;
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2732
	} else {
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2733
		return NULL;
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2734
	}
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2737
static void SetVehicleCrashed(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2738
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2739
	Vehicle *u;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2740
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2741
	if (v->u.rail.crash_anim_pos != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2742
		return;
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
	v->u.rail.crash_anim_pos++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2745
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2746
	u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2747
	BEGIN_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2748
		v->vehstatus |= VS_CRASHED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2749
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2750
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
  2751
	InvalidateWindowWidget(WC_VEHICLE_VIEW, u->index, STATUS_BAR);
0
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
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2754
static uint CountPassengersInTrain(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2755
{
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  2756
	uint num = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2757
	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
  2758
		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
  2759
	END_ENUM_WAGONS(v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2760
	return num;
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
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2763
/*
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2764
 * Checks whether the specified train 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
  2765
 * 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
  2766
 * 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
  2767
 * plays a sound.
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2768
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2769
static void CheckTrainCollision(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2770
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2771
	TrainCollideChecker tcc;
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2772
	Vehicle *coll;
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2773
	Vehicle *realcoll;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2774
	int num;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2775
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2776
	/* can't collide in depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2777
	if (v->u.rail.track == 0x80)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2778
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2779
1980
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
  2780
	assert(v->u.rail.track == 0x40 || TileVirtXY(v->x_pos, v->y_pos) == v->tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2781
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2782
	tcc.v = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2783
	tcc.v_skip = v->next;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2784
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2785
	/* find colliding vehicle */
1980
6c5917cfcb78 (svn r2486) Turn TILE_FROM_XY into an inline function and rename it to TileVirtXY
tron
parents: 1977
diff changeset
  2786
	realcoll = VehicleFromPos(TileVirtXY(v->x_pos, v->y_pos), &tcc, FindTrainCollideEnum);
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2787
	if (realcoll == NULL)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2788
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2789
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2790
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2791
	coll = GetFirstVehicleInChain(realcoll);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2792
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2793
	/* it can't collide with its own wagons */
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2794
	if (v == coll ||
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2795
			(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
  2796
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2797
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2798
	//two drivers + passangers killed in train v
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2799
	num = 2 + CountPassengersInTrain(v);
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2800
	if (!(coll->vehstatus & VS_CRASHED))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2801
		//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
  2802
		num += 2 + CountPassengersInTrain(coll);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2803
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2804
	SetVehicleCrashed(v);
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  2805
	if (coll->subtype == TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2806
		SetVehicleCrashed(coll);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2807
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2808
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  2809
	SetDParam(0, num);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2810
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2811
	AddNewsItem(STR_8868_TRAIN_CRASH_DIE_IN_FIREBALL,
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2812
		NEWS_FLAGS(NM_THIN, NF_VIEWPORT | NF_VEHICLE, NT_ACCIDENT, 0),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2813
		v->index,
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2814
		0
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  2815
	);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2816
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2817
	ModifyStationRatingAround(v->tile, v->owner, -160, 30);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  2818
	SndPlayVehicleFx(SND_13_BIG_CRASH, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2819
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2820
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2821
typedef struct VehicleAtSignalData {
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2822
	TileIndex tile;
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2823
	byte direction;
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2824
} VehicleAtSignalData;
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2825
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2826
static void *CheckVehicleAtSignal(Vehicle *v, void *data)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2827
{
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2828
	const VehicleAtSignalData* vasd = data;
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2829
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2830
	if (v->type == VEH_Train && v->subtype == TS_Front_Engine &&
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2831
			v->tile == vasd->tile) {
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2832
		byte diff = (v->direction - vasd->direction + 2) & 7;
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2833
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2834
		if (diff == 2 || (v->cur_speed <= 5 && diff <= 4))
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2835
			return v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2836
	}
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  2837
	return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2838
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2839
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2840
static void TrainController(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2841
{
1961
60efcaa14311 (svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents: 1942
diff changeset
  2842
	Vehicle *prev;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2843
	GetNewVehiclePosResult gp;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2844
	uint32 r, tracks,ts;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2845
	int i, enterdir, newdir, dir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2846
	byte chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2847
	byte chosen_track;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2848
	byte old_z;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2849
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2850
	/* For every vehicle after and including the given vehicle */
1961
60efcaa14311 (svn r2467) - Fix: [newgrf] Prevent trains with shorter wagons breaking apart when reversing in some cases. (Therax)
hackykid
parents: 1942
diff changeset
  2851
	for (prev = GetPrevVehicleInChain(v); v != NULL; prev = v, v = v->next) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2852
		BeginVehicleMove(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2853
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2854
		if (v->u.rail.track != 0x40) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2855
			/* Not inside tunnel */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2856
			if (GetNewVehiclePos(v, &gp)) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2857
				/* Staying in the old tile */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2858
				if (v->u.rail.track == 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2859
					/* inside depot */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2860
					gp.x = v->x_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2861
					gp.y = v->y_pos;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2862
				} else {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  2863
					/* 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
  2864
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2865
					if ((prev == NULL) && (!TrainCheckIfLineEnds(v)))
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2866
						return;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  2867
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2868
					r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2869
					if (r & 0x8) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2870
						//debug("%x & 0x8", r);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2871
						goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2872
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2873
					if (r & 0x2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2874
						TrainEnterStation(v, r >> 8);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2875
						return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2876
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2877
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2878
					if (v->current_order.type == OT_LEAVESTATION) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2879
						v->current_order.type = OT_NOTHING;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  2880
						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
  2881
						InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2882
					}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2883
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2884
			} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2885
				/* A new tile is about to be entered. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2886
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2887
				byte bits;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2888
				/* 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
  2889
				dir = GetNewVehicleDirectionByTile(gp.new_tile, gp.old_tile);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2890
				enterdir = dir >> 1;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2891
				assert(enterdir==0 || enterdir==1 || enterdir==2 || enterdir==3);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2892
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2893
				/* 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
  2894
				 * away the bits that aren't reachable. */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2895
				ts = GetTileTrackStatus(gp.new_tile, TRANSPORT_RAIL) & _reachable_tracks[enterdir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2896
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2897
				/* Combine the from & to directions.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2898
				 * 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
  2899
				 * the signal status. */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2900
				tracks = ts|(ts >> 8);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2901
				bits = tracks & 0xFF;
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2902
				if (_patches.new_pathfinding_all && _patches.forbid_90_deg && prev == NULL)
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2903
					/* We allow wagons to make 90 deg turns, because forbid_90_deg
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2904
					 * can be switched on halfway a turn */
1942
c5d5cf5b0263 (svn r2448) General cleanup of rail related code, more to follow.
matthijs
parents: 1935
diff changeset
  2905
					bits &= ~TrackCrossesTracks(FIND_FIRST_BIT(v->u.rail.track));
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2906
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2907
				if ( bits == 0) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2908
					//debug("%x == 0", bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2909
					goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2910
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2911
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2912
				/* 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
  2913
				 * with the current train, if not, bail out. */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2914
				if (!CheckCompatibleRail(v, gp.new_tile)) {
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2915
					//debug("!CheckCompatibleRail(%p, %x)", v, gp.new_tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2916
					goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2917
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2918
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2919
				if (prev == NULL) {
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2920
					byte trackdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2921
					/* 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
  2922
					 * available tracks to choose */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2923
					chosen_track = 1 << ChooseTrainTrack(v, gp.new_tile, enterdir, bits);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2924
					assert(chosen_track & tracks);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2925
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2926
					trackdir = TrackEnterdirToTrackdir(FIND_FIRST_BIT(chosen_track), enterdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2927
					assert(trackdir != 0xff);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2928
2451
f7d6ed0f564d (svn r2977) - CodeChange: [pbs] Use a check to see if a rail segment is a pbs segment, instead of letting the pathfinder determine this.
hackykid
parents: 2441
diff changeset
  2929
					if (PBSIsPbsSignal(gp.new_tile,trackdir) && PBSIsPbsSegment(gp.new_tile,trackdir)) {
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2930
						// encountered a pbs signal, and possible a pbs block
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2931
						DEBUG(pbs, 3) ("pbs: (%i) arrive AT signal, tile:%x  pbs_stat:%i",v->unitnumber, gp.new_tile, v->u.rail.pbs_status);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2932
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2933
						if (v->u.rail.pbs_status == PBS_STAT_NONE) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2934
							// we havent planned a path already, so try to find one now
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2935
							NPFFindStationOrTileData fstd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2936
							NPFFoundTargetData ftd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2937
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2938
							NPFFillWithOrderData(&fstd, v);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2939
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2940
							DEBUG(pbs, 2) ("pbs: (%i) choose signal (TC), tile:%x, trackdir:%i",v->unitnumber,  gp.new_tile, trackdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2941
							ftd = NPFRouteToStationOrTile(gp.new_tile, trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_GREEN);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2942
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2943
							if (v->u.rail.force_proceed != 0)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2944
								goto green_light;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2945
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2946
							if (ftd.best_trackdir == 0xFF)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2947
								goto red_light;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2948
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2949
							// we found a way out of the pbs block
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2950
							if (NPFGetFlag(&ftd.node, NPF_FLAG_PBS_EXIT)) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2951
								if (NPFGetFlag(&ftd.node, NPF_FLAG_PBS_BLOCKED) || NPFGetFlag(&ftd.node, NPF_FLAG_PBS_RED))
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2952
									goto red_light;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2953
								else {
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  2954
									v->u.rail.pbs_end_tile = ftd.node.tile;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  2955
									v->u.rail.pbs_end_trackdir = ftd.node.direction;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2956
									goto green_light;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2957
								}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2958
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2959
							};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2960
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2961
						} else {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2962
							// we have already planned a path through this pbs block
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2963
							// on entering the block, we reset our status
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2964
							v->u.rail.pbs_status = PBS_STAT_NONE;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2965
							goto green_light;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2966
						};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2967
						DEBUG(pbs, 3) ("pbs: (%i) no green light found, or was no pbs-block",v->unitnumber);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2968
					};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2969
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2970
					/* 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
  2971
					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
  2972
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2973
					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
  2974
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2975
					/* The wagon is active, simply follow the prev vehicle. */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2976
					chosen_track = (byte)(_matching_tracks[GetDirectionToVehicle(prev, gp.x, gp.y)] & bits);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2977
				}
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2978
green_light:
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2979
				if (v->next == NULL)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  2980
					PBSClearTrack(gp.old_tile, FIND_FIRST_BIT(v->u.rail.track));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2981
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2982
				/* make sure chosen track is a valid track */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2983
				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
  2984
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2985
				/* 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
  2986
				{
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2987
					const byte *b = _initial_tile_subcoord[FIND_FIRST_BIT(chosen_track)][enterdir];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2988
					gp.x = (gp.x & ~0xF) | b[0];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2989
					gp.y = (gp.y & ~0xF) | b[1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2990
					chosen_dir = b[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2991
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  2992
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2993
				/* 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
  2994
				r = VehicleEnterTile(v, gp.new_tile, gp.x, gp.y);
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2995
				if (r&0x8){
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2996
					//debug("%x & 0x8", r);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2997
					goto invalid_rail;
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  2998
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2999
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3000
				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
  3001
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3002
				if (!(r&0x4)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3003
					v->tile = gp.new_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3004
					v->u.rail.track = chosen_track;
1330
5d76a0522a11 (svn r1834) - Fix: NPF does not check the owner of its target, busses try to enter other players' depots. TODO
matthijs
parents: 1313
diff changeset
  3005
					assert(v->u.rail.track);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3006
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3007
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3008
				if (v->subtype == TS_Front_Engine)
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3009
				TrainMovedChangeSignals(gp.new_tile, enterdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3010
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3011
				/* Signals can only change when the first
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3012
				 * (above) or the last vehicle moves. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3013
				if (v->next == NULL)
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3014
				TrainMovedChangeSignals(gp.old_tile, (enterdir) ^ 2);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3015
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3016
				if (prev == NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3017
					AffectSpeedByDirChange(v, chosen_dir);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3018
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3019
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3020
				v->direction = chosen_dir;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3021
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3022
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3023
			/* in tunnel */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3024
			GetNewVehiclePos(v, &gp);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3025
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3026
			// Check if to exit the tunnel...
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3027
			if (!IsTunnelTile(gp.new_tile) ||
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3028
					!(VehicleEnterTile(v, gp.new_tile, gp.x, gp.y)&0x4) ) {
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3029
				v->x_pos = gp.x;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3030
				v->y_pos = gp.y;
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3031
				VehiclePositionChanged(v);
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2115
diff changeset
  3032
				continue;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3033
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3034
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3035
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3036
		/* update image of train, as well as delta XY */
1247
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3037
		newdir = GetNewVehicleDirection(v, gp.x, gp.y);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3038
		UpdateTrainDeltaXY(v, newdir);
3851739bfd09 (svn r1751) - Feature: New PathFinder (NPF).
matthijs
parents: 1245
diff changeset
  3039
		v->cur_image = GetTrainImage(v, newdir);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3040
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3041
		v->x_pos = gp.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3042
		v->y_pos = gp.y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3043
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3044
		/* 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
  3045
		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
  3046
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3047
		if (prev == NULL) {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3048
			/* This is the first vehicle in the train */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3049
			AffectSpeedByZChange(v, old_z);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3050
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3051
	}
1438
98d01c6873f7 (svn r1942) Fix r1938
tron
parents: 1434
diff changeset
  3052
	return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3053
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3054
invalid_rail:
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3055
	/* We've reached end of line?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3056
	if (prev != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3057
		error("!Disconnecting train");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3058
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3059
	goto reverse_train_direction;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3060
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3061
red_light: {
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3062
	/* We're in front of a red signal ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3063
		/* 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
  3064
		 * 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
  3065
		i = FindFirstBit2x64(ts);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3066
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  3067
		if (!(_m[gp.new_tile].m3 & SignalAgainstTrackdir(i))) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3068
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3069
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3070
			v->progress = 255-100;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3071
			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
  3072
				return;
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  3073
		} else if (_m[gp.new_tile].m3 & SignalAlongTrackdir(i)){
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3074
			v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3075
			v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3076
			v->progress = 255-10;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3077
			if (++v->load_unload_time_rem < _patches.wait_twoway_signal * 73) {
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3078
				TileIndex o_tile = gp.new_tile + TileOffsByDir(enterdir);
1507
18cb39ddddfd (svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents: 1475
diff changeset
  3079
				VehicleAtSignalData vasd;
18cb39ddddfd (svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents: 1475
diff changeset
  3080
				vasd.tile = o_tile;
18cb39ddddfd (svn r2011) - Fix: [ 1162209 ] Fix OS/2 build (orudge) #2
darkvater
parents: 1475
diff changeset
  3081
				vasd.direction = dir ^ 4;
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3082
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3083
				/* check if a train is waiting on the other side */
1432
768af765b5be (svn r1936) End some void-pointer-as-int-abuse; this also fixes a latent bug where a TileIndex was only 24bit wide (on 32bit architectures)
tron
parents: 1431
diff changeset
  3084
				if (VehicleFromPos(o_tile, &vasd, CheckVehicleAtSignal) == NULL)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3085
					return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3086
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3087
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3088
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3089
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3090
reverse_train_direction:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3091
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3092
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3093
	v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3094
	ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3095
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3096
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3097
1430
967f56d158a5 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3098
extern TileIndex CheckTunnelBusy(TileIndex tile, uint *length);
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3099
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3100
/**
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3101
 * Deletes/Clears the last wagon of a crashed train. It takes the engine of the
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3102
 * train, then goes to the last wagon and deletes that. Each call to this function
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3103
 * will remove the last wagon of a crashed train. If this wagon was on a crossing,
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3104
 * or inside a tunnel, recalculate the signals as they might need updating
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3105
 * @param v the @Vehicle of which last wagon is to be removed
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3106
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3107
static void DeleteLastWagon(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3108
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3109
	Vehicle *u = v;
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3110
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3111
	/* Go to the last wagon and delete the link pointing there
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3112
	 * *u is then the one-before-last wagon, and *v the last
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3113
	 * one which will physicially be removed */
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3114
	for (; v->next != NULL; v = v->next) u = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3115
	u->next = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3116
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3117
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3118
	DeleteWindowById(WC_VEHICLE_VIEW, v->index);
588
03521b270f62 (svn r1009) -Feature: per-station vehicle lists
tron
parents: 578
diff changeset
  3119
	RebuildVehicleLists();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3120
	InvalidateWindow(WC_COMPANY, v->owner);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3121
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3122
	BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3123
	EndVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3124
	DeleteVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3125
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3126
	// clear up reserved pbs tracks
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3127
	if (PBSTileReserved(v->tile) & v->u.rail.track) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3128
		if (v == u) {
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  3129
			PBSClearPath(v->tile, FIND_FIRST_BIT(v->u.rail.track), v->u.rail.pbs_end_tile, v->u.rail.pbs_end_trackdir);
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  3130
			PBSClearPath(v->tile, FIND_FIRST_BIT(v->u.rail.track) + 8, v->u.rail.pbs_end_tile, v->u.rail.pbs_end_trackdir);
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3131
		};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3132
		if (v->tile != u->tile) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3133
			PBSClearTrack(v->tile, FIND_FIRST_BIT(v->u.rail.track));
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3134
		};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3135
	}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3136
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3137
	if (!(v->u.rail.track & 0xC0))
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3138
		SetSignalsOnBothDir(v->tile, FIND_FIRST_BIT(v->u.rail.track));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3139
1434
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3140
	/* Check if the wagon was on a road/rail-crossing and disable it if no
e3f4a55b8bcc (svn r1938) Miscellaneous cleanups: const correctness, kill a goto, ...
tron
parents: 1432
diff changeset
  3141
	 * others are on it */
1103
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  3142
	DisableTrainCrossing(v->tile);
530d0a75f91c (svn r1604) Fix: [ 1105112 ] Destroyed train locks crossings
dominik
parents: 1095
diff changeset
  3143
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3144
	if (v->u.rail.track == 0x40) { // inside a tunnel
1430
967f56d158a5 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3145
		TileIndex endtile = CheckTunnelBusy(v->tile, NULL);
967f56d158a5 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3146
967f56d158a5 (svn r1934) Small cleanup (uint -> TileIndex, (uint)-1 -> INVALID_TILE and similar stuff)
tron
parents: 1418
diff changeset
  3147
		if (endtile == INVALID_TILE) // tunnel is busy (error returned)
1418
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3148
			return;
f71b68950a6f (svn r1922) - Fix: Disappearing of crashed trains inside tunnels were not checked properly.
Darkvater
parents: 1394
diff changeset
  3149
1431
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3150
		switch (v->direction) {
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3151
			case 1:
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3152
			case 5:
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3153
				SetSignalsOnBothDir(v->tile, 0);
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3154
				SetSignalsOnBothDir(endtile, 0);
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3155
				break;
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3156
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3157
			case 3:
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3158
			case 7:
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3159
				SetSignalsOnBothDir(v->tile, 1);
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3160
				SetSignalsOnBothDir(endtile, 1);
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3161
				break;
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3162
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3163
			default:
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3164
				break;
37f7348421d0 (svn r1935) Missing braces; while here turn the ifs into a single switch
tron
parents: 1430
diff changeset
  3165
		}
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3166
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3167
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3168
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3169
static void ChangeTrainDirRandomly(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3170
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3171
	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
  3172
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3173
	do {
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3174
		//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
  3175
		if (!(v->u.rail.track & 0x40))
2635
7ed07303448d (svn r3177) GB, CLRBIT, HASBIT, TOGGLEBIT
tron
parents: 2634
diff changeset
  3176
			v->direction = (v->direction + _random_dir_change[GB(Random(), 0, 2)]) & 7;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3177
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3178
			BeginVehicleMove(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3179
			UpdateTrainDeltaXY(v, v->direction);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3180
			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
  3181
			AfterSetTrainPos(v, false);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3182
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3183
	} while ( (v=v->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3184
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3185
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3186
static void HandleCrashedTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3187
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3188
	int state = ++v->u.rail.crash_anim_pos, index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3189
	uint32 r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3190
	Vehicle *u;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3191
98
91d63b83cece (svn r99) Fix: [992726] No tunnel crash (Celestar)
dominik
parents: 76
diff changeset
  3192
	if ( (state == 4) && (v->u.rail.track != 0x40) ) {
1359
52782e5cf7c9 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  3193
		CreateEffectVehicleRel(v, 4, 4, 8, EV_EXPLOSION_LARGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3194
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3195
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3196
	if (state <= 200 && GB(r = Random(), 0, 16) <= 0x2492) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3197
		index = (r * 10 >> 16);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3198
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3199
		u = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3200
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3201
			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
  3202
				r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3203
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3204
				CreateEffectVehicleRel(u,
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2135
diff changeset
  3205
					GB(r,  8, 3) + 2,
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2135
diff changeset
  3206
					GB(r, 16, 3) + 2,
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2135
diff changeset
  3207
					GB(r,  0, 3) + 5,
1359
52782e5cf7c9 (svn r1863) Give the effect vehicle type enums more descriptive names and use the enum as parameter type for CreateEffectVehicle*()
tron
parents: 1337
diff changeset
  3208
					EV_EXPLOSION_SMALL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3209
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3210
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3211
		} while ( (u=u->next) != NULL);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3212
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3213
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3214
	if (state <= 240 && !(v->tick_counter&3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3215
		ChangeTrainDirRandomly(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3216
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3217
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
  3218
	if (state >= 4440 && !(v->tick_counter&0x1F)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3219
		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
  3220
		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
  3221
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3222
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3223
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3224
static void HandleBrokenTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3225
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3226
	if (v->breakdown_ctr != 1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3227
		v->breakdown_ctr = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3228
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3229
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3230
		if (v->breakdowns_since_last_service != 255)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3231
			v->breakdowns_since_last_service++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3232
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3233
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3234
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3235
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3236
		SndPlayVehicleFx((_opt.landscape != LT_CANDY) ?
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3237
			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
  3238
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3239
		if (!(v->vehstatus & VS_HIDDEN)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3240
			Vehicle *u = CreateEffectVehicleRel(v, 4, 4, 5, EV_BREAKDOWN_SMOKE);
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2529
diff changeset
  3241
			if (u != NULL) u->u.special.unk0 = v->breakdown_delay * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3242
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3243
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3244
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3245
	if (!(v->tick_counter & 3)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3246
		if (!--v->breakdown_delay) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3247
			v->breakdown_ctr = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3248
			InvalidateWindow(WC_VEHICLE_VIEW, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3249
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3250
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3251
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3252
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3253
static const byte _breakdown_speeds[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3254
	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
  3255
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3256
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3257
static bool TrainCheckIfLineEnds(Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3258
{
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  3259
	TileIndex tile;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3260
	uint x,y;
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3261
	uint16 break_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3262
	int t;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3263
	uint32 ts;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3264
	byte trackdir;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3265
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3266
	t = v->breakdown_ctr;
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3267
	if (t > 1) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3268
		v->vehstatus |= VS_TRAIN_SLOWING;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3269
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3270
		break_speed = _breakdown_speeds[GB(~t, 4, 4)];
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3271
		if (break_speed < v->cur_speed) v->cur_speed = break_speed;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3272
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3273
		v->vehstatus &= ~VS_TRAIN_SLOWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3274
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3275
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3276
	// exit if inside a tunnel
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3277
	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
  3278
		return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3279
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3280
	// exit if inside a depot
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3281
	if (v->u.rail.track & 0x80)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3282
		return true;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3283
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3284
	tile = v->tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3285
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3286
	// tunnel entrance?
2150
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  3287
	if (IsTunnelTile(tile) && GB(_m[tile].m5, 0, 2) * 2 + 1 == v->direction)
f710b959b1c4 (svn r2660) Get rid of some more shifting/anding/casting
tron
parents: 2140
diff changeset
  3288
		return true;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3289
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3290
	// depot?
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3291
	/* 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
  3292
	     (by building a depot right against a station) */
2049
538e73c53f54 (svn r2558) Change the internal map format from 7 arrays to one array of structs, this doesn't change the saved format for now. It's a stepping stone for further changes.
tron
parents: 2044
diff changeset
  3293
/*	if (IsTileType(tile, MP_RAILWAY) && (_m[tile].m5 & 0xFC) == 0xC0)
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3294
		return true;*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3295
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3296
	/* 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
  3297
	t = v->direction >> 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3298
	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
  3299
		t = (t - 1) & 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3300
	}
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3301
	/* Calculate next tile */
900
27eb21ced433 (svn r1386) Move TileIndexDiff to map.h
tron
parents: 889
diff changeset
  3302
	tile += TileOffsByDir(t);
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3303
	// 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
  3304
	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
  3305
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3306
	// if there are tracks on the new tile, pick one (trackdir will only be used when its a signal tile, in which case only 1 trackdir is accessible for us)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3307
	if (ts & TRACKDIR_BIT_MASK)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3308
		trackdir = FindFirstBit2x64(ts & TRACKDIR_BIT_MASK);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3309
	else
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3310
		trackdir = INVALID_TRACKDIR;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3311
22
fe6f35cc987b (svn r23) -Some omments on the code (blathijs)
darkvater
parents: 19
diff changeset
  3312
	/* Calc position within the current tile ?? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3313
	x = v->x_pos & 0xF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3314
	y = v->y_pos & 0xF;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3315
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3316
	switch(v->direction) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3317
	case 0:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3318
		x = (~x) + (~y) + 24;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3319
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3320
	case 7:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3321
		x = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3322
		/* fall through */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3323
	case 1:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3324
		x = (~x) + 16;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3325
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3326
	case 2:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3327
		x = (~x) + y + 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3328
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3329
	case 3:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3330
		x = y;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3331
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3332
	case 4:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3333
		x = x + y - 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3334
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3335
	case 6:
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3336
		x = (~y) + x + 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3337
		break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3338
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3339
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3340
	if (GB(ts, 0, 16) != 0) {
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3341
		/* 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
  3342
		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
  3343
			v->cur_speed = 0;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3344
			ReverseTrainDirection(v);
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3345
			return false;
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3346
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3347
		if ((ts &= (ts >> 16)) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3348
			// make a rail/road crossing red
1927
1181e7652e2f (svn r2433) - CodeChange: unmagicify all road/train crossings with IsLevelCrossing() function (peter1138)
Darkvater
parents: 1926
diff changeset
  3349
			if (IsTileType(tile, MP_STREET) && IsLevelCrossing(tile)) {
2493
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2484
diff changeset
  3350
				if (GB(_m[tile].m5, 2, 1) == 0) {
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2484
diff changeset
  3351
					SB(_m[tile].m5, 2, 1, 1);
541
625227bb2a3d (svn r925) Use sound enums
tron
parents: 540
diff changeset
  3352
					SndPlayVehicleFx(SND_0E_LEVEL_CROSSING, v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3353
					MarkTileDirtyByTile(tile);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3354
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3355
			}
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3356
			return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3357
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3358
	} else if (x + 4 > 15) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3359
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3360
		ReverseTrainDirection(v);
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3361
		return false;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3362
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3363
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3364
	if  (v->u.rail.pbs_status == PBS_STAT_HAS_PATH)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3365
		return true;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3366
2451
f7d6ed0f564d (svn r2977) - CodeChange: [pbs] Use a check to see if a rail segment is a pbs segment, instead of letting the pathfinder determine this.
hackykid
parents: 2441
diff changeset
  3367
	if ((trackdir != INVALID_TRACKDIR) && (PBSIsPbsSignal(tile,trackdir) && PBSIsPbsSegment(tile,trackdir)) && !(IsTileType(v->tile, MP_STATION) && (v->current_order.station == _m[v->tile].m2))) {
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3368
		NPFFindStationOrTileData fstd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3369
		NPFFoundTargetData ftd;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3370
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3371
		NPFFillWithOrderData(&fstd, v);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3372
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3373
		DEBUG(pbs, 2) ("pbs: (%i) choose signal (CEOL), tile:%x  trackdir:%i", v->unitnumber, tile, trackdir);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3374
		ftd = NPFRouteToStationOrTile(tile, trackdir, &fstd, TRANSPORT_RAIL, v->owner, v->u.rail.railtype, PBS_MODE_GREEN);
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3375
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3376
		if (ftd.best_trackdir != 0xFF && NPFGetFlag(&ftd.node, NPF_FLAG_PBS_EXIT)) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3377
			if (!(NPFGetFlag(&ftd.node, NPF_FLAG_PBS_BLOCKED) || NPFGetFlag(&ftd.node, NPF_FLAG_PBS_RED))) {
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3378
				v->u.rail.pbs_status = PBS_STAT_HAS_PATH;
2115
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  3379
				v->u.rail.pbs_end_tile = ftd.node.tile;
99198b563a9c (svn r2625) - Fix: [pbs] Store the end of a train's reserved path explicitly. Prevents trains from unreserving eachothers paths in some cases.
hackykid
parents: 2049
diff changeset
  3380
				v->u.rail.pbs_end_trackdir = ftd.node.direction;
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3381
				return true;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3382
			}
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3383
		};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3384
	};
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3385
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3386
	// slow down
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3387
	v->vehstatus |= VS_TRAIN_SLOWING;
2484
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3388
	break_speed = _breakdown_speeds[x & 0xF];
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3389
	if (!(v->direction&1)) break_speed >>= 1;
0e45d70ae908 (svn r3010) Get rid of quite some dubious casts, either by using GB(), proper types or just removing them
tron
parents: 2482
diff changeset
  3390
	if (break_speed < v->cur_speed) v->cur_speed = break_speed;
742
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3391
c71e6120b789 (svn r1198) -Fix: [ 1087701 ] It is no longer possible to crash trains of other
truelight
parents: 737
diff changeset
  3392
	return true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3393
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3394
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3395
static void TrainLocoHandler(Vehicle *v, bool mode)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3396
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3397
	int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3398
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3399
	/* train has crashed? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3400
	if (v->u.rail.crash_anim_pos != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3401
		if (!mode) HandleCrashedTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3402
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3403
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3404
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3405
	if (v->u.rail.force_proceed != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3406
		v->u.rail.force_proceed--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3407
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3408
	/* train is broken down? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3409
	if (v->breakdown_ctr != 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3410
		if (v->breakdown_ctr <= 2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3411
			HandleBrokenTrain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3412
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3413
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3414
		v->breakdown_ctr--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3415
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3416
954
66c89362a778 (svn r1445) -Fix: reversing a train also reverses the UP and DOWN status for the
truelight
parents: 926
diff changeset
  3417
	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
  3418
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3419
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3420
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3421
	/* exit if train is stopped */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3422
	if (v->vehstatus & VS_STOPPED && v->cur_speed == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3423
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3424
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3425
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3426
	if (ProcessTrainOrder(v)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3427
		v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3428
		v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3429
		v->subspeed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3430
		ReverseTrainDirection(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3431
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3432
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3433
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3434
	HandleTrainLoading(v, mode);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3435
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3436
	if (v->current_order.type == OT_LOADING)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3437
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3438
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3439
	if (CheckTrainStayInDepot(v))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3440
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3441
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3442
	if (!mode) HandleLocomotiveSmokeCloud(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3443
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3444
	j = UpdateTrainSpeed(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3445
	if (j == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3446
		// 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
  3447
		if (v->cur_speed != 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3448
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3449
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3450
		TrainCheckIfLineEnds(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3451
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3452
		do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3453
			TrainController(v);
1922
bda6f85eefaa (svn r2428) - Feature: [newgrf] Implement shorter train vehicles (Therax), and the callback that goes with it.
hackykid
parents: 1921
diff changeset
  3454
			CheckTrainCollision(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3455
			if (v->cur_speed <= 0x100)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3456
				break;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3457
		} while (--j != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3458
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3459
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3460
	SetLastSpeed(v, v->cur_speed);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3461
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3462
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3463
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3464
void Train_Tick(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3465
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3466
	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
  3467
		v->cargo_days++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3468
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3469
	v->tick_counter++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3470
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3471
	if (v->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3472
		TrainLocoHandler(v, false);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3473
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3474
		// 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
  3475
		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
  3476
			TrainLocoHandler(v, true);
1132
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  3477
	} 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
  3478
		// Delete flooded standalone wagon
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  3479
		if (++v->u.rail.crash_anim_pos >= 4400)
71a2168ceee1 (svn r1633) -Fix: [1040119] Flood and wagons in depot
darkvater
parents: 1128
diff changeset
  3480
			DeleteVehicle(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3481
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3482
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3483
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3484
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3485
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
  3486
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
  3487
// Validation for the news item "Train is waiting in depot"
1095
b59632d9df1b (svn r1596) Add some more statics
tron
parents: 1093
diff changeset
  3488
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
  3489
{
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
  3490
	Vehicle *v = GetVehicle(data_a);
1064
2242cd7419bb (svn r1565) -Fix: [ 1104969 ] Aircraft in hangar messages are now revalidated before
celestar
parents: 1060
diff changeset
  3491
	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
  3492
}
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
  3493
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1969
diff changeset
  3494
void TrainEnterDepot(Vehicle *v, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3495
{
2493
f6b4300cc2b0 (svn r3019) -Codechange: Replace explicit shifting/anding/oring with GB and SB
tron
parents: 2484
diff changeset
  3496
	SetSignalsOnBothDir(tile, _depot_track_ind[GB(_m[tile].m5, 0, 2)]);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3497
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3498
	if (v->subtype != TS_Front_Engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3499
		v = GetFirstVehicleInChain(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3500
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
  3501
	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
  3502
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3503
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3504
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3505
	v->load_unload_time_rem = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3506
	v->cur_speed = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3507
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
  3508
	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
  3509
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3510
	if (v->current_order.type == OT_GOTO_DEPOT) {
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3511
		Order t;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3512
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3513
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3514
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3515
		t = v->current_order;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3516
		v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3517
		v->current_order.flags = 0;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3518
1530
2b46869fc0d4 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  3519
		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) { // Part of the orderlist?
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3520
			v->u.rail.days_since_order_progr = 0;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3521
			v->cur_order_index++;
1530
2b46869fc0d4 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  3522
		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) { // User initiated?
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3523
			v->vehstatus |= VS_STOPPED;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3524
			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
  3525
				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
  3526
				AddValidatedNewsItem(
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3527
					STR_8814_TRAIN_IS_WAITING_IN_DEPOT,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3528
					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
  3529
					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
  3530
					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
  3531
					ValidateTrainInDepot);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3532
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3533
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3534
	}
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
  3535
	InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3536
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3537
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3538
static void CheckIfTrainNeedsService(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3539
{
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2618
diff changeset
  3540
	const Depot* 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
  3541
	TrainFindDepotData tfdd;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3542
2008
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3543
	if (PBSTileReserved(v->tile) & v->u.rail.track)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3544
		return;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3545
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3546
	if (v->u.rail.pbs_status == PBS_STAT_HAS_PATH)
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3547
		return;
cdb444f6d43c (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 2006
diff changeset
  3548
1520
d88442095697 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1507
diff changeset
  3549
	if (_patches.servint_trains == 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3550
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3551
1520
d88442095697 (svn r2024) -Fix: [autoreplace] reverted all changes involving v->set_for_replacement as they caused desyncs.
bjarni
parents: 1507
diff changeset
  3552
	if (!VehicleNeedsService(v))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3553
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3554
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3555
	if (v->vehstatus & VS_STOPPED)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3556
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3557
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 954
diff changeset
  3558
	if (_patches.gotodepot && VehicleHasDepotOrders(v))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3559
		return;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3560
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3561
	// 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
  3562
	// depot visit by the order list.
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3563
	if (v->current_order.type == OT_GOTO_DEPOT &&
1530
2b46869fc0d4 (svn r2034) -Codechange: Replaced the rather meaningless LOAD and UNLOAD flags for depots by something more meaningful
celestar
parents: 1520
diff changeset
  3564
			(v->current_order.flags & (OF_HALT_IN_DEPOT | OF_PART_OF_ORDERS)) != 0)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3565
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3566
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
  3567
	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
  3568
	/* 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
  3569
	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
  3570
		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
  3571
			/* 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
  3572
			 * 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
  3573
			 * schedule? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3574
			v->current_order.type = OT_DUMMY;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3575
			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
  3576
			InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3577
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3578
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3579
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3580
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
  3581
	depot = GetDepotByTile(tfdd.tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3582
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3583
	if (v->current_order.type == OT_GOTO_DEPOT &&
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
  3584
			v->current_order.station != depot->index &&
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3585
			!CHANCE16(3,16))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3586
		return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3587
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3588
	v->current_order.type = OT_GOTO_DEPOT;
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3589
	v->current_order.flags = OF_NON_STOP;
1313
f1013ec3d318 (svn r1817) -Codechange: Moved depot-functions to depot.c
truelight
parents: 1282
diff changeset
  3590
	v->current_order.station = depot->index;
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
  3591
	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
  3592
	InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, STATUS_BAR);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3593
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3594
1790
47963a0cfca3 (svn r2294) - CodeChange: check the service interval settings when changing of all vehicle-types. To simplify things introduce GetServiceIntervalClamped() that returns the same or clamped value of the new service interval. There were some inconsistencies in the gui files so I had to change those, and const correctness kicked in, so it's a bit messy at certain points.
Darkvater
parents: 1786
diff changeset
  3595
int32 GetTrainRunningCost(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3596
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3597
	int32 cost = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3598
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3599
	do {
540
2987d7976ea2 (svn r924) Use RailVehInfo() instead of &_rail_vehicle_info[]
tron
parents: 534
diff changeset
  3600
		const RailVehicleInfo *rvi = RailVehInfo(v->engine_type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3601
		if (rvi->running_cost_base)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3602
			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
  3603
	} while ( (v=v->next) != NULL );
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3604
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3605
	return cost;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3606
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3607
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3608
void OnNewDay_Train(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3609
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3610
	TileIndex tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3611
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3612
	if ((++v->day_counter & 7) == 0)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3613
		DecreaseVehicleValue(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3614
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3615
	if (v->subtype == TS_Front_Engine) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3616
		CheckVehicleBreakdown(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3617
		AgeVehicle(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3618
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3619
		CheckIfTrainNeedsService(v);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3620
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3621
		// 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
  3622
		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
  3623
			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
  3624
			SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3625
			AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3626
				STR_TRAIN_IS_LOST,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3627
				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
  3628
				v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3629
				0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3630
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3631
1053
8d90844ddc2e (svn r1554) -Fix: [ 1103187 ] Order Check messages are now validated before
celestar
parents: 1049
diff changeset
  3632
		CheckOrders(v->index, OC_INIT);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 166
diff changeset
  3633
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3634
		/* update destination */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 541
diff changeset
  3635
		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
  3636
				(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
  3637
					v->dest_tile = tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3638
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3639
		if ((v->vehstatus & VS_STOPPED) == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3640
			/* running costs */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3641
			int32 cost = GetTrainRunningCost(v) / 364;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3642
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3643
			v->profit_this_year -= cost >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3644
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3645
			SET_EXPENSES_TYPE(EXPENSES_TRAIN_RUN);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3646
			SubtractMoneyFromPlayerFract(v->owner, cost);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3647
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3648
			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
  3649
			InvalidateWindowClasses(WC_TRAINS_LIST);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3650
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3651
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3652
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3653
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
  3654
void TrainsYearlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3655
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3656
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3657
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3658
	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
  3659
		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
  3660
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3661
			// 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
  3662
			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
  3663
				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
  3664
				SetDParam(0, v->unitnumber);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3665
				AddNewsItem(
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3666
					STR_TRAIN_IS_UNPROFITABLE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3667
					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
  3668
					v->index,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3669
					0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3670
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3671
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3672
			v->profit_last_year = v->profit_this_year;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3673
			v->profit_this_year = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3674
			InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3675
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3676
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3677
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3678
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3679
void HandleClickOnTrain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3680
{
1067
3ba7987a004e (svn r1568) made an enum of train subtypes to make the code more readable
bjarni
parents: 1064
diff changeset
  3681
	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
  3682
	ShowTrainViewWindow(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3683
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3684
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
  3685
void InitializeTrains(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3686
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3687
	_age_cargo_skip_counter = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  3688
}