159 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)); |
160 |
160 |
161 /* 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. |
162 * 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. |
163 * 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 */ |
164 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
165 SubtractMoneyFromPlayer(sell_value); |
164 SubtractMoneyFromPlayer(sell_value); |
166 |
165 |
167 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)); |
168 if (CmdFailed(cost)) { |
167 if (CmdFailed(cost)) { |
169 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
170 /* Take back the money we just gave the player */ |
168 /* Take back the money we just gave the player */ |
171 sell_value.MultiplyCost(-1); |
169 sell_value.MultiplyCost(-1); |
172 SubtractMoneyFromPlayer(sell_value); |
170 SubtractMoneyFromPlayer(sell_value); |
173 return cost; |
171 return cost; |
174 } |
172 } |
264 } |
262 } |
265 |
263 |
266 /* 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 |
267 * 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 */ |
268 if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) { |
266 if (CmdFailed(tmp_move) || p->player_money < (cost.GetCost() + total_cost)) { |
269 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
270 /* Pay back the loan */ |
267 /* Pay back the loan */ |
271 sell_value.MultiplyCost(-1); |
268 sell_value.MultiplyCost(-1); |
272 SubtractMoneyFromPlayer(sell_value); |
269 SubtractMoneyFromPlayer(sell_value); |
273 return CMD_ERROR; |
270 return CMD_ERROR; |
274 } |
271 } |
275 } |
272 } |
276 |
273 |
277 /* 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 |
278 * 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 */ |
279 SET_EXPENSES_TYPE(EXPENSES_NEW_VEHICLES); |
|
280 sell_value.MultiplyCost(-1); |
276 sell_value.MultiplyCost(-1); |
281 SubtractMoneyFromPlayer(sell_value); |
277 SubtractMoneyFromPlayer(sell_value); |
282 |
278 |
283 /* 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) */ |
284 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))); |
334 * 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 */ |
335 stopped = v->leave_depot_instantly; |
331 stopped = v->leave_depot_instantly; |
336 v->leave_depot_instantly = false; |
332 v->leave_depot_instantly = false; |
337 |
333 |
338 for (;;) { |
334 for (;;) { |
339 cost = CommandCost(); |
335 cost = CommandCost(EXPENSES_NEW_VEHICLES); |
340 w = v; |
336 w = v; |
341 do { |
337 do { |
342 if (w->type == VEH_TRAIN && IsRearDualheaded(w)) { |
338 if (w->type == VEH_TRAIN && IsRearDualheaded(w)) { |
343 /* 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 */ |
344 continue; |
340 continue; |