Beispiel #1
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                if (interval <= -1)
                {
                    ServiceMethod.Invoke(ref context);
                }
                else
                {
                    uint lastServiceGameTime = context.Agent.Blackboard.Get <uint>("lastServiceGameTime", context.Tree.Id, this.Id, 0);
                    uint gameTime            = Game.GameTime;

                    if (gameTime - lastServiceGameTime > interval)
                    {
                        ServiceMethod.Invoke(ref context);
                        context.Agent.Blackboard.Set <uint>("lastServiceGameTime", gameTime, context.Tree.Id, this.Id);
                    }
                }

                return(Child.Behave(ref context));
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Failure);
            }
        }
        private void DoService(ref BehaviorTreeContext context)
        {
            if (!(context.Agent.Target is ISpatial))
            {
                throw new InvalidOperationException($"The behavior condition {nameof(GetNearestPedToAgent)} can't be used with {context.Agent.Target.GetType().Name}, it can only be used with {nameof(ISpatial)}s");
            }

            Vector3 position = ((ISpatial)context.Agent.Target).Position;

            Entity[] nearbyPeds = World.GetEntities(position, range, GetEntitiesFlags.ConsiderAllPeds);

            foreach (Entity e in nearbyPeds.OrderBy(e => Vector3.DistanceSquared(e.Position, position)))
            {
                Ped p = (Ped)e;
                if (pedPredicate == null || pedPredicate.Invoke(p))
                {
                    Ped contextPed = context.Agent.Target as Ped;
                    if (contextPed == null || contextPed != p)
                    {
                        pedSetter.Set(context, this, p);
                        return;
                    }
                }
            }
            pedSetter.Set(context, this, null);
        }
Beispiel #3
0
        private void GiveTasks(ref BehaviorTreeContext context)
        {
            Ped     ped = ((Ped)context.Agent.Target);
            Vehicle veh = vehicle.Get(context, this);

            if (Vector3.DistanceSquared(ped.Position, veh) > 6.5f * 6.5f)
            {
                Task goToTask = context.Agent.Blackboard.Get <Task>("goToTask", context.Tree.Id, this.Id, null);
                context.Agent.Blackboard.Set <Task>("enterTask", null, context.Tree.Id, this.Id);
                if ((goToTask == null || !goToTask.IsActive))
                {
                    goToTask = ped.Tasks.FollowNavigationMeshToPosition(veh.Position, ped.Position.GetHeadingTowards(veh), 2.0f, 5.0f);
                    context.Agent.Blackboard.Set <Task>("goToTask", goToTask, context.Tree.Id, this.Id);
                }
            }
            else if (!ped.IsInVehicle(veh, true))
            {
                Task enterTask = context.Agent.Blackboard.Get <Task>("enterTask", context.Tree.Id, this.Id, null);
                context.Agent.Blackboard.Set <Task>("goToTask", null, context.Tree.Id, this.Id);
                if (enterTask == null || !enterTask.IsActive)
                {
                    int seat = seatIndex.Get(context, this, -2);

                    enterTask = ped.Tasks.EnterVehicle(veh, -1, seat, speed, flags);
                    Game.LogTrivial(enterTask.Status.ToString());

                    context.Agent.Blackboard.Set <Task>("enterTask", enterTask, context.Tree.Id, this.Id);
                }
            }
        }
        protected override bool CheckCondition(ref BehaviorTreeContext context)
        {
            Ped     playerPed = Game.LocalPlayer.Character;
            Vector3 playerPos = playerPed.Position;

            return((playerPed.Inventory.EquippedWeapon?.Hash == WeaponHash.FireExtinguisher /*|| playerIsUsingHose*/) && World.GetAllFires().Any(f => Vector3.DistanceSquared(f.Position, playerPos) < 20.0f * 20.0f));
        }
Beispiel #5
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            Ped ped = ((Ped)context.Agent.Target);

            Task task = context.Agent.Blackboard.Get <Task>("shootAtPedTargetTask", context.Tree.Id, this.Id, null);

            if (task != null && task.IsActive)
            {
                DateTime startTime = context.Agent.Blackboard.Get <DateTime>("startTime", context.Tree.Id, this.Id);

                if ((DateTime.UtcNow - startTime).TotalMilliseconds > duration)
                {
                    task.Ped.Tasks.Clear();
                    return(BehaviorStatus.Success);
                }

                return(BehaviorStatus.Running);
            }
            else
            {
                if (task != null && !task.IsActive)
                {
                    return(BehaviorStatus.Success);
                }
                else
                {
                    return(BehaviorStatus.Failure);
                }
            }
        }
Beispiel #6
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                if (Children == null || Children.Length == 0)
                {
                    return(BehaviorStatus.Success);
                }

                for (int i = 0; i < Children.Length; i++)
                {
                    BehaviorStatus status = Children[i].Behave(ref context);

                    if (status != BehaviorStatus.Failure)
                    {
                        return(status);
                    }
                }

                return(BehaviorStatus.Failure);
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Failure);
            }
        }
        private void DoService(ref BehaviorTreeContext context)
        {
            ISpatial p = spatial.Get(context, this);

            if (p != null)
            {
                positionSetter.Set(context, this, p.Position);
            }
        }
 protected override void OnClose(ref BehaviorTreeContext context)
 {
     context.Agent.Blackboard.Set <List <Fire> >("firesToExtinguish", null, context.Tree.Id, this.Id);
     context.Agent.Blackboard.Set <Fire>("closestFire", null, context.Tree.Id, this.Id);
     context.Agent.Blackboard.Set <Fire>("furthestFire", null, context.Tree.Id, this.Id);
     context.Agent.Blackboard.Set <Fire>("targetFire", null, context.Tree.Id, this.Id);
     context.Agent.Blackboard.Set <Task>("fireWeaponAtTargetFireTask", null, context.Tree.Id, this.Id);
     context.Agent.Blackboard.Set <Task>("goToTask", null, context.Tree.Id, this.Id);
 }
        protected override bool CheckCondition(ref BehaviorTreeContext context)
        {
            Ped p = ped.Get(context, this);

            if (!p)
            {
                return(false);
            }

            return(p.IsInAnyVehicle(true));
        }
Beispiel #10
0
        private void DoService(ref BehaviorTreeContext context)
        {
            Ped p = ped.Get(context, this);

            if (!p)
            {
                return;
            }

            Vehicle v = p.CurrentVehicle;

            vehicleSetter.Set(context, this, v);
        }
Beispiel #11
0
        protected override bool CheckCondition(ref BehaviorTreeContext context)
        {
            if (condition != null)
            {
                return(condition.Invoke());
            }

            if (conditionWithContext != null)
            {
                return(conditionWithContext.Invoke(ref context));
            }

            return(false);
        }
Beispiel #12
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            Task task = context.Agent.Blackboard.Get <Task>("goToPosTask", context.Tree.Id, this.Id, null);

            if (task != null && task.IsActive)
            {
                return(BehaviorStatus.Running);
            }
            else
            {
                Ped ped = (Ped)context.Agent.Target;

                return(Vector3.Distance2D(ped.Position, getTarget()) > (distanceThreshold * 1.25f) ? BehaviorStatus.Failure : BehaviorStatus.Success);
            }
        }
Beispiel #13
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                Child.Behave(ref context);

                return(BehaviorStatus.Success);
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Success);
            }
        }
        private void DoService(ref BehaviorTreeContext context)
        {
            Entity[] nearbyPeds = World.GetEntities(position, range, GetEntitiesFlags.ConsiderAllPeds);

            foreach (Entity e in nearbyPeds.OrderBy(e => Vector3.DistanceSquared(e.Position, position)))
            {
                Ped p = (Ped)e;
                if (pedPredicate == null || pedPredicate.Invoke(p))
                {
                    pedSetter.Set(context, this, p);
                    return;
                }
            }

            pedSetter.Set(context, this, null);
        }
Beispiel #15
0
        protected override void OnOpen(ref BehaviorTreeContext context)
        {
            if (!(context.Agent.Target is Ped))
            {
                throw new InvalidOperationException($"The behavior action {nameof(GoToPosition)} can't be used with {context.Agent.Target.GetType().Name}, it can only be used with {nameof(Ped)}s");
            }

            Task task = context.Agent.Blackboard.Get <Task>("goToPosTask", context.Tree.Id, this.Id, null);

            if (task == null)
            {
                Ped     ped       = ((Ped)context.Agent.Target);
                Vector3 targetPos = getTarget?.Invoke() ?? target.Get(context, this);
                float   heading   = MathHelper.ConvertDirectionToHeading((targetPos - ped.Position).ToNormalized());

                task = ped.Tasks.FollowNavigationMeshToPosition(targetPos, heading, speed, distanceThreshold);
                context.Agent.Blackboard.Set <Task>("goToPosTask", task, context.Tree.Id, this.Id);
            }
        }
Beispiel #16
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                if (CheckCondition(ref context))
                {
                    return(BehaviorStatus.Success);
                }
                else
                {
                    return(BehaviorStatus.Failure);
                }
            }
            catch (Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Failure);
            }
        }
        protected override void OnOpen(ref BehaviorTreeContext context)
        {
            if (!(context.Agent.Target is Ped))
            {
                throw new InvalidOperationException($"The behavior action {nameof(ExtinguishFire)} can't be used with {context.Agent.Target.GetType().Name}, it can only be used with {nameof(Ped)}s");
            }

            Ped ped = ((Ped)context.Agent.Target);

            if (!ped || ped.IsDead)
            {
                return;
            }

            if (!ped.Inventory.Weapons.Contains(WeaponHash.FireExtinguisher))
            {
                ped.Inventory.GiveNewWeapon(WeaponHash.FireExtinguisher, -1, true);
            }

            GiveTasks(ref context);
        }
Beispiel #18
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                if (action != null)
                {
                    action.Invoke();
                }
                else
                {
                    actionWithContext?.Invoke(ref context);
                }

                return(BehaviorStatus.Success);
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Failure);
            }
        }
Beispiel #19
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                switch (Child.Behave(ref context))
                {
                case BehaviorStatus.Failure: return(BehaviorStatus.Success);

                case BehaviorStatus.Success: return(BehaviorStatus.Failure);

                case BehaviorStatus.Running: return(BehaviorStatus.Running);

                default: return(BehaviorStatus.Success);
                }
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Success);
            }
        }
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            Ped ped = ((Ped)context.Agent.Target);

            if (!ped || ped.IsDead)
            {
                return(BehaviorStatus.Failure);
            }

            GiveTasks(ref context);

            List <Fire> firesToExtinguish = context.Agent.Blackboard.Get <List <Fire> >("firesToExtinguish", context.Tree.Id, this.Id, null);

            if (firesToExtinguish.Count == 0) // if no fires are found, this task is finished
            {
                return(BehaviorStatus.Success);
            }
            else
            {
                return(BehaviorStatus.Running);
            }
        }
Beispiel #21
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            Ped ped = ((Ped)context.Agent.Target);

            if (!ped || ped.IsDead)
            {
                return(BehaviorStatus.Failure);
            }

            Vehicle veh = vehicle.Get(context, this);

            if (!veh || veh.IsDead)
            {
                ped.Tasks.Clear();
                return(BehaviorStatus.Failure);
            }

            GiveTasks(ref context);

            Task goToTask  = context.Agent.Blackboard.Get <Task>("goToTask", context.Tree.Id, this.Id, null);
            Task enterTask = context.Agent.Blackboard.Get <Task>("enterTask", context.Tree.Id, this.Id, null);

            if ((goToTask != null && goToTask.IsActive) || (enterTask != null && enterTask.IsActive))
            {
                return(BehaviorStatus.Running);
            }
            else
            {
                if (ped.IsInVehicle(veh, true))
                {
                    return(BehaviorStatus.Success);
                }
                else
                {
                    return(BehaviorStatus.Failure);
                }
            }
        }
Beispiel #22
0
        protected override void OnOpen(ref BehaviorTreeContext context)
        {
            if (!(context.Agent.Target is Ped))
            {
                throw new InvalidOperationException($"The behavior action {nameof(EnterVehicle)} can't be used with {context.Agent.Target.GetType().Name}, it can only be used with {nameof(Ped)}s");
            }

            Ped ped = ((Ped)context.Agent.Target);

            if (!ped || ped.IsDead)
            {
                return;
            }

            Vehicle veh = vehicle.Get(context, this);

            if (!veh || veh.IsDead)
            {
                return;
            }

            GiveTasks(ref context);
        }
        protected override bool CheckCondition(ref BehaviorTreeContext context)
        {
            if (!(context.Agent.Target is Ped))
            {
                throw new System.InvalidOperationException($"The behavior condition {nameof(IsInVehicle)} can't be used with {context.Agent.Target.GetType().Name}, it can only be used with {nameof(Ped)}s");
            }

            Ped ped = ((Ped)context.Agent.Target);

            if (!ped)
            {
                return(false);
            }

            Vehicle v = vehicle.Get(context, this);

            if (!v)
            {
                return(false);
            }

            return(ped.IsInVehicle(v, true));
        }
Beispiel #24
0
        protected override void OnOpen(ref BehaviorTreeContext context)
        {
            if (!(context.Agent.Target is Ped))
            {
                throw new InvalidOperationException($"The behavior action {nameof(GoToPosition)} can't be used with {context.Agent.Target.GetType().Name}, it can only be used with {nameof(Ped)}s");
            }

            context.Agent.Blackboard.Set <DateTime>("startTime", DateTime.UtcNow, context.Tree.Id, this.Id);

            Task task = context.Agent.Blackboard.Get <Task>("shootAtPedTargetTask", context.Tree.Id, this.Id, null);

            if (task == null)
            {
                Ped ped = ((Ped)context.Agent.Target);

                Entity ent = target.Get(context, this);

                if (ent)
                {
                    task = ped.Tasks.FireWeaponAt(ent, duration, firingPattern);
                    context.Agent.Blackboard.Set <Task>("shootAtPedTargetTask", task, context.Tree.Id, this.Id);
                }
            }
        }
Beispiel #25
0
        private void GiveTasks(ref BehaviorTreeContext context)
        {
            Ped    ped         = ((Ped)context.Agent.Target);
            Entity entToFollow = entity.Get(context, this);

            Task followTask = context.Agent.Blackboard.Get <Task>("followTask", context.Tree.Id, this.Id, null);

            float actualSpeed = context.Agent.Blackboard.Get <float>("followTaskActualSpeed", context.Tree.Id, this.Id, -1.0f);

            bool speedChanged = false;

            if (speedGetter != null)
            {
                float speedFromKey = Math.Max(speedGetter.Get(context, this), 1.0f);
                if (actualSpeed != speedFromKey)
                {
                    actualSpeed = speedFromKey;
                    context.Agent.Blackboard.Set <float>("followTaskActualSpeed", actualSpeed, context.Tree.Id, this.Id);
                    speedChanged = true;
                }
            }
            else if (actualSpeed <= 0.0f)
            {
                actualSpeed = speed;
                context.Agent.Blackboard.Set <float>("followTaskActualSpeed", actualSpeed, context.Tree.Id, this.Id);
                speedChanged = true;
            }

            if (speedChanged || followTask == null || (!followTask.IsActive && persistFollowing))
            {
                NativeFunction.Natives.TaskFollowToOffsetOfEntity(ped, entToFollow, offset.X, offset.Y, offset.Z, actualSpeed, -1, stoppingRange, persistFollowing);
                followTask = Task.GetTask(ped, "TASK_FOLLOW_TO_OFFSET_OF_ENTITY");

                context.Agent.Blackboard.Set <Task>("followTask", followTask, context.Tree.Id, this.Id);
            }
        }
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            try
            {
                if (Children == null || Children.Length == 0)
                {
                    return(BehaviorStatus.Success);
                }

                int runningChildIndex = context.Agent.Blackboard.Get("runningChildIndex", context.Tree.Id, this.Id, 0);

                for (int i = runningChildIndex; i < Children.Length; i++)
                {
                    BehaviorStatus status = Children[i].Behave(ref context);

                    switch (status)
                    {
                    case BehaviorStatus.Failure: return(BehaviorStatus.Failure);

                    case BehaviorStatus.Running:
                    {
                        context.Agent.Blackboard.Set("runningChildIndex", i, context.Tree.Id, this.Id);
                        return(BehaviorStatus.Running);
                    }
                    }
                }

                return(BehaviorStatus.Success);
            }
            catch (System.Exception ex)
            {
                Game.LogTrivial($"[{this.GetType().Name}] Exception thrown at OnBehave(): {ex}");

                return(BehaviorStatus.Failure);
            }
        }
Beispiel #27
0
        protected override BehaviorStatus OnBehave(ref BehaviorTreeContext context)
        {
            Ped ped = ((Ped)context.Agent.Target);

            if (!ped || ped.IsDead)
            {
                return(BehaviorStatus.Failure);
            }

            Entity entToFollow = entity.Get(context, this);

            if (!entToFollow)
            {
                return(BehaviorStatus.Failure);
            }

            GiveTasks(ref context);

            Task followTask = context.Agent.Blackboard.Get <Task>("followTask", context.Tree.Id, this.Id, null);

            if (followTask != null && followTask.IsActive)
            {
                return(BehaviorStatus.Running);
            }
            else
            {
                if (persistFollowing)
                {
                    return(BehaviorStatus.Failure);
                }
                else
                {
                    return(BehaviorStatus.Success);
                }
            }
        }
Beispiel #28
0
 protected abstract bool CheckCondition(ref BehaviorTreeContext context);
        private void DoService(ref BehaviorTreeContext context)
        {
            Entity e = entity.Get(context, this);

            speedSetter.Set(context, this, e.Speed);
        }
Beispiel #30
0
 protected override void OnClose(ref BehaviorTreeContext context)
 {
     context.Agent.Blackboard.Set <Task>("goToPosTask", null, context.Tree.Id, this.Id);
 }