solidity

gps tower builder

Jan 18th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.84 KB | None | 0 0
  1. --#############################################################################
  2. --#
  3. --# This program will take 4 computers, 4 wireless modems a disk drive and a
  4. --# disk to build a gps tower at max height
  5. --#
  6. --#############################################################################
  7.  
  8.  
  9. local fNames = { "south", "west", "north", "east" }
  10. local fIndex = 0
  11. local pos = {}
  12. local yStart = 0
  13. local slotComputer = 1
  14. local slotModem = 2
  15. local slotFuel = 3
  16. local slotDisk = 4
  17. local slotDrive = 5
  18. local diskSide = ""
  19. local hitSky = false
  20. pos[1], pos[2], pos[3] = "", "", ""
  21. local move = { east = {1, 0, 0}, west = {-1, 0, 0}, south = {0, 0, 1}, north = {0, 0, -1} }
  22.  
  23. function isnumber(test)
  24.     --print(type(test))
  25.     if type(test) == "number" then
  26.         return true
  27.     else
  28.         return false
  29.     end
  30. end
  31.  
  32. function isdirection(test)
  33.     if test == "east" or test == "west" or test == "north" or test == "south" then
  34.         for i = 1,4 do
  35.             if test == fNames[i] then
  36.                 fIndex = i
  37.             end
  38.         end
  39.         return true
  40.     else
  41.         return false
  42.     end
  43. end
  44.  
  45. function moveUp()
  46.     while not hitSky do
  47.         moved, dug, attack = false, false, false
  48.         moved = turtle.up()
  49.         if not moved then
  50.             if turtle.detectUp() then
  51.                 dug = turtle.digUp()
  52.             else
  53.                 attacked = turtle.attackUp()
  54.             end
  55.         else
  56.             pos[2] = pos[2] + 1
  57.         end
  58.         hitSky = not (moved or dug or attacked)
  59.         print(pos[1].." "..pos[2].." "..pos[3].." "..f)
  60.     end
  61. end
  62.  
  63. function moveDown()
  64.     while not turtle.down() do
  65.         if turtle.detectDown() then
  66.             turtle.digDown()
  67.         else
  68.             turtle.attackDown()
  69.         end
  70.     end
  71.     pos[2] = pos[2] - 1
  72.     print(pos[1].." "..pos[2].." "..pos[3].." "..f)
  73. end
  74.  
  75. function moveForward()
  76.     while not turtle.forward() do
  77.         if turtle.detect() then
  78.             turtle.dig()
  79.         else
  80.             turtle.attack()
  81.         end
  82.     end
  83.     for i = 1,3 do
  84.         pos[i] = pos[i] + move[f][i]
  85.     end
  86.     print(pos[1].." "..pos[2].." "..pos[3].." "..f)
  87. end
  88.  
  89. function left()
  90.     turtle.turnLeft()
  91.     fIndex = fIndex - 1
  92.     if fIndex == 0 then
  93.         fIndex = 4
  94.     end
  95.     f = fNames[fIndex]
  96.     print(pos[1].." "..pos[2].." "..pos[3].." "..f)
  97. end
  98.  
  99. function right()
  100.     turtle.turnRight()
  101.     fIndex = fIndex + 1
  102.     if fIndex == 5 then
  103.         fIndex = 1
  104.     end
  105.     f = fNames[fIndex]
  106.     print(pos[1].." "..pos[2].." "..pos[3].." "..f)
  107. end
  108.    
  109.  
  110. if turtle then      -- build a gps tower
  111.     print("i'm a turtle")
  112.     print("provide me with the following items:")
  113.     print("slot 1: 4 computers")
  114.     print("slot 2: 4 wireless modems")
  115.     print("slot 3: enough fuel to get me to max y and back. < insert calculated amount here >")
  116.     print("what are my current coordinates? use the integers on the f3 screen or REI's minimap")
  117.     while not isnumber(pos[1]) do
  118.         print("x = ")
  119.         pos[1] = tonumber(read())
  120.         if not isnumber(pos[1]) then
  121.             print("that's not a number")
  122.         end
  123.     end
  124.     while not isnumber(pos[2]) do
  125.         print("y = ")
  126.         pos[2] = tonumber(read())
  127.         if not isnumber(pos[2]) then
  128.             print("that's not a number")
  129.         end
  130.     end
  131.     yStart = pos[2]
  132.     while not isnumber(pos[3]) do
  133.         print("z = ")
  134.         pos[3] = tonumber(read())
  135.         if not isnumber(pos[3]) then
  136.             print("that's not a number")
  137.         end
  138.     end
  139.     while not isdirection(f) do
  140.         print("which direction am i facing?")
  141.         f = read()
  142.         if not isdirection(f) then
  143.             print("that's not a valid direction")
  144.         end
  145.     end
  146.     -- check inventory (can only check for matching numbers atm)
  147.     if turtle.getItemCount(1) == 4 and turtle.getItemCount(2) == 4 then
  148.         --get disk from drive
  149.         for k,v in pairs(redstone.getSides()) do
  150.             if disk.isPresent(v) then
  151.                 diskSide = v
  152.                 print(diskSide)
  153.             end
  154.         end
  155.         if diskSide == "left" then
  156.             turtle.turnLeft()
  157.             turtle.suck()
  158.             turtle.dig()
  159.             turtle.turnRight()
  160.         elseif diskSide == "right" then
  161.             turtle.turnRight()
  162.             turtle.suck()
  163.             turtle.dig()
  164.             turtle.turnLeft()
  165.         elseif diskSide == "top" then
  166.             turtle.suckUp()
  167.             turtle.digUp()
  168.         elseif diskSide == "bottom" then
  169.             turtle.suckDown()
  170.             turtle.digDown()
  171.         elseif diskSide == "front" then
  172.             turtle.suck()
  173.             turtle.dig()
  174.         elseif diskSide == "back" then
  175.             turtle.turnLeft()
  176.             turtle.turnLeft()
  177.             turtle.suck()
  178.             turtle.dig()
  179.             turtle.turnLeft()
  180.             turtle.turnLeft()
  181.         end
  182.         turtle.select(slotFuel)
  183.         while turtle.getFuelLevel() < 64 + 2*(254-pos[2]) do
  184.             turtle.refuel(1)
  185.         end
  186.         moveUp()
  187.         -- build gps array
  188.         for c = 1,2 do
  189.             for d = 1,2 do
  190.                 for m = 1,3 do
  191.                     moveForward()
  192.                 end
  193.                 turtle.select(slotComputer)
  194.                 turtle.place()
  195.                 local gpspos = {}
  196.                 for i = 1,3 do
  197.                     gpspos[i] = pos[i] + move[f][i]
  198.                 end
  199.                 left()
  200.                 moveForward()
  201.                 moveForward()
  202.                 right()
  203.                 moveForward()
  204.                 right()
  205.                 turtle.select(slotModem)
  206.                 turtle.place()
  207.                 right()
  208.                 moveForward()
  209.                 left()
  210.                 moveForward()
  211.                 moveForward()
  212.                 moveForward()
  213.                 left()
  214.                 turtle.select(slotDrive)
  215.                 turtle.place()
  216.                 turtle.select(slotDisk)
  217.                 turtle.drop()
  218.                 --write gps program to disk
  219.                 local labelString = "os.setComputerLabel('gps_"..gpspos[1].."_"..gpspos[2].."_"..gpspos[3].."')"
  220.                 local gpsString = "shell.run('gps','host',"..gpspos[1]..","..gpspos[2]..","..gpspos[3]..")"
  221.                 f = fs.open("disk/gpsstartup", "w")
  222.                 f.writeLine(labelString)
  223.                 f.writeLine(gpsString)
  224.                 f.close()
  225.                 -- turn on computer
  226.                 left()
  227.                 moveForward()
  228.                 right()
  229.                 m = peripheral.wrap("front")
  230.                 m.turnOn()
  231.                 -- retreive disk and drive
  232.                 right()
  233.                 moveForward()
  234.                 left()
  235.                 turtle.select(slotDisk)
  236.                 turtle.suck()
  237.                 turtle.select(slotDrive)
  238.                 turtle.dig()
  239.                 -- reboot gps computer
  240.                 left()
  241.                 moveForward()
  242.                 --m = peripheral.wrap("right")
  243.                 --m.reboot()
  244.                 -- go back to center
  245.                 left()
  246.                 for i = 1,3 do
  247.                     moveForward()
  248.                 end
  249.             end
  250.             moveDown()
  251.             moveDown()
  252.             left()
  253.         end
  254.     end
  255.     while pos[2] > yStart do
  256.         moveDown()
  257.     end
  258. else
  259.     print("i'm a computer")
  260.     fs.move("disk/gpsstartup","startup")
  261.     sleep(5)
  262.     os.reboot()
  263. end
Advertisement
Add Comment
Please, Sign In to add comment