Advertisement
Chocobot

[CC]Movement System (installer)

Sep 14th, 2012
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.96 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. print("Do you want to instal the coördinate based movement system made by Cookiebal?")
  4. local answer = read()
  5. if answer == "yes" then
  6.  
  7. file = io.open("movesys", "w")
  8. file:write([[
  9. maxx, maxy = term.getSize()
  10.  
  11. pervarpath = "/movementsystem/pervar/"
  12.  
  13. function mspervarcreate(pervarname)
  14. pervarfile = io.open(pervarpath..pervarname..".txt", "r")
  15. if pervarfile
  16. then
  17.   pervarfile:close()
  18.   return "Variable already exists"  
  19. else
  20.   pervarfile = io.open(pervarpath..pervarname..".txt","w")
  21. end
  22. end
  23.  
  24. function mspervardelete(pervarname)
  25. pervarfile = io.open(pervarpath..pervarname..".txt", "r")
  26. if pervarfile
  27. then
  28.   pervarfile:close()
  29.   fs.delete(pervarpath..pervarname..".txt")
  30. else
  31.   return "Variable doesn't exist"  
  32. end
  33. end
  34.  
  35. function mspervarchange(pervarname, newvalue)
  36. pervarfile = io.open(pervarpath..pervarname..".txt", "r")
  37. if pervarfile
  38. then
  39.   pervarfile:close()
  40.   pervarfile = io.open(pervarpath..pervarname..".txt","w")
  41.   pervarfile:write(newvalue)
  42.   pervarfile:close()
  43. else
  44.   return "Variable doesn't exist"
  45. end
  46. end
  47.  
  48. function mspervarread(pervarname)
  49. pervarfile = io.open(pervarpath..pervarname..".txt", "r")
  50. if pervarfile
  51. then
  52.   pervarcontent = pervarfile:read()
  53.   pervarfile:close()
  54.   return pervarcontent
  55. else
  56.   return "Variable doesn't exist"
  57. end
  58. end
  59.  
  60.  
  61. function facenorth()
  62. local currentface = tonumber(mspervarread("currentface"))
  63. if currentface == 0 then turtle.turnRight() turtle.turnRight() end
  64. if currentface == 1 then turtle.turnRight() end
  65. if currentface == 3 then turtle.turnLeft() end
  66. mspervarchange("currentface", 2)
  67. end
  68.  
  69. function faceeast()
  70. local currentface = tonumber(mspervarread("currentface"))
  71. if currentface == 1 then turtle.turnRight() turtle.turnRight() end
  72. if currentface == 2 then turtle.turnRight() end
  73. if currentface == 0 then turtle.turnLeft() end
  74. mspervarchange("currentface", 3)
  75. end
  76.  
  77. function facesouth()
  78. local currentface = tonumber(mspervarread("currentface"))
  79. if currentface == 2 then turtle.turnRight() turtle.turnRight() end
  80. if currentface == 3 then turtle.turnRight() end
  81. if currentface == 1 then turtle.turnLeft() end
  82. mspervarchange("currentface", 0)
  83. end
  84.  
  85. function facewest()
  86. local currentface = tonumber(mspervarread("currentface"))
  87. if currentface == 3 then turtle.turnRight() turtle.turnRight() end
  88. if currentface == 0 then turtle.turnRight() end
  89. if currentface == 2 then turtle.turnLeft() end
  90. mspervarchange("currentface", 1)
  91. end
  92.  
  93. function movenorth()
  94. facenorth()
  95. didmove = turtle.forward()
  96.   if didmove then
  97.   local newzcoord = mspervarread("coordz") - 1
  98.   mspervarchange("coordz", newzcoord)
  99. else
  100.   return "blocked"
  101. end
  102. end
  103.  
  104. function movesouth()
  105. facesouth()
  106. didmove = turtle.forward()
  107.   if didmove then
  108.   local newzcoord = mspervarread("coordz") + 1
  109.   mspervarchange("coordz", newzcoord)
  110. else
  111.   return "blocked"
  112. end
  113. end
  114.  
  115. function movewest()
  116. facewest()
  117. didmove = turtle.forward()
  118.   if didmove then
  119.   local newxcoord = mspervarread("coordx") - 1
  120.   mspervarchange("coordx", newxcoord)
  121. else
  122.   return "blocked"
  123. end
  124. end
  125.  
  126. function moveeast()
  127. faceeast()
  128. didmove = turtle.forward()
  129.   if didmove then
  130.   local newxcoord = mspervarread("coordx") + 1
  131.   mspervarchange("coordx", newxcoord)
  132. else
  133.   return "blocked"
  134. end
  135. end
  136.  
  137. function moveup()
  138. didmove = turtle.up()
  139.   if didmove then
  140.   local newxcoord = mspervarread("coordy") + 1
  141.   mspervarchange("coordy", newxcoord)
  142. else
  143.   return "blocked"
  144. end
  145. end
  146.  
  147. function movedown()
  148. didmove = turtle.down()
  149.   if didmove then
  150.   local newxcoord = mspervarread("coordy") - 1
  151.   mspervarchange("coordy", newxcoord)
  152. else
  153.   return "blocked"
  154. end
  155. end
  156.  
  157.  
  158.  
  159. function printui()
  160.  term.clear()
  161.  for rawr=1,1 do
  162.  if maxx > 16 then
  163.   if maxx > 34 then
  164.    if maxx > 50 then
  165.     term.setCursorPos(((maxx-47)/2)+1, 1)
  166.     write("Coordinate Based Movement System - by Cookiebal")
  167.     break
  168.    end
  169.    term.setCursorPos((maxx-32)/2, 1)
  170.    write("Movement System - by Cookiebal")
  171.    break
  172.   end
  173.   term.setCursorPos((maxx-15)/2, 1)
  174.   write("Movement System")
  175.  end
  176.  end
  177.  term.setCursorPos(1,2)
  178.  for n=1, maxx-1 do write("=") end
  179.  term.setCursorPos(1,3)
  180. end
  181.  
  182. function coordchoicequestion()
  183.  print("Do you want to use the -current- coördinates, -locate- them via gps or enter them -manual-ly?")
  184.  while true do
  185.   input = read()
  186.   if input == "current" or input == "locate" or input == "manual" then
  187.    coordchoice = input
  188.    break
  189.   else print('Type either "current", "locate" or "manual"')
  190.   end
  191.  end
  192. end
  193.  
  194. function coordmanual()
  195.  print("Give the coördinates and which direction the turtle is facing.")
  196.  write("X: ")
  197.  xcoord = read()
  198.  write("Y: ")
  199.  ycoord = read()
  200.  write("Z: ")
  201.  zcoord = read()
  202.  write("Direction: ")
  203.  direction = read()
  204.  
  205.  mspervarchange("coordx", xcoord)
  206.  mspervarchange("coordy", ycoord)
  207.  mspervarchange("coordz", zcoord)
  208.  mspervarchange("currentface", direction)
  209. end
  210.  
  211. function coordlocate()
  212.  print("Getting Coördinates with GPS")
  213.  rednet.open("right")
  214.  xcoord, ycoord, zcoord = gps.locate(2)
  215.  if xcoord ~= nil then
  216.   mspervarchange("coordx", xcoord)
  217.   mspervarchange("coordy", ycoord)
  218.   mspervarchange("coordz", zcoord)
  219.  
  220.   for n=1, 4 do
  221.    local getdirtry = turtle.forward()
  222.     if getdirtry == true then
  223.      xcoord2, ycoord2, zcoord2 = gps.locate(2)
  224.      xcoorddif = xcoord2-xcoord
  225.      zcoorddif = zcoord2-zcoord
  226.     if xcoorddif == 1 then direction = 3
  227.     elseif xcoorddif == -1 then direction = 1
  228.     elseif zcoorddif == 1 then direction = 0
  229.     elseif zcoord == -1 then direction = 2
  230.     end
  231.     mspervarchange("coordx", xcoord2)
  232.     mspervarchange("coordy", ycoord2)
  233.     mspervarchange("coordz", zcoord2)
  234.     mspervarchange("currentface", direction)
  235.     break
  236.    end
  237.    turtle.turnRight()
  238.   end
  239.  else
  240.   print("Couldn't find coördinates")
  241.   print("Please enter them manually")
  242.   sleep(1)
  243.   os.pullEvent()
  244.   coordmanual()
  245.  end
  246. end
  247.  
  248. function showwaypointlist()
  249.  term.setCursorPos(1,6)
  250.  write(mspervarread("coordx")..","..mspervarread("coordy")..","..mspervarread("coordz"))
  251.  term.setCursorPos(1,7)
  252.  for n=1, #waypoint+1 do
  253.   print(waypoint[n])
  254.  end
  255. end
  256.  
  257. function getnextwaypoint()
  258.  term.setCursorPos(1,3)
  259.  term.clearLine()
  260.  term.setCursorPos(1,4)
  261.  term.clearLine()
  262.  term.setCursorPos(1,5)
  263.  term.clearLine()
  264.  term.setCursorPos(1,3)
  265.  print('Give the next waypoint in x,y,z format (ex: -43,78,62) or type "done"')
  266.  write("Waypoint Coördinate: ")
  267.  newwaypoint = read()
  268.  if newwaypoint ~= "done" then
  269.   wpn = wpn+1
  270.   waypoint[wpn] = newwaypoint
  271.  end
  272. end
  273.  
  274. function movetowaypoints()
  275.  file = io.open("/movementsystem/waypoints.txt", "w")
  276.  for n=1, #waypoint do
  277.   file:write(waypoint[n].."\n")
  278.  end
  279.  file:close()
  280. end
  281.  
  282. printui()
  283. coordchoicequestion()
  284. printui()
  285. if coordchoice == "manual" then coordmanual() end
  286. if coordchoice == "locate" then coordlocate() end
  287.  
  288. printui()
  289. waypoint = {}
  290. wpn = 0
  291. while true do
  292.  getnextwaypoint()
  293.  if newwaypoint == "done" then break end
  294.  showwaypointlist()
  295. end
  296.  
  297. movetowaypoints()
  298. term.clear()
  299. printui()
  300. shell.run("/movementsystem/msfollowwaypoints")
  301.  
  302.  
  303. ]])
  304. file:close()
  305.  
  306. fs.makeDir("movementsystem")
  307. fs.makeDir("movementsystem/pervar")
  308.  
  309. file = io.open("movementsystem/mspathfinding", "w")
  310. file:write([[
  311.  
  312.  
  313. tArgs = {...}
  314. ngtx = tArgs[1]
  315. ngty = tArgs[2]
  316. ngtz = tArgs[3]
  317.  
  318. facenumbertostring = {"south","west","north","east"}
  319.  
  320. function createvars()
  321. mspervarcreate("coordx") -- +:east -: west
  322. mspervarcreate("coordy") -- +:up -: down
  323. mspervarcreate("coordz") -- +:south -:north
  324. mspervarcreate("currentface")
  325. mspervarcreate("gotox")
  326. mspervarcreate("gotoy")
  327. mspervarcreate("gotoz")
  328.  
  329. mspervarchange("coordx", 0)
  330. mspervarchange("coordy", 0)
  331. mspervarchange("coordz", 0)
  332. mspervarchange("currentface", 0) -- 0=south,1=west,2=north,3=east
  333. end
  334.  
  335. function showcoords()
  336. print("The current coordinates are: ("..mspervarread("coordx")..","..mspervarread("coordy")..","..mspervarread("coordz")..") and it faces to the "..facenumbertostring[mspervarread("currentface")+1]..".")
  337. end
  338.  
  339.  
  340.  
  341. function avoidsmallobstaclehorizontal()
  342. stntbl = ("print(\"rawr\")")
  343. movedirnum = tonumber(mspervarread("currentface"))
  344. c = moveup()
  345. if c == "blocked"
  346. then
  347.   c = movedown()
  348.   if c == "blocked"
  349.   then
  350.    if movedirnum == 3 then c = movesouth()
  351.    elseif movedirnum == 2 then c = moveeast()
  352.    elseif movedirnum == 1 then c = movenorth()
  353.    elseif movedirnum == 0 then c = movewest()
  354.    end
  355.    if c == "blocked"
  356.    then
  357.         if movedirnum == 0 then c = moveeast()
  358.         elseif movedirnum == 1 then c = movesouth()
  359.         elseif movedirnum == 2 then c = movewest()
  360.         elseif movedirnum == 3 then c = movenorth()
  361.         end
  362.    end
  363.   end
  364. end
  365. end
  366.  
  367. function avoidsmallobstacleup()
  368. passedobstacle = false
  369. c = movenorth()
  370. if c ~= "blocked"
  371. then
  372.   d = moveup()
  373.   if d ~= "blocked"
  374.   then
  375.    passedobstacle = true
  376.   else
  377.    movesouth()
  378.   end
  379. end
  380.  
  381. if passedobstacle ~= true
  382. then
  383.   c = movesouth()
  384.   if c ~= "blocked"
  385.   then
  386.    d = moveup()
  387.    if d ~= "blocked"
  388.    then
  389.         passedobstacle = true
  390.    else
  391.         movenorth()
  392.    end
  393.   end
  394. end
  395.  
  396. if passedobstacle ~= true
  397. then
  398.   c = moveeast()
  399.   if c ~= "blocked"
  400.   then
  401.    d = moveup()
  402.    if d ~= "blocked"
  403.    then
  404.         passedobstacle = true
  405.    else
  406.         movewest()
  407.    end
  408.   end
  409. end
  410.  
  411. if passedobstacle ~= true
  412. then
  413.   c = movewest()
  414.   if c ~= "blocked"
  415.   then
  416.    d = moveup()
  417.    if d ~= "blocked"
  418.    then
  419.         passedobstacle = true
  420.    else
  421.         moveeast()
  422.    end
  423.   end
  424. end
  425. end
  426.  
  427. function avoidsmallobstacledown()
  428. passedobstacle = false
  429.  
  430. c = movenorth()
  431. if c ~= "blocked"
  432. then
  433.   d = movedown()
  434.   if d ~= "blocked"
  435.   then
  436.    passedobstacle = true
  437.   else
  438.    movesouth()
  439.   end
  440. end
  441.  
  442. if passedobstacle ~= true
  443. then
  444.   c = moveeast()
  445.   if c ~= "blocked"
  446.   then
  447.    d = movedown()
  448.    if d ~= "blocked"
  449.    then
  450.         passedobstacle = true
  451.    else
  452.         movewest()
  453.    end
  454.   end
  455. end
  456.  
  457. if passedobstacle ~= true
  458. then
  459.   c = movewest()
  460.   if c ~= "blocked"
  461.   then
  462.    d = movedown()
  463.    if d ~= "blocked"
  464.    then
  465.         passedobstacle = true
  466.    else
  467.         moveeast()
  468.    end
  469.   end
  470. end
  471.  
  472. if passedobstacle ~= true
  473. then
  474.   c = movesouth()
  475.   if c ~= "blocked"
  476.   then
  477.    d = movedown()
  478.    if d ~= "blocked"
  479.    then
  480.         passedobstacle = true
  481.    else
  482.         movenorth()
  483.    end
  484.   end
  485. end
  486.  
  487. end
  488.  
  489. function settargetcoords(gtx,gty,gtz)
  490. mspervarchange("gotox", gtx)
  491. mspervarchange("gotoy", gty)
  492. mspervarchange("gotoz", gtz)
  493. end
  494.  
  495. function moveclosertotargetx()
  496. currentx = tonumber(mspervarread("coordx"))
  497. targetx = tonumber(mspervarread("gotox"))
  498. if currentx == targetx
  499. then
  500.   return "reached x"
  501. elseif currentx > targetx
  502. then
  503.   b = movewest()
  504. elseif currentx < targetx
  505. then
  506.   b = moveeast()
  507. end
  508. end
  509.  
  510.  
  511. function moveclosertotargetz()
  512. currentz = tonumber(mspervarread("coordz"))
  513. targetz = tonumber(mspervarread("gotoz"))
  514. if currentz == targetz
  515. then
  516.   return "reached z"
  517. elseif currentz > targetz
  518. then
  519.   b = movenorth()
  520. elseif currentz < targetz
  521. then
  522.   b = movesouth()
  523. end
  524. end
  525.  
  526.  
  527. function moveclosertotargety()
  528. currenty = tonumber(mspervarread("coordy"))
  529. targety = tonumber(mspervarread("gotoy"))
  530. if currenty == targety
  531. then
  532.   return "reached y"
  533. elseif currenty > targety
  534. then
  535.   b = movedown()
  536.   if b ~= nil then b = (b.."down") end
  537. elseif currenty < targety
  538. then
  539.   b = moveup()
  540.   if b ~= nil then b = (b.."up") end
  541. end
  542. end
  543.  
  544.  
  545. function movetotarget()
  546. repeat
  547. repeat
  548.   a = moveclosertotargetx()
  549.   if b == "blocked" then avoidsmallobstaclehorizontal() end
  550. until a == "reached x"
  551. repeat
  552.   a = moveclosertotargetz()
  553.   if b == "blocked" then avoidsmallobstaclehorizontal() end
  554. until a == "reached z"
  555. repeat
  556.   a = moveclosertotargety()
  557.   if b == "blockedup" then avoidsmallobstacleup() end
  558.   if b == "blockeddown" then avoidsmallobstacledown() end
  559. until a == "reached y"
  560. until mspervarread("coordx") == mspervarread("gotox") and mspervarread("coordz") == mspervarread("gotoz") and mspervarread("coordy") == mspervarread("gotoy")
  561. end
  562.  
  563. settargetcoords(ngtx,ngty,ngtz)
  564. movetotarget()
  565.  
  566. ]])
  567. file:close()
  568.  
  569. file = io.open("movementsystem/msfollowwaypoints", "w")
  570. file:write([[
  571. function readwaypointlist()
  572. waypointlist = {}
  573. file = io.open("movementsystem/waypoints.txt", "r")
  574. n = 0
  575. while true do
  576.   n = n + 1
  577.   nextwaypointline = file:read()
  578.   if nextwaypointline == nil then break end
  579.   waypointlist[n] = nextwaypointline
  580. end
  581. end
  582.  
  583. function formatwaypoint(waypointstring)
  584. wpsx = nil
  585. wpsy = nil
  586. wpsz = nil
  587. wpsh2 = waypointstring
  588. for i=1, 3 do
  589.   wpsh = wpsh2
  590.   n = 0
  591.   while true do
  592.    n = n + 1
  593.    fwpsc = string.sub(wpsh,1,1)
  594.    wpsh = string.sub(wpsh,2)
  595.    if fwpsc == "" then break end
  596.    if fwpsc == "," then break end
  597.   end
  598.   if i == 1 then wpsx = tonumber(string.sub(wpsh2,1,n-1)) wpsh2 = string.sub(wpsh2,n+1)
  599.   elseif i == 2 then wpsy = tonumber(string.sub(wpsh2,1,n-1)) wpsh2 = string.sub(wpsh2,n+1)
  600.   elseif i == 3 then wpsz = tonumber(string.sub(wpsh2,1,n-1))
  601.   end
  602. end
  603. return wpsx, wpsy, wpsz
  604. end
  605.  
  606. function formatwaypointlist()
  607. waypointx = {}
  608. waypointy = {}
  609. waypointz = {}
  610. for j=1, #waypointlist do
  611.   waypointx[j], waypointy[j], waypointz[j] = formatwaypoint(waypointlist[j])
  612. end
  613. end
  614.  
  615. function followwaypointlist()
  616. for n=1, #waypointlist do
  617.   print(waypointx[n].." "..waypointy[n].." "..waypointz[n])
  618.   shell.run("/movementsystem/mspathfinding", waypointx[n], waypointy[n], waypointz[n])
  619. end
  620. end
  621.  
  622. readwaypointlist()
  623. formatwaypointlist()
  624. followwaypointlist()
  625.  
  626. ]])
  627. file:close()
  628.  
  629. file = io.open("movementsystem/waypoints.txt", "w")
  630. file:close()
  631.  
  632. file = io.open("movementsystem/pervar/coordx.txt", "w")
  633. file:close()
  634. file = io.open("movementsystem/pervar/coordy.txt", "w")
  635. file:close()
  636. file = io.open("movementsystem/pervar/coordz.txt", "w")
  637. file:close()
  638. file = io.open("movementsystem/pervar/currentface.txt", "w")
  639. file:close()
  640.  
  641. file = io.open("movementsystem/pervar/gotox.txt", "w")
  642. file:close()
  643. file = io.open("movementsystem/pervar/gotoy.txt", "w")
  644. file:close()
  645. file = io.open("movementsystem/pervar/gotoz.txt", "w")
  646. file:close()
  647.  
  648. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement