定义一系列的AI行为类型和它们的优先级,以及一个映射表`ai_tbl`来关联每种AI行为类型与对应的脚本文件和优先级。以下是对代码的详细解释:
lua 游戏架构 之 游戏 AI (一)ai_base-CSDN博客https://blog.csdn.net/heyuchang666/article/details/140624481?spm=1001.2014.3001.5501lua 游戏架构 之 游戏 AI (二)ai_base_move_base-CSDN博客https://blog.csdn.net/heyuchang666/article/details/140624583?spm=1001.2014.3001.5501
lua 游戏架构 之 游戏 AI (三)ai_attack-CSDN博客https://blog.csdn.net/heyuchang666/article/details/140625113?spm=1001.2014.3001.5501
定义AI行为类型
代码中首先定义了一系列的AI行为类型,使用枚举的方式表示不同的AI行为。例如:
- - `eAType_BASE` 表示基础AI行为,数值为0。
- - `eAType_IDLE` 表示空闲状态,数值为5。
- - `eAType_ATTACK` 表示攻击行为,数值为50。
- - `eAType_DEAD` 表示死亡状态,数值为130。
这些类型用于区分不同的AI行为模式,每个类型都有一个唯一的数值标识。
定义优先级
接着定义了两个优先级常量:
- - `eAI_Priority_Low` 表示低优先级,数值为0。
- - `eAI_Priority_High` 表示高优先级,数值为1。
优先级用于控制AI行为的执行顺序,高优先级的行为会优先执行。
AI行为映射表 `ai_tbl`
- `ai_tbl` 是一个Lua表,用于映射AI行为类型到具体的脚本文件和它们的优先级。表中的每个元素是一个表,包含`script`和`priority`两个字段:
- - `script` 字段是一个字符串,表示实现该AI行为的脚本文件名。
- - `priority` 字段是一个数值,表示该AI行为的优先级。
例如:
[ eAType_BASE ] = { script = "ai_base", priority = eAI_Priority_High },
[ eAType_IDLE ] = { script = "ai_idle", priority = eAI_Priority_High },
[ eAType_ATTACK ] = { script = "ai_attack", priority = eAI_Priority_Low },
这意味着当AI需要执行基础行为时,会加载`ai_base`脚本,并且由于其高优先级,它会优先于其他低优先级的行为执行。类似地,`eAType_IDLE` 表示空闲状态,也会加载`ai_idle` 脚本,并具有高优先级。而`eAType_ATTACK` 表示攻击行为,会加载`ai_attack` 脚本,但具有低优先级。
总结
这段代码为游戏的AI系统提供了一个灵活的框架,根据不同的AI行为分配不同的处理脚本和优先级。通过这种方式,可以轻松地管理和扩展AI的行为逻辑,同时保持代码的组织性和可维护性。
----------------------------------------------------------------
eAType_BASE = 0;
eAType_IDLE = 5;
eAType_IDLE_NPC = 10;
eAType_IDLE_STAND = 11;
eAType_IDLE_JUST_STAND = 12;
eAType_GUARD = 13;
eAType_AUTO_NORMALATTACK = 14;
eAType_AUTOFIGHT_FIND_WAY = 15;
eAType_AUTOFIGHT_SKILL = 16;
eAType_AUTOFIGHT_STUNT_SKILL = 17;
eAType_AUTOFIGHT_MISSION_SKILL = 18;
eAType_AUTOFIGHT_CAST_FIND_TARGET = 19
eAType_MERCENARY_AUTO_FIND_TARGET = 20;
eAType_MOVE = 21;
eAType_SKILLENTITY_MOVE = 22;
eAType_NETWORK_SKILLENTITY_MOVE = 23;
eAType_AUTO_MOVE = 30;
eAType_NETWORK_MOVE = 31;
eAType_FOLLOW = 40;
eAType_FOLLOW_ARENA = 41;
eAType_ATTACK = 50;
eAType_NETWORK_ATTACK = 51;
eAType_Blink = 55;
eAType_MANUAL_ATTACK = 60;
eAType_FIND_TARGET = 70;
eAType_AUTOFIGHT_FIND_TARGET = 71;
eAType_ARENA_MERCENARY_FIND_TARGET = 72;
eAType_AUTO_SKILL = 80;
eAType_MANUAL_SKILL = 90;
eAType_FORCE_FOLLOW = 100;
eAType_FEAR = 105;
eAType_SPA = 110;
--eAType_RUSH = 119;
eAType_SHIFT = 131;
eAType_RETREAT = 121;
eAType_DEAD = 130;
eAType_DEAD_MERCENARY = 135;
eAType_DEAD_REVIVE = 140;
eAType_NETWORK_CACHE = 190;
eAType_AUTOFIGHT_MERCENARY_SKILL = 199;
eAType_AUTO_SUPERMODE = 200;
eAType_AUTOFIGHT_TRIGGER_SKILL = 201;
eAType_PRECOMMOND = 300;
eAType_AUTOFIGHT_RETURN = 310;
eAType_PET_CHECK_ALIVE = 500;
eAType_DAMAGE_CAR = 501;
eAType_CHECK_WATER = 502;
------------------------------------------------------
eAI_Priority_Low = 0;
eAI_Priority_High = 1;
------------------------------------------------------
ai_tbl =
{
[eAType_BASE] = { script = "ai_base", priority = eAI_Priority_High },
[eAType_GUARD] = { script = "ai_guard", priority = eAI_Priority_Low },
[eAType_IDLE_STAND] = { script = "ai_idle_stand", priority = eAI_Priority_High },
[eAType_IDLE_JUST_STAND] = { script = "ai_idle_just_stand", priority = eAI_Priority_High },
[eAType_IDLE] = { script = "ai_idle", priority = eAI_Priority_High },
[eAType_MOVE] = { script = "ai_move", priority = eAI_Priority_High },
[eAType_AUTO_MOVE] = { script = "ai_auto_move", priority = eAI_Priority_High },
[eAType_FOLLOW] = { script = "ai_follow", priority = eAI_Priority_Low },
[eAType_FOLLOW_ARENA] = { script = "ai_follow_arena", priority = eAI_Priority_Low },
[eAType_MERCENARY_AUTO_FIND_TARGET] = { script = "ai_mercenary_auto_find_target", priority = eAI_Priority_Low },
[eAType_Blink] = { script = "ai_blink", priority = eAI_Priority_Low },
[eAType_ATTACK] = { script = "ai_attack", priority = eAI_Priority_Low },
[eAType_NETWORK_ATTACK] = { script = "ai_network_attack", priority = eAI_Priority_Low },
[eAType_MANUAL_ATTACK] = { script = "ai_manual_attack", priority = eAI_Priority_Low },
[eAType_FIND_TARGET] = { script = "ai_find_target", priority = eAI_Priority_Low },
[eAType_ARENA_MERCENARY_FIND_TARGET]= { script = "ai_arena_mercenary_find_target", priority = eAI_Priority_Low },
[eAType_AUTO_SKILL] = { script = "ai_auto_skill", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_SKILL] = { script = "ai_autofight_special_skill", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_TRIGGER_SKILL] = { script = "ai_autofight_trigger_skill", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_STUNT_SKILL] = { script = "ai_autofight_stunt_skill", priority = eAI_Priority_Low },
[eAType_MANUAL_SKILL] = { script = "ai_manual_skill", priority = eAI_Priority_Low },
[eAType_FORCE_FOLLOW] = { script = "ai_force_follow", priority = eAI_Priority_Low },
[eAType_FEAR] = { script = "ai_fear", priority = eAI_Priority_Low },
[eAType_SPA] = { script = "ai_spa", priority = eAI_Priority_Low },
[eAType_SHIFT] = { script = "ai_shift", priority = eAI_Priority_Low },
[eAType_RETREAT] = { script = "ai_retreat", priority = eAI_Priority_Low },
[eAType_DEAD] = { script = "ai_dead", priority = eAI_Priority_High },
[eAType_DEAD_MERCENARY] = { script = "ai_dead_mercenary", priority = eAI_Priority_High },
[eAType_DEAD_REVIVE] = { script = "ai_dead_revive", priority = eAI_Priority_High },
[eAType_NETWORK_MOVE] = { script = "ai_network_auto_move", priority = eAI_Priority_High },
[eAType_NETWORK_CACHE] = { script = "ai_network_cache", priority = eAI_Priority_High },
[eAType_PET_CHECK_ALIVE] = { script = "ai_pet_check_alive", priority = eAI_Priority_Low },
[eAType_IDLE_NPC] = { script = "ai_idle_npc", priority = eAI_Priority_High },
[eAType_AUTOFIGHT_MERCENARY_SKILL] = { script = "ai_autofight_mercenary_skill", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_FIND_TARGET] = { script = "ai_autofight_find_target", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_FIND_WAY] = { script = "ai_autofight_find_way", priority = eAI_Priority_Low },
[eAType_PRECOMMOND] = { script = "ai_precommond", priority = eAI_Priority_Low },
[eAType_AUTO_NORMALATTACK] = { script = "ai_auto_normalattack", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_RETURN] = { script = "ai_autofight_return", priority = eAI_Priority_Low },
[eAType_SKILLENTITY_MOVE] = { script = "ai_skillentity_move", priority = eAI_Priority_Low },
[eAType_NETWORK_SKILLENTITY_MOVE] = { script = "ai_network_skillentity_move", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_MISSION_SKILL] = { script = "ai_autofight_mission_skill", priority = eAI_Priority_Low },
[eAType_AUTOFIGHT_CAST_FIND_TARGET] = { script = "ai_autofight_cast_find_target", priority = eAI_Priority_Low },
[eAType_DAMAGE_CAR] = { script = "ai_damage_car", priority = eAI_Priority_Low },
[eAType_AUTO_SUPERMODE] = { script = "ai_auto_supermode", priority = eAI_Priority_Low },
[eAType_CHECK_WATER] = { script = "ai_check_water", priority = eAI_Priority_Low },
};