今天很是开心,观看cocos官方直播竟然在几千人中中奖,能够买彩票了。
言归正传,所谓的人工智能,也就是你们常说的AI(Artificial Intelligence)。一说到AI可能就会让人以为比较深奥,其实也就是非玩家角色思考和行为的综合。好比,在什么样的条件下,触发什么样的行为。
其实咱们在游戏开发中的AI要比学术理论中的AI简单不少,甚至有些行为不须要AI也能体现。好比使用剧情对话体现非玩家角色的想法。
那么AI 都涉及到哪些东西呢?dom
首先仍是要定义好行为枚举,经过状态机,不一样的行为实现不一样的逻辑。
函数
定义感知器特征
不一样的游戏感知的特征确定是不同的,根据游戏需求而定
工具
实现感知类
学习
定义决策树this
export default class DecisionTree { private decisionData: XlsxData; private perception: Perception; constructor(data: XlsxData) { this.decisionData = data; } setPerception(perception: Perception) { this.perception = perception; } getPerception(obj, perceptionType: PerceptionType, value: number) { return this.perception.action(obj, perceptionType, value) } //开始思考 action(obj: RoleView, decisionID: number) { let data = this.decisionData.getRowData(decisionID) let flag = false; if (data) { let perceptionType = data[Ai_dataEnum.condition]; let type = 0; let id: number[] = null; flag = this.perception.action(obj, perceptionType, data[Ai_dataEnum.cParam]) if (flag) { type = data[Ai_dataEnum.conditionYes] id = data[Ai_dataEnum.parm1] } else { type = data[Ai_dataEnum.conditionNo] id = data[Ai_dataEnum.parm2] } this.judge(obj, type, id) }else{ } return flag; } //断定感知条件 private judge(obj: RoleView, type: ThinkType, param: number[]) { if (type == ThinkType.ACTION) { this.doLogic(obj, param) } else { for (let index = 0; index < param.length; index++) { const element = param[index]; if (this.action(obj, element)) { break;//目前仅支持串行,不支持并行。如需支持并行,须要添加是否拦截字段。 } } } } // 50 30 20 : 80 根据几率选择行为 private doLogic(obj: RoleView, param: number[]) { if (param.length > 0) { let r = RandomHelper.random(0, 100); let count = param.length / 2 for (let index = 0; index < count; index++) { let behaveType: number = param[index * 2] let random: number = param[index * 2 + 1] // if (r <= random) { // 设置非玩家角色的行为。 obj.setBehaveType(behaveType) return; } } } } }
export default class EnemyController extends GameController { private perception: Perception = new Perception(); private ai: DecisionTree; constructor() { super() let ai_data: XlsxData = GameDataManager.instance().get(DataName.ai_data) this.ai = new DecisionTree(ai_data) this.ai.setPerception(this.perception) } getPerception(obj, perceptionType: PerceptionType, value: number) { return this.perception.action(obj, perceptionType, value) } action(obj: RoleView, decisionID: number) { this.ai.action(obj, decisionID) } }
在非玩家角色中声明控制器和行为管理器
人工智能
定义思考函数3d
think() { this.ai.action(this, this.model.getAI()) }
以上就是我我的对游戏开发中AI的理解,固然我是拜读了《游戏人工智能——计算机游戏中的人工智能》这本书的。好像此书已经绝版了。但愿放出来对热衷于游戏开发的小伙伴们有所帮助。code
欢迎扫码关注公众号《微笑游戏》,浏览更多内容。blog