src/vehicle.cpp
author rubidium
Fri, 12 Sep 2008 17:38:57 +0000
changeset 10115 e162dbf901d2
parent 10103 ad05ade1f2c4
child 10138 fca05009f8ce
permissions -rw-r--r--
(svn r14299) -Fix: disable 'toggle palette' when no NewGRF has been selected.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 9081
diff changeset
     3
/** @file vehicle.cpp Base implementations of all vehicles. */
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1881
diff changeset
     6
#include "openttd.h"
3957
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3953
diff changeset
     7
#include "road_map.h"
3959
48f178431701 (svn r5118) Add IsRoadVehInDepot{Stopped,}()
tron
parents: 3957
diff changeset
     8
#include "roadveh.h"
3961
9868b766fda7 (svn r5120) Add IsShipInDepot{Stopped,}() and remove some redundant checks
tron
parents: 3959
diff changeset
     9
#include "ship.h"
1349
15979a2e9001 (svn r1853) Move spritecache function declarations into a header of their own and use SpriteID as parameter type where appropriate
tron
parents: 1328
diff changeset
    10
#include "spritecache.h"
8119
52b48108425a (svn r11680) -Codechange: refactor more out of openttd.h and functions.h.
rubidium
parents: 8116
diff changeset
    11
#include "tile_cmd.h"
6453
226bcddeba32 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6444
diff changeset
    12
#include "landscape.h"
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
    13
#include "timetable.h"
8224
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8214
diff changeset
    14
#include "viewport_func.h"
c5a64d87cc54 (svn r11787) -Codechange: more header rewrites. This time related to viewport.h.
rubidium
parents: 8214
diff changeset
    15
#include "gfx_func.h"
8763
81fadd257f67 (svn r12459) -Codechange: split news.h into news_type.h and news_func.h.
rubidium
parents: 8736
diff changeset
    16
#include "news_func.h"
8116
8da76dcb3287 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8114
diff changeset
    17
#include "command_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
#include "saveload.h"
8254
1496654ca5e7 (svn r11818) -Codechange: split player.h into smaller pieces.
rubidium
parents: 8245
diff changeset
    19
#include "player_func.h"
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: 1600
diff changeset
    20
#include "debug.h"
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1735
diff changeset
    21
#include "vehicle_gui.h"
8103
cf92483a0abf (svn r11664) -Codechange: use more specific ("rail_type.h" instead of "rail.h" that includes way more than only "rail_type.h") includes at some places.
rubidium
parents: 8081
diff changeset
    22
#include "rail_type.h"
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
    23
#include "train.h"
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
    24
#include "aircraft.h"
3428
146dc5f172fc (svn r4256) - Codechange: Replace lone map access in vehicle.c with its map accessor.
peter1138
parents: 3422
diff changeset
    25
#include "industry_map.h"
3404
eb8ebfe5df67 (svn r4215) -Codechange: Renamed *RoadStation* functions to *RoadStop* and moved them to station_map.h to keep consistency
celestar
parents: 3393
diff changeset
    26
#include "station_map.h"
3957
25f4a4f22e05 (svn r5116) Move the overly generic GetDepotDirection() from a header to its only consumer
tron
parents: 3953
diff changeset
    27
#include "water_map.h"
4130
fd86f49a1e96 (svn r5483) -Fix: [YAPF] desync - for MP games invalidate YAPF cache on every tick to keep it exactly the same on server and clients (it doesn't fix the real source of the problem, but should solve it). Thanks TrueLight for hunting this bug.
KUDr
parents: 4124
diff changeset
    28
#include "yapf/yapf.h"
5717
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
    29
#include "newgrf_callbacks.h"
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
    30
#include "newgrf_engine.h"
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
    31
#include "newgrf_sound.h"
9041
2e64f7dc7a48 (svn r12860) -Fix [FS#1947]: Train depart animation trigger was not called in some cases.
peter1138
parents: 9009
diff changeset
    32
#include "newgrf_station.h"
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
    33
#include "newgrf_text.h"
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
    34
#include "group.h"
8784
c2e9d649a9ce (svn r12488) -Codechange: split order.h into order_base.h and order_func.h.
rubidium
parents: 8763
diff changeset
    35
#include "order_func.h"
8114
dd6d21dc99c1 (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8108
diff changeset
    36
#include "strings_func.h"
8123
ce31d2843a95 (svn r11684) -Codechange: split gfx.h in a type and functional header.
rubidium
parents: 8121
diff changeset
    37
#include "zoom_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    38
#include "functions.h"
8140
0d0d8c94f84b (svn r11702) -Codechange: move all date related stuff to date*.
rubidium
parents: 8139
diff changeset
    39
#include "date_func.h"
8131
160939e24ed3 (svn r11692) -Codechange: move some functions from 'functions.h' to a more logical place and remove about 50% of the includes of 'functions.h'
rubidium
parents: 8130
diff changeset
    40
#include "window_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    41
#include "vehicle_func.h"
8238
d47a86c79603 (svn r11802) -Fix [FS#716]: do not crash trains when leaving depot to a very long track
smatz
parents: 8230
diff changeset
    42
#include "signal_func.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8144
diff changeset
    43
#include "sound_func.h"
8211
29a8510dfd62 (svn r11774) -Change: do not include variables.h in a header when it is not needed.
rubidium
parents: 8172
diff changeset
    44
#include "variables.h"
8212
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8211
diff changeset
    45
#include "autoreplace_func.h"
cf3fce5c7464 (svn r11775) -Codechange: move all autoreplace/autorenew functions to a single location.
rubidium
parents: 8211
diff changeset
    46
#include "autoreplace_gui.h"
8214
971f861d5543 (svn r11777) -Codechange: split the string header and make do not include it when it's not necessary.
rubidium
parents: 8212
diff changeset
    47
#include "string_func.h"
8270
e7c342f6b14c (svn r11834) -Codechange: only include settings_type.h if needed.
rubidium
parents: 8264
diff changeset
    48
#include "settings_type.h"
8847
426dd2d582e7 (svn r12599) -Codechange: force AllocateSafeRaw() to be linked to simplify compiler's decisions about inlining
smatz
parents: 8843
diff changeset
    49
#include "oldpool_func.h"
8962
1b263c69799d (svn r12754) -Codechange: split depot.h into depot_map.h, depot_func.h and depot_base.h and remove quite a lot of unneeded (before this) includes of depot.h.
rubidium
parents: 8961
diff changeset
    50
#include "depot_map.h"
9005
d6b0e0a54ef2 (svn r12800) -Codechange: move the animated tile related functions out of texteff.cpp (it isn't a text effect after all). Also remove a few more functions from functions.
rubidium
parents: 8997
diff changeset
    51
#include "animated_tile_func.h"
9009
6684576ef32b (svn r12804) -Codechange: move the effect vehicle handling out of vehicle.cpp
rubidium
parents: 9008
diff changeset
    52
#include "effectvehicle_base.h"
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
    53
#include "core/alloc_func.hpp"
9396
3f70b1b8642c (svn r13307) -Codechange: Separate VehicleList and its two functions so only the 3 users include it, reducing dependencies on misc/smallvec.h
peter1138
parents: 9365
diff changeset
    54
#include "vehiclelist.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
8264
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    56
#include "table/sprites.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    57
#include "table/strings.h"
b1e85998c7d3 (svn r11828) -Codechange: include table/* as the last includes and remove an unneeded include from openttd.h.
rubidium
parents: 8258
diff changeset
    58
6904
cc02894b3cce (svn r10153) -Fix [FS#869]: vehicles disappear when crossing certain tiles. Fix by B. N. SmatZ!.
rubidium
parents: 6886
diff changeset
    59
#define INVALID_COORD (0x7fffffff)
4174
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
    60
#define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    62
VehicleID _vehicle_id_ctr_day;
9344
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
    63
const Vehicle *_place_clicked_vehicle;
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    64
VehicleID _new_vehicle_id;
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    65
uint16 _returned_refit_capacity;
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
    66
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
    67
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
    68
/* Tables used in vehicle.h to find the right command for a certain vehicle type */
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
    69
const uint32 _veh_build_proc_table[] = {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    70
	CMD_BUILD_RAIL_VEHICLE,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    71
	CMD_BUILD_ROAD_VEH,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    72
	CMD_BUILD_SHIP,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    73
	CMD_BUILD_AIRCRAFT,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    74
};
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
    75
const uint32 _veh_sell_proc_table[] = {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    76
	CMD_SELL_RAIL_WAGON,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    77
	CMD_SELL_ROAD_VEH,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    78
	CMD_SELL_SHIP,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    79
	CMD_SELL_AIRCRAFT,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    80
};
2753
b25bd313785e (svn r3298) Remove unused and write-only variables
tron
parents: 2752
diff changeset
    81
5792
c4dad358b30b (svn r8349) -Codechange: replaced CMD_REFIT_VEH() and similar defines with real static inline functions
bjarni
parents: 5763
diff changeset
    82
const uint32 _veh_refit_proc_table[] = {
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    83
	CMD_REFIT_RAIL_VEHICLE,
3990
a317e665fb77 (svn r5191) - NewGRF: add cargo refit support for road vehicles
peter1138
parents: 3973
diff changeset
    84
	CMD_REFIT_ROAD_VEH,
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    85
	CMD_REFIT_SHIP,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    86
	CMD_REFIT_AIRCRAFT,
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    87
};
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    88
4451
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
    89
const uint32 _send_to_depot_proc_table[] = {
4495
b6381e4bcb61 (svn r6280) -Codechange: Use the same naming for trains as for other vehicles:
Darkvater
parents: 4465
diff changeset
    90
	CMD_SEND_TRAIN_TO_DEPOT,
4451
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
    91
	CMD_SEND_ROADVEH_TO_DEPOT,
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
    92
	CMD_SEND_SHIP_TO_DEPOT,
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
    93
	CMD_SEND_AIRCRAFT_TO_HANGAR,
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
    94
};
2e86da0081b5 (svn r6229) -Feature: Shared order lists now got a "goto depot" button
bjarni
parents: 4434
diff changeset
    95
2552
5b586a545665 (svn r3081) -Codechange: [autoreplace] complete rewrite of autoreplace
bjarni
parents: 2549
diff changeset
    96
1279
bc761aad52b3 (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1266
diff changeset
    97
/* Initialize the vehicle-pool */
7400
e211c7b9cb8f (svn r10772) -Codechange: also make use of the generic clean and destroy pool callback functions for vehicle.
rubidium
parents: 7398
diff changeset
    98
DEFINE_OLD_POOL_GENERIC(Vehicle, Vehicle)
1279
bc761aad52b3 (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1266
diff changeset
    99
8629
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   100
/** Function to tell if a vehicle needs to be autorenewed
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   101
 * @param *p The vehicle owner
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   102
 * @return true if the vehicle is old enough for replacement
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   103
 */
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   104
bool Vehicle::NeedsAutorenewing(const Player *p) const
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   105
{
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   106
	/* We can always generate the Player pointer when we have the vehicle.
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   107
	 * However this takes time and since the Player pointer is often present
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   108
	 * when this function is called then it's faster to pass the pointer as an
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   109
	 * argument rather than finding it again. */
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   110
	assert(p == GetPlayer(this->owner));
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   111
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   112
	if (!p->engine_renew) return false;
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   113
	if (this->age - this->max_age < (p->engine_renew_months * 30)) return false;
8997
3ee8e18dff23 (svn r12792) -Codechange: [autoreplace] added a function to figure out if a replacement is needed and if so, to which EngineID
bjarni
parents: 8996
diff changeset
   114
	if (this->age == 0) return false; // rail cars don't age and lacks a max age
8629
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   115
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   116
	return true;
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   117
}
f61bb746249f (svn r12231) -Cleanup (r12230): [autoreplace] moved Vehicle::NeedsAutorenewing() and added comments
bjarni
parents: 8582
diff changeset
   118
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: 577
diff changeset
   119
void VehicleServiceInDepot(Vehicle *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: 577
diff changeset
   120
{
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: 577
diff changeset
   121
	v->date_of_last_service = _date;
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: 577
diff changeset
   122
	v->breakdowns_since_last_service = 0;
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1922
diff changeset
   123
	v->reliability = GetEngine(v->engine_type)->reliability;
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
   124
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index); // ensure that last service date and reliability are updated
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: 577
diff changeset
   125
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
8862
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   127
bool Vehicle::NeedsServicing() const
593
4240f624bca0 (svn r1015) MFM r789
tron
parents: 588
diff changeset
   128
{
8862
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   129
	if (this->vehstatus & (VS_STOPPED | VS_CRASHED)) return false;
1757
89f63a75ffd2 (svn r2261) - Fix: When crashed vehicles try to find a depot for servicing, openttd asserts.
matthijs
parents: 1752
diff changeset
   130
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
   131
	if (_settings_game.order.no_servicing_if_no_breakdowns && _settings_game.difficulty.vehicle_breakdowns == 0) {
8862
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   132
		/* Vehicles set for autoreplacing needs to go to a depot even if breakdowns are turned off.
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   133
		 * Note: If servicing is enabled, we postpone replacement till next service. */
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   134
		return EngineHasReplacementForPlayer(GetPlayer(this->owner), this->engine_type, this->group_id);
4262
048f6a144c7c (svn r5888) -Fix: [autoreplace] if vehicles breakdowns and service are turned off, the vehicles failed to enter any depots
bjarni
parents: 4261
diff changeset
   135
	}
048f6a144c7c (svn r5888) -Fix: [autoreplace] if vehicles breakdowns and service are turned off, the vehicles failed to enter any depots
bjarni
parents: 4261
diff changeset
   136
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
   137
	return _settings_game.vehicle.servint_ispercent ?
8862
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   138
		(this->reliability < GetEngine(this->engine_type)->reliability * (100 - this->service_interval) / 100) :
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   139
		(this->date_of_last_service + this->service_interval < _date);
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   140
}
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   141
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   142
bool Vehicle::NeedsAutomaticServicing() const
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   143
{
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
   144
	if (_settings_game.order.gotodepot && VehicleHasDepotOrders(this)) return false;
8862
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   145
	if (this->current_order.IsType(OT_LOADING))            return false;
8914
6bbc5f1455f7 (svn r12684) -Fix [FS#1891]: manually given service at depot order is not forgotten anymore when autoservice 'interferes'.
rubidium
parents: 8902
diff changeset
   146
	if (this->current_order.IsType(OT_GOTO_DEPOT) && this->current_order.GetDepotOrderType() != ODTFB_SERVICE) return false;
8862
f72f256c0259 (svn r12629) -Codechange: Split VehicleNeedsService() into Vehicle::NeedsServicing() and Vehicle::NeedsAutomaticServicing().
frosch
parents: 8855
diff changeset
   147
	return NeedsServicing();
593
4240f624bca0 (svn r1015) MFM r789
tron
parents: 588
diff changeset
   148
}
4240f624bca0 (svn r1015) MFM r789
tron
parents: 588
diff changeset
   149
3881
c899867fd46e (svn r4937) Reduce the use of _error_message a bit
tron
parents: 3819
diff changeset
   150
StringID VehicleInTheWayErrMsg(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
{
2631
2ed0eb408229 (svn r3173) Use the trinary operator and switch to improve readability
tron
parents: 2630
diff changeset
   152
	switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   153
		case VEH_TRAIN:    return STR_8803_TRAIN_IN_THE_WAY;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   154
		case VEH_ROAD:     return STR_9000_ROAD_VEHICLE_IN_THE_WAY;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   155
		case VEH_AIRCRAFT: return STR_A015_AIRCRAFT_IN_THE_WAY;
3881
c899867fd46e (svn r4937) Reduce the use of _error_message a bit
tron
parents: 3819
diff changeset
   156
		default:           return STR_980E_SHIP_IN_THE_WAY;
2631
2ed0eb408229 (svn r3173) Use the trinary operator and switch to improve readability
tron
parents: 2630
diff changeset
   157
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9704
diff changeset
   160
static Vehicle *EnsureNoVehicleProcZ(Vehicle *v, void *data)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
{
8081
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8078
diff changeset
   162
	byte z = *(byte*)data;
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8078
diff changeset
   163
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8078
diff changeset
   164
	if (v->type == VEH_DISASTER || (v->type == VEH_AIRCRAFT && v->subtype == AIR_SHADOW)) return NULL;
4fddceb00aa6 (svn r11642) -Codechange: VehicleFromPos does check for v->tile == tile, so remove useless checks
smatz
parents: 8078
diff changeset
   165
	if (v->z_pos > z) return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
3881
c899867fd46e (svn r4937) Reduce the use of _error_message a bit
tron
parents: 3819
diff changeset
   167
	_error_message = VehicleInTheWayErrMsg(v);
537
901bd4c077f0 (svn r909) Small cleanup in vehicle.c, this should fix some warnings on 64bit machines
tron
parents: 534
diff changeset
   168
	return v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
8078
d7c60bf804d5 (svn r11639) -Codechange: simplify EnsureNoVehicleOnGround
smatz
parents: 8076
diff changeset
   171
bool EnsureNoVehicleOnGround(TileIndex tile)
d7c60bf804d5 (svn r11639) -Codechange: simplify EnsureNoVehicleOnGround
smatz
parents: 8076
diff changeset
   172
{
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   173
	byte z = GetTileMaxZ(tile);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   174
	return !HasVehicleOnPos(tile, &z, &EnsureNoVehicleProcZ);
8078
d7c60bf804d5 (svn r11639) -Codechange: simplify EnsureNoVehicleOnGround
smatz
parents: 8076
diff changeset
   175
}
d7c60bf804d5 (svn r11639) -Codechange: simplify EnsureNoVehicleOnGround
smatz
parents: 8076
diff changeset
   176
8072
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   177
/** Procedure called for every vehicle found in tunnel/bridge in the hash map */
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9704
diff changeset
   178
static Vehicle *GetVehicleTunnelBridgeProc(Vehicle *v, void *data)
8072
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   179
{
9495
cafcf9e8dbdf (svn r13475) -Fix (r13464): crash on destroying aquaduct with ship on in and on company bankrupt
smatz
parents: 9428
diff changeset
   180
	if (v->type != VEH_TRAIN && v->type != VEH_ROAD && v->type != VEH_SHIP) return NULL;
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10083
diff changeset
   181
	if (v == (const Vehicle *)data) return NULL;
8072
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   182
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   183
	_error_message = VehicleInTheWayErrMsg(v);
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   184
	return v;
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   185
}
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   186
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   187
/**
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   188
 * Finds vehicle in tunnel / bridge
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   189
 * @param tile first end
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   190
 * @param endtile second end
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10083
diff changeset
   191
 * @param ignore Ignore this vehicle when searching
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   192
 * @return true if the bridge has a vehicle
8072
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   193
 */
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10083
diff changeset
   194
bool HasVehicleOnTunnelBridge(TileIndex tile, TileIndex endtile, const Vehicle *ignore)
8072
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   195
{
10103
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10083
diff changeset
   196
	return HasVehicleOnPos(tile, (void *)ignore, &GetVehicleTunnelBridgeProc) ||
ad05ade1f2c4 (svn r14285) -Fix [FS#2263]: A train inside a wormhole could free the reservation of another train inside the wormhole.
michi_cc
parents: 10083
diff changeset
   197
			HasVehicleOnPos(endtile, (void *)ignore, &GetVehicleTunnelBridgeProc);
8072
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   198
}
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   199
7b188d7b8199 (svn r11633) -Codechange: merge CheckTunnelEmpty and IsVehicleOnBridge into GetVehicleTunnelBridge
smatz
parents: 8071
diff changeset
   200
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   201
static void UpdateVehiclePosHash(Vehicle *v, int x, int y);
2817
cdf488223c23 (svn r3365) Staticise 36 functions
tron
parents: 2814
diff changeset
   202
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
void VehiclePositionChanged(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
	int img = v->cur_image;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
	Point pt = RemapCoords(v->x_pos + v->x_offs, v->y_pos + v->y_offs, v->z_pos);
10056
48659f7d4fa5 (svn r14223) -Codechange: make GetSprite aware of the 4 different types of sprites: fonts, recolour, mapgen and normal sprites.
rubidium
parents: 9975
diff changeset
   207
	const Sprite *spr = GetSprite(img, ST_NORMAL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
2319
fba7d61df04a (svn r2845) Remove sprite size caching, it was unused
tron
parents: 2296
diff changeset
   209
	pt.x += spr->x_offs;
fba7d61df04a (svn r2845) Remove sprite size caching, it was unused
tron
parents: 2296
diff changeset
   210
	pt.y += spr->y_offs;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
	UpdateVehiclePosHash(v, pt.x, pt.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   213
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
	v->left_coord = pt.x;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
	v->top_coord = pt.y;
2319
fba7d61df04a (svn r2845) Remove sprite size caching, it was unused
tron
parents: 2296
diff changeset
   216
	v->right_coord = pt.x + spr->width + 2;
fba7d61df04a (svn r2845) Remove sprite size caching, it was unused
tron
parents: 2296
diff changeset
   217
	v->bottom_coord = pt.y + spr->height + 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   220
/** Called after load to update coordinates */
8172
bcad4f33bd3d (svn r11735) -Fix [FS#1574]: Don't reset loading indicator IDs when only reloading NewGRFs.
peter1138
parents: 8157
diff changeset
   221
void AfterLoadVehicles(bool clear_te_id)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
	FOR_ALL_VEHICLES(v) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   226
		/* Reinstate the previous pointer */
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   227
		if (v->Next() != NULL) v->Next()->previous = v;
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   228
		if (v->NextShared() != NULL) v->NextShared()->previous_shared = v;
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   229
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   230
		v->UpdateDeltaXY(v->direction);
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
   231
8172
bcad4f33bd3d (svn r11735) -Fix [FS#1574]: Don't reset loading indicator IDs when only reloading NewGRFs.
peter1138
parents: 8157
diff changeset
   232
		if (clear_te_id) v->fill_percent_te_id = INVALID_TE_ID;
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: 1600
diff changeset
   233
		v->first = NULL;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   234
		if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
   235
		if (v->type == VEH_ROAD)  v->u.road.first_engine = INVALID_ENGINE;
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
   236
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
   237
		v->cargo.InvalidateCache();
3355
e414a0b104a6 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3326
diff changeset
   238
	}
e414a0b104a6 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3326
diff changeset
   239
e414a0b104a6 (svn r4150) -Feature: Merged elrails into trunk. Thanks to Tron for lots of code and proofreading, thanks to peter1138 for another lot of code and ideas.
celestar
parents: 3326
diff changeset
   240
	FOR_ALL_VEHICLES(v) {
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   241
		/* Fill the first pointers */
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   242
		if (v->Previous() == NULL) {
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   243
			for (Vehicle *u = v; u != NULL; u = u->Next()) {
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   244
				u->first = v;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   245
			}
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   246
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   247
			/* Shared orders are only valid for first vehicles in chains. */
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   248
			if (v->previous_shared == NULL) {
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   249
				for (Vehicle *u = v; u != NULL; u = u->NextShared()) {
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   250
					u->first_shared = v;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   251
				}
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   252
			}
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   253
		}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   254
	}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   255
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   256
	FOR_ALL_VEHICLES(v) {
9625
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   257
		assert(v->first != NULL);
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   258
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   259
		if (v->type == VEH_TRAIN && (IsFrontEngine(v) || IsFreeWagon(v))) {
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   260
			if (IsFrontEngine(v)) v->u.rail.last_speed = v->cur_speed; // update displayed train speed
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9652
diff changeset
   261
			TrainConsistChanged(v, false);
9625
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   262
		} else if (v->type == VEH_ROAD && IsRoadVehFront(v)) {
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   263
			RoadVehUpdateCache(v);
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   264
		}
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   265
	}
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   266
9f6c09b4a030 (svn r13681) -Revert (r13678, r13677): the fixes didn't work in all cases (assertions on savegame loads).
rubidium
parents: 9624
diff changeset
   267
	FOR_ALL_VEHICLES(v) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   268
		switch (v->type) {
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6665
diff changeset
   269
			case VEH_ROAD:
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   270
				v->u.road.roadtype = HasBit(EngInfo(v->engine_type)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD;
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6665
diff changeset
   271
				v->u.road.compatible_roadtypes = RoadTypeToRoadTypes(v->u.road.roadtype);
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7108
diff changeset
   272
				/* FALL THROUGH */
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7108
diff changeset
   273
			case VEH_TRAIN:
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7108
diff changeset
   274
			case VEH_SHIP:
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7108
diff changeset
   275
				v->cur_image = v->GetImage(v->direction);
6683
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6665
diff changeset
   276
				break;
b88ae30866ce (svn r9914) -Codechange: prepare GTTS and the pathfinders to handle multiple road types on a single tile.
rubidium
parents: 6665
diff changeset
   277
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   278
			case VEH_AIRCRAFT:
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5823
diff changeset
   279
				if (IsNormalAircraft(v)) {
7134
5b2d47128d05 (svn r10408) -Codechange: make GetImage a class method of Vehicle instead of Get(Aircraft|RoadVeh|Ship|Train)Image.
rubidium
parents: 7108
diff changeset
   280
					v->cur_image = v->GetImage(v->direction);
5866
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   281
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   282
					/* The plane's shadow will have the same image as the plane */
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
   283
					Vehicle *shadow = v->Next();
5866
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   284
					shadow->cur_image = v->cur_image;
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   285
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   286
					/* In the case of a helicopter we will update the rotor sprites */
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   287
					if (v->subtype == AIR_HELICOPTER) {
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
   288
						Vehicle *rotor = shadow->Next();
5866
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   289
						rotor->cur_image = GetRotorImage(v);
60ee9fff5c99 (svn r8448) -Codechange/Fix (r2993): Also update the helicopter's rotor when loading the game. This can solve crashes when a game is loaded with missing GRF's
Darkvater
parents: 5855
diff changeset
   290
					}
6490
fe4443fba5e3 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6484
diff changeset
   291
fe4443fba5e3 (svn r9671) -Codechange: Implement NewGRF callback 36, which allows changing of various properties which were previously static. Vehicle max speed and train power/te/running costs are adjustable.
peter1138
parents: 6484
diff changeset
   292
					UpdateAircraftCache(v);
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   293
				}
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   294
				break;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   295
			default: break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
		}
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   297
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   298
		v->left_coord = INVALID_COORD;
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   299
		VehiclePositionChanged(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   302
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   303
Vehicle::Vehicle()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
{
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   305
	this->type               = VEH_INVALID;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   306
	this->left_coord         = INVALID_COORD;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   307
	this->group_id           = DEFAULT_GROUP;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   308
	this->fill_percent_te_id = INVALID_TE_ID;
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
   309
	this->first              = this;
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
   310
	this->first_shared       = this;
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   311
	this->colormap           = PAL_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
2804
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   314
/**
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   315
 * Get a value for a vehicle's random_bits.
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   316
 * @return A random value from 0 to 255.
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   317
 */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   318
byte VehicleRandomBits()
2804
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   319
{
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   320
	return GB(Random(), 0, 8);
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   321
}
ea4080819175 (svn r3352) - NewGRF: Move initialization of vehicle random_bits to DC_EXEC blocks to allow use of Random() instead of InteractiveRandom(), which will alleviate some possible network desyncs.
peter1138
parents: 2791
diff changeset
   322
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   323
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   324
/* static */ bool Vehicle::AllocateList(Vehicle **vl, int num)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
{
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   326
	uint counter = _Vehicle_pool.first_free_index;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   327
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   328
	for (int i = 0; i != num; i++) {
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   329
		Vehicle *v = AllocateRaw(counter);
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   330
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   331
		if (v == NULL) return false;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   332
		v = new (v) InvalidVehicle();
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   333
2606
715bba67addf (svn r3143) -Codechange: greatly increased speed when allocating vehicles
bjarni
parents: 2602
diff changeset
   334
		if (vl != NULL) {
715bba67addf (svn r3143) -Codechange: greatly increased speed when allocating vehicles
bjarni
parents: 2602
diff changeset
   335
			vl[i] = v;
715bba67addf (svn r3143) -Codechange: greatly increased speed when allocating vehicles
bjarni
parents: 2602
diff changeset
   336
		}
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   337
		counter++;
2601
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
   338
	}
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
   339
2606
715bba67addf (svn r3143) -Codechange: greatly increased speed when allocating vehicles
bjarni
parents: 2602
diff changeset
   340
	return true;
2601
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
   341
}
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
   342
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   343
/* Size of the hash, 6 = 64 x 64, 7 = 128 x 128. Larger sizes will (in theory) reduce hash
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   344
 * lookup times at the expense of memory usage. */
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   345
const int HASH_BITS = 7;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   346
const int HASH_SIZE = 1 << HASH_BITS;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   347
const int HASH_MASK = HASH_SIZE - 1;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   348
const int TOTAL_HASH_SIZE = 1 << (HASH_BITS * 2);
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   349
const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   350
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   351
/* Resolution of the hash, 0 = 1*1 tile, 1 = 2*2 tiles, 2 = 4*4 tiles, etc.
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   352
 * Profiling results show that 0 is fastest. */
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   353
const int HASH_RES = 0;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   354
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   355
static Vehicle *_new_vehicle_position_hash[TOTAL_HASH_SIZE];
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   356
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   357
static Vehicle *VehicleFromHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc, bool find_first)
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   358
{
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   359
	for (int y = yl; ; y = (y + (1 << HASH_BITS)) & (HASH_MASK << HASH_BITS)) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   360
		for (int x = xl; ; x = (x + 1) & HASH_MASK) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   361
			Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   362
			for (; v != NULL; v = v->next_new_hash) {
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9704
diff changeset
   363
				Vehicle *a = proc(v, data);
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   364
				if (find_first && a != NULL) return a;
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   365
			}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   366
			if (x == xu) break;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   367
		}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   368
		if (y == yu) break;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   369
	}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   370
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   371
	return NULL;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   372
}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   373
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   374
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   375
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   376
 * Helper function for FindVehicleOnPos/HasVehicleOnPos.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   377
 * @note Do not call this function directly!
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   378
 * @param x    The X location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   379
 * @param y    The Y location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   380
 * @param data Arbitrary data passed to proc
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   381
 * @param proc The proc that determines whether a vehicle will be "found".
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   382
 * @param find_first Whether to return on the first found or iterate over
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   383
 *                   all vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   384
 * @return the best matching or first vehicle (depending on find_first).
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   385
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   386
static Vehicle *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc, bool find_first)
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   387
{
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   388
	const int COLL_DIST = 6;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   389
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   390
	/* Hash area to scan is from xl,yl to xu,yu */
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   391
	int xl = GB((x - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   392
	int xu = GB((x + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   393
	int yl = GB((y - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   394
	int yu = GB((y + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   395
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   396
	return VehicleFromHash(xl, yl, xu, yu, data, proc, find_first);
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   397
}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   398
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   399
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   400
 * Find a vehicle from a specific location. It will call proc for ALL vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   401
 * on the tile and YOU must make SURE that the "best one" is stored in the
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   402
 * data value and is ALWAYS the same regardless of the order of the vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   403
 * where proc was called on!
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   404
 * When you fail to do this properly you create an almost untraceable DESYNC!
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   405
 * @note The return value of proc will be ignored.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   406
 * @note Use this when you have the intention that all vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   407
 *       should be iterated over.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   408
 * @param x    The X location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   409
 * @param y    The Y location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   410
 * @param data Arbitrary data passed to proc
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   411
 * @param proc The proc that determines whether a vehicle will be "found".
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   412
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   413
void FindVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   414
{
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   415
	VehicleFromPosXY(x, y, data, proc, false);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   416
}
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   417
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   418
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   419
 * Checks whether a vehicle in on a specific location. It will call proc for
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   420
 * vehicles until it returns non-NULL.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   421
 * @note Use FindVehicleOnPosXY when you have the intention that all vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   422
 *       should be iterated over.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   423
 * @param x    The X location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   424
 * @param y    The Y location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   425
 * @param data Arbitrary data passed to proc
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   426
 * @param proc The proc that determines whether a vehicle will be "found".
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   427
 * @return True if proc returned non-NULL.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   428
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   429
bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   430
{
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   431
	return VehicleFromPosXY(x, y, data, proc, true) != NULL;
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   432
}
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   433
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   434
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   435
 * Helper function for FindVehicleOnPos/HasVehicleOnPos.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   436
 * @note Do not call this function directly!
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   437
 * @param tile The location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   438
 * @param data Arbitrary data passed to proc
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   439
 * @param proc The proc that determines whether a vehicle will be "found".
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   440
 * @param find_first Whether to return on the first found or iterate over
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   441
 *                   all vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   442
 * @return the best matching or first vehicle (depending on find_first).
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   443
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   444
static Vehicle *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc, bool find_first)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
{
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   446
	int x = GB(TileX(tile), HASH_RES, HASH_BITS);
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   447
	int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   448
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   449
	Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   450
	for (; v != NULL; v = v->next_new_hash) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   451
		if (v->tile != tile) continue;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   452
9775
22e256c3bf46 (svn r13912) -Codechange: remove some casts by returning the proper type instead of void*.
rubidium
parents: 9704
diff changeset
   453
		Vehicle *a = proc(v, data);
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   454
		if (find_first && a != NULL) return a;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
	}
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   456
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   459
10083
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   460
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   461
 * Find a vehicle from a specific location. It will call proc for ALL vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   462
 * on the tile and YOU must make SURE that the "best one" is stored in the
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   463
 * data value and is ALWAYS the same regardless of the order of the vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   464
 * where proc was called on!
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   465
 * When you fail to do this properly you create an almost untraceable DESYNC!
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   466
 * @note The return value of proc will be ignored.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   467
 * @note Use this when you have the intention that all vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   468
 *       should be iterated over.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   469
 * @param tile The location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   470
 * @param data Arbitrary data passed to proc
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   471
 * @param proc The proc that determines whether a vehicle will be "found".
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   472
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   473
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   474
{
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   475
	VehicleFromPos(tile, data, proc, false);
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   476
}
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   477
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   478
/**
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   479
 * Checks whether a vehicle in on a specific location. It will call proc for
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   480
 * vehicles until it returns non-NULL.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   481
 * @note Use FindVehicleOnPos when you have the intention that all vehicles
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   482
 *       should be iterated over.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   483
 * @param tile The location on the map
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   484
 * @param data Arbitrary data passed to proc
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   485
 * @param proc The proc that determines whether a vehicle will be "found".
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   486
 * @return True if proc returned non-NULL.
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   487
 */
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   488
bool HasVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   489
{
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   490
	return VehicleFromPos(tile, data, proc, true) != NULL;
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   491
}
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   492
eee4e42aa15b (svn r14258) -Codechange: rework the way to query the vehicle hash to make sure it always results in the same irregardless of the order of the hash-linked-list.
rubidium
parents: 10056
diff changeset
   493
6886
e7f939f0206d (svn r10129) -Fix (r10111): Test for x == INVALID_COORD instead of v->tile == INVALID_TILE to determine if a vehicle should be removed from the has.
peter1138
parents: 6883
diff changeset
   494
static void UpdateNewVehiclePosHash(Vehicle *v, bool remove)
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   495
{
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   496
	Vehicle **old_hash = v->old_new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   497
	Vehicle **new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   498
6886
e7f939f0206d (svn r10129) -Fix (r10111): Test for x == INVALID_COORD instead of v->tile == INVALID_TILE to determine if a vehicle should be removed from the has.
peter1138
parents: 6883
diff changeset
   499
	if (remove) {
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   500
		new_hash = NULL;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   501
	} else {
7371
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7358
diff changeset
   502
		int x = GB(TileX(v->tile), HASH_RES, HASH_BITS);
0bb7e1279675 (svn r10734) -Fix [FS#1030]: Revert r10513) and add special cases for collision detection on bridges/tunnels.
peter1138
parents: 7358
diff changeset
   503
		int y = GB(TileY(v->tile), HASH_RES, HASH_BITS) << HASH_BITS;
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   504
		new_hash = &_new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   505
	}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   506
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   507
	if (old_hash == new_hash) return;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   508
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   509
	/* Remove from the old position in the hash table */
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   510
	if (old_hash != NULL) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   511
		Vehicle *last = NULL;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   512
		Vehicle *u = *old_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   513
		while (u != v) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   514
			last = u;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   515
			u = u->next_new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   516
			assert(u != NULL);
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   517
		}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   518
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   519
		if (last == NULL) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   520
			*old_hash = v->next_new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   521
		} else {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   522
			last->next_new_hash = v->next_new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   523
		}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   524
	}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   525
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   526
	/* Insert vehicle at beginning of the new position in the hash table */
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   527
	if (new_hash != NULL) {
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   528
		v->next_new_hash = *new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   529
		*new_hash = v;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   530
		assert(v != v->next_new_hash);
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   531
	}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   532
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   533
	/* Remember current hash position */
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   534
	v->old_new_hash = new_hash;
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   535
}
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   536
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   537
static Vehicle *_vehicle_position_hash[0x1000];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   538
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   539
static void UpdateVehiclePosHash(Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   540
{
6886
e7f939f0206d (svn r10129) -Fix (r10111): Test for x == INVALID_COORD instead of v->tile == INVALID_TILE to determine if a vehicle should be removed from the has.
peter1138
parents: 6883
diff changeset
   541
	UpdateNewVehiclePosHash(v, x == INVALID_COORD);
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   542
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   543
	Vehicle **old_hash, **new_hash;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   544
	int old_x = v->left_coord;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   545
	int old_y = v->top_coord;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   546
6491
00dc414c909d (svn r9672) -Cleanup: lots of coding style fixes around operands.
rubidium
parents: 6490
diff changeset
   547
	new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   548
	old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   549
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   550
	if (old_hash == new_hash) return;
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
	/* remove from hash table? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   553
	if (old_hash != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   554
		Vehicle *last = NULL;
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   555
		Vehicle *u = *old_hash;
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   556
		while (u != v) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   557
			last = u;
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   558
			u = u->next_hash;
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   559
			assert(u != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   561
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   562
		if (last == NULL) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   563
			*old_hash = v->next_hash;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   564
		} else {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   565
			last->next_hash = v->next_hash;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   566
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
	/* insert into hash table? */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   570
	if (new_hash != NULL) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   571
		v->next_hash = *new_hash;
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   572
		*new_hash = v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   573
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   574
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   576
void ResetVehiclePosHash()
5352
86fca6a41aa1 (svn r7523) -Feature: Add the possibility to change the newgrf configuration of a running game.
Darkvater
parents: 5295
diff changeset
   577
{
6883
a7be3fb54737 (svn r10126) -Fix (r10111, FS#864): old_new_hash cache was not reset when the position hash were
glx
parents: 6871
diff changeset
   578
	Vehicle *v;
a7be3fb54737 (svn r10126) -Fix (r10111, FS#864): old_new_hash cache was not reset when the position hash were
glx
parents: 6871
diff changeset
   579
	FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; }
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   580
	memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
6871
d8b53c9f0b13 (svn r10111) -Codechange: Add new vehicle hash table for collision detection and finding vehicles on a tile. The hash area scanned is far smaller than the old hash table, which is now used for viewport updates only. This should give a significant performance improvement for games with many vehicles. (Based on work by 'B. N. SmatZ!' and 'madman2003')
peter1138
parents: 6857
diff changeset
   581
	memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash));
5352
86fca6a41aa1 (svn r7523) -Feature: Add the possibility to change the newgrf configuration of a running game.
Darkvater
parents: 5295
diff changeset
   582
}
86fca6a41aa1 (svn r7523) -Feature: Add the possibility to change the newgrf configuration of a running game.
Darkvater
parents: 5295
diff changeset
   583
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   584
void ResetVehicleColorMap()
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   585
{
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   586
	Vehicle *v;
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   587
	FOR_ALL_VEHICLES(v) { v->colormap = PAL_NONE; }
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   588
}
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
   589
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   590
void InitializeVehicles()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
{
7400
e211c7b9cb8f (svn r10772) -Codechange: also make use of the generic clean and destroy pool callback functions for vehicle.
rubidium
parents: 7398
diff changeset
   592
	_Vehicle_pool.CleanPool();
e211c7b9cb8f (svn r10772) -Codechange: also make use of the generic clean and destroy pool callback functions for vehicle.
rubidium
parents: 7398
diff changeset
   593
	_Vehicle_pool.AddBlockToPool();
5352
86fca6a41aa1 (svn r7523) -Feature: Add the possibility to change the newgrf configuration of a running game.
Darkvater
parents: 5295
diff changeset
   594
86fca6a41aa1 (svn r7523) -Feature: Add the possibility to change the newgrf configuration of a running game.
Darkvater
parents: 5295
diff changeset
   595
	ResetVehiclePosHash();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   596
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   597
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   598
Vehicle *GetLastVehicleInChain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   599
{
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
   600
	while (v->Next() != NULL) v = v->Next();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   601
	return v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   602
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   603
9344
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
   604
const Vehicle *GetLastVehicleInChain(const Vehicle *v)
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
   605
{
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
   606
	while (v->Next() != NULL) v = v->Next();
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
   607
	return v;
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
   608
}
d3b1b1fbbad9 (svn r13240) -Codechange: More const-ness for GUI code.
peter1138
parents: 9340
diff changeset
   609
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2617
diff changeset
   610
uint CountVehiclesInChain(const Vehicle* v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
   612
	uint count = 0;
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
   613
	do count++; while ((v = v->Next()) != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	return count;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
4574
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   617
/** Check if a vehicle is counted in num_engines in each player struct
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   618
 * @param *v Vehicle to test
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   619
 * @return true if the vehicle is counted in num_engines
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   620
 */
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   621
bool IsEngineCountable(const Vehicle *v)
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   622
{
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   623
	switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   624
		case VEH_AIRCRAFT: return IsNormalAircraft(v); // don't count plane shadows and helicopter rotors
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   625
		case VEH_TRAIN:
4574
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   626
			return !IsArticulatedPart(v) && // tenders and other articulated parts
7526
27c4fbf8aef0 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7512
diff changeset
   627
			!IsRearDualheaded(v); // rear parts of multiheaded engines
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
   628
		case VEH_ROAD: return IsRoadVehFront(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
   629
		case VEH_SHIP: return true;
4574
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   630
		default: return false; // Only count player buildable vehicles
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   631
	}
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   632
}
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   633
7412
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   634
void Vehicle::PreDestructor()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
{
7413
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7412
diff changeset
   636
	if (CleaningPool()) return;
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7412
diff changeset
   637
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   638
	if (IsValidStationID(this->last_station_visited)) {
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   639
		GetStation(this->last_station_visited)->loading_vehicles.remove(this);
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   640
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   641
		HideFillingPercent(this->fill_percent_te_id);
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   642
		this->fill_percent_te_id = INVALID_TE_ID;
6500
f12678890222 (svn r9683) -Fix [FS#423]: improved loading does not use a huge amount of processing power anymore when having a lot of trains.
rubidium
parents: 6492
diff changeset
   643
	}
f12678890222 (svn r9683) -Fix [FS#423]: improved loading does not use a huge amount of processing power anymore when having a lot of trains.
rubidium
parents: 6492
diff changeset
   644
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   645
	if (IsEngineCountable(this)) {
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   646
		GetPlayer(this->owner)->num_engines[this->engine_type]--;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   647
		if (this->owner == _local_player) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   648
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   649
		if (IsValidGroupID(this->group_id)) GetGroup(this->group_id)->num_engines[this->engine_type]--;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   650
		if (this->IsPrimaryVehicle()) DecreaseGroupNumVehicle(this->group_id);
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5940
diff changeset
   651
	}
4574
61a17f80cfb9 (svn r6424) -Codechange: [autoreplace] removed a loop though all vehicles from each time the window is redrawn
bjarni
parents: 4560
diff changeset
   652
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   653
	if (this->type == VEH_ROAD) ClearSlot(this);
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   654
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   655
	if (this->type != VEH_TRAIN || (this->type == VEH_TRAIN && (IsFrontEngine(this) || IsFreeWagon(this)))) {
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   656
		InvalidateWindowData(WC_VEHICLE_DEPOT, this->tile);
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4736
diff changeset
   657
	}
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4736
diff changeset
   658
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   659
	this->cargo.Truncate(0);
7412
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   660
	DeleteVehicleOrders(this);
4404
b58b0af53da3 (svn r6157) -Codechange: DeleteVehicle removes a vehicle from the pool
truelight
parents: 4389
diff changeset
   661
b58b0af53da3 (svn r6157) -Codechange: DeleteVehicle removes a vehicle from the pool
truelight
parents: 4389
diff changeset
   662
	/* Now remove any artic part. This will trigger an other
b58b0af53da3 (svn r6157) -Codechange: DeleteVehicle removes a vehicle from the pool
truelight
parents: 4389
diff changeset
   663
	 *  destroy vehicle, which on his turn can remove any
b58b0af53da3 (svn r6157) -Codechange: DeleteVehicle removes a vehicle from the pool
truelight
parents: 4389
diff changeset
   664
	 *  other artic parts. */
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   665
	if ((this->type == VEH_TRAIN && EngineHasArticPart(this)) || (this->type == VEH_ROAD && RoadVehHasArticPart(this))) {
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
   666
		delete this->Next();
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
   667
	}
7765
a09738561237 (svn r11314) -Fix [FS#1356]: stop following a vehicle when you sell/destroyed it.
rubidium
parents: 7758
diff changeset
   668
9274
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9259
diff changeset
   669
	extern void StopGlobalFollowVehicle(const Vehicle *v);
42b67e65f1c2 (svn r13140) -Codechange: move the gui-list-sorting out of window_gui.h so window_gui.h only needs to be included in *_gui.cpp.
rubidium
parents: 9259
diff changeset
   670
	StopGlobalFollowVehicle(this);
7412
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   671
}
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   672
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   673
Vehicle::~Vehicle()
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   674
{
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
   675
	free(this->name);
7413
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7412
diff changeset
   676
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7412
diff changeset
   677
	if (CleaningPool()) return;
a590f7f0edb3 (svn r10799) -Fix: only calling QuickFree and not the destructor on pool cleanups might cause memory leaks due to the way C++ works.
rubidium
parents: 7412
diff changeset
   678
7499
7b5d83a10725 (svn r11014) -Fix (r11011) [FS#1170]: crash when selling the first train of a consist.
rubidium
parents: 7497
diff changeset
   679
	this->SetNext(NULL);
7412
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   680
	UpdateVehiclePosHash(this, INVALID_COORD, 0);
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   681
	this->next_hash = NULL;
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   682
	this->next_new_hash = NULL;
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   683
51db351a3313 (svn r10798) -Fix [FS#1105]: virtual functions do not work in destructors :(.
rubidium
parents: 7400
diff changeset
   684
	DeleteVehicleNews(this->index, INVALID_STRING_ID);
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   685
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   686
	new (this) InvalidVehicle();
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   687
}
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   688
6782
e36e22d356a9 (svn r10020) -Fix [FS#824]: GetNextVehicle() is invalid for anything that isn't a train.
maedhros
parents: 6773
diff changeset
   689
/**
e36e22d356a9 (svn r10020) -Fix [FS#824]: GetNextVehicle() is invalid for anything that isn't a train.
maedhros
parents: 6773
diff changeset
   690
 * Deletes all vehicles in a chain.
e36e22d356a9 (svn r10020) -Fix [FS#824]: GetNextVehicle() is invalid for anything that isn't a train.
maedhros
parents: 6773
diff changeset
   691
 * @param v The first vehicle in the chain.
e36e22d356a9 (svn r10020) -Fix [FS#824]: GetNextVehicle() is invalid for anything that isn't a train.
maedhros
parents: 6773
diff changeset
   692
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   693
void DeleteVehicleChain(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   694
{
7702
f963cd54d4d5 (svn r11236) -Fix [FS#1322]: DeleteVehicleChain can just work as good for all vehicles, instead of not allowing trains to use it (enforced by an assert), but actually using it for trains. Patch by SmatZ.
rubidium
parents: 7642
diff changeset
   695
	assert(v->First() == v);
6782
e36e22d356a9 (svn r10020) -Fix [FS#824]: GetNextVehicle() is invalid for anything that isn't a train.
maedhros
parents: 6773
diff changeset
   696
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   697
	do {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   698
		Vehicle *u = v;
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
   699
		/* sometimes, eg. for disaster vehicles, when company bankrupts, when removing crashed/flooded vehicles,
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
   700
		 * it may happen that vehicle chain is deleted when visible */
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
   701
		if (!(v->vehstatus & VS_HIDDEN)) MarkSingleVehicleDirty(v);
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
   702
		v = v->Next();
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
   703
		delete u;
1765
155dfdbbbc8d (svn r2269) - CodeChange: no else after return; exchange some magic 0xffff with INVALID_VEHICLE, vehicle index is of type VehicleID
Darkvater
parents: 1758
diff changeset
   704
	} while (v != NULL);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   705
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   706
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   707
/** head of the linked list to tell what vehicles that visited a depot in a tick */
2630
7206058a7e82 (svn r3172) static, const
tron
parents: 2617
diff changeset
   708
static Vehicle* _first_veh_in_depot_list;
2574
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   709
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   710
/** Adds a vehicle to the list of vehicles, that visited a depot this tick
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
   711
 * @param *v vehicle to add
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
   712
 */
2574
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   713
void VehicleEnteredDepotThisTick(Vehicle *v)
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   714
{
8582
601d2fafb775 (svn r12163) -Fix [FS#1705]: if a train is 'stopping' when entering a depot, do not let it leave again
smatz
parents: 8556
diff changeset
   715
	/* We need to set v->leave_depot_instantly as we have no control of it's contents at this time.
601d2fafb775 (svn r12163) -Fix [FS#1705]: if a train is 'stopping' when entering a depot, do not let it leave again
smatz
parents: 8556
diff changeset
   716
	 * Vehicle should stop in the depot if it was in 'stopping' state - train intered depot while slowing down. */
8853
3266f0374302 (svn r12615) -Codechange: rename some enums related to depot orders to make it more clear that they are no loading/unloading flags. Also add more type strictness.
rubidium
parents: 8848
diff changeset
   717
	if (((v->current_order.GetDepotActionType() & ODATFB_HALT) && !(v->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) && v->current_order.IsType(OT_GOTO_DEPOT)) ||
8582
601d2fafb775 (svn r12163) -Fix [FS#1705]: if a train is 'stopping' when entering a depot, do not let it leave again
smatz
parents: 8556
diff changeset
   718
			(v->vehstatus & VS_STOPPED)) {
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   719
		/* we keep the vehicle in the depot since the user ordered it to stay */
2590
64e3f69a4ad8 (svn r3127) -Fix: [autoreplace] fixed a condition where a vehicle could fail to stop when autoreplacing
bjarni
parents: 2579
diff changeset
   720
		v->leave_depot_instantly = false;
64e3f69a4ad8 (svn r3127) -Fix: [autoreplace] fixed a condition where a vehicle could fail to stop when autoreplacing
bjarni
parents: 2579
diff changeset
   721
	} else {
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   722
		/* the vehicle do not plan on stopping in the depot, so we stop it to ensure that it will not reserve the path
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   723
		 * out of the depot before we might autoreplace it to a different engine. The new engine would not own the reserved path
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   724
		 * we store that we stopped the vehicle, so autoreplace can start it again */
2579
8048bed6e67e (svn r3116) -Fix: [autoreplace] fixed issue where autorenewing/autoreplacing a plane could lock up an airport
bjarni
parents: 2575
diff changeset
   725
		v->vehstatus |= VS_STOPPED;
8048bed6e67e (svn r3116) -Fix: [autoreplace] fixed issue where autorenewing/autoreplacing a plane could lock up an airport
bjarni
parents: 2575
diff changeset
   726
		v->leave_depot_instantly = true;
8048bed6e67e (svn r3116) -Fix: [autoreplace] fixed issue where autorenewing/autoreplacing a plane could lock up an airport
bjarni
parents: 2575
diff changeset
   727
	}
8048bed6e67e (svn r3116) -Fix: [autoreplace] fixed issue where autorenewing/autoreplacing a plane could lock up an airport
bjarni
parents: 2575
diff changeset
   728
2574
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   729
	if (_first_veh_in_depot_list == NULL) {
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   730
		_first_veh_in_depot_list = v;
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   731
	} else {
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   732
		Vehicle *w = _first_veh_in_depot_list;
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   733
		while (w->depot_list != NULL) w = w->depot_list;
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   734
		w->depot_list = v;
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   735
	}
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   736
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   737
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
   738
void CallVehicleTicks()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   739
{
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
   740
	_first_veh_in_depot_list = NULL; // now we are sure it's initialized at the start of each tick
2574
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   741
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
   742
	Station *st;
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
   743
	FOR_ALL_STATIONS(st) LoadUnloadStation(st);
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
   744
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
   745
	Vehicle *v;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
	FOR_ALL_VEHICLES(v) {
7135
3c1bcca0d6bb (svn r10409) -Codechange: replace (Aircraft|RoadVeh|Ship|Train)_Tick with a Tick method in the Vehicle class.
rubidium
parents: 7134
diff changeset
   747
		v->Tick();
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   748
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   749
		switch (v->type) {
6621
441559124aea (svn r9841) -Codechange: add a little more type strictness to the vehicle types.
rubidium
parents: 6616
diff changeset
   750
			default: break;
441559124aea (svn r9841) -Codechange: add a little more type strictness to the vehicle types.
rubidium
parents: 6616
diff changeset
   751
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   752
			case VEH_TRAIN:
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   753
			case VEH_ROAD:
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   754
			case VEH_AIRCRAFT:
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   755
			case VEH_SHIP:
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   756
				if (v->type == VEH_TRAIN && IsTrainWagon(v)) continue;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   757
				if (v->type == VEH_AIRCRAFT && v->subtype != AIR_HELICOPTER) continue;
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
   758
				if (v->type == VEH_ROAD && !IsRoadVehFront(v)) continue;
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   759
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   760
				v->motion_counter += (v->direction & 1) ? (v->cur_speed * 3) / 4 : v->cur_speed;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   761
				/* Play a running sound if the motion counter passes 256 (Do we not skip sounds?) */
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   762
				if (GB(v->motion_counter, 0, 8) < v->cur_speed) PlayVehicleSound(v, VSE_RUNNING);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   763
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   764
				/* Play an alterate running sound every 16 ticks */
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   765
				if (GB(v->tick_counter, 0, 4) == 0) PlayVehicleSound(v, v->cur_speed > 0 ? VSE_RUNNING_16 : VSE_STOPPED_16);
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4648
diff changeset
   766
		}
2574
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   767
	}
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   768
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   769
	/* now we handle all the vehicles that entered a depot this tick */
2574
70a4e3cb85bf (svn r3111) -Fix: [autoreplace] [ 1341783 ] Assertion failure in vehicle.c line 378
bjarni
parents: 2564
diff changeset
   770
	v = _first_veh_in_depot_list;
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   771
	if (v != NULL) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   772
		while (v != NULL) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   773
			/* Autoreplace needs the current player set as the vehicle owner */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   774
			_current_player = v->owner;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   775
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   776
			/* Buffer v->depot_list and clear it.
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   777
			 * Autoreplace might clear this so it has to be buffered. */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   778
			Vehicle *w = v->depot_list;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   779
			v->depot_list = NULL; // it should always be NULL at the end of each tick
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   780
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   781
			/* Start vehicle if we stopped them in VehicleEnteredDepotThisTick()
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   782
			 * We need to stop them between VehicleEnteredDepotThisTick() and here or we risk that
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   783
			 * they are already leaving the depot again before being replaced. */
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   784
			if (v->leave_depot_instantly) {
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   785
				v->leave_depot_instantly = false;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   786
				v->vehstatus &= ~VS_STOPPED;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   787
			}
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   788
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   789
			/* Store the position of the effect as the vehicle pointer will become invalid later */
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   790
			int x = v->x_pos;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   791
			int y = v->y_pos;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   792
			int z = v->z_pos;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   793
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   794
			const Player *p = GetPlayer(_current_player);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   795
			SubtractMoneyFromPlayer(CommandCost(EXPENSES_NEW_VEHICLES, (Money)p->engine_renew_money));
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   796
			CommandCost res = DoCommand(0, v->index, 0, DC_EXEC, CMD_AUTOREPLACE_VEHICLE);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   797
			if (res.Succeeded()) v = NULL; // no longer valid
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   798
			SubtractMoneyFromPlayer(CommandCost(EXPENSES_NEW_VEHICLES, -(Money)p->engine_renew_money));
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   799
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   800
			if (IsLocalPlayer()) {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   801
				if (res.Succeeded()) {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   802
					ShowCostOrIncomeAnimation(x, y, z, res.GetCost());
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   803
				} else {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   804
					StringID error_message = res.GetErrorMessage();
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   805
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   806
					if (error_message != STR_AUTOREPLACE_NOTHING_TO_DO && error_message != INVALID_STRING_ID) {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   807
						if (error_message == STR_0003_NOT_ENOUGH_CASH_REQUIRES) error_message = STR_AUTOREPLACE_MONEY_LIMIT;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   808
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   809
						StringID message;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   810
						if (error_message == STR_TRAIN_TOO_LONG_AFTER_REPLACEMENT) {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   811
							message = error_message;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   812
						} else {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   813
							switch (v->type) {
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   814
								case VEH_TRAIN:    message = STR_TRAIN_AUTORENEW_FAILED;       break;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   815
								case VEH_ROAD:     message = STR_ROADVEHICLE_AUTORENEW_FAILED; break;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   816
								case VEH_SHIP:     message = STR_SHIP_AUTORENEW_FAILED;        break;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   817
								case VEH_AIRCRAFT: message = STR_AIRCRAFT_AUTORENEW_FAILED;    break;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   818
								default: NOT_REACHED();
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   819
							}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   820
						}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   821
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   822
						SetDParam(0, v->unitnumber);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   823
						SetDParam(1, error_message);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   824
						AddNewsItem(message, NS_ADVICE, v->index, 0);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   825
					}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   826
				}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   827
			}
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
   828
8996
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   829
			v = w;
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   830
		}
0873e8d827e7 (svn r12791) -Codechange: [autoreplace] Added a flag parameter (listens for DC_EXEC and DC_QUERY_COST) and included more info when returning CommandCost
bjarni
parents: 8980
diff changeset
   831
		_current_player = OWNER_NONE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
2704
bdf6ae0cb27c (svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents: 2697
diff changeset
   835
/** Check if a given engine type can be refitted to a given cargo
bdf6ae0cb27c (svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents: 2697
diff changeset
   836
 * @param engine_type Engine type to check
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: 1796
diff changeset
   837
 * @param cid_to check refit to this 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: 1796
diff changeset
   838
 * @return true if it is possible, false otherwise
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: 1796
diff changeset
   839
 */
2704
bdf6ae0cb27c (svn r3248) - Codechange: Change interface of CanRefitTo() to supply the engine type directly instead of getting it from a vehicle. This allows the function to be used before vehicles are involved.
peter1138
parents: 2697
diff changeset
   840
bool CanRefitTo(EngineID engine_type, CargoID cid_to)
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: 1796
diff changeset
   841
{
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   842
	return HasBit(EngInfo(engine_type)->refit_mask, cid_to);
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: 1796
diff changeset
   843
}
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: 1796
diff changeset
   844
3973
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   845
/** Find the first cargo type that an engine can be refitted to.
6484
79156627e1ba (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents: 6453
diff changeset
   846
 * @param engine_type Which engine to find cargo for.
3973
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   847
 * @return A climate dependent cargo type. CT_INVALID is returned if not refittable.
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   848
 */
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   849
CargoID FindFirstRefittableCargo(EngineID engine_type)
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   850
{
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   851
	uint32 refit_mask = EngInfo(engine_type)->refit_mask;
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   852
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   853
	if (refit_mask != 0) {
6350
04b19f551aec (svn r9388) -Codechange: variable scope and type, and standardify all CargoID loops.
peter1138
parents: 6331
diff changeset
   854
		for (CargoID cid = 0; cid < NUM_CARGO; cid++) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   855
			if (HasBit(refit_mask, cid)) return cid;
3973
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   856
		}
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   857
	}
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   858
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   859
	return CT_INVALID;
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   860
}
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   861
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   862
/** Learn the price of refitting a certain engine
6484
79156627e1ba (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents: 6453
diff changeset
   863
* @param engine_type Which engine to refit
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   864
* @return Price for refitting
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   865
*/
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
   866
CommandCost GetRefitCost(EngineID engine_type)
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   867
{
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   868
	Money base_cost;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   869
	ExpensesType expense_type;
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   870
	switch (GetEngine(engine_type)->type) {
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   871
		case VEH_SHIP:
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   872
			base_cost = _price.ship_base;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   873
			expense_type = EXPENSES_SHIP_RUN;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   874
			break;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   875
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   876
		case VEH_ROAD:
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   877
			base_cost = _price.roadveh_base;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   878
			expense_type = EXPENSES_ROADVEH_RUN;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   879
			break;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   880
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   881
		case VEH_AIRCRAFT:
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   882
			base_cost = _price.aircraft_base;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   883
			expense_type = EXPENSES_AIRCRAFT_RUN;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   884
			break;
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   885
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
   886
		case VEH_TRAIN:
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   887
			base_cost = 2 * ((RailVehInfo(engine_type)->railveh_type == RAILVEH_WAGON) ?
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   888
							 _price.build_railwagon : _price.build_railvehicle);
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   889
			expense_type = EXPENSES_TRAIN_RUN;
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   890
			break;
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   891
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   892
		default: NOT_REACHED();
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   893
	}
8230
64f28fe2d5c8 (svn r11793) -Codechange: pass the expense type via the CommandCost instead of a global variable. Patch by Noldo (FS#1114).
rubidium
parents: 8224
diff changeset
   894
	return CommandCost(expense_type, (EngInfo(engine_type)->refit_cost * base_cost) >> 10);
4544
69f1248a2d97 (svn r6376) -Codechange: [vehicle refit] moved all refit cost calculations into GetRefitCost()
bjarni
parents: 4527
diff changeset
   895
}
3973
9d3cd1ed6ac0 (svn r5146) - NewGRF: Support selection of aircraft default cargo type by checking its refit mask. If aircraft can be refitted to passengers, no change happens, else the first refittable type is chosen. Also use refit capacity to determine the default capacity.
peter1138
parents: 3961
diff changeset
   896
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
   897
static void DoDrawVehicle(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   898
{
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   899
	SpriteID image = v->cur_image;
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   900
	SpriteID pal = PAL_NONE;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   901
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   902
	if (v->vehstatus & VS_DEFPAL) pal = (v->vehstatus & VS_CRASHED) ? PALETTE_CRASH : GetVehiclePalette(v);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
   904
	AddSortableSpriteToDraw(image, pal, v->x_pos + v->x_offs, v->y_pos + v->y_offs,
8793
5e35232593c3 (svn r12531) -Codechange: Rename some variables for consistency.
frosch
parents: 8786
diff changeset
   905
		v->x_extent, v->y_extent, v->z_extent, v->z_pos, (v->vehstatus & VS_SHADOW) != 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
void ViewportAddVehicles(DrawPixelInfo *dpi)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
{
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   910
	/* The bounding rectangle */
4174
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   911
	const int l = dpi->left;
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   912
	const int r = dpi->left + dpi->width;
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   913
	const int t = dpi->top;
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   914
	const int b = dpi->top + dpi->height;
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   915
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
   916
	/* The hash area to scan */
6905
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   917
	int xl, xu, yl, yu;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   918
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   919
	if (dpi->width + 70 < (1 << (7 + 6))) {
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   920
		xl = GB(l - 70, 7, 6);
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   921
		xu = GB(r,      7, 6);
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   922
	} else {
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   923
		/* scan whole hash row */
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   924
		xl = 0;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   925
		xu = 0x3F;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   926
	}
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   927
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   928
	if (dpi->height + 70 < (1 << (6 + 6))) {
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   929
		yl = GB(t - 70, 6, 6) << 6;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   930
		yu = GB(b,      6, 6) << 6;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   931
	} else {
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   932
		/* scan whole column */
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   933
		yl = 0;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   934
		yu = 0x3F << 6;
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   935
	}
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   936
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   937
	for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   938
		for (int x = xl;; x = (x + 1) & 0x3F) {
cc682e1608bf (svn r10154) -Fix [FS#870]: some vehicles were not drawn when having a high resolution and a high zoom-out level. Patch by B. N. SmatZ!.
rubidium
parents: 6904
diff changeset
   939
			const Vehicle *v = _vehicle_position_hash[x + y]; // already masked & 0xFFF
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   940
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   941
			while (v != NULL) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   942
				if (!(v->vehstatus & VS_HIDDEN) &&
4174
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   943
						l <= v->right_coord &&
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   944
						t <= v->bottom_coord &&
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   945
						r >= v->left_coord &&
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   946
						b >= v->top_coord) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
					DoDrawVehicle(v);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
				}
5574
2cb543714009 (svn r8007) -Codechange: Change _vehicle_position_hash from VehicleID to Vehicle*. This removes the need for look ups by index. Also declare the array static.
peter1138
parents: 5475
diff changeset
   949
				v = v->next_hash;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   950
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
4174
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   952
			if (x == xu) break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
		}
4174
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   954
0b13c25586f7 (svn r5612) Restructure some code which uses the vehicle position hash to make it a bit clearer what's going on
tron
parents: 4165
diff changeset
   955
		if (y == yu) break;
0
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
2116
23031555ff54 (svn r2626) static, const, misc.
tron
parents: 2115
diff changeset
   959
Vehicle *CheckClickOnVehicle(const ViewPort *vp, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
	Vehicle *found = NULL, *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
	uint dist, best_dist = (uint)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   964
	if ((uint)(x -= vp->left) >= (uint)vp->width || (uint)(y -= vp->top) >= (uint)vp->height) return NULL;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
6626
207875b9069a (svn r9846) -Codechange: introduced ZOOM_LVL_MIN and ZOOM_LVL_MAX for the obvious reasons
truelight
parents: 6621
diff changeset
   966
	x = ScaleByZoom(x, vp->zoom) + vp->virtual_left;
207875b9069a (svn r9846) -Codechange: introduced ZOOM_LVL_MIN and ZOOM_LVL_MAX for the obvious reasons
truelight
parents: 6621
diff changeset
   967
	y = ScaleByZoom(y, vp->zoom) + vp->virtual_top;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
	FOR_ALL_VEHICLES(v) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
   970
		if ((v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
				x >= v->left_coord && x <= v->right_coord &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
				y >= v->top_coord && y <= v->bottom_coord) {
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
   973
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   974
			dist = max(
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   975
				abs(((v->left_coord + v->right_coord) >> 1) - x),
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
   976
				abs(((v->top_coord + v->bottom_coord) >> 1) - y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
			);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
			if (dist < best_dist) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
				found = v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
				best_dist = dist;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
	return found;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
7803
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   989
void CheckVehicle32Day(Vehicle *v)
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   990
{
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   991
	if ((v->day_counter & 0x1F) != 0) return;
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   992
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   993
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_32DAY_CALLBACK, 0, 0, v->engine_type, v);
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   994
	if (callback == CALLBACK_FAILED) return;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   995
	if (HasBit(callback, 0)) TriggerVehicle(v, VEHICLE_TRIGGER_CALLBACK_32); // Trigger vehicle trigger 10
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
   996
	if (HasBit(callback, 1)) v->colormap = PAL_NONE;                         // Update colormap via callback 2D
7803
55307751965b (svn r11353) -Codechange: add support for newgrf callback 32
glx
parents: 7802
diff changeset
   997
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
void DecreaseVehicleValue(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
	v->value -= v->value >> 8;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
}
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
static const byte _breakdown_chance[64] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1006
	  3,   3,   3,   3,   3,   3,   3,   3,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1007
	  4,   4,   5,   5,   6,   6,   7,   7,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1008
	  8,   8,   9,   9,  10,  10,  11,  11,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1009
	 12,  13,  13,  13,  13,  14,  15,  16,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1010
	 17,  19,  21,  25,  28,  31,  34,  37,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1011
	 40,  44,  48,  52,  56,  60,  64,  68,
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  1012
	 72,  80,  90, 100, 110, 120, 130, 140,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1013
	150, 170, 190, 210, 230, 250, 250, 250,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
void CheckVehicleBreakdown(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1017
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
	int rel, rel_old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1020
	/* decrease reliability */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
	v->reliability = rel = max((rel_old = v->reliability) - v->reliability_spd_dec, 0);
8980
3f434ded0a30 (svn r12772) -Codechange: some vehicle.cpp coding style.
rubidium
parents: 8970
diff changeset
  1022
	if ((rel_old >> 8) != (rel >> 8)) InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1024
	if (v->breakdown_ctr != 0 || v->vehstatus & VS_STOPPED ||
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1025
			_settings_game.difficulty.vehicle_breakdowns < 1 ||
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1026
			v->cur_speed < 5 || _game_mode == GM_MENU) {
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1027
		return;
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1028
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1029
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1030
	uint32 r = Random();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1031
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1032
	/* increase chance of failure */
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1033
	int chance = v->breakdown_chance + 1;
8980
3f434ded0a30 (svn r12772) -Codechange: some vehicle.cpp coding style.
rubidium
parents: 8970
diff changeset
  1034
	if (Chance16I(1, 25, r)) chance += 25;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
	v->breakdown_chance = min(255, chance);
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
	/* calculate reliability value to use in comparison */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1038
	rel = v->reliability;
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1039
	if (v->type == VEH_SHIP) rel += 0x6666;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1040
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1041
	/* reduced breakdowns? */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1042
	if (_settings_game.difficulty.vehicle_breakdowns == 1) rel += 0x6666;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
	/* check if to break down */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
	if (_breakdown_chance[(uint)min(rel, 0xffff) >> 10] <= v->breakdown_chance) {
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: 2116
diff changeset
  1046
		v->breakdown_ctr    = GB(r, 16, 6) + 0x3F;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 2116
diff changeset
  1047
		v->breakdown_delay  = GB(r, 24, 7) + 0x80;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
		v->breakdown_chance = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
static const StringID _vehicle_type_names[4] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	STR_019F_TRAIN,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
	STR_019C_ROAD_VEHICLE,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
	STR_019E_SHIP,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
	STR_019D_AIRCRAFT,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1057
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1058
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1059
static void ShowVehicleGettingOld(Vehicle *v, StringID msg)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1060
{
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1061
	if (v->owner != _local_player) return;
812
65ecc321b3db (svn r1283) -Add: AutoRenew is now a client-side patch instead of a game-side patch
truelight
parents: 755
diff changeset
  1062
7544
cac538ba670a (svn r11064) -Fix [FS#553]: when autorenew is enabled and it cannot renew the vehicle anymore (because the player cannot build the engine), the aging warnings as if autorenew is not enabled are shown.
rubidium
parents: 7532
diff changeset
  1063
	/* Do not show getting-old message if autorenew is active (and it can replace the vehicle) */
cac538ba670a (svn r11064) -Fix [FS#553]: when autorenew is enabled and it cannot renew the vehicle anymore (because the player cannot build the engine), the aging warnings as if autorenew is not enabled are shown.
rubidium
parents: 7532
diff changeset
  1064
	if (GetPlayer(v->owner)->engine_renew && GetEngine(v->engine_type)->player_avail != 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1065
5955
fe61588d5188 (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 5944
diff changeset
  1066
	SetDParam(0, _vehicle_type_names[v->type]);
534
306bc86eb23e (svn r901) Small step in the process to clean up the DPARAM mess:
tron
parents: 507
diff changeset
  1067
	SetDParam(1, v->unitnumber);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9224
diff changeset
  1068
	AddNewsItem(msg, NS_ADVICE, v->index, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1069
}
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
void AgeVehicle(Vehicle *v)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1072
{
7544
cac538ba670a (svn r11064) -Fix [FS#553]: when autorenew is enabled and it cannot renew the vehicle anymore (because the player cannot build the engine), the aging warnings as if autorenew is not enabled are shown.
rubidium
parents: 7532
diff changeset
  1073
	if (v->age < 65535) v->age++;
cac538ba670a (svn r11064) -Fix [FS#553]: when autorenew is enabled and it cannot renew the vehicle anymore (because the player cannot build the engine), the aging warnings as if autorenew is not enabled are shown.
rubidium
parents: 7532
diff changeset
  1074
cac538ba670a (svn r11064) -Fix [FS#553]: when autorenew is enabled and it cannot renew the vehicle anymore (because the player cannot build the engine), the aging warnings as if autorenew is not enabled are shown.
rubidium
parents: 7532
diff changeset
  1075
	int age = v->age - v->max_age;
cac538ba670a (svn r11064) -Fix [FS#553]: when autorenew is enabled and it cannot renew the vehicle anymore (because the player cannot build the engine), the aging warnings as if autorenew is not enabled are shown.
rubidium
parents: 7532
diff changeset
  1076
	if (age == 366*0 || age == 366*1 || age == 366*2 || age == 366*3 || age == 366*4) v->reliability_spd_dec <<= 1;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1077
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1078
	InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1079
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1080
	if (age == -366) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1081
		ShowVehicleGettingOld(v, STR_01A0_IS_GETTING_OLD);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1082
	} else if (age == 0) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1083
		ShowVehicleGettingOld(v, STR_01A1_IS_GETTING_VERY_OLD);
7545
d44e19c5671e (svn r11065) -Documentation [FS#1186]: of the dirty marking/repainting subsystem. Patch by Progman.
rubidium
parents: 7544
diff changeset
  1084
	} else if (age > 0 && (age % 366) == 0) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1085
		ShowVehicleGettingOld(v, STR_01A2_IS_GETTING_VERY_OLD_AND);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1086
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1087
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1088
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1089
/** Start/Stop a vehicle
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1090
 * @param tile unused
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1091
 * @param flags type of operation
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1092
 * @param p1 vehicle to start/stop
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1093
 * @param p2 bit 0: Shall the start/stop newgrf callback be evaluated (only valid with DC_AUTOREPLACE for network safety)
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1094
 * @return result of operation.  Nothing if everything went well
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1095
 */
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1096
CommandCost CmdStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1097
{
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1098
	/* Disable the effect of p2 bit 0, when DC_AUTOREPLACE is not set */
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1099
	if ((flags & DC_AUTOREPLACE) == 0) SetBit(p2, 0);
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1100
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1101
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1102
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1103
	Vehicle *v = GetVehicle(p1);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1104
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1105
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1106
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1107
	switch (v->type) {
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1108
		case VEH_TRAIN:
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1109
			if (v->vehstatus & VS_STOPPED && v->u.rail.cached_power == 0) return_cmd_error(STR_TRAIN_START_NO_CATENARY);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1110
			break;
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1111
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1112
		case VEH_SHIP:
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1113
		case VEH_ROAD:
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1114
			break;
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1115
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1116
		case VEH_AIRCRAFT:
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1117
			/* cannot stop airplane when in flight, or when taking off / landing */
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1118
			if (v->u.air.state >= STARTTAKEOFF && v->u.air.state < TERM7) return_cmd_error(STR_A017_AIRCRAFT_IS_IN_FLIGHT);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1119
			break;
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1120
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1121
		default: return CMD_ERROR;
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1122
	}
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1123
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1124
	/* Check if this vehicle can be started/stopped. The callback will fail or
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1125
	 * return 0xFF if it can. */
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1126
	uint16 callback = GetVehicleCallback(CBID_VEHICLE_START_STOP_CHECK, 0, 0, v->engine_type, v);
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1127
	if (callback != CALLBACK_FAILED && GB(callback, 0, 8) != 0xFF && HasBit(p2, 0)) {
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1128
		StringID error = GetGRFStringID(GetEngineGRFID(v->engine_type), 0xD000 + callback);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1129
		return_cmd_error(error);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1130
	}
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1131
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1132
	if (flags & DC_EXEC) {
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1133
		static const StringID vehicle_waiting_in_depot[] = {
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1134
			STR_8814_TRAIN_IS_WAITING_IN_DEPOT,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1135
			STR_9016_ROAD_VEHICLE_IS_WAITING,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1136
			STR_981C_SHIP_IS_WAITING_IN_DEPOT,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1137
			STR_A014_AIRCRAFT_IS_WAITING_IN,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1138
		};
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1139
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1140
		static const WindowClass vehicle_list[] = {
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1141
			WC_TRAINS_LIST,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1142
			WC_ROADVEH_LIST,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1143
			WC_SHIPS_LIST,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1144
			WC_AIRCRAFT_LIST,
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1145
		};
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1146
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1147
		if (v->IsStoppedInDepot() && (flags & DC_AUTOREPLACE) == 0) DeleteVehicleNews(p1, vehicle_waiting_in_depot[v->type]);
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1148
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1149
		v->vehstatus ^= VS_STOPPED;
9940
a3a283320355 (svn r14095) -Fix (r14076): Trains shall not stop instantly.
frosch
parents: 9934
diff changeset
  1150
		if (v->type != VEH_TRAIN) v->cur_speed = 0; // trains can stop 'slowly'
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1151
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1152
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1153
		InvalidateWindowClasses(vehicle_list[v->type]);
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1154
	}
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1155
	return CommandCost();
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1156
}
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1157
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1158
/** Starts or stops a lot of vehicles
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1159
 * @param tile Tile of the depot where the vehicles are started/stopped (only used for depots)
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1160
 * @param flags type of operation
4762
771ccf746531 (svn r6676) -Fix: [vehicle list windows] mass start/stop now works correctly in shared orders and station lists
bjarni
parents: 4741
diff changeset
  1161
 * @param p1 Station/Order/Depot ID (only used for vehicle list windows)
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1162
 * @param p2 bitmask
4762
771ccf746531 (svn r6676) -Fix: [vehicle list windows] mass start/stop now works correctly in shared orders and station lists
bjarni
parents: 4741
diff changeset
  1163
 *   - bit 0-4 Vehicle type
771ccf746531 (svn r6676) -Fix: [vehicle list windows] mass start/stop now works correctly in shared orders and station lists
bjarni
parents: 4741
diff changeset
  1164
 *   - bit 5 false = start vehicles, true = stop vehicles
771ccf746531 (svn r6676) -Fix: [vehicle list windows] mass start/stop now works correctly in shared orders and station lists
bjarni
parents: 4741
diff changeset
  1165
 *   - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1166
 *   - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1167
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1168
CommandCost CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1169
{
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1170
	VehicleList list;
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1171
	CommandCost return_value = CMD_ERROR;
6641
1b14f8887cb7 (svn r9872) -Codechange: more type strictness for vehicle types
rubidium
parents: 6638
diff changeset
  1172
	VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1173
	bool start_stop = HasBit(p2, 5);
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1174
	bool vehicle_list_window = HasBit(p2, 6);
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1175
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1176
	if (vehicle_list_window) {
5748
cf2f07fa20ca (svn r8296) -Fix: GenerateVehicleSortList() tried to put a TileIndex into an uint16
bjarni
parents: 5747
diff changeset
  1177
		uint32 id = p1;
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1178
		uint16 window_type = p2 & VLW_MASK;
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1179
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1180
		GenerateVehicleSortList(&list, vehicle_type, _current_player, id, window_type);
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1181
	} else {
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1182
		/* Get the list of vehicles in the depot */
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1183
		BuildDepotVehicleList(vehicle_type, tile, &list, NULL);
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1184
	}
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1185
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1186
	for (uint i = 0; i < list.Length(); i++) {
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1187
		const Vehicle *v = list[i];
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1188
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1189
		if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1190
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1191
		if (!vehicle_list_window) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1192
			if (vehicle_type == VEH_TRAIN) {
4673
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1193
				if (CheckTrainInDepot(v, false) == -1) continue;
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1194
			} else {
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1195
				if (!(v->vehstatus & VS_HIDDEN)) continue;
3160067b8751 (svn r6570) -Feature: added "start all" and "stop all" buttons to the vehicle lists
bjarni
parents: 4662
diff changeset
  1196
			}
4648
7bde2a5c36e8 (svn r6524) -Code cleanup r6515: cleaned up the command to start/stop all vehicles in a depot.
bjarni
parents: 4640
diff changeset
  1197
		}
7bde2a5c36e8 (svn r6524) -Code cleanup r6515: cleaned up the command to start/stop all vehicles in a depot.
bjarni
parents: 4640
diff changeset
  1198
9921
4dfac3c48507 (svn r14076) -Codechange: Merge the four start/stop commands into a single CMD_START_STOP_VEHICLE.
frosch
parents: 9914
diff changeset
  1199
		CommandCost ret = DoCommand(tile, v->index, 0, flags, CMD_START_STOP_VEHICLE);
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1200
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1201
		if (CmdSucceeded(ret)) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1202
			return_value = CommandCost();
4640
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1203
			/* We know that the command is valid for at least one vehicle.
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1204
			 * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1205
			if (!(flags & DC_EXEC)) break;
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1206
		}
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1207
	}
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1208
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1209
	return return_value;
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1210
}
4e380e2ecfa7 (svn r6515) -Feature: added "start all" and "stop all" buttons to the depot windows
bjarni
parents: 4635
diff changeset
  1211
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1212
/** Sells all vehicles in a depot
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1213
 * @param tile Tile of the depot where the depot is
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1214
 * @param flags type of operation
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1215
 * @param p1 Vehicle type
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1216
 * @param p2 unused
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1217
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1218
CommandCost CmdDepotSellAllVehicles(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1219
{
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1220
	VehicleList list;
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1221
8245
86caf8eb69dd (svn r11809) -Fix [FS#1643]: set expenses type when selling all vehicles in depot
smatz
parents: 8238
diff changeset
  1222
	CommandCost cost(EXPENSES_NEW_VEHICLES);
9345
498b7ff11b25 (svn r13241) -Codechange: Simplify CmdDepotSellAllVehicles() a bit
peter1138
parents: 9344
diff changeset
  1223
	uint sell_command;
6641
1b14f8887cb7 (svn r9872) -Codechange: more type strictness for vehicle types
rubidium
parents: 6638
diff changeset
  1224
	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1225
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1226
	switch (vehicle_type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1227
		case VEH_TRAIN:    sell_command = CMD_SELL_RAIL_WAGON; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1228
		case VEH_ROAD:     sell_command = CMD_SELL_ROAD_VEH;   break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1229
		case VEH_SHIP:     sell_command = CMD_SELL_SHIP;       break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1230
		case VEH_AIRCRAFT: sell_command = CMD_SELL_AIRCRAFT;   break;
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1231
		default: return CMD_ERROR;
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1232
	}
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1233
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1234
	/* Get the list of vehicles in the depot */
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1235
	BuildDepotVehicleList(vehicle_type, tile, &list, &list);
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1236
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1237
	for (uint i = 0; i < list.Length(); i++) {
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1238
		CommandCost ret = DoCommand(tile, list[i]->index, 1, flags, sell_command);
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1239
		if (CmdSucceeded(ret)) cost.AddCost(ret);
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1240
	}
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1241
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1242
	if (cost.GetCost() == 0) return CMD_ERROR; // no vehicles to sell
4659
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1243
	return cost;
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1244
}
e476c76389a8 (svn r6542) -Feature: [depot window] added a "sell all vehicles in depot" button
bjarni
parents: 4656
diff changeset
  1245
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1246
/** Autoreplace all vehicles in the depot
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1247
 * Note: this command can make incorrect cost estimations
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1248
 * Luckily the final price can only drop, not increase. This is due to the fact that
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1249
 * estimation can't predict wagon removal so it presumes worst case which is no income from selling wagons.
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1250
 * @param tile Tile of the depot where the vehicles are
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1251
 * @param flags type of operation
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1252
 * @param p1 Type of vehicle
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1253
 * @param p2 If bit 0 is set, then either replace all or nothing (instead of replacing until money runs out)
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1254
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1255
CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1256
{
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1257
	VehicleList list;
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1258
	CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES);
6641
1b14f8887cb7 (svn r9872) -Codechange: more type strictness for vehicle types
rubidium
parents: 6638
diff changeset
  1259
	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1260
	bool all_or_nothing = HasBit(p2, 0);
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1261
7512
dd5d439e1337 (svn r11027) -Fix: do not unconditionally assume that a tile has a depot.
rubidium
parents: 7502
diff changeset
  1262
	if (!IsDepotTile(tile) || !IsTileOwner(tile, _current_player)) return CMD_ERROR;
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1263
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1264
	/* Get the list of vehicles in the depot */
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1265
	BuildDepotVehicleList(vehicle_type, tile, &list, &list);
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1266
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1267
	bool did_something = false;
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1268
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1269
	for (uint i = 0; i < list.Length(); i++) {
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1270
		Vehicle *v = (Vehicle*)list[i];
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1271
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1272
		/* Ensure that the vehicle completely in the depot */
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7486
diff changeset
  1273
		if (!v->IsInDepot()) continue;
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1274
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1275
		CommandCost ret = DoCommand(0, v->index, 0, flags, CMD_AUTOREPLACE_VEHICLE);
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1276
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1277
		if (CmdSucceeded(ret)) {
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1278
			did_something = true;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1279
			cost.AddCost(ret);
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1280
		} else {
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1281
			if (ret.GetErrorMessage() != STR_AUTOREPLACE_NOTHING_TO_DO && all_or_nothing) {
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1282
				/* We failed to replace a vehicle even though we set all or nothing.
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1283
				 * We should never reach this if DC_EXEC is set since then it should
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1284
				 * have failed the estimation guess. */
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1285
				assert(!(flags & DC_EXEC));
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1286
				/* Now we will have to return an error. */
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1287
				return CMD_ERROR;
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1288
			}
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1289
		}
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1290
	}
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1291
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1292
	if (!did_something) {
8736
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1293
		/* Either we didn't replace anything or something went wrong.
ff99c0258370 (svn r12421) -Feature: [autoreplace] the autoreplace button in train depots will now also replace wagons even if they aren't connected to a locomotive
bjarni
parents: 8673
diff changeset
  1294
		 * Either way we want to return an error and not execute this command. */
4662
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1295
		cost = CMD_ERROR;
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1296
	}
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1297
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1298
	return cost;
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1299
}
e36935c7bdde (svn r6552) -Feature: [depot window] added a "autoreplace all vehicles in depot" button
bjarni
parents: 4659
diff changeset
  1300
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1301
/** Clone a vehicle. If it is a train, it will clone all the cars too
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1302
 * @param tile tile of the depot where the cloned vehicle is build
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1303
 * @param flags type of operation
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1304
 * @param p1 the original vehicle's index
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1305
 * @param p2 1 = shared orders, else copied orders
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1306
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1307
CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1308
{
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1309
	CommandCost total_cost(EXPENSES_NEW_VEHICLES);
3816
28ee8b8c2522 (svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
bjarni
parents: 3815
diff changeset
  1310
	uint32 build_argument = 2;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1311
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4346
diff changeset
  1312
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1313
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1314
	Vehicle *v = GetVehicle(p1);
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1315
	Vehicle *v_front = v;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1316
	Vehicle *w = NULL;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1317
	Vehicle *w_front = NULL;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1318
	Vehicle *w_rear = NULL;
6444
31f400a4b890 (svn r9581) -Revert (r9562): This broke as much as it fixed, so revert until a proper way of paying for refits when cloning can be found.
maedhros
parents: 6426
diff changeset
  1319
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1320
	/*
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1321
	 * v_front is the front engine in the original vehicle
6444
31f400a4b890 (svn r9581) -Revert (r9562): This broke as much as it fixed, so revert until a proper way of paying for refits when cloning can be found.
maedhros
parents: 6426
diff changeset
  1322
	 * v is the car/vehicle of the original vehicle, that is currently being copied
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1323
	 * w_front is the front engine of the cloned vehicle
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1324
	 * w is the car/vehicle currently being cloned
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1325
	 * w_rear is the rear end of the cloned train. It's used to add more cars and is only used by trains
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1326
	 */
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1327
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1328
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1329
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1330
	if (v->type == VEH_TRAIN && (!IsFrontEngine(v) || v->u.rail.crash_anim_pos >= 4400)) return CMD_ERROR;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1331
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1332
	/* check that we can allocate enough vehicles */
2601
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1333
	if (!(flags & DC_EXEC)) {
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1334
		int veh_counter = 0;
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1335
		do {
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1336
			veh_counter++;
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
  1337
		} while ((v = v->Next()) != NULL);
2601
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1338
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  1339
		if (!Vehicle::AllocateList(NULL, veh_counter)) {
2606
715bba67addf (svn r3143) -Codechange: greatly increased speed when allocating vehicles
bjarni
parents: 2602
diff changeset
  1340
			return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
2601
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1341
		}
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1342
	}
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1343
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1344
	v = v_front;
d1a193af01e6 (svn r3138) -Fix: [clone vehicles] fixed assert when it was possible to allocate some vehicles to clone a train, but not for all cars
bjarni
parents: 2600
diff changeset
  1345
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1346
	do {
7526
27c4fbf8aef0 (svn r11045) -Codechange: added a function to tell if a vehicle is the rear part of a dualheaded train engine
bjarni
parents: 7512
diff changeset
  1347
		if (v->type == VEH_TRAIN && IsRearDualheaded(v)) {
2676
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1348
			/* we build the rear ends of multiheaded trains with the front ones */
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1349
			continue;
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1350
		}
59b65b4fb480 (svn r3218) -Feature: Multiheaded train engines will now stay in the same train
bjarni
parents: 2666
diff changeset
  1351
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1352
		CommandCost cost = DoCommand(tile, v->engine_type, build_argument, flags, GetCmdBuildVeh(v));
3819
64c55c77567a (svn r4829) -Codechage: [clone vehilces] only check for a free unitnumber for the first engine in the test run as well as execute run (to improve speed. The result should be unchanged)
bjarni
parents: 3816
diff changeset
  1353
		build_argument = 3; // ensure that we only assign a number to the first engine
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1354
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1355
		if (CmdFailed(cost)) return cost;
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1356
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1357
		total_cost.AddCost(cost);
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1358
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1359
		if (flags & DC_EXEC) {
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2635
diff changeset
  1360
			w = GetVehicle(_new_vehicle_id);
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1361
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1362
			if (v->type == VEH_TRAIN && HasBit(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
  1363
				SetBit(w->u.rail.flags, VRF_REVERSE_DIRECTION);
3896
580ff4e6a49e (svn r4967) -Fix: [clone vehicles] a cloned train engine heads the same way as the original (pointed out by bobingabout)
bjarni
parents: 3881
diff changeset
  1364
			}
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1365
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1366
			if (v->type == VEH_TRAIN && !IsFrontEngine(v)) {
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1367
				/* this s a train car
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1368
				 * add this unit to the end of the train */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1369
				CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
6838
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1370
				if (CmdFailed(result)) {
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1371
					/* The train can't be joined to make the same consist as the original.
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1372
					 * Sell what we already made (clean up) and return an error.           */
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1373
					DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1374
					DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1375
					return result; // return error and the message returned from CMD_MOVE_RAIL_VEHICLE
243a588e6a0d (svn r10077) -Fix: FS#845 Cloning Trains Longer Than 8 Units Causes Crash
bjarni
parents: 6823
diff changeset
  1376
				}
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1377
			} else {
7850
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1378
				/* this is a front engine or not a train. */
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1379
				w_front = w;
3679
252b4ab2aad4 (svn r4599) -Fix: [Cloning, autoreplace] FS#141 clone service-interval
bjarni
parents: 3657
diff changeset
  1380
				w->service_interval = v->service_interval;
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1381
			}
4434
a08cb4b5c179 (svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents: 4404
diff changeset
  1382
			w_rear = w; // trains needs to know the last car in the train, so they can add more in next loop
2563
abf7a5e73988 (svn r3100) -Codechange [Clone vehicles] Major change to clone vehicles
bjarni
parents: 2558
diff changeset
  1383
		}
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1384
	} while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1385
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1386
	if (flags & DC_EXEC && v_front->type == VEH_TRAIN) {
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1387
		/* for trains this needs to be the front engine due to the callback function */
3948
a09379beffd5 (svn r5094) Remove _new_{aircraft,roadveh,ship,train,wagon}_id. _new_vehicle_id is enough.
tron
parents: 3896
diff changeset
  1388
		_new_vehicle_id = w_front->index;
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1389
	}
5062
470335730a60 (svn r7115) -Fix [FS#371]: Cloning a vehicle that has been refitted would incur the expense as running costs, not new vehicles.
peter1138
parents: 4972
diff changeset
  1390
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  1391
	if (flags & DC_EXEC) {
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  1392
		/* Cloned vehicles belong to the same group */
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  1393
		DoCommand(0, v_front->group_id, w_front->index, flags, CMD_ADD_VEHICLE_GROUP);
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  1394
	}
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  1395
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  1396
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1397
	/* Take care of refitting. */
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1398
	w = w_front;
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1399
	v = v_front;
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1400
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1401
	/* Both building and refitting are influenced by newgrf callbacks, which
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1402
	 * makes it impossible to accurately estimate the cloning costs. In
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1403
	 * particular, it is possible for engines of the same type to be built with
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1404
	 * different numbers of articulated parts, so when refitting we have to
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1405
	 * loop over real vehicles first, and then the articulated parts of those
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1406
	 * vehicles in a different loop. */
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1407
	do {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1408
		do {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1409
			if (flags & DC_EXEC) {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1410
				assert(w != NULL);
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1411
7812
9b694475d8f5 (svn r11362) -Fix [FS#1380]: cloning vehicles with non-standard sub-cargotypes (i.e. livery refits) failed.
rubidium
parents: 7803
diff changeset
  1412
				if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_subtype) {
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1413
					CommandCost cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1414
					if (CmdSucceeded(cost)) total_cost.AddCost(cost);
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1415
				}
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1416
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1417
				if (w->type == VEH_TRAIN && EngineHasArticPart(w)) {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1418
					w = GetNextArticPart(w);
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1419
				} else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) {
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
  1420
					w = w->Next();
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1421
				} else {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1422
					break;
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1423
				}
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1424
			} else {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1425
				CargoID initial_cargo = GetEngineCargoType(v->engine_type);
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1426
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1427
				if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1428
					total_cost.AddCost(GetRefitCost(v->engine_type));
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1429
				}
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1430
			}
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1431
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1432
			if (v->type == VEH_TRAIN && EngineHasArticPart(v)) {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1433
				v = GetNextArticPart(v);
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1434
			} else if (v->type == VEH_ROAD && RoadVehHasArticPart(v)) {
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
  1435
				v = v->Next();
6857
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1436
			} else {
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1437
				break;
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1438
			}
60130753e595 (svn r10097) -Feature: Add support for articulated road vehicles, or callbacks 11 and 17 for
maedhros
parents: 6838
diff changeset
  1439
		} while (v != NULL);
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1440
6727
7b016b7e17af (svn r9959) -Fix: Only call GetNextVehicle for trains.
maedhros
parents: 6724
diff changeset
  1441
		if ((flags & DC_EXEC) && v->type == VEH_TRAIN) w = GetNextVehicle(w);
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1442
	} while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1443
7850
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1444
	if (flags & DC_EXEC) {
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1445
		/*
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1446
		 * Set the orders of the vehicle. Cannot do it earlier as we need
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1447
		 * the vehicle refitted before doing this, otherwise the moved
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1448
		 * cargo types might not match (passenger vs non-passenger)
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1449
		 */
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1450
		DoCommand(0, (v_front->index << 16) | w_front->index, p2 & 1 ? CO_SHARE : CO_COPY, flags, CMD_CLONE_ORDER);
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1451
	}
1001568e7551 (svn r11400) -Fix [FS#1409]: assign orders during cloning after refitting as it needs the cargo types to be the same.
rubidium
parents: 7812
diff changeset
  1452
6546
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1453
	/* Since we can't estimate the cost of cloning a vehicle accurately we must
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1454
	 * check whether the player has enough money manually. */
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1455
	if (!CheckPlayerHasMoney(total_cost)) {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1456
		if (flags & DC_EXEC) {
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1457
			/* The vehicle has already been bought, so now it must be sold again. */
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1458
			DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1459
		}
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1460
		return CMD_ERROR;
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1461
	}
4c6d90bde803 (svn r9742) -Fix (r9689) [FS#739]: Fix cloning with refit costs again, hopefully for good this time.
maedhros
parents: 6526
diff changeset
  1462
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1463
	return total_cost;
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1464
}
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1465
9348
dc680f675138 (svn r13245) -Codechange: Use SmallVectors for generating vehicle lists, simplifying calling code somewhat.
peter1138
parents: 9345
diff changeset
  1466
/**
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1467
 * Send all vehicles of type to depots
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1468
 * @param type type of vehicle
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1469
 * @param flags the flags used for DoCommand()
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1470
 * @param service should the vehicles only get service in the depots
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1471
 * @param owner PlayerID of owner of the vehicles to send
6484
79156627e1ba (svn r9665) -Documentation: Doxygen corrections,errors, corrections of corrections...
belugas
parents: 6453
diff changeset
  1472
 * @param vlw_flag tells what kind of list requested the goto depot
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1473
 * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  1474
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1475
CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id)
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1476
{
9353
9d76aef9fe2b (svn r13250) -Codechange: Use VehicleList in SendAllVehiclesToDepot()
peter1138
parents: 9351
diff changeset
  1477
	VehicleList list;
9d76aef9fe2b (svn r13250) -Codechange: Use VehicleList in SendAllVehiclesToDepot()
peter1138
parents: 9351
diff changeset
  1478
9d76aef9fe2b (svn r13250) -Codechange: Use VehicleList in SendAllVehiclesToDepot()
peter1138
parents: 9351
diff changeset
  1479
	GenerateVehicleSortList(&list, type, owner, id, vlw_flag);
4465
262c344f6f3d (svn r6249) -Fix: fixed assert when pressing goto depot in an empty list (forgot to disable the button in this condition)
bjarni
parents: 4463
diff changeset
  1480
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1481
	/* Send all the vehicles to a depot */
9353
9d76aef9fe2b (svn r13250) -Codechange: Use VehicleList in SendAllVehiclesToDepot()
peter1138
parents: 9351
diff changeset
  1482
	for (uint i = 0; i < list.Length(); i++) {
9d76aef9fe2b (svn r13250) -Codechange: Use VehicleList in SendAllVehiclesToDepot()
peter1138
parents: 9351
diff changeset
  1483
		const Vehicle *v = list[i];
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1484
		CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
4560
434cb4b568d4 (svn r6407) -Fix: Check return values of DoCommand() with CmdFailed and that of DoCommandP
Darkvater
parents: 4555
diff changeset
  1485
434cb4b568d4 (svn r6407) -Fix: Check return values of DoCommand() with CmdFailed and that of DoCommandP
Darkvater
parents: 4555
diff changeset
  1486
		/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
434cb4b568d4 (svn r6407) -Fix: Check return values of DoCommand() with CmdFailed and that of DoCommandP
Darkvater
parents: 4555
diff changeset
  1487
			* In this case we know that at least one vehicle can be sent to a depot
434cb4b568d4 (svn r6407) -Fix: Check return values of DoCommand() with CmdFailed and that of DoCommandP
Darkvater
parents: 4555
diff changeset
  1488
			* and we will issue the command. We can now safely quit the loop, knowing
434cb4b568d4 (svn r6407) -Fix: Check return values of DoCommand() with CmdFailed and that of DoCommandP
Darkvater
parents: 4555
diff changeset
  1489
			* it will succeed at least once. With DC_EXEC we really need to send them to the depot */
6946
a48b23f1cc8a (svn r10200) -Codechange: add "shortcut" for !CmdFailed (CmdSucceeded).
rubidium
parents: 6943
diff changeset
  1490
		if (CmdSucceeded(ret) && !(flags & DC_EXEC)) {
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1491
			return CommandCost();
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1492
		}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1493
	}
4465
262c344f6f3d (svn r6249) -Fix: fixed assert when pressing goto depot in an empty list (forgot to disable the button in this condition)
bjarni
parents: 4463
diff changeset
  1494
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1495
	return (flags & DC_EXEC) ? CommandCost() : CMD_ERROR;
4463
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1496
}
4d67d3a36fc5 (svn r6246) -Feature: added the many times requested "send all vehicle to depot" button
bjarni
parents: 4451
diff changeset
  1497
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1498
/**
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1499
 * Calculates how full a vehicle is.
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1500
 * @param v The Vehicle to check. For trains, use the first engine.
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1501
 * @param color The string to show depending on if we are unloading or loading
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1502
 * @return A percentage of how full the Vehicle is.
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1503
 */
9221
1cd04b558663 (svn r13087) -Codechange: Constify CalcPercentVehicleFilled() (michi_cc)
peter1138
parents: 9184
diff changeset
  1504
uint8 CalcPercentVehicleFilled(const Vehicle *v, StringID *color)
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1505
{
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1506
	int count = 0;
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1507
	int max = 0;
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1508
	int cars = 0;
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1509
	int unloading = 0;
7087
0e076cedc6fd (svn r10354) -Fix [FS#950]: loading indicator showed "^" when the train would load at the given station.
rubidium
parents: 7086
diff changeset
  1510
	bool loading = false;
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1511
7087
0e076cedc6fd (svn r10354) -Fix [FS#950]: loading indicator showed "^" when the train would load at the given station.
rubidium
parents: 7086
diff changeset
  1512
	const Vehicle *u = v;
8902
545851a52ad5 (svn r12669) -Fix (r12667): crash when a vehicle has never been to a station and the load percentage conditional variable is evaluated.
rubidium
parents: 8897
diff changeset
  1513
	const Station *st = v->last_station_visited != INVALID_STATION ? GetStation(v->last_station_visited) : NULL;
7087
0e076cedc6fd (svn r10354) -Fix [FS#950]: loading indicator showed "^" when the train would load at the given station.
rubidium
parents: 7086
diff changeset
  1514
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1515
	/* Count up max and used */
7492
35acee076719 (svn r11003) -Codechange: replace Vehicle->next to Vehicle->Next() and Vehicle->SetNext() so we can trap instances that change a next pointer and (in the future) update the first/previous pointers based on that.
rubidium
parents: 7490
diff changeset
  1516
	for (; v != NULL; v = v->Next()) {
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  1517
		count += v->cargo.Count();
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1518
		max += v->cargo_cap;
8902
545851a52ad5 (svn r12669) -Fix (r12667): crash when a vehicle has never been to a station and the load percentage conditional variable is evaluated.
rubidium
parents: 8897
diff changeset
  1519
		if (v->cargo_cap != 0 && color != NULL) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  1520
			unloading += HasBit(v->vehicle_flags, VF_CARGO_UNLOADING) ? 1 : 0;
8855
6d841d5d4789 (svn r12617) -Codechange: add type safety to the Order's load and unload types.
rubidium
parents: 8853
diff changeset
  1521
			loading |= !(u->current_order.GetUnloadType() & OUFB_UNLOAD) && st->goods[v->cargo_type].days_since_pickup != 255;
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1522
			cars++;
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1523
		}
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1524
	}
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1525
8897
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1526
	if (color != NULL) {
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1527
		if (unloading == 0 && loading) {
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1528
			*color = STR_PERCENT_UP;
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1529
		} else if (cars == unloading || !loading) {
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1530
			*color = STR_PERCENT_DOWN;
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1531
		} else {
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1532
			*color = STR_PERCENT_UP_DOWN;
d53dbad308cc (svn r12664) -Codechange: do not force the 'color' pointer to be non-NULL when trying to get the load percentage.
rubidium
parents: 8892
diff changeset
  1533
		}
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1534
	}
7014
533521754123 (svn r10270) -Add: prefixed the loading indicator with an arrow, up meaning vehicle is loading, down meaning vehicle is unloading
truelight
parents: 7010
diff changeset
  1535
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1536
	/* Train without capacity */
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1537
	if (max == 0) return 100;
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1538
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1539
	/* Return the percentage */
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1540
	return (count * 100) / max;
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1541
}
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  1542
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1543
void VehicleEnterDepot(Vehicle *v)
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1544
{
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1545
	switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1546
		case VEH_TRAIN:
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1547
			InvalidateWindowClasses(WC_TRAINS_LIST);
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1548
			if (!IsFrontEngine(v)) v = v->First();
8300
dfd530665621 (svn r11864) -Codechange: pass owner whose signals we will update instead of complex detection later
smatz
parents: 8270
diff changeset
  1549
			UpdateSignalsOnSegment(v->tile, INVALID_DIAGDIR, v->owner);
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1550
			v->load_unload_time_rem = 0;
8938
9147361fb2be (svn r12711) -Codechange: remove unneeded loops
glx
parents: 8914
diff changeset
  1551
			ClrBit(v->u.rail.flags, VRF_TOGGLE_REVERSE);
9704
54123af5f9a5 (svn r13816) -Fix [FS#2150]: check for vehicle length changes outside a depot (callback 0x11) and give a warning about that
smatz
parents: 9652
diff changeset
  1552
			TrainConsistChanged(v, true);
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1553
			break;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1554
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1555
		case VEH_ROAD:
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1556
			InvalidateWindowClasses(WC_ROADVEH_LIST);
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  1557
			if (!IsRoadVehFront(v)) v = v->First();
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1558
			break;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1559
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1560
		case VEH_SHIP:
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1561
			InvalidateWindowClasses(WC_SHIPS_LIST);
5993
956d341d930e (svn r8701) -Codechange: replace magic numbers with enums for u.rail.track.
rubidium
parents: 5991
diff changeset
  1562
			v->u.ship.state = TRACK_BIT_DEPOT;
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1563
			RecalcShipStuff(v);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1564
			break;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1565
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1566
		case VEH_AIRCRAFT:
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1567
			InvalidateWindowClasses(WC_AIRCRAFT_LIST);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1568
			HandleAircraftEnterHangar(v);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1569
			break;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1570
		default: NOT_REACHED();
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1571
	}
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1572
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1573
	if (v->type != VEH_TRAIN) {
4739
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4736
diff changeset
  1574
		/* Trains update the vehicle list when the first unit enters the depot and calls VehicleEnterDepot() when the last unit enters.
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4736
diff changeset
  1575
		 * We only increase the number of vehicles when the first one enters, so we will not need to search for more vehicles in the depot */
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4736
diff changeset
  1576
		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
e626ef1b522e (svn r6651) -Coding feature: added the windowevent WE_INVALIDATE_DATA
bjarni
parents: 4736
diff changeset
  1577
	}
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1578
	InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1579
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1580
	v->vehstatus |= VS_HIDDEN;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1581
	v->cur_speed = 0;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1582
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1583
	VehicleServiceInDepot(v);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1584
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1585
	TriggerVehicle(v, VEHICLE_TRIGGER_DEPOT);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1586
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  1587
	if (v->current_order.IsType(OT_GOTO_DEPOT)) {
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1588
		InvalidateWindow(WC_VEHICLE_VIEW, v->index);
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1589
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1590
		Order t = v->current_order;
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  1591
		v->current_order.MakeDummy();
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1592
8838
068d63397dc3 (svn r12586) -Codechange: do not access an order's refit variables directly.
rubidium
parents: 8837
diff changeset
  1593
		if (t.IsRefit()) {
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1594
			_current_player = v->owner;
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1595
			CommandCost cost = DoCommand(v->tile, v->index, t.GetRefitCargo() | t.GetRefitSubtype() << 8, DC_EXEC, GetCmdRefitVeh(v));
4783
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1596
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1597
			if (CmdFailed(cost)) {
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1598
				v->leave_depot_instantly = false; // We ensure that the vehicle stays in the depot
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1599
				if (v->owner == _local_player) {
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1600
					/* Notify the user that we stopped the vehicle */
5955
fe61588d5188 (svn r8621) -Codechange: assigned new numbers to the VEH_(type) enum so that VEH_Train is 0, VEH_Road is 1 and so on
bjarni
parents: 5944
diff changeset
  1601
					SetDParam(0, _vehicle_type_names[v->type]);
4783
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1602
					SetDParam(1, v->unitnumber);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9224
diff changeset
  1603
					AddNewsItem(STR_ORDER_REFIT_FAILED, NS_ADVICE, v->index, 0);
4783
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1604
				}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1605
			} else if (v->owner == _local_player && cost.GetCost() != 0) {
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1606
				ShowCostOrIncomeAnimation(v->x_pos, v->y_pos, v->z_pos, cost.GetCost());
4783
3d3cbea8b503 (svn r6697) -Fix: [order refit] if an order refit fails, the vehicle is now stopped and the player gets a message (like vehicle stopped in depot)
bjarni
parents: 4782
diff changeset
  1607
			}
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1608
		}
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1609
8853
3266f0374302 (svn r12615) -Codechange: rename some enums related to depot orders to make it more clear that they are no loading/unloading flags. Also add more type strictness.
rubidium
parents: 8848
diff changeset
  1610
		if (t.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) {
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1611
			/* Part of orders */
7070
2f4e1b2facc6 (svn r10335) -Fix (r10331): Increment the current order index *after* using it in UpdateVehicleTimetable.
maedhros
parents: 7038
diff changeset
  1612
			UpdateVehicleTimetable(v, true);
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1613
			v->cur_order_index++;
8853
3266f0374302 (svn r12615) -Codechange: rename some enums related to depot orders to make it more clear that they are no loading/unloading flags. Also add more type strictness.
rubidium
parents: 8848
diff changeset
  1614
		} else if (t.GetDepotActionType() & ODATFB_HALT) {
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1615
			/* Force depot visit */
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1616
			v->vehstatus |= VS_STOPPED;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1617
			if (v->owner == _local_player) {
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1618
				StringID string;
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1619
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1620
				switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1621
					case VEH_TRAIN:    string = STR_8814_TRAIN_IS_WAITING_IN_DEPOT; break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1622
					case VEH_ROAD:     string = STR_9016_ROAD_VEHICLE_IS_WAITING;   break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1623
					case VEH_SHIP:     string = STR_981C_SHIP_IS_WAITING_IN_DEPOT;  break;
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1624
					case VEH_AIRCRAFT: string = STR_A014_AIRCRAFT_IS_WAITING_IN;    break;
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1625
					default: NOT_REACHED(); string = STR_EMPTY; // Set the string to something to avoid a compiler warning
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1626
				}
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1627
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1628
				SetDParam(0, v->unitnumber);
9234
bfc9d27d3d0d (svn r13100) -Codechange: reduce the amount of parameters passed via AddNewsMessage as there is (for each news message type) a tuple of 4 parameters that is the same for all calls.
rubidium
parents: 9224
diff changeset
  1629
				AddNewsItem(string, NS_ADVICE, v->index, 0);
4725
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1630
			}
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1631
		}
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1632
	}
40cccaaa042c (svn r6637) -Codechange: merged all (vehicle type)EnterDepot into VehicleEnterDepot()
bjarni
parents: 4712
diff changeset
  1633
}
2244
417a472e7cb5 (svn r2764) -Feature: Clone vehicles
bjarni
parents: 2186
diff changeset
  1634
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1635
static bool IsUniqueVehicleName(const char *name)
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1636
{
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1637
	const Vehicle *v;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1638
	char buf[512];
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1639
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1640
	FOR_ALL_VEHICLES(v) {
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1641
		switch (v->type) {
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1642
			case VEH_TRAIN:
7108
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1643
				if (!IsTrainEngine(v)) continue;
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1644
				break;
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1645
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1646
			case VEH_ROAD:
7358
29ceb533895a (svn r10721) -Fix [FS#1084]: Skip articulated parts of road vehicles during unique name check.
peter1138
parents: 7351
diff changeset
  1647
				if (!IsRoadVehFront(v)) continue;
7108
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1648
				break;
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1649
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1650
			case VEH_AIRCRAFT:
7108
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1651
				if (!IsNormalAircraft(v)) continue;
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1652
				break;
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1653
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1654
			case VEH_SHIP:
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1655
				break;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1656
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1657
			default:
7108
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1658
				continue;
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1659
		}
7108
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1660
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1661
		SetDParam(0, v->index);
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1662
		GetString(buf, STR_VEHICLE_NAME, lastof(buf));
fef4440c4063 (svn r10380) -Fix (r10364): when checking for unique names, only check vehicles that can have names, and skip inactive players.
peter1138
parents: 7097
diff changeset
  1663
		if (strcmp(buf, name) == 0) return false;
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1664
	}
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1665
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1666
	return true;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1667
}
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1668
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1765
diff changeset
  1669
/** Give a custom name to your vehicle
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3490
diff changeset
  1670
 * @param tile unused
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1671
 * @param flags type of operation
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1765
diff changeset
  1672
 * @param p1 vehicle ID to name
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1765
diff changeset
  1673
 * @param p2 unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1765
diff changeset
  1674
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1675
CommandCost CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1676
{
9914
c07d0352d8d5 (svn r14064) -Fix [FS#1752]: check for the length of strings (in bytes) in the command. Checking for the length in pixels is impossible because that differs per client.
rubidium
parents: 9891
diff changeset
  1677
	if (!IsValidVehicleID(p1)) return CMD_ERROR;
c07d0352d8d5 (svn r14064) -Fix [FS#1752]: check for the length of strings (in bytes) in the command. Checking for the length in pixels is impossible because that differs per client.
rubidium
parents: 9891
diff changeset
  1678
	if (StrEmpty(_cmd_text) || strlen(_cmd_text) >= MAX_LENGTH_VEHICLE_NAME_BYTES) 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: 1217
diff changeset
  1679
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1680
	Vehicle *v = GetVehicle(p1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1681
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1765
diff changeset
  1682
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1683
9928
fa24e759e11d (svn r14083) -Fix [FS#1264, FS#2037, FS#2038, FS#2110]: Rewrite the autoreplace kernel.
frosch
parents: 9921
diff changeset
  1684
	if ((flags & DC_AUTOREPLACE) == 0 && !IsUniqueVehicleName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 7096
diff changeset
  1685
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1686
	if (flags & DC_EXEC) {
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  1687
		free(v->name);
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  1688
		v->name = strdup(_cmd_text);
9297
1cb8d7bbdc8a (svn r13165) -Codechange: replace some RebuildXXXList/ResortXXXList+InvalidateWindowXXX with InvalidateWindowXXXData.
rubidium
parents: 9280
diff changeset
  1689
		InvalidateWindowClassesData(WC_TRAINS_LIST, 1);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1690
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1691
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1692
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1693
	return CommandCost();
0
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1696
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1697
/** Change the service interval of a vehicle
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3490
diff changeset
  1698
 * @param tile unused
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1699
 * @param flags type of operation
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1700
 * @param p1 vehicle ID that is being service-interval-changed
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1701
 * @param p2 new service interval
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1702
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6905
diff changeset
  1703
CommandCost CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1704
{
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1705
	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1706
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4346
diff changeset
  1707
	if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1708
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1709
	Vehicle *v = GetVehicle(p1);
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1710
4352
6703cd8d39e0 (svn r6053) -Codechange: renamed all IsXXXIndex to IsValidXXXID
truelight
parents: 4346
diff changeset
  1711
	if (!CheckOwnership(v->owner)) return CMD_ERROR;
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1712
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1713
	if (flags & DC_EXEC) {
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1714
		v->service_interval = serv_int;
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1715
		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1716
	}
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1717
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6946
diff changeset
  1718
	return CommandCost();
2819
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1719
}
f25fb6ee397f (svn r3367) Unify the 4 distinct CMD_CHANGE_{AIRCRAFT,ROADVEH,SHIP,TRAIN}_SERVICE_INT commands into one CMD_CHANGE_SERVICE_INT command.
tron
parents: 2817
diff changeset
  1720
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1721
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1722
static Rect _old_vehicle_coords; ///< coords of vehicle before it has moved
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1723
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1724
/**
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1725
 * Stores the vehicle image coords for later call to EndVehicleMove()
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1726
 * @param v vehicle which image's coords to store
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1727
 * @see _old_vehicle_coords
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1728
 * @see EndVehicleMove()
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1729
 */
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1730
void BeginVehicleMove(const Vehicle *v)
7321
f91bdca345e8 (svn r10684) -Codechange: some more coding style related changes. Primarily moving { to a new line.
rubidium
parents: 7273
diff changeset
  1731
{
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1732
	_old_vehicle_coords.left   = v->left_coord;
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1733
	_old_vehicle_coords.top    = v->top_coord;
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1734
	_old_vehicle_coords.right  = v->right_coord;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1735
	_old_vehicle_coords.bottom = v->bottom_coord;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1736
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1737
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1738
/**
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1739
 * Marks screen dirty after a vehicle has moved
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1740
 * @param v vehicle which is marked dirty
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1741
 * @see _old_vehicle_coords
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1742
 * @see BeginVehicleMove()
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1743
 */
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1744
void EndVehicleMove(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1745
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1746
	MarkAllViewportsDirty(
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1747
		min(_old_vehicle_coords.left,   v->left_coord),
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1748
		min(_old_vehicle_coords.top,    v->top_coord),
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1749
		max(_old_vehicle_coords.right,  v->right_coord) + 1,
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1750
		max(_old_vehicle_coords.bottom, v->bottom_coord) + 1
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1751
	);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1752
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1753
8317
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1754
/**
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1755
 * Marks viewports dirty where the vehicle's image is
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1756
 * In fact, it equals
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1757
 *   BeginVehicleMove(v); EndVehicleMove(v);
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1758
 * @param v vehicle to mark dirty
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1759
 * @see BeginVehicleMove()
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1760
 * @see EndVehicleMove()
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1761
 */
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1762
void MarkSingleVehicleDirty(const Vehicle *v)
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1763
{
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1764
	MarkAllViewportsDirty(v->left_coord, v->top_coord, v->right_coord + 1, v->bottom_coord + 1);
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1765
}
539038de35cb (svn r11882) -Codechange: introduce MarkSingleVehicleDirty() and simplify the code at some places
smatz
parents: 8309
diff changeset
  1766
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1767
/* returns true if staying in the same tile */
6153
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1768
GetNewVehiclePosResult GetNewVehiclePos(const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1769
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1770
	static const int8 _delta_coord[16] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1771
		-1,-1,-1, 0, 1, 1, 1, 0, /* x */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1772
		-1, 0, 1, 1, 1, 0,-1,-1, /* y */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1773
	};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1774
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1775
	int x = v->x_pos + _delta_coord[v->direction];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1776
	int y = v->y_pos + _delta_coord[v->direction + 8];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1777
6153
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1778
	GetNewVehiclePosResult gp;
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1779
	gp.x = x;
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1780
	gp.y = y;
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1781
	gp.old_tile = v->tile;
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1782
	gp.new_tile = TileVirtXY(x, y);
eb35b73b300a (svn r8897) -Fix
tron
parents: 6152
diff changeset
  1783
	return gp;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1784
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1785
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3139
diff changeset
  1786
static const Direction _new_direction_table[] = {
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3139
diff changeset
  1787
	DIR_N , DIR_NW, DIR_W ,
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3139
diff changeset
  1788
	DIR_NE, DIR_SE, DIR_SW,
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3139
diff changeset
  1789
	DIR_E , DIR_SE, DIR_S
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1790
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1791
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1792
Direction GetDirectionTowards(const Vehicle *v, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1793
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1794
	int i = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1795
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1796
	if (y >= v->y_pos) {
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1797
		if (y != v->y_pos) i += 3;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1798
		i += 3;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1799
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1800
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1801
	if (x >= v->x_pos) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1802
		if (x != v->x_pos) i++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1803
		i++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1804
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1805
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1806
	Direction dir = v->direction;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1807
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1808
	DirDiff dirdiff = DirDifference(_new_direction_table[i], dir);
3158
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  1809
	if (dirdiff == DIRDIFF_SAME) return dir;
696a6ca0bfa9 (svn r3784) Add a type and functions to handle direction changes
tron
parents: 3157
diff changeset
  1810
	return ChangeDir(dir, dirdiff > DIRDIFF_REVERSE ? DIRDIFF_45LEFT : DIRDIFF_45RIGHT);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1811
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1812
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1813
Trackdir GetVehicleTrackdir(const Vehicle *v)
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1735
diff changeset
  1814
{
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1815
	if (v->vehstatus & VS_CRASHED) return INVALID_TRACKDIR;
1758
2158a0938e45 (svn r2262) - Fix: Assertion when vehicle in a depot wants to do pathfinding.
matthijs
parents: 1757
diff changeset
  1816
2952
58522ed8f0f1 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2951
diff changeset
  1817
	switch (v->type) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1818
		case VEH_TRAIN:
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1819
			if (v->u.rail.track == TRACK_BIT_DEPOT) // We'll assume the train is facing outwards
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1820
				return DiagDirToDiagTrackdir(GetRailDepotDirection(v->tile)); // Train in depot
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1821
9624
957b141496ee (svn r13680) -Fix: NPF crashing when a ship tried to find a nearby depot when on an aqueduct.
rubidium
parents: 9622
diff changeset
  1822
			if (v->u.rail.track == TRACK_BIT_WORMHOLE) // train in tunnel or on bridge, so just use his direction and assume a diagonal track
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1823
				return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
1765
155dfdbbbc8d (svn r2269) - CodeChange: no else after return; exchange some magic 0xffff with INVALID_VEHICLE, vehicle index is of type VehicleID
Darkvater
parents: 1758
diff changeset
  1824
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1825
			return TrackDirectionToTrackdir(FindFirstTrack(v->u.rail.track), v->direction);
1959
c2c3a9850c2e (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
  1826
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1827
		case VEH_SHIP:
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7486
diff changeset
  1828
			if (v->IsInDepot())
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1829
				// We'll assume the ship is facing outwards
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1830
				return DiagDirToDiagTrackdir(GetShipDepotDirection(v->tile));
1765
155dfdbbbc8d (svn r2269) - CodeChange: no else after return; exchange some magic 0xffff with INVALID_VEHICLE, vehicle index is of type VehicleID
Darkvater
parents: 1758
diff changeset
  1831
9624
957b141496ee (svn r13680) -Fix: NPF crashing when a ship tried to find a nearby depot when on an aqueduct.
rubidium
parents: 9622
diff changeset
  1832
			if (v->u.ship.state == TRACK_BIT_WORMHOLE) // ship on aqueduct, so just use his direction and assume a diagonal track
957b141496ee (svn r13680) -Fix: NPF crashing when a ship tried to find a nearby depot when on an aqueduct.
rubidium
parents: 9622
diff changeset
  1833
				return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
957b141496ee (svn r13680) -Fix: NPF crashing when a ship tried to find a nearby depot when on an aqueduct.
rubidium
parents: 9622
diff changeset
  1834
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1835
			return TrackDirectionToTrackdir(FindFirstTrack(v->u.ship.state), v->direction);
1959
c2c3a9850c2e (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
  1836
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1837
		case VEH_ROAD:
7490
bb27d92565d3 (svn r11001) -Codechange: unify the way to determine whether a vehicle is in a depot.
rubidium
parents: 7486
diff changeset
  1838
			if (v->IsInDepot()) // We'll assume the road vehicle is facing outwards
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1839
				return DiagDirToDiagTrackdir(GetRoadDepotDirection(v->tile));
1765
155dfdbbbc8d (svn r2269) - CodeChange: no else after return; exchange some magic 0xffff with INVALID_VEHICLE, vehicle index is of type VehicleID
Darkvater
parents: 1758
diff changeset
  1840
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1841
			if (IsStandardRoadStopTile(v->tile)) // We'll assume the road vehicle is facing outwards
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1842
				return DiagDirToDiagTrackdir(GetRoadStopDir(v->tile)); // Road vehicle in a station
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1843
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1844
			if (IsDriveThroughStopTile(v->tile)) return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
6012
065d7234a7a9 (svn r8735) -Feature: drive-through road stops made possible by the hard work of mart3p.
rubidium
parents: 6009
diff changeset
  1845
4270
923ac6b88dad (svn r5898) -Fix [FS#249]: Bugfix: Goto Depot not always working for road vehicles. (mart3p)
Darkvater
parents: 4263
diff changeset
  1846
			/* If vehicle's state is a valid track direction (vehicle is not turning around) return it */
6009
bbe756070082 (svn r8730) -Codechange: more replacements of magic numbers by enums and removal of some (by now) redundant comments.
rubidium
parents: 6000
diff changeset
  1847
			if (!IsReversingRoadTrackdir((Trackdir)v->u.road.state)) return (Trackdir)v->u.road.state;
4270
923ac6b88dad (svn r5898) -Fix [FS#249]: Bugfix: Goto Depot not always working for road vehicles. (mart3p)
Darkvater
parents: 4263
diff changeset
  1848
923ac6b88dad (svn r5898) -Fix [FS#249]: Bugfix: Goto Depot not always working for road vehicles. (mart3p)
Darkvater
parents: 4263
diff changeset
  1849
			/* Vehicle is turning around, get the direction from vehicle's direction */
9224
93528d9cb96d (svn r13090) -Codechange: add functions for direct conversion from DiagDirection to Track and Trackbits
smatz
parents: 9221
diff changeset
  1850
			return DiagDirToDiagTrackdir(DirToDiagDir(v->direction));
1959
c2c3a9850c2e (svn r2465) Remove some unreachable code
tron
parents: 1944
diff changeset
  1851
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9005
diff changeset
  1852
		/* case VEH_AIRCRAFT: case VEH_EFFECT: case VEH_DISASTER: */
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  1853
		default: return INVALID_TRACKDIR;
1752
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1735
diff changeset
  1854
	}
d65cd19f7117 (svn r2256) - Fix: Trains cannot find a depot when they are in a tunnel. (glx)
matthijs
parents: 1735
diff changeset
  1855
}
5991
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5955
diff changeset
  1856
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5955
diff changeset
  1857
/**
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5955
diff changeset
  1858
 * Returns some meta-data over the to be entered tile.
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5955
diff changeset
  1859
 * @see VehicleEnterTileStatus to see what the bits in the return value mean.
ec2eebfe86de (svn r8698) -Codechange: enumify the returns of VehicleEnterTile
rubidium
parents: 5955
diff changeset
  1860
 */
1977
37bbebf94434 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1962
diff changeset
  1861
uint32 VehicleEnterTile(Vehicle *v, TileIndex tile, int x, int y)
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  1862
{
3657
eb28d97d3d18 (svn r4572) Remove vehicle_leave_tile_proc
tron
parents: 3645
diff changeset
  1863
	return _tile_type_procs[GetTileType(tile)]->vehicle_enter_tile_proc(v, tile, x, y);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1864
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1865
6641
1b14f8887cb7 (svn r9872) -Codechange: more type strictness for vehicle types
rubidium
parents: 6638
diff changeset
  1866
UnitID GetFreeUnitNumber(VehicleType type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1867
{
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1868
	UnitID max = 0;
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1869
	const Vehicle *u;
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1870
	static bool *cache = NULL;
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1871
	static UnitID gmax = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1872
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1873
	switch (type) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1874
		case VEH_TRAIN:    max = _settings_game.vehicle.max_trains; break;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1875
		case VEH_ROAD:     max = _settings_game.vehicle.max_roadveh; break;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1876
		case VEH_SHIP:     max = _settings_game.vehicle.max_ships; break;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1877
		case VEH_AIRCRAFT: max = _settings_game.vehicle.max_aircraft; break;
2996
9d77b3aec341 (svn r3573) - Replace assert(0) with NOT_REACHED(). This commit sponsored by "giving Darkvater credit for the last three".
peter1138
parents: 2995
diff changeset
  1878
		default: NOT_REACHED();
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1879
	}
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1880
4096
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1881
	if (max == 0) {
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1882
		/* we can't build any of this kind of vehicle, so we just return 1 instead of looking for a free number
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1883
		 * a max of 0 will cause the following code to write to a NULL pointer
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1884
		 * We know that 1 is bigger than the max allowed vehicle number, so it's the same as returning something, that is too big
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1885
		 */
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1886
		return 1;
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1887
	}
3ee959ce63c9 (svn r5428) -Fix: [vehicles] sovled crash when trying to build a vehicle type, that is set to max 0 (spotted by roboman)
bjarni
parents: 4077
diff changeset
  1888
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1889
	if (max > gmax) {
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1890
		gmax = max;
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1891
		free(cache);
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
  1892
		cache = MallocT<bool>(max + 1);
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1893
	}
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1894
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1895
	/* Clear the cache */
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1896
	memset(cache, 0, (max + 1) * sizeof(*cache));
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1897
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1898
	/* Fill the cache */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1899
	FOR_ALL_VEHICLES(u) {
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1900
		if (u->type == type && u->owner == _current_player && u->unitnumber != 0 && u->unitnumber <= max)
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1901
			cache[u->unitnumber] = true;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1902
	}
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1903
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  1904
	/* Find the first unused unit number */
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1905
	UnitID unit = 1;
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  1906
	for (; unit <= max; unit++) {
2995
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1907
		if (!cache[unit]) break;
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1908
	}
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1909
fcc4c71eccda (svn r3572) - Rewrite GetFreeUnitNumber() so that only one loop of vehicles is required. Instead a list of used/unused numbers is created and the first unused number is chosen. This significantly improves performance in large games.
peter1138
parents: 2994
diff changeset
  1910
	return unit;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1911
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1912
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  1913
7086
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1914
/**
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1915
 * Check whether we can build infrastructure for the given
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1916
 * vehicle type. This to disable building stations etc. when
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1917
 * you are not allowed/able to have the vehicle type yet.
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1918
 * @param type the vehicle type to check this for
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1919
 * @return true if there is any reason why you may build
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1920
 *         the infrastructure for the given vehicle type
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1921
 */
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1922
bool CanBuildVehicleInfrastructure(VehicleType type)
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1923
{
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1924
	assert(IsPlayerBuildableVehicleType(type));
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1925
9652
0405e98d8e96 (svn r13719) -Codechange: rename IsValidPlayer to IsValidPlayerID in line with all other structs/classes that are in a pool.
rubidium
parents: 9625
diff changeset
  1926
	if (!IsValidPlayerID(_current_player)) return false;
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1927
	if (_settings_client.gui.always_build_infrastructure) return true;
7086
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1928
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1929
	UnitID max;
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1930
	switch (type) {
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1931
		case VEH_TRAIN:    max = _settings_game.vehicle.max_trains; break;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1932
		case VEH_ROAD:     max = _settings_game.vehicle.max_roadveh; break;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1933
		case VEH_SHIP:     max = _settings_game.vehicle.max_ships; break;
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1934
		case VEH_AIRCRAFT: max = _settings_game.vehicle.max_aircraft; break;
7086
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1935
		default: NOT_REACHED();
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1936
	}
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1937
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1938
	/* We can build vehicle infrastructure when we may build the vehicle type */
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1939
	if (max > 0) {
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1940
		/* Can we actually build the vehicle type? */
9070
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9065
diff changeset
  1941
		const Engine *e;
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9065
diff changeset
  1942
		FOR_ALL_ENGINES_OF_TYPE(e, type) {
dd0121143eba (svn r12924) -Feature: Introducing the so called 'engine pool' which primarily removes the fixed engine type limits and also happens to allow (with the patch option 'dynamic_engines') multiple NewGRF vehicle sets to coexist.
peter1138
parents: 9065
diff changeset
  1943
			if (HasBit(e->player_avail, _local_player)) return true;
7086
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1944
		}
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1945
		return false;
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1946
	}
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1947
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1948
	/* We should be able to build infrastructure when we have the actual vehicle type */
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1949
	const Vehicle *v;
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1950
	FOR_ALL_VEHICLES(v) {
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1951
		if (v->owner == _local_player && v->type == type) return true;
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1952
	}
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1953
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1954
	return false;
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1955
}
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1956
de7e83fb1602 (svn r10353) -Fix/Feature [FS#669]: disallow (in the GUI) the building of infrastructure you do not have available vehicles for. This means that the airport building button is disabled till you can actually build aircraft. The game itself will not disallow you to build the infrastructure and this "new" behaviour can be overriden with a patch setting.
rubidium
parents: 7076
diff changeset
  1957
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  1958
const Livery *GetEngineLivery(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v)
3040
7043c0de7c6d (svn r3620) - 2cc: Replace use of macro to determine colour map with a function call for drawing of vehicles.
peter1138
parents: 3039
diff changeset
  1959
{
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1960
	const Player *p = GetPlayer(player);
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1961
	LiveryScheme scheme = LS_DEFAULT;
5717
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  1962
	CargoID cargo_type = v == NULL ? (CargoID)CT_INVALID : v->cargo_type;
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  1963
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1964
	/* The default livery is always available for use, but its in_use flag determines
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1965
	 * whether any _other_ liveries are in use. */
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  1966
	if (p->livery[LS_DEFAULT].in_use && (_settings_client.gui.liveries == 2 || (_settings_client.gui.liveries == 1 && player == _local_player))) {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1967
		/* Determine the livery scheme to use */
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1968
		switch (GetEngine(engine_type)->type) {
6638
09c5d7bf69a5 (svn r9869) -Codechange: replace some bytes with VehicleType, i.e. more type strictness.
rubidium
parents: 6626
diff changeset
  1969
			default: NOT_REACHED();
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  1970
			case VEH_TRAIN: {
5763
a6029daa547f (svn r8314) -Fix
tron
parents: 5748
diff changeset
  1971
				const RailVehicleInfo *rvi = RailVehInfo(engine_type);
a6029daa547f (svn r8314) -Fix
tron
parents: 5748
diff changeset
  1972
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1973
				if (cargo_type == CT_INVALID) cargo_type = rvi->cargo_type;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1974
				if (rvi->railveh_type == RAILVEH_WAGON) {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1975
					if (!GetCargo(cargo_type)->is_freight) {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1976
						if (parent_engine_type == INVALID_ENGINE) {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1977
							scheme = LS_PASSENGER_WAGON_STEAM;
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1978
						} else {
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1979
							switch (RailVehInfo(parent_engine_type)->engclass) {
6585
46035c6baa8d (svn r9799) -Codechange: separate engine class and engine running cost class (mart3p).
rubidium
parents: 6581
diff changeset
  1980
								default: NOT_REACHED();
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1981
								case EC_STEAM:    scheme = LS_PASSENGER_WAGON_STEAM;    break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1982
								case EC_DIESEL:   scheme = LS_PASSENGER_WAGON_DIESEL;   break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1983
								case EC_ELECTRIC: scheme = LS_PASSENGER_WAGON_ELECTRIC; break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1984
								case EC_MONORAIL: scheme = LS_PASSENGER_WAGON_MONORAIL; break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1985
								case EC_MAGLEV:   scheme = LS_PASSENGER_WAGON_MAGLEV;   break;
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1986
							}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1987
						}
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1988
					} else {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1989
						scheme = LS_FREIGHT_WAGON;
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  1990
					}
8309
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1991
				} else {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1992
					bool is_mu = HasBit(EngInfo(engine_type)->misc_flags, EF_RAIL_IS_MU);
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1993
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1994
					switch (rvi->engclass) {
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1995
						default: NOT_REACHED();
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1996
						case EC_STEAM:    scheme = LS_STEAM; break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1997
						case EC_DIESEL:   scheme = is_mu ? LS_DMU : LS_DIESEL;   break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1998
						case EC_ELECTRIC: scheme = is_mu ? LS_EMU : LS_ELECTRIC; break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  1999
						case EC_MONORAIL: scheme = LS_MONORAIL; break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  2000
						case EC_MAGLEV:   scheme = LS_MAGLEV; break;
5d3e63cd70e0 (svn r11874) -Fix [FS#1655]: all wagons of maglev/monorail trains would get the livery colour of the engine instead of their wagon type.
rubidium
parents: 8302
diff changeset
  2001
					}
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2002
				}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2003
				break;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2004
			}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2005
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2006
			case VEH_ROAD: {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2007
				const RoadVehicleInfo *rvi = RoadVehInfo(engine_type);
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2008
				if (cargo_type == CT_INVALID) cargo_type = rvi->cargo_type;
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2009
				if (HasBit(EngInfo(engine_type)->misc_flags, EF_ROAD_TRAM)) {
6724
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6721
diff changeset
  2010
					/* Tram */
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6721
diff changeset
  2011
					scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_TRAM : LS_FREIGHT_TRAM;
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6721
diff changeset
  2012
				} else {
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6721
diff changeset
  2013
					/* Bus or truck */
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6721
diff changeset
  2014
					scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_BUS : LS_TRUCK;
4f404dfe9752 (svn r9956) -Codechange: Add tram livery schemes
peter1138
parents: 6721
diff changeset
  2015
				}
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2016
				break;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2017
			}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2018
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2019
			case VEH_SHIP: {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2020
				const ShipVehicleInfo *svi = ShipVehInfo(engine_type);
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2021
				if (cargo_type == CT_INVALID) cargo_type = svi->cargo_type;
6330
989ca49d0da3 (svn r9302) -Codechange: Use cargo class to test for passengers when deciding on the livery scheme to use.
peter1138
parents: 6264
diff changeset
  2022
				scheme = IsCargoInClass(cargo_type, CC_PASSENGERS) ? LS_PASSENGER_SHIP : LS_FREIGHT_SHIP;
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2023
				break;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2024
			}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2025
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2026
			case VEH_AIRCRAFT: {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2027
				const AircraftVehicleInfo *avi = AircraftVehInfo(engine_type);
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2028
				if (cargo_type == CT_INVALID) cargo_type = CT_PASSENGERS;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2029
				switch (avi->subtype) {
5855
6f273e99a57a (svn r8429) -Codechange: Add a name for AircraftVehicleInfo subtype helicopter and remove some magic numbers related to the subtype.
Darkvater
parents: 5854
diff changeset
  2030
					case AIR_HELI: scheme = LS_HELICOPTER; break;
6f273e99a57a (svn r8429) -Codechange: Add a name for AircraftVehicleInfo subtype helicopter and remove some magic numbers related to the subtype.
Darkvater
parents: 5854
diff changeset
  2031
					case AIR_CTOL: scheme = LS_SMALL_PLANE; break;
6f273e99a57a (svn r8429) -Codechange: Add a name for AircraftVehicleInfo subtype helicopter and remove some magic numbers related to the subtype.
Darkvater
parents: 5854
diff changeset
  2032
					case AIR_CTOL | AIR_FAST: scheme = LS_LARGE_PLANE; break;
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2033
				}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2034
				break;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2035
			}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2036
		}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2037
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2038
		/* Switch back to the default scheme if the resolved scheme is not in use */
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2039
		if (!p->livery[scheme].in_use) scheme = LS_DEFAULT;
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2040
	}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2041
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2042
	return &p->livery[scheme];
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2043
}
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2044
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2045
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2046
static SpriteID GetEngineColourMap(EngineID engine_type, PlayerID player, EngineID parent_engine_type, const Vehicle *v)
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2047
{
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2048
	SpriteID map = (v != NULL) ? v->colormap : PAL_NONE;
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2049
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2050
	/* Return cached value if any */
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2051
	if (map != PAL_NONE) return map;
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2052
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2053
	/* Check if we should use the colour map callback */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2054
	if (HasBit(EngInfo(engine_type)->callbackmask, CBM_VEHICLE_COLOUR_REMAP)) {
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2055
		uint16 callback = GetVehicleCallback(CBID_VEHICLE_COLOUR_MAPPING, 0, 0, engine_type, v);
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2056
		/* A return value of 0xC000 is stated to "use the default two-color
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2057
		 * maps" which happens to be the failure action too... */
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2058
		if (callback != CALLBACK_FAILED && callback != 0xC000) {
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2059
			map = GB(callback, 0, 14);
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2060
			/* If bit 14 is set, then the company colours are applied to the
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2061
			 * map else it's returned as-is. */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2062
			if (!HasBit(callback, 14)) {
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2063
				/* Update cache */
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2064
				if (v != NULL) ((Vehicle*)v)->colormap = map;
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2065
				return map;
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2066
			}
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2067
		}
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2068
	}
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2069
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2070
	bool twocc = HasBit(EngInfo(engine_type)->misc_flags, EF_USES_2CC);
5717
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  2071
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  2072
	if (map == PAL_NONE) map = twocc ? (SpriteID)SPR_2CCMAP_BASE : (SpriteID)PALETTE_RECOLOR_START;
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  2073
6516
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2074
	const Livery *livery = GetEngineLivery(engine_type, player, parent_engine_type, v);
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2075
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2076
	map += livery->colour1;
0d14650b196b (svn r9702) -Codechange: Add livery support to newgrf vehicle var 43
peter1138
parents: 6509
diff changeset
  2077
	if (twocc) map += livery->colour2 * 16;
3113
d79500c648da (svn r3717) - [2cc] Add 2cc colour maps and use for newgrf engines requiring them. Currently the second colour is fixed to be the player's colour.
peter1138
parents: 3105
diff changeset
  2078
7802
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2079
	/* Update cache */
246a94599679 (svn r11352) -Codechange: cache callback 2D result (vehicle color mapping) instead calling it every time the vehicle is drawn
glx
parents: 7765
diff changeset
  2080
	if (v != NULL) ((Vehicle*)v)->colormap = map;
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
  2081
	return map;
3040
7043c0de7c6d (svn r3620) - 2cc: Replace use of macro to determine colour map with a function call for drawing of vehicles.
peter1138
parents: 3039
diff changeset
  2082
}
7043c0de7c6d (svn r3620) - 2cc: Replace use of macro to determine colour map with a function call for drawing of vehicles.
peter1138
parents: 3039
diff changeset
  2083
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
  2084
SpriteID GetEnginePalette(EngineID engine_type, PlayerID player)
3105
02f849f946d0 (svn r3701) [2cc] move vehicle/engine palette functions out of header file and document the remaining definitions
peter1138
parents: 3047
diff changeset
  2085
{
5717
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  2086
	return GetEngineColourMap(engine_type, player, INVALID_ENGINE, NULL);
3105
02f849f946d0 (svn r3701) [2cc] move vehicle/engine palette functions out of header file and document the remaining definitions
peter1138
parents: 3047
diff changeset
  2087
}
02f849f946d0 (svn r3701) [2cc] move vehicle/engine palette functions out of header file and document the remaining definitions
peter1138
parents: 3047
diff changeset
  2088
5668
36b39f4a9032 (svn r8128) -Codechange: Split sprite and palette remap into separate 32 bit values.
peter1138
parents: 5651
diff changeset
  2089
SpriteID GetVehiclePalette(const Vehicle *v)
3105
02f849f946d0 (svn r3701) [2cc] move vehicle/engine palette functions out of header file and document the remaining definitions
peter1138
parents: 3047
diff changeset
  2090
{
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2091
	if (v->type == VEH_TRAIN) {
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2092
		return GetEngineColourMap(
6809
0f306c3e1030 (svn r10048) -Fix (r6455): Don't use override engine type for articulated wagon parts.
peter1138
parents: 6782
diff changeset
  2093
			(v->u.rail.first_engine != INVALID_ENGINE && (UsesWagonOverride(v) || (IsArticulatedPart(v) && RailVehInfo(v->engine_type)->railveh_type != RAILVEH_WAGON))) ?
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2094
				v->u.rail.first_engine : v->engine_type,
5717
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  2095
			v->owner, v->u.rail.first_engine, v);
4603
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2096
	}
20c816f664de (svn r6455) - Feature: Add 2cc (two company colours) livery schemes. This replaces the original colour selection window and bumps the saveload version. Liveries are supported for all vehicles, not just those with 2cc support. Thanks to lakie for GUI inspiration.
peter1138
parents: 4574
diff changeset
  2097
5717
b6fb89cd9cce (svn r8211) -Codechange: (NewGRF) Implement callback 2D, explicit vehicle colour map
peter1138
parents: 5693
diff changeset
  2098
	return GetEngineColourMap(v->engine_type, v->owner, INVALID_ENGINE, v);
3105
02f849f946d0 (svn r3701) [2cc] move vehicle/engine palette functions out of header file and document the remaining definitions
peter1138
parents: 3047
diff changeset
  2099
}
02f849f946d0 (svn r3701) [2cc] move vehicle/engine palette functions out of header file and document the remaining definitions
peter1138
parents: 3047
diff changeset
  2100
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2101
static uint8  _cargo_days;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2102
static uint16 _cargo_source;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2103
static uint32 _cargo_source_xy;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2104
static uint16 _cargo_count;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2105
static uint16 _cargo_paid_for;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2106
static Money  _cargo_feeder_share;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2107
static uint32 _cargo_loaded_at_xy;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2108
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2109
/**
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2110
 * Make it possible to make the saveload tables "friends" of other classes.
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2111
 * @param vt the vehicle type. Can be VEH_END for the common vehicle description data
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2112
 * @return the saveload description
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2113
 */
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2114
const SaveLoad *GetVehicleDescription(VehicleType vt)
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2115
{
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2116
/** Save and load of vehicles */
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2117
static const SaveLoad _common_veh_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2118
	    SLE_VAR(Vehicle, subtype,              SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2119
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2120
	    SLE_REF(Vehicle, next,                 REF_VEHICLE_OLD),
8258
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  2121
	SLE_CONDVAR(Vehicle, name,                 SLE_NAME,                    0, 83),
9fa31acb07bc (svn r11822) -Codechange: Replaced fixed size custom name array. Names are now attached to their object directly and there is
peter1138
parents: 8254
diff changeset
  2122
	SLE_CONDSTR(Vehicle, name,                 SLE_STR, 0,                 84, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2123
	SLE_CONDVAR(Vehicle, unitnumber,           SLE_FILE_U8  | SLE_VAR_U16,  0, 7),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2124
	SLE_CONDVAR(Vehicle, unitnumber,           SLE_UINT16,                  8, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2125
	    SLE_VAR(Vehicle, owner,                SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2126
	SLE_CONDVAR(Vehicle, tile,                 SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2127
	SLE_CONDVAR(Vehicle, tile,                 SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2128
	SLE_CONDVAR(Vehicle, dest_tile,            SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2129
	SLE_CONDVAR(Vehicle, dest_tile,            SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2130
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2131
	SLE_CONDVAR(Vehicle, x_pos,                SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2132
	SLE_CONDVAR(Vehicle, x_pos,                SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2133
	SLE_CONDVAR(Vehicle, y_pos,                SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2134
	SLE_CONDVAR(Vehicle, y_pos,                SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2135
	    SLE_VAR(Vehicle, z_pos,                SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2136
	    SLE_VAR(Vehicle, direction,            SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2137
6561
073e48516812 (svn r9763) -Codechange/Fix (r9760): don't read the variables that are calculated automatically either.
rubidium
parents: 6558
diff changeset
  2138
	SLE_CONDNULL(2,                                                         0, 57),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2139
	    SLE_VAR(Vehicle, spritenum,            SLE_UINT8),
6561
073e48516812 (svn r9763) -Codechange/Fix (r9760): don't read the variables that are calculated automatically either.
rubidium
parents: 6558
diff changeset
  2140
	SLE_CONDNULL(5,                                                         0, 57),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2141
	    SLE_VAR(Vehicle, engine_type,          SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2142
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2143
	    SLE_VAR(Vehicle, max_speed,            SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2144
	    SLE_VAR(Vehicle, cur_speed,            SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2145
	    SLE_VAR(Vehicle, subspeed,             SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2146
	    SLE_VAR(Vehicle, acceleration,         SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2147
	    SLE_VAR(Vehicle, progress,             SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2148
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2149
	    SLE_VAR(Vehicle, vehstatus,            SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2150
	SLE_CONDVAR(Vehicle, last_station_visited, SLE_FILE_U8  | SLE_VAR_U16,  0, 4),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2151
	SLE_CONDVAR(Vehicle, last_station_visited, SLE_UINT16,                  5, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2152
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2153
	     SLE_VAR(Vehicle, cargo_type,           SLE_UINT8),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2154
	 SLE_CONDVAR(Vehicle, cargo_subtype,        SLE_UINT8,                  35, SL_MAX_VERSION),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2155
	SLEG_CONDVAR(         _cargo_days,          SLE_UINT8,                   0, 67),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2156
	SLEG_CONDVAR(         _cargo_source,        SLE_FILE_U8  | SLE_VAR_U16,  0, 6),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2157
	SLEG_CONDVAR(         _cargo_source,        SLE_UINT16,                  7, 67),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2158
	SLEG_CONDVAR(         _cargo_source_xy,     SLE_UINT32,                 44, 67),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2159
	     SLE_VAR(Vehicle, cargo_cap,            SLE_UINT16),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2160
	SLEG_CONDVAR(         _cargo_count,         SLE_UINT16,                  0, 67),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2161
	 SLE_CONDLST(Vehicle, cargo,                REF_CARGO_PACKET,           68, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2162
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2163
	    SLE_VAR(Vehicle, day_counter,          SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2164
	    SLE_VAR(Vehicle, tick_counter,         SLE_UINT8),
8556
ce8d86cc14d2 (svn r12134) -Change: count the number of ticks a vehicle was running this day to calculate running cost
smatz
parents: 8522
diff changeset
  2165
	SLE_CONDVAR(Vehicle, running_ticks,        SLE_UINT8,                   88, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2166
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2167
	    SLE_VAR(Vehicle, cur_order_index,      SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2168
	    SLE_VAR(Vehicle, num_orders,           SLE_UINT8),
956
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2169
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2170
	/* This next line is for version 4 and prior compatibility.. it temporarily reads
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2171
	    type and flags (which were both 4 bits) into type. Later on this is
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2172
	    converted correctly */
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2173
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8,                 0, 4),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2174
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
956
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2175
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2176
	/* Orders for version 5 and on */
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2177
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type),  SLE_UINT8,  5, SL_MAX_VERSION),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2178
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, flags), SLE_UINT8,  5, SL_MAX_VERSION),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2179
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest),  SLE_UINT16, 5, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2180
4712
4335ad42e163 (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4695
diff changeset
  2181
	/* Refit in current order */
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2182
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_cargo),    SLE_UINT8, 36, SL_MAX_VERSION),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2183
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_subtype),  SLE_UINT8, 36, SL_MAX_VERSION),
4712
4335ad42e163 (svn r6624) -Feature: added ability to add refit commands to vehicle orders (can only be done in goto depot orders)
bjarni
parents: 4695
diff changeset
  2184
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2185
	/* Timetable in current order */
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2186
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, wait_time),      SLE_UINT16, 67, SL_MAX_VERSION),
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2187
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, travel_time),    SLE_UINT16, 67, SL_MAX_VERSION),
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2188
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2189
	    SLE_REF(Vehicle, orders,               REF_ORDER),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2190
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2191
	SLE_CONDVAR(Vehicle, age,                  SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2192
	SLE_CONDVAR(Vehicle, age,                  SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2193
	SLE_CONDVAR(Vehicle, max_age,              SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2194
	SLE_CONDVAR(Vehicle, max_age,              SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2195
	SLE_CONDVAR(Vehicle, date_of_last_service, SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2196
	SLE_CONDVAR(Vehicle, date_of_last_service, SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2197
	SLE_CONDVAR(Vehicle, service_interval,     SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2198
	SLE_CONDVAR(Vehicle, service_interval,     SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2199
	    SLE_VAR(Vehicle, reliability,          SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2200
	    SLE_VAR(Vehicle, reliability_spd_dec,  SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2201
	    SLE_VAR(Vehicle, breakdown_ctr,        SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2202
	    SLE_VAR(Vehicle, breakdown_delay,      SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2203
	    SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2204
	    SLE_VAR(Vehicle, breakdown_chance,     SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2205
	SLE_CONDVAR(Vehicle, build_year,           SLE_FILE_U8 | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2206
	SLE_CONDVAR(Vehicle, build_year,           SLE_INT32,                 31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2207
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2208
	     SLE_VAR(Vehicle, load_unload_time_rem, SLE_UINT16),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2209
	SLEG_CONDVAR(         _cargo_paid_for,      SLE_UINT16,                45, SL_MAX_VERSION),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2210
	 SLE_CONDVAR(Vehicle, vehicle_flags,        SLE_UINT8,                 40, SL_MAX_VERSION),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2211
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2212
	 SLE_CONDVAR(Vehicle, profit_this_year,     SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2213
	 SLE_CONDVAR(Vehicle, profit_this_year,     SLE_INT64,                 65, SL_MAX_VERSION),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2214
	 SLE_CONDVAR(Vehicle, profit_last_year,     SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2215
	 SLE_CONDVAR(Vehicle, profit_last_year,     SLE_INT64,                 65, SL_MAX_VERSION),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2216
	SLEG_CONDVAR(         _cargo_feeder_share,  SLE_FILE_I32 | SLE_VAR_I64,51, 64),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2217
	SLEG_CONDVAR(         _cargo_feeder_share,  SLE_INT64,                 65, 67),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2218
	SLEG_CONDVAR(         _cargo_loaded_at_xy,  SLE_UINT32,                51, 67),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2219
	 SLE_CONDVAR(Vehicle, value,                SLE_FILE_I32 | SLE_VAR_I64, 0, 64),
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2220
	 SLE_CONDVAR(Vehicle, value,                SLE_INT64,                 65, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2221
8522
956a6b77e0ee (svn r12097) -Fix (r654, r1525): loading old, pre savegame version 2, savegames.
glx
parents: 8463
diff changeset
  2222
	 SLE_CONDVAR(Vehicle, random_bits,          SLE_UINT8,                 2, SL_MAX_VERSION),
956a6b77e0ee (svn r12097) -Fix (r654, r1525): loading old, pre savegame version 2, savegames.
glx
parents: 8463
diff changeset
  2223
	 SLE_CONDVAR(Vehicle, waiting_triggers,     SLE_UINT8,                 2, SL_MAX_VERSION),
956a6b77e0ee (svn r12097) -Fix (r654, r1525): loading old, pre savegame version 2, savegames.
glx
parents: 8463
diff changeset
  2224
956a6b77e0ee (svn r12097) -Fix (r654, r1525): loading old, pre savegame version 2, savegames.
glx
parents: 8463
diff changeset
  2225
	 SLE_CONDREF(Vehicle, next_shared,        REF_VEHICLE,                 2, SL_MAX_VERSION),
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2226
	SLE_CONDNULL(2,                                                        2, 68),
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2227
	SLE_CONDNULL(4,                                                       69, 100),
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2228
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  2229
	SLE_CONDVAR(Vehicle, group_id,             SLE_UINT16,                60, SL_MAX_VERSION),
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  2230
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2231
	SLE_CONDVAR(Vehicle, current_order_time,   SLE_UINT32,                67, SL_MAX_VERSION),
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2232
	SLE_CONDVAR(Vehicle, lateness_counter,     SLE_INT32,                 67, SL_MAX_VERSION),
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2233
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2234
	/* reserve extra space in savegame here. (currently 10 bytes) */
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2235
	SLE_CONDNULL(10,                                                       2, SL_MAX_VERSION),
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
  2236
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2237
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2238
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2239
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2240
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2241
static const SaveLoad _train_desc[] = {
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2242
	SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN),
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2243
	SLE_VEH_INCLUDEX(),
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2244
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, crash_anim_pos),         SLE_UINT16),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2245
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, force_proceed),          SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2246
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, railtype),               SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2247
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, track),                  SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2248
9808
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  2249
	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags), SLE_FILE_U8  | SLE_VAR_U16,  2, 99),
a914bcdca6fb (svn r13950) -Add [YAPP]: Trains can now also be in a 'stuck' state when waiting on a path reservation. (michi_cc)
rubidium
parents: 9775
diff changeset
  2250
	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags),                 SLE_UINT16,100, SL_MAX_VERSION),
6573
26d4c4618f60 (svn r9784) -Codechange: remove unused variable.
rubidium
parents: 6568
diff changeset
  2251
	SLE_CONDNULL(2, 2, 59),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2252
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3185
diff changeset
  2253
	SLE_CONDNULL(2, 2, 19),
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2254
	/* reserve extra space in savegame here. (currently 11 bytes) */
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3185
diff changeset
  2255
	SLE_CONDNULL(11, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2256
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2257
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2258
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2259
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2260
static const SaveLoad _roadveh_desc[] = {
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2261
	SLE_WRITEBYTE(Vehicle, type, VEH_ROAD),
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2262
	SLE_VEH_INCLUDEX(),
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2263
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, state),          SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2264
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, frame),          SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2265
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, blocked_ctr),    SLE_UINT16),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2266
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking),     SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2267
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking_ctr), SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2268
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, crashed_ctr),    SLE_UINT16),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2269
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, reverse_ctr),    SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2270
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2271
	SLE_CONDREFX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot),     REF_ROADSTOPS, 6, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2272
	SLE_CONDNULL(1,                                                                     6, SL_MAX_VERSION),
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2273
	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot_age), SLE_UINT8,     6, SL_MAX_VERSION),
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2274
	/* reserve extra space in savegame here. (currently 16 bytes) */
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2275
	SLE_CONDNULL(16,                                                                    2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2276
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2277
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2278
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2279
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2280
static const SaveLoad _ship_desc[] = {
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2281
	SLE_WRITEBYTE(Vehicle, type, VEH_SHIP),
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2282
	SLE_VEH_INCLUDEX(),
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2283
	SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2284
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2285
	/* reserve extra space in savegame here. (currently 16 bytes) */
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3185
diff changeset
  2286
	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2287
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2288
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2289
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2290
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2291
static const SaveLoad _aircraft_desc[] = {
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2292
	SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT),
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2293
	SLE_VEH_INCLUDEX(),
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2294
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, crashed_counter), SLE_UINT16),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2295
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, pos),             SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2296
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2297
	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport),   SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2298
	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport),   SLE_UINT16,                5, SL_MAX_VERSION),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2299
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2300
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, state),           SLE_UINT8),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2301
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2302
	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, previous_pos),    SLE_UINT8,                 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2303
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2304
	/* reserve extra space in savegame here. (currently 15 bytes) */
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2305
	SLE_CONDNULL(15,                                                                                      2, SL_MAX_VERSION),
0
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
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2308
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2309
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2310
static const SaveLoad _special_desc[] = {
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9005
diff changeset
  2311
	SLE_WRITEBYTE(Vehicle, type, VEH_EFFECT),
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2312
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2313
	    SLE_VAR(Vehicle, subtype,       SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2314
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2315
	SLE_CONDVAR(Vehicle, tile,          SLE_FILE_U16 | SLE_VAR_U32, 0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2316
	SLE_CONDVAR(Vehicle, tile,          SLE_UINT32,                 6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2317
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2318
	SLE_CONDVAR(Vehicle, x_pos,         SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2319
	SLE_CONDVAR(Vehicle, x_pos,         SLE_INT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2320
	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2321
	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2322
	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2323
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2324
	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
6561
073e48516812 (svn r9763) -Codechange/Fix (r9760): don't read the variables that are calculated automatically either.
rubidium
parents: 6558
diff changeset
  2325
	SLE_CONDNULL(5,                                                 0, 57),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2326
	    SLE_VAR(Vehicle, progress,      SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2327
	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2328
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9005
diff changeset
  2329
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleEffect, animation_state),    SLE_UINT16),
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9005
diff changeset
  2330
	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleEffect, animation_substate), SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2331
9955
1a16c6aee460 (svn r14110) -Fix: desyncs due to bubbles in toyland
glx
parents: 9942
diff changeset
  2332
	SLE_CONDVAR(Vehicle, spritenum,     SLE_UINT8, 2, SL_MAX_VERSION),
1a16c6aee460 (svn r14110) -Fix: desyncs due to bubbles in toyland
glx
parents: 9942
diff changeset
  2333
1a16c6aee460 (svn r14110) -Fix: desyncs due to bubbles in toyland
glx
parents: 9942
diff changeset
  2334
	/* reserve extra space in savegame here. (currently 15 bytes) */
1a16c6aee460 (svn r14110) -Fix: desyncs due to bubbles in toyland
glx
parents: 9942
diff changeset
  2335
	SLE_CONDNULL(15, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2336
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2337
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2338
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2339
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1840
diff changeset
  2340
static const SaveLoad _disaster_desc[] = {
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2341
	SLE_WRITEBYTE(Vehicle, type, VEH_DISASTER),
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2342
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2343
	    SLE_REF(Vehicle, next,          REF_VEHICLE_OLD),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2345
	    SLE_VAR(Vehicle, subtype,       SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2346
	SLE_CONDVAR(Vehicle, tile,          SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2347
	SLE_CONDVAR(Vehicle, tile,          SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2348
	SLE_CONDVAR(Vehicle, dest_tile,     SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2349
	SLE_CONDVAR(Vehicle, dest_tile,     SLE_UINT32,                  6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2350
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2351
	SLE_CONDVAR(Vehicle, x_pos,         SLE_FILE_I16 | SLE_VAR_I32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2352
	SLE_CONDVAR(Vehicle, x_pos,         SLE_INT32,                   6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2353
	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32,  0, 5),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2354
	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                   6, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2355
	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2356
	    SLE_VAR(Vehicle, direction,     SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2357
6561
073e48516812 (svn r9763) -Codechange/Fix (r9760): don't read the variables that are calculated automatically either.
rubidium
parents: 6558
diff changeset
  2358
	SLE_CONDNULL(5,                                                  0, 57),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2359
	    SLE_VAR(Vehicle, owner,         SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2360
	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2361
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2362
	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16,                5, SL_MAX_VERSION),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2363
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2364
	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2365
	SLE_CONDVAR(Vehicle, age,           SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2366
	SLE_CONDVAR(Vehicle, age,           SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2367
	    SLE_VAR(Vehicle, tick_counter,  SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2368
7334
ed9a43cf642a (svn r10697) -Codechange: give a more sensible names to some of the unkX variables.
rubidium
parents: 7333
diff changeset
  2369
	   SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, image_override),           SLE_UINT16),
ed9a43cf642a (svn r10697) -Codechange: give a more sensible names to some of the unkX variables.
rubidium
parents: 7333
diff changeset
  2370
	   SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, big_ufo_destroyer_target), SLE_UINT16),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2371
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2372
	/* reserve extra space in savegame here. (currently 16 bytes) */
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
  2373
	SLE_CONDNULL(16,                                                 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2374
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2375
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2376
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2377
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2378
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2379
static const SaveLoad *_veh_descs[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2380
	_train_desc,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2381
	_roadveh_desc,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2382
	_ship_desc,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2383
	_aircraft_desc,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2384
	_special_desc,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2385
	_disaster_desc,
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2386
	_common_veh_desc,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2387
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2388
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2389
	return _veh_descs[vt];
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2390
}
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2391
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2392
/** Will be called when the vehicles need to be saved. */
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6198
diff changeset
  2393
static void Save_VEHS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2394
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2395
	Vehicle *v;
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2396
	/* Write the vehicles */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2397
	FOR_ALL_VEHICLES(v) {
4346
66105d4f6e83 (svn r6047) -Codechange: FOR_ALL now _only_ loops valid items, and skips invalid ones
truelight
parents: 4344
diff changeset
  2398
		SlSetArrayIndex(v->index);
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2399
		SlObject(v, GetVehicleDescription(v->type));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2400
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2401
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2402
6423
8e10e79e0fd1 (svn r9559) -Documentation: doxygen and comment changes: 'U' and 'V' now. Almost done. Yeah. I know, I've already said that...
belugas
parents: 6350
diff changeset
  2403
/** Will be called when vehicles need to be loaded. */
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  2404
void Load_VEHS()
0
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
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2407
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2408
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2409
	_cargo_count = 0;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2410
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2411
	while ((index = SlIterateArray()) != -1) {
1279
bc761aad52b3 (svn r1783) -Add: Dynamic vehicles (now up to 64k of vehicles)
truelight
parents: 1266
diff changeset
  2412
		Vehicle *v;
6817
ca1cc2fd4cfa (svn r10056) -Revert (r10049): removing SLE_WRITEBYTE didn't work as expected :(. Somehow SlIterateArray and SlObject depend on eachother and adding a some arbitrary data before the SlObject makes it go crazy.
rubidium
parents: 6810
diff changeset
  2413
		VehicleType vtype = (VehicleType)SlReadByte();
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2414
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2415
		switch (vtype) {
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2416
			case VEH_TRAIN:    v = new (index) Train();           break;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2417
			case VEH_ROAD:     v = new (index) RoadVehicle();     break;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2418
			case VEH_SHIP:     v = new (index) Ship();            break;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2419
			case VEH_AIRCRAFT: v = new (index) Aircraft();        break;
9008
de19c73e405f (svn r12803) -Cleanup: rename SpecialVehicle to EffectVehicle to have a uniform naming of the thing instead of using both names for the same thing.
rubidium
parents: 9005
diff changeset
  2420
			case VEH_EFFECT:   v = new (index) EffectVehicle();   break;
7398
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2421
			case VEH_DISASTER: v = new (index) DisasterVehicle(); break;
b933416cf32b (svn r10770) -Codechange: use the pool item class as super class for the vehicle struct.
rubidium
parents: 7387
diff changeset
  2422
			case VEH_INVALID:  v = new (index) InvalidVehicle();  break;
6621
441559124aea (svn r9841) -Codechange: add a little more type strictness to the vehicle types.
rubidium
parents: 6616
diff changeset
  2423
			default: NOT_REACHED();
6552
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2424
		}
d87268e08799 (svn r9754) -Codechange: make classes for all vehicle types, so we can make nicer/better maintainable code, i.e. virtual methods instead of switches.
rubidium
parents: 6550
diff changeset
  2425
7493
07944c9e005f (svn r11004) -Codechange: some reworks of the saveload mechanism to be able to save and load private and protected variables in the vehicle struct.
rubidium
parents: 7492
diff changeset
  2426
		SlObject(v, GetVehicleDescription(vtype));
7010
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2427
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2428
		if (_cargo_count != 0 && IsPlayerBuildableVehicleType(v)) {
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2429
			/* Don't construct the packet with station here, because that'll fail with old savegames */
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2430
			CargoPacket *cp = new CargoPacket();
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2431
			cp->source          = _cargo_source;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2432
			cp->source_xy       = _cargo_source_xy;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2433
			cp->count           = _cargo_count;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2434
			cp->days_in_transit = _cargo_days;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2435
			cp->feeder_share    = _cargo_feeder_share;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2436
			cp->loaded_at_xy    = _cargo_loaded_at_xy;
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2437
			v->cargo.Append(cp);
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2438
		}
6f0d9f03180d (svn r10266) -Codechange: keep track of the origin, time of travel and accumulated feeder share (transfers) of individual pieces of cargo. This means that cargo isn't thrown on a big pile when it's put in a station or unloaded at a station, however the GUI does not reflect these changes yet so you will not actually see it.
rubidium
parents: 6998
diff changeset
  2439
2549
5587f9a38563 (svn r3078) Some more stuff, which piled up:
tron
parents: 2498
diff changeset
  2440
		/* Old savegames used 'last_station_visited = 0xFF' */
2685
805a28b7c3b7 (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2681
diff changeset
  2441
		if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
2469
59a0073914d8 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2467
diff changeset
  2442
			v->last_station_visited = INVALID_STATION;
956
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2443
2685
805a28b7c3b7 (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2681
diff changeset
  2444
		if (CheckSavegameVersion(5)) {
956
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2445
			/* Convert the current_order.type (which is a mix of type and flags, because
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  2446
			 *  in those versions, they both were 4 bits big) to type and flags */
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  2447
			v->current_order.flags = GB(v->current_order.type, 4, 4);
8881
ad608e8305ad (svn r12648) -Feature: allow four different non-stop types in a single game instead of two. The "TTDP compatible order" setting now only sets the default behaviour of new trains.
rubidium
parents: 8862
diff changeset
  2448
			v->current_order.type &= 0x0F;
956
dda0863632df (svn r1448) -Fix: The current_order of a vehicle is now correctly saved (caused
truelight
parents: 941
diff changeset
  2449
		}
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  2450
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  2451
		/* Advanced vehicle lists got added */
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6641
diff changeset
  2452
		if (CheckSavegameVersion(60)) v->group_id = DEFAULT_GROUP;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2453
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2454
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2455
	/* Check for shared order-lists (we now use pointers for that) */
2685
805a28b7c3b7 (svn r3227) -Codechange: [Savegame] removed 'minor' version, and renamed 'major' version to just: version.
truelight
parents: 2681
diff changeset
  2456
	if (CheckSavegameVersionOldStyle(5, 2)) {
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2457
		FOR_ALL_VEHICLES(v) {
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2458
			Vehicle *u;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2459
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2460
			FOR_ALL_VEHICLES_FROM(u, v->index + 1) {
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2461
				/* If a vehicle has the same orders, add the link to eachother
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4544
diff changeset
  2462
				 *  in both vehicles */
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2463
				if (v->orders == u->orders) {
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2464
					u->AddToShared(v);
1024
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2465
					break;
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2466
				}
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2467
			}
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2468
		}
5e446b5b3ec5 (svn r1525) -Codechange: rewrote the _order_array, now it can be made dynamic.
truelight
parents: 956
diff changeset
  2469
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2470
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2471
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
  2472
extern const ChunkHandler _veh_chunk_handlers[] = {
1542
62a03537ad0b (svn r2046) -Codechange: moved all waypoint code to waypoint.c/waypoint.h
truelight
parents: 1541
diff changeset
  2473
	{ 'VEHS', Save_VEHS, Load_VEHS, CH_SPARSE_ARRAY | CH_LAST},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  2474
};
5651
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2475
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2476
void Vehicle::BeginLoading()
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2477
{
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6248
diff changeset
  2478
	assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP);
6550
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2479
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  2480
	if (this->current_order.IsType(OT_GOTO_STATION) &&
8840
332412c2e9c1 (svn r12588) -Codechange: do not access the destination of an order directly.
rubidium
parents: 8838
diff changeset
  2481
			this->current_order.GetDestination() == this->last_station_visited) {
8881
ad608e8305ad (svn r12648) -Feature: allow four different non-stop types in a single game instead of two. The "TTDP compatible order" setting now only sets the default behaviour of new trains.
rubidium
parents: 8862
diff changeset
  2482
		current_order.MakeLoading(true);
ad608e8305ad (svn r12648) -Feature: allow four different non-stop types in a single game instead of two. The "TTDP compatible order" setting now only sets the default behaviour of new trains.
rubidium
parents: 8862
diff changeset
  2483
		UpdateVehicleTimetable(this, true);
ad608e8305ad (svn r12648) -Feature: allow four different non-stop types in a single game instead of two. The "TTDP compatible order" setting now only sets the default behaviour of new trains.
rubidium
parents: 8862
diff changeset
  2484
6550
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2485
		/* Furthermore add the Non Stop flag to mark that this station
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2486
		 * is the actual destination of the vehicle, which is (for example)
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2487
		 * necessary to be known for HandleTrainLoading to determine
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2488
		 * whether the train is lost or not; not marking a train lost
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2489
		 * that arrives at random stations is bad. */
8848
635cb164edd4 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 8847
diff changeset
  2490
		this->current_order.SetNonStopType(ONSF_NO_STOP_AT_ANY_STATION);
8843
45fdf630deaa (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 8840
diff changeset
  2491
6550
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2492
	} else {
8843
45fdf630deaa (svn r12593) -Codechange: hide Order's flags in most of the code.
rubidium
parents: 8840
diff changeset
  2493
		current_order.MakeLoading(false);
6550
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2494
	}
8bdb954bba16 (svn r9752) -Codechange: remove some duplication related to BeginLoading.
rubidium
parents: 6546
diff changeset
  2495
6500
f12678890222 (svn r9683) -Fix [FS#423]: improved loading does not use a huge amount of processing power anymore when having a lot of trains.
rubidium
parents: 6492
diff changeset
  2496
	GetStation(this->last_station_visited)->loading_vehicles.push_back(this);
6553
976a684212ad (svn r9755) -Codechange: refactor some more of the begin loading stuff.
rubidium
parents: 6552
diff changeset
  2497
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6562
diff changeset
  2498
	VehiclePayment(this);
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6562
diff changeset
  2499
9043
eb4f06b99146 (svn r12862) -Codechange: reduce code duplication for VehicleType -> WindowClass lookup
smatz
parents: 9041
diff changeset
  2500
	InvalidateWindow(GetWindowClassForVehicleType(this->type), this->owner);
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
  2501
	InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6562
diff changeset
  2502
	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6562
diff changeset
  2503
	InvalidateWindow(WC_STATION_VIEW, this->last_station_visited);
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6562
diff changeset
  2504
6823
1f2075619543 (svn r10062) -Codechange: Don't redraw all station tiles when cargo is added or removed if the station has no custom graphics.
peter1138
parents: 6817
diff changeset
  2505
	GetStation(this->last_station_visited)->MarkTilesDirty(true);
6565
96ef0ede4b83 (svn r9770) -Codechange: perform the payment of cargo when loading/unloading, but when arriving at the station.
rubidium
parents: 6562
diff changeset
  2506
	this->MarkDirty();
5651
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2507
}
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2508
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2509
void Vehicle::LeaveStation()
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2510
{
8837
72125f871eb4 (svn r12585) -Fix (r12584): assumption that assertions were enabled during compilation/testing was flawed.
rubidium
parents: 8836
diff changeset
  2511
	assert(current_order.IsType(OT_LOADING));
7076
0416a03dff7c (svn r10341) -Fix (r10236): Only update the timetable when leaving stations if this is a scheduled stop.
maedhros
parents: 7070
diff changeset
  2512
0416a03dff7c (svn r10341) -Fix (r10236): Only update the timetable when leaving stations if this is a scheduled stop.
maedhros
parents: 7070
diff changeset
  2513
	/* Only update the timetable if the vehicle was supposed to stop here. */
8848
635cb164edd4 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 8847
diff changeset
  2514
	if (current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE) UpdateVehicleTimetable(this, false);
7076
0416a03dff7c (svn r10341) -Fix (r10236): Only update the timetable when leaving stations if this is a scheduled stop.
maedhros
parents: 7070
diff changeset
  2515
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  2516
	current_order.MakeLeaveStation();
9041
2e64f7dc7a48 (svn r12860) -Fix [FS#1947]: Train depart animation trigger was not called in some cases.
peter1138
parents: 9009
diff changeset
  2517
	Station *st = GetStation(this->last_station_visited);
2e64f7dc7a48 (svn r12860) -Fix [FS#1947]: Train depart animation trigger was not called in some cases.
peter1138
parents: 9009
diff changeset
  2518
	st->loading_vehicles.remove(this);
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2519
6998
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  2520
	HideFillingPercent(this->fill_percent_te_id);
39e783d3816c (svn r10254) -Feature: loading indicator, which shows in % how full a vehicle is while loading/unloading (TheJosh)
truelight
parents: 6980
diff changeset
  2521
	this->fill_percent_te_id = INVALID_TE_ID;
9041
2e64f7dc7a48 (svn r12860) -Fix [FS#1947]: Train depart animation trigger was not called in some cases.
peter1138
parents: 9009
diff changeset
  2522
9891
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2523
	if (this->type == VEH_TRAIN) {
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2524
		/* Trigger station animation (trains only) */
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2525
		if (IsTileType(this->tile, MP_STATION)) StationAnimationTrigger(st, this->tile, STAT_ANIM_TRAIN_DEPARTS);
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2526
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2527
		/* Try to reserve a path when leaving the station as we
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2528
		 * might not be marked as wanting a reservation, e.g.
9975
214628d1c9e8 (svn r14132) -Fix (r14039): Only reserve a path on leaving a station if the current block really is a PBS block. (michi_cc)
frosch
parents: 9955
diff changeset
  2529
		 * when an overlength train gets turned around in a station. */
214628d1c9e8 (svn r14132) -Fix (r14039): Only reserve a path on leaving a station if the current block really is a PBS block. (michi_cc)
frosch
parents: 9955
diff changeset
  2530
		if (UpdateSignalsOnSegment(this->tile, TrackdirToExitdir(GetVehicleTrackdir(this)), this->owner) == SIGSEG_PBS || _settings_game.pf.reserve_paths) {
214628d1c9e8 (svn r14132) -Fix (r14039): Only reserve a path on leaving a station if the current block really is a PBS block. (michi_cc)
frosch
parents: 9955
diff changeset
  2531
			TryPathReserve(this, true, true);
214628d1c9e8 (svn r14132) -Fix (r14039): Only reserve a path on leaving a station if the current block really is a PBS block. (michi_cc)
frosch
parents: 9955
diff changeset
  2532
		}
9891
edc31db66fbb (svn r14039) -Fix [FS#2217]: reversing an overlength train at a station would try to find a route out and sometimes reset the unload counter making it wait 2.5 years before departing instead of waiting until the train would actually leave the station (michi_cc)
rubidium
parents: 9808
diff changeset
  2533
	}
5651
79496e6d8a56 (svn r8110) -Codechange: direct Vehicle::current_order.type changes (to OT_LOADING and OT_LEAVESTATION) replaced by v->BeginLoading() and v->LeaveStation() calls. This should allow easy hooking of those state transitions in order to maintain vehicle loading queue.
KUDr
parents: 5609
diff changeset
  2534
}
6558
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  2535
c88e142f896e (svn r9760) -Codechange: remove the need for saving some vehicle variables.
rubidium
parents: 6553
diff changeset
  2536
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2537
void Vehicle::HandleLoading(bool mode)
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2538
{
8836
f6f1ea3d7e93 (svn r12584) -Codechange: do not access the order type directly.
rubidium
parents: 8793
diff changeset
  2539
	switch (this->current_order.GetType()) {
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2540
		case OT_LOADING: {
6980
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2541
			uint wait_time = max(this->current_order.wait_time - this->lateness_counter, 0);
c7c4f3bf5901 (svn r10236) -Feature: Introduce a form of timetabling for vehicles.
maedhros
parents: 6955
diff changeset
  2542
6616
0b3835bdd796 (svn r9836) -Codechange: make non-improved loading happen FIFO-ish; generally loading/unloading will happen fifo, but there are no guarantees on the FIFO-ness. For (better) FIFO guarantees you still need to use improved loading.
rubidium
parents: 6611
diff changeset
  2543
			/* Not the first call for this tick, or still loading */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7923
diff changeset
  2544
			if (mode || !HasBit(this->vehicle_flags, VF_LOADING_FINISHED) ||
9413
7042a8ec3fa8 (svn r13325) -Codechange: split the client-side only settings from the settings stored in the savegame so there is no need to have a duplicate copy of it for new games.
rubidium
parents: 9396
diff changeset
  2545
					(_settings_game.order.timetabling && this->current_order_time < wait_time)) return;
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2546
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2547
			this->PlayLeaveStationSound();
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2548
8848
635cb164edd4 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 8847
diff changeset
  2549
			bool at_destination_station = this->current_order.GetNonStopType() != ONSF_STOP_EVERYWHERE;
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2550
			this->LeaveStation();
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2551
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2552
			/* If this was not the final order, don't remove it from the list. */
8848
635cb164edd4 (svn r12600) -Codechange: make GetNonStopType return a more augmented type; not is there a non-stop order but the kind of non-stop order, so one doesn't need to check _patches.new_nonstop type everywhere.
rubidium
parents: 8847
diff changeset
  2553
			if (!at_destination_station) return;
6594
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2554
			break;
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2555
		}
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2556
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2557
		case OT_DUMMY: break;
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2558
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2559
		default: return;
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2560
	}
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2561
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2562
	this->cur_order_index++;
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2563
	InvalidateVehicleOrder(this);
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2564
}
9673331eb174 (svn r9808) -Codechange: unify the Handle<VehicleType>Loading functions.
rubidium
parents: 6585
diff changeset
  2565
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2566
CommandCost Vehicle::SendToDepot(uint32 flags, DepotCommand command)
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2567
{
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2568
	if (!CheckOwnership(this->owner)) return CMD_ERROR;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2569
	if (this->vehstatus & VS_CRASHED) return CMD_ERROR;
9934
232a55f10cba (svn r14089) -Fix [FS#2219]: trains not being able to find a route to a depot when the front is already in the depot.
rubidium
parents: 9930
diff changeset
  2570
	if (this->IsStoppedInDepot()) return CMD_ERROR;
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2571
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2572
	if (this->current_order.IsType(OT_GOTO_DEPOT)) {
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2573
		bool halt_in_depot = this->current_order.GetDepotActionType() & ODATFB_HALT;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2574
		if (!!(command & DEPOT_SERVICE) == halt_in_depot) {
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2575
			/* We called with a different DEPOT_SERVICE setting.
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2576
			 * Now we change the setting to apply the new one and let the vehicle head for the same depot.
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2577
			 * Note: the if is (true for requesting service == true for ordered to stop in depot)          */
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2578
			if (flags & DC_EXEC) {
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2579
				this->current_order.SetDepotOrderType(ODTF_MANUAL);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2580
				this->current_order.SetDepotActionType(halt_in_depot ? ODATF_SERVICE_ONLY : ODATFB_HALT);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2581
				InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2582
			}
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2583
			return CommandCost();
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2584
		}
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2585
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2586
		if (command & DEPOT_DONT_CANCEL) return CMD_ERROR; // Requested no cancelation of depot orders
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2587
		if (flags & DC_EXEC) {
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2588
			/* If the orders to 'goto depot' are in the orders list (forced servicing),
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2589
			 * then skip to the next order; effectively cancelling this forced service */
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2590
			if (this->current_order.GetDepotOrderType() & ODTFB_PART_OF_ORDERS) this->cur_order_index++;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2591
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2592
			this->current_order.MakeDummy();
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2593
			InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2594
		}
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2595
		return CommandCost();
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2596
	}
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2597
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2598
	TileIndex location;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2599
	DestinationID destination;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2600
	bool reverse;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2601
	static const StringID no_depot[] = {STR_883A_UNABLE_TO_FIND_ROUTE_TO, STR_9019_UNABLE_TO_FIND_LOCAL_DEPOT, STR_981A_UNABLE_TO_FIND_LOCAL_DEPOT, STR_A012_CAN_T_SEND_AIRCRAFT_TO};
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  2602
	if (!this->FindClosestDepot(&location, &destination, &reverse)) return_cmd_error(no_depot[this->type]);
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2603
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2604
	if (flags & DC_EXEC) {
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2605
		if (this->current_order.IsType(OT_LOADING)) this->LeaveStation();
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2606
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2607
		this->dest_tile = location;
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2608
		this->current_order.MakeGoToDepot(destination, ODTF_MANUAL);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2609
		if (!(command & DEPOT_SERVICE)) this->current_order.SetDepotActionType(ODATFB_HALT);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2610
		InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, VVW_WIDGET_START_STOP_VEH);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2611
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2612
		/* If there is no depot in front, reverse automatically (trains only) */
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2613
		if (this->type == VEH_TRAIN && reverse) DoCommand(this->tile, this->index, 0, DC_EXEC, CMD_REVERSE_TRAIN_DIRECTION);
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2614
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2615
		if (this->type == VEH_AIRCRAFT && this->u.air.state == FLYING && this->u.air.targetairport != destination) {
8892
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  2616
			/* The aircraft is now heading for a different hangar than the next in the orders */
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  2617
			extern void AircraftNextAirportPos_and_Order(Vehicle *v);
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  2618
			AircraftNextAirportPos_and_Order(this);
8401e1338665 (svn r12659) -Cleanup: some style cleanups in vehicle.cpp.
rubidium
parents: 8891
diff changeset
  2619
		}
8891
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2620
	}
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2621
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2622
	return CommandCost();
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2623
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2624
}
1058c3b69b7f (svn r12658) -Codechange: unify a vast portion of the CmdSend<VehicleType>ToDepot commands.
rubidium
parents: 8881
diff changeset
  2625
7497
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2626
void Vehicle::SetNext(Vehicle *next)
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2627
{
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2628
	if (this->next != NULL) {
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2629
		/* We had an old next vehicle. Update the first and previous pointers */
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2630
		for (Vehicle *v = this->next; v != NULL; v = v->Next()) {
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2631
			v->first = this->next;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2632
		}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2633
		this->next->previous = NULL;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2634
	}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2635
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2636
	this->next = next;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2637
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2638
	if (this->next != NULL) {
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2639
		/* A new next vehicle. Update the first and previous pointers */
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2640
		if (this->next->previous != NULL) this->next->previous->next = NULL;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2641
		this->next->previous = this;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2642
		for (Vehicle *v = this->next; v != NULL; v = v->Next()) {
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2643
			v->first = this->first;
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2644
		}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2645
	}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2646
}
40e457c0a8ac (svn r11011) -Fix [FS#1129]: GetFirstVehicleInChain did change the game state while being marked const.
rubidium
parents: 7493
diff changeset
  2647
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2648
void Vehicle::AddToShared(Vehicle *shared_chain)
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2649
{
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2650
	assert(!this->IsOrderListShared());
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2651
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2652
	this->next_shared     = shared_chain->next_shared;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2653
	this->previous_shared = shared_chain;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2654
	this->first_shared    = shared_chain->first_shared;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2655
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2656
	shared_chain->next_shared = this;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2657
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2658
	if (this->next_shared != NULL) this->next_shared->previous_shared = this;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2659
}
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2660
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2661
void Vehicle::RemoveFromShared()
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2662
{
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2663
	Vehicle *new_first;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2664
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2665
	if (this->FirstShared() == this) {
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2666
		/* We are the first shared one, so update all the first pointers of our next shared ones. */
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2667
		new_first = this->NextShared();
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2668
		for (Vehicle *u = new_first; u != NULL; u = u->NextShared()) {
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2669
			u->first_shared = new_first;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2670
		}
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2671
	} else {
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2672
		/* We are not the first shared one, so only relink our previous one. */
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2673
		new_first = this->FirstShared();
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2674
		this->previous_shared->next_shared = this->NextShared();
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2675
	}
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2676
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2677
	if (this->next_shared != NULL) this->next_shared->previous_shared = this->previous_shared;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2678
9942
c3677fa5563f (svn r14097) -Fix [FS#2085]: one couldn't get a list of vehicles sharing an order when the number of orders was 0; you could see that the vehicles had a shared order though.
rubidium
parents: 9941
diff changeset
  2679
	uint32 old_window_number = (this->FirstShared()->index << 16) | (this->type << 11) | VLW_SHARED_ORDERS | this->owner;
c3677fa5563f (svn r14097) -Fix [FS#2085]: one couldn't get a list of vehicles sharing an order when the number of orders was 0; you could see that the vehicles had a shared order though.
rubidium
parents: 9941
diff changeset
  2680
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2681
	if (new_first->NextShared() == NULL) {
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2682
		/* When there is only one vehicle, remove the shared order list window. */
9942
c3677fa5563f (svn r14097) -Fix [FS#2085]: one couldn't get a list of vehicles sharing an order when the number of orders was 0; you could see that the vehicles had a shared order though.
rubidium
parents: 9941
diff changeset
  2683
		DeleteWindowById(GetWindowClassForVehicleType(this->type), old_window_number);
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2684
		InvalidateVehicleOrder(new_first);
9942
c3677fa5563f (svn r14097) -Fix [FS#2085]: one couldn't get a list of vehicles sharing an order when the number of orders was 0; you could see that the vehicles had a shared order though.
rubidium
parents: 9941
diff changeset
  2685
	} else if (this->FirstShared() == this) {
c3677fa5563f (svn r14097) -Fix [FS#2085]: one couldn't get a list of vehicles sharing an order when the number of orders was 0; you could see that the vehicles had a shared order though.
rubidium
parents: 9941
diff changeset
  2686
		/* If we were the first one, update to the new first one. */
c3677fa5563f (svn r14097) -Fix [FS#2085]: one couldn't get a list of vehicles sharing an order when the number of orders was 0; you could see that the vehicles had a shared order though.
rubidium
parents: 9941
diff changeset
  2687
		InvalidateWindowData(GetWindowClassForVehicleType(this->type), old_window_number, (new_first->index << 16) | (1 << 15));
9941
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2688
	}
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2689
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2690
	this->first_shared    = this;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2691
	this->next_shared     = NULL;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2692
	this->previous_shared = NULL;
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2693
}
8549448b55fa (svn r14096) -Codechange: merge the shared order's vehicle list management to a single location.
rubidium
parents: 9940
diff changeset
  2694
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2695
void StopAllVehicles()
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2696
{
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2697
	Vehicle *v;
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2698
	FOR_ALL_VEHICLES(v) {
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2699
		/* Code ripped from CmdStartStopTrain. Can't call it, because of
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2700
		 * ownership problems, so we'll duplicate some code, for now */
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2701
		v->vehstatus |= VS_STOPPED;
8350
8979cccdbed2 (svn r11916) -Codechange: use enums from VehicleViewWindowWidgets instead of constants
smatz
parents: 8317
diff changeset
  2702
		InvalidateWindowWidget(WC_VEHICLE_VIEW, v->index, VVW_WIDGET_START_STOP_VEH);
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2703
		InvalidateWindow(WC_VEHICLE_DEPOT, v->tile);
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2704
	}
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8140
diff changeset
  2705
}