src/vehicle.cpp
branchgamebalance
changeset 9910 0b2aebc8283e
parent 9909 dce9a6923bb7
child 9911 0b8b245a2391
--- a/src/vehicle.cpp	Wed Jun 13 11:00:24 2007 +0000
+++ b/src/vehicle.cpp	Wed Jun 13 11:17:30 2007 +0000
@@ -188,9 +188,9 @@
 	}
 	FOR_ALL_VEHICLES(veh) {
 		if (without_crashed && (veh->vehstatus & VS_CRASHED) != 0) continue;
-		if ((veh->type == VEH_TRAIN || veh->type == VEH_ROAD) && (z==0xFF || veh->z_pos == z)) {
-			if ((veh->x_pos>>4) >= x1 && (veh->x_pos>>4) <= x2 &&
-					(veh->y_pos>>4) >= y1 && (veh->y_pos>>4) <= y2) {
+		if ((veh->type == VEH_TRAIN || veh->type == VEH_ROAD) && (z == 0xFF || veh->z_pos == z)) {
+			if ((veh->x_pos >> 4) >= x1 && (veh->x_pos >> 4) <= x2 &&
+					(veh->y_pos >> 4) >= y1 && (veh->y_pos >> 4) <= y2) {
 				return veh;
 			}
 		}
@@ -251,6 +251,8 @@
 						Vehicle *rotor = shadow->next;
 						rotor->cur_image = GetRotorImage(v);
 					}
+
+					UpdateAircraftCache(v);
 				}
 				break;
 			default: break;
@@ -419,7 +421,7 @@
 	int old_x = v->left_coord;
 	int old_y = v->top_coord;
 
-	new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x,y)];
+	new_hash = (x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(x, y)];
 	old_hash = (old_x == INVALID_COORD) ? NULL : &_vehicle_position_hash[GEN_HASH(old_x, old_y)];
 
 	if (old_hash == new_hash) return;
@@ -568,6 +570,10 @@
 
 void DestroyVehicle(Vehicle *v)
 {
+	if (IsValidStationID(v->last_station_visited)) {
+		GetStation(v->last_station_visited)->loading_vehicles.remove(v);
+	}
+
 	if (IsEngineCountable(v)) {
 		GetPlayer(v->owner)->num_engines[v->engine_type]--;
 		if (v->owner == _local_player) InvalidateAutoreplaceWindow(v->engine_type);
@@ -1837,6 +1843,17 @@
 
 	do {
 
+		if (!(flags & DC_EXEC)) {
+			/* Get the refit cost.
+			 * This is only needed when estimating as when the command is executed, the cost from the refit command is used.
+			 * This needs to be done for every single unit, so it should be done before checking if it's a multiheaded engine. */
+			CargoID new_cargo_type = GetEngineCargoType(v->engine_type);
+
+			if (new_cargo_type != v->cargo_type && new_cargo_type != CT_INVALID) {
+				total_cost += GetRefitCost(v->engine_type);
+			}
+		}
+
 		if (IsMultiheaded(v) && !IsTrainEngine(v)) {
 			/* we build the rear ends of multiheaded trains with the front ones */
 			continue;
@@ -1859,7 +1876,7 @@
 					/* We can't pay for refitting because we can't estimate refitting costs for a vehicle before it's build.
 					 * If we pay for it anyway, the cost and the estimated cost will not be the same and we will have an assert.
 					 * We need to check the whole chain if it is a train because some newgrf articulated engines can refit some units only (and not the front) */
-					DoCommand(0, w->index, v2->cargo_type | (v2->cargo_subtype << 8), flags, GetCmdRefitVeh(v));
+					total_cost += DoCommand(0, w->index, v2->cargo_type | (v2->cargo_subtype << 8), flags, GetCmdRefitVeh(v));
 					break; // We learned that the engine in question needed a refit. No need to check anymore
 				}
 			} while (v->type == VEH_TRAIN && (w2 = w2->next) != NULL && (v2 = v2->next) != NULL);
@@ -2221,7 +2238,7 @@
 				}
 
 				SetDParam(0, v->unitnumber);
-				AddNewsItem(string, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0),	v->index, 0);
+				AddNewsItem(string, NEWS_FLAGS(NM_SMALL, NF_VIEWPORT|NF_VEHICLE, NT_ADVICE, 0), v->index, 0);
 			}
 		}
 	}
@@ -2901,6 +2918,7 @@
 {
 	assert(IsTileType(tile, MP_STATION) || type == VEH_SHIP);
 	current_order.type = OT_LOADING;
+	GetStation(this->last_station_visited)->loading_vehicles.push_back(this);
 }
 
 void Vehicle::LeaveStation()
@@ -2909,4 +2927,5 @@
 	assert(current_order.type == OT_LOADING);
 	current_order.type = OT_LEAVESTATION;
 	current_order.flags = 0;
+	GetStation(this->last_station_visited)->loading_vehicles.remove(this);
 }