src/vehicle.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
    41 #include "newgrf_sound.h"
    41 #include "newgrf_sound.h"
    42 #include "helpers.hpp"
    42 #include "helpers.hpp"
    43 #include "group.h"
    43 #include "group.h"
    44 #include "economy.h"
    44 #include "economy.h"
    45 
    45 
    46 #define INVALID_COORD (-0x8000)
    46 #define INVALID_COORD (0x7fffffff)
    47 #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
    47 #define GEN_HASH(x, y) ((GB((y), 6, 6) << 6) + GB((x), 7, 6))
    48 
    48 
    49 
    49 
    50 /* Tables used in vehicle.h to find the right command for a certain vehicle type */
    50 /* Tables used in vehicle.h to find the right command for a certain vehicle type */
    51 const uint32 _veh_build_proc_table[] = {
    51 const uint32 _veh_build_proc_table[] = {
   392 	}
   392 	}
   393 
   393 
   394 	return true;
   394 	return true;
   395 }
   395 }
   396 
   396 
       
   397 /* Size of the hash, 6 = 64 x 64, 7 = 128 x 128. Larger sizes will (in theory) reduce hash
       
   398  * lookup times at the expense of memory usage. */
       
   399 const int HASH_BITS = 7;
       
   400 const int HASH_SIZE = 1 << HASH_BITS;
       
   401 const int HASH_MASK = HASH_SIZE - 1;
       
   402 const int TOTAL_HASH_SIZE = 1 << (HASH_BITS * 2);
       
   403 const int TOTAL_HASH_MASK = TOTAL_HASH_SIZE - 1;
       
   404 
       
   405 /* Resolution of the hash, 0 = 1*1 tile, 1 = 2*2 tiles, 2 = 4*4 tiles, etc.
       
   406  * Profiling results show that 0 is fastest. */
       
   407 const int HASH_RES = 0;
       
   408 
       
   409 static Vehicle *_new_vehicle_position_hash[TOTAL_HASH_SIZE];
       
   410 
       
   411 static void *VehicleFromHash(int xl, int yl, int xu, int yu, void *data, VehicleFromPosProc *proc)
       
   412 {
       
   413 	for (int y = yl; ; y = (y + (1 << HASH_BITS)) & (HASH_MASK << HASH_BITS)) {
       
   414 		for (int x = xl; ; x = (x + 1) & HASH_MASK) {
       
   415 			Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
       
   416 			for (; v != NULL; v = v->next_new_hash) {
       
   417 				void *a = proc(v, data);
       
   418 				if (a != NULL) return a;
       
   419 			}
       
   420 			if (x == xu) break;
       
   421 		}
       
   422 		if (y == yu) break;
       
   423 	}
       
   424 
       
   425 	return NULL;
       
   426 }
       
   427 
       
   428 
       
   429 void *VehicleFromPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
       
   430 {
       
   431 	const int COLL_DIST = 6;
       
   432 
       
   433 	/* Hash area to scan is from xl,yl to xu,yu */
       
   434 	int xl = GB((x - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
       
   435 	int xu = GB((x + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS);
       
   436 	int yl = GB((y - COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
       
   437 	int yu = GB((y + COLL_DIST) / TILE_SIZE, HASH_RES, HASH_BITS) << HASH_BITS;
       
   438 
       
   439 	return VehicleFromHash(xl, yl, xu, yu, data, proc);
       
   440 }
       
   441 
       
   442 
       
   443 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
       
   444 {
       
   445 	int x = GB(TileX(tile), HASH_RES, HASH_BITS);
       
   446 	int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS;
       
   447 
       
   448 	Vehicle *v = _new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
       
   449 	for (; v != NULL; v = v->next_new_hash) {
       
   450 		if (v->tile != tile) continue;
       
   451 
       
   452 		void *a = proc(v, data);
       
   453 		if (a != NULL) return a;
       
   454 	}
       
   455 
       
   456 	return NULL;
       
   457 }
       
   458 
       
   459 static void UpdateNewVehiclePosHash(Vehicle *v, bool remove)
       
   460 {
       
   461 	Vehicle **old_hash = v->old_new_hash;
       
   462 	Vehicle **new_hash;
       
   463 
       
   464 	if (remove) {
       
   465 		new_hash = NULL;
       
   466 	} else {
       
   467 		int x = GB(TileX(v->tile), HASH_RES, HASH_BITS);
       
   468 		int y = GB(TileY(v->tile), HASH_RES, HASH_BITS) << HASH_BITS;
       
   469 		new_hash = &_new_vehicle_position_hash[(x + y) & TOTAL_HASH_MASK];
       
   470 	}
       
   471 
       
   472 	if (old_hash == new_hash) return;
       
   473 
       
   474 	/* Remove from the old position in the hash table */
       
   475 	if (old_hash != NULL) {
       
   476 		Vehicle *last = NULL;
       
   477 		Vehicle *u = *old_hash;
       
   478 		while (u != v) {
       
   479 			last = u;
       
   480 			u = u->next_new_hash;
       
   481 			assert(u != NULL);
       
   482 		}
       
   483 
       
   484 		if (last == NULL) {
       
   485 			*old_hash = v->next_new_hash;
       
   486 		} else {
       
   487 			last->next_new_hash = v->next_new_hash;
       
   488 		}
       
   489 	}
       
   490 
       
   491 	/* Insert vehicle at beginning of the new position in the hash table */
       
   492 	if (new_hash != NULL) {
       
   493 		v->next_new_hash = *new_hash;
       
   494 		*new_hash = v;
       
   495 		assert(v != v->next_new_hash);
       
   496 	}
       
   497 
       
   498 	/* Remember current hash position */
       
   499 	v->old_new_hash = new_hash;
       
   500 }
   397 
   501 
   398 static Vehicle *_vehicle_position_hash[0x1000];
   502 static Vehicle *_vehicle_position_hash[0x1000];
   399 
   503 
   400 void *VehicleFromPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
       
   401 {
       
   402 	Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, 0);
       
   403 
       
   404 	/* The hash area to scan */
       
   405 	const int xl = GB(pt.x - 174, 7, 6);
       
   406 	const int xu = GB(pt.x + 104, 7, 6);
       
   407 	const int yl = GB(pt.y - 294, 6, 6) << 6;
       
   408 	const int yu = GB(pt.y +  56, 6, 6) << 6;
       
   409 
       
   410 	int x;
       
   411 	int y;
       
   412 
       
   413 	for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
       
   414 		for (x = xl;; x = (x + 1) & 0x3F) {
       
   415 			Vehicle *v = _vehicle_position_hash[(x + y) & 0xFFFF];
       
   416 
       
   417 			while (v != NULL) {
       
   418 				void* a = proc(v, data);
       
   419 
       
   420 				if (a != NULL) return a;
       
   421 				v = v->next_hash;
       
   422 			}
       
   423 
       
   424 			if (x == xu) break;
       
   425 		}
       
   426 
       
   427 		if (y == yu) break;
       
   428 	}
       
   429 	return NULL;
       
   430 }
       
   431 
       
   432 
       
   433 static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
   504 static void UpdateVehiclePosHash(Vehicle* v, int x, int y)
   434 {
   505 {
       
   506 	UpdateNewVehiclePosHash(v, x == INVALID_COORD);
       
   507 
   435 	Vehicle **old_hash, **new_hash;
   508 	Vehicle **old_hash, **new_hash;
   436 	int old_x = v->left_coord;
   509 	int old_x = v->left_coord;
   437 	int old_y = v->top_coord;
   510 	int old_y = v->top_coord;
   438 
   511 
   439 	new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)];
   512 	new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)];
   465 	}
   538 	}
   466 }
   539 }
   467 
   540 
   468 void ResetVehiclePosHash()
   541 void ResetVehiclePosHash()
   469 {
   542 {
       
   543 	Vehicle *v;
       
   544 	FOR_ALL_VEHICLES(v) { v->old_new_hash = NULL; }
   470 	memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
   545 	memset(_vehicle_position_hash, 0, sizeof(_vehicle_position_hash));
       
   546 	memset(_new_vehicle_position_hash, 0, sizeof(_new_vehicle_position_hash));
   471 }
   547 }
   472 
   548 
   473 void InitializeVehicles()
   549 void InitializeVehicles()
   474 {
   550 {
   475 	uint i;
   551 	uint i;
   611 		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
   687 		InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
   612 	}
   688 	}
   613 
   689 
   614 	UpdateVehiclePosHash(v, INVALID_COORD, 0);
   690 	UpdateVehiclePosHash(v, INVALID_COORD, 0);
   615 	v->next_hash = NULL;
   691 	v->next_hash = NULL;
       
   692 	v->next_new_hash = NULL;
   616 	if (IsPlayerBuildableVehicleType(v)) DeleteVehicleOrders(v);
   693 	if (IsPlayerBuildableVehicleType(v)) DeleteVehicleOrders(v);
   617 
   694 
   618 	/* Now remove any artic part. This will trigger an other
   695 	/* Now remove any artic part. This will trigger an other
   619 	 *  destroy vehicle, which on his turn can remove any
   696 	 *  destroy vehicle, which on his turn can remove any
   620 	 *  other artic parts. */
   697 	 *  other artic parts. */
   766 
   843 
   767 /** Learn the price of refitting a certain engine
   844 /** Learn the price of refitting a certain engine
   768 * @param engine_type Which engine to refit
   845 * @param engine_type Which engine to refit
   769 * @return Price for refitting
   846 * @return Price for refitting
   770 */
   847 */
   771 int32 GetRefitCost(EngineID engine_type)
   848 CommandCost GetRefitCost(EngineID engine_type)
   772 {
   849 {
   773 	int32 base_cost = 0;
   850 	CommandCost base_cost = 0;
   774 
   851 
   775 	switch (GetEngine(engine_type)->type) {
   852 	switch (GetEngine(engine_type)->type) {
   776 		case VEH_SHIP: base_cost = _eco->GetPrice(CEconomy::SHIP_BASE); break;
   853 		case VEH_SHIP: base_cost = _eco->GetPrice(CEconomy::SHIP_BASE); break;
   777 		case VEH_ROAD: base_cost = _eco->GetPrice(CEconomy::ROADVEH_BASE); break;
   854 		case VEH_ROAD: base_cost = _eco->GetPrice(CEconomy::ROADVEH_BASE); break;
   778 		case VEH_AIRCRAFT: base_cost = _eco->GetPrice(CEconomy::AIRCRAFT_BASE); break;
   855 		case VEH_AIRCRAFT: base_cost = _eco->GetPrice(CEconomy::AIRCRAFT_BASE); break;
   810 	const int r = dpi->left + dpi->width;
   887 	const int r = dpi->left + dpi->width;
   811 	const int t = dpi->top;
   888 	const int t = dpi->top;
   812 	const int b = dpi->top + dpi->height;
   889 	const int b = dpi->top + dpi->height;
   813 
   890 
   814 	/* The hash area to scan */
   891 	/* The hash area to scan */
   815 	const int xl = GB(l - 70, 7, 6);
   892 	int xl, xu, yl, yu;
   816 	const int xu = GB(r,      7, 6);
   893 
   817 	const int yl = GB(t - 70, 6, 6) << 6;
   894 	if (dpi->width + 70 < (1 << (7 + 6))) {
   818 	const int yu = GB(b,      6, 6) << 6;
   895 		xl = GB(l - 70, 7, 6);
   819 
   896 		xu = GB(r,      7, 6);
   820 	int x;
   897 	} else {
   821 	int y;
   898 		/* scan whole hash row */
   822 
   899 		xl = 0;
   823 	for (y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
   900 		xu = 0x3F;
   824 		for (x = xl;; x = (x + 1) & 0x3F) {
   901 	}
   825 			const Vehicle *v = _vehicle_position_hash[(x + y) & 0xFFFF];
   902 
       
   903 	if (dpi->height + 70 < (1 << (6 + 6))) {
       
   904 		yl = GB(t - 70, 6, 6) << 6;
       
   905 		yu = GB(b,      6, 6) << 6;
       
   906 	} else {
       
   907 		/* scan whole column */
       
   908 		yl = 0;
       
   909 		yu = 0x3F << 6;
       
   910 	}
       
   911 
       
   912 	for (int y = yl;; y = (y + (1 << 6)) & (0x3F << 6)) {
       
   913 		for (int x = xl;; x = (x + 1) & 0x3F) {
       
   914 			const Vehicle *v = _vehicle_position_hash[x + y]; // already masked & 0xFFF
   826 
   915 
   827 			while (v != NULL) {
   916 			while (v != NULL) {
   828 				if (!(v->vehstatus & VS_HIDDEN) &&
   917 				if (!(v->vehstatus & VS_HIDDEN) &&
   829 						l <= v->right_coord &&
   918 						l <= v->right_coord &&
   830 						t <= v->bottom_coord &&
   919 						t <= v->bottom_coord &&
  1588  *   - bit 0-4 Vehicle type
  1677  *   - bit 0-4 Vehicle type
  1589  *   - bit 5 false = start vehicles, true = stop vehicles
  1678  *   - bit 5 false = start vehicles, true = stop vehicles
  1590  *   - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
  1679  *   - bit 6 if set, then it's a vehicle list window, not a depot and Tile is ignored in this case
  1591  *   - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)
  1680  *   - bit 8-11 Vehicle List Window type (ignored unless bit 1 is set)
  1592  */
  1681  */
  1593 int32 CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1682 CommandCost CmdMassStartStopVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1594 {
  1683 {
  1595 	Vehicle **vl = NULL;
  1684 	Vehicle **vl = NULL;
  1596 	uint16 engine_list_length = 0;
  1685 	uint16 engine_list_length = 0;
  1597 	uint16 engine_count = 0;
  1686 	uint16 engine_count = 0;
  1598 	int32 return_value = CMD_ERROR;
  1687 	CommandCost return_value = CMD_ERROR;
  1599 	uint i;
  1688 	uint i;
  1600 	uint stop_command;
  1689 	uint stop_command;
  1601 	VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
  1690 	VehicleType vehicle_type = (VehicleType)GB(p2, 0, 5);
  1602 	bool start_stop = HASBIT(p2, 5);
  1691 	bool start_stop = HASBIT(p2, 5);
  1603 	bool vehicle_list_window = HASBIT(p2, 6);
  1692 	bool vehicle_list_window = HASBIT(p2, 6);
  1620 		BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, NULL, NULL, NULL);
  1709 		BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, NULL, NULL, NULL);
  1621 	}
  1710 	}
  1622 
  1711 
  1623 	for (i = 0; i < engine_count; i++) {
  1712 	for (i = 0; i < engine_count; i++) {
  1624 		const Vehicle *v = vl[i];
  1713 		const Vehicle *v = vl[i];
  1625 		int32 ret;
  1714 		CommandCost ret;
  1626 
  1715 
  1627 		if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
  1716 		if (!!(v->vehstatus & VS_STOPPED) != start_stop) continue;
  1628 
  1717 
  1629 		if (!vehicle_list_window) {
  1718 		if (!vehicle_list_window) {
  1630 			if (vehicle_type == VEH_TRAIN) {
  1719 			if (vehicle_type == VEH_TRAIN) {
  1634 			}
  1723 			}
  1635 		}
  1724 		}
  1636 
  1725 
  1637 		ret = DoCommand(tile, v->index, 0, flags, stop_command);
  1726 		ret = DoCommand(tile, v->index, 0, flags, stop_command);
  1638 
  1727 
  1639 		if (!CmdFailed(ret)) {
  1728 		if (CmdSucceeded(ret)) {
  1640 			return_value = 0;
  1729 			return_value = 0;
  1641 			/* We know that the command is valid for at least one vehicle.
  1730 			/* We know that the command is valid for at least one vehicle.
  1642 			 * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
  1731 			 * If we haven't set DC_EXEC, then there is no point in continueing because it will be valid */
  1643 			if (!(flags & DC_EXEC)) break;
  1732 			if (!(flags & DC_EXEC)) break;
  1644 		}
  1733 		}
  1652  * @param tile Tile of the depot where the depot is
  1741  * @param tile Tile of the depot where the depot is
  1653  * @param flags type of operation
  1742  * @param flags type of operation
  1654  * @param p1 Vehicle type
  1743  * @param p1 Vehicle type
  1655  * @param p2 unused
  1744  * @param p2 unused
  1656  */
  1745  */
  1657 int32 CmdDepotSellAllVehicles(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1746 CommandCost CmdDepotSellAllVehicles(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1658 {
  1747 {
  1659 	Vehicle **engines = NULL;
  1748 	Vehicle **engines = NULL;
  1660 	Vehicle **wagons = NULL;
  1749 	Vehicle **wagons = NULL;
  1661 	uint16 engine_list_length = 0;
  1750 	uint16 engine_list_length = 0;
  1662 	uint16 engine_count = 0;
  1751 	uint16 engine_count = 0;
  1663 	uint16 wagon_list_length = 0;
  1752 	uint16 wagon_list_length = 0;
  1664 	uint16 wagon_count = 0;
  1753 	uint16 wagon_count = 0;
  1665 
  1754 
  1666 	int32 cost = 0;
  1755 	CommandCost cost = 0;
  1667 	uint i, sell_command, total_number_vehicles;
  1756 	uint i, sell_command, total_number_vehicles;
  1668 	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
  1757 	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
  1669 
  1758 
  1670 	switch (vehicle_type) {
  1759 	switch (vehicle_type) {
  1671 		case VEH_TRAIN:    sell_command = CMD_SELL_RAIL_WAGON; break;
  1760 		case VEH_TRAIN:    sell_command = CMD_SELL_RAIL_WAGON; break;
  1680 						                      &wagons,  &wagon_list_length,  &wagon_count);
  1769 						                      &wagons,  &wagon_list_length,  &wagon_count);
  1681 
  1770 
  1682 	total_number_vehicles = engine_count + wagon_count;
  1771 	total_number_vehicles = engine_count + wagon_count;
  1683 	for (i = 0; i < total_number_vehicles; i++) {
  1772 	for (i = 0; i < total_number_vehicles; i++) {
  1684 		const Vehicle *v;
  1773 		const Vehicle *v;
  1685 		int32 ret;
  1774 		CommandCost ret;
  1686 
  1775 
  1687 		if (i < engine_count) {
  1776 		if (i < engine_count) {
  1688 			v = engines[i];
  1777 			v = engines[i];
  1689 		} else {
  1778 		} else {
  1690 			v = wagons[i - engine_count];
  1779 			v = wagons[i - engine_count];
  1691 		}
  1780 		}
  1692 
  1781 
  1693 		ret = DoCommand(tile, v->index, 1, flags, sell_command);
  1782 		ret = DoCommand(tile, v->index, 1, flags, sell_command);
  1694 
  1783 
  1695 		if (!CmdFailed(ret)) cost += ret;
  1784 		if (CmdSucceeded(ret)) cost += ret;
  1696 	}
  1785 	}
  1697 
  1786 
  1698 	free(engines);
  1787 	free(engines);
  1699 	free(wagons);
  1788 	free(wagons);
  1700 	if (cost == 0) return CMD_ERROR; // no vehicles to sell
  1789 	if (cost == 0) return CMD_ERROR; // no vehicles to sell
  1705  * @param tile Tile of the depot where the vehicles are
  1794  * @param tile Tile of the depot where the vehicles are
  1706  * @param flags type of operation
  1795  * @param flags type of operation
  1707  * @param p1 Type of vehicle
  1796  * @param p1 Type of vehicle
  1708  * @param p2 Unused
  1797  * @param p2 Unused
  1709  */
  1798  */
  1710 int32 CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1799 CommandCost CmdDepotMassAutoReplace(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1711 {
  1800 {
  1712 	Vehicle **vl = NULL;
  1801 	Vehicle **vl = NULL;
  1713 	uint16 engine_list_length = 0;
  1802 	uint16 engine_list_length = 0;
  1714 	uint16 engine_count = 0;
  1803 	uint16 engine_count = 0;
  1715 	uint i, x = 0, y = 0, z = 0;
  1804 	uint i, x = 0, y = 0, z = 0;
  1716 	int32 cost = 0;
  1805 	CommandCost cost = 0;
  1717 	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
  1806 	VehicleType vehicle_type = (VehicleType)GB(p1, 0, 8);
  1718 
  1807 
  1719 	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
  1808 	if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
  1720 
  1809 
  1721 	/* Get the list of vehicles in the depot */
  1810 	/* Get the list of vehicles in the depot */
  1723 
  1812 
  1724 
  1813 
  1725 	for (i = 0; i < engine_count; i++) {
  1814 	for (i = 0; i < engine_count; i++) {
  1726 		Vehicle *v = vl[i];
  1815 		Vehicle *v = vl[i];
  1727 		bool stopped = !(v->vehstatus & VS_STOPPED);
  1816 		bool stopped = !(v->vehstatus & VS_STOPPED);
  1728 		int32 ret;
  1817 		CommandCost ret;
  1729 
  1818 
  1730 		/* Ensure that the vehicle completely in the depot */
  1819 		/* Ensure that the vehicle completely in the depot */
  1731 		if (!IsVehicleInDepot(v)) continue;
  1820 		if (!IsVehicleInDepot(v)) continue;
  1732 
  1821 
  1733 		x = v->x_pos;
  1822 		x = v->x_pos;
  1738 			v->vehstatus |= VS_STOPPED; // Stop the vehicle
  1827 			v->vehstatus |= VS_STOPPED; // Stop the vehicle
  1739 			v->leave_depot_instantly = true;
  1828 			v->leave_depot_instantly = true;
  1740 		}
  1829 		}
  1741 		ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false);
  1830 		ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false);
  1742 
  1831 
  1743 		if (!CmdFailed(ret)) {
  1832 		if (CmdSucceeded(ret)) {
  1744 			cost += ret;
  1833 			cost += ret;
  1745 			if (!(flags & DC_EXEC)) break;
  1834 			if (!(flags & DC_EXEC)) break;
  1746 			/* There is a problem with autoreplace and newgrf
  1835 			/* There is a problem with autoreplace and newgrf
  1747 			 * It's impossible to tell the length of a train after it's being replaced before it's actually done
  1836 			 * It's impossible to tell the length of a train after it's being replaced before it's actually done
  1748 			 * Because of this, we can't estimate costs due to wagon removal and we will have to always return 0 and pay manually
  1837 			 * Because of this, we can't estimate costs due to wagon removal and we will have to always return 0 and pay manually
  1771  * @param tile tile of the depot where the cloned vehicle is build
  1860  * @param tile tile of the depot where the cloned vehicle is build
  1772  * @param flags type of operation
  1861  * @param flags type of operation
  1773  * @param p1 the original vehicle's index
  1862  * @param p1 the original vehicle's index
  1774  * @param p2 1 = shared orders, else copied orders
  1863  * @param p2 1 = shared orders, else copied orders
  1775  */
  1864  */
  1776 int32 CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1865 CommandCost CmdCloneVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  1777 {
  1866 {
  1778 	Vehicle *v_front, *v;
  1867 	Vehicle *v_front, *v;
  1779 	Vehicle *w_front, *w, *w_rear;
  1868 	Vehicle *w_front, *w, *w_rear;
  1780 	int32 cost, total_cost = 0;
  1869 	CommandCost cost, total_cost = 0;
  1781 	uint32 build_argument = 2;
  1870 	uint32 build_argument = 2;
  1782 
  1871 
  1783 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
  1872 	if (!IsValidVehicleID(p1)) return CMD_ERROR;
  1784 	v = GetVehicle(p1);
  1873 	v = GetVehicle(p1);
  1785 	v_front = v;
  1874 	v_front = v;
  1835 			}
  1924 			}
  1836 
  1925 
  1837 			if (v->type == VEH_TRAIN && !IsFrontEngine(v)) {
  1926 			if (v->type == VEH_TRAIN && !IsFrontEngine(v)) {
  1838 				/* this s a train car
  1927 				/* this s a train car
  1839 				 * add this unit to the end of the train */
  1928 				 * add this unit to the end of the train */
  1840 				int32 result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
  1929 				CommandCost result = DoCommand(0, (w_rear->index << 16) | w->index, 1, flags, CMD_MOVE_RAIL_VEHICLE);
  1841 				if (CmdFailed(result)) {
  1930 				if (CmdFailed(result)) {
  1842 					/* The train can't be joined to make the same consist as the original.
  1931 					/* The train can't be joined to make the same consist as the original.
  1843 					 * Sell what we already made (clean up) and return an error.           */
  1932 					 * Sell what we already made (clean up) and return an error.           */
  1844 					DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
  1933 					DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
  1845 					DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
  1934 					DoCommand(w_front->tile, w->index,       1, flags, GetCmdSellVeh(w));
  1881 			if (flags & DC_EXEC) {
  1970 			if (flags & DC_EXEC) {
  1882 				assert(w != NULL);
  1971 				assert(w != NULL);
  1883 
  1972 
  1884 				if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_type) {
  1973 				if (w->cargo_type != v->cargo_type || w->cargo_subtype != v->cargo_type) {
  1885 					cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
  1974 					cost = DoCommand(0, w->index, v->cargo_type | (v->cargo_subtype << 8) | 1U << 16 , flags, GetCmdRefitVeh(v));
  1886 					if (!CmdFailed(cost)) total_cost += cost;
  1975 					if (CmdSucceeded(cost)) total_cost += cost;
  1887 				}
  1976 				}
  1888 
  1977 
  1889 				if (w->type == VEH_TRAIN && EngineHasArticPart(w)) {
  1978 				if (w->type == VEH_TRAIN && EngineHasArticPart(w)) {
  1890 					w = GetNextArticPart(w);
  1979 					w = GetNextArticPart(w);
  1891 				} else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) {
  1980 				} else if (w->type == VEH_ROAD && RoadVehHasArticPart(w)) {
  2132  * @param service should the vehicles only get service in the depots
  2221  * @param service should the vehicles only get service in the depots
  2133  * @param owner PlayerID of owner of the vehicles to send
  2222  * @param owner PlayerID of owner of the vehicles to send
  2134  * @param vlw_flag tells what kind of list requested the goto depot
  2223  * @param vlw_flag tells what kind of list requested the goto depot
  2135  * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
  2224  * @return 0 for success and CMD_ERROR if no vehicle is able to go to depot
  2136  */
  2225  */
  2137 int32 SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id)
  2226 CommandCost SendAllVehiclesToDepot(VehicleType type, uint32 flags, bool service, PlayerID owner, uint16 vlw_flag, uint32 id)
  2138 {
  2227 {
  2139 	const Vehicle **sort_list = NULL;
  2228 	const Vehicle **sort_list = NULL;
  2140 	uint n, i;
  2229 	uint n, i;
  2141 	uint16 array_length = 0;
  2230 	uint16 array_length = 0;
  2142 
  2231 
  2143 	n = GenerateVehicleSortList(&sort_list, &array_length, type, owner, id, vlw_flag);
  2232 	n = GenerateVehicleSortList(&sort_list, &array_length, type, owner, id, vlw_flag);
  2144 
  2233 
  2145 	/* Send all the vehicles to a depot */
  2234 	/* Send all the vehicles to a depot */
  2146 	for (i = 0; i < n; i++) {
  2235 	for (i = 0; i < n; i++) {
  2147 		const Vehicle *v = sort_list[i];
  2236 		const Vehicle *v = sort_list[i];
  2148 		int32 ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
  2237 		CommandCost ret = DoCommand(v->tile, v->index, (service ? 1 : 0) | DEPOT_DONT_CANCEL, flags, GetCmdSendToDepot(type));
  2149 
  2238 
  2150 		/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
  2239 		/* Return 0 if DC_EXEC is not set this is a valid goto depot command)
  2151 			* In this case we know that at least one vehicle can be sent to a depot
  2240 			* In this case we know that at least one vehicle can be sent to a depot
  2152 			* and we will issue the command. We can now safely quit the loop, knowing
  2241 			* and we will issue the command. We can now safely quit the loop, knowing
  2153 			* it will succeed at least once. With DC_EXEC we really need to send them to the depot */
  2242 			* it will succeed at least once. With DC_EXEC we really need to send them to the depot */
  2154 		if (!CmdFailed(ret) && !(flags & DC_EXEC)) {
  2243 		if (CmdSucceeded(ret) && !(flags & DC_EXEC)) {
  2155 			free((void*)sort_list);
  2244 			free((void*)sort_list);
  2156 			return 0;
  2245 			return 0;
  2157 		}
  2246 		}
  2158 	}
  2247 	}
  2159 
  2248 
  2223 		t = v->current_order;
  2312 		t = v->current_order;
  2224 		v->current_order.type = OT_DUMMY;
  2313 		v->current_order.type = OT_DUMMY;
  2225 		v->current_order.flags = 0;
  2314 		v->current_order.flags = 0;
  2226 
  2315 
  2227 		if (t.refit_cargo < NUM_CARGO) {
  2316 		if (t.refit_cargo < NUM_CARGO) {
  2228 			int32 cost;
  2317 			CommandCost cost;
  2229 
  2318 
  2230 			_current_player = v->owner;
  2319 			_current_player = v->owner;
  2231 			cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, GetCmdRefitVeh(v));
  2320 			cost = DoCommand(v->tile, v->index, t.refit_cargo | t.refit_subtype << 8, DC_EXEC, GetCmdRefitVeh(v));
  2232 
  2321 
  2233 			if (CmdFailed(cost)) {
  2322 			if (CmdFailed(cost)) {
  2271  * @param tile unused
  2360  * @param tile unused
  2272  * @param flags type of operation
  2361  * @param flags type of operation
  2273  * @param p1 vehicle ID to name
  2362  * @param p1 vehicle ID to name
  2274  * @param p2 unused
  2363  * @param p2 unused
  2275  */
  2364  */
  2276 int32 CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  2365 CommandCost CmdNameVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  2277 {
  2366 {
  2278 	Vehicle *v;
  2367 	Vehicle *v;
  2279 	StringID str;
  2368 	StringID str;
  2280 
  2369 
  2281 	if (!IsValidVehicleID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
  2370 	if (!IsValidVehicleID(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
  2305  * @param tile unused
  2394  * @param tile unused
  2306  * @param flags type of operation
  2395  * @param flags type of operation
  2307  * @param p1 vehicle ID that is being service-interval-changed
  2396  * @param p1 vehicle ID that is being service-interval-changed
  2308  * @param p2 new service interval
  2397  * @param p2 new service interval
  2309  */
  2398  */
  2310 int32 CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  2399 CommandCost CmdChangeServiceInt(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
  2311 {
  2400 {
  2312 	Vehicle* v;
  2401 	Vehicle* v;
  2313 	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
  2402 	uint16 serv_int = GetServiceIntervalClamped(p2); /* Double check the service interval from the user-input */
  2314 
  2403 
  2315 	if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;
  2404 	if (serv_int != p2 || !IsValidVehicleID(p1)) return CMD_ERROR;