vehicle.c
branch0.4.5
changeset 9924 0d005642432e
parent 2942 a57900fda8b8
child 9928 cc7b6d3bea3e
equal deleted inserted replaced
9923:4bafc584ea69 9924:0d005642432e
   216 {
   216 {
   217 	Vehicle *v;
   217 	Vehicle *v;
   218 
   218 
   219 	FOR_ALL_VEHICLES(v) {
   219 	FOR_ALL_VEHICLES(v) {
   220 		v->first = NULL;
   220 		v->first = NULL;
       
   221 		if (v->type == VEH_Train && (IsFrontEngine(v) || IsFreeWagon(v)))
       
   222 			TrainConsistChanged(v);
       
   223 	}
       
   224 
       
   225 	FOR_ALL_VEHICLES(v) {
   221 		if (v->type != 0) {
   226 		if (v->type != 0) {
   222 			switch (v->type) {
   227 			switch (v->type) {
   223 				case VEH_Train: v->cur_image = GetTrainImage(v, v->direction); break;
   228 				case VEH_Train: v->cur_image = GetTrainImage(v, v->direction); break;
   224 				case VEH_Road: v->cur_image = GetRoadVehImage(v, v->direction); break;
   229 				case VEH_Road: v->cur_image = GetRoadVehImage(v, v->direction); break;
   225 				case VEH_Ship: v->cur_image = GetShipImage(v, v->direction); break;
   230 				case VEH_Ship: v->cur_image = GetShipImage(v, v->direction); break;
   232 				default: break;
   237 				default: break;
   233 			}
   238 			}
   234 
   239 
   235 			v->left_coord = INVALID_COORD;
   240 			v->left_coord = INVALID_COORD;
   236 			VehiclePositionChanged(v);
   241 			VehiclePositionChanged(v);
   237 
       
   238 			if (v->type == VEH_Train && (IsFrontEngine(v) || IsFreeWagon(v)))
       
   239 				TrainConsistChanged(v);
       
   240 		}
   242 		}
   241 	}
   243 	}
   242 }
   244 }
   243 
   245 
   244 static Vehicle *InitializeVehicle(Vehicle *v)
   246 static Vehicle *InitializeVehicle(Vehicle *v)
  1997 	return result;
  1999 	return result;
  1998 }
  2000 }
  1999 
  2001 
  2000 UnitID GetFreeUnitNumber(byte type)
  2002 UnitID GetFreeUnitNumber(byte type)
  2001 {
  2003 {
  2002 	UnitID unit_num = 0;
  2004 	UnitID unit, max;
  2003 	Vehicle *u;
  2005 	const Vehicle *u;
  2004 
  2006 	static bool *cache = NULL;
  2005 restart:
  2007 	static UnitID gmax = 0;
  2006 	unit_num++;
  2008 
       
  2009 	switch (type) {
       
  2010 		case VEH_Train:    max = _patches.max_trains; break;
       
  2011 		case VEH_Road:     max = _patches.max_roadveh; break;
       
  2012 		case VEH_Ship:     max = _patches.max_ships; break;
       
  2013 		case VEH_Aircraft: max = _patches.max_aircraft; break;
       
  2014 		default: assert(0);
       
  2015 	}
       
  2016 
       
  2017 	if (max > gmax) {
       
  2018 		gmax = max;
       
  2019 		free(cache);
       
  2020 		cache = malloc((max + 1) * sizeof(*cache));
       
  2021 	}
       
  2022 
       
  2023 	// Clear the cache
       
  2024 	memset(cache, 0, (max + 1) * sizeof(*cache));
       
  2025 
       
  2026 	// Fill the cache
  2007 	FOR_ALL_VEHICLES(u) {
  2027 	FOR_ALL_VEHICLES(u) {
  2008 		if (u->type == type && u->owner == _current_player &&
  2028 		if (u->type == type && u->owner == _current_player && u->unitnumber != 0 && u->unitnumber <= max)
  2009 		    unit_num == u->unitnumber)
  2029 			cache[u->unitnumber] = true;
  2010 					goto restart;
  2030 	}
  2011 	}
  2031 
  2012 	return unit_num;
  2032 	// Find the first unused unit number
  2013 }
  2033 	for (unit = 1; unit <= max; unit++) {
  2014 
  2034 		if (!cache[unit]) break;
       
  2035 	}
       
  2036 
       
  2037 	return unit;
       
  2038 }
  2015 
  2039 
  2016 // Save and load of vehicles
  2040 // Save and load of vehicles
  2017 const SaveLoad _common_veh_desc[] = {
  2041 const SaveLoad _common_veh_desc[] = {
  2018 	SLE_VAR(Vehicle,subtype,					SLE_UINT8),
  2042 	SLE_VAR(Vehicle,subtype,					SLE_UINT8),
  2019 
  2043