教你如何制作怪物智能AI(转)

转自:http://cocos2d.9tech.cn/news/2013/1112/38577.html

谈到怪物AI,我觉得就比较话多了,首先理解一下(Artificial Intelligence人工智能),人工智能是计算机科学的一个分支,人工智能是计算机科学技术的前沿科技领域。人工智能与计算机软件有密切的关系。各种人工智能应用系统都要用计算机软件去实现,许多聪明的计算机软件也应用了人工智能的理论方法和技术。

总而言之,言而总之,我理解的AI就是:用人类自己的思考模式去赋予游戏中角色判断的能力,来进行某些特定的行为!

下面具体图文介绍一下,我在游戏中一些实用的AI制作思路。

这是这个思路就可以让怪物捕获玩家,并且追踪玩家攻击的一个图示!这个思路是基本,最终决策怪物是否去执行这个行为的操作算法又是一个AI智能,所以咱们又可以分为:

决策AI:可以根据怪物等级,怪物的类型(BOSS),怪物技能类型等很多决策和根本属性相连所起作用的操作,都可以做成决策AI;

行为AI:追踪玩家,攻击玩家,给玩家加血(如同伴),逃跑等这些行为动作操作;

OK,下面大家应该了解AI的基本思路了,那我们直接贴代码了;

我直接把AI写在了地图层里面了,因为之前我框架的设计,是有一个地图base,而所有的怪物,主角色,NPC都属于地图层,因为要做很多遍历操作,所以在地图base中写上智能AI算法,然后再继承拓展下来。

首先先制造一群怪物!并且让怪物在设置的Rect内随机移动!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/********************************************
注释:
makenum 最大怪物数量
monsteridtags 怪物起始标签
randomrange 配置怪圈大小(只能填正整数)
monster_model 怪物数据
yinzihigth 影子的距离
fristpoint 怪物初始设定的怪圈点
*********************************************/
void Maps_Diyu::makemonster(int makenum,int monsteridtags,CCPoint randomrange,MainRoledata monster_model,int yinzihigth,CCPoint fristpoint)
{
     for (int i = 0; i < makenum; i++)
     {
         int add_x = (int)(CCRANDOM_MINUS1_1()*randomrange.x);
         int add_y =(int)(CCRANDOM_MINUS1_1()*randomrange.y);
         monster_model.tags=monsteridtags+i;
         monster_model.nowpoint= CCPointMake(nowmap->getContentSize().width/2+fristpoint.x+add_x,nowmap->getContentSize().height/2+fristpoint.y+add_y);
         SpiritsMonster* newmonster = new SpiritsMonster(monster_model,1,yinzihigth);
         nowmap->addChild(newmonster->monster, 1,monsteridtags+i);
     }
}

然后处理怪物的行为AI:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/********************************************
注释:
monsternum 最大怪物数量
monsteridtags 怪物起始标签
monster_model 怪物数据
reatR 怪物仇恨视角的范围值(通常200左右)
attck_reatR 怪物可攻击范围值(远程怪和物理怪物)
playerpoint 主角坐标
randomrange 怪物默认随机移动范围
fristpoint 怪物初始设定的怪圈点
*********************************************/
void Maps_Diyu::attact_todo(int monsternum,int monsteridtags ,MainRoledata monster_model,int reatR,int attck_reatR,CCPoint playerpoint,CCPoint randomrange,CCPoint fristpoint)
{
     for (int i = 0; i < monsternum; i++)
     {
         CCRect* attck_rect = new CCRectMake( this ->getChildByTag(monsteridtags+i)->getPosition().x-attck_reatR/2, this ->getChildByTag(monsteridtags+i)->getPosition().y-attck_reatR/2,attck_reatR,attck_reatR);
         if (attck_rect->containsPoint(playerpoint)== true )
         {
             //执行怪物攻击主角
             CCLog(FontChina::G2U( "进入怪物可攻击范围" ));
             //攻击时改变方向
             SpiritsMonster::attackTomainRole_dir(ccp((int)playerpoint.x,(int)playerpoint.y),(CCSprite*)( this ->getChildByTag(monsteridtags+i)),monster_model);
             //释放技能动画
             int add_x = (int)(CCRANDOM_MINUS1_1()*8);
             int add_y =(int)(CCRANDOM_MINUS1_1()*8);
             SkillEffects* skill = new SkillEffects(ccp((int)playerpoint.x+add_x,(int)playerpoint.y+add_y),monster_model,(0.2f+CCRANDOM_0_1()),8,3,1000);
             this ->addChild(skill->effects_main,1000,50);
         }
         else
         {
             CCRect* track_rect = new CCRectMake( this ->getChildByTag(monsteridtags+i)->getPosition().x-reatR/2, this ->getChildByTag(monsteridtags+i)->getPosition().y-reatR/2,reatR,reatR);
             if (track_rect->containsPoint(playerpoint)== true )
             {
                 //执行移动,不停追杀主角
                 CCLog(FontChina::G2U( "进入怪物仇恨视野区域" ));
                 //防止靠近主角,保持一定距离!主要为了好看和处理被攻击效果。
                 int add_x = (int)(CCRANDOM_MINUS1_1()*25);
                 int add_y =(int)(CCRANDOM_MINUS1_1()*25);
                 //移动时改变方向
                 SpiritsMonster::moveTomap_dir(ccp((int)playerpoint.x,(int)playerpoint.y),(CCSprite*)( this ->getChildByTag(monsteridtags+i)),monster_model);
                 CCArray* callbackArray = CCArray::create();
                 int nowtags = monsteridtags+i;
                 char strs[64];
                 sprintf(strs, "%d" ,nowtags);
                 CCString* ccnowtags = CCString::create(strs);
                 callbackArray->addObject(ccnowtags);
                 callbackArray->addObject(monster_model.spiritname);
                 char dir_x[64];
                 sprintf(dir_x, "%d" ,(int) this ->getChildByTag(nowtags)->getPositionX());
                 char dir_y[64];
                 sprintf(dir_y, "%d" ,(int) this ->getChildByTag(nowtags)->getPositionY());
                 CCString* d_x = CCString::create(dir_x);
                 CCString* d_y = CCString::create(dir_y);
                 callbackArray->addObject(d_x);
                 callbackArray->addObject(d_y);
                 CCFiniteTimeAction *actbackfun = CCCallFuncO::create( this , callfuncO_selector(Maps_Diyu::moveoverCallBack),callbackArray);
                 CCActionInterval* act_movexixue = CCMoveTo::create(3+CCRANDOM_MINUS1_1(),ccp((int)playerpoint.x+add_x,(int)playerpoint.y+add_y));
                 this ->getChildByTag(nowtags)->runAction(CCSequence::create(act_movexixue,actbackfun,NULL));
             }
             else
             {
                 //脱离仇恨时,随机定点周围移动
                 int stopandrun = (int)(CCRANDOM_MINUS1_1()*10);
                 if (stopandrun>=1)
                 {
                     //脱离仇恨
                     int add_x = (int)(CCRANDOM_MINUS1_1()*randomrange.x);
                     int add_y =(int)(CCRANDOM_MINUS1_1()*randomrange.y);
                     CCPoint move_dir = ccp( this ->getContentSize().width/2+fristpoint.x+add_x, this ->getContentSize().height/2+fristpoint.y+add_y);
 
                     SpiritsMonster::moveTomap_dir(ccp((int)move_dir.x,(int)move_dir.y),(CCSprite*)( this ->getChildByTag(monsteridtags+i)),monster_model);
                     CCArray* callbackArray = CCArray::create();
                     int nowtags = monsteridtags+i;
                     char strs[64];
                     sprintf(strs, "%d" ,nowtags);
                     CCString* ccnowtags = CCString::create(strs);
                     callbackArray->addObject(ccnowtags);
                     callbackArray->addObject(monster_model.spiritname);
                     char dir_x[64];
                     sprintf(dir_x, "%d" ,(int) this ->getChildByTag(nowtags)->getPositionX());
                     char dir_y[64];
                     sprintf(dir_y, "%d" ,(int) this ->getChildByTag(nowtags)->getPositionY());
                     CCString* d_x = CCString::create(dir_x);
                     CCString* d_y = CCString::create(dir_y);
                     callbackArray->addObject(d_x);
                     callbackArray->addObject(d_y);
                     CCFiniteTimeAction *actbackfun = CCCallFuncO::create( this , callfuncO_selector(Maps_Diyu::moveoverCallBack),callbackArray);
                     CCActionInterval* act_movexixue = CCMoveTo::create(3+CCRANDOM_MINUS1_1(),ccp(move_dir.x,move_dir.y));
                     this ->getChildByTag(nowtags)->runAction(CCSequence::create(act_movexixue,actbackfun,NULL));
 
                 }
             }
         }
     }
}

这段逻辑包括了,如果玩家进入怪物的仇恨区域,那么怪物会先移动到玩家附近的点(此时玩家移动开,怪物会根据定时器去判断下一步如何操作:如1,玩家移动后还在仇恨区域,那么继续追踪玩家;2,如果玩家脱离仇恨,那么怪物执行回到初始坐标点的操作,然后继续原地巡逻!),如果此时玩家已经在怪物的攻击范围内了,那么怪物会直接选择攻击!这样怪物就会变得貌似有了思想一样,他会去考虑判断各种行为,那个区域我该做什么?不该做什么?

有了这个逻辑,我们还要赋予怪物循环思考的能力!

1
2
//启动所有怪物智能AI监听器
     nowmap->schedule(schedule_selector(Maps_Diyu::makemonsterAttack), (3.0f));

我们根据定时器去每隔3秒内去监控行为AI!

整个行为AI已经完毕!就可以实现比较智能的怪物追杀主角了!这也就牵扯到了游戏难度怪物难度的问题了,往往好玩的游戏智能AI都占用了程序一半以上时间去处理;怪物并不是血多,防御高他就是一个BOSS,如果BOSS他会满地图追着玩家打,会自己加血,逃跑,那这样的BOSS我们作为玩家来说,打起来也比较刺激有意思,有挑战!

来自:cnblogs

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值