CreatureAI这个类并不大,构造函数要传入一个Creature的指针,然后就是20来个接口函数。
构造函数:
explicitCreatureAI(Creature*creature) :m_creature(creature) {}
接口函数不多,可以全部列出来:
// Called if IsVisible(Unit *who) is true at each *who move, reaction at visibility zone enter
virtualvoidMoveInLineOfSight(Unit*) {}
// Called for reaction at enter to combat if not in combat yet (enemy can be NULL)
virtualvoidEnterCombat(Unit*/*enemy*/) {}
// Called for reaction at stopping attack at no attackers or targets
virtualvoidEnterEvadeMode() {}
// Called at reaching home after evade
virtualvoidJustReachedHome() {}
// Called at any heal cast/item used (call non implemented)
virtualvoidHealBy(Unit*/*healer*/,uint32/*amount_healed*/) {}
// Called at any Damage to any victim (before damage apply)
virtualvoidDamageDeal(Unit*/*done_to*/,uint32&/*damage*/) {}
// Called at any Damage from any attacker (before damage apply)
// Note: it for recalculation damage or special reaction at damage
// for attack reaction use AttackedBy called for not DOT damage in Unit::DealDamage also
virtualvoidDamageTaken(Unit*/*done_by*/,uint32&/*damage*/) {}
// Called when the creature is killed
virtualvoidJustDied(Unit*) {}
// Called when the creature kills a unit
virtualvoidKilledUnit(Unit*) {}
// Called when the creature summon successfully other creature
virtualvoidJustSummoned(Creature* ) {}
virtualvoidSummonedCreatureDespawn(Creature*/*unit*/) {}
// Called when hit by a spell
virtualvoidSpellHit(Unit*,constSpellEntry*) {}
// Called when spell hits creature's target
virtualvoidSpellHitTarget(Unit*,constSpellEntry*) {}
// Called when the creature is target of hostile action: swing, hostile spell landed, fear/etc)
virtualvoidAttackedBy(Unit*attacker);
// Called when creature is spawned or respawned (for reseting variables)
virtualvoidJustRespawned() {}
// Called at waypoint reached or point movement finished
virtualvoidMovementInform(uint32/*MovementType*/,uint32/*Data*/) {}
// Called at text emote receive from player
virtualvoidReceiveEmote(Player*/*pPlayer*/,uint32/*text_emote*/) {}
// Called when creature attack expected (if creature can and no have current victim)
// Note: for reaction at hostile action must be called AttackedBy function.
virtualvoidAttackStart(Unit*) {}
// Is unit visible for MoveInLineOfSight
virtualboolIsVisible(Unit*)const{returnfalse; }
// Called when victim entered water and creature can not enter water
virtualboolcanReachByRangeAttack(Unit*) {returnfalse; }
// Called at World update tick
virtualvoidUpdateAI(constuint32/*diff*/) {}
可以看到,大部分接口是事件触发和消息通知。UpdateAI我放到最后,这个和别的接口函数不同,是负责AI状态的更新处理。
以CreatureAI为基类,派生出了PetAI、CreatureEventAI、GuardAI、ReactorAI、AggressAI、TotemAI和NullCreatureAI 7类基本AI。Creature的AIM_Initialize()函数,对AI进行初始化,根据Creature的类型选择不同的AI。值得提出的是,Mangos还支持ScriptAI,对于非宠物类的生物支持扩展脚本AI。Mangos的脚本系统我还没有怎么看,有空研究下。
可以看到Mangos的AI设计还是比较传统的,Creature类相当于身体和物理存在,干很多“傻大粗”的事情;AI相当于Creature的大脑,对事件进行响应和处理,指挥着身体物理存在进行反应。
下面可以看下宠物AI的一段代码: