Skip to content

Commit

Permalink
wireless charger and FTL plug
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaPRIME committed Apr 30, 2019
1 parent 8db7aa8 commit c4643e4
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 1 deletion.
4 changes: 3 additions & 1 deletion StarTech/startech/objects/power/battery.t1.object
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"startech:augpack",
"startech:nanofield",
"startech:autosmelter",
"startech:chargingbench"
"startech:chargingbench",
"startech:wirelesscharger",
"startech:ftlplug"
],

"iconBaseImage" : "battery.frame.png",
Expand Down
22 changes: 22 additions & 0 deletions StarTech/startech/objects/power/ftlplug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--

require "/lib/stardust/power.lua"

shared.energyReceptor = { }

function init()
rate = config.getParameter("conversionRate")
end

function shared.energyReceptor:receive(socket, amount, testOnly)
local fuel = world.getProperty("ship.fuel")
local maxFuel = world.getProperty("ship.maxFuel")
if type(fuel) ~= "number" or type(maxFuel) ~= "number" then return 0 end -- inactive on non-ship worlds
local result = math.min(amount, (maxFuel - fuel) * rate)
if not testOnly then -- commit
world.setProperty("ship.fuel", math.min(fuel + (result / rate), maxFuel))
end
return result
end

--
31 changes: 31 additions & 0 deletions StarTech/startech/objects/power/ftlplug.object
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{ // -*- grammar-ext: json -*-
"objectName" : "startech:ftlplug",
"rarity" : "Legendary",

"shortdescription" : "Erchius Resonator",
"description" : "Fuels a ship's FTL drive with fluxpulse. ^yellow;Power hungry.^reset;",
"category" : "power.transport",
"price" : 500,
"health" : 5.73,

"inventoryIcon" : [
{ "image" : "ftlplug.png" }
],

"orientations" : [
{
"image" : "ftlplug.png",
"imagePosition" : [-8, -8],

"spaces" : [ [0, 0], [-1, 0], [1, 0], [0, -1], [0, 1] ],
"anchors" : [ "background" ]
}
],

"scripts" : [ "ftlplug.lua" ],
"scriptDelta" : 0,

"conversionRate" : 5000, // FP per fuel unit

"inputNodes" : [ [0, 0] ]
}
Binary file added StarTech/startech/objects/power/ftlplug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions StarTech/startech/objects/power/ftlplug.recipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ // -*- grammar-ext: json -*-
"input" : [
{ "item" : "durasteelbar", "count" : 15 },
{ "item" : "silverbar", "count" : 5 },
{ "item" : "startech:resonitebar", "count" : 1 },
{ "item" : "supermatter", "count" : 5 },
{ "item" : "liquidfuel", "count" : 250 }
],
"output" : { "item" : "startech:ftlplug", "count" : 1 },
"duration" : 1,
"groups" : [ "startech:tinkertable", "power", "all" ]
}
Binary file added StarTech/startech/objects/power/ftlplug.xcf
Binary file not shown.
50 changes: 50 additions & 0 deletions StarTech/startech/objects/power/wirelesscharger.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--

require "/lib/stardust/prefabs.lua"
require "/lib/stardust/power.lua"
require "/lib/stardust/network.lua"

iou = { } -- table of promises for sent power
commit = 0 -- how much of the battery has been committed
baseDelta = 15 -- ticks between updates while active
idleDelta = 60 -- ticks between updates while idle (no charge)

function init()
local cfg = config.getParameter("batteryStats")
battery = prefabs.power.battery(cfg.capacity, cfg.ioRate):hookUp():autoSave()
range = config.getParameter("tileRange")
packetSize = cfg.ioRate
script.setUpdateDelta(baseDelta)
end

function update()
local active = false
for k, p in pairs(iou) do
if p:finished() then
if p:succeeded() then
local taken = p:result()
battery.state.energy = battery.state.energy - taken
if taken > 0 then active = true end
end
commit = commit - packetSize
iou[k] = nil
end
end

if battery.state.energy > commit + packetSize then -- only if more exists than committed
local pl = world.playerQuery(entity.position(), range)
for _, p in pairs(pl) do
local ps = math.min(battery.state.energy - commit, packetSize)
if ps <= 0 then break end -- no energy to give
--active = true
totalGiven = ps
commit = commit + ps
iou[{ }] = world.sendEntityMessage(p, "playerext:fillEquipEnergyAsync", ps, baseDelta)
end
end

-- slow updates when not actively charging
script.setUpdateDelta(active and baseDelta or idleDelta)

--object.say(string.format("energy: %0.2f\ncommit: %0.2f\nactive: %s", battery.state.energy, commit, active and "y" or "n"))
end
35 changes: 35 additions & 0 deletions StarTech/startech/objects/power/wirelesscharger.object
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{ // -*- grammar-ext: json -*-
"objectName" : "startech:wirelesscharger",
"rarity" : "Legendary",

"shortdescription" : "Resonite Transmitter",
"description" : "Charges nearby players' worn fluxpulse equipment wirelessly, in a radius of ^green;25m^reset;.",
"category" : "power.transport",
"price" : 500,
"health" : 5.73,

"inventoryIcon" : [
{ "image" : "wirelesscharger.png" }
],

"orientations" : [
{
"image" : "wirelesscharger.png",
"imagePosition" : [-8, -8],

"spaces" : [ [0, 0], [-1, 0], [1, 0], [0, -1], [0, 1] ],
"anchors" : [ "background" ]
}
],

"scripts" : [ "wirelesscharger.lua" ],
"scriptDelta" : 5,

"batteryStats" : {
"capacity" : 25000,
"ioRate" : 1000
},
"tileRange" : 50,

"inputNodes" : [ [0, 0] ]
}
Binary file added StarTech/startech/objects/power/wirelesscharger.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions StarTech/startech/objects/power/wirelesscharger.recipe
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ // -*- grammar-ext: json -*-
"input" : [
{ "item" : "durasteelbar", "count" : 15 },
{ "item" : "silverbar", "count" : 5 },
{ "item" : "wire", "count" : 25 },
{ "item" : "startech:resonitebar", "count" : 1 },
{ "item" : "supermatter", "count" : 1 }
],
"output" : { "item" : "startech:wirelesscharger", "count" : 1 },
"duration" : 1,
"groups" : [ "startech:tinkertable", "power", "all" ]
}
Binary file not shown.
1 change: 1 addition & 0 deletions StardustLib/lib/stardust/playerext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ if not playerext then

"fillEquipEnergy",
"drawEquipEnergy",
"fillEquipEnergyAsync",

"getTechOverride",
"overrideTech",
Expand Down
8 changes: 8 additions & 0 deletions StardustLib/sys/stardust/playerext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ end

function svc.fillEquipEnergy(msg, isLocal, amount, testOnly) return power.fillEquipEnergy(amount, testOnly) end
function svc.drawEquipEnergy(msg, isLocal, amount, testOnly) return power.drawEquipEnergy(amount, testOnly) end
function svc.fillEquipEnergyAsync(msg, isLocal, amount, iterations)
local acc = 0
for i = 1, iterations do
acc = acc + power.fillEquipEnergy(amount - acc)
if acc >= amount then break end
end
return acc
end

-- read/write equipped items, generally meant to be used synchronously (from techs, etc.)
function svc.getEquip(msg, isLocal, slot)
Expand Down

0 comments on commit c4643e4

Please sign in to comment.