From 282c2e6dfed1878054fc7103a109a3a87be95a81 Mon Sep 17 00:00:00 2001 From: Dave Van Vessem Date: Wed, 19 Apr 2017 10:38:21 -0400 Subject: [PATCH 1/3] Add remote interface and AAIPV integration --- Localized_Charging_1.0.0/control.lua | 41 +++++++++++++++++++++++++++- Localized_Charging_1.0.0/info.json | 2 +- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Localized_Charging_1.0.0/control.lua b/Localized_Charging_1.0.0/control.lua index 07a1e46..31a95fa 100644 --- a/Localized_Charging_1.0.0/control.lua +++ b/Localized_Charging_1.0.0/control.lua @@ -28,6 +28,15 @@ local charging_info = { }, } +--Check for the presence of AAI Programmable Vehicles +local aaipv_present = false +for interface_name, _ in pairs(remote.interfaces) do + if(interface_name == 'aai-programmable-vehicles') then + aaipv_present = true + break + end +end + local electric_vehicles = { ['electric-locomotive'] = true, -- airship mod lol @@ -39,6 +48,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(aaipv_present) 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, { @@ -71,7 +109,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 @@ -271,6 +309,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, diff --git a/Localized_Charging_1.0.0/info.json b/Localized_Charging_1.0.0/info.json index 00b7b8e..b1b8037 100644 --- a/Localized_Charging_1.0.0/info.json +++ b/Localized_Charging_1.0.0/info.json @@ -5,6 +5,6 @@ "title": "Localized_Charging", "author": "Sirenfal", "description": "A charging mod that will recharge batteries in vehicles and power armor when near a charging pole.", - "dependencies": ["base >= 0.14.0"], + "dependencies": ["base >= 0.14.0", "?aai-programmable-vehicles >= 0.2.8"], "factorio_version": "0.14.0" } From 3f6e8275555e720d30e89a9968bf4b49fc044d65 Mon Sep 17 00:00:00 2001 From: Dave Van Vessem Date: Wed, 19 Apr 2017 11:26:41 -0400 Subject: [PATCH 2/3] Remove unnecessary code and dependency --- Localized_Charging_1.0.0/control.lua | 11 +---------- Localized_Charging_1.0.0/info.json | 2 +- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/Localized_Charging_1.0.0/control.lua b/Localized_Charging_1.0.0/control.lua index 31a95fa..5f202eb 100644 --- a/Localized_Charging_1.0.0/control.lua +++ b/Localized_Charging_1.0.0/control.lua @@ -28,15 +28,6 @@ local charging_info = { }, } ---Check for the presence of AAI Programmable Vehicles -local aaipv_present = false -for interface_name, _ in pairs(remote.interfaces) do - if(interface_name == 'aai-programmable-vehicles') then - aaipv_present = true - break - end -end - local electric_vehicles = { ['electric-locomotive'] = true, -- airship mod lol @@ -67,7 +58,7 @@ remote.add_interface('Localized_Charging', { local vehicle = vehicle_info.vehicle if(vehicle == event.old_entity) then global.vehicles[k]['vehicle'] = event.new_entity - if(aaipv_present) and (remote.call('aai-programmable-vehicles', 'get_unit_by_entity', vehicle) ~= nil) then + if(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 diff --git a/Localized_Charging_1.0.0/info.json b/Localized_Charging_1.0.0/info.json index b1b8037..00b7b8e 100644 --- a/Localized_Charging_1.0.0/info.json +++ b/Localized_Charging_1.0.0/info.json @@ -5,6 +5,6 @@ "title": "Localized_Charging", "author": "Sirenfal", "description": "A charging mod that will recharge batteries in vehicles and power armor when near a charging pole.", - "dependencies": ["base >= 0.14.0", "?aai-programmable-vehicles >= 0.2.8"], + "dependencies": ["base >= 0.14.0"], "factorio_version": "0.14.0" } From 42c7eb035cbdf8931387178d40f0fadfd1124f95 Mon Sep 17 00:00:00 2001 From: Dave Van Vessem Date: Wed, 19 Apr 2017 11:43:18 -0400 Subject: [PATCH 3/3] Handle an on_entity_replaced edge case In the unlikely event that on_entity_replaced gets called when the AAIPV remote interface is not available, we should no longer error out. --- Localized_Charging_1.0.0/control.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Localized_Charging_1.0.0/control.lua b/Localized_Charging_1.0.0/control.lua index 5f202eb..6bf4b7c 100644 --- a/Localized_Charging_1.0.0/control.lua +++ b/Localized_Charging_1.0.0/control.lua @@ -58,7 +58,7 @@ remote.add_interface('Localized_Charging', { local vehicle = vehicle_info.vehicle if(vehicle == event.old_entity) then global.vehicles[k]['vehicle'] = event.new_entity - if(remote.call('aai-programmable-vehicles', 'get_unit_by_entity', vehicle) ~= nil) then + 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