src/autoreplace_cmd.cpp
branchnoai
changeset 9723 eee46cb39750
parent 9722 ebf0ece7d8f6
child 9724 b39bc69bb2f2
equal deleted inserted replaced
9722:ebf0ece7d8f6 9723:eee46cb39750
     3 #include "stdafx.h"
     3 #include "stdafx.h"
     4 #include "openttd.h"
     4 #include "openttd.h"
     5 #include "roadveh.h"
     5 #include "roadveh.h"
     6 #include "ship.h"
     6 #include "ship.h"
     7 #include "table/strings.h"
     7 #include "table/strings.h"
     8 #include "functions.h"
       
     9 #include "news.h"
     8 #include "news.h"
    10 #include "command.h"
       
    11 #include "player.h"
     9 #include "player.h"
    12 #include "engine.h"
    10 #include "engine.h"
    13 #include "debug.h"
    11 #include "debug.h"
    14 #include "vehicle_gui.h"
    12 #include "vehicle_gui.h"
    15 #include "depot.h"
    13 #include "depot.h"
    16 #include "train.h"
    14 #include "train.h"
    17 #include "aircraft.h"
    15 #include "aircraft.h"
    18 #include "cargotype.h"
    16 #include "cargotype.h"
    19 #include "group.h"
    17 #include "group.h"
    20 #include "strings.h"
    18 #include "order.h"
    21 
    19 #include "strings_func.h"
       
    20 #include "command_func.h"
       
    21 #include "vehicle_func.h"
       
    22 #include "functions.h"
       
    23 #include "variables.h"
       
    24 #include "autoreplace_func.h"
    22 
    25 
    23 /*
    26 /*
    24  * move the cargo from one engine to another if possible
    27  * move the cargo from one engine to another if possible
    25  */
    28  */
    26 static void MoveVehicleCargo(Vehicle *dest, Vehicle *source)
    29 static void MoveVehicleCargo(Vehicle *dest, Vehicle *source)
   156 	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
   159 	sell_value = DoCommand(0, old_v->index, 0, DC_QUERY_COST, GetCmdSellVeh(old_v));
   157 
   160 
   158 	/* We give the player a loan of the same amount as the sell value.
   161 	/* We give the player a loan of the same amount as the sell value.
   159 	 * This is needed in case he needs the income from the sale to build the new vehicle.
   162 	 * This is needed in case he needs the income from the sale to build the new vehicle.
   160 	 * We take it back if building fails or when we really sell the old engine */
   163 	 * We take it back if building fails or when we really sell the old engine */
   161 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
   162 	SubtractMoneyFromPlayer(sell_value);
   164 	SubtractMoneyFromPlayer(sell_value);
   163 
   165 
   164 	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v));
   166 	cost = DoCommand(old_v->tile, new_engine_type, 3, flags, GetCmdBuildVeh(old_v));
   165 	if (CmdFailed(cost)) {
   167 	if (CmdFailed(cost)) {
   166 		SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
   167 		/* Take back the money we just gave the player */
   168 		/* Take back the money we just gave the player */
   168 		sell_value.MultiplyCost(-1);
   169 		sell_value.MultiplyCost(-1);
   169 		SubtractMoneyFromPlayer(sell_value);
   170 		SubtractMoneyFromPlayer(sell_value);
   170 		return cost;
   171 		return cost;
   171 	}
   172 	}
   261 		}
   262 		}
   262 
   263 
   263 		/* Ensure that the player will not end up having negative money while autoreplacing
   264 		/* Ensure that the player will not end up having negative money while autoreplacing
   264 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
   265 		 * This is needed because the only other check is done after the income from selling the old vehicle is substracted from the cost */
   265 		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
   266 		if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) {
   266 			SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
   267 			/* Pay back the loan */
   267 			/* Pay back the loan */
   268 			sell_value.MultiplyCost(-1);
   268 			sell_value.MultiplyCost(-1);
   269 			SubtractMoneyFromPlayer(sell_value);
   269 			SubtractMoneyFromPlayer(sell_value);
   270 			return CMD_ERROR;
   270 			return CMD_ERROR;
   271 		}
   271 		}
   272 	}
   272 	}
   273 
   273 
   274 	/* Take back the money we just gave the player just before building the vehicle
   274 	/* Take back the money we just gave the player just before building the vehicle
   275 	 * The player will get the same amount now that the sale actually takes place */
   275 	 * The player will get the same amount now that the sale actually takes place */
   276 	SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES);
       
   277 	sell_value.MultiplyCost(-1);
   276 	sell_value.MultiplyCost(-1);
   278 	SubtractMoneyFromPlayer(sell_value);
   277 	SubtractMoneyFromPlayer(sell_value);
   279 
   278 
   280 	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
   279 	/* sell the engine/ find out how much you get for the old engine (income is returned as negative cost) */
   281 	cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
   280 	cost.AddCost(DoCommand(0, old_v->index, 0, flags, GetCmdSellVeh(old_v)));
   331 	 * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */
   330 	 * We also need to reset the flag since it should remain false except from when the vehicle enters a depot until autoreplace is handled in the same tick */
   332 	stopped = v->leave_depot_instantly;
   331 	stopped = v->leave_depot_instantly;
   333 	v->leave_depot_instantly = false;
   332 	v->leave_depot_instantly = false;
   334 
   333 
   335 	for (;;) {
   334 	for (;;) {
   336 		cost = CommandCost();
   335 		cost = CommandCost(EXPENSES_NEW_VEHICLES);
   337 		w = v;
   336 		w = v;
   338 		do {
   337 		do {
   339 			if (w->type == VEH_TRAIN && IsRearDualheaded(w)) {
   338 			if (w->type == VEH_TRAIN && IsRearDualheaded(w)) {
   340 				/* we build the rear ends of multiheaded trains with the front ones */
   339 				/* we build the rear ends of multiheaded trains with the front ones */
   341 				continue;
   340 				continue;