Skip to content

Commit

Permalink
* Correct an issue where power wasn't properly being split between mu…
Browse files Browse the repository at this point in the history
…ltiple targets
  • Loading branch information
avosirenfal committed Apr 9, 2017
1 parent aae9c8d commit f9fdbcb
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 16 deletions.
42 changes: 27 additions & 15 deletions Localized_Charging_1.0.0/control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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']},
Expand Down Expand Up @@ -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']},
Expand Down Expand Up @@ -178,33 +178,43 @@ 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
local power_info = charging_info[tower.name]
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Localized_Charging_1.0.0/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
38 changes: 38 additions & 0 deletions Localized_Charging_1.0.0/prototypes/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
},
})

0 comments on commit f9fdbcb

Please sign in to comment.