Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local speaker=peripheral.wrap("right")
- rednet.open("left")
- local lastPlayer=nil
- local pos={["x"]=0,["y"]=0,["z"]=0}
- local homePos={["x"]=-2523,["y"]=49,["z"]=2697}
- local teleporters={}
- local talking=true
- local facingDirection="north"
- if fs.exists("database")==false then
- shell.run("pastebin get PdhBaBdZ database")
- end
- local success=os.loadAPI("database")
- if success==false or database==nil then
- error "The database system failed to load"
- end
- if fs.exists("databases/data")==false then
- database.createDataFile("data")
- end
- if fs.exists("databases/position")==false then
- database.createDataFile("position")
- end
- function outputMessage(message,range,language)
- if talking==true and type(message)=="string" then
- if range==nil then
- speaker.speak(message)
- elseif lanugage==nil then
- speaker.speak(message,range)
- else
- speaker.speak(message,range,language)
- end
- end
- end
- function changePos(key,change)
- if pos[key]==nil then
- pos[key]=0
- end
- pos[key]=pos[key]+change
- database.setData("position",key,pos[key])
- end
- function updatePos(key)
- database.setData("position",key,pos[key])
- end
- function getFilePos()
- if database.getData("position","x")==false then
- pos.x=0
- pos.y=0
- pos.z=0
- database.setData("position","x",0)
- database.setData("position","y",0)
- database.setData("position","z",0)
- else
- pos.x=database.getData("position","x")
- pos.y=database.getData("position","y")
- pos.z=database.getData("position","z")
- end
- end
- getFilePos()
- function move(direction,blocks)
- if direction=="up" then
- for i=1,blocks do
- moved=turtle.up()
- if moved==true then
- changePos("y",1)
- else
- return false
- end
- end
- elseif direction=="down" then
- for i=1,blocks do
- moved=turtle.down()
- if moved==true then
- changePos("y",-1)
- else
- return false
- end
- end
- elseif direction=="back" then
- local facing=string.lower(getFacing())
- for i=1,blocks do
- local moved=turtle.back()
- if moved==false then return false end
- if facing=="north" then
- changePos("z",1)
- elseif facing=="east" then
- changePos("x",-1)
- elseif facing=="south" then
- changePos("z",-1)
- elseif facing=="west" then
- changePos("x",1)
- end
- end
- else
- local facing=string.lower(getFacing())
- for i=1,blocks do
- local moved=turtle.forward()
- if moved==false then return false end
- if facing=="north" then
- changePos("z",-1)
- elseif facing=="east" then
- changePos("x",1)
- elseif facing=="south" then
- changePos("z",1)
- elseif facing=="west" then
- changePos("x",-1)
- end
- end
- end
- return true
- end
- local facingTable={
- ["north"]=1,
- ["east"]=2,
- ["south"]=3,
- ["west"]=4,
- }
- local facingTable2={
- [0]="west","north","east","south","west","north"
- }
- function turn(direction)
- if direction=="right" then
- turtle.turnRight()
- local currentFacingID=facingTable[facingDirection]
- facingDirection=facingTable2[currentFacingID+1]
- elseif direction=="left" then
- turtle.turnLeft()
- local currentFacingID=facingTable[facingDirection]
- facingDirection=facingTable2[currentFacingID-1]
- end
- end
- function setFacing(facing)
- local facing=string.lower(facing)
- local currentFacing=string.lower(getFacing())
- if facing==currentFacing then
- return
- else
- local currentFacingIndex=facingTable[currentFacing]
- local facingIndex=facingTable[facing]
- local turns=facingIndex-currentFacingIndex
- if turns<0 then
- turns=turns+4
- end
- if turns==3 then
- turn("left")
- else
- for i=1,turns do
- turn("right")
- end
- end
- end
- end
- function getFacing()
- return facingDirection
- end
- function getCompassFacing()
- turtle.select(2)
- turtle.equipRight()
- local compass=peripheral.wrap("right")
- local heading=compass.getFacing()
- turtle.select(2)
- turtle.equipRight()
- facingDirection=heading
- end
- getCompassFacing()
- function outputMessage(message,range,language)
- if talking==true and type(message)=="string" then
- if range==nil then
- speaker.speak(message)
- elseif lanugage==nil then
- speaker.speak(message,range)
- else
- speaker.speak(message,range,language)
- end
- end
- end
- function pathFind(x,y,z)
- local yDistance=(pos.y-y)*-1
- print("Y axis travel distance : "..yDistance)
- local yTravel=true
- if yDistance>0 then
- local yTravel=move("up",yDistance)
- elseif yDistance<0 then
- local yTravel=move("down",yDistance*-1)
- end
- local xDistance=(pos.x-x)*-1
- print("X axis travel distance : "..xDistance)
- local xTravel=true
- if xDistance>0 then
- setFacing("east")
- elseif xDistance<0 then
- setFacing("west")
- xDistance=xDistance*-1
- end
- xTravel=move("forward",xDistance)
- local zDistance=(pos.z-z)*-1
- print("Z axis travel distance : "..zDistance)
- local zTravel=true
- if zDistance>0 then
- setFacing("south")
- elseif zDistance<0 then
- setFacing("north")
- zDistance=zDistance*-1
- end
- zTravel=move("forward",zDistance)
- if xTravel==false or zTravel==false or yTravel==false then
- pathFind(x,y,z)
- end
- end
- function getWood()
- pathFind(-2520,51,2697)
- pathFind(-2520,51,2697)
- pathFind(-2520,51,2705)
- pathFind(-2508,51,2705)
- pathFind(-2508,53,2705)
- repeat
- rednet.broadcast("open up","turtleShaft")
- local id,message,protocol=rednet.receive(1)
- until message=="I have accepted your request!" and protocol=="returnMessage"
- sleep(0.1)
- pathFind(-2508,61,2705)
- sleep(2)
- pathFind(-2508,63,2705)
- pathFind(pos.x,pos.y+8,pos.z)
- pathFind(-2494,pos.y,2664)
- pathFind(-2494,64,2664)
- setFacing("west")
- turtle.suck(64)
- pathFind(pos.x,pos.y+8,pos.z)
- pathFind(-2508,pos.y,2705)
- pathFind(-2508,64,2705)
- repeat
- rednet.broadcast("open down","turtleShaft")
- local id,message,protocol=rednet.receive(1)
- until message=="I have accepted your request!" and protocol=="returnMessage"
- sleep(0.1)
- pathFind(-2508,61,2705)
- sleep(2)
- pathFind(-2508,54,2705)
- pathFind(-2508,51,2705)
- pathFind(-2517,51,2706)
- pathFind(-2517,50,2706)
- turtle.select(3)
- turtle.dropDown(64)
- end
- function getToast()
- pathFind(-2527,51,2704)
- pathFind(-2527,50,2704)
- turtle.suckDown(64)
- pathFind(-2520,51,2699)
- pathFind(-2520,51,2697)
- pathFind(homePos.x,homePos.y,homePos.z)
- setFacing("west")
- turtle.select(3)
- turtle.drop(64)
- end
- function main()
- 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
- outputMessage("I'm not at my home position!")
- sleep(2)
- outputMessage("My position is : "..pos.x..","..pos.y..","..pos.z)
- sleep(8)
- outputMessage("My home position is : "..homePos.x..","..homePos.y..","..homePos.z)
- sleep(15)
- return
- end
- if getFacing()~="north" then
- setFacing("north")
- end
- outputMessage("Woodtruck is starting a work shift!")
- getWood()
- getToast()
- setFacing("north")
- sleep(60)
- end
- function splitString(sourceString,splittingChar,index)
- results={}
- currentWord=""
- for i=1,string.len(sourceString) do
- letter=string.sub(sourceString,i,i)
- if letter==splittingChar then
- results[#results+1]=currentWord
- currentWord=""
- else
- currentWord=currentWord..letter
- end
- end
- results[#results+1]=currentWord
- return results[index]
- end
- while true do
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement