Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Player ignore table
- tPlayers = {"Lil122j", "CometWolf"}
- -- Mob ignore table
- tMobs = {}
- -- range settings
- -- program start
- os.loadAPI"cTurtle"
- os.pullEvent = os.pullEventRaw
- os.loadAPI"ocs/apis/sensor"
- cTurtle.locate()
- -- locates sensors
- local tSides = rs.getSides()
- local tSensors = {}
- for i=1,#tSides do
- if peripheral.getType(tSides[i]) == "sensor" then
- tSensors[#tSensors+1] = sensor.wrap(tSides[i])
- end
- end
- -- no sensors found
- if not tSensors[1] then
- error"ERROR: No sensors found!"
- elseif tSensors[2] then
- error"ERROR: Multiple sensors found!"
- end
- -- sensor loop
- function detect()
- while true do
- local tEntities = {}
- local tDetails = {}
- -- entity detection
- for name,table in pairs(tSensors[1].getTargets()) do
- tDetails[#tEntities+1] = tSensors[1].getTargetDetails(name)
- tEntities[#tEntities+1] = name
- end
- -- ignore check
- for i=1,#tEntities do
- if not tDetails[i] then
- break
- end
- local ignoreMob = false
- local ignorePlayer = false
- local ignore = false
- local entity = ""
- -- players
- if tDetails[i]["Name"] == "Player" then
- entity = "player"
- for t=1,#tPlayers do
- if tDetails[i]["Username"] == tPlayers[t] then
- ignorePlayer = true
- break
- end
- end
- -- mobs
- else
- for t=1,#tMobs do
- entity = "mob"
- if tDetails[i]["Name"] == tMobs[t] then
- ignoreMob = true
- break
- end
- end
- end
- if not ignorePlayer and entity == "player" then
- -- un-ignored player detected
- -- action
- distX = tDetails[i].Position.X
- distZ = tDetails[i].Position.Z
- distY = tDetails[i].Position.Y
- term.clear()
- term.setCursorPos(1,1)
- print("X: "..distX)
- print("Z: "..distZ)
- print("Y: "..distY)
- return
- elseif not ignoreMob and entity == "mob" then
- -- un-ignored mob detected
- else
- -- any entity detected
- end
- end
- sleep(0.1)
- end
- end
- function action()
- -- dist checks
- local tDist = {}
- -- X dist
- if distX < -1.2 then
- cTurtle.move"X-"
- return
- elseif distX > 1.2 then
- cTurtle.move"X+"
- return
- else
- tDist["X"] = true
- end
- -- Z dist
- if distZ < -1.2 then
- cTurtle.move"Z-"
- return
- elseif distZ > 1.2 then
- cTurtle.move"Z+"
- return
- else
- tDist["Z"] = true
- end
- -- Y dist
- if distY < -2 then
- cTurtle.move"down"
- return
- elseif distY > 0 then
- cTurtle.move"up"
- return
- else
- tDist["Y"] = true
- end
- -- dist ok
- if tDist["X"] and tDist["Z"] and tDist["Y"] then
- targetDir = math.max(math.abs(distX),math.abs(distZ))
- if targetDir == math.abs(distX) then
- if distX > 0 then
- cTurtle.turn"X+"
- if not turtle.attack() then
- cTurtle.move"forward"
- end
- else
- cTurtle.turn"X-"
- if not turtle.attack() then
- cTurtle.move"forward"
- end
- end
- end
- if targetDir == math.abs(distZ) then
- if distZ > 0 then
- cTurtle.turn"Z+"
- if not turtle.attack() then
- cTurtle.move"forward"
- end
- else
- cTurtle.turn"Z-"
- if not turtle.attack() then
- cTurtle.move"forward"
- end
- end
- end
- end
- end
- while true do
- detect()
- action()
- end
Advertisement
Add Comment
Please, Sign In to add comment