local destx = {} local desty = {} local destz = {} function ai_freeroam_init(e) CharacterControlManual(e) destx[e] = g_Entity[e]['x']+math.random(-1000,1000) desty[e] = g_Entity[e]['y'] destz[e] = g_Entity[e]['z']+math.random(-1000,1000) ai_bot_state[e] = "roam" end function ai_freeroam_main(e) if ai_bot_state[e] == "roam" then if GetDistanceToPoint(e,destx[e],destz[e]) < 100 then destx[e] = g_Entity[e]['x']+math.random(-1000,1000) desty[e] = g_Entity[e]['y'] destz[e] = g_Entity[e]['z']+math.random(-1000,1000) else AIEntityGoToPosition(g_Entity[e]['obj'],destx[e],desty[e],destz[e]) MoveForward(e,AIGetEntitySpeed(g_Entity[e]['obj'])) SetRotation(e,0,AIGetEntityAngleY(g_Entity[e]['obj']),0) AISetEntityPosition(g_Entity[e]['obj'],GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) if g_Entity[e]['animating'] > 0 then if g_Entity[e]['animating'] ~= 3 then StopAnimation(e) end else SetAnimationFrames(3870,3900) LoopAnimation(e) g_Entity[e]['animating'] = 3 end end if g_Entity[e]['plrvisible'] == 1 or AIGetEntityHeardSound(g_Entity[e]['obj']) == 1 or GetPlayerDistance(e) < AIGetEntityViewRange(g_Entity[e]['obj'])*0.3 then ai_bot_state[e] = "move to player" end elseif ai_bot_state[e] == "move to player" then if g_Entity[e]['plrvisible'] == 1 then destx[e] = g_PlayerPosX desty[e] = g_PlayerPosY destz[e] = g_PlayerPosZ end if (GetPlayerDistance(e) > AIGetEntityViewRange(g_Entity[e]['obj'])*0.75 or g_Entity[e]['plrvisible'] == 0) then if g_Entity[e]['animating'] ~= 2 then if GetDistanceToPoint(e,destx[e],destz[e]) > 100 then AIEntityGoToPosition(g_Entity[e]['obj'],destx[e],desty[e],destz[e]) MoveForward(e,AIGetEntitySpeed(g_Entity[e]['obj'])*2) SetRotation(e,0,AIGetEntityAngleY(g_Entity[e]['obj']),0) AISetEntityPosition(g_Entity[e]['obj'],GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) if g_Entity[e]['animating'] > 0 then if g_Entity[e]['animating'] ~= 1 then StopAnimation(e) end else SetAnimationFrames(795,811) LoopAnimation(e) g_Entity[e]['animating'] = 1 end else ai_bot_state[e] = "roam" end else AIEntityStop(g_Entity[e]['obj']) SetRotation(e,0,AngleToPoint(e,g_PlayerPosX,g_PlayerPosZ),0) AISetEntityPosition(g_Entity[e]['obj'],GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) FireWeapon(e) end else AIEntityStop(g_Entity[e]['obj']) SetRotation(e,0,AngleToPoint(e,g_PlayerPosX,g_PlayerPosZ),0) AISetEntityPosition(g_Entity[e]['obj'],GetEntityPositionX(e),GetEntityPositionY(e),GetEntityPositionZ(e)) if g_Entity[e]['animating'] > 0 then if g_Entity[e]['animating'] ~= 2 then StopAnimation(e) end else SetAnimationFrames(100,205) PlayAnimation(e) g_Entity[e]['animating'] = 2 end FireWeapon(e) end end end function ai_freeroam_exit(e) end function GetDistanceToPoint(e,x,z) if g_Entity[e] ~= nil and g_Entity[e] ~= 0 then local disx = GetEntityPositionX(e) - x local disz = GetEntityPositionZ(e) - z return math.sqrt(disx^2 + disz^2) end end function AngleToPoint(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) if angle < 0 then angle = 360 + angle elseif angle > 360 then angle = angle - 360 end --SetRotation(e,0,angle,0) return angle end end