LatvianModder

LM_Pathfinder

May 3rd, 2014
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1. -- LatvianModder's Pathfinding code
  2. running = true
  3. homePos = nil
  4. pos = nil
  5. --[[ 0 = z-, 1 = x+, 2 = z+, 3 = x- ]]--
  6. rotation = 0
  7.  
  8. function resetTerm()
  9.     term.clear()
  10.     term.setCursorPos(1, 1)
  11. end
  12.  
  13. function init()
  14.     resetTerm()
  15.    
  16.     rednet.open("left")
  17.    
  18.     homePos = vector.new(0, 0, 0)
  19.     pos = vector.new(0, 0, 0)
  20.    
  21.     printNet("Turtle inited! 'help' for more info")
  22. end
  23.  
  24. function loop()
  25.     event, p1, p2, p3 = os.pullEvent()
  26.    
  27.     if event == "key" then
  28.         if p1 == 41 then
  29.             running = false
  30.         end
  31.     end
  32.    
  33.     if event == "rednet_message" then
  34.         params = split(p2, " ")
  35.        
  36.         num = #params
  37.        
  38.         if num >= 2 and params[1] == "latpath" then
  39.             --print("Received wireless command from " .. p1 .. ": " .. p2)
  40.             if num == 2 then
  41.                 if params[2] == "reboot" then
  42.                     printNet("Rebooting...")
  43.                     return os.reboot()
  44.                 end
  45.                
  46.                 if params[2] == "home" then
  47.                     printNet("Going home...")
  48.                     return moveAbs(homePos)
  49.                 end
  50.                
  51.                 if params[2] == "refuel" then
  52.                     printNet("Fuel added")
  53.                     return turtle.refuel(1)
  54.                 end
  55.                
  56.                 if params[2] == "refuel_all" then
  57.                     printNet("All fuel added")
  58.                     return shell.run("refuel all")
  59.                 end
  60.                
  61.                 if params[2] == "getpos" then
  62.                     return printNet("Pos: " .. pos.x .. ", " .. pos.y .. ", " .. pos.z)
  63.                 end
  64.                
  65.                 if params[2] == "homedist" then
  66.                     return printNet("Dist from home: " .. getDist(pos, homePos));
  67.                 end
  68.                
  69.                 if params[2] == "ff" then
  70.                     return t_ff()
  71.                 end
  72.                
  73.                 if params[2] == "rl" then
  74.                     return t_rotLeft()
  75.                 end
  76.                
  77.                 if params[2] == "rr" then
  78.                     return t_rotRight()
  79.                 end
  80.                
  81.                 if params[2] == "bb" then
  82.                     return t_bb()
  83.                 end
  84.                
  85.                 if params[2] == "uu" then
  86.                     return t_up()
  87.                 end
  88.                
  89.                 if params[2] == "dd" then
  90.                     return t_down()
  91.                 end
  92.                
  93.                 if params[2] == "rot0" then
  94.                     return t_rot0()
  95.                 end
  96.                
  97.                 if params[2] == "getrot" then
  98.                     return printNet("Rotation: " .. rotation)
  99.                 end
  100.                
  101.                 if params[2] == "setdefrot" then
  102.                     rotation = 0
  103.                     return true
  104.                 end
  105.                
  106.                 if params[2] == "help" then
  107.                     printNet("reboot - Restartet")
  108.                     printNet("refuel - Refuel 1 coal")
  109.                     printNet("refuel_all - Refuel all")
  110.                     printNet("getpos - Pozicija")
  111.                     printNet("getrot - Rotacija")
  112.                     printNet("setdefrot - Nofixet rotaciju")
  113.                     printNet("home - Braukt uz majam")
  114.                     printNet("homedist - Attalums lidz sakumpunktam")
  115.                     printNet("ff, bb, rl, rr, uu, dd - Braukt, griezties")
  116.                     printNet("ff, bb <x> - Braukt uz prieksu/atpakal x metrus")
  117.                     printNet("goto <x> <y> <z> - Braukt uz absolutam x y z")
  118.                     printNet("goto_rel <x> <y> <z> - Braukt uz relativam x y z")
  119.                     return
  120.                 end
  121.                
  122.                 printNet("Unknown cmd: " .. params[2])
  123.             end
  124.            
  125.             if num == 3 then
  126.                 if params[2] == "ff" then
  127.                     return t_ff_dist(params[3] + 0)
  128.                 end
  129.                
  130.                 if params[2] == "bb" then
  131.                     t_rot180()
  132.                     ff = t_ff_dist(params[3] + 0)
  133.                     t_rot180()
  134.                     return ff
  135.                 end
  136.             end
  137.            
  138.             if num == 5 then
  139.                 if params[2] == "goto" then
  140.                     x = params[3] + 0
  141.                     y = params[4] + 0
  142.                     z = params[5] + 0
  143.                     return moveAbs(vector.new(x, y, z))
  144.                 end
  145.                
  146.                 if params[2] == "goto_rel" then
  147.                     x = params[3] + 0
  148.                     y = params[4] + 0
  149.                     z = params[5] + 0
  150.                     return moveRel(vector.new(x, y, z))
  151.                 end
  152.             end
  153.         end
  154.     end
  155. end
  156.  
  157. function printNet(txt)
  158.     print(txt)
  159.     rednet.broadcast("latprint " .. txt)
  160. end
  161.  
  162. function getDist(p1, p2)
  163.     return (p1:sub(p2)):length()
  164. end
  165.  
  166. function t_rot0()
  167.     if rotation == 0 then return end
  168.     if rotation == 1 then return t_rotLeft() end
  169.     if rotation == 3 then return t_rotRight() end
  170.     t_rotLeft() t_rotLeft() return true
  171. end
  172.  
  173. function moveAbs(p)
  174.     return moveRel(p - pos)
  175. end
  176.  
  177. function moveRel(p)
  178.     if p == nil or ( p.x == 0 and p.y == 0 and p.z == 0 )
  179.     then return false end
  180.    
  181.     t_rot0()
  182.    
  183.     if p.z ~= 0 then
  184.         invXZ = false
  185.         if p.z > 0 then
  186.             t_rot180()
  187.             p.z = -p.z
  188.             --p.x = -p.x
  189.             invXZ = true
  190.         end
  191.        
  192.         t_ff_dist(-p.z)
  193.        
  194.         if invXZ then
  195.             t_rot180()
  196.         end
  197.     end
  198.    
  199.     if p.x ~= 0 then
  200.         if p.x > 0 then
  201.             t_rotRight()
  202.             t_ff_dist(p.x)
  203.         else
  204.             t_rotLeft()
  205.             t_ff_dist(-p.x)
  206.         end
  207.     end
  208.    
  209.     t_toAbsY(p.y)
  210.    
  211.     t_rot0()
  212. end
  213.  
  214. function refuel()
  215.     --if turtle.refuel(0) then
  216.         turtle.refuel(1)
  217.     --end
  218. end
  219.  
  220. function t_up()
  221.     refuel()
  222.     if turtle.up() then
  223.         pos.y = pos.y + 1
  224.         return true
  225.     end
  226.    
  227.     return false
  228. end
  229.  
  230. function t_down()
  231.     refuel()
  232.     if turtle.down() then
  233.         pos.y = pos.y - 1
  234.         return true
  235.     end
  236.    
  237.     return false
  238. end
  239.  
  240. function t_ff()
  241.     refuel()
  242.     if turtle.forward() then
  243.         if rotation == 0 then
  244.             pos.z = pos.z - 1
  245.         end
  246.        
  247.         if rotation == 1 then
  248.             pos.x = pos.x + 1
  249.         end
  250.        
  251.         if rotation == 2 then
  252.             pos.z = pos.z + 1
  253.         end
  254.        
  255.         if rotation == 3 then
  256.             pos.x = pos.x - 1
  257.         end
  258.        
  259.         return true
  260.     end
  261.    
  262.     return false
  263. end
  264.  
  265. function t_rotLeft()
  266.     if not turtle.turnLeft() then return false end
  267.     rotation = rotation - 1
  268.     if rotation == -1 then
  269.     rotation = 3 end
  270.     return true
  271. end
  272.  
  273. function t_rotRight()
  274.     if not turtle.turnRight() then return false end
  275.     rotation = rotation + 1
  276.     if rotation == 4 then
  277.     rotation = 0 end
  278.     return true
  279. end
  280.  
  281. function t_rot180()
  282.     t_rotLeft()
  283.     t_rotLeft()
  284. end
  285.  
  286. function t_bb()
  287.     t_rot180()
  288.     ff = t_ff()
  289.     t_rot180()
  290.     return ff
  291. end
  292.  
  293. function t_ff_dist(dist)
  294.     timesUp = 0
  295.     for i = 1, dist, 1 do
  296.         while not t_ff() do
  297.             t_up()
  298.             timesUp = timesUp + 1
  299.         end
  300.     end
  301.    
  302.     if timesUp > 0 then
  303.         for i = 1, timesUp, 1 do
  304.             t_down()
  305.         end
  306.     end
  307. end
  308.  
  309. function t_bb_dist(dist)
  310.     t_rot180()
  311.     t_ff_dist(dist)
  312.     t_rot180()
  313. end
  314.  
  315. function t_toAbsY(y)
  316.     return t_toRelY(y - pos.y)
  317. end
  318.  
  319. function t_toRelY(y)
  320.     moved = 0
  321.     if y == 0 then return 0 end
  322.     if y > 0 then
  323.         for i = 1, y, 1 do
  324.             if t_up() then
  325.                 moved = moved + 1
  326.             end
  327.         end
  328.     end
  329.    
  330.     for i = 1, -y, 1 do
  331.         if t_down() then
  332.             moved = moved + 1
  333.         end
  334.     end
  335.    
  336.     return moved
  337. end
  338.  
  339. -- String Utils
  340.  
  341. function split(s, regex)
  342.     outResults = { }
  343.     local theStart = 1
  344.     local theSplitStart, theSplitEnd = string.find( s, regex, theStart )
  345.     while theSplitStart do
  346.         table.insert( outResults, string.sub( s, theStart, theSplitStart-1 ) )
  347.         theStart = theSplitEnd + 1
  348.         theSplitStart, theSplitEnd = string.find( s, regex, theStart )
  349.     end
  350.     table.insert( outResults, string.sub( s, theStart ) )
  351.     return outResults
  352. end
  353.  
  354. function stringStarts(s, st)
  355.    return string.sub(s, 1, string.len(st)) == st
  356. end
  357.  
  358. -- Loop
  359.  
  360. init()
  361. while running do
  362.     loop()
  363. end
  364.  
  365. resetTerm()
Advertisement
Add Comment
Please, Sign In to add comment