Beispiel #1
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);
                }
            }
        }
        private void DoService(ref BehaviorTreeContext context)
        {
            ISpatial p = spatial.Get(context, this);

            if (p != null)
            {
                positionSetter.Set(context, this, p.Position);
            }
        }
        protected override bool CheckCondition(ref BehaviorTreeContext context)
        {
            Ped p = ped.Get(context, this);

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

            return(p.IsInAnyVehicle(true));
        }
Beispiel #4
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 #5
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);
        }
Beispiel #6
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);
            }
        }
        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 #8
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 #9
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);
            }
        }
        private void DoService(ref BehaviorTreeContext context)
        {
            Entity e = entity.Get(context, this);

            speedSetter.Set(context, this, e.Speed);
        }
        private void GiveTasks(ref BehaviorTreeContext context)
        {
            Ped         ped = ((Ped)context.Agent.Target);
            Vector3     pos = position.Get(context, this);
            List <Fire> firesToExtinguish          = context.Agent.Blackboard.Get <List <Fire> >("firesToExtinguish", context.Tree.Id, this.Id, null);
            Fire        closestFire                = context.Agent.Blackboard.Get <Fire>("closestFire", context.Tree.Id, this.Id, null);
            Fire        furthestFire               = context.Agent.Blackboard.Get <Fire>("furthestFire", context.Tree.Id, this.Id, null);
            Fire        targetFire                 = context.Agent.Blackboard.Get <Fire>("targetFire", context.Tree.Id, this.Id, null);
            Task        fireWeaponAtTargetFireTask = context.Agent.Blackboard.Get <Task>("fireWeaponAtTargetFireTask", context.Tree.Id, this.Id, null);
            Task        goToTask = context.Agent.Blackboard.Get <Task>("goToTask", context.Tree.Id, this.Id, null);

            if (firesToExtinguish == null)
            {
                firesToExtinguish = new List <Fire>();
                context.Agent.Blackboard.Set <List <Fire> >("firesToExtinguish", firesToExtinguish, context.Tree.Id, this.Id);
            }

            if (Vector3.DistanceSquared(ped.Position, pos) < rangeSq)
            {
                if (firesToExtinguish.Count == 0)
                {
                    foreach (Fire f in World.GetAllFires())
                    {
                        if (Vector3.DistanceSquared(f.Position, pos) < rangeSq)
                        {
                            firesToExtinguish.Add(f);
                        }
                    }

                    if (firesToExtinguish.Count == 0)
                    {
                        return;
                    }
                }
                else
                {
                    if (!closestFire || !furthestFire)
                    {
                        if (fireWeaponAtTargetFireTask != null && fireWeaponAtTargetFireTask.IsActive)
                        {
                            ped.Tasks.Clear();
                            fireWeaponAtTargetFireTask = null;
                            context.Agent.Blackboard.Set <List <Fire> >("fireWeaponAtTargetFireTask", null, context.Tree.Id, this.Id);
                        }

                        firesToExtinguish.RemoveAll(f => !f.Exists());

                        if (firesToExtinguish.Count >= 1)
                        {
                            IOrderedEnumerable <Fire> orderedFires = firesToExtinguish.OrderBy(f => Vector3.DistanceSquared(f.Position, ped.Position));
                            closestFire  = orderedFires.FirstOrDefault();
                            furthestFire = orderedFires.LastOrDefault();

                            context.Agent.Blackboard.Set <Fire>("closestFire", closestFire, context.Tree.Id, this.Id);
                            context.Agent.Blackboard.Set <Fire>("furthestFire", furthestFire, context.Tree.Id, this.Id);

                            targetFire = closestFire;
                            context.Agent.Blackboard.Set <Fire>("targetFire", targetFire, context.Tree.Id, this.Id);
                        }
                    }
                    else
                    {
                        if (Vector3.DistanceSquared(ped.Position, closestFire) > 4.0f * 4.0f)
                        {
                            if (goToTask == null || !goToTask.IsActive)
                            {
                                goToTask = ped.Tasks.FollowNavigationMeshToPosition(targetFire.Position, ped.Position.GetHeadingTowards(targetFire), 2.0f, 2.75f);
                                context.Agent.Blackboard.Set <Task>("goToTask", goToTask, context.Tree.Id, this.Id);
                            }
                        }
                        else
                        {
                            if (fireWeaponAtTargetFireTask == null || !fireWeaponAtTargetFireTask.IsActive)
                            {
                                if ((goToTask != null && goToTask.IsActive))
                                {
                                    ped.Tasks.Clear();
                                    ped.Tasks.ClearSecondary();
                                }

                                fireWeaponAtTargetFireTask = ped.Tasks.FireWeaponAt(targetFire.Position, 7500, FiringPattern.FullAutomatic);
                                context.Agent.Blackboard.Set <Task>("fireWeaponAtTargetFireTask", fireWeaponAtTargetFireTask, context.Tree.Id, this.Id);
                            }
                        }
                    }
                }
            }
            else
            {
                if ((goToTask == null || !goToTask.IsActive))
                {
                    goToTask = ped.Tasks.FollowNavigationMeshToPosition(pos.Around2D(range), ped.Position.GetHeadingTowards(pos), 2.0f, 5.0f);
                    context.Agent.Blackboard.Set <Task>("goToTask", goToTask, context.Tree.Id, this.Id);
                }
            }
        }
        protected override bool CheckCondition(ref BehaviorTreeContext context)
        {
            Entity ent = entity.Get(context, this);

            return(ent.Exists());
        }