src/vehicle.cpp
changeset 10259 23ab8ff3d044
parent 10240 c4788a9b2cdf
child 10260 2772bc477a4f
equal deleted inserted replaced
10258:2aa4a5f35186 10259:23ab8ff3d044
   689 		}
   689 		}
   690 	}
   690 	}
   691 
   691 
   692 	/* now we handle all the vehicles that entered a depot this tick */
   692 	/* now we handle all the vehicles that entered a depot this tick */
   693 	v = _first_veh_in_depot_list;
   693 	v = _first_veh_in_depot_list;
   694 	while (v != NULL) {
   694 	if (v != NULL) {
   695 		Vehicle *w = v->depot_list;
   695 		while (v != NULL) {
   696 		v->depot_list = NULL; // it should always be NULL at the end of each tick
   696 			/* Autoreplace needs the current player set as the vehicle owner */
   697 		MaybeReplaceVehicle(v, false, true);
   697 			_current_player = v->owner;
   698 		v = w;
   698 
       
   699 			/* Buffer v->depot_list and clear it.
       
   700 			 * Autoreplace might clear this so it has to be buffered. */
       
   701 			Vehicle *w = v->depot_list;
       
   702 			v->depot_list = NULL; // it should always be NULL at the end of each tick
       
   703 
       
   704 			/* Start vehicle if we stopped them in VehicleEnteredDepotThisTick()
       
   705 			 * We need to stop them between VehicleEnteredDepotThisTick() and here or we risk that
       
   706 			 * they are already leaving the depot again before being replaced. */
       
   707 			if (v->leave_depot_instantly) {
       
   708 				v->leave_depot_instantly = false;
       
   709 				v->vehstatus &= ~VS_STOPPED;
       
   710 			}
       
   711 
       
   712 			CommandCost cost = MaybeReplaceVehicle(v, 0, true);
       
   713 			if (CmdSucceeded(cost) && cost.GetCost() != 0) {
       
   714 				/* Looks like we can replace this vehicle so we go ahead and do so */
       
   715 				MaybeReplaceVehicle(v, DC_EXEC, true);
       
   716 			}
       
   717 			v = w;
       
   718 		}
       
   719 		_current_player = OWNER_NONE;
   699 	}
   720 	}
   700 }
   721 }
   701 
   722 
   702 /** Check if a given engine type can be refitted to a given cargo
   723 /** Check if a given engine type can be refitted to a given cargo
   703  * @param engine_type Engine type to check
   724  * @param engine_type Engine type to check
  1693 	/* Get the list of vehicles in the depot */
  1714 	/* Get the list of vehicles in the depot */
  1694 	BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, &vl, &engine_list_length, &engine_count);
  1715 	BuildDepotVehicleList(vehicle_type, tile, &vl, &engine_list_length, &engine_count, &vl, &engine_list_length, &engine_count);
  1695 
  1716 
  1696 	for (uint i = 0; i < engine_count; i++) {
  1717 	for (uint i = 0; i < engine_count; i++) {
  1697 		Vehicle *v = vl[i];
  1718 		Vehicle *v = vl[i];
  1698 		bool stopped = !(v->vehstatus & VS_STOPPED);
       
  1699 
  1719 
  1700 		/* Ensure that the vehicle completely in the depot */
  1720 		/* Ensure that the vehicle completely in the depot */
  1701 		if (!v->IsInDepot()) continue;
  1721 		if (!v->IsInDepot()) continue;
  1702 
  1722 
  1703 		if (stopped) {
  1723 		CommandCost ret = MaybeReplaceVehicle(v, flags, false);
  1704 			v->vehstatus |= VS_STOPPED; // Stop the vehicle
       
  1705 			v->leave_depot_instantly = true;
       
  1706 		}
       
  1707 		CommandCost ret = MaybeReplaceVehicle(v, !(flags & DC_EXEC), false);
       
  1708 
  1724 
  1709 		if (CmdSucceeded(ret)) {
  1725 		if (CmdSucceeded(ret)) {
  1710 			cost.AddCost(ret);
  1726 			cost.AddCost(ret);
  1711 		} else {
  1727 		} else {
  1712 			if (all_or_nothing) {
  1728 			if (all_or_nothing) {