From a27b563c404b84ce5e017042581f326c5a1456b5 Mon Sep 17 00:00:00 2001 From: Darkvater Date: Sat, 20 May 2006 17:11:09 +0000 Subject: [PATCH] (svn r4921) - Backport from trunk (r4825, r4826, r4829): Game crashes when cloning/autoreplace reaches train-limit --- aircraft_cmd.c | 4 ++-- roadveh_cmd.c | 4 ++-- ship_cmd.c | 7 ++++--- train_cmd.c | 7 ++++--- vehicle.c | 7 +++++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/aircraft_cmd.c b/aircraft_cmd.c index 5a5392c6bb..fd407738b1 100644 --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -130,7 +130,7 @@ static int32 EstimateAircraftCost(EngineID engine_type) /** Build an aircraft. * @param x,y tile coordinates 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(int x, int y, uint32 flags, uint32 p1, uint32 p2) { @@ -159,7 +159,7 @@ int32 CmdBuildAircraft(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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); diff --git a/roadveh_cmd.c b/roadveh_cmd.c index 4db73d9d10..bb505205e2 100644 --- a/roadveh_cmd.c +++ b/roadveh_cmd.c @@ -99,7 +99,7 @@ static int32 EstimateRoadVehCost(EngineID engine_type) /** Build a road vehicle. * @param x,y tile coordinates 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(int x, int y, uint32 flags, uint32 p1, uint32 p2) { @@ -126,7 +126,7 @@ int32 CmdBuildRoadVeh(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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); diff --git a/ship_cmd.c b/ship_cmd.c index e8dc947fef..568003dca3 100644 --- a/ship_cmd.c +++ b/ship_cmd.c @@ -824,7 +824,7 @@ void ShipsYearlyLoop(void) /** Build a ship. * @param x,y tile coordinates 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(int x, int y, uint32 flags, uint32 p1, uint32 p2) { @@ -847,8 +847,9 @@ int32 CmdBuildShip(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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) { diff --git a/train_cmd.c b/train_cmd.c index cc432ab53e..f7cf9cd5ba 100644 --- a/train_cmd.c +++ b/train_cmd.c @@ -670,7 +670,8 @@ static void AddRearEngineToMultiheadedTrain(Vehicle* v, Vehicle* u, bool buildin /** Build a railroad vehicle. * @param x,y tile coordinates (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 + * @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(int x, int y, uint32 flags, uint32 p1, uint32 p2) { @@ -714,7 +715,7 @@ int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) v = vl[0]; - unit_num = GetFreeUnitNumber(VEH_Train); + 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); @@ -782,7 +783,7 @@ int32 CmdBuildRailVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) 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); } diff --git a/vehicle.c b/vehicle.c index dbfc035bd2..99d0a3b71f 100644 --- a/vehicle.c +++ b/vehicle.c @@ -1495,6 +1495,7 @@ int32 CmdCloneVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) Vehicle *v_front, *v; Vehicle *w_front, *w, *w_rear; int cost, total_cost = 0; + uint32 build_argument = 2; if (!IsVehicleIndex(p1)) return CMD_ERROR; v = GetVehicle(p1); @@ -1537,7 +1538,8 @@ int32 CmdCloneVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2) continue; } - cost = DoCommand(x, y, v->engine_type, 1, flags, CMD_BUILD_VEH(v->type)); + cost = DoCommand(x, y, v->engine_type, build_argument, flags, CMD_BUILD_VEH(v->type)); + build_argument = 3; // ensure that we only assign a number to the first engine if (CmdFailed(cost)) return cost; @@ -1625,7 +1627,7 @@ static int32 ReplaceVehicle(Vehicle **w, byte flags) 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->x_pos, old_v->y_pos, new_engine_type, 1, flags, CMD_BUILD_VEH(old_v->type)); + cost = DoCommand(old_v->x_pos, old_v->y_pos, new_engine_type, 3, flags, CMD_BUILD_VEH(old_v->type)); if (CmdFailed(cost)) return cost; if (flags & DC_EXEC) { @@ -1661,6 +1663,7 @@ static int32 ReplaceVehicle(Vehicle **w, byte flags) 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){