Advertisement
Guest User

daewoo.lua

a guest
Oct 23rd, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.29 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. function drillUp()
  46.   drillUp(1)
  47. end
  48.  
  49. function drillDn()
  50.   drillDn(1)
  51. end
  52.  
  53. function drillDn(n)
  54.   for i=0,n-1,1 do
  55.     io.write(".")
  56.     redout[6] = 255
  57.     updateRed()
  58.     os.sleep(drilltime + 0.5)
  59.     redout[6] = 0
  60.     updateRed()
  61.   end
  62. end
  63.  
  64. function drillUp(n)
  65.   for i=0,n-1,1 do
  66.     io.write(".")
  67.     redout[0] = 255
  68.     updateRed()
  69.     os.sleep(drilltime)
  70.     redout[0] = 0
  71.     updateRed()
  72.   end
  73. end
  74.  
  75. --Machine movement -------------
  76.  
  77. function west()
  78.   west(drillsize)
  79. end
  80.  
  81. function east()
  82.   east(drillsize)
  83. end
  84.  
  85. function north()
  86.   north(drillsize)
  87. end
  88.  
  89. function south()
  90.   south(drillsize)
  91. end
  92.  
  93. function up()
  94.   up(1)
  95. end
  96.  
  97. function down()
  98.   down(1)
  99. end
  100.  
  101. --
  102.  
  103. function west(n)
  104.   for i=0,n-1,1 do
  105.     io.write(".")
  106.     redout[13] = 255
  107.     updateRed()
  108.     os.sleep(movesleep)
  109.     redout[13] = 0
  110.     updateRed()
  111.     os.sleep(1)
  112.   end
  113. end
  114.  
  115. function east(n)
  116.   for i=0,n-1,1 do
  117.     io.write(".")
  118.     redout[8] = 255
  119.     updateRed()
  120.     os.sleep(movesleep)
  121.     redout[8] = 0
  122.     updateRed()
  123.     os.sleep(1)
  124.   end
  125. end
  126.  
  127. function north(n)
  128.   for i=0,n-1,1 do
  129.     io.write(".")
  130.     redout[3] = 255
  131.     updateRed()
  132.     os.sleep(0.4)
  133.     redout[3] = 0
  134.     updateRed()
  135.     os.sleep(1)
  136.   end
  137. end
  138.  
  139. function south(n)
  140.   for i=0,n-1,1 do
  141.     io.write(".")
  142.     redout[11] = 255
  143.     updateRed()
  144.     os.sleep(0.4)
  145.     redout[11] = 0
  146.     updateRed()
  147.     os.sleep(1)
  148.   end
  149. end
  150.  
  151. function up(n)
  152.   for i=0,n-1,1 do
  153.     io.write(".")
  154.     redout[2] = 255
  155.     updateRed()
  156.     os.sleep(movesleep)
  157.     redout[2] = 0
  158.     updateRed()
  159.     os.sleep(1)
  160.   end
  161. end
  162.  
  163. function up(n)
  164.   for i=0,n-1,1 do
  165.     io.write(".")
  166.     redout[2] = 255
  167.     updateRed()
  168.     os.sleep(movesleep)
  169.     redout[2] = 0
  170.     updateRed()
  171.     os.sleep(1)
  172.   end
  173. end
  174.  
  175. -- Functions
  176.  
  177. function parseGo()
  178.   str = io.read()
  179.   n = io.read("*number*")
  180.  
  181.   if str == "north" then
  182.     north(n)
  183.   elseif str == "south" then
  184.     south(n)
  185.   elseif str == "east" then
  186.     east(n)
  187.   elseif str == "west" then
  188.     west(n)
  189.   elseif str == "up" then
  190.     up(n)
  191.   elseif str == "down" then
  192.     down(n)
  193.   else
  194.     error("Idiootti")
  195.   end
  196. end
  197.  
  198.  
  199. function getDrillLevel()
  200.  
  201.   m.open(2304)
  202.   print("[DEBUG]getDrillLevel")
  203.   m.broadcast(portout, "loc")
  204.   print("[DEBUG]getDrillLevel: Broadcast OK!")
  205.   local _, _, _, _, _, message = event.pull("modem message")
  206.   print("[DEBUG]getDrilllLEvel: Message Received!")
  207.   print("Drilling at Y = " .. message)
  208.   return message
  209. end
  210.  
  211. function cobble(n)
  212.   if n == 0 then
  213.     m.broadcast(2303,"stop")
  214.   elseif n == 1 then
  215.     m.broadcast(2303,"start")
  216.  
  217.  
  218. function getY()
  219.   m.open(2303)
  220.   m.broadcast(2303,"y")
  221.   local _,_,_,_,_, message = event.pull("modem_message")
  222.  
  223.   return(message)
  224. end
  225.  
  226. function getX()
  227.  
  228. function dig(n)
  229.   lastHeight = getY()
  230.   drillStuck = 0
  231.  
  232. --Digging down
  233.   while true do
  234.  
  235. --Checks wether cobble walls need to be built  
  236.     if isCobbling == false then
  237.       if getDrillLevel < cobbleStart then
  238.        cobble(1)
  239.       end
  240.    end
  241.  
  242.   drillDn()
  243.  
  244. --Check if drill is stuck
  245.     if getY() == lastHeight then
  246.       if drillStuck == drillStuckMax then
  247.         io.write("\nMining complete")
  248.         break
  249.       else
  250.         drillStuck = drillStuck + 1
  251.       end
  252.     end
  253.  
  254.   end
  255.  
  256. drillStuck = 0
  257. lastHeight = getY()
  258.  
  259. --Rising up
  260.   while true do
  261.  
  262.     if isCobbling == true then
  263.       if getDrillLevel > cobbleStart then
  264.         cobble(0)
  265.       end
  266.     end
  267.  
  268.     drillUp()
  269.  
  270.     --Check if drill is stuck
  271.     if getY == lastHeight then
  272.       if drillStuck == drillStuckMax then
  273.         io.write("\nDrill is up")
  274.         break
  275.       else
  276.         drillStuck = drillStuck + 1
  277.       end
  278.     end
  279.  
  280.   end
  281. end
  282.  
  283. function menu()
  284.   while true do
  285.     print("Kauppisen maansiirtofirima, Pete Kauppinen puhelimessa?")
  286.     print("\nhelp,?,go,drilldn,drillup,dig,homma,lvl,q")
  287.     ucommand = io.read()
  288.  
  289. --parsing command
  290.  
  291.     if ucommand == "go" then
  292.       parseGo(ucommand)
  293.     elseif ucommand == "drillup" then
  294.       print("Paljonko nostetaan?")
  295.       drillUp(io.read("*number"))
  296.     elseif ucommand == "drillup" then
  297.       print("Paljonko lasketaan?")
  298.       drillDn(io.read("*number"))
  299.     elseif ucommand == "dig" then
  300.       dig()
  301.     elseif ucommand == "homma" then
  302. --      homma()
  303.     elseif ucommand == "q" then
  304.       break
  305.     elseif ucommand == "lvl" then
  306.       getY()
  307.     else
  308.       print("Kjeh?")
  309.     end
  310.   end
  311. end
  312.  
  313.  
  314.  
  315. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement