fatboychummy

TurtleDigitClient.lua

Jan 10th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1. if not turtle then
  2.   print("Running on a computer.  yeet")
  3.   return
  4. else
  5.   print("Turtle dooting doots.")
  6. end
  7.  
  8. local mod = peripheral.wrap("back")
  9. local black = 1
  10. local white = 2
  11. local tnt = 3
  12. local myDigit, myPos
  13.  
  14. if not mod then
  15.   error("Modem not found... Somehow.")
  16. end
  17. mod.open(1)
  18.  
  19. local function check(got, want, id)
  20.   id = id or "yea"
  21.   if type(got) ~= want then
  22.     error("Expected " .. want .. " got " .. type(got) .. " (" .. id .. ").", 2)
  23.   end
  24. end
  25.  
  26. local function safecheck(got, want, id)
  27.   id = id or "yea"
  28.   if type(got) ~= want then
  29.     printError("[warn] Expected " .. want .. " got " .. type(got) .. " (" .. id
  30.                 .. ").", 2)
  31.     return false
  32.   end
  33.   return true
  34. end
  35.  
  36. local function getItems()
  37.   local self = mod.getNameLocal()
  38.   local chest
  39.  
  40.   for _, name in ipairs(peripheral.getNames()) do
  41.     if name:find("chest") then
  42.       chest = peripheral.wrap(name)
  43.       break
  44.     end
  45.   end
  46.   if not chest then
  47.     error("Couldn't find chest!")
  48.   end
  49.  
  50.   chest.pushItems(self, 1, 1, 1)
  51.   chest.pushItems(self, 2, 1, 2)
  52.   chest.pushItems(self, 3, 1, 3)
  53. end
  54.  
  55. local function set(i)
  56.   if i == 4 then
  57.     rs.setOutput("front", true)
  58.     os.sleep(0.2)
  59.     rs.setOutput("front", false)
  60.     return
  61.   end
  62.   if turtle.detect() then turtle.dig() end
  63.   turtle.select(i)
  64.   turtle.place()
  65. end
  66.  
  67. local function getPosition()
  68.   local h = io.open("/POS", 'r')
  69.   local n = h:read()
  70.   local p = h:read()
  71.   h:close()
  72.  
  73.   return tonumber(n), tonumber(p)
  74. end
  75.  
  76. local function setPosition()
  77.   local h = io.open("/disk/last", 'r')
  78.   local n = h:read()
  79.   local p = h:read()
  80.   h:close()
  81.  
  82.   h = io.open("/disk/last", 'w')
  83.   h:write(n)
  84.   h:write('\n')
  85.   h:write(p + 1)
  86.   h:close()
  87.  
  88.   h = io.open("/POS", 'w')
  89.   h:write(n)
  90.   h:write('\n')
  91.   h:write(p + 1)
  92.   h:close()
  93. end
  94.  
  95. local function draw(msg)
  96.   if safecheck(msg.digit, "number", "checktable - digit")
  97.   and safecheck(msg.pos, "number", "checktable - pos")
  98.   and safecheck(msg.tp, "number", "checktable - tp") then
  99.     if myDigit == msg.digit
  100.     and myPos == msg.pos then
  101.       print("This message is for me!")
  102.       set(msg.tp)
  103.     else
  104.       print("This message is not for me.")
  105.     end
  106.   end
  107. end
  108.  
  109. local function main()
  110.   myDigit, myPos = getPosition()
  111.   print("Going into main loop.")
  112.   while true do
  113.     local _, _, _, _, message = os.pullEvent("modem_message")
  114.     print("Recieved a message.")
  115.     if safecheck(message, "table", "Message recieve") then
  116.       print("It is good, draw.")
  117.       draw(message)
  118.     else
  119.       print("Do not draw.")
  120.     end
  121.   end
  122. end
  123.  
  124. main()
Add Comment
Please, Sign In to add comment