Skip to content

Instantly share code, notes, and snippets.

@auxiliary-character
Last active December 19, 2015 13:08
Show Gist options
  • Save auxiliary-character/5959523 to your computer and use it in GitHub Desktop.
Save auxiliary-character/5959523 to your computer and use it in GitHub Desktop.
--auxchar's ComputerCraft Trade Script--
--Edit These.
tradeName = "Example Trade"
supplyChestName = "diamond_chest_1" --name the upper chest gives you when you right click on its modem.
currencyChestName = "diamond_chest_0" --same, but for the lower chest
price = 1 --These need to be whole numbers.
payOut = 1
--You may or may not need to chaange these...
payOutStackSize = 64
currencyId = 21256
currencyDamage = 1
currencyStackSize = 64
--Don't change these unless you know what you're doing.
modemDirection = "right"
tradeChestDirection = "left"
currencyChestDirection = "down"
supplyChestDirection = "down"
--From here on down, it's just code. You shouldn't have to care what lies beyond this point.
function remoteWrap(side, name)
local m = peripheral.wrap(side)
local remotePeripheral = {
wrapData = {
modem = m,
peripheralName = name
}
}
local meta = {
__index = function(t,k)
return function(...)
local wrapData = t.wrapData
return wrapData.modem.callRemote(wrapData.peripheralName, k, ...)
end
end
}
setmetatable(remotePeripheral,meta)
return remotePeripheral
end
function stackCheck(id,damage,stack)
local idCheck
local damageCheck
if id then
idCheck = (id == stack.id)
else
idCheck = true
end
if damage then
damageCheck = (damage == stack.dmg)
else
damageCheck = true
end
return damageCheck and idCheck
end
function pushAmount(chest,amount,direction,...)
local id = arg[1]
local damgae = arg[2]
local pushed = 0
for i = 0,chest.getSizeInventory()-1 do
stack = chest.getStackInSlot(i)
if stack then
if stackCheck(id,damage,stack) then
if pushed < amount then
pushed = pushed + chest.push(direction, i, amount-pushed)
end
if pushed == amount then
break
end
end
end
sleep(.02)
end
return pushed
end
function getAmount(chest,...)
local id = arg[1]
local damage = arg[2]
local found = 0
for i = 0,chest.getSizeInventory()-1 do
stack = chest.getStackInSlot(i)
if stack then
if stackCheck(id,damage,stack) then
found = found + stack.qty
end
end
sleep(.02)
end
return found
end
function getSpace(chest, stackSize,...)
local id = arg[1]
local damage = arg[2]
local found = 0
for i = 0,chest.getSizeInventory()-1 do
stack = chest.getStackInSlot(i)
if stack then
if stack.id == id and stack.damage == damage then
found = found + stackSize - stack.qty
end
else
found = found + stackSize
end
sleep(.02)
end
return found
end
-- Main stuff
supplyChest = remoteWrap(modemDirection,supplyChestName)
currencyChest = remoteWrap(modemDirection,currencyChestName)
tradeChest = peripheral.wrap(tradeChestDirection)
function exchange(trades)
pushAmount(tradeChest, price*trades, currencyChestDirection, currencyId, currencyDamage)
pushAmount(supplyChest, payOut*trades, supplyChestDirection)
end
function trade()
limits = {
math.floor(getAmount(supplyChest)/payOut),
math.floor(getSpace(currencyChest,currencyStackSize)/price),
math.floor(getSpace(tradeChest, payOutStackSize)/payOut),
math.floor(getAmount(tradeChest, currencyId, currencyDamage)/price)
}
table.sort(limits, function(a,b) return a<b end)
exchange(limits[1])
end
while true do
for i = 0,tradeChest.getSizeInventory()-1 do
stack = tradeChest.getStackInSlot(i)
if stack then
if stack.name == tradeName then
trade()
sleep(1)
break
end
end
sleep(.02)
end
sleep(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment