Advertisement
UNOBTANIUM

TurtleTradingStationTurtle

May 15th, 2013
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.90 KB | None | 0 0
  1. local version = "Turtle Trading Station Beta 0.3.0"
  2. local working = false
  3. local trade = 0
  4. local maxChestSlots = 27
  5. local turtleID = os.getComputerID()
  6. local computerID = 1
  7. local chest = {}
  8. w,h = term.getSize()
  9. -- chest[left|right][Name|Amount][x][y]
  10. for a=1,2 do
  11.  chest[a] = {}
  12.  for b=1,2 do
  13.   chest[a][b] = {}
  14.   for c=1,4 do
  15.    chest[a][b][c] = {}
  16.    for d=1,4 do
  17.     if b == 1 then
  18.      chest[a][b][c][d] = "ttsEmpty"
  19.     else
  20.      chest[a][b][c][d] = -1
  21.     end
  22.    end
  23.   end
  24.  end
  25. end
  26.  
  27. rednet.open("right")
  28.  
  29.  
  30. function printCentered(str, ypos)
  31.  term.setCursorPos(w/2 - #str/2, ypos)
  32.  term.write(str)
  33. end
  34.  
  35. function printCopyright()
  36.  local str = "by UNOBTANIUM"
  37.  term.setCursorPos(w-#str, h)
  38.  term.write(str)
  39. end
  40.  
  41. function printHeader(title, line)
  42.  printCentered(title, line)
  43.  printCentered(string.rep("-", w), line+1)
  44. end
  45.  
  46. function printReplace(str,line)
  47.  printCentered(string.rep(" ", w), line)
  48.  printCentered(str, line)
  49. end
  50.  
  51. function clearScreen()
  52.  term.clear()
  53.  term.setCursorPos(1,1)
  54.  term.clear()
  55. end
  56.  
  57. local function transfer(i)
  58.  if i == 2 then
  59.   return 1
  60.  elseif i == 3 then
  61.   return 2
  62.  elseif i == 4 then
  63.   return 3
  64.  elseif i == 5 then
  65.   return 4
  66.  elseif i == 6 then
  67.   return 5
  68.  elseif i == 7 then
  69.   return 6
  70.  elseif i == 8 then
  71.   return 7
  72.  elseif i == 9 then
  73.   return 8
  74.  elseif i == 9 then
  75.   return 9
  76.  elseif i == 10 then
  77.   return 0
  78.  elseif i == 17 then
  79.   return "w"
  80.  elseif i == 30 then
  81.   return "a"
  82.  elseif i == 31 then
  83.   return "s"
  84.  elseif i == 32 then
  85.   return "d"
  86.  elseif i == 28 then
  87.   return "enter"
  88.  elseif i == 14 then
  89.   return "backspace"
  90.  else
  91.   return ""
  92.  end
  93. end
  94.  
  95. function countArray(array)
  96.  local x = 0
  97.  for k,v in pairs(array) do
  98.   x = x + 1
  99.  end
  100.  return x
  101. end
  102.  
  103. function countItems(first, last)
  104.  local amount = 0
  105.  for i=first,last do
  106.   amount = amount + turtle.getItemCount(i)
  107.  end
  108.  return amount
  109. end
  110.  
  111. function suckDownSlots(first, last)
  112.  for i=first,last do
  113.   if turtle.getItemCount(i) == 0 then
  114.    turtle.select(i)
  115.    turtle.suckDown()
  116.   end
  117.  end
  118. end
  119.  
  120. function suckSlots(first, last)
  121.  for i=first,last do
  122.   if turtle.getItemCount(i) == 0 then
  123.    turtle.select(i)
  124.    turtle.suck()
  125.   end
  126.  end
  127. end
  128.  
  129. function dropDownSlots(first, last)
  130.  for i=first,last do
  131.   if turtle.getItemCount(i) > 0 then
  132.    turtle.select(i)
  133.    turtle.dropDown()
  134.   end
  135.  end
  136. end
  137.  
  138. function dropSlots(first, last)
  139.  for i=first,last do
  140.   if turtle.getItemCount(i) > 0 then
  141.    turtle.select(i)
  142.    turtle.drop()
  143.   end
  144.  end
  145. end
  146.  
  147.  
  148. --MESSAGE
  149.  
  150. function sendMessage(sendingmessage)
  151.  sendingmessage = tostring(sendingmessage)
  152.  while true do
  153.   repeat
  154.    rednet.send(computerID,sendingmessage)
  155.    senderChannel, message, distance = rednet.receive(1)
  156.   until type(message) == "string"
  157.   if senderChannel == computerID then
  158.    sleep(0.1)
  159.    return
  160.   end
  161.  end
  162. end
  163.  
  164. function receiveMessage()
  165.  while true do
  166.   local senderChannel, receivedmessage = rednet.receive()
  167.   if senderChannel == computerID then
  168.    sleep(0.1)
  169.    rednet.send(computerID,"successful")
  170.    return receivedmessage
  171.   end
  172.  end
  173. end
  174.  
  175. function sendSuccess()
  176.  rednet.send(turtleID,"successful")
  177. end
  178.  
  179.  
  180. -- SAVE & LOAD
  181.  
  182. function loadVariables()
  183.  if not fs.exists("ttsVariables") then
  184.   return false
  185.  end
  186.  local file = fs.open("ttsVariables","r")
  187.  computerID = tonumber(file.readLine())
  188.  maxChestSlots = tonumber(file.readLine())
  189.  working = file.readLine()
  190.  for a=1,2 do
  191.   for b=1,2 do
  192.    for c=1,4 do
  193.     for d=1,4 do
  194.      if b == 1 then
  195.       chest[a][b][c][d] = file.readLine()
  196.      else
  197.       chest[a][b][c][d] = tonumber(file.readLine())
  198.      end
  199.     end
  200.    end
  201.   end
  202.  end
  203.  file.close()
  204.  return true
  205. end
  206.  
  207.  
  208. function saveVariables()
  209.  local file = fs.open("ttsVariables", "w")
  210.  file.writeLine(computerID)
  211.  file.writeLine(maxChestSlots)
  212.  file.writeLine(working)
  213.  for a=1,2 do
  214.   for b=1,2 do
  215.    for c=1,4 do
  216.     for d=1,4 do
  217.      file.writeLine(chest[a][b][c][d])
  218.     end
  219.    end
  220.   end
  221.  end
  222.  file.close()
  223. end
  224.  
  225. -- MOVEMENT
  226.  
  227. function moveToChest(side,x,y,action,slot,amount)
  228.  if action == "suck" or action == "drop" then
  229.   moveCoord(0,x-1,y-1,side)
  230.   if action == "suck" then
  231.    turtle.select(slot)
  232.    turtle.suck()
  233.    turtle.drop((0-amount)+turtle.getItemCount(slot))
  234.    chest[side][2][x][y] = chest[side][2][x][y] - amount
  235.   else
  236.    turtle.select(slot)
  237.    turtle.drop(amount)
  238.    chest[side][2][x][y] = chest[side][2][x][y] + amount
  239.   end
  240.   moveCoord(side,-(x-1),-(y-1),0)
  241.  elseif action == "get" then
  242.   local amountitems = 0
  243.   while amount > 0 do
  244.    moveCoord(0,x-1,y-1,side)
  245.    suckSlots(1,16)
  246.    amountitems = countItems(1,16)
  247.    while amountitems > amount do
  248.     local selectedslot = 1
  249.     while turtle.getItemCount(selectedslot) == 0 do
  250.      selectedslot = selectedslot + 1
  251.     end
  252.     turtle.select(selectedslot)
  253.     local amountDrop = 0
  254.     if (amountitems - turtle.getItemCount(selectedslot)) >= amount then
  255.      amountDrop = turtle.getItemCount(selectedslot)
  256.     else
  257.      for i=1, turtle.getItemCount(selectedslot) do
  258.       if amountitems-i == amount then
  259.        amountDrop = i
  260.        break
  261.       end
  262.      end
  263.     end
  264.     turtle.drop(amountDrop)
  265.     amountitems = amountitems - amountDrop  
  266.    end
  267.    moveCoord(side,-(x-1),-(y-1),0)
  268.    amount = amount - countItems(1,16)
  269.    dropSlots(1,16)
  270.   end
  271.  elseif action == "store" then
  272.   while amount > 0 do
  273.    print(amount .. "   " .. math.floor(amount/64))
  274.    suckDownSlots(1,16)
  275.    moveCoord(0,x-1,y-1,side)
  276.    print("Moving to chest to store...")
  277.    amount = amount - countItems(1,16)
  278.    dropSlots(1,16)
  279.    moveCoord(side,-(x-1),-(y-1),0)
  280.    print("Moving to base...")
  281.   end
  282.   print(amount .. "   " .. math.floor(amount/64))
  283.   print("Finished!")
  284.   sleep(1)
  285.  end
  286. end
  287.  
  288.  
  289. function moveCoord(side,x,y,sideend)
  290.  if side == 1 then
  291.   turtle.turnLeft()
  292.  elseif side == 2 then
  293.   turtle.turnRight()
  294.  end
  295.  if x > 0 then
  296.   for i=1,x do
  297.    while not turtle.back() do sleep(1) end
  298.   end
  299.  elseif x < 0 then
  300.   x = -x
  301.   for i=1,x do
  302.    while not turtle.forward() do sleep(1) end
  303.   end
  304.  end
  305.  
  306.  if y > 0 then
  307.   for i=1,y do
  308.    while not turtle.up() do sleep(1) end
  309.   end
  310.  elseif y < 0 then
  311.   y = -y
  312.   for i=1,y do
  313.    while not turtle.down() do sleep(1) end
  314.   end
  315.  end
  316.  
  317.  if sideend == 1 then
  318.   turtle.turnRight()
  319.  elseif sideend == 2 then
  320.   turtle.turnLeft()
  321.  end
  322. end
  323.  
  324. -- TRADE
  325.  
  326. function dropUntilNothingIsLeft()
  327.  dropSlots(2,16)
  328.  turtle.select(2)
  329.  repeat
  330.   turtle.drop()
  331.  until not turtle.suckDown()
  332.  
  333. end
  334.  
  335. function trade()
  336.  local offerItemName = receiveMessage()
  337.  local amountOffer = tonumber(receiveMessage())
  338.  local demandItemName = receiveMessage()
  339.  local amountDemand = tonumber(receiveMessage())
  340.  local side = 0
  341.  local chestX = 0
  342.  local chestY = 0
  343.  local Oside = 0
  344.  local OchestX = 0
  345.  local OchestY = 0
  346.  local timesOffer = 0
  347.  local timesDemand = 0
  348.  local insertDemand = 0
  349.  
  350.  for a=1,2 do
  351.   for c=1,4 do
  352.    for d=1,4 do
  353.     if chest[a][1][c][d] == demandItemName and side == 0 then
  354.      side = a
  355.      chestX = c
  356.      chestY = d
  357.     end
  358.    end
  359.   end
  360.  end
  361.  
  362.  for a=1,2 do
  363.   for c=1,4 do
  364.    for d=1,4 do
  365.     if chest[a][1][c][d] == offerItemName and Oside == 0 then
  366.      Oside = a
  367.      OchestX = c
  368.      OchestY = d
  369.     end
  370.    end
  371.   end
  372.  end
  373.  
  374.  if side == 0 or Oside == 0 then --error: no name found
  375.   sendMessage(-4)
  376.   return
  377.  end
  378.  
  379.  timesOffer = math.floor(chest[Oside][2][OchestX][OchestY] / amountOffer)
  380.  
  381.  if chest[side][1][chestX][chestY] == 0 or timesOffer == 0 then --no items left to trade with
  382.   sendMessage(-2)
  383.   return
  384.  end
  385.  
  386.  turtle.select(2)
  387.  if not turtle.suck() then
  388.   sendMessage(0)
  389.   return
  390.  end
  391.  
  392.  moveToChest(side,chestX,chestY,"suck",1,1)
  393.  local lastSlot = 17
  394.  local currentSlot = 2
  395.  turtle.select(currentSlot)
  396.  if not turtle.compareTo(1) then
  397.   lastSlot = lastSlot - 1
  398.   turtle.transferTo(lastSlot)
  399.  else
  400.   currentSlot = 3
  401.   turtle.select(currentSlot)
  402.  end
  403.  
  404.  while turtle.suck() do
  405.   if not turtle.compareTo(1) then
  406.    lastSlot = lastSlot - 1
  407.    turtle.select(currentSlot)
  408.    turtle.transferTo(lastSlot)
  409.   end
  410.   if lastSlot == 10 then
  411.    sendMessage(-1)
  412.    dropUntilNothingIsLeft()
  413.    moveToChest(side,chestX,chestY,"drop",1,1)
  414.    return
  415.   end
  416.   if lastSlot - 1 == currentSlot then
  417.    insertDemand = insertDemand + countItems(2,currentSlot)
  418.    dropDownSlots(2,currentSlot)
  419.    currentSlot = 2
  420.   else
  421.    currentSlot = currentSlot + 1
  422.   end
  423.   turtle.select(currentSlot)
  424.  end
  425.  
  426.  if lastSlot <= 16 then
  427.   dropSlots(lastSlot,16)
  428.  end
  429.  insertDemand = insertDemand + countItems(2,lastSlot-1)
  430.  
  431.  if insertDemand == 0 then
  432.   sendMessage(0)
  433.   moveToChest(side,chestX,chestY,"drop",1,1)
  434.   return
  435.  end
  436.  
  437.  timesDemand = math.floor(insertDemand / amountDemand)
  438.  local tradeTimes = 0
  439.  
  440.  local allmost = false
  441.  
  442.  if timesDemand == 0 then
  443.   sendMessage(-3)
  444.   dropUntilNothingIsLeft()
  445.   moveToChest(side,chestX,chestY,"drop",1,1)
  446.   return
  447.  elseif timesDemand <= timesOffer then
  448.   tradeTimes = timesDemand
  449.  elseif timesDemand > timesOffer then
  450.   allmost = true
  451.   tradeTimes = timesOffer
  452.  end
  453.  
  454.  while (chest[side][2][chestX][chestY] + (tradeTimes * amountDemand)) > (64*maxChestSlots) and tradeTimes >= 1 do
  455.   tradeTimes = tradeTimes - 1
  456.   allmost = true
  457.  end
  458.  
  459.  if tradeTimes == 0 then
  460.   sendMessage(-5)
  461.   dropUntilNothingIsLeft()
  462.   moveToChest(side,chestX,chestY,"drop",1,1)
  463.   return
  464.  end
  465.  
  466.  if allmost then
  467.   sendMessage(-6)
  468.  end
  469.  sendMessage(tradeTimes)
  470.  
  471.  if receiveMessage() == "trade canceled"  then
  472.   dropUntilNothingIsLeft()
  473.   moveToChest(side,chestX,chestY,"drop",1,1)
  474.   return
  475.  end
  476.  
  477.  dropDownSlots(1,16)
  478.  
  479.  local leftover = insertDemand - (tradeTimes*amountDemand)
  480.  turtle.select(1)
  481.  print("Leftover: " .. leftover)
  482.  local slotamount = 0
  483.  while leftover > 0 do
  484.   turtle.suckDown()
  485.   slotamount = turtle.getItemCount(1)
  486.   if slotamount <= leftover then
  487.    turtle.drop()
  488.    leftover = leftover - slotamount
  489.   else
  490.    for i=1,slotamount do
  491.     if (leftover - i) == 0 then
  492.      turtle.drop(i)
  493.      turtle.dropDown()
  494.      leftover = leftover - i
  495.      break
  496.     end
  497.    end
  498.   end
  499.  end
  500.  
  501.  moveToChest(Oside,OchestX,OchestY,"get",0,(tradeTimes*amountOffer))
  502.  chest[Oside][2][OchestX][OchestY] = chest[Oside][2][OchestX][OchestY] - (tradeTimes*amountOffer)
  503.  sendMessage("finished")
  504.  chest[side][2][chestX][chestY] = chest[side][2][chestX][chestY] + (tradeTimes*amountDemand) + 1
  505.  moveToChest(side,chestX,chestY,"store",0,(tradeTimes*amountDemand) + 1)
  506. end
  507.  
  508. -- NEW ITEM
  509.  
  510. function addNewItem()
  511.  turtle.select(1)
  512.  turtle.suck()
  513.  if turtle.getItemCount(1) == 0 then
  514.   sendMessage("Place a freaking item in the chest!!!")
  515.   return
  516.  elseif turtle.getItemCount(1) >= 2 then
  517.   sendMessage("Just one item...")
  518.   turtle.drop()
  519.   return
  520.  end
  521.  
  522.  local side = 0
  523.  local chestX = 0
  524.  local chestY = 0
  525.  for c=1,4 do
  526.   for d=1,4 do
  527.    for a=1,2 do
  528.     if chest[a][1][c][d] == "ttsEmpty" and side == 0 then
  529.      side = a
  530.      chestX = c
  531.      chestY = d
  532.     end
  533.    end
  534.   end
  535.  end
  536.  
  537.  if side == 0 then
  538.   sendMessage("All chests are full!")
  539.   turtle.drop()
  540.   return
  541.  end
  542.  
  543.  sendMessage("successful")
  544.  local name = receiveMessage()
  545.  chest[side][1][chestX][chestY] = name
  546.  moveToChest(side,chestX,chestY,"drop",1,1)
  547. end
  548.  
  549. -- GET INFO
  550.  
  551. function getInfo()
  552.  if turtle.getFuelLevel() < 500 then
  553.   sendMessage("Please refuel your turtle!")
  554.   sendMessage("Fuel Level: " .. turtle.getFuelLevel())
  555.   sendMessage(" ")
  556.   return
  557.  end
  558.  for c=1,4 do
  559.   for d=1,4 do
  560.    for a=1,2 do
  561.     if chest[a][2][c][d] >= 0 then
  562.      if chest[a][2][c][d] < 16 then
  563.       sendMessage("There are just " .. chest[a][2][c][d] .. " " .. chest[a][1][c][d] .. "s left.")
  564.       sendMessage("Please refill the chest, otherwise this item")
  565.       sendMessage("wont be available for any trades")
  566.       return
  567.      elseif chest[a][2][c][d] == 0 then
  568.       sendMessage("There are no " .. chest[a][1][c][d] .. "s left.")
  569.       sendMessage("Please refill the chest, otherwise this item")
  570.       sendMessage("wont be available for any trades")
  571.       return
  572.      elseif chest[a][2][c][d] == maxChestSlots*64 or chest[a][2][c][d] == maxChestSlots*16 then
  573.       sendMessage("The " .. chest[a][1][c][d] .. " chest might be full!")
  574.       sendMessage("Items in it: " .. chest[a][2][c][d])
  575.       sendMessage("Take some items out if possible.")
  576.       return
  577.      end
  578.     end
  579.    end
  580.   end
  581.  end
  582.  sendMessage("Turtle is fine!")
  583.  sendMessage(" ")
  584.  sendMessage(" ")
  585. end
  586.  
  587. -- GET AMOUNT ITEM
  588.  
  589. function getAmountItem()
  590.  local name = receiveMessage()
  591.  local amount = -1
  592.  for c=1,4 do
  593.   for d=1,4 do
  594.    for a=1,2 do
  595.     if chest[a][1][c][d] == name and amount == -1 then
  596.      amount = chest[a][2][c][d]
  597.     end
  598.    end
  599.   end
  600.  end
  601.  return amount
  602. end
  603.  
  604. -- GET ITEMS
  605.  
  606. function getItems()
  607.  local name = receiveMessage()
  608.  local amount = tonumber(receiveMessage())
  609.  local side = 0
  610.  local X = 0
  611.  local Y = 0
  612.  for c=1,4 do
  613.   for d=1,4 do
  614.    for a=1,2 do
  615.     if chest[a][1][c][d] == name and side == 0 then
  616.      side = a
  617.      X = c
  618.      Y = d
  619.     end
  620.    end
  621.   end
  622.  end
  623.  if side == 0 then
  624.   sendMessage("No items found!")
  625.   return
  626.  end
  627.  chest[side][2][X][Y] = chest[side][2][X][Y] - amount
  628.  moveToChest(side,X,Y,"get",0,amount)
  629.  sendMessage(" ")
  630. end
  631.  
  632. --REFILL CHEST
  633.  
  634. function refill()
  635.  local name = receiveMessage()
  636.  print("Refilling " .. name .. " chest!")
  637.  local side = 0
  638.  local chestX = 0
  639.  local chestY = 0
  640.  for c=1,4 do
  641.   for d=1,4 do
  642.    for a=1,2 do
  643.     if chest[a][1][c][d] == name and side == 0 then
  644.      side = a
  645.      chestX = c
  646.      chestY = d
  647.     end
  648.    end
  649.   end
  650.  end
  651.  
  652.  if side == 0 then
  653.   sendMessage("No items found!")
  654.   return
  655.  end
  656.  
  657.  print("Moving to chest...")
  658.  moveToChest(side,chestX,chestY,"suck",1,1)
  659.  local currentSlot = 2
  660.  local lastSlot = 17
  661.  local insertDemand = 0
  662.  turtle.select(currentSlot)
  663.  while turtle.suck() do
  664.   if not turtle.compareTo(1) then
  665.    lastSlot = lastSlot - 1
  666.    turtle.select(currentSlot)
  667.    turtle.transferTo(lastSlot)
  668.   end
  669.   if lastSlot == 10 then
  670.    sendMessage("Give me the right kind of item!!!")
  671.    dropUntilNothingIsLeft()
  672.    moveToChest(side,chestX,chestY,"drop",1,1)
  673.    return
  674.   end
  675.   if lastSlot -1 == currentSlot then
  676.    insertDemand = insertDemand + countItems(2,currentSlot)
  677.    dropDownSlots(2,currentSlot)
  678.    currentSlot = 2
  679.   else
  680.    currentSlot = currentSlot + 1
  681.   end
  682.   turtle.select(currentSlot)
  683.  end
  684.  print("Finished comparing.")
  685.  if lastSlot <= 16 then
  686.   dropSlots(lastSlot,16)
  687.  end
  688.  insertDemand = insertDemand + countItems(2,15)
  689.  
  690.  if insertDemand == 0 then
  691.   sendMessage("There are no items in the chest!!!")
  692.   moveToChest(side,chestX,chestY,"drop",1,1)
  693.   return
  694.  end
  695.  
  696.  turtle.select(1)
  697.  turtle.dropDown()
  698.  
  699.  sendMessage("Placing items in the chest...")
  700.  chest[side][2][chestX][chestY] = chest[side][2][chestX][chestY] + insertDemand + 1
  701.  moveToChest(side,chestX,chestY,"store",0,insertDemand + 1)
  702. end
  703.  
  704.  
  705. -- WAITING MENU
  706.  
  707.  
  708. function run()
  709.  clearScreen()
  710.  if not loadVariables() then
  711.   print("The turtle's ID is: " .. turtleID)
  712.   print("Enter the computer ID: ")
  713.   sleep(0.5)
  714.   computerID = tonumber(read())
  715.   saveVariables()
  716.  end
  717.  while true do
  718.   clearScreen()
  719.   print("Waiting for message...")
  720.   print("Turtle ID: " .. turtleID)
  721.   print("Fuel Level: " .. turtle.getFuelLevel())
  722.   saveVariables()
  723.   local message = receiveMessage()
  724.  
  725.   if message == "indicator item" then
  726.    addNewItem()
  727.   elseif message == "trade" then
  728.    if receiveMessage() == "trade goes on" then
  729.     trade()
  730.    end
  731.   elseif message == "information" then
  732.    getInfo()
  733.   elseif message == "refuel" then
  734.    turtle.select(1)
  735.    while turtle.suck() do
  736.     turtle.refuel()
  737.    end
  738.    dropSlots(1,16)
  739.   elseif message == "change slot amount" then
  740.    maxChestSlots = tonumber(receiveMessage())
  741.   elseif message == "refill" then
  742.    refill()
  743.   elseif message == "get amount item" then
  744.    local amount = getAmountItem()
  745.    print("sendingMessage")
  746.    sendMessage(amount)
  747.   elseif message == "get items" then
  748.    getItems()
  749.   end
  750.  end
  751. end
  752.  
  753.  
  754.  
  755.  
  756. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement