src/autoreplace_cmd.cpp
branchnoai
changeset 9703 d2a6acdbd665
parent 9701 d1ac22c62f64
child 9722 ebf0ece7d8f6
equal deleted inserted replaced
9702:e782b59f1f6a 9703:d2a6acdbd665
   200 			 * sell the old engine in a moment
   200 			 * sell the old engine in a moment
   201 			 */
   201 			 */
   202 			/* Get the vehicle in front of the one we move out */
   202 			/* Get the vehicle in front of the one we move out */
   203 			Vehicle *front = old_v->Previous();
   203 			Vehicle *front = old_v->Previous();
   204 			/* If the vehicle in front is the rear end of a dualheaded engine, then we need to use the one in front of that one */
   204 			/* If the vehicle in front is the rear end of a dualheaded engine, then we need to use the one in front of that one */
   205 			if (IsMultiheaded(front) && !IsTrainEngine(front)) front = front->Previous();
   205 			if (IsRearDualheaded(front)) front = front->Previous();
   206 			/* Now we move the old one out of the train */
   206 			/* Now we move the old one out of the train */
   207 			DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
   207 			DoCommand(0, (INVALID_VEHICLE << 16) | old_v->index, 0, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
   208 			/* Add the new vehicle */
   208 			/* Add the new vehicle */
   209 			DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
   209 			DoCommand(0, (front->index << 16) | new_v->index, 1, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
   210 		} else {
   210 		} else {
   245 			SetDParam(0, old_v->index);
   245 			SetDParam(0, old_v->index);
   246 			GetString(vehicle_name, STR_VEHICLE_NAME, lastof(vehicle_name));
   246 			GetString(vehicle_name, STR_VEHICLE_NAME, lastof(vehicle_name));
   247 		}
   247 		}
   248 	} else { // flags & DC_EXEC not set
   248 	} else { // flags & DC_EXEC not set
   249 		CommandCost tmp_move;
   249 		CommandCost tmp_move;
   250 		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v) && old_v->Next() != NULL) {
   250 
   251 			/* Verify that the wagons can be placed on the engine in question.
   251 		if (old_v->type == VEH_TRAIN && IsFrontEngine(old_v)) {
   252 			 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
   252 			Vehicle *next_veh = GetNextUnit(old_v); // don't try to move the rear multiheaded engine or articulated parts
   253 			DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
   253 			if (next_veh != NULL) {
   254 			Vehicle *temp = GetVehicle(_new_vehicle_id);
   254 				/* Verify that the wagons can be placed on the engine in question.
   255 			tmp_move = DoCommand(0, (temp->index << 16) | old_v->Next()->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
   255 				 * This is done by building an engine, test if the wagons can be added and then sell the test engine. */
   256 			DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
   256 				DoCommand(old_v->tile, new_engine_type, 3, DC_EXEC, GetCmdBuildVeh(old_v));
       
   257 				Vehicle *temp = GetVehicle(_new_vehicle_id);
       
   258 				tmp_move = DoCommand(0, (temp->index << 16) | next_veh->index, 1, 0, CMD_MOVE_RAIL_VEHICLE);
       
   259 				DoCommand(0, temp->index, 0, DC_EXEC, GetCmdSellVeh(old_v));
       
   260 			}
   257 		}
   261 		}
   258 
   262 
   259 		/* Ensure that the player will not end up having negative money while autoreplacing
   263 		/* Ensure that the player will not end up having negative money while autoreplacing
   260 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
   264 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
   261 		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
   265 		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
   330 
   334 
   331 	for (;;) {
   335 	for (;;) {
   332 		cost = CommandCost();
   336 		cost = CommandCost();
   333 		w = v;
   337 		w = v;
   334 		do {
   338 		do {
   335 			if (w->type == VEH_TRAIN && IsMultiheaded(w) && !IsTrainEngine(w)) {
   339 			if (w->type == VEH_TRAIN && IsRearDualheaded(w)) {
   336 				/* we build the rear ends of multiheaded trains with the front ones */
   340 				/* we build the rear ends of multiheaded trains with the front ones */
   337 				continue;
   341 				continue;
   338 			}
   342 			}
   339 
   343 
   340 			// check if the vehicle should be replaced
   344 			// check if the vehicle should be replaced