Advertisement
Guest User

daewoo.lua

a guest
Oct 23rd, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.21 KB | None | 0 0
  1. --[[  VÄREJÄ
  2.  
  3. 0: white == Poran nostaminen
  4. 1: orange == TYHJÄ
  5. 2: magenta == UP
  6. 3: lightblue == NORTH
  7. 4: yellow == TYHJÄ
  8. 5: lime == DOWN
  9. 6: pink == Poran laskeminen
  10. 7: grey == TYHJÄ
  11. 8: lightgrey == EAST
  12. 9: cyan == TYHJÄ
  13. 10: purple == TYHJÄ
  14. 11: blue (da ba dee) == SOUTH
  15. 12: brown = TYHJÄ
  16. 13: green = WEST
  17. 14: red == TYHJÄ
  18. 15: black == TYHJÄ
  19. 16: eof
  20. --]]
  21.  
  22. --globals
  23. local component = require("component")
  24. local sides = require("sides")
  25. local colors = require("colors")
  26. local event = require("event")
  27. local rs = component.redstone
  28. local m = component.modem
  29. local drilltime = 0.7       --Delay to let the engine move the drillhead
  30. local movesleep = 0.4       --Delay to let the engine move the machine
  31. local drillsize = 7         --Width of the drillhead
  32. local cobbleStart = 70      --Level when the cobblewall will be built onwards
  33. local drillStuckMax = 5     --How long the drill will try to drill downwards before rising the drill up.
  34. local isCobbling = false
  35.  
  36. --Redstone table
  37. local redout = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
  38.  
  39. function updateRed()
  40.   rs.setBundledOutput(sides.north,redout)
  41. end
  42.  
  43.  
  44. --Drill controls ------------------
  45.  
  46. function stop()
  47.   m.broadcast(2303,"stop")
  48. end
  49.  
  50. function drillUp()
  51.   drillUp(1)
  52. end
  53.  
  54. function drillDn()
  55.   drillDn(1)
  56. end
  57.  
  58. function drillDn(n)
  59.   print(":")
  60.   for i=0,n-1,1 do
  61.     io.write(".")
  62.     redout[6] = 255
  63.     updateRed()
  64.     os.sleep(drilltime + 0.5)
  65.     redout[6] = 0
  66.     updateRed()
  67.   end
  68. end
  69.  
  70. function drillUp(n)
  71.   for i=0,n-1,1 do
  72.     io.write(".")
  73.     redout[0] = 255
  74.     updateRed()
  75.     os.sleep(drilltime)
  76.     redout[0] = 0
  77.     updateRed()
  78.   end
  79. end
  80.  
  81. --Machine movement -------------
  82.  
  83. function west()
  84.   awest(drillsize)
  85. end
  86.  
  87. function east()
  88.   east(drillsize)
  89. end
  90.  
  91. function north()
  92.   north(drillsize)
  93. end
  94.  
  95. function south()
  96.   south(drillsize)
  97. end
  98.  
  99. function up()
  100.   up(1)
  101. end
  102.  
  103. function down()
  104.   down(1)
  105. end
  106.  
  107. -- ACCURATE MOVING
  108.  
  109. --Länteen mennessä X pienenee
  110. function awest(n)
  111.   print("awest")
  112.   goal = getX() - n
  113.   i = n*3
  114.  
  115.   print("Going to east " .. n .. " blocks")
  116.   print("Goal == " .. goal)
  117.   print("Safety: " .. i .." tries")
  118.  
  119.   for i=0,n*3,1 do
  120.     west(1)    
  121.     if getX() > goal then
  122.       print("keep going")
  123.     else
  124.       print("done")
  125.       break
  126.     end
  127.   end
  128. end
  129.  
  130. function aeast(n)
  131.   print("aeast")
  132.   goal = getX() + n
  133.   i = n*3
  134.  
  135.   print("Going to east " .. n .. " blocks")
  136.   print("Goal == " .. goal)
  137.   print("Safety: " .. i .." tries")
  138.  
  139.   for i=0,n*3,1 do
  140.     east(1)
  141.     if getX() < goal then
  142.       print("keep going")
  143.     else
  144.       print("done")
  145.       break
  146.     end
  147.   end
  148. end
  149.  
  150. --Etelään mennessä Z koordinaatti kasvaa
  151. function asouth(n)
  152.   print("asouth")
  153.   goal = getZ() + n
  154.   i = n*3
  155.  
  156.   print("Going to east " .. n .. " blocks")
  157.   print("Goal == " .. goal)
  158.   print("Safety: " .. i .." tries")
  159.  
  160.   for i=0,n*3,1 do
  161.     south(1)    
  162.     if getZ() < goal then
  163.       print("keep going")
  164.     else
  165.       print("done")
  166.       break
  167.     end
  168.   end
  169. end
  170.  
  171. function north(n)
  172.   print("asouth")
  173.   goal = getZ() - n
  174.   i = n*3
  175.  
  176.   print("Going to east " .. n .. " blocks")
  177.   print("Goal == " .. goal)
  178.   print("Safety: " .. i .." tries")
  179.  
  180.   for i=0,n*3,1 do
  181.     north(1)    
  182.     if getZ() > goal then
  183.       print("keep going")
  184.     else
  185.       print("done")
  186.       break
  187.     end
  188.   end
  189. end
  190.  
  191. function aup(n)
  192.   print("aup")
  193.   goal = getY() + n
  194.   i = n*3
  195.  
  196.   print("Going to east " .. n .. " blocks")
  197.   print("Goal == " .. goal)
  198.   print("Safety: " .. i .." tries")
  199.  
  200.   for i=0,n*3,1 do
  201.     up(1)    
  202.     if getY() < goal then
  203.       print("keep going")
  204.     else
  205.       print("done")
  206.       break
  207.     end
  208.   end
  209. end
  210.  
  211. function adown(n)
  212.   print("adown")
  213.   goal = getY() - n
  214.   i = n*3
  215.  
  216.   print("Going to east " .. n .. " blocks")
  217.   print("Goal == " .. goal)
  218.   print("Safety: " .. i .." tries")
  219.  
  220.   for i=0,n*3,1 do
  221.     down(1)    
  222.     if getY() > goal then
  223.       print("keep going")
  224.     else
  225.       print("done")
  226.       break
  227.     end
  228.   end
  229. end
  230.  
  231.  
  232. --REGULAR MOVING
  233.  
  234.  
  235. function west(n)
  236.   for i=0,n-1,1 do
  237.     io.write(".")
  238.     redout[13] = 255
  239.     updateRed()
  240.     os.sleep(movesleep)
  241.     redout[13] = 0
  242.     updateRed()
  243.     os.sleep(1)
  244.   end
  245. end
  246.  
  247. function east(n)
  248.   for i=0,n-1,1 do
  249.     io.write(".")
  250.     redout[8] = 255
  251.     updateRed()
  252.     os.sleep(movesleep)
  253.     redout[8] = 0
  254.     updateRed()
  255.     os.sleep(1)
  256.   end
  257. end
  258.  
  259. function north(n)
  260.   for i=0,n-1,1 do
  261.     io.write(".")
  262.     redout[3] = 255
  263.     updateRed()
  264.     os.sleep(0.4)
  265.     redout[3] = 0
  266.     updateRed()
  267.     os.sleep(1)
  268.   end
  269. end
  270.  
  271. function south(n)
  272.   for i=0,n-1,1 do
  273.     io.write(".")
  274.     redout[11] = 255
  275.     updateRed()
  276.     os.sleep(0.4)
  277.     redout[11] = 0
  278.     updateRed()
  279.     os.sleep(1)
  280.   end
  281. end
  282.  
  283. function up(n)
  284.   for i=0,n-1,1 do
  285.     io.write(".")
  286.     redout[2] = 255
  287.     updateRed()
  288.     os.sleep(movesleep)
  289.     redout[2] = 0
  290.     updateRed()
  291.     os.sleep(1)
  292.   end
  293. end
  294.  
  295. function up(n)
  296.   for i=0,n-1,1 do
  297.     io.write(".")
  298.     redout[2] = 255
  299.     updateRed()
  300.     os.sleep(movesleep)
  301.     redout[2] = 0
  302.     updateRed()
  303.     os.sleep(1)
  304.   end
  305. end
  306.  
  307. -- Functions
  308.  
  309. function parseGo()
  310.   str = io.read()
  311.   n = io.read("*number*")
  312.  
  313.   if str == "north" then
  314.     anorth(n)
  315.   elseif str == "south" then
  316.     asouth(n)
  317.   elseif str == "east" then
  318.     aeast(n)
  319.   elseif str == "west" then
  320.    awest(n)
  321.   elseif str == "up" then
  322.     aup(n)
  323.   elseif str == "down" then
  324.     adown(n)
  325.   else
  326.     error("Idiootti")
  327.   end
  328. end
  329.  
  330.  
  331. function getDrillLevel()
  332.  
  333.   m.open(2304)
  334.   print("[DEBUG]getDrillLevel")
  335.   m.broadcast(portout, "loc")
  336.   print("[DEBUG]getDrillLevel: Broadcast OK!")
  337.   local _, _, _, _, _, message = event.pull("modem message")
  338.   print("[DEBUG]getDrilllLEvel: Message Received!")
  339.   print("Drilling at Y = " .. message)
  340.   return message
  341. end
  342.  
  343. function cobble(n)
  344.   if n == 0 then
  345.     m.broadcast(2303,"stop")
  346.     isCobbling = false
  347.   elseif n == 1 then
  348.     m.broadcast(2303,"start")
  349.     isCobbling = true
  350.   end
  351. end
  352.  
  353.  
  354. function getY()
  355.   m.open(2303)
  356.   m.broadcast(2303,"y")
  357.   local _,_,_,_,_, message = event.pull("modem_message")
  358.  
  359.   return(message)
  360. end
  361.  
  362. function getX()
  363.   m.open(2303)
  364.   m.broadcast(2303,"x")
  365.   local _,_,_,_,_, message = event.pull("modem_message")
  366.  
  367.   return(message)
  368. end
  369.  
  370. function getZ()
  371.   m.open(2303)
  372.   m.broadcast(2303,"z")
  373.   local _,_,_,_,_, message = event.pull("modem_message")
  374.  
  375.   return(message)
  376. end
  377.  
  378. function dig(n)
  379.   lastHeight = getY()
  380.   drillStuck = 0
  381.  
  382. --Digging down
  383.   while true do
  384.  
  385. --Checks wether cobble walls need to be built  
  386.     if isCobbling == false then
  387.       if getY() < cobbleStart then
  388.        cobble(1)
  389.       end
  390.    end
  391.   lastHeight = getY()
  392.   drillDn(1)
  393.   print("pärr pärr xd")
  394.   print("Y: " .. getY() .. " last: " .. lastHeight)
  395.   print("Drillstuck: " .. drillStuck)
  396.  
  397. --Check if drill is stuck
  398.     if getY() == lastHeight then
  399.       if drillStuck == drillStuckMax then
  400.         io.write("\nMining complete")
  401.         break
  402.       else
  403.         drillStuck = drillStuck + 1
  404.         print("Drill has been stuck for " .. drillStuck .. " times in a row.")
  405.         print("lastY: " .. lastHeight .. " current: " .. getY())
  406.       end
  407.     end
  408.  
  409.   end
  410.  
  411. drillStuck = 0
  412.  
  413. --Rising up
  414.   while true do
  415.  
  416.     if isCobbling == true then
  417.       if getY() > cobbleStart then
  418.         cobble(0)
  419.       end
  420.     end
  421.    
  422.     lastHeight = getY()
  423.     drillUp(1)
  424.  
  425.     print("pärr pärr x-d")
  426.     print("Y: " .. getY() .. " last: " .. lastHeight)
  427.     print("Drillstuck: " .. drillStuck)
  428.  
  429.     --Check if drill is stuck
  430.     if getY() == lastHeight then
  431.       if drillStuck == drillStuckMax then
  432.         io.write("\nDrill is up")
  433.         break
  434.       else
  435.         drillStuck = drillStuck + 1
  436.         print("Drillstuck is now " .. drillStuck)
  437.       end
  438.     end
  439.  
  440.   end
  441. end
  442.  
  443. --TYÄ OHOJEET MENNÖÖ TÄNNE
  444. function homma()
  445.  
  446. end
  447.  
  448. function menu()
  449.   while true do
  450.     print("Kauppisen maansiirtofirima, Pete Kauppinen puhelimessa?")
  451.     print("\nhelp,?,go,drilldn,drillup,dig,homma,lvl,q")
  452.     ucommand = io.read()
  453.  
  454. --parsing command
  455.  
  456.     if ucommand == "go" then
  457.       parseGo(ucommand)
  458.     elseif ucommand == "drillup" then
  459.       print("Paljonko nostetaan?")
  460.       drillUp(io.read("*number"))
  461.     elseif ucommand == "drillup" then
  462.       print("Paljonko lasketaan?")
  463.       drillDn(io.read("*number"))
  464.     elseif ucommand == "dig" then
  465.       dig()
  466.     elseif ucommand == "homma" then
  467. --      homma()
  468.     elseif ucommand == "stop" then
  469.       stop()
  470.     elseif ucommand == "q" then
  471.       break
  472.     elseif ucommand == "lvl" then
  473.       getY()
  474.     else
  475.       print("Kjeh?")
  476.     end
  477.   end
  478. end
  479.  
  480.  
  481.  
  482. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement