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"]=-2489,["y"]=49,["z"]=2703}
- local teleporters={}
- local talking=true
- local warningLevel=5
- 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(-2489,52,2703)
- pathFind(-2508,52,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,62,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 goHome()
- pathFind(-2489,52,2703)
- pathFind(-2489,49,2703)
- end
- function getToast()
- pathFind(-2527,52,2704)
- pathFind(-2527,50,2704)
- turtle.suckDown(64)
- pathFind(-2527,52,2704)
- pathFind(-2489,51,2705)
- turtle.select(3)
- turtle.dropDown(64)
- end
- function powerGuard()
- local level=redstone.getAnalogInput("left")
- return level
- 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()~="west" then
- setFacing("west")
- end
- local powerLevels=powerGuard()
- print(powerLevels)
- if powerLevels<warningLevel then
- outputMessage("Warning, oxygen production power levels are nearly depleted!")
- getWood()
- getToast()
- goHome()
- sleep(240)
- end
- sleep(3)
- 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
- --[[os.startTimer(1)
- event=os.pullEvent()
- if event=="char" then
- pos.x=-2489
- pos.y=49
- pos.z=2703
- database.setData("position","x",pos.x)
- database.setData("position","y",pos.y)
- database.setData("position","z",pos.z)
- print("Set to home position!")
- end
- print(event..":")]]
- while true do
- main()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement