Guest User

movement api

a guest
Sep 5th, 2013
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.03 KB | None | 0 0
  1. --[[
  2. Compass api.
  3. Save as compass on a computer and everything should be able to be used using compass.[function]([parameters])
  4.  
  5. API for turtles to enhance the movement of them and give them smarter navigation options.
  6. Made by Blipman17
  7. version; 1.2.0
  8.  
  9. It is not allowed to distribute this API in any form.
  10. Feel free to modify all the code, but make credits then.
  11. (also of yours.)
  12. ]]--
  13. fuellevel = 10
  14. fuelmode="normal"
  15.  
  16. function setMinFuel(level)
  17. fuellevel=level
  18. end
  19. --[[
  20. -list of valid modes; "normal", "none"
  21. -work in progress
  22. ]]--
  23.  
  24. function setFuelMode(thingy)
  25. fuelmode=thingy
  26. end
  27.  
  28. function fuel()
  29. if fuelmode=="normal" then
  30. if turtle.getFuelLevel()<fuellevel then
  31. print("insert fuel in bottom right slot")
  32. while turtle.getItemCount(16)<1 do
  33. sleep(1)
  34. end
  35. turtle.select(16)
  36. if turtle.refuel()==false then
  37. print("inserted invalid fuel source")
  38. while turtle.refuel()==false do
  39. sleep(1)
  40. end
  41. end
  42. end
  43. elseif fuelmode=="none" then
  44. --nothing
  45. elseif fuelmode=="custom" then
  46. -- work in progress
  47. end
  48. end
  49.  
  50. currentplace={}
  51. --[[
  52. currentplace
  53.             [1] = relative position on x axis
  54.             [2] = relative position on y axis
  55.             [3] = relative position on z axis
  56.             [4] = relative facing (north, east, south, west)
  57.             [5] = absolute position on x axis
  58.             [6] = absolute position on y axis
  59.             [7] = absolute position on z axis
  60.             [8] = absolute facing (north, east, south, west)
  61. can be called without any arguments
  62. ]]--
  63. function setplace(a, b, c, d)
  64.     if a ==nil then a=0 end
  65.     if b ==nil then b=0 end
  66.     if c==nil then c=0 end
  67.     if d==nil then d="north" end
  68.     currentplace[1]=a
  69.     currentplace[2]=b
  70.     currentplace[3]=c
  71.     currentplace[4]=d
  72.     if gps.locate()~=nil then
  73.     currentplace[5], currentplace[6], currentplace[7] = gps.locate()
  74.     -- gps facing code
  75.     location={}
  76.     if turtle.detect()==false then
  77.     fuel()
  78.     turtle.forward()
  79.     location[1], location[2], location[3]=gps.locate()
  80.     if location[3]-currentplace[7]==-1 then
  81.     currentplace[8]="north"
  82.     turtle.back()
  83.     return true
  84.     elseif location[3]-currentplace[7]==1 then
  85.     currentplace[8]="south"
  86.     turtle.back()
  87.     return true
  88.     elseif location[1]-currentplace[5]==-1 then
  89.     currentplace[8]="west"
  90.     turtle.back()
  91.     return true
  92.     else
  93.     currentplace[8]="east"
  94.     turtle.back()
  95.     return true
  96.     end
  97.     else
  98.     right()
  99.     if turtle.detect()==false then
  100.     fuel()
  101.     turtle.forward()
  102.     location[1], location[2], location[3]=gps.locate()
  103.     if location[3]-currentplace[7]==-1 then
  104.     currentplace[8]="north"
  105.     fuel()
  106.     turtle.back()
  107.     left()
  108.     return true
  109.     elseif location[3]-currentplace[7]==1 then
  110.     currentplace[8]="south"
  111.     fuel()
  112.     turtle.back()
  113.     left()
  114.     return true
  115.     elseif location[1]-currentplace[5]==-1 then
  116.     currentplace[8]="west"
  117.     fuel()
  118.     turtle.back()
  119.     left()
  120.     return true
  121.     else
  122.     currentplace[8]="east"
  123.     fuel()
  124.     turtle.back()
  125.     left()
  126.     return true
  127.     end
  128.     else
  129.     right()
  130.     if turtle.detect()==false then
  131.     fuel()
  132.     turtle.forward()
  133.     location[1], location[2], location[3]=gps.locate()
  134.     if location[3]-currentplace[7]==-1 then
  135.     currentplace[8]="north"
  136.     fuel()
  137.     turtle.back()
  138.     right()
  139.     right()
  140.     return true
  141.     elseif location[3]-currentplace[7]==1 then
  142.     currentplace[8]="south"
  143.     fuel()
  144.     turtle.back()
  145.     right()
  146.     right()
  147.     return true
  148.     elseif location[1]-currentplace[5]==-1 then
  149.     currentplace[8]="west"
  150.     fuel()
  151.     turtle.back()
  152.     right()
  153.     right()
  154.     return true
  155.     else
  156.     currentplace[8]="east"
  157.     fuel()
  158.     turtle.back()
  159.     right()
  160.     right()
  161.     return true
  162.     end
  163.     else
  164.     right()
  165.     if turtle.detect()==false then
  166.     fuel()
  167.     turtle.forward()
  168.     location[1], location[2], location[3]=gps.locate()
  169.     if location[3]-currentplace[7]==-1 then
  170.     currentplace[8]="north"
  171.     fuel()
  172.     turtle.back()
  173.     right()
  174.     return true
  175.     elseif location[3]-currentplace[7]==1 then
  176.     currentplace[8]="south"
  177.     fuel()
  178.     turtle.back()
  179.     right()
  180.     return true
  181.     elseif location[1]-currentplace[5]==-1 then
  182.     currentplace[8]="west"
  183.     fuel()
  184.     turtle.back()
  185.     right()
  186.     return true
  187.     else
  188.     currentplace[8]="east"
  189.     fuel()
  190.     turtle.back()
  191.     right()
  192.     return true
  193.     end
  194.     else
  195.     right()
  196.     return false
  197.     end
  198.     end
  199.     end
  200.     end
  201.  
  202.     -- end of gps facing code
  203.  
  204.     end
  205. end --of function
  206.  
  207.  
  208. --use "force" as parameter to break blocks
  209. function up(p)
  210.     if turtle.detectUp()==false then
  211.         fuel()
  212.         turtle.up()
  213.             if currentplace[2]~=nil then
  214.                 currentplace[2]=currentplace[2]+1
  215.             end
  216.             if currentplace[6]~=nil then
  217.                 currentplace[6]=currentplace[6]+1
  218.             end
  219.     elseif p=="force" then
  220.         fuel()
  221.   turtle.select(1)
  222.         turtle.digUp()
  223.         turtle.up()
  224.             if currentplace[2]~=nil then
  225.                 currentplace[2]=currentplace[2]+1
  226.             end
  227.             if currentplace[6]~=nil then
  228.                 currentplace[6]=currentplace[6]+1
  229.             end
  230.     end
  231. end
  232. --use "force" as parameter to break blocks
  233. function down(q)
  234. if turtle.detectDown()==false then
  235. fuel()
  236. turtle.down()
  237. if currentplace[2]~=nil then
  238. currentplace[2]=currentplace[2]-1
  239. end
  240. if currentplace[6]~=nil then
  241. currentplace[6]=currentplace[6]-1
  242. end
  243. elseif q=="force" then
  244. turtle.select(1)
  245. turtle.digDown()
  246. fuel()
  247. turtle.down()
  248. if currentplace[2]~=nil then
  249. currentplace[2]=currentplace[2]-1
  250. end
  251. if currentplace[6]~=nil then
  252. currentplace[6]=currentplace[6]-1
  253. end
  254. end
  255. end
  256.  
  257. function left()
  258. turtle.turnLeft()
  259. if currentplace[4]== "north" then
  260. currentplace[4]="west"
  261. elseif currentplace[4]== "west" then
  262. currentplace[4]= "south"
  263. elseif currentplace[4]=="south" then
  264. currentplace[4]="east"
  265. elseif currentplace[4]=="east" then
  266. currentplace[4]="north"
  267. end
  268. if currentplace[8]== "north" then
  269. currentplace[8]="west"
  270. elseif currentplace[8]== "west" then
  271. currentplace[8]= "south"
  272. elseif currentplace[8]=="south" then
  273. currentplace[8]="east"
  274. elseif currentplace[8]=="east" then
  275. currentplace[8]="north"
  276. end
  277. end
  278.  
  279. function right()
  280. turtle.turnRight()
  281. if currentplace[4]== "north" then
  282. currentplace[4]="east"
  283. elseif currentplace[4]== "west" then
  284. currentplace[4]= "north"
  285. elseif currentplace[4]=="south" then
  286. currentplace[4]="west"
  287. elseif currentplace[4]=="east" then
  288. currentplace[4]="south"
  289. end
  290. if currentplace[8]== "north" then
  291. currentplace[8]="east"
  292. elseif currentplace[8]== "west" then
  293. currentplace[8]= "north"
  294. elseif currentplace[8]=="south" then
  295. currentplace[8]="west"
  296. elseif currentplace[8]=="east" then
  297. currentplace[8]="south"
  298. end
  299. end
  300. --use "force" as parameter to break blocks
  301. function forward(m)
  302. if turtle.detect()==false then
  303. fuel()
  304. turtle.forward()
  305.  
  306. if currentplace[4]=="north" then
  307. currentplace[3]=currentplace[3]-1
  308. elseif currentplace[4]=="west" then
  309. currentplace[1]=currentplace[1]-1
  310. elseif currentplace[4]=="south" then
  311. currentplace[3] = currentplace[3]+1
  312. elseif currentplace[4]=="east" then
  313. currentplace[1]=currentplace[1]+1
  314. end
  315. if currentplace[8]=="north" then
  316. currentplace[7]=currentplace[7]-1
  317. elseif currentplace[8]=="west" then
  318. currentplace[5]=currentplace[5]-1
  319. elseif currentplace[8]=="south" then
  320. currentplace[7] = currentplace[7]+1
  321. elseif currentplace[8]=="east" then
  322. currentplace[5]=currentplace[5]+1
  323. end
  324.  
  325. elseif m=="force" then
  326. turtle.select(1)
  327. turtle.dig()
  328. fuel()
  329. turtle.forward()
  330.  
  331. if currentplace[4]=="north" then
  332. currentplace[3]=currentplace[3]-1
  333. elseif currentplace[4]=="west" then
  334. currentplace[1]=currentplace[1]-1
  335. elseif currentplace[4]=="south" then
  336. currentplace[3] = currentplace[3]+1
  337. elseif currentplace[4]=="east" then
  338. currentplace[1]=currentplace[1]+1
  339. end
  340. if currentplace[8]=="north" then
  341. currentplace[7]=currentplace[7]-1
  342. elseif currentplace[8]=="west" then
  343. currentplace[5]=currentplace[5]-1
  344. elseif currentplace[8]=="south" then
  345. currentplace[7] = currentplace[7]+1
  346. elseif currentplace[8]=="east" then
  347. currentplace[5]=currentplace[5]+1
  348. end
  349. end
  350. end
  351.  
  352. function getplace()
  353. return currentplace
  354. end
  355.  
  356. --moce, gps/relative
  357. function orient(mode, facing)
  358.  
  359. if mode=="gps" then --gps mode
  360.  
  361. if facing == currentplace[8] then
  362. --nothing
  363. elseif currentplace[8]=="north" and facing=="west" then
  364. left()
  365. elseif currentplace[8]=="west" and facing=="south" then
  366. left()
  367. elseif currentplace[8]=="south" and facing=="east" then
  368. left()
  369. elseif currentplace[8]=="east" and facing=="north" then
  370. left()
  371. else
  372. repeat
  373. right()
  374. until currentplace[8]==facing
  375. end
  376.  
  377. else --normal mode
  378.  
  379. if facing == currentplace[4] then
  380. --nothing
  381. elseif currentplace[4]=="north" and facing=="west" then
  382. left()
  383. elseif currentplace[4]=="west" and facing=="south" then
  384. left()
  385. elseif currentplace[4]=="south" and facing=="east" then
  386. left()
  387. elseif currentplace[4]=="east" and facing=="north" then
  388. left()
  389. else
  390. repeat
  391. right()
  392. until facing==currentplace[4]
  393. end
  394. -- end of mode selection
  395. end
  396. -- end of function
  397. end
  398.  
  399. --mode, gps/relative r, force/nil
  400. --use "force" as parameter to break blocks
  401. function goto(mode, xAxis, yAxis, zAxis, r)
  402.     if mode=="gps" then
  403.         --gpsmode
  404.         if currentplace[5] ~=nil and currentplace[6] ~=nil and currentplace[7] ~=nil and currentplace[8]~=nil then
  405.             if xAxis-currentplace[5]>0 then
  406.                 orient("gps", "east")
  407.             elseif xAxis-currentplace[5]<0 then
  408.                 orient("gps", "west")
  409.             end
  410.             while xAxis~=currentplace[5] do
  411.                 forward()
  412.             end
  413.             if zAxis-currentplace[7]>0 then
  414.                 orient("gps", "south")
  415.             elseif zAxis-currentplace[7]<0 then
  416.                 orient("gps", "north")
  417.             end
  418.             while zAxis~=currentplace[7] do
  419.                 forward(r)
  420.             end
  421.             if yAxis-currentplace[6]<0 then
  422.                 while yAxis~=currentplace[6] do
  423.                     down(r)
  424.                 end
  425.             elseif yAxis-currentplace[6]>0 then
  426.                 while yAxis~=currentplace[6] do
  427.                     up(r)
  428.                 end
  429.             end
  430.         else
  431.             print("no valid mode stated")
  432.         end
  433.     elseif mode=="relative" then
  434.         --relative positioning mode
  435.         if xAxis-currentplace[1]>0 then
  436.             orient("relative", "east")
  437.         elseif xAxis-currentplace[1]<0 then
  438.             orient("relative", "west")
  439.         end
  440.         while xAxis~=currentplace[1] do
  441.             forward(r)
  442.         end
  443.         if zAxis-currentplace[3]>0 then
  444.             orient("relative", "south")
  445.         elseif zAxis-currentplace[3]<0 then
  446.             orient("relative", "north")
  447.         end
  448.         while zAxis~=currentplace[3] do
  449.             forward(r)
  450.         end
  451.         if yAxis-currentplace[2]<0 then
  452.             while yAxis~=currentplace[2] do
  453.                 down(r)
  454.             end
  455.         elseif yAxis-currentplace[2]>0 then
  456.             while yAxis~=currentplace[2] do
  457.                 up(r)
  458.             end
  459.         end
  460.     else
  461.     print("no gps signal stated")
  462.     end
  463. end
Advertisement
Add Comment
Please, Sign In to add comment