MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / checkForCollisions

Function checkForCollisions

src/OpenLoco/src/Vehicles/Vehicle.cpp:378–445  ·  view source on GitHub ↗

0x004B1876

Source from the content-addressed store, hash-verified

376
377 // 0x004B1876
378 EntityId checkForCollisions(VehicleBogie& bogie, World::Pos3& loc)
379 {
380 if (bogie.mode != TransportMode::rail)
381 {
382 return EntityId::null;
383 }
384
385 Vehicle srcTrain(bogie.head);
386
387 for (const auto& nearby : kMooreNeighbourhood)
388 {
389 const auto inspectionPos = World::toTileSpace(loc) + nearby;
390 for (auto* entity : EntityManager::EntityTileList(World::toWorldSpace(inspectionPos)))
391 {
392 auto* vehicleBase = entity->asBase<VehicleBase>();
393 if (vehicleBase == nullptr || vehicleBase == &bogie)
394 {
395 continue;
396 }
397 if (vehicleBase->getTransportMode() != TransportMode::rail)
398 {
399 continue;
400 }
401
402 const auto zDiff = std::abs(loc.z - vehicleBase->position.z);
403 if (zDiff > 16)
404 {
405 continue;
406 }
407
408 // vanilla did some overflow checks here but since we promote to int it shouldn't be needed
409 const auto distance = Math::Vector::manhattanDistance2D(vehicleBase->position, loc);
410 if (distance >= 12)
411 {
412 continue;
413 }
414
415 const auto subType = vehicleBase->getSubType();
416 // Does it actually have a collidable body
417 if (subType != VehicleEntityType::body_continued && subType != VehicleEntityType::body_start && subType != VehicleEntityType::bogie)
418 {
419 continue;
420 }
421
422 if (vehicleBase->owner != bogie.owner)
423 {
424 continue;
425 }
426
427 // This is an optimisation compared to vanilla
428 if (vehicleBase->getHead() != bogie.head)
429 {
430 return vehicleBase->id;
431 }
432
433 if (ignoreSelfCollision(bogie, *vehicleBase))
434 {
435 continue;

Callers 3

updateRoadMotionFunction · 0.85
updateTrackMotionFunction · 0.85

Calls 8

toTileSpaceFunction · 0.85
EntityTileListClass · 0.85
toWorldSpaceFunction · 0.85
manhattanDistance2DFunction · 0.85
ignoreSelfCollisionFunction · 0.85
getTransportModeMethod · 0.80
getHeadMethod · 0.80
getSubTypeMethod · 0.45

Tested by

no test coverage detected