economy.c
changeset 523 1184a22723ad
parent 507 8aa8100b0b22
child 534 17ab2f22ff74
equal deleted inserted replaced
522:24d930de85c8 523:1184a22723ad
  1198 		profit += profit >> 2;
  1198 		profit += profit >> 2;
  1199 
  1199 
  1200 	return profit;
  1200 	return profit;
  1201 }
  1201 }
  1202 
  1202 
       
  1203 /*
       
  1204  * Returns true if Vehicle v should wait loading because other vehicle is
       
  1205  * already loading the same cargo type
       
  1206  * v = vehicle to load, u = GetFirstInChain(v)
       
  1207  */
       
  1208 static bool LoadWait(const Vehicle *v, const Vehicle *u) {
       
  1209 	const Vehicle *w;
       
  1210 	const Vehicle *x;
       
  1211 	bool has_any_cargo = false;
       
  1212 
       
  1213 	if (!(u->next_order & OF_FULL_LOAD)) return false;
       
  1214 
       
  1215 	for (w = u; w != NULL; w = w->next) {
       
  1216 		if (w->cargo_count != 0) {
       
  1217 			if (v->cargo_type == w->cargo_type &&
       
  1218 					u->last_station_visited == w->cargo_source)
       
  1219 				return false;
       
  1220 			has_any_cargo = true;
       
  1221 		}
       
  1222 	}
       
  1223 
       
  1224 	FOR_ALL_VEHICLES(x) {
       
  1225 		if ((x->type != VEH_Train || x->subtype == 0) && // for all locs
       
  1226 				u->last_station_visited == x->last_station_visited && // at the same station
       
  1227 				!(x->vehstatus & VS_STOPPED) && // not stopped
       
  1228 				(x->next_order & OT_MASK) == OT_LOADING && // loading
       
  1229 				u != x) { // not itself
       
  1230 			bool other_has_any_cargo = false;
       
  1231 			bool has_space_for_same_type = false;
       
  1232 			bool other_has_same_type = false;
       
  1233 
       
  1234 			for (w = x; w != NULL; w = w->next) {
       
  1235 				if (w->cargo_count < w->cargo_cap && v->cargo_type == w->cargo_type)
       
  1236 					has_space_for_same_type = true;
       
  1237 
       
  1238 				if (w->cargo_count != 0) {
       
  1239 					if (v->cargo_type == w->cargo_type &&
       
  1240 							u->last_station_visited == w->cargo_source)
       
  1241 						other_has_same_type = true;
       
  1242 					other_has_any_cargo = true;
       
  1243 				}
       
  1244 			}
       
  1245 
       
  1246 			if (has_space_for_same_type) {
       
  1247 				if (other_has_same_type) return true;
       
  1248 				if (other_has_any_cargo && !has_any_cargo) return true;
       
  1249 			}
       
  1250 		}
       
  1251 	}
       
  1252 
       
  1253 	return false;
       
  1254 }
       
  1255 
  1203 int LoadUnloadVehicle(Vehicle *v)
  1256 int LoadUnloadVehicle(Vehicle *v)
  1204 {
  1257 {
  1205 	int profit = 0;
  1258 	int profit = 0;
  1206 	int unloading_time = 20;
  1259 	int unloading_time = 20;
  1207 	Vehicle *u = v;
  1260 	Vehicle *u = v;
  1277 		if ((count=ge->waiting_acceptance & 0xFFF) != 0 &&
  1330 		if ((count=ge->waiting_acceptance & 0xFFF) != 0 &&
  1278 				(cap = v->cargo_cap - v->cargo_count) != 0) {
  1331 				(cap = v->cargo_cap - v->cargo_count) != 0) {
  1279 			if (v->cargo_count == 0)
  1332 			if (v->cargo_count == 0)
  1280 				TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
  1333 				TriggerVehicle(v, VEHICLE_TRIGGER_NEW_CARGO);
  1281 
  1334 
       
  1335 			/* Skip loading this vehicle if another train/vehicle is already handling
       
  1336 			 * the same cargo type at this station */
       
  1337 			if (_patches.improved_load && LoadWait(v,u)) continue;
       
  1338 
  1282 			/* TODO: Regarding this, when we do gradual loading, we
  1339 			/* TODO: Regarding this, when we do gradual loading, we
  1283 			 * should first unload all vehicles and then start
  1340 			 * should first unload all vehicles and then start
  1284 			 * loading them. Since this will cause
  1341 			 * loading them. Since this will cause
  1285 			 * VEHICLE_TRIGGER_EMPTY to be called at the time when
  1342 			 * VEHICLE_TRIGGER_EMPTY to be called at the time when
  1286 			 * the whole vehicle chain is really totally empty, the
  1343 			 * the whole vehicle chain is really totally empty, the