Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ocs/apis/sensor")
- SHOOT_AT = {
- [ "Creeper" ] = true,
- [ "Skeleton" ] = true,
- [ "Spider"] = true,
- }
- local function isTarget( data )
- return SHOOT_AT[ data.Name ]
- end
- TRESHOLD = 45
- PITCH_OFF = { 0, 90, 180, -90 }
- local bow = peripheral.wrap( "left" )
- local prox = sensor.wrap("right")
- local dir = 0
- while true do
- local targets = prox.getTargets()
- local voteLeft = 0
- local voteRight = 0
- local hadTarget = false
- local shot = false
- for key, data in pairs(targets) do
- if ( isTarget(data) ) then
- local off = data.Position
- local aux = prox.getTargetDetails( key )
- if ( aux and aux.IsAlive ) then
- local x = off.X
- local z = off.Z
- local y = off.Y + 1
- local len = math.sqrt( x * x + z * z )
- local pitch = math.deg( -math.atan2( x/len, z/len ) ) - PITCH_OFF[dir +1]
- local yaw = math.deg( math.atan( y/len ) )
- if ( pitch < -180 ) then
- pitch = pitch + 360
- end
- if ( math.abs( yaw ) < TRESHOLD ) then
- hadTarget = true
- if ( pitch < -TRESHOLD ) then
- voteLeft = voteLeft +1
- elseif ( pitch > TRESHOLD ) then
- voteRight = voteRight +1
- else
- bow.shoot( 1, pitch, yaw )
- sleep( 0.5 )
- shot = true
- break
- end
- end
- end
- end
- end
- if ( hadTarget ) then
- if ( not shot ) then
- if ( voteRight > voteLeft ) then
- dir = (dir +1) % 4
- turtle.turnRight()
- else
- dir = (dir +3) % 4
- turtle.turnLeft()
- end
- end
- else
- sleep( 5 )
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement