Created
May 19, 2021 12:50
-
-
Save Krutoy242/c845fb938e121e0a2818d32dfd24e079 to your computer and use it in GitHub Desktop.
Auto Trader - OpenComputers Robot Program for Villagers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
Auto Trader | |
Robot program to trade with villagers | |
Setup: | |
- Robot on top of inventory (i use Storage Drawers) | |
- External power source for Robot | |
- Villagers around it (<6 blocks as default) | |
Robot: | |
- Tier3+ Ram (when working with huge inventories) | |
- Trading Upgrade | |
- Inventory Upgrade | |
- Inventory Controller Upgrade | |
Usage: | |
- Specify Item Names in BUY map | |
- Lunch program | |
]] | |
-- Item Names that should always bought | |
local BUY = { | |
['Emerald'] = true, | |
['Simmering Comb'] = true, | |
['Silky Comb'] = true, | |
['Apiary'] = true, | |
} | |
local sides = require'sides' | |
local robot_api = require'robot' | |
local component = require'component' | |
local robot = component.robot | |
local trade = component.trading | |
local inventory = setmetatable({}, { __index = function(self, key) | |
return function(...) return component.inventory_controller[key](sides.down, ...) end | |
end }) | |
local hashed = {} | |
local needHash = {} | |
local outputHash = {} | |
local inventory_size, err = inventory.getInventorySize() | |
if err then print(err); return end | |
function hashInventory() | |
io.write('\nhashing') | |
hashed = {} | |
needHash = {} | |
outputHash = {} | |
for i=1,inventory_size,1 do | |
local item = inventory.getStackInSlot(i) | |
if item then | |
local hash = hashed[item.label] or {} | |
hashed[item.label] = hash | |
hash.slots = hash.slots or {} | |
table.insert(hash.slots, i) | |
hash.size = hash.size or 0 | |
hash.size = hash.size + item.size | |
io.write('.') | |
end | |
end | |
io.write('done\n') | |
end | |
function need(label) | |
if needHash[label] then return false end | |
needHash[label] = true | |
print('need:', label) | |
return true | |
end | |
function suckItems(input) | |
if not input then return true end | |
local hash = hashed[input.label] | |
if not hash or hash.size < input.size then | |
need(input.label) | |
return | |
end | |
for k,i in pairs(hash.slots) do | |
local count = inventory.getSlotStackSize(i) | |
if count >= input.size then | |
local item = inventory.getStackInSlot(i) | |
if item and item.label == input.label then | |
local succes = inventory.suckFromSlot(i, input.size) | |
if succes then | |
hash.size = hash.size - input.size | |
return true | |
end | |
end | |
end | |
end | |
need(input.label) | |
end | |
function clearInventory() | |
for i=1,robot.inventorySize(),1 do | |
if robot.count(i) > 0 then | |
robot.select(i) | |
robot_api.dropDown() | |
end | |
end | |
robot.select(1) | |
end | |
function safeCheck(item, field, value) | |
return not item or item[field] == value | |
end | |
function safeNotCheck(item, field, value) | |
return not item or item[field] ~= value | |
end | |
function checkBuy(in1, in2, output) | |
return BUY[output.label] | |
or (safeNotCheck(in1, 'label', 'Emerald') and safeNotCheck(in2, 'label', 'Emerald')) | |
-- or (safeCheck(in1, 'size', 1) and safeCheck(in2, 'size', 1)) | |
end | |
function doTrade(trade) | |
local in1, in2 = trade.getInput() | |
if in1 == nil and in2 == nil then return end | |
local output = trade.getOutput() | |
if output == nil then return end | |
if not outputHash[output.label] then | |
outputHash[output.label] = true | |
end | |
if checkBuy(in1, in2, output) then | |
while true do | |
if not suckItems(in1) or not suckItems(in2) then return end | |
local succes, err = trade.trade() | |
clearInventory() | |
if not succes then return false end | |
end | |
end | |
end | |
while true do | |
hashInventory() | |
for index,trade in pairs(trade.getTrades()) do | |
if type(trade)=="table" and trade.isEnabled() then | |
doTrade(trade) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment