Piotr

Member
Hi,

I got the following script some time ago and I'd like to modify it a bit. At the moment the mob only flees when attacked, but I'd like to make it so it also flees once a player gets too close to it - say, 6 tiles. Don't actually know how to get it done, so any help will be much appreciated.

C#:
using System;
using System.Collections;
using Server.Targeting;
using Server.Network;

namespace Server.Mobiles
{
    public class FearfulAI : BaseAI
    {
        public FearfulAI(BaseCreature m) : base(m) { }

        private int RangeFocusMob { get { return m_Mobile.RangePerception * 4; } }
        public override bool DoActionWander()
        {
            Mobile controller = m_Mobile.ControlMaster;

            if (m_Mobile.Combatant != null)
                Action = ActionType.Flee;
            else if (controller != null && controller.Combatant != null && controller.GetDistanceToSqrt(m_Mobile) < 5)
                Action = ActionType.Flee;
            else return base.DoActionWander();

            return true;
        }

        public override bool DoActionCombat()
        {
                Action = ActionType.Wander;
                return true;
        }

        public override bool DoActionBackoff()
        {
            double hitPercent = (double)m_Mobile.Hits / m_Mobile.HitsMax;

            if ( !m_Mobile.Summoned && !m_Mobile.Controlled && hitPercent < 0.1 )  Action = ActionType.Flee;
            else
            {
                if (AcquireFocusMob(RangeFocusMob, FightMode.Closest, true, false, true))
                {
                    if (WalkMobileRange(m_Mobile.FocusMob, 1, false, m_Mobile.RangePerception, RangeFocusMob)) 
                        Action = ActionType.Wander;               
                }
                else Action = ActionType.Wander;
            }

            return true;
        }

        public override bool DoActionFlee()
        {
            AcquireFocusMob(RangeFocusMob, m_Mobile.FightMode, true, false, true);

            if ( m_Mobile.FocusMob == null ) m_Mobile.FocusMob = m_Mobile.Combatant;
            return base.DoActionFlee();
        }
    }
}
 
That script just defines the AI Type, you'll need to add the type to your mob scripts, for example, open a dragon.cs and look near the top where it says MageAI, change it too FearAI, you also might need to add the fearai to the baseai, not sure, just remember when I worked on ai's years ago I had to make edits in base ai for new ai's I added!
 
Yeah, I know that. I got that specific AI applied to certain mobs already.
My problem is not knowing how to modify the way this AI works, as explained above. I want the mob to run not only when attacked, but also when spotted a player.

I might have found a workaround by setting the particular mob's FightMode to "Closest" and using this:

C#:
            else if (AcquireFocusMob(m_Mobile.RangePerception, m_Mobile.FightMode, false, false, true))
            {
                m_Mobile.DebugSay("I have detected {0}, fleeing", m_Mobile.FocusMob.Name);

                m_Mobile.Combatant = m_Mobile.FocusMob;
                Action = ActionType.Flee;
            }
So now the scirpt forces the mob to attack the player and then to flee...
It works, but feels kind of crude.
 
I didn't have my coffee yet, after re reading your first post, I see what you mean now! For a solution, I'll take a look when I am coding later and will reply with something if your still stuck!
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back