Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local sendChannel=666
- local pingLimit=5
- if fs.exists("events")==false then shell.run("pastebin get L7KYC10V events") end
- if os.loadAPI("events")==false then error("Failed to load events API") end
- if fs.exists("data")==false then shell.run("pastebin get 5FpayqrQ data") end
- if os.loadAPI("data")==false then error("Failed to load data API") end
- if fs.exists("utils")==false then shell.run("pastebin get c4CBkgKV utils") end
- if os.loadAPI("utils")==false then error("Failed to load utils API") end
- local self={
- facing="north",
- pos={x=0,y=0,z=0},
- }
- local whitelist={
- ["NanoBob_"]=true,
- ["DDerek"]=true,
- ["bas260"]=true,
- }
- local sensor
- local mainTimer
- local facing={
- [0]="west","north","east","south","west","north",
- north=1,east=2,south=3,west=4,
- }
- function self.turn(direction,ammount)
- for i=1,ammount do
- if direction=="right" then
- turtle.turnRight()
- self.facing=facing[ facing[self.facing]+1 ]
- else
- turtle.turnLeft()
- self.facing=facing[ facing[self.facing]-1 ]
- end
- end
- data.set("facing",self.facing,"db/position")
- end
- function self.setFacing(targetFacing)
- local targetFacing=string.lower(targetFacing)
- local currentFacing=string.lower(self.facing)
- if targetFacing==currentFacing then
- return
- else
- local currentFacingIndex=facing[currentFacing]
- local facingIndex=facing[targetFacing]
- local turns=facingIndex-currentFacingIndex
- if turns<0 then
- turns=turns+4
- end
- if turns==3 then
- self.turn("left",1)
- else
- for i=1,turns do
- self.turn("right",1)
- end
- end
- end
- end
- function initPos()
- local facing=data.get("facing","db/position")
- if facing==nil then
- print("Please input facing:")
- io.write("facing: ")
- facing=read()
- data.set("facing",facing,"db/position")
- end
- self.facing=facing
- end
- function initSensor()
- sensor=peripheral.wrap("left")
- end
- function init()
- initPos()
- initSensor()
- end
- init()
- function sensorHandle()
- end
- local messageHandles={
- }
- function handleMessages(side,channel,replyChannel,message)
- if replyChannel==nil then return end
- local messageType=utils.split(message,"|",1)
- if messageHandles[messageType]==nil then return end
- local returnMessage=messageHandles[messageType](replyChannel,message)
- if returnMessage==nil then return end
- wireless.transmit(replyChannel,localChannel,returnMessage)
- end
- events.addHandler("modem_message",handleMessages)
- function searchForPlayers(id)
- if id==mainTimer then
- local players=sensor.getMobIds()
- for _,player in ipairs(players) do
- --if whitelist[player.name]==nil then
- local data=sensor.getMobData(player)
- if data==nil then break end
- local data=data.all()
- local pos=data.position
- pos.y=pos.y+1
- if pos.x>0.5 then
- self.setFacing("east")
- turtle.attack()
- if pos.x>1 then
- turtle.forward()
- end
- elseif pos.x<-0.5 then
- self.setFacing("west")
- turtle.attack()
- if pos.x<-1 then
- turtle.forward()
- end
- end
- if pos.y>0.5 then
- turtle.attackUp()
- if pos.y>-0.6 then
- turtle.up()
- end
- elseif pos.y<0 then
- turtle.attackDown()
- if pos.y<=-1.5 then
- turtle.down()
- end
- end
- if pos.z>0.5 then
- self.setFacing("south")
- turtle.attack()
- if pos.z>1 then
- turtle.forward()
- end
- elseif pos.z<-0.5 then
- self.setFacing("north")
- turtle.attack()
- if pos.z<-1 then
- turtle.forward()
- end
- end
- break
- --end
- end
- end
- end
- events.addHandler("timer",timerExpired)
- while true do
- searchForPlayers()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement