(svn r4826) -Fix: [autoreplace] fixed possible problem when autoreplacing and was number of vehicles (of a type, not total) was reached
now the new vehicle gets the same number as the old one, completely removing the problem where we could run out of numbers
since we don't have to find free numbers for the new vehicles, autoreplace should be somewhat faster, specially in late games
NOTE: in CmdBuildRailVehicle(), bit 0 and 1 in p2 have been switched to make the meaning of bit 0 consistent with the other build commands. CmdCloneVehicle() is modified to follow this as well
--- a/aircraft_cmd.c Thu May 11 12:42:24 2006 +0000
+++ b/aircraft_cmd.c Thu May 11 13:31:14 2006 +0000
@@ -158,7 +158,7 @@
/** Build an aircraft.
* @param tile tile of depot where aircraft is built
* @param p1 aircraft type being built (engine)
- * @param p2 unused
+ * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
*/
int32 CmdBuildAircraft(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
@@ -186,7 +186,7 @@
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
}
- unit_num = GetFreeUnitNumber(VEH_Aircraft);
+ unit_num = (HASBIT(p2, 0) == true) ? 0 : GetFreeUnitNumber(VEH_Aircraft);
if (unit_num > _patches.max_aircraft)
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
--- a/roadveh_cmd.c Thu May 11 12:42:24 2006 +0000
+++ b/roadveh_cmd.c Thu May 11 13:31:14 2006 +0000
@@ -103,7 +103,7 @@
/** Build a road vehicle.
* @param tile tile of depot where road vehicle is built
* @param p1 bus/truck type being built (engine)
- * @param p2 unused
+ * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
*/
int32 CmdBuildRoadVeh(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
@@ -129,7 +129,7 @@
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
/* find the first free roadveh id */
- unit_num = GetFreeUnitNumber(VEH_Road);
+ unit_num = (HASBIT(p2, 0) == true) ? 0 : GetFreeUnitNumber(VEH_Road);
if (unit_num > _patches.max_roadveh)
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
--- a/ship_cmd.c Thu May 11 12:42:24 2006 +0000
+++ b/ship_cmd.c Thu May 11 13:31:14 2006 +0000
@@ -811,7 +811,7 @@
/** Build a ship.
* @param tile tile of depot where ship is built
* @param p1 ship type being built (engine)
- * @param p2 unused
+ * @param p2 bit 0 when set, the unitnumber will be 0, otherwise it will be a free number
*/
int32 CmdBuildShip(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
@@ -833,8 +833,9 @@
if (!IsTileOwner(tile, _current_player)) return CMD_ERROR;
v = AllocateVehicle();
- if (v == NULL || IsOrderPoolFull() ||
- (unit_num = GetFreeUnitNumber(VEH_Ship)) > _patches.max_ships)
+ unit_num = (HASBIT(p2, 0) == true) ? 0 : GetFreeUnitNumber(VEH_Ship);
+
+ if (v == NULL || IsOrderPoolFull() || unit_num > _patches.max_ships)
return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
if (flags & DC_EXEC) {
--- a/train_cmd.c Thu May 11 12:42:24 2006 +0000
+++ b/train_cmd.c Thu May 11 13:31:14 2006 +0000
@@ -699,8 +699,8 @@
/** Build a railroad vehicle.
* @param tile tile of the depot where rail-vehicle is built
* @param p1 engine type id
- * @param p2 bit 0 prevents any free cars from being added to the train
- * bit 1 when set, the train will get number 0, otherwise it will get a free number
+ * @param p2 bit 0 when set, the train will get number 0, otherwise it will get a free number
+ * bit 1 prevents any free cars from being added to the train
*/
int32 CmdBuildRailVehicle(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
{
@@ -744,14 +744,10 @@
v = vl[0];
- if (HASBIT(p2, 1)) {
- // no number is needed, so we assign 0. The engine is likely intended for a train with more than one engine
- unit_num = 0;
- } else {
- unit_num = GetFreeUnitNumber(VEH_Train);
- if (unit_num > _patches.max_trains)
- return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
- }
+ unit_num = (HASBIT(p2, 0) == true) ? 0 : GetFreeUnitNumber(VEH_Train);
+ if (unit_num > _patches.max_trains)
+ return_cmd_error(STR_00E1_TOO_MANY_VEHICLES_IN_GAME);
+
if (flags & DC_EXEC) {
DiagDirection dir = GetRailDepotDirection(tile);
int x = TileX(tile) * TILE_SIZE + _vehicle_initial_x_fract[dir];
@@ -815,7 +811,7 @@
TrainConsistChanged(v);
UpdateTrainAcceleration(v);
- if (!HASBIT(p2, 0)) { // check if the cars should be added to the new vehicle
+ if (!HASBIT(p2, 1)) { // check if the cars should be added to the new vehicle
NormalizeTrainVehInDepot(v);
}
--- a/vehicle.c Thu May 11 12:42:24 2006 +0000
+++ b/vehicle.c Thu May 11 13:31:14 2006 +0000
@@ -1490,7 +1490,7 @@
Vehicle *v_front, *v;
Vehicle *w_front, *w, *w_rear;
int cost, total_cost = 0;
- uint32 build_argument = 1;
+ uint32 build_argument = 2;
if (!IsVehicleIndex(p1)) return CMD_ERROR;
v = GetVehicle(p1);
@@ -1624,7 +1624,7 @@
new_engine_type = EngineReplacementForPlayer(p, old_v->engine_type);
if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
- cost = DoCommand(old_v->tile, new_engine_type, 1, flags, CMD_BUILD_VEH(old_v->type));
+ cost = DoCommand(old_v->tile, new_engine_type, 3, flags, CMD_BUILD_VEH(old_v->type));
if (CmdFailed(cost)) return cost;
if (flags & DC_EXEC) {
@@ -1660,6 +1660,7 @@
new_v->profit_last_year = old_v->profit_last_year;
new_v->service_interval = old_v->service_interval;
new_front = true;
+ new_v->unitnumber = old_v->unitnumber; // use the same unit number
new_v->current_order = old_v->current_order;
if (old_v->type == VEH_Train && GetNextVehicle(old_v) != NULL){