-- Loop over each turret we are adding energy to for _, v in pairs{data.raw["artillery-turret"]["artillery-turret"], data.raw["fluid-turret"]["flamethrower-turret"]} do data:extend{{ type = "electric-energy-interface", name = v.name.."-power", icon = v.icon, icon_size = v.icon_size, icon_mipmaps = v.icon_mipmaps, flags = {"player-creation", "not-deconstructable","not-blueprintable"}, max_health = 1, resistances = resistances_immune, collision_box = v.collision_box, selection_box = v.selection_box, drawing_box = v.drawing_box, selectable_in_game = false, energy_source = { type = "electric", buffer_capacity = "800kJ", input_flow_limit = "100kW", usage_priority = "primary-input", drain = "10kW" }, energy_production = "0W" }} end -- tbl[field] should be nil, a single Trigger, or an array of Trigger's -- (Also works for TriggerDelivery's, and TriggerEffect's) -- get_trigger will either return on of the triggers, or return default_trigger, after storing it in tbl[field] function get_trigger(tbl, field, default_trigger) if not tbl[field] then -- no Triggers tbl[field] = default_trigger return tbl[field] elseif tbl[field].type then -- not an array return tbl[field] elseif #tbl[field] > 0 then -- non-empty array of Triggers return tbl[field][#tbl[field]] -- use the last Trigger else -- should be an empty array table.insert(tbl[field], default_trigger) return tbl[field][1] end end function insert_trigger(tbl, field, new_trigger) if not tbl[field] then tbl[field] = new_trigger elseif tbl[field].type then -- not an array, turn it into one tbl[field] = {tbl[field], new_trigger} else -- should be an array tbl.insert(tbl[field], new_trigger) end return new_trigger end for _, ammo in pairs{data.raw["ammo"]["artillery-shell"], data.raw["fluid-turret"]["flamethrower-turret"].attack_parameters} do if true then -- option 1 local action = get_trigger(ammo.ammo_type, "action", {type = "direct"}) local action_delivery = get_trigger(action, "action_delivery", {type = "instant"}) insert_trigger(action_delivery, "source_effects", {type = "script", effect_id = "turret-consume-energy"}) else -- option 2 local action = insert_trigger(ammo.ammo_type, "action", {type = "direct"}) local action_delivery = insert_trigger(action, "action_delivery", {type = "instant"}) insert_trigger(action_delivery, "source_effects", {type = "script", effect_id = "turret-consume-energy"}) end end