local state = {} local angle = {} local attack = {} local swim_speed = {} local swim_speed_max = {} --how far away fish can "detect" player from local alert_range = 500 --how much damage per attack local damage = 1 --height from which player is considered in water local in_water_height = 500 function fish_init(e) if first_fish == nil then first_fish = e end last_fish = e angle[e] = 0 state[e] = "swim" swim_speed[e] = 100 attack[e] = 0 swim_speed_max[e] = swim_speed[e] ai_soldier_pathindex[e] = -1 end function fish_main(e) --Prompt(g_PlayerPosY) CollisionOff(e) if g_Entity[e]['animating'] == 0 then SetAnimationFrames(0,20) PlayAnimation(e) g_Entity[e]['animating'] = 1 attack[e] = 0 end if state[e] == "swim" then if ai_soldier_pathindex[e] == -1 then ai_soldier_pathindex[e] = -2 PathIndex = -1 PathPointIndex = -1 pClosest = 99999 for pa = 1, AIGetTotalPaths(), 1 do for po = 1 , AIGetPathCountPoints(pa), 1 do pDX = g_Entity[e]['x'] - AIPathGetPointX(pa,po) pDZ = g_Entity[e]['z'] - AIPathGetPointZ(pa,po) pDist = math.sqrt(math.abs(pDX*pDX)+math.abs(pDZ*pDZ)) if pDist < pClosest and pDist < 200 then pClosest = pDist PathIndex = pa PathPointIndex = po end end -- po end -- pa if PathIndex > -1 then ai_soldier_pathindex[e] = PathIndex ai_path_point_index[e] = PathPointIndex ai_path_point_direction[e] = 1 ai_path_point_max[e] = AIGetPathCountPoints(ai_soldier_pathindex[e]) end end if ai_soldier_pathindex[e] > -1 then --SetCharacterToWalk(e) --CharacterControlUnarmed(e) --ModulateSpeed(e,1) ai_patrol_x[e] = AIPathGetPointX(ai_soldier_pathindex[e],ai_path_point_index[e]) ai_patrol_z[e] = AIPathGetPointZ(ai_soldier_pathindex[e],ai_path_point_index[e]) if angle[e] == 0 then angle[e] = RotateFishToPoint(e,ai_patrol_x[e],ai_patrol_z[e]) end --AIEntityGoToPosition(g_Entity[e]['obj'],ai_patrol_x[e],ai_patrol_z[e]) MoveForward(e,swim_speed[e]) tDistX = g_Entity[e]['x'] - ai_patrol_x[e] tDistZ = g_Entity[e]['z'] - ai_patrol_z[e] DistFromPath = math.sqrt(math.abs(tDistX*tDistX)+math.abs(tDistZ*tDistZ)) if DistFromPath < 95 then angle[e] = 0 swim_speed[e] = swim_speed_max[e] * math.random(0.75,1.25) if ai_path_point_direction[e] == 1 then ai_path_point_index[e] = ai_path_point_index[e] + 1 if ( ai_path_point_index[e] > ai_path_point_max[e] ) then ai_path_point_index[e] = ai_path_point_max[e] - 1 ai_path_point_direction[e] = 0 end else ai_path_point_index[e] = ai_path_point_index[e] - 1 if ( ai_path_point_index[e] < 1 ) then ai_path_point_index[e] = 2 ai_path_point_direction[e] = 1 end end end end if g_PlayerPosY < in_water_height and GetPlayerFlatDistance(e) < alert_range and GetTimer(e) > 6000 then StartTimer(e) state[e] = "alert" end elseif state[e] == "alert" then if GetTimer(e) < 4000 then RotateToPlayerSlowly(e,7) MoveForward(e,swim_speed[e] / 3) if g_Entity[e]['plrvisible'] == 1 then state[e] = "attack" end else angle[e] = 0 state[e] = "swim" end elseif state[e] == "attack" then if g_PlayerPosY < in_water_height then RotateToPlayer(e) if GetPlayerFlatDistance(e) > 80 then MoveForward(e,swim_speed_max[e]*1.25) else if attack[e] == 0 then HurtPlayer(e,damage) attack[e] = 1 end end else StartTimer(e) angle[e] = 0 state[e] = "swim" end end --state CollisionOn(e) end --main function fish_exit(e) end function RotateFishToPoint(e,x,z) if g_Entity[e] ~= nil and x > 0 and z > 0 then local destx = x - g_Entity[e]['x'] local destz = z - g_Entity[e]['z'] local angle = math.atan2(destx,destz) angle = angle * (180.0 / math.pi) --angle = angle + math.random(-1,1) if angle < 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end SetRotation(e,0,angle,0) return angle end end