Car Optimizer

by mulark

Sets cars to inactive if they aren't on a belt or being driven by a player

Tweaks
3 years ago
1.0 - 1.1
1.49K

i Compatibility request

3 years ago
(updated 3 years ago)

Hi! Your mod is based on the assumption that a car could only drive if a player was inside. But this isn't true if mods like Autodrive or AAI Programmable Vehicles are active. (I haven't updated Autodrive yet, but tested with my Work-in-Progress version.) Currently, your mod interferes with mine because I can order the cars to go somewhere, but they won't move unless a player enters. However, the point of my mod (this applies to AAI as well -- Autodrive is kind of a light version of it) is that you can use a remote control (or even combinators) to send vehicles to other places even if no player is inside.

Would you mind adding a remote interface that other mods can use to notify yours when they take over/give up control of a vehicle? You could then build an ignore list (stored in the table "global" of your mod, so it gets saved with the game) and only make vehicles inactive if they are not on this list.

3 years ago

I will consider adding such an interface although I want to be careful about how I go about it. It should probably have some cleanup mechanism outside of a remote control mod releasing control of the vehicle.

3 years ago
(updated 3 years ago)

You could make it that the other mods need to pass on their name, then store that with the car id. First thing that comes to mind:

-- On remote call to lock
global.locked[car.unit_number] = { 
  car = car,
 mods = { [mod_name] = true }
}

-- On remote call to unlock
global.locked[car.unit_number].mods[mod_name] = nil
-- Empty table (no other mod locks this car)
if not next(global.locked[car.unit_number].mods) then 
  local tmp = car.car
  global.locked[car.unit_number] = nil 
  toggle_cars(tmp)
end

-- Additional check for toggle_cars(entity)
if global.locked[entity.unit_number] then return end

-- on_configuration_changed
local function config_changed(event)
-- Check all mods that have been changed
for mod_name, data in pairs(event.mod_changes or {}) do
  -- Mod has been removed
  if  not mod_name.new_version then
    -- Remove mod entry from tables of all cars
    for c, car in pairs(global.locked) do
      car.mods[mod_name] = nil
    end
  end
end

[EDIT: Added the car entity to the table, that's needed for toggling its state.)

3 years ago

I have thought about this issue a bit and think I have a solution. Let me know if the following API would work for you.

  1. Check the car driving_state in toggle_cars. If it's anything but defines.riding.acceleration.nothing && defines.riding.direction.straight then we know the car is being controlled, therefore do not set inactive
  2. Add a remote call to notify that a car is being remotely controlled.
  3. Add a remote call to notify that a car is no longer being remotely controlled/ is parked (not strictly necessary).

With this method, mods seeking compatibility here would have to at least put a car under acceleration/braking/reversing(/turning) before notifying, but speed can be 0.

New response