Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Advertisement
KTRD

World eater master

Feb 21st, 2025 (edited)
618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. -- Mocking the APIs for editor support
  2. -- Remove before running in minecraft
  3. --[[
  4. local rednet = {
  5.   open = function(_) end,
  6.   close = function(_) end,
  7.   receive = function(_)
  8.     return 0, "ok"
  9.   end,
  10. }
  11. local os = {
  12.   sleep = function(_) end,
  13. }
  14. local gps = {
  15.   locate = function(_)
  16.     return 0, 0, 0
  17.   end,
  18. }
  19. local fs = {
  20.   exists = function(_)
  21.     return false
  22.   end,
  23.   open = function(_, _)
  24.     return {
  25.       writeLine = function(_) end,
  26.       close = function(_) end,
  27.     }
  28.   end,
  29. }
  30. --]]
  31.  
  32. local function getSide(x, z)
  33.   if (x <= 0) and (z <= 0) then
  34.     return "NW"
  35.   end
  36.   if (x > 0) and (z > 0) then
  37.     return "SE"
  38.   end
  39.   if (x <= 0) and (z > 0) then
  40.     return "SW"
  41.   end
  42.   if (x > 0) and (z <= 0) then
  43.     return "NE"
  44.   end
  45. end
  46.  
  47. local function nextCoordsEvery(x, z, limit)
  48.   local side = getSide(x, z)
  49.   if side == "NW" then
  50.     if z == 0 then
  51.       return x, z + 1, false
  52.     end
  53.     return x - 1, z + 1, false
  54.   end
  55.   if side == "SE" then
  56.     if z == 1 then
  57.       return x, z - 1, false
  58.     end
  59.     return x + 1, z - 1, false
  60.   end
  61.   if side == "SW" then
  62.     if x == 0 then
  63.       return x + 1, z, false
  64.     end
  65.     return x + 1, z + 1, false
  66.   end
  67.   if side == "NE" then
  68.     if x == 1 then
  69.       if math.abs(z) == limit then
  70.         return x, z, true
  71.       end
  72.       return x - 1, z - 1, false
  73.     end
  74.     return x - 1, z - 1, false
  75.   end
  76. end
  77.  
  78. local function nextCoords(x, z, limits)
  79.   local x1, z1, limitReached = nextCoordsEvery(x, z, limits)
  80.   if limitReached then
  81.     return x1, z1, true
  82.   elseif math.fmod(x1, 2) == 0 then
  83.     return nextCoordsEvery(x1, z1, limits)
  84.   else
  85.     return x1, z1, false
  86.   end
  87. end
  88.  
  89. local function locate()
  90.   print("Getting the current location via GPS...")
  91.   local x, y, z = gps.locate(1)
  92.   if x == nil then
  93.     print("Waiting for GPS signal")
  94.     while x == nil do
  95.       os.sleep(10)
  96.       x, y, z = gps.locate(1)
  97.     end
  98.   end
  99.   print("Got the current location via GPS!")
  100.   return x, y, z
  101. end
  102.  
  103. local NEXT_COORDS_FILE_NAME = ".eater_next_coords"
  104. local LIMIT = 10
  105.  
  106. -- Init
  107. rednet.open("back")
  108.  
  109. local x0, y0, z0 = locate()
  110. y0 = y0 - 1
  111.  
  112. if not fs.exists(NEXT_COORDS_FILE_NAME) then
  113.   local f = fs.open(NEXT_COORDS_FILE_NAME, "w")
  114.   f.writeLine("0")
  115.   f.writeLine("0")
  116.   f.writeLine("false")
  117.   f.close()
  118. end
  119.  
  120. local f = fs.open(NEXT_COORDS_FILE_NAME, "r")
  121. local x, z = tonumber(f.readLine()), tonumber(f.readLine())
  122. local limitReached = f.readLine() == "true"
  123.  
  124. -- Main loop
  125. while true do
  126.   print("Waiting for a signal from a turtle...")
  127.   local id, msg = rednet.receive()
  128.   if msg == "eater_turtle ready" then
  129.     print("Received ready signal from #" .. id .. " turtle")
  130.     if limitReached then
  131.       print("Nothing to order!")
  132.       rednet.send(id, "eater_master done")
  133.     else
  134.       print("Ordering the turtle #" .. id .. " to go to " .. x .. ", " .. z)
  135.       rednet.send(id, "eater_master go " .. x .. " " .. z)
  136.       x, z, limitReached = nextCoords(x, z, LIMIT)
  137.       local saveF = fs.open(NEXT_COORDS_FILE_NAME, "w")
  138.       saveF.writeLine(tostring(x))
  139.       saveF.writeLine(tostring(z))
  140.       saveF.writeLine(tostring(limitReached))
  141.       saveF.close()
  142.     end
  143.   elseif msg == "eater_turtle search" then
  144.     print("Received search signal from #" .. id .. " turtle")
  145.     print("Sending zero coordinates to the turtle #" .. id)
  146.     rednet.send(id, "eater_master zero " .. x0 .. " " .. y0 .. " " .. z0)
  147.   else
  148.     print("Received unknown signal from #" .. id .. ", not sure what to do")
  149.   end
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement