Advertisement
NanoBob

woodTruck

Mar 22nd, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.29 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"]=-2523,["y"]=49,["z"]=2697}
  6. local teleporters={}
  7. local talking=true
  8. local facingDirection="north"
  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.  
  122. local facingTable={
  123.     ["north"]=1,
  124.     ["east"]=2,
  125.     ["south"]=3,
  126.     ["west"]=4,
  127. }
  128.  
  129. local facingTable2={
  130.     [0]="west","north","east","south","west","north"
  131. }
  132.  
  133. function turn(direction)
  134.     if direction=="right" then
  135.         turtle.turnRight()
  136.         local currentFacingID=facingTable[facingDirection]
  137.         facingDirection=facingTable2[currentFacingID+1]
  138.     elseif direction=="left" then
  139.         turtle.turnLeft()
  140.         local currentFacingID=facingTable[facingDirection]
  141.         facingDirection=facingTable2[currentFacingID-1]
  142.     end
  143. end
  144.  
  145. function setFacing(facing)
  146.     local facing=string.lower(facing)
  147.     local currentFacing=string.lower(getFacing())
  148.     if facing==currentFacing then
  149.         return
  150.     else
  151.         local currentFacingIndex=facingTable[currentFacing]
  152.         local facingIndex=facingTable[facing]
  153.         local turns=facingIndex-currentFacingIndex
  154.         if turns<0 then
  155.             turns=turns+4
  156.         end
  157.         if turns==3 then
  158.             turn("left")
  159.         else
  160.             for i=1,turns do
  161.                 turn("right")
  162.             end
  163.         end
  164.     end
  165.    
  166. end
  167.  
  168. function getFacing()
  169.     return facingDirection
  170. end
  171.  
  172. function getCompassFacing()
  173.     turtle.select(2)
  174.     turtle.equipRight()
  175.     local compass=peripheral.wrap("right")
  176.     local heading=compass.getFacing()
  177.     turtle.select(2)
  178.     turtle.equipRight()
  179.     facingDirection=heading
  180. end
  181. getCompassFacing()
  182.  
  183. function outputMessage(message,range,language)
  184.     if talking==true and type(message)=="string" then
  185.         if range==nil then
  186.             speaker.speak(message)
  187.         elseif lanugage==nil then
  188.             speaker.speak(message,range)
  189.         else
  190.             speaker.speak(message,range,language)
  191.         end
  192.     end
  193. end
  194.  
  195. function pathFind(x,y,z)
  196.     local yDistance=(pos.y-y)*-1
  197.     print("Y axis travel distance : "..yDistance)
  198.     local yTravel=true
  199.     if yDistance>0 then
  200.         local yTravel=move("up",yDistance)
  201.     elseif yDistance<0 then
  202.         local yTravel=move("down",yDistance*-1)
  203.     end
  204.     local xDistance=(pos.x-x)*-1
  205.     print("X axis travel distance : "..xDistance)
  206.     local xTravel=true
  207.     if xDistance>0 then
  208.         setFacing("east")
  209.     elseif xDistance<0 then
  210.         setFacing("west")
  211.         xDistance=xDistance*-1
  212.     end
  213.     xTravel=move("forward",xDistance)
  214.     local zDistance=(pos.z-z)*-1
  215.     print("Z axis travel distance : "..zDistance)
  216.     local zTravel=true
  217.     if zDistance>0 then
  218.         setFacing("south")
  219.     elseif zDistance<0 then
  220.         setFacing("north")
  221.         zDistance=zDistance*-1
  222.     end
  223.     zTravel=move("forward",zDistance)
  224.     if xTravel==false or zTravel==false or yTravel==false then
  225.         pathFind(x,y,z)
  226.     end
  227. end
  228.  
  229. function getWood()
  230.     pathFind(-2520,51,2697)
  231.     pathFind(-2520,51,2697)
  232.     pathFind(-2520,51,2705)
  233.     pathFind(-2508,51,2705)
  234.    
  235.     pathFind(-2508,53,2705)
  236.     repeat
  237.         rednet.broadcast("open up","turtleShaft")
  238.         local id,message,protocol=rednet.receive(1)        
  239.     until message=="I have accepted your request!" and protocol=="returnMessage"
  240.     sleep(0.1)
  241.     pathFind(-2508,61,2705)
  242.     sleep(2)
  243.     pathFind(-2508,63,2705)
  244.    
  245.    
  246.     pathFind(pos.x,pos.y+8,pos.z)
  247.     pathFind(-2494,pos.y,2664)
  248.     pathFind(-2494,64,2664)
  249.     setFacing("west")
  250.     turtle.suck(64)
  251.     pathFind(pos.x,pos.y+8,pos.z)
  252.     pathFind(-2508,pos.y,2705)
  253.    
  254.     pathFind(-2508,64,2705)
  255.     repeat
  256.         rednet.broadcast("open down","turtleShaft")
  257.         local id,message,protocol=rednet.receive(1)        
  258.     until message=="I have accepted your request!" and protocol=="returnMessage"
  259.     sleep(0.1)
  260.     pathFind(-2508,61,2705)
  261.     sleep(2)
  262.     pathFind(-2508,54,2705)
  263.    
  264.     pathFind(-2508,51,2705)
  265.     pathFind(-2517,51,2706)
  266.     pathFind(-2517,50,2706)
  267.     turtle.select(3)
  268.     turtle.dropDown(64)
  269.    
  270.  
  271. end
  272.  
  273. function getToast()
  274.     pathFind(-2527,51,2704)
  275.     pathFind(-2527,50,2704)
  276.     turtle.suckDown(64)
  277.     pathFind(-2520,51,2699)
  278.     pathFind(-2520,51,2697)
  279.     pathFind(homePos.x,homePos.y,homePos.z)
  280.     setFacing("west")
  281.     turtle.select(3)
  282.     turtle.drop(64)
  283. end
  284.  
  285. function main()
  286.     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
  287.         outputMessage("I'm not at my home position!")
  288.         sleep(2)
  289.         outputMessage("My position is : "..pos.x..","..pos.y..","..pos.z)
  290.         sleep(8)
  291.         outputMessage("My home position is : "..homePos.x..","..homePos.y..","..homePos.z)
  292.         sleep(15)
  293.         return
  294.     end
  295.     if getFacing()~="north" then
  296.         setFacing("north")
  297.     end
  298.     outputMessage("Woodtruck is starting a work shift!")
  299.     getWood()
  300.     getToast()
  301.     setFacing("north")
  302.     sleep(60)
  303. end
  304.  
  305. function splitString(sourceString,splittingChar,index)
  306.     results={}
  307.     currentWord=""
  308.     for i=1,string.len(sourceString) do
  309.         letter=string.sub(sourceString,i,i)
  310.         if letter==splittingChar then
  311.             results[#results+1]=currentWord
  312.             currentWord=""
  313.         else
  314.             currentWord=currentWord..letter
  315.         end
  316.     end
  317.     results[#results+1]=currentWord
  318.     return results[index]
  319. end
  320.  
  321. while true do
  322.     main()
  323. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement