Advertisement
NanoBob

powerGuard

Mar 22nd, 2015
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.74 KB | None | 0 0
  1. local speaker=peripheral.wrap("right")
  2. rednet.open("left")
  3. local lastPlayer=nil
  4. local pos={["x"]=0,["y"]=0,["z"]=0}
  5. local homePos={["x"]=-2489,["y"]=49,["z"]=2703}
  6. local teleporters={}
  7. local talking=true
  8. local warningLevel=5
  9.  
  10. if fs.exists("database")==false then
  11.     shell.run("pastebin get PdhBaBdZ database")
  12. end
  13. local success=os.loadAPI("database")
  14.  
  15. if success==false or database==nil then
  16.     error "The database system failed to load"
  17. end
  18.  
  19. if fs.exists("databases/data")==false then
  20.     database.createDataFile("data")
  21. end
  22.  
  23. if fs.exists("databases/position")==false then
  24.     database.createDataFile("position")
  25. end
  26.  
  27. function outputMessage(message,range,language)
  28.     if talking==true and type(message)=="string" then
  29.         if range==nil then
  30.             speaker.speak(message)
  31.         elseif lanugage==nil then
  32.             speaker.speak(message,range)
  33.         else
  34.             speaker.speak(message,range,language)
  35.         end
  36.     end
  37. end
  38.  
  39. function changePos(key,change)
  40.     if pos[key]==nil then
  41.         pos[key]=0
  42.     end
  43.     pos[key]=pos[key]+change
  44.     database.setData("position",key,pos[key])
  45. end
  46.  
  47. function updatePos(key)
  48.     database.setData("position",key,pos[key])
  49. end
  50.  
  51. function getFilePos()
  52.     if database.getData("position","x")==false then
  53.         pos.x=0
  54.         pos.y=0
  55.         pos.z=0
  56.         database.setData("position","x",0)
  57.         database.setData("position","y",0)
  58.         database.setData("position","z",0)
  59.     else
  60.         pos.x=database.getData("position","x")
  61.         pos.y=database.getData("position","y")
  62.         pos.z=database.getData("position","z")
  63.     end
  64. end
  65.  
  66. getFilePos()
  67.  
  68. function move(direction,blocks)
  69.     if direction=="up" then
  70.         for i=1,blocks do
  71.             moved=turtle.up()
  72.             if moved==true then
  73.                 changePos("y",1)
  74.             else
  75.                 return false
  76.             end
  77.         end
  78.     elseif direction=="down" then
  79.         for i=1,blocks do
  80.             moved=turtle.down()
  81.             if moved==true then
  82.                 changePos("y",-1)
  83.             else
  84.                 return false
  85.             end
  86.         end
  87.     elseif direction=="back" then
  88.         local facing=string.lower(getFacing())
  89.         for i=1,blocks do  
  90.             local moved=turtle.back()
  91.             if moved==false then return false end  
  92.             if facing=="north" then
  93.                 changePos("z",1)
  94.             elseif facing=="east" then
  95.                 changePos("x",-1)
  96.             elseif facing=="south" then
  97.                 changePos("z",-1)
  98.             elseif facing=="west" then
  99.                 changePos("x",1)       
  100.             end
  101.         end
  102.     else
  103.         local facing=string.lower(getFacing())
  104.         for i=1,blocks do
  105.             local moved=turtle.forward()
  106.             if moved==false then return false end
  107.             if facing=="north" then
  108.                 changePos("z",-1)
  109.             elseif facing=="east" then
  110.                 changePos("x",1)   
  111.             elseif facing=="south" then
  112.                 changePos("z",1)
  113.             elseif facing=="west" then
  114.                 changePos("x",-1)      
  115.             end
  116.         end
  117.     end
  118.     return true
  119. end
  120.  
  121. local facingTable={
  122.     ["north"]=1,
  123.     ["east"]=2,
  124.     ["south"]=3,
  125.     ["west"]=4,
  126. }
  127.  
  128. local facingTable2={
  129.     [0]="west","north","east","south","west","north"
  130. }
  131.  
  132. function turn(direction)
  133.     if direction=="right" then
  134.         turtle.turnRight()
  135.         local currentFacingID=facingTable[facingDirection]
  136.         facingDirection=facingTable2[currentFacingID+1]
  137.     elseif direction=="left" then
  138.         turtle.turnLeft()
  139.         local currentFacingID=facingTable[facingDirection]
  140.         facingDirection=facingTable2[currentFacingID-1]
  141.     end
  142. end
  143.  
  144. function setFacing(facing)
  145.     local facing=string.lower(facing)
  146.     local currentFacing=string.lower(getFacing())
  147.     if facing==currentFacing then
  148.         return
  149.     else
  150.         local currentFacingIndex=facingTable[currentFacing]
  151.         local facingIndex=facingTable[facing]
  152.         local turns=facingIndex-currentFacingIndex
  153.         if turns<0 then
  154.             turns=turns+4
  155.         end
  156.         if turns==3 then
  157.             turn("left")
  158.         else
  159.             for i=1,turns do
  160.                 turn("right")
  161.             end
  162.         end
  163.     end
  164.    
  165. end
  166.  
  167. function getFacing()
  168.     return facingDirection
  169. end
  170.  
  171. function getCompassFacing()
  172.     turtle.select(2)
  173.     turtle.equipRight()
  174.     local compass=peripheral.wrap("right")
  175.     local heading=compass.getFacing()
  176.     turtle.select(2)
  177.     turtle.equipRight()
  178.     facingDirection=heading
  179. end
  180. getCompassFacing()
  181.  
  182. function outputMessage(message,range,language)
  183.     if talking==true and type(message)=="string" then
  184.         if range==nil then
  185.             speaker.speak(message)
  186.         elseif lanugage==nil then
  187.             speaker.speak(message,range)
  188.         else
  189.             speaker.speak(message,range,language)
  190.         end
  191.     end
  192. end
  193.  
  194. function pathFind(x,y,z)
  195.     local yDistance=(pos.y-y)*-1
  196.     print("Y axis travel distance : "..yDistance)
  197.     local yTravel=true
  198.     if yDistance>0 then
  199.         local yTravel=move("up",yDistance)
  200.     elseif yDistance<0 then
  201.         local yTravel=move("down",yDistance*-1)
  202.     end
  203.     local xDistance=(pos.x-x)*-1
  204.     print("X axis travel distance : "..xDistance)
  205.     local xTravel=true
  206.     if xDistance>0 then
  207.         setFacing("east")
  208.     elseif xDistance<0 then
  209.         setFacing("west")
  210.         xDistance=xDistance*-1
  211.     end
  212.     xTravel=move("forward",xDistance)
  213.     local zDistance=(pos.z-z)*-1
  214.     print("Z axis travel distance : "..zDistance)
  215.     local zTravel=true
  216.     if zDistance>0 then
  217.         setFacing("south")
  218.     elseif zDistance<0 then
  219.         setFacing("north")
  220.         zDistance=zDistance*-1
  221.     end
  222.     zTravel=move("forward",zDistance)
  223.     if xTravel==false or zTravel==false or yTravel==false then
  224.         pathFind(x,y,z)
  225.     end
  226. end
  227.  
  228. function getWood()
  229.     pathFind(-2489,52,2703)
  230.     pathFind(-2508,52,2705)
  231.    
  232.     pathFind(-2508,53,2705)
  233.     repeat
  234.         rednet.broadcast("open up","turtleShaft")
  235.         local id,message,protocol=rednet.receive(1)        
  236.     until message=="I have accepted your request!" and protocol=="returnMessage"
  237.     sleep(0.1)
  238.     pathFind(-2508,61,2705)
  239.     sleep(2)
  240.     pathFind(-2508,63,2705)
  241.    
  242.    
  243.     pathFind(pos.x,pos.y+8,pos.z)
  244.     pathFind(-2494,pos.y,2664)
  245.     pathFind(-2494,64,2664)
  246.     setFacing("west")
  247.     turtle.suck(64)
  248.     pathFind(pos.x,pos.y+8,pos.z)
  249.     pathFind(-2508,pos.y,2705)
  250.    
  251.     pathFind(-2508,64,2705)
  252.     repeat
  253.         rednet.broadcast("open down","turtleShaft")
  254.         local id,message,protocol=rednet.receive(1)        
  255.     until message=="I have accepted your request!" and protocol=="returnMessage"
  256.     sleep(0.1)
  257.     pathFind(-2508,62,2705)
  258.     sleep(2)
  259.     pathFind(-2508,54,2705)
  260.    
  261.     pathFind(-2508,51,2705)
  262.     pathFind(-2517,51,2706)
  263.     pathFind(-2517,50,2706)
  264.     turtle.select(3)
  265.     turtle.dropDown(64)
  266.    
  267.  
  268. end
  269.  
  270. function goHome()
  271.     pathFind(-2489,52,2703)
  272.     pathFind(-2489,49,2703)
  273. end
  274.  
  275. function getToast()
  276.     pathFind(-2527,52,2704)
  277.     pathFind(-2527,50,2704)
  278.     turtle.suckDown(64)
  279.     pathFind(-2527,52,2704)
  280.     pathFind(-2489,51,2705)
  281.     turtle.select(3)
  282.     turtle.dropDown(64)
  283. end
  284.  
  285. function powerGuard()
  286.     local level=redstone.getAnalogInput("left")
  287.     return level
  288. end
  289.  
  290. function main()
  291.     if math.floor(pos.x)~=math.floor(homePos.x) or math.floor(pos.y)~=math.floor(homePos.y) or math.floor(pos.z)~=math.floor(homePos.z) then
  292.         outputMessage("I'm not at my home position!")
  293.         sleep(2)
  294.         outputMessage("My position is : "..pos.x..","..pos.y..","..pos.z)
  295.         sleep(8)
  296.         outputMessage("My home position is : "..homePos.x..","..homePos.y..","..homePos.z)
  297.         sleep(15)
  298.         return
  299.     end
  300.     if getFacing()~="west" then
  301.         setFacing("west")
  302.     end
  303.     local powerLevels=powerGuard()
  304.     print(powerLevels)
  305.     if powerLevels<warningLevel then
  306.         outputMessage("Warning, oxygen production power levels are nearly depleted!")
  307.         getWood()
  308.         getToast()
  309.         goHome()
  310.         sleep(240)
  311.     end
  312.     sleep(3)
  313. end
  314.  
  315. function splitString(sourceString,splittingChar,index)
  316.     results={}
  317.     currentWord=""
  318.     for i=1,string.len(sourceString) do
  319.         letter=string.sub(sourceString,i,i)
  320.         if letter==splittingChar then
  321.             results[#results+1]=currentWord
  322.             currentWord=""
  323.         else
  324.             currentWord=currentWord..letter
  325.         end
  326.     end
  327.     results[#results+1]=currentWord
  328.     return results[index]
  329. end
  330.  
  331. --[[os.startTimer(1)
  332. event=os.pullEvent()
  333. if event=="char" then
  334.     pos.x=-2489
  335.     pos.y=49
  336.     pos.z=2703
  337.     database.setData("position","x",pos.x)
  338.     database.setData("position","y",pos.y)
  339.     database.setData("position","z",pos.z)
  340.     print("Set to home position!")
  341. end
  342. print(event..":")]]
  343. while true do
  344.     main()
  345. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement