From f9fdbcb457f0df39f584d3d7c83df9a2c77a3304 Mon Sep 17 00:00:00 2001 From: Sirenfal Date: Sun, 9 Apr 2017 14:24:13 -0400 Subject: [PATCH] * Correct an issue where power wasn't properly being split between multiple targets --- Localized_Charging_1.0.0/control.lua | 42 ++++++++++++------- Localized_Charging_1.0.0/info.json | 2 +- Localized_Charging_1.0.0/prototypes/items.lua | 38 +++++++++++++++++ 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/Localized_Charging_1.0.0/control.lua b/Localized_Charging_1.0.0/control.lua index e723c54..26f29c3 100644 --- a/Localized_Charging_1.0.0/control.lua +++ b/Localized_Charging_1.0.0/control.lua @@ -110,7 +110,7 @@ script.on_event(defines.events.on_tick, function(event) } for name, info in pairs(charging_info) do - for _, tower in pairs(game.surfaces[1].find_entities_filtered{ + for _, tower in pairs(vehicle.surface.find_entities_filtered{ area={ {vehicle.position['x'] - info['range'], vehicle.position['y'] - info['range']}, {vehicle.position['x'] + info['range'], vehicle.position['y'] + info['range']}, @@ -149,7 +149,7 @@ script.on_event(defines.events.on_tick, function(event) } for name, info in pairs(charging_info) do - for _, tower in pairs(game.surfaces[1].find_entities_filtered{ + for _, tower in pairs(player.surface.find_entities_filtered{ area={ {player.position['x'] - info['range'], player.position['y'] - info['range']}, {player.position['x'] + info['range'], player.position['y'] + info['range']}, @@ -178,8 +178,8 @@ script.on_event(defines.events.on_tick, function(event) local item = table.remove(items, 1) if(item == nil) then -- wat - game.print('Voltage: this should never happen: no chargable items?') - error('Voltage: this should never happen: no chargable items?') + game.print('Localized Charging: this should never happen: no chargable items?') + error('Localized Charging: this should never happen: no chargable items?') end for _, tower in pairs(info.towers) do @@ -187,24 +187,34 @@ script.on_event(defines.events.on_tick, function(event) local interface = global.interfaces[tower.unit_number] -- lightning effect - game.surfaces[1].create_entity{ + tower.surface.create_entity{ name = "charging-charge", position = {tower.position['x'], tower.position['y'] - 1.5}, force = info.ent.force, target = info.ent } - repeat - local s = split[tower.unit_number] + local s = split[tower.unit_number] - if(s == nil or s < 1) then - error('Voltage: invalid tower split: ' .. tostring(s)) - end + if(s == nil or s < 1) then + error('Localized Charging: invalid tower split: ' .. tostring(s)) + end + + -- use ceil here to counter float imprecision + local transfer = interface.energy / s --math.ceil(math.min(item.max_energy - item.energy, interface.energy / s)) + + -- we've partitioned off the energy we need + split[tower.unit_number] = split[tower.unit_number] - 1 - -- use ceil here to counter float imprecision - local transfer = math.ceil(math.min(item.max_energy - item.energy, interface.energy / s)) - interface.energy = interface.energy - transfer + repeat + local spent = item.energy item.energy = item.energy + math.ceil(transfer * power_info.efficiency) + spent = (item.energy - spent) * (1 / power_info.efficiency) + + -- game.print(string.format('transferring %f to %i (split: %i, pool: %f, interface: %f)', spent, info.ent.unit_number, s, transfer, interface.energy)) + + transfer = transfer - spent + interface.energy = interface.energy - spent if(item.max_energy - item.energy < 1) then -- charge the next item @@ -215,19 +225,21 @@ script.on_event(defines.events.on_tick, function(event) break end end - until interface.energy < 1 + until transfer < 1 if(item == nil) then break end end end + + -- game.print('-----') end) local function on_entity_created(entity) if(charging_info[entity.name] ~= nil) then local uid = entity.unit_number - local interface = game.surfaces[1].create_entity{ + local interface = entity.surface.create_entity{ name = "localized-charging-pole-interface", position = {entity.position['x'], entity.position['y']}, -- stop ai in everything mode from attacking this invulnerable building diff --git a/Localized_Charging_1.0.0/info.json b/Localized_Charging_1.0.0/info.json index 7e6a568..00b7b8e 100644 --- a/Localized_Charging_1.0.0/info.json +++ b/Localized_Charging_1.0.0/info.json @@ -4,7 +4,7 @@ "version": "1.0.0", "title": "Localized_Charging", "author": "Sirenfal", - "description": "Wireless electricity mod", + "description": "A charging mod that will recharge batteries in vehicles and power armor when near a charging pole.", "dependencies": ["base >= 0.14.0"], "factorio_version": "0.14.0" } diff --git a/Localized_Charging_1.0.0/prototypes/items.lua b/Localized_Charging_1.0.0/prototypes/items.lua index c9c0eab..3f6299f 100644 --- a/Localized_Charging_1.0.0/prototypes/items.lua +++ b/Localized_Charging_1.0.0/prototypes/items.lua @@ -19,4 +19,42 @@ data:extend({ order = "a[train-system]-f[electric-locomotive]", place_result = "electric-locomotive", }), + + { + type = "item", + name = "test-battery-please-ignore", + icon = "__base__/graphics/icons/battery-mk2-equipment.png", + placed_as_equipment_result = "test-battery-please-ignore", + flags = {"goes-to-main-inventory"}, + subgroup = "equipment", + order = "c[battery]-b[test-battery-please-ignore]", + stack_size = 50, + default_request_amount = 10 + }, + { + type = "battery-equipment", + name = "test-battery-please-ignore", + sprite = + { + filename = "__base__/graphics/equipment/battery-mk2-equipment.png", + width = 32, + height = 64, + priority = "medium" + }, + shape = + { + width = 1, + height = 2, + type = "full" + }, + energy_source = + { + type = "electric", + buffer_capacity = "30GJ", + input_flow_limit = "1GW", + output_flow_limit = "1GW", + usage_priority = "terciary" + }, + categories = {"armor"} + }, }) \ No newline at end of file