ayangd

ayangd's mine V1.0.lua

Nov 7th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.04 KB | None | 0 0
  1. -- ayangd's Mine V1.0
  2. -- Made by ayangd
  3.  
  4. geo = require("component").geolyzer
  5. robot = require("robot")
  6. sides = require("sides")
  7. shell = require("shell")
  8. computer = require("computer")
  9.  
  10. args = shell.parse(...)
  11. if #args < 2 then
  12.     print("Tell me where am I facing and how deep can I dig.")
  13.     return
  14. end
  15. facing = args[1]
  16. deep = tonumber(args[2])
  17.  
  18. function div(t)
  19.     if t == nil then return nil end
  20.     if type(t) == 'table' then
  21.         a = t[1]
  22.         table.remove(t, 1)
  23.         return a, table.unpack(t)
  24.     end
  25.     return t
  26. end
  27.  
  28. function ru(fu, ar) robot.up(); fu(div(ar)); robot.down(); end
  29. function rd(fu, ar) robot.down(); fu(div(ar)); robot.up(); end
  30. function rl(fu, ar) robot.turnLeft(); fu(div(ar)); robot.turnRight(); end
  31. function rr(fu, ar) robot.turnRight(); fu(div(ar)); robot.turnLeft(); end
  32. function rf(fu, ar) robot.forward(); fu(div(ar)); robot.back(); end
  33. function rb(fu, ar) robot.back(); fu(div(ar)); robot.forward(); end
  34.  
  35. function sw(fu)
  36.     fures = fu()
  37.     while not fures do
  38.         computer.beep(300, 0.1)
  39.         computer.beep(100, 0.1)
  40.         os.sleep(0.8)
  41.         fures = fu()
  42.     end
  43. end
  44.  
  45. function fobs(fu, ar) if robot.detect() then sw(robot.swing); end; rf(fu, ar); end
  46. function uobs(fu, ar) if robot.detectUp() then sw(robot.swingUp); end; ru(fu, ar); end
  47. function dobs(fu, ar) if robot.detectDown() then sw(robot.swingDown); end; rd(fu, ar); end
  48.  
  49. function isOre(v) if v > 2 and v < 45 then return true else return false end end
  50.  
  51. function faceTo(face, fu, ar)
  52.     if facing == 'south' then
  53.         if face == 'east' then
  54.             facing = face
  55.             rl(fu, ar)
  56.             facing = 'south'
  57.         elseif face == 'west' then
  58.             facing = face
  59.             rr(fu, ar)
  60.             facing = 'south'
  61.         elseif face == 'north' then
  62.             facing = face
  63.             rr(rr, {fu, ar})
  64.             facing = 'south'
  65.         else
  66.             fu(div(ar))
  67.         end
  68.     elseif facing == 'east' then
  69.         if face == 'south' then
  70.             facing = face
  71.             rr(fu, ar)
  72.             facing = 'east'
  73.         elseif face == 'north' then
  74.             facing = face
  75.             rl(fu, ar)
  76.             facing = 'east'
  77.         elseif face == 'west' then
  78.             facing = face
  79.             rr(rr, {fu, ar})
  80.             facing = 'east'
  81.         else
  82.             fu(div(ar))
  83.         end
  84.     elseif facing == 'north' then
  85.         if face == 'east' then
  86.             facing = face
  87.             rr(fu, ar)
  88.             facing = 'north'
  89.         elseif face == 'west' then
  90.             facing = face
  91.             rl(fu, ar)
  92.             facing = 'north'
  93.         elseif face == 'south' then
  94.             facing = face
  95.             rr(rr, {fu, ar})
  96.             facing = 'north'
  97.         else
  98.             fu(div(ar))
  99.         end
  100.     elseif facing == 'west' then
  101.         if face == 'north' then
  102.             facing = face
  103.             rr(fu, ar)
  104.             facing = 'west'
  105.         elseif face == 'south' then
  106.             facing = face
  107.             rl(fu, ar)
  108.             facing = 'west'
  109.         elseif face == 'east' then
  110.             facing = face
  111.             rr(rr, {fu, ar})
  112.             facing = 'west'
  113.         else
  114.             fu(div(ar))
  115.         end
  116.     end
  117. end
  118.  
  119. function moveToward(face, fu, ar)
  120.     faceTo(face, fobs, {fu, ar})
  121. end
  122.  
  123. function nextOre()
  124.     computer.beep(1000, 0.01)
  125.     tn = geo.scan(-1, -1, -1, 3, 3, 3)
  126.     if isOre(tn[5]) then
  127.         computer.beep(1500, 0.01)
  128.         dobs(nextOre)
  129.         nextOre()
  130.     end
  131.     if isOre(tn[11]) then
  132.         computer.beep(1500, 0.01)
  133.         moveToward('north', nextOre)
  134.         nextOre()
  135.     end
  136.     if isOre(tn[13]) then
  137.         computer.beep(1500, 0.01)
  138.         moveToward('west', nextOre)
  139.         nextOre()
  140.     end
  141.     if isOre(tn[15]) then
  142.         computer.beep(1500, 0.01)
  143.         moveToward('east', nextOre)
  144.         nextOre()
  145.     end
  146.     if isOre(tn[17]) then
  147.         computer.beep(1500, 0.01)
  148.         moveToward('south', nextOre)
  149.         nextOre()
  150.     end
  151.     if isOre(tn[23]) then
  152.         computer.beep(1500, 0.01)
  153.         uobs(nextOre)
  154.         nextOre()
  155.     end
  156. end
  157.  
  158. function relCoordMove(x, z, fu, ar)
  159.     fui, ari = fu, ar
  160.     if x > 0 then
  161.         for i = 1, x do
  162.             fui, ari = moveToward, {'east', fui, ari}
  163.         end
  164.     elseif x < 0 then
  165.         for i = 1, math.abs(x) do
  166.             fui, ari = moveToward, {'west', fui, ari}
  167.         end
  168.     end
  169.     if z > 0 then
  170.         for i = 1, z do
  171.             fui, ari = moveToward, {'south', fui, ari}
  172.         end
  173.     elseif z < 0 then
  174.         for i = 1, math.abs(z) do
  175.             fui, ari = moveToward, {'north', fui, ari}
  176.         end
  177.     end
  178.     fui(div(ari))
  179. end
  180.  
  181. function map(vmin1, vmax1, vmin2, vmax2, val)
  182.     return (val - vmin1) / (vmax1 - vmin1) * (vmax2 - vmin2) + vmin2
  183. end
  184.  
  185. function steppedScan()
  186.     for i = 1, 3 do
  187.         computer.beep(220 * i * 2, 0.01)
  188.         computer.beep(220 * i, 0.01)
  189.         s = i*2 + 1
  190.         t = geo.scan(-i, -i, 0, s, s, 1)
  191.         l = 0
  192.         gout = false
  193.         for j = -i, i do
  194.             m = 0
  195.             for k = -i, i do
  196.                 if ((j == -i) or (j == i)) or ((k == -i) or (k == i)) then
  197.                     if isOre(t[l * s + m + 1]) then computer.beep(100, 0.01); relCoordMove(k, j, nextOre); gout = true; break; end
  198.                 end
  199.                 m = m + 1
  200.             end
  201.             if gout then break end
  202.             l = l + 1
  203.         end
  204.         computer.beep(220 * i, 0.01)
  205.         computer.beep(220 * i * 2, 0.01)
  206.     end
  207. end
  208.  
  209. function quickScan()
  210.     t = geo.scan(-3, -3, 0, 7, 7, 1)
  211.     for i = 1, 49 do
  212.         if isOre(t[i]) then
  213.             computer.beep(1320)
  214.             steppedScan()
  215.             break
  216.         end
  217.     end
  218. end
  219.  
  220. down = 0
  221.  
  222. for depth = 1, deep do
  223.     if not robot.detect() then robot.place() end
  224.     if robot.detectDown() then if not robot.swingDown() then break end end
  225.     robot.down()
  226.     down = down + 1
  227.     quickScan()
  228. end
  229.  
  230. for depth = 1, down do
  231.     robot.up()
  232. end
  233.  
  234. for i = 1, 5 do
  235.     computer.beep(220 * i, 0.01)
  236. end
Advertisement
Add Comment
Please, Sign In to add comment