@@ -394,14 +394,59 @@ class VehiclesComponent final : public IVehiclesComponent, public CoreEventHandl
394394 {
395395 return nullptr ;
396396 }
397- IVehicle* ret = create (VehicleSpawnData { respawnDelay, modelID, position, Z, colour1, colour2, addSiren, 0 });
397+
398+ IVehicle* ret = nullptr ;
398399 if (modelID == 538 || modelID == 537 )
399400 {
401+ auto findConsecutiveFreeIds = [&](size_t count)
402+ {
403+ int candidate = storage.findFreeIndex (storage.Lower );
404+ while (candidate < storage.Upper - count + 1 )
405+ {
406+ bool all_free = true ;
407+ for (size_t i = 1 ; i < count; i++)
408+ {
409+ if (storage.get (candidate + i) != nullptr )
410+ {
411+ candidate = storage.findFreeIndex (candidate + i + 1 );
412+ all_free = false ;
413+ break ;
414+ }
415+ }
416+
417+ if (all_free)
418+ {
419+ return candidate;
420+ }
421+ }
422+
423+ return -1 ;
424+ };
425+
426+ int startId = findConsecutiveFreeIds (4 );
427+ if (startId < 0 )
428+ {
429+ core->logLn (LogLevel::Warning, " Creation of the train failed due to the absence of 4 consecutive vehicle IDs." );
430+ return nullptr ;
431+ }
432+
433+ ret = createWithMustHint (startId, VehicleSpawnData { respawnDelay, modelID, position, Z, colour1, colour2, addSiren, 0 });
434+ if (!ret)
435+ {
436+ core->logLn (LogLevel::Warning, " Creation of the train failed because the locomotive could not be created." );
437+ return nullptr ;
438+ }
439+
400440 int carridgeModel = modelID == 538 ? 570 : 569 ;
401- ret->addCarriage (create (VehicleSpawnData { respawnDelay, carridgeModel, position, Z, colour1, colour2, 0 }), 0 );
402- ret->addCarriage (create (VehicleSpawnData { respawnDelay, carridgeModel, position, Z, colour1, colour2, 0 }), 1 );
403- ret->addCarriage (create (VehicleSpawnData { respawnDelay, carridgeModel, position, Z, colour1, colour2, 0 }), 2 );
441+ ret->addCarriage (createWithMustHint (startId + 1 , VehicleSpawnData { respawnDelay, carridgeModel, position, Z, colour1, colour2, 0 }), 0 );
442+ ret->addCarriage (createWithMustHint (startId + 2 , VehicleSpawnData { respawnDelay, carridgeModel, position, Z, colour1, colour2, 0 }), 1 );
443+ ret->addCarriage (createWithMustHint (startId + 3 , VehicleSpawnData { respawnDelay, carridgeModel, position, Z, colour1, colour2, 0 }), 2 );
444+ }
445+ else
446+ {
447+ ret = create (VehicleSpawnData { respawnDelay, modelID, position, Z, colour1, colour2, addSiren, 0 });
404448 }
449+
405450 return ret;
406451 }
407452
@@ -424,6 +469,31 @@ class VehiclesComponent final : public IVehiclesComponent, public CoreEventHandl
424469 return vehicle;
425470 }
426471
472+ IVehicle* createWithMustHint (int hint, const VehicleSpawnData& data)
473+ {
474+ int vehicleId = storage.claimHint (hint, this , data);
475+ if (hint != vehicleId)
476+ {
477+ storage.release (vehicleId, true );
478+ return nullptr ;
479+ }
480+
481+ IVehicle* vehicle = storage.get (vehicleId);
482+ if (vehicle)
483+ {
484+ ++preloadModels[data.modelID - 400 ];
485+
486+ static bool delay_warn = false ;
487+ if (!delay_warn && data.respawnDelay == Seconds (0 ))
488+ {
489+ core->logLn (LogLevel::Warning, " Vehicle created with respawn delay 0 which is undefined behaviour that might change in the future." );
490+ delay_warn = true ;
491+ }
492+ }
493+
494+ return vehicle;
495+ }
496+
427497 void free () override
428498 {
429499 delete this ;
0 commit comments