Autodrive

by Pi-C

Car equipment for train avoidance, logistic network integration, circuit network connectivity, fuel refill, ammo reload, vehicle repair, radio control, enemy targeting, and gate control.

Content
2 months ago
0.17 - 1.1
2.98K
Transportation Combat Logistic network Circuit network

i [Implemented] Use crazy math to work out a vehicles max speed, and set the "auto pilot" to that

5 years ago

Hi,
While i love this mod and use it always now, one thing that bugs me is how slow some of the vehicles are when they are self driving.

Looking at the code, you have this limited to 50 - i am unsure if this is 50kph or tiles/s, but meh.

I've written this code which will work out a vehicles max speed that the player is currently in, using this you could allow auto driven vehicles to drive at their valid speeds.

local player = game.player
local vehicle = player.vehicle
if ( vehicle ) then
    local tileUnderVehicle = player.surface.get_tile(player.position)
    if ( tileUnderVehicle ) then        
        if not global.vehMaxSpeeds then global.vehMaxSpeeds = {}

        if ( global.vehMaxSpeeds[vehicle.name] ) then
            -- just return the stored speeds, so we dont have to calc again
            return global.vehMaxSpeeds[vehicle.name]
        end

        -- Setup defaults
        local consumption_modifier = 1
        local fuel_speed_modifier = 1
        local grid_speed_bonus = 1
        local sticker_speed_modifiers = 1
        local proto_effectivity_modifier = 1
        local car_friction = 1
        local sticker_friction_modifier = 1

        -- Get vehicles prototype engine details
        local prototype = vehicle.prototype
        local consumption = prototype.consumption
        local proto_effectivity = prototype.effectivity
        local burner_effectivity = prototype.burner_prototype.effectivity
        local friction = prototype.friction_force
        local terrain_friction_modifier = prototype.terrain_friction_modifier
        local tile_fiction_modifier = tileUnderVehicle.prototype.vehicle_friction_modifier
        local vehicle_weight = prototype.weight

        -- Do some crazy math
        local acceleration_eng_per_tick = (consumption * consumption_modifier * proto_effectivity * proto_effectivity_modifier * burner_effectivity * fuel_speed_modifier * sticker_speed_modifiers)
        local friction_modifier = (1 - friction * (1 + terrain_friction_modifier * (tile_fiction_modifier - 1) ) * car_friction * sticker_friction_modifier)
        local max_energy = acceleration_eng_per_tick * friction_modifier * friction_modifier / (1-friction_modifier*friction_modifier)

        -- Max speed in tiles/s and kph
        local max_speed_tiles_sec = math.sqrt(max_energy/0.5/vehicle_weight)
        local max_speed_kph = max_speed_tiles_sec * 3.6

        -- Store the max speed, so we dont have to calculate it later
        -- You can always clear the global max speeds data on mod init, so that it calculates it once every game at least, so if a mod updates a vehicles speed, you can re-read it.
        global.vehMaxSpeeds[vehicle.name] = max_speed_kph

        game.print("Vehicle " .. vehicle.name .. " max speed: " .. max_speed_tiles_sec .. " tiles/s | " .. max_speed_kph .. " kph")
    end
end
5 years ago

Awesome, thanks :-)

I did wonder if we'd get the same result just by not having the limit at all. Let them accelerate hard to their natural top speed... Either way, I think the only other fix required will be to calc when to start braking on path segments.

4 years ago

I wonder if it really makes sense to store the max speed. Especially tileUnderVehicle.prototype.vehicle_friction_modifier is bound to change with a vehicle's position. To set the max speed to a reasonable value, max speed should be recalculated whenever it's needed. However, I'd store consumption, effectivity, burner_prototype.effectivity, friction_force, terrain_friction_modifier, and weight when a vehicle is added so we'd only have to query tileUnderVehicle.prototype.vehicle_friction_modifier each time.

Also, the top speed should be limited to about 300 (need to test again for the precise value). A jet with rocket-fuel, going at a top speed of about 770, will be much faster than the game can generate new chunks, so that could cause problems.

1 year, 5 months ago
(updated 1 year, 2 months ago)

@AlienX: Thanks a lot for the code! Although I've made some changes (e.g. including boosts/nerfs from fuel), but the credit for figuring out the maximum speeds goes to you! :-)

From version 1.1.6, a vehicle's maximum speed will be calculated based on vehicle prototype, tile prototype, and the fuel consumed. Speed will be capped at 300 kph, so the game has a chance to keep up with generating new chunks.

The first time a max_speed for a vehicle of a certain prototype moving over a certain tile using a certain kind of fuel has been calculated, it will be stored with the vehicle prototype's data, so it can be looked up easily. To make sure that the tables won't be bloated with obsolete data, the max speed data will be reset when the configuration is changed.

New response