From 39cb980e0a9ed0111afb148b65147c83dfcf453a Mon Sep 17 00:00:00 2001 From: robot256 <> Date: Thu, 4 Apr 2019 00:08:57 -0400 Subject: [PATCH 1/2] Add mod support Use script_raised_built event to detect when mods create electric vehicles. --- control.lua | 1 + src/main.lua | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/control.lua b/control.lua index 5c6d772..620fdf9 100644 --- a/control.lua +++ b/control.lua @@ -37,6 +37,7 @@ script.on_load(on_load) script.on_configuration_changed(on_configuration_changed) script.on_event(defines.events.on_built_entity, on_built_entity) +script.on_event(defines.events.script_raised_built, script_raised_built) script.on_event(defines.events.on_entity_died, on_entity_died) script.on_event(defines.events.on_player_placed_equipment, on_player_placed_equipment) script.on_event(defines.events.on_player_removed_equipment, on_player_removed_equipment) diff --git a/src/main.lua b/src/main.lua index e42255d..27aacba 100644 --- a/src/main.lua +++ b/src/main.lua @@ -34,6 +34,7 @@ local fuel_tick local handle_train_state_change local on_entity_removed local on_train_removed +local on_entity_added local read_equipment local recover_energy local update_equipment @@ -333,15 +334,26 @@ on_entity_removed = function(entity) end end +on_entity_added = function(entity) + if(entity and entity.valid) then + if(vehicle_types[entity.type] and entity.grid) then + global.vehicles[entity.unit_number] = entity + update_equipment(entity.unit_number, entity.grid) + end + end +end + -- Event entry points ------------------------------------------------------------------------------- function on_built_entity(event) local entity = event.created_entity - if(vehicle_types[entity.type] and entity.grid) then - global.vehicles[entity.unit_number] = entity - update_equipment(entity.unit_number, entity.grid) - end + on_entity_added(entity) +end + +function script_raised_built(event) + local entity = event.entity -- script_raised has different structure than on_built + on_entity_added(entity) end function on_entity_died(event) From 0eb8ebd9c3fcd3cfea2ef465bf4c8cc6b74e1ff3 Mon Sep 17 00:00:00 2001 From: robot256 <> Date: Thu, 4 Apr 2019 01:11:18 -0400 Subject: [PATCH 2/2] Added handler for script_raised_destroy --- control.lua | 1 + src/main.lua | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/control.lua b/control.lua index 620fdf9..3c8a422 100644 --- a/control.lua +++ b/control.lua @@ -39,6 +39,7 @@ script.on_configuration_changed(on_configuration_changed) script.on_event(defines.events.on_built_entity, on_built_entity) script.on_event(defines.events.script_raised_built, script_raised_built) script.on_event(defines.events.on_entity_died, on_entity_died) +script.on_event(defines.events.script_raised_destroy, script_raised_destroy) script.on_event(defines.events.on_player_placed_equipment, on_player_placed_equipment) script.on_event(defines.events.on_player_removed_equipment, on_player_removed_equipment) script.on_event(defines.events.on_pre_player_mined_item, on_pre_player_mined_item) diff --git a/src/main.lua b/src/main.lua index 27aacba..3d6aaf7 100644 --- a/src/main.lua +++ b/src/main.lua @@ -347,19 +347,22 @@ end ------------------------------------------------------------------------------- function on_built_entity(event) - local entity = event.created_entity - on_entity_added(entity) + on_entity_added(event.created_entity) end function script_raised_built(event) - local entity = event.entity -- script_raised has different structure than on_built - on_entity_added(entity) + on_entity_added(event.entity) -- script_raised has different structure than on_built_entity end function on_entity_died(event) on_entity_removed(event.entity) end +function script_raised_destroy(event) + on_entity_removed(event.entity) +end + + function on_player_placed_equipment(event) for unit, entity in pairs(global.vehicles) do if(entity.valid) then