CelticCoder

turtleVM

Dec 19th, 2024 (edited)
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.59 KB | None | 0 0
  1. os.loadAPI("turtleChest.lua")
  2.  
  3. username = nil
  4. password = nil
  5. serverID = 0
  6. atmID = 37
  7. maxitems = 1024
  8.  
  9. -- This code DRASTICALLY needs an update, as of now only 1024 of each item can be accessed at a time.
  10. -- Items MUST only have 1 chest for this to work. (I think) At least the subtraction method of item count needs modification to
  11. -- change this
  12.  
  13. function isStorageBlock(direction)
  14.     local storageBlocks = {
  15.         "minecraft:chest",
  16.         "minecraft:trapped_chest",
  17.         "minecraft:barrel",
  18.         "minecraft:shulker_box",
  19.         "minecraft:white_shulker_box",
  20.         "minecraft:orange_shulker_box",
  21.         "minecraft:magenta_shulker_box",
  22.         "minecraft:light_blue_shulker_box",
  23.         "minecraft:yellow_shulker_box",
  24.         "minecraft:lime_shulker_box",
  25.         "minecraft:pink_shulker_box",
  26.         "minecraft:gray_shulker_box",
  27.         "minecraft:light_gray_shulker_box",
  28.         "minecraft:cyan_shulker_box",
  29.         "minecraft:purple_shulker_box",
  30.         "minecraft:blue_shulker_box",
  31.         "minecraft:brown_shulker_box",
  32.         "minecraft:green_shulker_box",
  33.         "minecraft:red_shulker_box",
  34.         "minecraft:black_shulker_box",
  35.         "minecraft:hopper",
  36.         "computercraft:turtle_normal"
  37.     }
  38.     if direction == "top" then
  39.         success, data = turtle.inspectUp()
  40.     elseif direction == "bottom" then
  41.         success, data = turtle.inspectDown()
  42.     else
  43.         success, data = turtle.inspect()
  44.     end
  45.     if success then
  46.         -- Check if the block's name is in the storageBlocks list
  47.         for _, block in ipairs(storageBlocks) do
  48.             if data.name == block then
  49.                 return true  -- The block is a storage block
  50.             end
  51.         end
  52.     end
  53.     return false  -- The block is not a storage block or no block detected
  54. end
  55.  
  56. function twoInputStrip(str)
  57.     first, second = str:match("(%S+)%s+(%S+)")
  58.     return first, second
  59. end
  60.  
  61. function fiveInputStrip(str)
  62.     first, second, third, fourth, five = str:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
  63.     return first, second, third, fourth, five
  64. end
  65.  
  66. function send(id, message)
  67.     rednet.open("left")
  68.     rednet.send(id, message)
  69.     print("send: " .. message)
  70. end
  71.  
  72. function getItemLocations()
  73.     local directions = {
  74.         {name = "front", takeAll = turtleChest.takeAll, insertAll = turtleChest.insertAll, detect = turtle.detect},
  75.         {name = "left",
  76.             takeAll = function()
  77.                 turtle.turnLeft()
  78.                 local success = turtleChest.takeAll()
  79.                 turtle.turnRight()
  80.                 return success
  81.             end,
  82.             insertAll = function()
  83.                 turtle.turnLeft()
  84.                 local success = turtleChest.insertAll()
  85.                 turtle.turnRight()
  86.                 return success
  87.             end,
  88.             detect = function()
  89.                 turtle.turnLeft()
  90.                 local success = turtle.detect()
  91.                 turtle.turnRight()
  92.                 return success
  93.             end
  94.         },
  95.         {name = "right",
  96.             takeAll = function()
  97.                 turtle.turnRight()
  98.                 local success = turtleChest.takeAll()
  99.                 turtle.turnLeft()
  100.                 return success
  101.             end,
  102.             insertAll = function()
  103.                 turtle.turnRight()
  104.                 local success = turtleChest.insertAll()
  105.                 turtle.turnLeft()
  106.                 return success
  107.             end,
  108.             detect = function()
  109.                 turtle.turnRight()
  110.                 local success = turtle.detect()
  111.                 turtle.turnLeft()
  112.                 return success
  113.             end
  114.         },
  115.         {name = "back",
  116.             takeAll = function()
  117.                 turtle.turnLeft()
  118.                 turtle.turnLeft()
  119.                 local success = turtleChest.takeAll()
  120.                 turtle.turnRight()
  121.                 turtle.turnRight()
  122.                 return success
  123.             end,
  124.             insertAll = function()
  125.                 turtle.turnLeft()
  126.                 turtle.turnLeft()
  127.                 local success = turtleChest.insertAll()
  128.                 turtle.turnRight()
  129.                 turtle.turnRight()
  130.                 return success
  131.             end,
  132.             detect = function()
  133.                 turtle.turnLeft()
  134.                 turtle.turnLeft()
  135.                 local success = turtle.detect()
  136.                 turtle.turnRight()
  137.                 turtle.turnRight()
  138.                 return success
  139.             end
  140.         },
  141.         {name = "top", takeAll = turtleChest.takeAllUp, insertAll = turtleChest.insertAllUp, detect = turtle.detectUp},
  142.         {name = "bottom", takeAll = turtleChest.takeAllDown, insertAll = turtleChest.insertAllDown, detect = turtle.detectDown}
  143.     }
  144.  
  145.     local items = {}  -- Array to store items
  146.  
  147.     for _, dir in ipairs(directions) do
  148.         -- Check if there is a chest in the specified direction
  149.         if dir.detect() and isStorageBlock(dir.name) then
  150.             -- Take all items from the chest
  151.             dir.takeAll()
  152.  
  153.             -- Get the count of items
  154.             local count = turtleChest.countCheck()
  155.             turtle.select(1)
  156.             local itemDetail = turtle.getItemDetail()
  157.  
  158.             -- Insert all items back into the chest
  159.             dir.insertAll()
  160.  
  161.             -- Add the information to the items array
  162.             if itemDetail then
  163.                 table.insert(items, {direction = dir.name, itemName = itemDetail.name, count = count})
  164.             else
  165.                 table.insert(items, {direction = dir.name, itemName = "Empty", count = 0})
  166.             end
  167.         end
  168.     end
  169.  
  170.     -- Return the array with items
  171.     return items
  172. end
  173.  
  174. function suckOnce(amount, direction)
  175.     if direction == "top" then
  176.         return turtle.suckUp(amount)
  177.     elseif direction == "bottom" then
  178.         return turtle.suckDown(amount)
  179.     else
  180.         return turtle.suck(amount)
  181.     end
  182. end
  183.  
  184. function suckChest(amount, direction)
  185.     success = false
  186.     slots = math.ceil(amount / 64)
  187.     remains = amount % 64
  188.     if slots > 16 then
  189.         print("Too Many Items Requested!")
  190.         return false
  191.     elseif slots == 1 then
  192.         return suckOnce(amount, direction)
  193.     end
  194.     for slot = 1, slots do
  195.         turtle.select(slot)
  196.         if slot == slots then
  197.             return suckOnce(remains, direction)
  198.         end
  199.         success = suckOnce(64, direction)
  200.         if not success then
  201.             return success
  202.         end
  203.     end
  204. end
  205.  
  206. function getItem(amount, direction)
  207.     if direction == "left" then
  208.         turtle.turnLeft()
  209.         if not suckChest(amount, direction) then
  210.             turtleChest.insertAll()
  211.             turtle.turnRight()
  212.             return false
  213.         end
  214.         turtle.turnRight()
  215.         turtleChest.insertAll()
  216.         return true
  217.     elseif direction == "right" then
  218.         turtle.turnRight()
  219.         if not suckChest(amount, direction) then
  220.             turtleChest.insertAll()
  221.             turtle.turnLeft()
  222.             return false
  223.         end
  224.         turtle.turnLeft()
  225.         turtleChest.insertAll()
  226.         return true
  227.     elseif direction == "back" then
  228.         turtle.turnLeft()
  229.         turtle.turnLeft()
  230.         if not suckChest(amount, direction) then
  231.             turtleChest.insertAll()
  232.             turtle.turnRight()
  233.             turtle.turnRight()
  234.             return false
  235.         end
  236.         turtle.turnRight()
  237.         turtle.turnRight()
  238.         turtleChest.insertAll()
  239.         return true
  240.     elseif direction == "top" then
  241.         if not suckChest(amount, direction) then
  242.             turtleChest.insertAllUp()
  243.             return false
  244.         end
  245.         turtleChest.insertAll()
  246.         return true
  247.     elseif direction == "bottom" then
  248.         if not suckChest(amount, direction) then
  249.             turtleChest.insertAllDown()
  250.             return false
  251.         end
  252.         turtleChest.insertAll()
  253.         return true
  254.     end
  255. end
  256.  
  257. function getItems(itemname, count, array)
  258.     for _, item in ipairs(array) do
  259.         if item.itemName == itemname then
  260.             direction = item.direction
  261.         end
  262.     end
  263.     if direction then
  264.         return getItem(count, direction)
  265.     else
  266.         return false
  267.     end
  268. end
  269.  
  270. function recieve(serverID)
  271.     rednet.open("left")
  272.     senderID, message, protocol = rednet.receive()
  273.     print(message)
  274.     print(tonumber(serverID) == tonumber(senderID))
  275.     if tonumber(serverID) == tonumber(senderID) then
  276.         return message
  277.     else
  278.         return nil
  279.     end
  280. end
  281.  
  282. function setItemlist(filename, itemlist)
  283.     local file = fs.open(filename, "w")
  284.     for _, line in pairs(itemlist) do
  285.         file.write(line .. "\n")
  286.     end
  287.     file.close()
  288. end
  289.  
  290. function searchItemlist(item)
  291.     local file = fs.open("itemlist.txt", "r")
  292.     local line = file.readLine()
  293.     while line do
  294.         name, cost = twoInputStrip(line)
  295.         if name == item then
  296.             return cost
  297.         end
  298.         line = file.readLine()
  299.     end
  300.     file.close()
  301.     return nil
  302. end
  303.  
  304. function conductTransaction(username, password, cost)
  305.   message = "buy " .. username .. " " .. password .. " " .. cost
  306.   rednet.send(serverID, message)
  307.   id, value = rednet.receive(nil, 3)
  308.   os.sleep(1)
  309.   return value
  310. end
  311.  
  312. function handleConnection(itemLocations)
  313.     rednet.open("left")
  314.     senderID, message, protocol = rednet.receive()
  315.     print(message)
  316.     command = twoInputStrip(message)
  317.     if command == "itemlist" then
  318.         senderID, titemlist = recieve()
  319.         if titemlist and tonumber(senderID) == tonumber(serverID) then
  320.             itemlist = titemlist
  321.             setItemlist(filename, itemlist)
  322.         end
  323.     elseif command == "vmitems" then
  324.         rednet.send(senderID, itemLocations)
  325.     elseif command == "buy" and tonumber(senderID) == tonumber(atmID) then
  326.         --change this strip to include an amount, username, and password. variables should be count, username and password
  327.         _, username, password, nitem, count = fiveInputStrip(message)
  328.         print("username: ." .. username .. ".")
  329.         print("nitem: ." .. nitem .. ".")
  330.         print("count: ." .. count .. ".")
  331.         count = tonumber(count)
  332.         cost = searchItemlist(nitem)
  333.         if not cost then
  334.             print("No Item Sold")
  335.             --error code
  336.             return "No Item Sold"
  337.         end
  338.         check = false
  339.         for _, item in ipairs(itemLocations) do
  340.             if item.itemName == nitem then
  341.                 check = true
  342.             end
  343.         end
  344.         if not check then
  345.             print("No Item Found")
  346.             --error code
  347.             return "No Item Found"
  348.         end
  349.  
  350.         lcount = 0
  351.         for _, item in pairs(itemLocations) do
  352.             if item.itemName == nitem then
  353.                 lcount = item.count + lcount
  354.             end
  355.         end
  356.         if count > maxitems or count > lcount then
  357.             print("Not Enough Items")
  358.             print("Only " .. lcount .. " found")
  359.             --error code
  360.             return "Not Enough Items"
  361.         end
  362.         --check if user has enough money
  363.         login = "search " .. username .. " " .. password
  364.         send(serverID, login)
  365.         balance = recieve(serverID)
  366.         if balance == "false" or tonumber(balance) < tonumber(cost) then
  367.             print("Not Enough Money")
  368.             --error code
  369.             return "Not Enough Money"
  370.         end
  371.         if not getItems(nitem, count, itemLocations) then
  372.             print("Item Retrieval Failed")
  373.             --error code
  374.             return "Item Retrieval Failed"
  375.         end
  376.         --buy code
  377.        
  378.         conductTransaction(username, password, cost*count)
  379.         for _, item in pairs(itemLocations) do
  380.             if item.itemName == nitem then
  381.                 item.count = item.count - count
  382.             end
  383.         end
  384.         return itemLocations
  385.     end
  386. end
  387.  
  388.  
  389. local itemLocations = getItemLocations()
  390. print("Array")
  391. for _, item in ipairs(itemLocations) do
  392.     print(item.direction .. ": " .. item.itemName .. " " .. item.count)
  393. end
  394.  
  395. rednet.open("left")
  396. filename = "itemlist.txt"
  397. rednet.send(serverID, "vmitems please")
  398. itemlist = recieve(serverID)
  399. setItemlist(filename, itemlist)
  400.  
  401. while true do
  402.     nitemLocations = handleConnection(itemLocations)
  403.     if type(nitemLocations) == "table" then
  404.         itemLocations = nitemLocations
  405.         rednet.send(atmID, "Success")
  406.     elseif type(nitemLocations) == "string" then
  407.         rednet.send(atmID, "Failure due to " .. nitemLocations)
  408.     else
  409.         rednet.send(atmID, "Unknown Failure")
  410.     end
  411. end
Add Comment
Please, Sign In to add comment