src/vehicle.cpp
branchnoai
changeset 9620 31e38d28a0af
parent 9601 b499fdd106d5
child 9624 b71483f2330f
equal deleted inserted replaced
9619:6e81cec30a2b 9620:31e38d28a0af
    38 #include "date.h"
    38 #include "date.h"
    39 #include "newgrf_callbacks.h"
    39 #include "newgrf_callbacks.h"
    40 #include "newgrf_engine.h"
    40 #include "newgrf_engine.h"
    41 #include "newgrf_sound.h"
    41 #include "newgrf_sound.h"
    42 #include "helpers.hpp"
    42 #include "helpers.hpp"
       
    43 #include "economy.h"
    43 
    44 
    44 #define INVALID_COORD (-0x8000)
    45 #define INVALID_COORD (-0x8000)
    45 #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
    46 #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
    46 
    47 
    47 
    48 
    87 
    88 
    88 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    89 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
    89 	 * TODO - This is just a temporary stage, this will be removed. */
    90 	 * TODO - This is just a temporary stage, this will be removed. */
    90 	for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
    91 	for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1U < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
    91 		v->index = start_item++;
    92 		v->index = start_item++;
    92 		v->type  = VEH_INVALID;
    93 		v = new (v) InvalidVehicle();
    93 	}
    94 	}
    94 }
    95 }
    95 
    96 
    96 /* Initialize the vehicle-pool */
    97 /* Initialize the vehicle-pool */
    97 DEFINE_OLD_POOL(Vehicle, Vehicle, VehiclePoolNewBlock, NULL)
    98 DEFINE_OLD_POOL(Vehicle, Vehicle, VehiclePoolNewBlock, NULL)
   222 void AfterLoadVehicles()
   223 void AfterLoadVehicles()
   223 {
   224 {
   224 	Vehicle *v;
   225 	Vehicle *v;
   225 
   226 
   226 	FOR_ALL_VEHICLES(v) {
   227 	FOR_ALL_VEHICLES(v) {
       
   228 		v->UpdateDeltaXY(v->direction);
       
   229 
   227 		v->first = NULL;
   230 		v->first = NULL;
   228 		if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE;
   231 		if (v->type == VEH_TRAIN) v->u.rail.first_engine = INVALID_ENGINE;
   229 	}
   232 	}
   230 
   233 
   231 	FOR_ALL_VEHICLES(v) {
   234 	FOR_ALL_VEHICLES(v) {
   269 	memset(v, 0, sizeof(Vehicle));
   272 	memset(v, 0, sizeof(Vehicle));
   270 	v->index = index;
   273 	v->index = index;
   271 
   274 
   272 	assert(v->orders == NULL);
   275 	assert(v->orders == NULL);
   273 
   276 
   274 	v->type = VEH_INVALID;
   277 	v = new (v) InvalidVehicle();
   275 	v->left_coord = INVALID_COORD;
   278 	v->left_coord = INVALID_COORD;
   276 	v->first = NULL;
   279 	v->first = NULL;
   277 	v->next = NULL;
   280 	v->next = NULL;
   278 	v->next_hash = NULL;
   281 	v->next_hash = NULL;
   279 	v->string_id = 0;
   282 	v->string_id = 0;
   694 		Vehicle *w = v->depot_list;
   697 		Vehicle *w = v->depot_list;
   695 		v->depot_list = NULL; // it should always be NULL at the end of each tick
   698 		v->depot_list = NULL; // it should always be NULL at the end of each tick
   696 		MaybeReplaceVehicle(v, false, true);
   699 		MaybeReplaceVehicle(v, false, true);
   697 		v = w;
   700 		v = w;
   698 	}
   701 	}
   699 }
       
   700 
       
   701 static bool CanFillVehicle_FullLoadAny(Vehicle *v)
       
   702 {
       
   703 	uint32 full = 0, not_full = 0;
       
   704 	bool keep_loading = false;
       
   705 	const GoodsEntry *ge = GetStation(v->last_station_visited)->goods;
       
   706 
       
   707 	/* special handling of aircraft */
       
   708 
       
   709 	/* if the aircraft carries passengers and is NOT full, then
       
   710 	 *continue loading, no matter how much mail is in */
       
   711 	if (v->type == VEH_AIRCRAFT &&
       
   712 			IsCargoInClass(v->cargo_type, CC_PASSENGERS) &&
       
   713 			v->cargo_cap != v->cargo_count) {
       
   714 		return true;
       
   715 	}
       
   716 
       
   717 	/* patch should return "true" to continue loading, i.e. when there is no cargo type that is fully loaded. */
       
   718 	do {
       
   719 		/* Should never happen, but just in case future additions change this */
       
   720 		assert(v->cargo_type<32);
       
   721 
       
   722 		if (v->cargo_cap != 0) {
       
   723 			uint32 mask = 1 << v->cargo_type;
       
   724 
       
   725 			if (v->cargo_cap == v->cargo_count) {
       
   726 				full |= mask;
       
   727 			} else if (GB(ge[v->cargo_type].waiting_acceptance, 0, 12) > 0 ||
       
   728 					(HASBIT(v->vehicle_flags, VF_CARGO_UNLOADING) && (ge[v->cargo_type].waiting_acceptance & 0x8000))) {
       
   729 				/* If there is any cargo waiting, or this vehicle is still unloading
       
   730 				 * and the station accepts the cargo, don't leave the station. */
       
   731 				keep_loading = true;
       
   732 			} else {
       
   733 				not_full |= mask;
       
   734 			}
       
   735 		}
       
   736 	} while ((v = v->next) != NULL);
       
   737 
       
   738 	/* continue loading if there is a non full cargo type and no cargo type that is full */
       
   739 	return keep_loading || (not_full && (full & ~not_full) == 0);
       
   740 }
       
   741 
       
   742 bool CanFillVehicle(Vehicle *v)
       
   743 {
       
   744 	TileIndex tile = v->tile;
       
   745 
       
   746 	if (IsTileType(tile, MP_STATION) ||
       
   747 			(v->type == VEH_SHIP && (
       
   748 				IsTileType(TILE_ADDXY(tile,  1,  0), MP_STATION) ||
       
   749 				IsTileType(TILE_ADDXY(tile, -1,  0), MP_STATION) ||
       
   750 				IsTileType(TILE_ADDXY(tile,  0,  1), MP_STATION) ||
       
   751 				IsTileType(TILE_ADDXY(tile,  0, -1), MP_STATION) ||
       
   752 				IsTileType(TILE_ADDXY(tile, -2,  0), MP_STATION)
       
   753 			))) {
       
   754 
       
   755 		/* If patch is active, use alternative CanFillVehicle-function */
       
   756 		if (_patches.full_load_any && v->current_order.flags & OF_FULL_LOAD) return CanFillVehicle_FullLoadAny(v);
       
   757 
       
   758 		do {
       
   759 			if (v->cargo_count != v->cargo_cap) return true;
       
   760 		} while ((v = v->next) != NULL);
       
   761 	}
       
   762 	return false;
       
   763 }
   702 }
   764 
   703 
   765 /** Check if a given engine type can be refitted to a given cargo
   704 /** Check if a given engine type can be refitted to a given cargo
   766  * @param engine_type Engine type to check
   705  * @param engine_type Engine type to check
   767  * @param cid_to check refit to this cargo-type
   706  * @param cid_to check refit to this cargo-type
  1436 {
  1375 {
  1437 	Vehicle *v;
  1376 	Vehicle *v;
  1438 
  1377 
  1439 	v = ForceAllocateSpecialVehicle();
  1378 	v = ForceAllocateSpecialVehicle();
  1440 	if (v != NULL) {
  1379 	if (v != NULL) {
  1441 		v->type = VEH_SPECIAL;
  1380 		v = new (v) SpecialVehicle();
  1442 		v->subtype = type;
  1381 		v->subtype = type;
  1443 		v->x_pos = x;
  1382 		v->x_pos = x;
  1444 		v->y_pos = y;
  1383 		v->y_pos = y;
  1445 		v->z_pos = z;
  1384 		v->z_pos = z;
  1446 		v->z_height = v->sprite_width = v->sprite_height = 1;
       
  1447 		v->x_offs = v->y_offs = 0;
       
  1448 		v->tile = 0;
  1385 		v->tile = 0;
       
  1386 		v->UpdateDeltaXY(INVALID_DIR);
  1449 		v->vehstatus = VS_UNCLICKABLE;
  1387 		v->vehstatus = VS_UNCLICKABLE;
  1450 
  1388 
  1451 		_effect_init_procs[type](v);
  1389 		_effect_init_procs[type](v);
  1452 
  1390 
  1453 		VehiclePositionChanged(v);
  1391 		VehiclePositionChanged(v);
  1802  */
  1740  */
  1803 int32 CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1741 int32 CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1804 {
  1742 {
  1805 	Vehicle *v_front, *v;
  1743 	Vehicle *v_front, *v;
  1806 	Vehicle *w_front, *w, *w_rear;
  1744 	Vehicle *w_front, *w, *w_rear;
  1807 	int cost, total_cost = 0;
  1745 	int32 cost, total_cost = 0;
  1808 	uint32 build_argument = 2;
  1746 	uint32 build_argument = 2;
  1809 
  1747 
  1810 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
  1748 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
  1811 	v = GetVehicle(p1);
  1749 	v = GetVehicle(p1);
  1812 	v_front = v;
  1750 	v_front = v;
  1840 	}
  1778 	}
  1841 
  1779 
  1842 	v = v_front;
  1780 	v = v_front;
  1843 
  1781 
  1844 	do {
  1782 	do {
  1845 
       
  1846 		if (!(flags & DC_EXEC)) {
       
  1847 			/* Get the refit cost.
       
  1848 			 * This is only needed when estimating as when the command is executed, the cost from the refit command is used.
       
  1849 			 * This needs to be done for every single unit, so it should be done before checking if it's a multiheaded engine. */
       
  1850 			CargoID new_cargo_type = GetEngineCargoType(v->engine_type);
       
  1851 
       
  1852 			if (new_cargo_type != v->cargo_type && new_cargo_type != CT_INVALID) {
       
  1853 				total_cost += GetRefitCost(v->engine_type);
       
  1854 			}
       
  1855 		}
       
  1856 
       
  1857 		if (IsMultiheaded(v) && !IsTrainEngine(v)) {
  1783 		if (IsMultiheaded(v) && !IsTrainEngine(v)) {
  1858 			/* we build the rear ends of multiheaded trains with the front ones */
  1784 			/* we build the rear ends of multiheaded trains with the front ones */
  1859 			continue;
  1785 			continue;
  1860 		}
  1786 		}
  1861 
  1787 
  1866 
  1792 
  1867 		total_cost += cost;
  1793 		total_cost += cost;
  1868 
  1794 
  1869 		if (flags & DC_EXEC) {
  1795 		if (flags & DC_EXEC) {
  1870 			w = GetVehicle(_new_vehicle_id);
  1796 			w = GetVehicle(_new_vehicle_id);
  1871 
       
  1872 			Vehicle *w2 = w;
       
  1873 			Vehicle *v2 = v;
       
  1874 			do {
       
  1875 				if (v2->cargo_type != w2->cargo_type || v2->cargo_subtype != w2->cargo_subtype) {
       
  1876 					/* We can't pay for refitting because we can't estimate refitting costs for a vehicle before it's build.
       
  1877 					 * If we pay for it anyway, the cost and the estimated cost will not be the same and we will have an assert.
       
  1878 					 * We need to check the whole chain if it is a train because some newgrf articulated engines can refit some units only (and not the front) */
       
  1879 					total_cost += DoCommand(0, w->index, v2->cargo_type | (v2->cargo_subtype << 8), flags, GetCmdRefitVeh(v));
       
  1880 					break; // We learned that the engine in question needed a refit. No need to check anymore
       
  1881 				}
       
  1882 			} while (v->type == VEH_TRAIN && (w2 = w2->next) != NULL && (v2 = v2->next) != NULL);
       
  1883 
  1797 
  1884 			if (v->type == VEH_TRAIN && HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
  1798 			if (v->type == VEH_TRAIN && HASBIT(v->u.rail.flags, VRF_REVERSE_DIRECTION)) {
  1885 				SETBIT(w->u.rail.flags, VRF_REVERSE_DIRECTION);
  1799 				SETBIT(w->u.rail.flags, VRF_REVERSE_DIRECTION);
  1886 			}
  1800 			}
  1887 
  1801 
  1900 	} while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
  1814 	} while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
  1901 
  1815 
  1902 	if (flags & DC_EXEC && v_front->type == VEH_TRAIN) {
  1816 	if (flags & DC_EXEC && v_front->type == VEH_TRAIN) {
  1903 		/* for trains this needs to be the front engine due to the callback function */
  1817 		/* for trains this needs to be the front engine due to the callback function */
  1904 		_new_vehicle_id = w_front->index;
  1818 		_new_vehicle_id = w_front->index;
       
  1819 	}
       
  1820 
       
  1821 	/* Take care of refitting. */
       
  1822 	w = w_front;
       
  1823 	v = v_front;
       
  1824 
       
  1825 	/* Both building and refitting are influenced by newgrf callbacks, which
       
  1826 	 * makes it impossible to accurately estimate the cloning costs. In
       
  1827 	 * particular, it is possible for engines of the same type to be built with
       
  1828 	 * different numbers of articulated parts, so when refitting we have to
       
  1829 	 * loop over real vehicles first, and then the articulated parts of those
       
  1830 	 * vehicles in a different loop. */
       
  1831 	do {
       
  1832 		do {
       
  1833 			if (flags & DC_EXEC) {
       
  1834 				assert(w != NULL);
       
  1835 
       
  1836 				if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_type) {
       
  1837 					cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
       
  1838 					if (!CmdFailed(cost)) total_cost += cost;
       
  1839 				}
       
  1840 
       
  1841 				if (w->type == VEH_TRAIN && EngineHasArticPart(w)) {
       
  1842 					w = GetNextArticPart(w);
       
  1843 				} else {
       
  1844 					break;
       
  1845 				}
       
  1846 			} else {
       
  1847 				CargoID initial_cargo = GetEngineCargoType(v->engine_type);
       
  1848 
       
  1849 				if (v->cargo_type != initial_cargo && initial_cargo != CT_INVALID) {
       
  1850 					total_cost += GetRefitCost(v->engine_type);
       
  1851 				}
       
  1852 			}
       
  1853 		} while (v->type == VEH_TRAIN && EngineHasArticPart(v) && (v = GetNextArticPart(v)) != NULL);
       
  1854 
       
  1855 		if (flags & DC_EXEC) w = GetNextVehicle(w);
       
  1856 	} while (v->type == VEH_TRAIN && (v = GetNextVehicle(v)) != NULL);
       
  1857 
       
  1858 	/* Since we can't estimate the cost of cloning a vehicle accurately we must
       
  1859 	 * check whether the player has enough money manually. */
       
  1860 	if (!CheckPlayerHasMoney(total_cost)) {
       
  1861 		if (flags & DC_EXEC) {
       
  1862 			/* The vehicle has already been bought, so now it must be sold again. */
       
  1863 			DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
       
  1864 		}
       
  1865 		return CMD_ERROR;
  1905 	}
  1866 	}
  1906 
  1867 
  1907 	/* Set the expense type last as refitting will make the cost go towards
  1868 	/* Set the expense type last as refitting will make the cost go towards
  1908 	 * running costs... */
  1869 	 * running costs... */
  1909 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
  1870 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
  2219 			}
  2180 			}
  2220 		}
  2181 		}
  2221 
  2182 
  2222 		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) {
  2183 		if (HASBIT(t.flags, OFB_PART_OF_ORDERS)) {
  2223 			/* Part of orders */
  2184 			/* Part of orders */
  2224 			if (v->type == VEH_TRAIN) v->u.rail.days_since_order_progr = 0;
       
  2225 			v->cur_order_index++;
  2185 			v->cur_order_index++;
  2226 		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) {
  2186 		} else if (HASBIT(t.flags, OFB_HALT_IN_DEPOT)) {
  2227 			/* Force depot visit */
  2187 			/* Force depot visit */
  2228 			v->vehstatus |= VS_STOPPED;
  2188 			v->vehstatus |= VS_STOPPED;
  2229 			if (v->owner == _local_player) {
  2189 			if (v->owner == _local_player) {
  2492 							if (cargo_type == CT_PASSENGERS || cargo_type == CT_MAIL || cargo_type == CT_VALUABLES) {
  2452 							if (cargo_type == CT_PASSENGERS || cargo_type == CT_MAIL || cargo_type == CT_VALUABLES) {
  2493 								if (parent_engine_type == INVALID_ENGINE) {
  2453 								if (parent_engine_type == INVALID_ENGINE) {
  2494 									scheme = LS_PASSENGER_WAGON_STEAM;
  2454 									scheme = LS_PASSENGER_WAGON_STEAM;
  2495 								} else {
  2455 								} else {
  2496 									switch (RailVehInfo(parent_engine_type)->engclass) {
  2456 									switch (RailVehInfo(parent_engine_type)->engclass) {
  2497 										case 0: scheme = LS_PASSENGER_WAGON_STEAM; break;
  2457 										default: NOT_REACHED();
  2498 										case 1: scheme = LS_PASSENGER_WAGON_DIESEL; break;
  2458 										case EC_STEAM:    scheme = LS_PASSENGER_WAGON_STEAM;    break;
  2499 										case 2: scheme = LS_PASSENGER_WAGON_ELECTRIC; break;
  2459 										case EC_DIESEL:   scheme = LS_PASSENGER_WAGON_DIESEL;   break;
       
  2460 										case EC_ELECTRIC: scheme = LS_PASSENGER_WAGON_ELECTRIC; break;
  2500 									}
  2461 									}
  2501 								}
  2462 								}
  2502 							} else {
  2463 							} else {
  2503 								scheme = LS_FREIGHT_WAGON;
  2464 								scheme = LS_FREIGHT_WAGON;
  2504 							}
  2465 							}
  2505 						} else {
  2466 						} else {
  2506 							bool is_mu = HASBIT(_engine_info[engine_type].misc_flags, EF_RAIL_IS_MU);
  2467 							bool is_mu = HASBIT(_engine_info[engine_type].misc_flags, EF_RAIL_IS_MU);
  2507 
  2468 
  2508 							switch (rvi->engclass) {
  2469 							switch (rvi->engclass) {
  2509 								case 0: scheme = LS_STEAM; break;
  2470 								default: NOT_REACHED();
  2510 								case 1: scheme = is_mu ? LS_DMU : LS_DIESEL; break;
  2471 								case EC_STEAM:    scheme = LS_STEAM; break;
  2511 								case 2: scheme = is_mu ? LS_EMU : LS_ELECTRIC; break;
  2472 								case EC_DIESEL:   scheme = is_mu ? LS_DMU : LS_DIESEL;   break;
       
  2473 								case EC_ELECTRIC: scheme = is_mu ? LS_EMU : LS_ELECTRIC; break;
  2512 							}
  2474 							}
  2513 						}
  2475 						}
  2514 						break;
  2476 						break;
  2515 					}
  2477 					}
  2516 
  2478 
  2619 	SLE_CONDVAR(Vehicle, y_pos,                SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
  2581 	SLE_CONDVAR(Vehicle, y_pos,                SLE_FILE_U16 | SLE_VAR_U32,  0, 5),
  2620 	SLE_CONDVAR(Vehicle, y_pos,                SLE_UINT32,                  6, SL_MAX_VERSION),
  2582 	SLE_CONDVAR(Vehicle, y_pos,                SLE_UINT32,                  6, SL_MAX_VERSION),
  2621 	    SLE_VAR(Vehicle, z_pos,                SLE_UINT8),
  2583 	    SLE_VAR(Vehicle, z_pos,                SLE_UINT8),
  2622 	    SLE_VAR(Vehicle, direction,            SLE_UINT8),
  2584 	    SLE_VAR(Vehicle, direction,            SLE_UINT8),
  2623 
  2585 
  2624 	    SLE_VAR(Vehicle, cur_image,            SLE_UINT16),
  2586 	SLE_CONDNULL(2,                                                         0, 57),
  2625 	    SLE_VAR(Vehicle, spritenum,            SLE_UINT8),
  2587 	    SLE_VAR(Vehicle, spritenum,            SLE_UINT8),
  2626 	    SLE_VAR(Vehicle, sprite_width,         SLE_UINT8),
  2588 	SLE_CONDNULL(5,                                                         0, 57),
  2627 	    SLE_VAR(Vehicle, sprite_height,        SLE_UINT8),
       
  2628 	    SLE_VAR(Vehicle, z_height,             SLE_UINT8),
       
  2629 	    SLE_VAR(Vehicle, x_offs,               SLE_INT8),
       
  2630 	    SLE_VAR(Vehicle, y_offs,               SLE_INT8),
       
  2631 	    SLE_VAR(Vehicle, engine_type,          SLE_UINT16),
  2589 	    SLE_VAR(Vehicle, engine_type,          SLE_UINT16),
  2632 
  2590 
  2633 	    SLE_VAR(Vehicle, max_speed,            SLE_UINT16),
  2591 	    SLE_VAR(Vehicle, max_speed,            SLE_UINT16),
  2634 	    SLE_VAR(Vehicle, cur_speed,            SLE_UINT16),
  2592 	    SLE_VAR(Vehicle, cur_speed,            SLE_UINT16),
  2635 	    SLE_VAR(Vehicle, subspeed,             SLE_UINT8),
  2593 	    SLE_VAR(Vehicle, subspeed,             SLE_UINT8),
  2656 	    SLE_VAR(Vehicle, num_orders,           SLE_UINT8),
  2614 	    SLE_VAR(Vehicle, num_orders,           SLE_UINT8),
  2657 
  2615 
  2658 	/* This next line is for version 4 and prior compatibility.. it temporarily reads
  2616 	/* This next line is for version 4 and prior compatibility.. it temporarily reads
  2659 	    type and flags (which were both 4 bits) into type. Later on this is
  2617 	    type and flags (which were both 4 bits) into type. Later on this is
  2660 	    converted correctly */
  2618 	    converted correctly */
  2661 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, type), SLE_UINT8,                 0, 4),
  2619 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type), SLE_UINT8,                 0, 4),
  2662 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
  2620 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
  2663 
  2621 
  2664 	/* Orders for version 5 and on */
  2622 	/* Orders for version 5 and on */
  2665 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, type),  SLE_UINT8,  5, SL_MAX_VERSION),
  2623 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, type),  SLE_UINT8,  5, SL_MAX_VERSION),
  2666 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, flags), SLE_UINT8,  5, SL_MAX_VERSION),
  2624 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, flags), SLE_UINT8,  5, SL_MAX_VERSION),
  2667 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, dest),  SLE_UINT16, 5, SL_MAX_VERSION),
  2625 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest),  SLE_UINT16, 5, SL_MAX_VERSION),
  2668 
  2626 
  2669 	/* Refit in current order */
  2627 	/* Refit in current order */
  2670 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, refit_cargo),    SLE_UINT8, 36, SL_MAX_VERSION),
  2628 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_cargo),    SLE_UINT8, 36, SL_MAX_VERSION),
  2671 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, refit_subtype),  SLE_UINT8, 36, SL_MAX_VERSION),
  2629 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, refit_subtype),  SLE_UINT8, 36, SL_MAX_VERSION),
  2672 
  2630 
  2673 	    SLE_REF(Vehicle, orders,               REF_ORDER),
  2631 	    SLE_REF(Vehicle, orders,               REF_ORDER),
  2674 
  2632 
  2675 	SLE_CONDVAR(Vehicle, age,                  SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
  2633 	SLE_CONDVAR(Vehicle, age,                  SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
  2676 	SLE_CONDVAR(Vehicle, age,                  SLE_INT32,                  31, SL_MAX_VERSION),
  2634 	SLE_CONDVAR(Vehicle, age,                  SLE_INT32,                  31, SL_MAX_VERSION),
  2713 
  2671 
  2714 
  2672 
  2715 static const SaveLoad _train_desc[] = {
  2673 static const SaveLoad _train_desc[] = {
  2716 	SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN, 0), // Train type. VEH_TRAIN in mem, 0 in file.
  2674 	SLE_WRITEBYTE(Vehicle, type, VEH_TRAIN, 0), // Train type. VEH_TRAIN in mem, 0 in file.
  2717 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2675 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2718 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRail, crash_anim_pos),         SLE_UINT16),
  2676 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, crash_anim_pos),         SLE_UINT16),
  2719 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRail, force_proceed),          SLE_UINT8),
  2677 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, force_proceed),          SLE_UINT8),
  2720 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRail, railtype),               SLE_UINT8),
  2678 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, railtype),               SLE_UINT8),
  2721 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRail, track),                  SLE_UINT8),
  2679 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, track),                  SLE_UINT8),
  2722 
  2680 
  2723 	SLE_CONDVARX(offsetof(Vehicle, u) + offsetof(VehicleRail, flags),                  SLE_UINT8,  2, SL_MAX_VERSION),
  2681 	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRail, flags),                  SLE_UINT8,  2, SL_MAX_VERSION),
  2724 	SLE_CONDVARX(offsetof(Vehicle, u) + offsetof(VehicleRail, days_since_order_progr), SLE_UINT16, 2, SL_MAX_VERSION),
  2682 	SLE_CONDNULL(2, 2, 59),
  2725 
  2683 
  2726 	SLE_CONDNULL(2, 2, 19),
  2684 	SLE_CONDNULL(2, 2, 19),
  2727 	/* reserve extra space in savegame here. (currently 11 bytes) */
  2685 	/* reserve extra space in savegame here. (currently 11 bytes) */
  2728 	SLE_CONDNULL(11, 2, SL_MAX_VERSION),
  2686 	SLE_CONDNULL(11, 2, SL_MAX_VERSION),
  2729 
  2687 
  2731 };
  2689 };
  2732 
  2690 
  2733 static const SaveLoad _roadveh_desc[] = {
  2691 static const SaveLoad _roadveh_desc[] = {
  2734 	SLE_WRITEBYTE(Vehicle, type, VEH_ROAD, 1), // Road type. VEH_ROAD in mem, 1 in file.
  2692 	SLE_WRITEBYTE(Vehicle, type, VEH_ROAD, 1), // Road type. VEH_ROAD in mem, 1 in file.
  2735 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2693 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2736 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, state),          SLE_UINT8),
  2694 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, state),          SLE_UINT8),
  2737 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, frame),          SLE_UINT8),
  2695 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, frame),          SLE_UINT8),
  2738 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, blocked_ctr),    SLE_UINT16),
  2696 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, blocked_ctr),    SLE_UINT16),
  2739 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, overtaking),     SLE_UINT8),
  2697 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking),     SLE_UINT8),
  2740 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, overtaking_ctr), SLE_UINT8),
  2698 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, overtaking_ctr), SLE_UINT8),
  2741 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, crashed_ctr),    SLE_UINT16),
  2699 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, crashed_ctr),    SLE_UINT16),
  2742 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, reverse_ctr),    SLE_UINT8),
  2700 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, reverse_ctr),    SLE_UINT8),
  2743 
  2701 
  2744 	SLE_CONDREFX(offsetof(Vehicle, u) + offsetof(VehicleRoad, slot),     REF_ROADSTOPS, 6, SL_MAX_VERSION),
  2702 	SLE_CONDREFX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot),     REF_ROADSTOPS, 6, SL_MAX_VERSION),
  2745 	SLE_CONDNULL(1,                                                                     6, SL_MAX_VERSION),
  2703 	SLE_CONDNULL(1,                                                                     6, SL_MAX_VERSION),
  2746 	SLE_CONDVARX(offsetof(Vehicle, u) + offsetof(VehicleRoad, slot_age), SLE_UINT8,     6, SL_MAX_VERSION),
  2704 	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleRoad, slot_age), SLE_UINT8,     6, SL_MAX_VERSION),
  2747 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2705 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2748 	SLE_CONDNULL(16,                                                                    2, SL_MAX_VERSION),
  2706 	SLE_CONDNULL(16,                                                                    2, SL_MAX_VERSION),
  2749 
  2707 
  2750 	SLE_END()
  2708 	SLE_END()
  2751 };
  2709 };
  2752 
  2710 
  2753 static const SaveLoad _ship_desc[] = {
  2711 static const SaveLoad _ship_desc[] = {
  2754 	SLE_WRITEBYTE(Vehicle, type, VEH_SHIP, 2), // Ship type. VEH_SHIP in mem, 2 in file.
  2712 	SLE_WRITEBYTE(Vehicle, type, VEH_SHIP, 2), // Ship type. VEH_SHIP in mem, 2 in file.
  2755 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2713 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2756 	SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleShip, state), SLE_UINT8),
  2714 	SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleShip, state), SLE_UINT8),
  2757 
  2715 
  2758 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2716 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2759 	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
  2717 	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
  2760 
  2718 
  2761 	SLE_END()
  2719 	SLE_END()
  2762 };
  2720 };
  2763 
  2721 
  2764 static const SaveLoad _aircraft_desc[] = {
  2722 static const SaveLoad _aircraft_desc[] = {
  2765 	SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT, 3), // Aircraft type. VEH_AIRCRAFT in mem, 3 in file.
  2723 	SLE_WRITEBYTE(Vehicle, type, VEH_AIRCRAFT, 3), // Aircraft type. VEH_AIRCRAFT in mem, 3 in file.
  2766 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2724 	SLE_INCLUDEX(0, INC_VEHICLE_COMMON),
  2767 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleAir, crashed_counter), SLE_UINT16),
  2725 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, crashed_counter), SLE_UINT16),
  2768 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleAir, pos),             SLE_UINT8),
  2726 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, pos),             SLE_UINT8),
  2769 
  2727 
  2770 	SLE_CONDVARX(offsetof(Vehicle, u) + offsetof(VehicleAir, targetairport),   SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
  2728 	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport),   SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
  2771 	SLE_CONDVARX(offsetof(Vehicle, u) + offsetof(VehicleAir, targetairport),   SLE_UINT16,                5, SL_MAX_VERSION),
  2729 	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, targetairport),   SLE_UINT16,                5, SL_MAX_VERSION),
  2772 
  2730 
  2773 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleAir, state),           SLE_UINT8),
  2731 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, state),           SLE_UINT8),
  2774 
  2732 
  2775 	SLE_CONDVARX(offsetof(Vehicle, u) + offsetof(VehicleAir, previous_pos),    SLE_UINT8,                 2, SL_MAX_VERSION),
  2733 	SLE_CONDVARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleAir, previous_pos),    SLE_UINT8,                 2, SL_MAX_VERSION),
  2776 
  2734 
  2777 	/* reserve extra space in savegame here. (currently 15 bytes) */
  2735 	/* reserve extra space in savegame here. (currently 15 bytes) */
  2778 	SLE_CONDNULL(15,                                                                                      2, SL_MAX_VERSION),
  2736 	SLE_CONDNULL(15,                                                                                      2, SL_MAX_VERSION),
  2779 
  2737 
  2780 	SLE_END()
  2738 	SLE_END()
  2793 	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
  2751 	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32, 0, 5),
  2794 	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                  6, SL_MAX_VERSION),
  2752 	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                  6, SL_MAX_VERSION),
  2795 	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
  2753 	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
  2796 
  2754 
  2797 	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
  2755 	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
  2798 	    SLE_VAR(Vehicle, sprite_width,  SLE_UINT8),
  2756 	SLE_CONDNULL(5,                                                 0, 57),
  2799 	    SLE_VAR(Vehicle, sprite_height, SLE_UINT8),
       
  2800 	    SLE_VAR(Vehicle, z_height,      SLE_UINT8),
       
  2801 	    SLE_VAR(Vehicle, x_offs,        SLE_INT8),
       
  2802 	    SLE_VAR(Vehicle, y_offs,        SLE_INT8),
       
  2803 	    SLE_VAR(Vehicle, progress,      SLE_UINT8),
  2757 	    SLE_VAR(Vehicle, progress,      SLE_UINT8),
  2804 	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
  2758 	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
  2805 
  2759 
  2806 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleSpecial, unk0), SLE_UINT16),
  2760 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleSpecial, unk0), SLE_UINT16),
  2807 	    SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleSpecial, unk2), SLE_UINT8),
  2761 	    SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleSpecial, unk2), SLE_UINT8),
  2808 
  2762 
  2809 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2763 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2810 	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
  2764 	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
  2811 
  2765 
  2812 	SLE_END()
  2766 	SLE_END()
  2828 	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32,  0, 5),
  2782 	SLE_CONDVAR(Vehicle, y_pos,         SLE_FILE_I16 | SLE_VAR_I32,  0, 5),
  2829 	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                   6, SL_MAX_VERSION),
  2783 	SLE_CONDVAR(Vehicle, y_pos,         SLE_INT32,                   6, SL_MAX_VERSION),
  2830 	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
  2784 	    SLE_VAR(Vehicle, z_pos,         SLE_UINT8),
  2831 	    SLE_VAR(Vehicle, direction,     SLE_UINT8),
  2785 	    SLE_VAR(Vehicle, direction,     SLE_UINT8),
  2832 
  2786 
  2833 	    SLE_VAR(Vehicle, x_offs,        SLE_INT8),
  2787 	SLE_CONDNULL(5,                                                  0, 57),
  2834 	    SLE_VAR(Vehicle, y_offs,        SLE_INT8),
       
  2835 	    SLE_VAR(Vehicle, sprite_width,  SLE_UINT8),
       
  2836 	    SLE_VAR(Vehicle, sprite_height, SLE_UINT8),
       
  2837 	    SLE_VAR(Vehicle, z_height,      SLE_UINT8),
       
  2838 	    SLE_VAR(Vehicle, owner,         SLE_UINT8),
  2788 	    SLE_VAR(Vehicle, owner,         SLE_UINT8),
  2839 	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
  2789 	    SLE_VAR(Vehicle, vehstatus,     SLE_UINT8),
  2840 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
  2790 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_FILE_U8 | SLE_VAR_U16, 0, 4),
  2841 	SLE_CONDVARX(offsetof(Vehicle, current_order) + offsetof(Order, dest), SLE_UINT16,                5, SL_MAX_VERSION),
  2791 	SLE_CONDVARX(cpp_offsetof(Vehicle, current_order) + cpp_offsetof(Order, dest), SLE_UINT16,                5, SL_MAX_VERSION),
  2842 
  2792 
  2843 	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
  2793 	    SLE_VAR(Vehicle, cur_image,     SLE_UINT16),
  2844 	SLE_CONDVAR(Vehicle, age,           SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
  2794 	SLE_CONDVAR(Vehicle, age,           SLE_FILE_U16 | SLE_VAR_I32,  0, 30),
  2845 	SLE_CONDVAR(Vehicle, age,           SLE_INT32,                  31, SL_MAX_VERSION),
  2795 	SLE_CONDVAR(Vehicle, age,           SLE_INT32,                  31, SL_MAX_VERSION),
  2846 	    SLE_VAR(Vehicle, tick_counter,  SLE_UINT8),
  2796 	    SLE_VAR(Vehicle, tick_counter,  SLE_UINT8),
  2847 
  2797 
  2848 	   SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleDisaster, image_override), SLE_UINT16),
  2798 	   SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, image_override), SLE_UINT16),
  2849 	   SLE_VARX(offsetof(Vehicle, u) + offsetof(VehicleDisaster, unk2),           SLE_UINT16),
  2799 	   SLE_VARX(cpp_offsetof(Vehicle, u) + cpp_offsetof(VehicleDisaster, unk2),           SLE_UINT16),
  2850 
  2800 
  2851 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2801 	/* reserve extra space in savegame here. (currently 16 bytes) */
  2852 	SLE_CONDNULL(16,                                                 2, SL_MAX_VERSION),
  2802 	SLE_CONDNULL(16,                                                 2, SL_MAX_VERSION),
  2853 
  2803 
  2854 	SLE_END()
  2804 	SLE_END()
  2887 		if (!AddBlockIfNeeded(&_Vehicle_pool, index))
  2837 		if (!AddBlockIfNeeded(&_Vehicle_pool, index))
  2888 			error("Vehicles: failed loading savegame: too many vehicles");
  2838 			error("Vehicles: failed loading savegame: too many vehicles");
  2889 
  2839 
  2890 		v = GetVehicle(index);
  2840 		v = GetVehicle(index);
  2891 		SlObject(v, (SaveLoad*)_veh_descs[SlReadByte()]);
  2841 		SlObject(v, (SaveLoad*)_veh_descs[SlReadByte()]);
       
  2842 
       
  2843 		switch (v->type) {
       
  2844 			case VEH_TRAIN:    v = new (v) Train();           break;
       
  2845 			case VEH_ROAD:     v = new (v) RoadVehicle();     break;
       
  2846 			case VEH_SHIP:     v = new (v) Ship();            break;
       
  2847 			case VEH_AIRCRAFT: v = new (v) Aircraft();        break;
       
  2848 			case VEH_SPECIAL:  v = new (v) SpecialVehicle();  break;
       
  2849 			case VEH_DISASTER: v = new (v) DisasterVehicle(); break;
       
  2850 			case VEH_INVALID:  v = new (v) InvalidVehicle();  break;
       
  2851 		}
  2892 
  2852 
  2893 		/* Old savegames used 'last_station_visited = 0xFF' */
  2853 		/* Old savegames used 'last_station_visited = 0xFF' */
  2894 		if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
  2854 		if (CheckSavegameVersion(5) && v->last_station_visited == 0xFF)
  2895 			v->last_station_visited = INVALID_STATION;
  2855 			v->last_station_visited = INVALID_STATION;
  2896 
  2856 
  2925 };
  2885 };
  2926 
  2886 
  2927 void Vehicle::BeginLoading()
  2887 void Vehicle::BeginLoading()
  2928 {
  2888 {
  2929 	assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP);
  2889 	assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP);
       
  2890 
       
  2891 	if (this->current_order.type == OT_GOTO_STATION &&
       
  2892 			this->current_order.dest == this->last_station_visited) {
       
  2893 		/* Arriving at the ordered station.
       
  2894 		 * Keep the load/unload flags, as we (obviously) still need them. */
       
  2895 		this->current_order.flags &= OF_FULL_LOAD | OF_UNLOAD | OF_TRANSFER;
       
  2896 
       
  2897 		/* Furthermore add the Non Stop flag to mark that this station
       
  2898 		 * is the actual destination of the vehicle, which is (for example)
       
  2899 		 * necessary to be known for HandleTrainLoading to determine
       
  2900 		 * whether the train is lost or not; not marking a train lost
       
  2901 		 * that arrives at random stations is bad. */
       
  2902 		this->current_order.flags |= OF_NON_STOP;
       
  2903 	} else {
       
  2904 		/* This is just an unordered intermediate stop */
       
  2905 		this->current_order.flags = 0;
       
  2906 	}
       
  2907 
  2930 	current_order.type = OT_LOADING;
  2908 	current_order.type = OT_LOADING;
  2931 	GetStation(this->last_station_visited)->loading_vehicles.push_back(this);
  2909 	GetStation(this->last_station_visited)->loading_vehicles.push_back(this);
       
  2910 
       
  2911 	SET_EXPENSES_TYPE(this->GetExpenseType(true));
       
  2912 	VehiclePayment(this);
       
  2913 
       
  2914 	InvalidateWindow(this->GetVehicleListWindowClass(), this->owner);
       
  2915 	InvalidateWindowWidget(WC_VEHICLE_VIEW, this->index, STATUS_BAR);
       
  2916 	InvalidateWindow(WC_VEHICLE_DETAILS, this->index);
       
  2917 	InvalidateWindow(WC_STATION_VIEW, this->last_station_visited);
       
  2918 
       
  2919 	GetStation(this->last_station_visited)->MarkTilesDirty();
       
  2920 	this->MarkDirty();
  2932 }
  2921 }
  2933 
  2922 
  2934 void Vehicle::LeaveStation()
  2923 void Vehicle::LeaveStation()
  2935 {
  2924 {
  2936 	assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP);
       
  2937 	assert(current_order.type == OT_LOADING);
  2925 	assert(current_order.type == OT_LOADING);
  2938 	current_order.type = OT_LEAVESTATION;
  2926 	current_order.type = OT_LEAVESTATION;
  2939 	current_order.flags = 0;
  2927 	current_order.flags = 0;
  2940 	GetStation(this->last_station_visited)->loading_vehicles.remove(this);
  2928 	GetStation(this->last_station_visited)->loading_vehicles.remove(this);
  2941 }
  2929 }
       
  2930 
       
  2931 
       
  2932 void Vehicle::HandleLoading(bool mode)
       
  2933 {
       
  2934 	switch (this->current_order.type) {
       
  2935 		case OT_LOADING: {
       
  2936 			/* Not the first call for this tick */
       
  2937 			if (mode) return;
       
  2938 
       
  2939 			/* We have not waited enough time till the next round of loading/unloading */
       
  2940 			if (--this->load_unload_time_rem) return;
       
  2941 
       
  2942 			/* Load/unload the vehicle; when it actually did something
       
  2943 			 * we do not leave the station. */
       
  2944 			if (LoadUnloadVehicle(this)) return;
       
  2945 
       
  2946 			this->PlayLeaveStationSound();
       
  2947 
       
  2948 			Order b = this->current_order;
       
  2949 			this->LeaveStation();
       
  2950 
       
  2951 			/* If this was not the final order, don't remove it from the list. */
       
  2952 			if (!(b.flags & OF_NON_STOP)) return;
       
  2953 			break;
       
  2954 		}
       
  2955 
       
  2956 		case OT_DUMMY: break;
       
  2957 
       
  2958 		default: return;
       
  2959 	}
       
  2960 
       
  2961 	this->cur_order_index++;
       
  2962 	InvalidateVehicleOrder(this);
       
  2963 }
       
  2964 
       
  2965 
       
  2966 void SpecialVehicle::UpdateDeltaXY(Direction direction)
       
  2967 {
       
  2968 	this->x_offs        = 0;
       
  2969 	this->y_offs        = 0;
       
  2970 	this->sprite_width  = 1;
       
  2971 	this->sprite_height = 1;
       
  2972 	this->z_height      = 1;
       
  2973 }