ACR_CreatureBehavior.CreatureObject.OnSpellCastAt C# (CSharp) Method

OnSpellCastAt() public method

Called when a spell is cast on the creature.
public OnSpellCastAt ( uint CasterObjectId, int SpellId ) : void
CasterObjectId uint Supplies the spell caster object id. ///
SpellId int Supplies the spell id.
return void
        public void OnSpellCastAt(uint CasterObjectId, int SpellId)
        {
            if (!IsAIControlled)
                return;

            int nSpell = Script.GetLastSpell();
            int nHostile = Script.StringToInt(Script.Get2DAString("spells","HostileSetting", nSpell));

//===================================================================================================================================
//=======================  Handling for harmful spells ==============================================================================
//===================================================================================================================================
            if (nHostile == 1)
            {
                // As this is a harmful spell, the target is probably going to be unhappy about it.
                bool bAngry = true;
                uint CasterId =  Script.GetLastSpellCaster();
                int nReputation = Script.GetReputation(this.ObjectId, CasterId);

                // If the creature is -already- hostile, we don't need to make it any -more- angry.
                if (nReputation <= 10)
                    bAngry = false;

                // If this is the caster hitting him or herself, no doubt that's angering, but we don't need to change behavior because of it.
                if (CasterId == this.ObjectId)
                    bAngry = false;

                // Maybe mind-controlling magic is in play?
                if (bAngry)
                {
                    if (_IsMindMagiced(this.ObjectId)) bAngry = false;
                    if (_IsMindMagiced(CasterId)) bAngry = false;
                }

                // If mind magics didn't motivate the spell, we check to see if it's plausibly friendly fire.
                if (bAngry)
                {
                    int nTargetArea = Script.StringToInt(Script.Get2DAString("spells", "TargetingUI", nSpell));
                    if (_IsFriendlyFire(nTargetArea)) bAngry = false;
                }

                // Is the target a friend to the caster?
                if (bAngry)
                {
                    CreatureObject Caster = Server.ObjectManager.GetCreatureObject(CasterId, true);
                    AIParty Party = this.Party;

                    // This is the fault of a bug in the AI; best not to compound it.
                    if (Party.PartyMembers.Contains(Caster))
                    {
                        bAngry = false;
                    }

                    // These two creatures are friends.
                    else if (nReputation > 89)
                    {
                        Script.SetLocalInt(this.ObjectId, "FRIENDLY_FIRED", Script.GetLocalInt(this.ObjectId, "FRIENDLY_FIRED") + 1);
                    }

                    // Neutral creatures take direct attacks personally. Your friends try to trust you, but will snap if abused too much.
                    else if (nReputation > 10 || Script.GetLocalInt(this.ObjectId, "FRIENDLY_FIRED") > Script.d4(3))
                    {
                        // And all of them are going to get angry.
                        foreach (CreatureObject CurrentPartyMember in this.Party.PartyMembers)
                        {
                            _SetMutualEnemies(CurrentPartyMember.ObjectId, CasterId);
                            if (!CurrentPartyMember.HasCombatRoundProcess)
                                 CurrentPartyMember.SelectCombatRoundAction(false);
                        }
                        this.Party.AddPartyEnemy(Caster);
                        this.Party.EnemySpellcasters.Add(Caster);
                        HasCombatRoundProcess = true;
                        if (!UsingEndCombatRound)
                        {
                            SelectCombatRoundAction(false);
                        }
                    }
                }
            }

//===================================================================================================================================
//=======================  Handling for non-harmful spells ==========================================================================
//===================================================================================================================================
            else
            {
            }
        }

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;
        }