ACR_CreatureBehavior.CreatureObject.OnPerception C# (CSharp) Method

OnPerception() public method

Called when the perception state of the creature changes.
public OnPerception ( uint PerceivedObjectId, bool Heard, bool Inaudible, bool Seen, bool Vanished ) : void
PerceivedObjectId uint Supplies the perceived object id. ///
Heard bool True if the object is now heard.
Inaudible bool True if the object is now not /// heard.
Seen bool True if the object is now seen.
Vanished bool True if the object is now not seen.
return void
        public void OnPerception(uint PerceivedObjectId, bool Heard, bool Inaudible, bool Seen, bool Vanished)
        {
            //
            // No need to manage perception tracking for PC or DM avatars as
            // these will never be AI controlled.
            //

            if (IsPC || IsDM)
                return;

            //
            // Update the perception list.  This is done even if we are not yet
            // AI controlled (as control could be temporarily suspended e.g. by
            // DM command).
            //

            PerceptionNode Node = (from PN in PerceivedObjects
                                   where PN.PerceivedObjectId == PerceivedObjectId
                                   select PN).FirstOrDefault();
            bool NewNode = false;
            bool RemoveNode = false;

            if (Node == null)
            {
                if (Heard || Seen)
                {
                    Node = new PerceptionNode(PerceivedObjectId);
                    NewNode = true;
                    PerceivedObjects.Add(Node);
                }
                else
                {
                    return;
                }
            }

            if (Heard)
            {
                Node.Heard = true;
                if (Party == null) OnSpawn();
                OnPerceptionSeenObject(PerceivedObjectId, NewNode);
            }

            if (Seen)
            {
                Node.Seen = true;
                if (Party == null) OnSpawn();
                OnPerceptionHeardObject(PerceivedObjectId, NewNode);
            }

            if (Vanished)
            {
                Node.Seen = false;

                if (!Node.Heard)
                {
                    RemoveNode = true;
                    PerceivedObjects.Remove(Node);
                }

                OnPerceptionLostSightObject(PerceivedObjectId, RemoveNode);
            }

            if (Inaudible)
            {
                Node.Heard = false;

                if (!Node.Seen)
                {
                    RemoveNode = true;
                    PerceivedObjects.Remove(Node);
                }

                OnPerceptionLostHearingObject(PerceivedObjectId, RemoveNode);
            }
        }

Usage Example

        public Int32 ScriptMain([In] object[] ScriptParameters, [In] Int32 DefaultReturnCode)
        {
            Int32 ReturnCode = 0;
            switch ((EVENT_TYPE)ScriptParameters[0])
            {
                case EVENT_TYPE.CREATURE_ON_SPAWN:
                    {
                        CreatureObject Creature = new CreatureObject(OBJECT_SELF, Server.ObjectManager);

                        Creature.OnSpawn();
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_SPELL_CAST_AT:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnSpellCastAt(GetLastSpellCaster(), GetLastSpell());
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_PHYSICALLY_ATTACKED:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnAttacked(GetLastAttacker(OBJECT_SELF));
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_DAMAGED:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnDamaged(GetLastDamager(OBJECT_SELF), GetTotalDamageDealt());
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_DEATH:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnDeath(GetLastKiller());
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_BLOCKED:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnBlocked(GetBlockingDoor());
                    }
                    break;

                case EVENT_TYPE.CREATURE_END_COMBAT_ROUND:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnEndCombatRound();
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_CONVERSATION:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnConversation();
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_INVENTORY_DISTURBED:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnInventoryDisturbed();
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_HEARTBEAT:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnHeartbeat();
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_RESTED:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnRested();
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_PERCEPTION:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                        {
                            Creature.OnPerception(GetLastPerceived(),
                                GetLastPerceptionHeard() != CLRScriptBase.FALSE ? true : false,
                                GetLastPerceptionInaudible() != CLRScriptBase.FALSE ? true : false,
                                GetLastPerceptionSeen() != CLRScriptBase.FALSE ? true : false,
                                GetLastPerceptionVanished() != CLRScriptBase.FALSE ? true : false);
                        }
                    }
                    break;

                case EVENT_TYPE.CREATURE_ON_USER_DEFINED:
                    {
                        CreatureObject Creature = Server.ObjectManager.GetCreatureObject(OBJECT_SELF);

                        if (Creature != null)
                            Creature.OnUserDefined(GetUserDefinedEventNumber());
                    }
                    break;

                case EVENT_TYPE.MODULE_ON_STARTED:
                    {
                        //
                        // Initialize the server subsystem.
                        //

                        Server.Initialize();

                        foreach (AreaObject Area in Server.ObjectManager.GetAreas())
                        {
                            foreach (uint ObjectInAreaId in Area.GetObjectIdsInArea())
                            {
                                if (GetObjectType(ObjectInAreaId) == CLRScriptBase.OBJECT_TYPE_TRIGGER)
                                {
                                    if (GetTransitionTarget(ObjectInAreaId) != OBJECT_INVALID)
                                    {
                                        AreaObject.AreaTransition Transition = new AreaObject.AreaTransition();
                                        Transition.ObjectId = ObjectInAreaId;
                                        Transition.TargetArea = Server.ObjectManager.GetAreaObject(GetArea(GetTransitionTarget(ObjectInAreaId)));
                                        Area.AreaTransitions.Add(Transition);
                                    }
                                }
                            }
                        }
                    }
                    break;

                case EVENT_TYPE.AREA_ON_INSTANCE_CREATE:
                    {
                        if (Server.ObjectManager == null)
                            break;

                        ModuleObject Module = Server.ObjectManager.Module;

                        if (Module == null)
                            break;

                        Module.AddInstancedArea(OBJECT_SELF);
                    }
                    break;
            }

            if (Server.ObjectManager != null)
                Server.ObjectManager.ProcessPendingDeletions();

            return ReturnCode;
        }