Skip to content

Commit

Permalink
Merge pull request #5 from undarl/master
Browse files Browse the repository at this point in the history
API and AAIPV
  • Loading branch information
avosirenfal authored Apr 19, 2017
2 parents b903220 + 42c7eb0 commit b7963a2
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Localized_Charging_1.0.0/control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,35 @@ local electric_vehicles = {
['tank-mk3'] = true,
}

remote.add_interface('Localized_Charging', {
--Allow other mods to add electric vehicles
add_electric_vehicle = function(name)
if (electric_vehicles[name] ~= nil) then
return true
end
local vehicle = game.entity_prototypes[name]
if (vehicle ~= nil) and ((vehicle.type == 'car') or (vehicle.type == 'locomotive')) then
electric_vehicles[name] = true
return true
end
return false
end,
--Handle entity replacement by AAI Programmable Vehicles
on_entity_replaced = function(event)
for k, vehicle_info in pairs(global.vehicles) do
local vehicle = vehicle_info.vehicle
if(vehicle == event.old_entity) then
global.vehicles[k]['vehicle'] = event.new_entity
if(remote.interfaces['aai-programmable-vehicles']) and (remote.call('aai-programmable-vehicles', 'get_unit_by_entity', vehicle) ~= nil) then
--AAIPV uses a special fueling method, so disable regular battery fueling
global.vehicles[k]['battery_fueling_enabled'] = false
end
break
end
end
end
})

local function defaultdict(d)
t = {}
setmetatable(t, {
Expand Down Expand Up @@ -71,7 +100,7 @@ script.on_event(defines.events.on_tick, function(event)

if(not vehicle.valid) then
table.insert(remove, k)
elseif(vehicle.grid ~= nil) then
elseif(vehicle.grid ~= nil) and (vehicle_info.battery_fueling_enabled) then
local grid = vehicle.grid
local available_power = grid.available_in_batteries
local current_power = vehicle.energy
Expand Down Expand Up @@ -271,6 +300,7 @@ local function on_entity_created(entity)
-- cars, etc can't be paused by event because there's no way to detect their inventory changing as it happens (no event)
table.insert(global.vehicles, {
['vehicle'] = entity,
['battery_fueling_enabled'] = true,
['charging'] = false,
-- the last time the grid was scanned for non-empty batteries
['lastCheck'] = 0,
Expand Down

0 comments on commit b7963a2

Please sign in to comment.