Exemple #1
0
        /// <summary>
        /// Call update methods on Sensors for each GameActor.
        /// </summary>
        private void UpdateGameThingSensors(BrainCategories category)
        {
            if (sensorCount > 0)
            {
                for (int i = 0; i < reflexes.Count; ++i)
                {
                    if (reflexes[i] == null)
                    {
                        continue;
                    }

                    Sensor sensor = (reflexes[i] as Reflex).Sensor;

                    if (sensor == null)
                    {
                        continue;
                    }

                    if (category == BrainCategories.NotSpecified || sensor.Categories.Get((int)category))
                    {
                        sensor.StartUpdate(GameActor);
                    }
                }

                if (haveThingUpdateSensors)
                {
                    Vector3 actorCenter = Vector3.Transform(GameActor.BoundingSphere.Center, GameActor.Movement.LocalMatrix);
                    for (int indexGameThing = 0; indexGameThing < brain.GameThings.Count; indexGameThing++)
                    {
                        // There a number of objects we don't want to consider to testing...
                        GameThing gameThing = brain.GameThings[indexGameThing] as GameThing;

                        if (gameThing.Ignored)
                        {
                            continue;
                        }

                        /*
                         * // Handled lower down now...
                         * // Don't test self.
                         * if (this.GameActor == gameThing)
                         *  continue;
                         */

                        // Don't test dead things.  You know you can't see dead things.
                        if (!gameThing.IsAlive())
                        {
                            continue;
                        }

                        /*
                         * // Handled lower down now...
                         * // Also, don't sense things that are dead in another way (knocked-out).
                         * if (gameThing.CurrentState == GameThing.State.Dead)
                         *  continue;
                         */

                        // Don't test things we're holding.
                        if (gameThing == this.GameActor.ThingBeingHeldByThisActor)
                        {
                            continue;
                        }

                        // Don't test missiles we've fired.
                        CruiseMissile missile = gameThing as CruiseMissile;
                        if (missile != null && missile.Launcher == this.GameActor)
                        {
                            continue;
                        }

                        Vector3 thingCenter = Vector3.Transform(gameThing.BoundingSphere.Center, gameThing.Movement.LocalMatrix);

                        Vector3 direction = thingCenter - actorCenter;

                        float range = direction.Length();
                        if (range > 0.0f)
                        {
                            direction *= 1.0f / range; // Normalize.
                        }

                        for (int i = 0; i < reflexes.Count; ++i)
                        {
                            if (reflexes[i] == null)
                            {
                                continue;
                            }

                            Sensor sensor = (reflexes[i] as Reflex).Sensor;

                            if (sensor == null)
                            {
                                continue;
                            }

                            // Handle strangeness of me filter.  Basically this is adds an implicit "not me" filter
                            // except in the case where the user explicitely added the "me " filter.
                            Reflex reflex = (Reflex)reflexes[i];
                            if (reflex.hasMeFilter)
                            {
                                if (GameActor != gameThing)
                                {
                                    continue;
                                }
                                direction = GameActor.Movement.Facing;
                            }
                            else
                            {
                                if (GameActor == gameThing)
                                {
                                    continue;
                                }
                            }

                            // Ignore dead things unless explicitly looking for them.
                            if (reflex.hasDeadFilter)
                            {
                                // We're looking for dead things so continue if not dead.
                                if (gameThing.CurrentState != GameThing.State.Dead)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                // We're not looking for dead things so ignore them.
                                if (gameThing.CurrentState == GameThing.State.Dead)
                                {
                                    continue;
                                }
                            }

                            // Ignore squashed things unless explicitly looking for them.
                            if (reflex.hasSquashedFilter)
                            {
                                // We're looking for dead things so continue if not dead.
                                if (gameThing.CurrentState != GameThing.State.Squashed)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                // We're not looking for squashed things so ignore them.
                                if (gameThing.CurrentState == GameThing.State.Squashed)
                                {
                                    continue;
                                }
                            }

                            // Ignore missiles unless explicitly looking for them.
                            if (reflex.hasMissileFilter)
                            {
                                // We're looking for missiles, continue otherwise
                                if (!(gameThing is CruiseMissile))
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                // We're not looking for missiles so ignore them.
                                if (gameThing is CruiseMissile)
                                {
                                    continue;
                                }
                            }

                            if (sensor.WantThingUpdate && (category == BrainCategories.NotSpecified || sensor.Categories.Get((int)category)))
                            {
                                if (gameThing.CanBeSensed())
                                {
                                    sensor.ThingUpdate(GameActor, gameThing, direction, range);
                                }
                            }
                        }
                    } // foreach gamething
                }     // if haveThingUpdateSensors

                for (int i = 0; i < reflexes.Count; ++i)
                {
                    if (reflexes[i] == null)
                    {
                        continue;
                    }

                    Sensor sensor = reflexes[i].Sensor;

                    if (sensor == null)
                    {
                        continue;
                    }

                    if (category == BrainCategories.NotSpecified || sensor.Categories.Get((int)category))
                    {
                        sensor.FinishUpdate(GameActor);
                    }
                }
            } // if sensorCount > 0
        }
Exemple #2
0
 public void UpdateSensors(BrainCategories category)
 {
     ActiveTask.UpdateSensors(category);
 }
Exemple #3
0
        public void UpdateSensors(BrainCategories category)
        {
            UpdateUserControlled();
            UpdateIsTurning();
            UpdateMouseButtonPresence();

            if (IsLeftMouseButtonPresent)
            {
                MouseEdit.DisableLeftDrag();
            }
            if (IsRightMouseButtonPresent)
            {
                MouseEdit.DisableRightOrbit();
            }

            for (int i = 0; i < reflexes.Count; i++)
            {
                Reflex reflex = reflexes[i] as Reflex;
                reflex.targetSet.Clear();
            }

            if (reflexes.Count > 0)
            {
                UpdateGameThingSensors(category);

                for (int indentationLevel = 0; indentationLevel <= maxReflexIndentation; ++indentationLevel)
                {
                    // After evaluating root-level reflexes, open up to all sensor categories.
                    if (indentationLevel > 0)
                    {
                        category = BrainCategories.NotSpecified;
                    }

                    for (int i = 0; i < reflexes.Count; i++)
                    {
                        Reflex reflex = reflexes[i];

                        // Only evaluate reflexes at the current level of indentation.
                        if (reflex.Indentation != indentationLevel)
                        {
                            continue;
                        }

                        // Only evaluate sub-reflexes of parents that evaluated true except
                        // in the case where the reflex actuator is movement based.
                        if (reflex.Parent != null && !reflex.Parent.targetSet.Action)
                        {
                            // Nested reflexes with once modifiers need to clear their "once" state.
                            reflex.ResetOnceModifiers();

                            // Nested reflexes with active mouse targets should still be acted on
                            // so create valid target and action sets for them.  Note that this
                            // only applies to reflexes with a movement actuator.  This is so you
                            // can have WHEN MouseLeft DO Move and the result will be that the
                            // bot continues to move toward the target (position or bot) even
                            // after the parent reflex goes false.
                            bool mouseTarget = reflex.MousePosition != null || reflex.MouseActor != null;
                            if (mouseTarget && reflex.IsMovement)
                            {
                                // Create a sensor target set
                                if (reflex.MouseActor != null)
                                {
                                    // We need to add the mouse actor to the targetSet.
                                    SensorTarget target = SensorTargetSpares.Alloc();
                                    target.Init(reflex.Task.GameActor, reflex.MouseActor);
                                    reflex.targetSet.Add(target);
                                }
                                else if (reflex.MousePosition != null)
                                {
                                    // We need to add the mouse position to the targetSet.
                                    SensorTarget target = SensorTargetSpares.Alloc();
                                    target.GameThing  = GameActor;
                                    target.Position   = reflex.MousePosition.Value;
                                    target.Direction  = target.Position - reflex.Task.GameActor.Movement.Position;
                                    target.Range      = target.Direction.Length();
                                    target.Direction /= target.Range;
                                    reflex.targetSet.Add(target);
                                }
                                reflex.targetSet.ActionMouseTarget = true;
                                reflex.CreateActionSet(GameActor, 0);
                            }

                            continue;
                        }

                        // Don't evaluate reflexes that don't match the given categories, if any were specified.
                        if ((category != BrainCategories.NotSpecified) && (reflex.Sensor == null || !reflex.Sensor.Categories.Get((int)category)))
                        {
                            continue;
                        }

                        reflex.Update(GameActor, i);
                    }
                }
            }
        }