Hi! Checking your code for potential compatibility issues with Autodrive, I've noticed two things you could optimize:
(1) In scripts/driven-car.lua, lines 288ff., you have this:
-- See if it stopped from hitting a cliff in its travelling direction.
if global.drivenCar.settings.cliffCollision then
-- CODE NOTE: Factorio doesn't support us passing in an orientation as part of a BoundingBox to any of its API functions, so we have to do it as a square area and then manually check any results for diagonal collisions.
You may have missed that, but with Factorio 1.1.82, it is indeed possible to use BoundingBox with orientation (see here). I use that to search the area before or behind a vehicle instead of approximating an area by searching multiple position. You probably should keep the old code for backwards compatibility until Factorio >=1.1.82 hits stable.
(2) I've also tried to detect collisions of vehicles with cliffs. The approach I've used so far is similar to yours (check for vehicle.position not changing over n ticks while vehicle.speed ~= 0). However, since Factorio 1.1.74 car prototypes have the property immune_to_cliff_impacts, which is true by default. If you set this to false, cars will take damage on hitting a cliff, so on_entity_damaged should be raised. I'm still undecided whether I should use this approach to detect cliff collisions because it is vulnerable to other mods (that run after mine) resetting the flag.