本文最后更新于13 天前,其中的信息可能已经过时,如有错误请发送邮件到big_fw@foxmail.com
- 模块边界:敌人 AI 由组件层与行为层构成。组件层负责“何时执行、执行什么、与战斗系统的对接”,行为层负责“如何打分与倾向配置”。组件层入口在 CharacterAIComponent;行为层资源在 AIBehavior。
- 决策范式:基于效用(Utility)评分与随机扰动的轻量策略系统,按“技能→防御→普攻”的优先链路逐步回退,确保总能产出行动或显式返回无行动。
CharacterAIComponent
AIBehavior
1) 功能清单
- CharacterAIComponent 暴露接口
- 属性(可在 Inspector 配置) character_ai_component.gd:L4-L18
- difficulty_level:int,aggression_level:float,skill_usage_chance:float,healing_threshold:float:难度/倾向/技能使用概率/治疗阈值;当前脚本内未直接使用(见“已知问题”)。
- ai_enabled:bool:是否启用 AI。
- behavior_resource:AIBehavior:行为资源;若为空在 initialize 时按 behavior_type 创建。
- behavior_type:String(balanced/aggressive/defensive/support/random):用于初始化 AIBehavior 的类型。
- 方法
- initialize(p_battle_manager:BattleManager) -> void character_ai_component.gd:L37-L44
- 时机:角色加入战场/开战初始化时由上层调用。
- 作用:注入 BattleManager;若无行为资源则创建并按 behavior_type 配置权重。
- 入参:战斗管理器;无返回。
- execute_action() -> AIActionResult(异步 await) character_ai_component.gd:L47-L84
- 时机:该角色回合开始时由回合控制器调用。
- 作用:调用 decide_action 产出决策,向战斗组件下发 execute_action,封装并返回结果。
- 返回:AIActionResult(is_valid/source/target/damage/action_type/skill)。
- decide_action() -> Dictionary character_ai_component.gd:L88-L170
- 时机:每次行动前内部调用;也可外部调试调用。
- 作用:效用评估并选择“技能/防御/普攻”;若无可行目标返回空行动。
- 返回:{ action_type, target, params };空行动为 { null, null, {} }。
- get_potential_targets() -> Array[Character] character_ai_component.gd:L172-L186
- 时机:决策时获取潜在敌我目标集合。
- 作用:合并敌方、友方候选。
- get_targets_for_skill(skill, potential_targets) -> Array[Character] character_ai_component.gd:L187-L236
- 时机:评估技能可用目标。
- 作用:依技能 target_type 过滤/压缩为单体/群体目标。
- set_ai_enabled(enabled:bool) -> void character_ai_component.gd:L238-L242
- 时机:外部启停自动战斗或玩家接管。
- 内部辅助(下划线约定“内部使用”)
- _should_use_defense(character)->bool character_ai_component.gd:L243-L259
- _select_best_target_for_skill(skill, valid_targets)->Character character_ai_component.gd:L261-L309
- _get_available_skills()->Array character_ai_component.gd:L311-L321
- _can_use_skill(skill)->bool character_ai_component.gd:L322-L338 (未被当前流程使用)
- AIBehavior 暴露接口
- 属性(可在 Inspector 配置) ai_behavior.gd:L5-L17
- behavior_type:String:行为风格。
- 一组权重:attack/skill_offensive/support/healing/target_low_health/target_high_threat/heal_low_health/self_preservation。
- 只读字典 weights ai_behavior.gd:L19-L30 便于统一取值。
- 方法
- set_behavior_type(type)->void ai_behavior.gd:L41-L46 :设置风格并重配权重。
- evaluate_skill(character, skill, targets)->float ai_behavior.gd:L47-L90 :计算技能效用分。
- evaluate_attack_target(_character, target)->float ai_behavior.gd:L91-L115 :计算普攻目标效用分。
- 内部辅助
- _get_skill_tags(skill)->Array[SkillTag] ai_behavior.gd:L117-L141
- _is_enemy(c1, c2)->bool ai_behavior.gd:L143-L154
- _configure_behavior()->void ai_behavior.gd:L155-L208
2) 实现机制
- 控制/数据流(ASCII 流程图) 回合开始(轮到我方/敌方角色)
↓
CharacterAIComponent.execute_action() - 校验 ai_enabled/组件有效性
- 决策 = decide_action()
↓
decide_action() - potential_targets = get_potential_targets()
- available_skills = _get_available_skills()
- 若满足:有技能且 randf()<skill_usage_chance:
• 为每个技能收集可用目标 get_targets_for_skill()
• 行为层 evaluate_skill() 评分(含随机扰动)
• 选最高分技能→SKILL 决策
否则:
• _should_use_defense() 判定→若可 DEFEND
• 否则评估普攻:对候选敌方 behavior.evaluate_attack_target() 打分,取最优→ATTACK 决策 - 无目标/无动作→返回空行动
↓
执行 - 组装 params 并注入 SkillExecutionContext
- await CharacterCombatComponent.execute_action(action_type, target, params)
- 包装 AIActionResult 返回
↓
回合结束(由上层战斗管理器推进) - 决策算法与关键代码
- 效用评分 + 随机扰动
- 技能评分:标签映射 + 场景加分 + 消耗惩罚 + 随机偏移 ai_behavior.gd:L52-L90
- OFFENSIVE/HEALING/SUPPORT/DEFENSIVE 分别叠加对应权重
- 治疗:对低血量友方按剩余血量提升加分
- 防御:自身低血时按 self_preservation 提升
- MP 消耗按 max_mp 比例惩罚
- randf_range(-0.2,0.2) 提供探索性
- 普攻目标评分:基础攻击倾向 + 打低血 + 高威胁 ai_behavior.gd:L95-L115
- health_percent 越低,分越高
- threat_score 由 attack_power 近似
- randf_range(-0.1,0.1) 扰动
- 行为层权重配置:依据 behavior_type 在 _configure_behavior 设定不同风格 ai_behavior.gd:L156-L208
- 组件层决策骨架:技能优先→防御→普攻 character_ai_component.gd:L107-L170
- 战场状态读取与使用
- 目标集:BattleManager.get_valid_enemy_targets()/get_valid_ally_targets() character_ai_component.gd:L174-L186
- 技能可用:CharacterCombatComponent.get_available_skills() character_ai_component.gd:L313-L321
- 能力/资源:health_percent、max_mp、attack_power 等在评分中使用 ai_behavior.gd:L76-L84 , ai_behavior.gd:L102-L110
- 技能效果:通过 SkillData.effects 判断标签 ai_behavior.gd:L120-L135
- 敌友判定:_is_enemy 期望由 character.ai_component.character_registry 提供 ai_behavior.gd:L147-L154
3) 行为体系 - 技能(SKILL)
- 触发条件:存在可用技能且 randf()<skill_usage_chance,且该技能对某些目标有效 character_ai_component.gd:L107-L133
- 优先级/评分:AIBehavior.evaluate_skill;攻击/治疗/辅助/防御标签加分,低血队友额外加分,MP 高消耗扣分,随机扰动 ai_behavior.gd:L52-L90
- 目标选择:get_targets_for_skill 先按 target_type 过滤,再单体类用 _select_best_target_for_skill 细选 character_ai_component.gd:L191-L236 , character_ai_component.gd:L265-L309
- 执行副作用:通过战斗组件执行,计算并返回伤害等信息;注入 SkillExecutionContext character_ai_component.gd:L64-L73
- 失败回退:若无技能或评分<=0→检查防御;仍不满足→普攻;仍无→返回空行动。
- 防御(DEFEND)
- 触发条件:_should_use_defense 为真且角色存在 defense_skill character_ai_component.gd:L136-L144
- 判定逻辑:health_percent<阈值时,按 self_preservation(1-health)0.5 概率触发 character_ai_component.gd:L246-L257
- 执行副作用:使用角色的 defense_skill 对自己施放,参数放入 params.skill。
- 失败回退:无防御技能或判定失败→普攻。
- 普攻(ATTACK)
- 触发条件:技能未选中且防御未触发。
- 优先级/评分:evaluate_attack_target(攻击倾向+打低血+高威胁+随机) ai_behavior.gd:L95-L115
- 目标选择:对 valid_attack_targets(确认为敌方的 potential_targets 子集)打分取最大 character_ai_component.gd:L146-L165
- 执行副作用:交由战斗组件执行基本攻击。
- 失败回退:若无有效敌方目标→空行动。
