Skip to content

Instantly share code, notes, and snippets.

@xarses
Last active August 4, 2016 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xarses/022f1faecc79f2b8db3757461fe05ce8 to your computer and use it in GitHub Desktop.
Save xarses/022f1faecc79f2b8db3757461fe05ce8 to your computer and use it in GitHub Desktop.
--[[
Copyright (c) 2016, Andrew Woodward (xarses)
All rights reserved.
License: BSD-3-Caluse, See LICENSE
--]]
component = require('component')
event = require('event')
local buffer = nil
local turbines = {}
local chest = 1 --side=up
local turbine = 0 --side=down
local _version = '0.1.0'
-- this is the list of rotors that we will search the chests for
-- the top of the list is the least ideal, with the bottom being the most ideal
-- maxDamage isn't used currently and I'll probably drop the wind values.
-- you can find the item name by using component.inventory_controller.getStackInSlot
-- or tools like minetweaker's /mthand
local rotors = {
{name="IC2:itemwoodrotor", maxDamage=10800, windMin=10, windMax=60},
{name="IC2:itemironrotor", maxDamage=86400, windMin=14, windMax=75},
{name="IC2:itemsteelrotor", maxDamage=172800, windMin=17, windMax=90},
{name="IC2:itemwcarbonrotor", maxDamage=604800, windMin=20, windMax=120},
}
function checkTurbine(inv, side)
return inv.getStackInSlot(side, 1)
end
debugLevel=true
function debug(...)
if debugLevel then print(...) end
end
local _itemCache ={}
function findItemInSide(inv, side, name, slot)
cacheKey = inv.address .. '_' .. side
local cache = nil
local slot = slot or nil
if _itemCache[cacheKey] then
cache = _itemCache[cacheKey]
if not slot then
for k,v in pairs(cache) do
if v.name == name then
slot = k
debug("findItemInSide cache hit")
break
end
end
end
else
_itemCache[cacheKey] = {}
cache = _itemCache[cacheKey]
debug("findItemInSide: new cache", cacheKey)
end
if slot then
item = inv.getStackInSlot(side, slot)
if item and item.name == name then
debug("findItemInSide: item confirmed in slot from cache. \\o/", slot)
return slot, item
else
debug("findItemInSide: item was in cache, but not what we wanted")
end
else
debug("findItemInSide cache miss")
end
-- lets scan the whole inventory again
for i=1, inv.getInventorySize(side) do
item = inv.getStackInSlot(side, i)
cache[i]=item
if item and item.name == name then
debug("findItemInSide: we found the item searching for it the hard way", slot)
return i, item
end
end
debug("findItemInSide: 404, item not found. Ejecting Cache")
_itemCache[cacheKey] = {}
return nil
end
local tierHandle = {}
function tierHandle.next(self)
local index = self.index + 1
return self.map[index], index
function tierHandle.prev(self)
local index = self.index - 1
return self.map[index], index
function tierHandle._setIndex(self,index)
self.index = index
self.cur = map[index]
return self.cur self.index
function tierHandle.set(self, name)
for k,v in ipairs(self.map) do
if v.name == name then
return self:_setIndex(k)
end
end
-- history, keep track of promote and fall back
function tierHandle.new(self, map, item)
obj = {index=0, steps=0, cur=nil, map=map or rotors
}
tierHandle.__index = tierHandle
setmetatable(obj,tierHandle)
if item then
obj:set(item)
end
return obj
end
function getRotor(inv, source, dest)
-- X if we don't have a rotor, we don't care and just grab any
-- X if we aren't on the max tier we can upgrade if any are present
-- X if we aren't taking damage, then we should downgrade
-- X if we are on wood, and not taking damage, then we have no wind
-- if keep upgrading, and then the next cycle downgrades, we shoudn't upgrade
-- again for a bit
-- X we need to skip missing items to upgrade
-- we should update our status on the display
-- we need to keep track of the rotors health, even if we put it away
-- we need to calculate expected modified runtime based on ^ ticks
local turbine = checkTurbine(inv, dest)
local tracking = inv.tracking
local wasDamaged = false
if tracking and turbine then
if turbine.name == tracking.name then
debug("tracking:", tracking.damage, tracking.avg, tracking.last, "turbine:", turbine.damage)
tracking.avg = tracking.damage + turbine.damage or 0 / 2
tracking.last = tracking.damage
tracking.damage = turbine.damage
if tracking.damage > tracking.last then
wasDamaged = true
end
else
-- somehow, this isn't the right turbine
inv.tracking = turbine
inv.tracking.last = turbine.damage
debug("Something was moved, we will try again next time")
return
end
end
local getNext = nil
if turbine then
if wasDamaged then
getNext = nextRotorTier
debug("Looking for higher rotor")
else
getNext = prevRotorTier
debug("Looking for lower rotor")
end
else
getNext = nextRotorTier
debug("Looking for any rotor (higher)")
end
local rotor = {}
if turbine then rotor.name = turbine.name end
repeat
rotor = getNext(rotor.name)
if rotor == last then
break -- we cant move any more
elseif turbine and rotor.name == turbine.name then
debug("Well that would have been silly, you wanted the same one", rotor.name)
if getNext == prevRotorTier then
debug("No wind to move rotor", turbine.name)
return
end
end
last = rotor
slot, item = findItemInSide(inv, source, rotor.name)
until slot
if slot then
if turbine then
if turbine.name ~= item.name then
-- git rid of whatever is there
inv.transferItem(dest, source)
else
debug("You wanted me to switch the the rotor, for the same type? No.")
return
end
end
if inv.transferItem(source, dest, 1, slot) then
inv.tracking = item
debug("We found one and put it in", item.name)
return
else
debug("uhh, something is wrong, it doesn't fit...", item.name)
end
else
debug("we went hunting, but was unable to find", rotor.name)
-- TODO: this needs to be extended to call for an item from deep storage
end
end
function transfer(inv, ...)
arg = {...}
if checkTurbine(inv, arg[1]) then
inv.tracking = nil
return inv.transferItem(table.unpack(arg))
end
end
function loop(invs, func, arg)
for _,v in pairs(invs) do
debug("in looper: ", v.address)
func(v, table.unpack(arg))
end
end
function findUsableComponent(allowed)
for _,v in ipairs(allowed) do
for addr, name in pairs(component.list(v)) do
if addr then
return component.proxy(addr)
end
end
end
end
function monitor(invs, breaker, buffer, chestSide, turbineSide)
local chestSide = chestSide or 1
local turbineSide = turbineSide or 0
local invs = invs or component.list('transposer')
for k,_ in pairs(invs) do
invs[k] = component.proxy(k)
debug(k)
end
local buffer = buffer or findUsableComponent{'batbox', 'cesu', 'mfe', 'mfsu'}
local breaker = breaker or function () return event.pull(10, 'interrupted') end
while not breaker() do
debug("loop")
if buffer.getStored() < buffer.getCapacity() then
-- we want the rotors in
debug("need charge", buffer.getStored())
loop(invs, getRotor, {chestSide, turbineSide})
else
debug("no charge, killing rotors", buffer.getStored())
loop(invs, transfer, {turbineSide, chestSide})
end
end
end
monitor()
Copyright (c) 2016, Andrew Woodward (xarses)
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment