vehicle.c
changeset 4346 66105d4f6e83
parent 4344 7e123fec5b0b
child 4352 6703cd8d39e0
equal deleted inserted replaced
4345:1da147230c79 4346:66105d4f6e83
    81  */
    81  */
    82 static void VehiclePoolNewBlock(uint start_item)
    82 static void VehiclePoolNewBlock(uint start_item)
    83 {
    83 {
    84 	Vehicle *v;
    84 	Vehicle *v;
    85 
    85 
    86 	FOR_ALL_VEHICLES_FROM(v, start_item) v->index = start_item++;
    86 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
    87 	 * TODO - This is just a temporary stage, this will be removed. */
       
    88 	for (v = GetVehicle(start_item); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) v->index = start_item++;
    87 }
    89 }
    88 
    90 
    89 /* Initialize the vehicle-pool */
    91 /* Initialize the vehicle-pool */
    90 MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, NULL, 0, 0, NULL };
    92 MemoryPool _vehicle_pool = { "Vehicle", VEHICLES_POOL_MAX_BLOCKS, VEHICLES_POOL_BLOCK_SIZE_BITS, sizeof(Vehicle), &VehiclePoolNewBlock, NULL, 0, 0, NULL };
    91 
    93 
   223 		if (v->type == VEH_Train && (IsFrontEngine(v) || IsFreeWagon(v)))
   225 		if (v->type == VEH_Train && (IsFrontEngine(v) || IsFreeWagon(v)))
   224 			TrainConsistChanged(v);
   226 			TrainConsistChanged(v);
   225 	}
   227 	}
   226 
   228 
   227 	FOR_ALL_VEHICLES(v) {
   229 	FOR_ALL_VEHICLES(v) {
   228 		if (v->type != 0) {
   230 		switch (v->type) {
   229 			switch (v->type) {
   231 			case VEH_Train: v->cur_image = GetTrainImage(v, v->direction); break;
   230 				case VEH_Train: v->cur_image = GetTrainImage(v, v->direction); break;
   232 			case VEH_Road: v->cur_image = GetRoadVehImage(v, v->direction); break;
   231 				case VEH_Road: v->cur_image = GetRoadVehImage(v, v->direction); break;
   233 			case VEH_Ship: v->cur_image = GetShipImage(v, v->direction); break;
   232 				case VEH_Ship: v->cur_image = GetShipImage(v, v->direction); break;
   234 			case VEH_Aircraft:
   233 				case VEH_Aircraft:
   235 				if (v->subtype == 0 || v->subtype == 2) {
   234 					if (v->subtype == 0 || v->subtype == 2) {
   236 					v->cur_image = GetAircraftImage(v, v->direction);
   235 						v->cur_image = GetAircraftImage(v, v->direction);
   237 					if (v->next != NULL) v->next->cur_image = v->cur_image;
   236 						if (v->next != NULL) v->next->cur_image = v->cur_image;
   238 				}
   237 					}
   239 				break;
   238 					break;
   240 			default: break;
   239 				default: break;
   241 		}
   240 			}
   242 
   241 
   243 		v->left_coord = INVALID_COORD;
   242 			v->left_coord = INVALID_COORD;
   244 		VehiclePositionChanged(v);
   243 			VehiclePositionChanged(v);
       
   244 		}
       
   245 	}
   245 	}
   246 }
   246 }
   247 
   247 
   248 static Vehicle *InitializeVehicle(Vehicle *v)
   248 static Vehicle *InitializeVehicle(Vehicle *v)
   249 {
   249 {
   282 	 * BLOCKS_FOR_SPECIAL_VEHICLES times block_size vehicles that may only
   282 	 * BLOCKS_FOR_SPECIAL_VEHICLES times block_size vehicles that may only
   283 	 * be used for special vehicles.. should work nicely :) */
   283 	 * be used for special vehicles.. should work nicely :) */
   284 
   284 
   285 	Vehicle *v;
   285 	Vehicle *v;
   286 
   286 
   287 	FOR_ALL_VEHICLES(v) {
   287 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
   288 	 * TODO - This is just a temporary stage, this will be removed. */
       
   289 	for (v = GetVehicle(0); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
   288 		/* No more room for the special vehicles, return NULL */
   290 		/* No more room for the special vehicles, return NULL */
   289 		if (v->index >= (1 << _vehicle_pool.block_size_bits) * BLOCKS_FOR_SPECIAL_VEHICLES)
   291 		if (v->index >= (1 << _vehicle_pool.block_size_bits) * BLOCKS_FOR_SPECIAL_VEHICLES)
   290 			return NULL;
   292 			return NULL;
   291 
   293 
   292 		if (v->type == 0)
   294 		if (!IsValidVehicle(v)) return InitializeVehicle(v);
   293 			return InitializeVehicle(v);
       
   294 	}
   295 	}
   295 
   296 
   296 	return NULL;
   297 	return NULL;
   297 }
   298 }
   298 
   299 
   309 	/* See note by ForceAllocateSpecialVehicle() why we skip the
   310 	/* See note by ForceAllocateSpecialVehicle() why we skip the
   310 	 * first blocks */
   311 	 * first blocks */
   311 	Vehicle *v;
   312 	Vehicle *v;
   312 	const int offset = (1 << VEHICLES_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES;
   313 	const int offset = (1 << VEHICLES_POOL_BLOCK_SIZE_BITS) * BLOCKS_FOR_SPECIAL_VEHICLES;
   313 
   314 
       
   315 	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
       
   316 	 * TODO - This is just a temporary stage, this will be removed. */
   314 	if (*skip_vehicles < (_vehicle_pool.total_items - offset)) {	// make sure the offset in the array is not larger than the array itself
   317 	if (*skip_vehicles < (_vehicle_pool.total_items - offset)) {	// make sure the offset in the array is not larger than the array itself
   315 		FOR_ALL_VEHICLES_FROM(v, offset + *skip_vehicles) {
   318 		for (v = GetVehicle(offset + *skip_vehicles); v != NULL; v = (v->index + 1 < GetVehiclePoolSize()) ? GetVehicle(v->index + 1) : NULL) {
   316 			(*skip_vehicles)++;
   319 			(*skip_vehicles)++;
   317 			if (v->type == 0)
   320 			if (!IsValidVehicle(v)) return InitializeVehicle(v);
   318 				return InitializeVehicle(v);
       
   319 		}
   321 		}
   320 	}
   322 	}
   321 
   323 
   322 	/* Check if we can add a block to the pool */
   324 	/* Check if we can add a block to the pool */
   323 	if (AddBlockToPool(&_vehicle_pool))
   325 	if (AddBlockToPool(&_vehicle_pool))
   618 #endif //ENABLE_NETWORK
   620 #endif //ENABLE_NETWORK
   619 
   621 
   620 	_first_veh_in_depot_list = NULL;	// now we are sure it's initialized at the start of each tick
   622 	_first_veh_in_depot_list = NULL;	// now we are sure it's initialized at the start of each tick
   621 
   623 
   622 	FOR_ALL_VEHICLES(v) {
   624 	FOR_ALL_VEHICLES(v) {
   623 		if (v->type != 0) {
   625 		_vehicle_tick_procs[v->type - 0x10](v);
   624 			_vehicle_tick_procs[v->type - 0x10](v);
       
   625 		}
       
   626 	}
   626 	}
   627 
   627 
   628 	// now we handle all the vehicles that entered a depot this tick
   628 	// now we handle all the vehicles that entered a depot this tick
   629 	v = _first_veh_in_depot_list;
   629 	v = _first_veh_in_depot_list;
   630 	while (v != NULL) {
   630 	while (v != NULL) {
  1393 
  1393 
  1394 	x = (x << vp->zoom) + vp->virtual_left;
  1394 	x = (x << vp->zoom) + vp->virtual_left;
  1395 	y = (y << vp->zoom) + vp->virtual_top;
  1395 	y = (y << vp->zoom) + vp->virtual_top;
  1396 
  1396 
  1397 	FOR_ALL_VEHICLES(v) {
  1397 	FOR_ALL_VEHICLES(v) {
  1398 		if (v->type != 0 && (v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 &&
  1398 		if ((v->vehstatus & (VS_HIDDEN|VS_UNCLICKABLE)) == 0 &&
  1399 				x >= v->left_coord && x <= v->right_coord &&
  1399 				x >= v->left_coord && x <= v->right_coord &&
  1400 				y >= v->top_coord && y <= v->bottom_coord) {
  1400 				y >= v->top_coord && y <= v->bottom_coord) {
  1401 
  1401 
  1402 			dist = max(
  1402 			dist = max(
  1403 				myabs( ((v->left_coord + v->right_coord)>>1) - x ),
  1403 				myabs( ((v->left_coord + v->right_coord)>>1) - x ),
  1942 
  1942 
  1943 	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
  1943 	if (serv_int != p2 || !IsVehicleIndex(p1)) return CMD_ERROR;
  1944 
  1944 
  1945 	v = GetVehicle(p1);
  1945 	v = GetVehicle(p1);
  1946 
  1946 
  1947 	if (v->type == 0 || !CheckOwnership(v->owner)) return CMD_ERROR;
  1947 	if (!IsValidVehicle(v) || !CheckOwnership(v->owner)) return CMD_ERROR;
  1948 
  1948 
  1949 	if (flags & DC_EXEC) {
  1949 	if (flags & DC_EXEC) {
  1950 		v->service_interval = serv_int;
  1950 		v->service_interval = serv_int;
  1951 		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
  1951 		InvalidateWindow(WC_VEHICLE_DETAILS, v->index);
  1952 	}
  1952 	}
  2394 static void Save_VEHS(void)
  2394 static void Save_VEHS(void)
  2395 {
  2395 {
  2396 	Vehicle *v;
  2396 	Vehicle *v;
  2397 	// Write the vehicles
  2397 	// Write the vehicles
  2398 	FOR_ALL_VEHICLES(v) {
  2398 	FOR_ALL_VEHICLES(v) {
  2399 		if (v->type != 0) {
  2399 		SlSetArrayIndex(v->index);
  2400 			SlSetArrayIndex(v->index);
  2400 		SlObject(v, _veh_descs[v->type - 0x10]);
  2401 			SlObject(v, _veh_descs[v->type - 0x10]);
       
  2402 		}
       
  2403 	}
  2401 	}
  2404 }
  2402 }
  2405 
  2403 
  2406 // Will be called when vehicles need to be loaded.
  2404 // Will be called when vehicles need to be loaded.
  2407 static void Load_VEHS(void)
  2405 static void Load_VEHS(void)
  2433 	/* Check for shared order-lists (we now use pointers for that) */
  2431 	/* Check for shared order-lists (we now use pointers for that) */
  2434 	if (CheckSavegameVersionOldStyle(5, 2)) {
  2432 	if (CheckSavegameVersionOldStyle(5, 2)) {
  2435 		FOR_ALL_VEHICLES(v) {
  2433 		FOR_ALL_VEHICLES(v) {
  2436 			Vehicle *u;
  2434 			Vehicle *u;
  2437 
  2435 
  2438 			if (v->type == 0)
       
  2439 				continue;
       
  2440 
       
  2441 			FOR_ALL_VEHICLES_FROM(u, v->index + 1) {
  2436 			FOR_ALL_VEHICLES_FROM(u, v->index + 1) {
  2442 				if (u->type == 0)
       
  2443 					continue;
       
  2444 
       
  2445 				/* If a vehicle has the same orders, add the link to eachother
  2437 				/* If a vehicle has the same orders, add the link to eachother
  2446 				    in both vehicles */
  2438 				    in both vehicles */
  2447 				if (v->orders == u->orders) {
  2439 				if (v->orders == u->orders) {
  2448 					v->next_shared = u;
  2440 					v->next_shared = u;
  2449 					u->prev_shared = v;
  2441 					u->prev_shared = v;