守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 2281|回复: 2

[算法/性能优化] 蚁群算法ACO(ant colony optimization)的原理以及实现源代码

[复制链接]
  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52620
    钢镚
    1422

    开源英雄守望者

    发表于 2015-2-6 10:33:35 | 显示全部楼层 |阅读模式
    本帖最后由 破晓 于 2015-2-6 10:58 编辑

    之前说的算法基本上都比较枯燥的(废话,算法都很枯燥……),这次要介绍的蚁群算法(Ant Colony Algorithm)却是一种源于自然现象的算法,也是一种 meta heuristic,即与具体问题关系不大的优化算法,也就是它是一种用来在图中寻找优化路径的机率型技术。Marco Dorigo于1992年在他的博士论文中引入,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。


    小小的蚂蚁总是能够找到食物,他们具有什么样的智能呢?设想,如果我们要为蚂蚁设计一个人工智能的程序,那么这个程序要多么复杂呢?首先,你要让蚂蚁能够避开障碍物,就必须根据适当的地形给它编进指令让他们能够巧妙的避开障碍物,其次,要让蚂蚁找到食物,就需要让他们遍历空间上的所有点;再次,如果要让蚂蚁找到最短的路径,那么需要计算所有可能的路径并且比较它们的大小,而且更重要的是,你要小心翼翼的编程,因为程序的错误也许会让你前功尽弃。这是多么不可思议的程序!太复杂了,恐怕没人能够完成这样繁琐冗余的程序。

    为什么这么简单的程序会让蚂蚁干这样复杂的事情?答案是:简单规则的涌现。事实上,每只蚂蚁并不是像我们想象的需要知道整个世界的信息,他们其实只关心很小范围内的眼前信息,而且根据这些局部信息利用几条简单的规则进行决策,这样,在蚁群这个集体里,复杂性的行为就会凸现出来。这就是人工生命、复杂性科学解释的规律!

    下面就是实现如此复杂性的七条简单规则:

    1、范围:
    蚂蚁观察到的范围是一个方格世界,蚂蚁有一个参数为速度半径(一般是3),那么它能观察到的范围就是3*3个方格世界,并且能移动的距离也在这个范围之内。
    2、环境:
    蚂蚁所在的环境是一个虚拟的世界,其中有障碍物,有别的蚂蚁,还有信息素,信息素有两种,一种是找到食物的蚂蚁洒下的食物信息素,一种是找到窝的蚂蚁洒下的窝的信息素。每个蚂蚁都仅仅能感知它范围内的环境信息。环境以一定的速率让信息素消失。
    3、觅食规则:
    在每只蚂蚁能感知的范围内寻找是否有食物,如果有就直接过去。否则看是否有信息素,并且比较在能感知的范围内哪一点的信息素最多,这样,它就朝信息素多的地方走,并且每只蚂蚁多会以小概率犯错误,从而并不是往信息素最多的点移动。蚂蚁找窝的规则和上面一样,只不过它对窝的信息素做出反应,而对食物信息素没反应。
    4、移动规则:
    每只蚂蚁都朝向信息素最多的方向移,并且,当周围没有信息素指引的时候,蚂蚁会按照自己原来运动的方向惯性的运动下去,并且,在运动的方向有一个随机的小的扰动。为了防止蚂蚁原地转圈,它会记住最近刚走过了哪些点,如果发现要走的下一点已经在最近走过了,它就会尽量避开。
    5、避障规则:
    如果蚂蚁要移动的方向有障碍物挡住,它会随机的选择另一个方向,并且有信息素指引的话,它会按照觅食的规则行为。
    7、播撒信息素规则:
    每只蚂蚁在刚找到食物或者窝的时候撒发的信息素最多,并随着它走远的距离,播撒的信息素越来越少。

    下面的程序开始运行之后,蚂蚁们开始从窝里出动了,寻找食物;他们会顺着屏幕爬满整个画面,直到找到食物再返回窝。

    其中,‘F’点表示食物,‘H’表示窝,白色块表示障碍物,‘+’就是蚂蚁了。

    参数说明:
    最大信息素:蚂蚁在一开始拥有的信息素总量,越大表示程序在较长一段时间能够存在信息素。信息素消减的速度:随着时间的流逝,已经存在于世界上的信息素会消减,这个数值越大,那么消减的越快。
    错误概率表示这个蚂蚁不往信息素最大的区域走的概率,越大则表示这个蚂蚁越有创新性。
    速度半径表示蚂蚁一次能走的最大长度,也表示这个蚂蚁的感知范围。
    记忆能力表示蚂蚁能记住多少个刚刚走过点的坐标,这个值避免了蚂蚁在本地打转,停滞不前。而这个值越大那么整个系统运行速度就慢,越小则蚂蚁越容易原地转圈。

    源代码如下(不同编译器可能需做一定修改):
    1. /*ant.c*/

    2. #define SPACE 0x20
    3. #define ESC 0x1b
    4. #define ANT_CHAR_EMPTY '+'
    5. #define ANT_CHAR_FOOD 153
    6. #define HOME_CHAR 'H'
    7. #define FOOD_CHAR 'F'
    8. #define FOOD_CHAR2 'f'
    9. #define FOOD_HOME_COLOR 12
    10. #define BLOCK_CHAR 177

    11. #define MAX_ANT 50
    12. #define INI_SPEED 3
    13. #define MAXX 80
    14. #define MAXY 23
    15. #define MAX_FOOD 10000
    16. #define TARGET_FOOD 200
    17. #define MAX_SMELL 5000
    18. #define SMELL_DROP_RATE 0.05
    19. #define ANT_ERROR_RATE 0.02
    20. #define ANT_EYESHOT 3
    21. #define SMELL_GONE_SPEED 50
    22. #define SMELL_GONE_RATE 0.05
    23. #define TRACE_REMEMBER 50
    24. #define MAX_BLOCK 100

    25. #define NULL 0
    26. #define UP 1
    27. #define DOWN 2
    28. #define LEFT 3
    29. #define RIGHT 4
    30. #define SMELL_TYPE_FOOD 0
    31. #define SMELL_TYPE_HOME 1

    32. #include "stdio.h"
    33. #include "conio.h"
    34. #include "dos.h"
    35. #include "stdlib.h"
    36. #include "dos.h"
    37. #include "process.h"
    38. #include "ctype.h"
    39. #include "math.h"

    40. void WorldInitial(void);
    41. void BlockInitial(void);
    42. void CreatBlock(void);
    43. void SaveBlock(void);
    44. void LoadBlock(void);
    45. void HomeFoodInitial(void);
    46. void AntInitial(void);
    47. void WorldChange(void);
    48. void AntMove(void);
    49. void AntOneStep(void);
    50. void DealKey(char key);
    51. void ClearSmellDisp(void);
    52. void DispSmell(int type);
    53. int AntNextDir(int xxx,int yyy,int ddir);
    54. int GetMaxSmell(int type,int xxx,int yyy,int ddir);
    55. int IsTrace(int xxx,int yyy);
    56. int MaxLocation(int num1,int num2,int num3);
    57. int CanGo(int xxx,int yyy,int ddir);
    58. int JudgeCanGo(int xxx,int yyy);
    59. int TurnLeft(int ddir);
    60. int TurnRight(int ddir);
    61. int TurnBack(int ddir);

    62. int MainTimer(void);
    63. char WaitForKey(int secnum);
    64. void DispPlayTime(void);
    65. int TimeUse(void);
    66. void HideCur(void);
    67. void ResetCur(void);

    68. /* ---------------  */
    69. struct HomeStruct
    70. {
    71.   int xxx,yyy;
    72.   int amount;
    73.   int TargetFood;
    74. }home;

    75. struct FoodStruct
    76. {
    77.   int xxx,yyy;
    78.   int amount;
    79. }food;

    80. struct AntStruct
    81. {
    82.   int xxx,yyy;
    83.   int dir;
    84.   int speed;
    85.   int SpeedTimer;
    86.   int food;
    87.   int SmellAmount[2];
    88.   int tracex[TRACE_REMEMBER];
    89.   int tracey[TRACE_REMEMBER];
    90.   int TracePtr;
    91.   int IQ;
    92. }ant[MAX_ANT];
    93. int AntNow;
    94. int timer10ms;
    95. struct time starttime,endtime;
    96. int Smell[2][MAXX+1][MAXY+1];
    97. int block[MAXX+1][MAXY+1];
    98. int SmellGoneTimer;
    99. int SmellDispFlag;
    100. int CanFindFood;
    101. int HardtoFindPath;

    102. /* ----- Main -------- */
    103. void main(void)
    104. {
    105.   char KeyPress;
    106.   int tu;

    107.   clrscr();
    108.   HideCur();
    109.   WorldInitial();
    110.   do
    111.   {
    112.     timer10ms = MainTimer();
    113.     if(timer10ms) AntMove();
    114.     if(timer10ms) WorldChange();
    115.     tu = TimeUse();
    116.     if(tu>=60&&!CanFindFood)
    117.     {
    118.       gotoxy(1,MAXY+1);
    119.       printf("Can not find food, maybe a block world.");
    120.       WaitForKey(10);
    121.       WorldInitial();
    122.     }
    123.     if(tu>=180&&home.amount<100&&!HardtoFindPath)
    124.     {
    125.       gotoxy(1,MAXY+1);
    126.       printf("God! it is so difficult to find a path.");
    127.       if(WaitForKey(10)==0x0d) WorldInitial();
    128.       else
    129.      {
    130.         HardtoFindPath = 1;
    131.         gotoxy(1,MAXY+1);
    132.         printf("                     ");
    133.      }
    134.     }
    135.     if(home.amount>=home.TargetFood)
    136.     {
    137.       gettime(&endtime);
    138.       KeyPress = WaitForKey(60);
    139.       DispPlayTime();
    140.       WaitForKey(10);
    141.       WorldInitial();
    142.     }
    143.     else if(kbhit())
    144.     {
    145.       KeyPress = getch();
    146.       DealKey(KeyPress);
    147.     }
    148.     else KeyPress = NULL;
    149.   }
    150.   while(KeyPress!=ESC);
    151.   gettime(&endtime);
    152.   DispPlayTime();
    153.   WaitForKey(10);
    154.   clrscr();
    155.   ResetCur();
    156. }

    157. /* ------ general sub process ----------- */
    158. int MainTimer(void)
    159. /* output: how much 10ms have pass from last time call this process */
    160. {
    161.   static int oldhund,oldsec;
    162.   struct  time t;
    163.   int timeuse;

    164.   gettime(&t);
    165.   timeuse = 0;
    166.   if(t.ti_hund!=oldhund)
    167.   {
    168.     if(t.ti_sec!=oldsec)
    169.     {
    170.       timeuse+=100;
    171.       oldsec = t.ti_sec;
    172.     }
    173.     timeuse+=t.ti_hund-oldhund;
    174.     oldhund = t.ti_hund;
    175.   }
    176.   else timeuse = 0;
    177.   return (timeuse);
    178. }

    179. char WaitForKey(int secnum)
    180. /* funtion: if have key in, exit immediately, else wait 'secnum' senconds then exit
    181.    input: secnum -- wait this senconds, must < 3600 (1 hour)
    182.    output: key char, if no key in(exit when timeout), return NULL */
    183. {
    184.   int secin,secnow;
    185.   int minin,minnow;
    186.   int hourin,hournow;
    187.   int secuse;
    188.   struct  time t;

    189.   gettime(&t);
    190.   secin = t.ti_sec;
    191.   minin = t.ti_min;
    192.   hourin = t.ti_hour;

    193.   do
    194.   {
    195.     if(kbhit()) return(getch());
    196.     gettime(&t);
    197.     secnow = t.ti_sec;
    198.     minnow = t.ti_min;
    199.     hournow = t.ti_hour;

    200.     if(hournow!=hourin) minnow+=60;
    201.     if(minnow>minin) secuse = (minnow-1-minin) + (secnow+60-secin);
    202.     else secuse = secnow - secin;

    203.     /* counting error check */
    204.     if(secuse<0)
    205.     {
    206.       gotoxy(1,MAXY+1);
    207.       printf("Time conuting error, any keyto exit...");
    208.       getch();
    209.       exit(3);
    210.     }
    211.   }
    212.   while(secuse<=secnum);
    213.   return (NULL);
    214. }

    215. void DispPlayTime(void)
    216. {
    217.   int ph,pm,ps;

    218.   ph = endtime.ti_hour - starttime.ti_hour;
    219.   pm = endtime.ti_min - starttime.ti_min;
    220.   ps = endtime.ti_sec - starttime.ti_sec;

    221.   if(ph<0) ph+=24;
    222.   if(pm<0) { ph--; pm+=60; }
    223.   if(ps<0) { pm--; ps+=60; }

    224.   gotoxy(1,MAXY+1);
    225.   printf("Time use: %d hour- %d min- %d sec ",ph,pm,ps);
    226. }

    227. int TimeUse(void)
    228. {
    229.   int ph,pm,ps;

    230.   gettime(&endtime);
    231.   ph = endtime.ti_hour - starttime.ti_hour;
    232.   pm = endtime.ti_min - starttime.ti_min;
    233.   ps = endtime.ti_sec - starttime.ti_sec;

    234.   if(ph<0) ph+=24;
    235.   if(pm<0) { ph--; pm+=60; }
    236.   if(ps<0) { pm--; ps+=60; }

    237.   return(ps+(60*(pm+60*ph)));
    238. }

    239. void HideCur(void)
    240. {
    241.   union REGS regs0;
    242.   regs0.h.ah=1;
    243.   regs0.h.ch=0x30;
    244.   regs0.h.cl=0x31;
    245.   int86(0x10,®s0,®s0);
    246. }

    247. void ResetCur(void)
    248. {
    249.   union REGS regs0;
    250.   regs0.h.ah=1;
    251.   regs0.h.ch=0x06;
    252.   regs0.h.cl=0x07;
    253.   int86(0x10,®s0,®s0);
    254. }

    255. /* ------------ main ANT programe ------------- */
    256. void WorldInitial(void)
    257. {
    258.   int k,i,j;
    259.   randomize();
    260.   clrscr();
    261.   HomeFoodInitial();
    262.   for(AntNow=0;AntNow<MAX_ANT;AntNow++)
    263.   {
    264.     AntInitial();
    265.   } /* of for AntNow */;

    266.   BlockInitial();
    267.   for(k=0;k<=1;k++)
    268.   /* SMELL TYPE FOOD and HOME */
    269.     for(i=0;i<=MAXX;i++)
    270.       for(j=0;j<=MAXY;j++)
    271.         Smell[k][i][j] = 0;
    272.   SmellGoneTimer = 0;
    273.   gettime(&starttime);
    274.   SmellDispFlag = 0;
    275.   CanFindFood = 0;
    276.   HardtoFindPath = 0;
    277. }

    278. void BlockInitial(void)
    279. {
    280.   int i,j;
    281.   int bn;

    282.   for(i=0;i<=MAXX;i++)
    283.     for(j=0;j<=MAXY;j++)
    284.       block[i][j] = 0;

    285.   bn = 1+ MAX_BLOCK/2 + random(MAX_BLOCK/2);
    286.   for(i=0;i<=bn;i++) CreatBlock();
    287. }

    288. void CreatBlock(void)
    289. {
    290.   int x1,y1,x2,y2;
    291.   int dx,dy;
    292.   int i,j;

    293.   x1 = random(MAXX)+1;
    294.   y1 = random(MAXY)+1;

    295.   dx = random(MAXX/10)+1;
    296.   dy = random(MAXY/10)+1;

    297.   x2 = x1+dx;
    298.   y2 = y1+dy;

    299.   if(x2>MAXX) x2 = MAXX;
    300.   if(y2>MAXY) y2 = MAXY;

    301.   if(food.xxx>=x1&&food.xxx<=x2&&food.yyy>=y1&&food.yyy<=y2) return;
    302.   if(home.xxx>=x1&&home.xxx<=x2&&home.yyy>=y1&&home.yyy<=y2) return;

    303.   for(i=x1;i<=x2;i++)
    304.     for(j=y1;j<=y2;j++)
    305.     {
    306.       block[i][j] = 1;
    307.       gotoxy(i,j);
    308.       putch(BLOCK_CHAR);
    309.     }
    310. }

    311. void SaveBlock(void)
    312. {
    313. FILE *fp_block;
    314. char FileNameBlock[20];
    315. int i,j;

    316. gotoxy(1,MAXY+1);
    317.   printf("                     ");
    318. gotoxy(1,MAXY+1);
    319. printf("Save to file...",FileNameBlock);
    320. gets(FileNameBlock);
    321. if(FileNameBlock[0]==0) strcpy(FileNameBlock,"Ant.ant");
    322. else strcat(FileNameBlock,".ant");

    323. if ((fp_block = fopen(FileNameBlock, "wb")) == NULL)
    324. { gotoxy(1,MAXY+1);
    325.     printf("Creat file %s fail...",FileNameBlock);
    326.   getch();
    327.   exit(2);
    328. }
    329. gotoxy(1,MAXY+1);
    330.   printf("                           ");

    331. fputc(home.xxx,fp_block);
    332. fputc(home.yyy,fp_block);
    333. fputc(food.xxx,fp_block);
    334. fputc(food.yyy,fp_block);

    335. for(i=0;i<=MAXX;i++)
    336.     for(j=0;j<=MAXY;j++)
    337.       fputc(block[i][j],fp_block);

    338.   fclose(fp_block);
    339. }

    340. void LoadBlock(void)
    341. {
    342. FILE *fp_block;
    343. char FileNameBlock[20];
    344. int i,j,k;

    345. gotoxy(1,MAXY+1);
    346.   printf("                     ");
    347. gotoxy(1,MAXY+1);
    348. printf("Load file...",FileNameBlock);
    349. gets(FileNameBlock);
    350. if(FileNameBlock[0]==0) strcpy(FileNameBlock,"Ant.ant");
    351. else strcat(FileNameBlock,".ant");

    352. if ((fp_block = fopen(FileNameBlock, "rb")) == NULL)
    353. { gotoxy(1,MAXY+1);
    354.     printf("Open file %s fail...",FileNameBlock);
    355.   getch();
    356.   exit(2);
    357. }

    358. clrscr();
    359. home.xxx = fgetc(fp_block);
    360. home.yyy = fgetc(fp_block);
    361. food.xxx = fgetc(fp_block);
    362. food.yyy = fgetc(fp_block);
    363. gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
    364.   gotoxy(food.xxx,food.yyy); putch(FOOD_CHAR);
    365.   food.amount = random(MAX_FOOD/3)+2*MAX_FOOD/3+1;
    366.   /* food.amount = MAX_FOOD; */
    367.   home.amount = 0;
    368.   home.TargetFood =
    369.     (food.amount<TARGET_FOOD)?food.amount:TARGET_FOOD;

    370. for(AntNow=0;AntNow<MAX_ANT;AntNow++)
    371.   {
    372.     AntInitial();
    373.   } /* of for AntNow */;

    374. for(i=0;i<=MAXX;i++)
    375.     for(j=0;j<=MAXY;j++)
    376.     {
    377.       block[i][j] = fgetc(fp_block);
    378.       if(block[i][j])
    379.       {
    380.        gotoxy(i,j);
    381.        putch(BLOCK_CHAR);
    382.      }
    383.     }

    384.   for(k=0;k<=1;k++)
    385.   /* SMELL TYPE FOOD and HOME */
    386.     for(i=0;i<=MAXX;i++)
    387.       for(j=0;j<=MAXY;j++)
    388.         Smell[k][i][j] = 0;
    389.   SmellGoneTimer = 0;
    390.   gettime(&starttime);
    391.   SmellDispFlag = 0;
    392.   CanFindFood = 0;
    393.   HardtoFindPath = 0;

    394.   fclose(fp_block);
    395. }

    396. void HomeFoodInitial(void)
    397. {
    398.   int randnum;
    399.   int homeplace;
    400.   /* 1 -- home at left-up, food at right-down
    401.      2 -- home at left-down, food at right-up
    402.      3 -- home at right-up, food at left-down
    403.      4 -- home at right-down, food at left-up */

    404.   randnum = random(100);
    405.   if(randnum<25) homeplace = 1;
    406.   else if (randnum>=25&&randnum<50) homeplace = 2;
    407.   else if (randnum>=50&&randnum<75) homeplace = 3;
    408.   else homeplace = 4;

    409.   switch(homeplace)
    410.   {
    411.     case 1: home.xxx = random(MAXX/3)+1;
    412.         home.yyy = random(MAXY/3)+1;
    413.         food.xxx = random(MAXX/3)+2*MAXX/3+1;
    414.         food.yyy = random(MAXY/3)+2*MAXY/3+1;
    415.         break;
    416.     case 2: home.xxx = random(MAXX/3)+1;
    417.         home.yyy = random(MAXY/3)+2*MAXY/3+1;
    418.         food.xxx = random(MAXX/3)+2*MAXX/3+1;
    419.         food.yyy = random(MAXY/3)+1;
    420.         break;
    421.     case 3: home.xxx = random(MAXX/3)+2*MAXX/3+1;
    422.         home.yyy = random(MAXY/3)+1;
    423.         food.xxx = random(MAXX/3)+1;
    424.         food.yyy = random(MAXY/3)+2*MAXY/3+1;
    425.         break;
    426.     case 4: home.xxx = random(MAXX/3)+2*MAXX/3+1;
    427.         home.yyy = random(MAXY/3)+2*MAXY/3+1;
    428.         food.xxx = random(MAXX/3)+1;
    429.         food.yyy = random(MAXY/3)+1;
    430.         break;
    431.   }

    432.   food.amount = random(MAX_FOOD/3)+2*MAX_FOOD/3+1;
    433.   /* food.amount = MAX_FOOD; */
    434.   home.amount = 0;
    435.   home.TargetFood = (food.amount<TARGET_FOOD)?food.amount:TARGET_FOOD;

    436.   /* data correctness check */
    437.   if(home.xxx<=0||home.xxx>MAXX||home.yyy<=0||home.yyy>MAXY||
    438.      food.xxx<=0||food.xxx>MAXX||food.yyy<=0||food.yyy>MAXY||
    439.      food.amount<=0)
    440.   {
    441.     gotoxy(1,MAXY+1);
    442.     printf("World initial fail, any key to exit...");
    443.     getch();
    444.     exit(2);
    445.   }

    446.   gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
    447.   gotoxy(food.xxx,food.yyy); putch(FOOD_CHAR);
    448. }
    449. void AntInitial(void)
    450. /* initial ant[AntNow] */
    451. {
    452.   int randnum;
    453.   int i;

    454.   ant[AntNow].xxx = home.xxx;
    455.   ant[AntNow].yyy = home.yyy;

    456.   randnum = random(100);
    457.   if(randnum<25) ant[AntNow].dir = UP;
    458.   else if (randnum>=25&&randnum<50) ant[AntNow].dir = DOWN;
    459.   else if (randnum>=50&&randnum<75) ant[AntNow].dir = LEFT;
    460.   else ant[AntNow].dir = RIGHT;

    461.   ant[AntNow].speed = 2*(random(INI_SPEED/2)+1);
    462.   ant[AntNow].SpeedTimer = 0;
    463.   ant[AntNow].food = 0;
    464.   ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = 0;
    465.   ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = MAX_SMELL;
    466.   ant[AntNow].IQ = 1;

    467.   for(i=0;i<TRACE_REMEMBER;i++)
    468.   {
    469.     ant[AntNow].tracex[i] = 0;
    470.     ant[AntNow].tracey[i] = 0;
    471.   }
    472.   ant[AntNow].TracePtr = 0;

    473.   /* a sepecail ant */
    474.   if(AntNow==0) ant[AntNow].speed = INI_SPEED;
    475. }

    476. void WorldChange(void)
    477. {
    478.   int k,i,j;
    479.   int smelldisp;

    480.   SmellGoneTimer+=timer10ms;
    481.   if(SmellGoneTimer>=SMELL_GONE_SPEED)
    482.   {
    483.     SmellGoneTimer = 0;
    484.     for(k=0;k<=1;k++)
    485.     /* SMELL TYPE FOOD and HOME */
    486.       for(i=1;i<=MAXX;i++)
    487.         for(j=1;j<=MAXY;j++)
    488.         {
    489.             if(Smell[k][i][j])
    490.           {
    491.               smelldisp = 1+((10*Smell[k][i][j])/(MAX_SMELL*SMELL_DROP_RATE));
    492.               if(smelldisp>=30000||smelldisp<0) smelldisp = 30000;
    493.               if(SmellDispFlag)
    494.            {
    495.                 gotoxy(i,j);
    496.                 if((i==food.xxx&&j==food.yyy)||(i==home.xxx&&j==home.yyy))
    497.                   /* don't over write Food and Home */;
    498.               else
    499.              {
    500.                   if(smelldisp>9) putch('#');
    501.                   else putch(smelldisp+'0');
    502.              }
    503.            }
    504.               Smell[k][i][j]-= 1+(Smell[k][i][j]*SMELL_GONE_RATE);
    505.               if(Smell[k][i][j]<0) Smell[k][i][j] = 0;
    506.               if(SmellDispFlag)
    507.            {
    508.                 if(Smell[k][i][j]<=2)
    509.              {
    510.                   gotoxy(i,j);
    511.                   putch(SPACE);
    512.              }
    513.            }
    514.           }
    515.           } /* of one location */
    516.   } /* of time to change the world */
    517. } /* of world change */

    518. void AntMove(void)
    519. {
    520.   int antx,anty;
    521.   int smelltodrop,smellnow;

    522.   for(AntNow=0;AntNow<MAX_ANT;AntNow++)
    523.   {
    524.     ant[AntNow].SpeedTimer+=timer10ms;
    525.     if(ant[AntNow].SpeedTimer>=ant[AntNow].speed)
    526.     {
    527.       ant[AntNow].SpeedTimer = 0;
    528.       gotoxy(ant[AntNow].xxx,ant[AntNow].yyy);
    529.       putch(SPACE);
    530.       AntOneStep();
    531.       gotoxy(ant[AntNow].xxx,ant[AntNow].yyy);
    532.       /* ant0 is a sepecail ant, use different color */
    533.       if(AntNow==0) textcolor(0xd);
    534.       if(ant[AntNow].food) putch(ANT_CHAR_FOOD);
    535.       else putch(ANT_CHAR_EMPTY);
    536.       if(AntNow==0) textcolor(0x7);

    537.       /* remember trace */
    538.       ant[AntNow].tracex[ant[AntNow].TracePtr] = ant[AntNow].xxx;
    539.       ant[AntNow].tracey[ant[AntNow].TracePtr] = ant[AntNow].yyy;
    540.       if(++(ant[AntNow].TracePtr)>=TRACE_REMEMBER) ant[AntNow].TracePtr = 0;

    541.       /* drop smell */
    542.       antx = ant[AntNow].xxx;
    543.       anty = ant[AntNow].yyy;

    544.       if(ant[AntNow].food)
    545.       /* have food, looking for home */
    546.      {
    547.         if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD])
    548.        {
    549.           smellnow = Smell[SMELL_TYPE_FOOD][antx][anty];
    550.           smelltodrop = ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]*SMELL_DROP_RATE;
    551.           if(smelltodrop>smellnow) Smell[SMELL_TYPE_FOOD][antx][anty] = smelltodrop;
    552.           /* else Smell[...] = smellnow */
    553.           ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]-= smelltodrop;
    554.           if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]<0) ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = 0;
    555.         } /* of have smell to drop */
    556.       } /* of have food */
    557.       else
    558.       /* no food, looking for food */
    559.      {
    560.         if(ant[AntNow].SmellAmount[SMELL_TYPE_HOME])
    561.        {
    562.           smellnow = Smell[SMELL_TYPE_HOME][antx][anty];
    563.           smelltodrop = ant[AntNow].SmellAmount[SMELL_TYPE_HOME]*SMELL_DROP_RATE;
    564.           if(smelltodrop>smellnow) Smell[SMELL_TYPE_HOME][antx][anty] = smelltodrop;
    565.           /* else Smell[...] = smellnow */
    566.           ant[AntNow].SmellAmount[SMELL_TYPE_HOME]-= smelltodrop;
    567.           if(ant[AntNow].SmellAmount[SMELL_TYPE_HOME]<0) ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = 0;
    568.         } /* of have smell to drop */
    569.      }
    570.     } /* of time to go */
    571.     /* else not go */
    572.   } /* of for AntNow */

    573.   textcolor(FOOD_HOME_COLOR);
    574.   gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
    575.   gotoxy(food.xxx,food.yyy);
    576.   if(food.amount>0) putch(FOOD_CHAR);
    577.   else putch(FOOD_CHAR2);
    578.   textcolor(7);

    579.   gotoxy(1,MAXY+1);
    580.   printf("Food %d, Home %d   ",food.amount,home.amount);
    581. }

    582. void AntOneStep(void)
    583. {
    584.   int ddir,tttx,ttty;
    585.   int i;

    586.   ddir = ant[AntNow].dir;
    587.   tttx = ant[AntNow].xxx;
    588.   ttty = ant[AntNow].yyy;

    589.   ddir = AntNextDir(tttx,ttty,ddir);

    590.   switch(ddir)
    591.   {
    592.     case UP:  ttty--;
    593.           break;
    594.     case DOWN:  ttty++;
    595.           break;
    596.     case LEFT:  tttx--;
    597.           break;
    598.     case RIGHT: tttx++;
    599.           break;
    600.     default:  break;
    601.   } /* of switch dir */

    602.   ant[AntNow].dir = ddir;
    603.   ant[AntNow].xxx = tttx;
    604.   ant[AntNow].yyy = ttty;

    605.   if(ant[AntNow].food)
    606.   /* this ant carry with food, search for home */
    607.   {
    608.     if(tttx==home.xxx&&ttty==home.yyy)
    609.     {
    610.       home.amount++;
    611.       AntInitial();
    612.     }
    613.     if(tttx==food.xxx&&ttty==food.yyy)
    614.       ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = MAX_SMELL;
    615.   } /* of search for home */
    616.   else
    617.   /* this ant is empty, search for food */
    618.   {
    619.     if(tttx==food.xxx&&ttty==food.yyy)
    620.     {
    621.       if(food.amount>0)
    622.      {
    623.         ant[AntNow].food = 1;
    624.         food.amount--;
    625.         ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = MAX_SMELL;
    626.         ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = 0;
    627.         ant[AntNow].dir = TurnBack(ant[AntNow].dir);
    628.         for(i=0;i<TRACE_REMEMBER;i++)
    629.        {
    630.           ant[AntNow].tracex[i] = 0;
    631.           ant[AntNow].tracey[i] = 0;
    632.        }
    633.         ant[AntNow].TracePtr = 0;
    634.         CanFindFood = 1;
    635.       } /* of still have food */
    636.     }
    637.     if(tttx==home.xxx&&ttty==home.yyy)
    638.       ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = MAX_SMELL;
    639.   }  /* of search for food */
    640. }

    641. void DealKey(char key)
    642. {
    643.   int i;
    644.   switch(key)
    645.   {
    646.     case 'p':   gettime(&endtime);
    647.           DispPlayTime();
    648.           getch();
    649.           gotoxy(1,MAXY+1);
    650.           for(i=1;i<=MAXX-1;i++) putch(SPACE);
    651.           break;
    652.     case 't':   if(SmellDispFlag)
    653.         {
    654.             SmellDispFlag=0;
    655.             ClearSmellDisp();
    656.         }
    657.           else SmellDispFlag = 1;
    658.           break;
    659.     case '1':   DispSmell(SMELL_TYPE_FOOD);
    660.           getch();
    661.           ClearSmellDisp();
    662.           break;
    663.     case '2':   DispSmell(SMELL_TYPE_HOME);
    664.           getch();
    665.           ClearSmellDisp();
    666.           break;
    667.     case '3':   DispSmell(2);
    668.           getch();
    669.           ClearSmellDisp();
    670.           break;
    671.     case 's':   SaveBlock();
    672.        break;
    673.     case 'l':   LoadBlock();
    674.        break;
    675.     default:  gotoxy(1,MAXY+1);
    676.           for(i=1;i<=MAXX-1;i++) putch(SPACE);
    677.   } /* of switch */
    678. }

    679. void ClearSmellDisp(void)
    680. {
    681.   int k,i,j;

    682.   for(k=0;k<=1;k++)
    683.   /* SMELL TYPE FOOD and HOME */
    684.     for(i=1;i<=MAXX;i++)
    685.       for(j=1;j<=MAXY;j++)
    686.        {
    687.           if(Smell[k][i][j])
    688.         {
    689.             gotoxy(i,j);
    690.             putch(SPACE);
    691.         }
    692.         } /* of one location */
    693. }

    694. void DispSmell(int type)
    695. /* input: 0 -- Only display food smell
    696.       1 -- Only display home smell
    697.       2 -- Display both food and home smell
    698. */
    699. {
    700.   int k,i,j;
    701.   int fromk,tok;
    702.   int smelldisp;

    703.   switch(type)
    704.   {
    705.     case 0: fromk = 0;
    706.         tok = 0;
    707.         break;
    708.     case 1: fromk = 1;
    709.         tok = 1;
    710.         break;
    711.     case 2: fromk = 0;
    712.         tok = 1;
    713.         break;
    714.     default:fromk = 0;
    715.         tok = 1;
    716.         break;
    717.   }
    718.   SmellGoneTimer = 0;
    719.   for(k=fromk;k<=tok;k++)
    720.   /* SMELL TYPE FOOD and HOME */
    721.     for(i=1;i<=MAXX;i++)
    722.       for(j=1;j<=MAXY;j++)
    723.        {
    724.           if(Smell[k][i][j])
    725.         {
    726.             smelldisp = 1+((10*Smell[k][i][j])/(MAX_SMELL*SMELL_DROP_RATE));
    727.             if(smelldisp>=30000||smelldisp<0) smelldisp = 30000;
    728.             gotoxy(i,j);
    729.             if(i!=food.xxx||j!=food.yyy)
    730.           {
    731.               if((i==food.xxx&&j==food.yyy)||(i==home.xxx&&j==home.yyy))
    732.                 /* don't over write Food and Home */;
    733.             else
    734.            {
    735.                 if(smelldisp>9) putch('#');
    736.                 else putch(smelldisp+'0');
    737.            }
    738.           }
    739.         }
    740.         } /* of one location */
    741. }

    742. int AntNextDir(int xxx,int yyy,int ddir)
    743. {
    744.   int randnum;
    745.   int testdir;
    746.   int CanGoState;
    747.   int cangof,cangol,cangor;
    748.   int msf,msl,msr,maxms;
    749.   int type;

    750.   CanGoState = CanGo(xxx,yyy,ddir);
    751.   if(CanGoState==0||CanGoState==2||CanGoState==3||CanGoState==6) cangof = 1;
    752.   else cangof = 0;
    753.   if(CanGoState==0||CanGoState==1||CanGoState==3||CanGoState==5) cangol = 1;
    754.   else cangol = 0;
    755.   if(CanGoState==0||CanGoState==1||CanGoState==2||CanGoState==4) cangor = 1;
    756.   else cangor = 0;

    757.   if(ant[AntNow].food) type = SMELL_TYPE_HOME;
    758.   else type = SMELL_TYPE_FOOD;

    759.   msf = GetMaxSmell(type,xxx,yyy,ddir);
    760.   msl = GetMaxSmell(type,xxx,yyy,TurnLeft(ddir));
    761.   msr= GetMaxSmell(type,xxx,yyy,TurnRight(ddir));
    762.   maxms = MaxLocation(msf,msl,msr);
    763.   /* maxms - 1 - msf is MAX
    764.          2 - msl is MAX
    765.          3 - msr is MAX
    766.          0 - all 3 number is 0 */

    767.   testdir = NULL;
    768.   switch(maxms)
    769.   {
    770.     case 0: /* all is 0, keep testdir = NULL, random select dir */
    771.         break;
    772.     case 1: if(cangof)
    773.           testdir = ddir;
    774.         else
    775.           if(msl>msr) if(cangol) testdir = TurnLeft(ddir);
    776.           else if(cangor) testdir = TurnRight(ddir);
    777.         break;
    778.     case 2: if(cangol)
    779.           testdir = TurnLeft(ddir);
    780.         else
    781.           if(msf>msr) if(cangof) testdir = ddir;
    782.           else if(cangor) testdir = TurnRight(ddir);
    783.         break;
    784.     case 3: if(cangor)
    785.           testdir = TurnRight(ddir);
    786.         else
    787.           if(msf>msl) if(cangof) testdir =ddir;
    788.           else if(cangol) testdir = TurnLeft(ddir);
    789.         break;
    790.     default:break;
    791.   } /* of maxms */

    792.   randnum = random(1000);
    793.   if(randnum<SMELL_DROP_RATE*1000||testdir==NULL)
    794.   /* 1. if testdir = NULL, means can not find the max smell or the dir to max smell can not go
    795.      then random select dir
    796.      2. if ant error, don't follow the smell, random select dir
    797.   */
    798.   {
    799.     randnum = random(100);
    800.     switch(CanGoState)
    801.     {
    802.       case 0: if(randnum<90) testdir = ddir;
    803.           else if (randnum>=90&&randnum<95) testdir = TurnLeft(ddir);
    804.           else testdir = TurnRight(ddir);
    805.           break;
    806.       case 1: if(randnum<50) testdir = TurnLeft(ddir);
    807.           else testdir = TurnRight(ddir);
    808.           break;
    809.       case 2: if(randnum<90) testdir = ddir;
    810.           else testdir = TurnRight(ddir);
    811.           break;
    812.       case 3: if(randnum<90) testdir = ddir;
    813.           else testdir = TurnLeft(ddir);
    814.           break;
    815.       case 4: testdir = TurnRight(ddir);
    816.           break;
    817.       case 5: testdir = TurnLeft(ddir);
    818.           break;
    819.       case 6: testdir = ddir;
    820.           break;
    821.       case 7: testdir = TurnBack(ddir);
    822.           break;
    823.       default:testdir = TurnBack(ddir);
    824.     } /* of can go state */
    825.   }
    826.   return(testdir);
    827. }
    828. int GetMaxSmell(int type,int xxx,int yyy,int ddir)
    829. {
    830.   int i,j;
    831.   int ms; /* MAX smell */

    832.   ms = 0;
    833.   switch(ddir)
    834.   {
    835.     case UP:  for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
    836.             for(j=yyy-ANT_EYESHOT;j<yyy;j++)
    837.           {
    838.               if(!JudgeCanGo(i,j)) continue;
    839.               if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
    840.                  (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
    841.            {
    842.                 ms = MAX_SMELL;
    843.                break;
    844.            }
    845.               if(IsTrace(i,j)) continue;
    846.               if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
    847.           }
    848.           break;
    849.     case DOWN:  for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
    850.             for(j=yyy+1;j<=yyy+ANT_EYESHOT;j++)
    851.           {
    852.               if(!JudgeCanGo(i,j)) continue;
    853.               if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
    854.                  (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
    855.            {
    856.                 ms = MAX_SMELL;
    857.                break;
    858.            }
    859.               if(IsTrace(i,j)) continue;
    860.               if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
    861.           }
    862.           break;
    863.     case LEFT:  for(i=xxx-ANT_EYESHOT;i<xxx;i++)
    864.             for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
    865.           {
    866.               if(!JudgeCanGo(i,j)) continue;
    867.               if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
    868.                  (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
    869.            {
    870.                 ms = MAX_SMELL;
    871.                break;
    872.            }
    873.               if(IsTrace(i,j)) continue;
    874.               if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
    875.           }
    876.           break;
    877.     case RIGHT: for(i=xxx+1;i<=xxx+ANT_EYESHOT;i++)
    878.             for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
    879.           {
    880.               if(!JudgeCanGo(i,j)) continue;
    881.               if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
    882.                  (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
    883.            {
    884.                 ms = MAX_SMELL;
    885.                break;
    886.            }
    887.               if(IsTrace(i,j)) continue;
    888.               if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
    889.           }
    890.           break;
    891.     default:  break;
    892.   }
    893.   return(ms);
    894. }

    895. int IsTrace(int xxx,int yyy)
    896. {
    897.   int i;

    898.   for(i=0;i<TRACE_REMEMBER;i++)
    899.     if(ant[AntNow].tracex[i]==xxx&&ant[AntNow].tracey[i]==yyy) return(1);
    900.   return(0);
    901. }

    902. int MaxLocation(int num1,int num2,int num3)
    903. {
    904.   int maxnum;

    905.   if(num1==0&&num2==0&&num3==0) return(0);

    906.   maxnum = num1;
    907.   if(num2>maxnum) maxnum = num2;
    908.   if(num3>maxnum) maxnum = num3;

    909.   if(maxnum==num1) return(1);
    910.   if(maxnum==num2) return(2);
    911.   if(maxnum==num3) return(3);
    912. }

    913. int CanGo(int xxx,int yyy,int ddir)
    914. /* input: xxx,yyy - location of ant
    915.       ddir - now dir
    916.    output: 0 - forward and left and right can go
    917.        1 - forward can not go
    918.        2 - left can not go
    919.        3 - right can not go
    920.        4 - forward and left can not go
    921.        5 - forward and right can not go
    922.        6 - left and right can not go
    923.        7 - forward and left and right all can not go
    924. */
    925. {
    926.   int tx,ty,tdir;
    927.   int okf,okl,okr;

    928.   /* forward can go ? */
    929.   tdir = ddir;
    930.   tx = xxx;
    931.   ty = yyy;
    932.   switch(tdir)
    933.   {
    934.     case UP:  ty--;
    935.           break;
    936.     case DOWN:  ty++;
    937.           break;
    938.     case LEFT:  tx--;
    939.           break;
    940.     case RIGHT: tx++;
    941.           break;
    942.     default:  break;
    943.   } /* of switch dir */
    944.   if(JudgeCanGo(tx,ty)) okf = 1;
    945.   else okf = 0;

    946.   /* turn left can go ? */
    947.   tdir = TurnLeft(ddir);
    948.   tx = xxx;
    949.   ty = yyy;
    950.   switch(tdir)
    951.   {
    952.     case UP:  ty--;
    953.           break;
    954.     case DOWN:  ty++;
    955.           break;
    956.     case LEFT:  tx--;
    957.           break;
    958.     case RIGHT: tx++;
    959.           break;
    960.     default:  break;
    961.   } /* of switch dir */
    962.   if(JudgeCanGo(tx,ty)) okl = 1;
    963.   else okl = 0;

    964.   /* turn right can go ? */
    965.   tdir = TurnRight(ddir);
    966.   tx = xxx;
    967.   ty = yyy;
    968.   switch(tdir)
    969.   {
    970.     case UP:  ty--;
    971.           break;
    972.     case DOWN:  ty++;
    973.           break;
    974.     case LEFT:  tx--;
    975.           break;
    976.     case RIGHT: tx++;
    977.           break;
    978.     default:  break;
    979.   } /* of switch dir */
    980.   if(JudgeCanGo(tx,ty)) okr = 1;
    981.   else okr = 0;

    982.   if(okf&&okl&&okr) return(0);
    983.   if(!okf&&okl&&okr) return(1);
    984.   if(okf&&!okl&&okr) return(2);
    985.   if(okf&&okl&&!okr) return(3);
    986.   if(!okf&&!okl&&okr) return(4);
    987.   if(!okf&&okl&&!okr) return(5);
    988.   if(okf&&!okl&&!okr) return(6);
    989.   if(!okf&&!okl&&!okr) return(7);
    990.   return(7);
    991. }

    992. int JudgeCanGo(int xxx,int yyy)
    993. /* input: location to judeg
    994.    output: 0 -- can not go
    995.        1 -- can go
    996. */
    997. {
    998.   int i,j;

    999.   if(xxx<=0||xxx>MAXX) return(0);
    1000.   if(yyy<=0||yyy>MAXY) return(0);
    1001.   if(block[xxx][yyy]) return(0);
    1002.   return(1);
    1003. }

    1004. int TurnLeft(int ddir)
    1005. {
    1006.   switch(ddir)
    1007.   {
    1008.     case UP:  return(LEFT);
    1009.     case DOWN:  return(RIGHT);
    1010.     case LEFT:  return(DOWN);
    1011.     case RIGHT: return(UP);
    1012.     default:  break;
    1013.   } /* of switch dir */
    1014. }

    1015. int TurnRight(int ddir)
    1016. {
    1017.   switch(ddir)
    1018.   {
    1019.     case UP:  return(RIGHT);
    1020.     case DOWN:  return(LEFT);
    1021.     case LEFT:  return(UP);
    1022.     case RIGHT: return(DOWN);
    1023.     default:  break;
    1024.   } /* of switch dir */
    1025. }

    1026. int TurnBack(int ddir)
    1027. {
    1028.   switch(ddir)
    1029.   {
    1030.     case UP:  return(DOWN);
    1031.     case DOWN:  return(UP);
    1032.     case LEFT:  return(RIGHT);
    1033.     case RIGHT: return(LEFT);
    1034.     default:  break;
    1035.   } /* of switch dir */
    1036. }
    复制代码


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52620
    钢镚
    1422

    开源英雄守望者

     楼主| 发表于 2015-2-6 10:40:22 | 显示全部楼层
    本帖最后由 破晓 于 2015-2-6 10:42 编辑

    AS3  实现



    1. /**
    2. * Copyright tencho ( http://wonderfl.net/user/tencho )
    3. * MIT License ( http://www.opensource.org/licenses/mit-license.php )
    4. * Downloaded from: http://wonderfl.net/c/2Yzr
    5. */

    6. /**
    7. * アリのエサ運びをシミュレーションしてみました
    8. * 土をクリックするとお菓子が置けて、左下のボタンでアリが増やせます(1000匹まで)
    9. * ※増やしすぎ注意(負荷的にも見た目的にも・・・)
    10. *
    11. * アリは普段ランダムに動いていますが、
    12. * エサを見つけると地面にフェロモンを残しながら巣まで戻り、
    13. * そのフェロモンを発見した他のアリがそれを辿ってエサの在り処を見つけるという流れです。
    14. * フェロモン濃度だけで判断するとなかなかうまく辿れなかったので
    15. * 濃度と一緒に進む方向も記録するようにしています。
    16. * (濃度と向きはBitmapDataのピクセルに色情報としてまとめて記録してます)
    17. * フェロモンが蒸発して消えないかぎり複雑な道のりも辿れるんですが、
    18. * 複雑すぎると巣に帰るプログラムかけなくなりそうだったのでやめました。。。
    19. */
    20. package {
    21.         import com.bit101.components.Label;
    22.         import com.bit101.components.PushButton;
    23.         import flash.display.Bitmap;
    24.         import flash.display.BitmapData;
    25.         import flash.display.BlendMode;
    26.         import flash.display.Sprite;
    27.         import flash.display.StageQuality;
    28.         import flash.events.Event;
    29.         import flash.events.MouseEvent;
    30.         import flash.filters.DropShadowFilter;
    31.         import flash.geom.ColorTransform;
    32.         import flash.geom.Rectangle;
    33.         public class AntsTest extends Sprite {
    34.                 ///画面サイズ
    35.                 public const DISPLAY:Rectangle = new Rectangle(0, 0, 465, 465);
    36.                 ///最初からいるアリの数
    37.                 public const ANTSNUM:int = 50;
    38.                 ///一度に追加するアリの数
    39.                 public const ADDNUM:int = 50;
    40.                 ///アリの最大数
    41.                 public const ANTSMAX:int = 1000;
    42.                 public const MATERIAL_URL:String = "http://assets.wonderfl.net/images/related_images/4/40/40c1/40c16871d560d547ebb1fc1725db47922ef9f261";
    43.                 public var world:World;
    44.                 public var bg:Sprite;
    45.                 public var containerAnts:Sprite;
    46.                 public var containerFoods:Sprite;
    47.                 public var canvasBmp:Bitmap;
    48.                 public var pheromoneBmp:Bitmap;
    49.                 public var canvas:BitmapData;
    50.                 public var loader:ImageLoader;
    51.                 public var stats:Label;
    52.                 public var whiteColor:ColorTransform = new ColorTransform();
    53.                 ///コンストラクタ
    54.                 public function AntsTest() {
    55.                         stage.frameRate = 30;
    56.                         stage.quality = StageQuality.MEDIUM;
    57.                         //Wonderfl.capture_delay(5);
    58.                         whiteColor.color = 0xFFFFFF;
    59.                         
    60.                         world = new World();
    61.                         world.home.setPosition(110, 350);
    62.                         world.obstacles.push(new Obstacle(280, 225, 90));
    63.                         world.obstacles.push(new Obstacle(0, -150, 280));
    64.                         world.init(DISPLAY.width, DISPLAY.height);
    65.                         
    66.                         bg = new Sprite();
    67.                         bg.graphics.beginFill(0x444444, 1);
    68.                         bg.graphics.drawRect(0, 0, DISPLAY.width, DISPLAY.height);
    69.                         bg.graphics.endFill();
    70.                         
    71.                         addChild(bg);
    72.                         
    73.                         //外部画像読み込み開始
    74.                         loader = new ImageLoader();
    75.                         loader.load(MATERIAL_URL, onLoadImage, onErrorImage);
    76.                 }
    77.                 ///画像読み込み失敗
    78.                 private function onErrorImage(str:String):void {
    79.                         var msg:Label = new Label(this, 5, 5, str);
    80.                         msg.textField.wordWrap = true;
    81.                         msg.textField.width = 440;
    82.                         msg.transform.colorTransform = whiteColor;
    83.                 }
    84.                 ///画像読み込み完了
    85.                 private function onLoadImage():void {
    86.                         loader.ground.width = DISPLAY.width;
    87.                         loader.ground.height = DISPLAY.height;
    88.                         
    89.                         pheromoneBmp = new Bitmap(world.pheromone.map);
    90.                         pheromoneBmp.visible = false;
    91.                         pheromoneBmp.blendMode = BlendMode.LIGHTEN;
    92.                         
    93.                         //障害物領域をマウスクリックできなくさせる
    94.                         for each(var obs:Obstacle in world.obstacles) {
    95.                                 var sp:Sprite = addChild(new Sprite()) as Sprite;
    96.                                 sp.graphics.beginFill(0x444444, 0);
    97.                                 sp.graphics.drawCircle(obs.center.x, obs.center.y, obs.radius);
    98.                                 sp.graphics.endFill();
    99.                         }
    100.                         canvas = new BitmapData(DISPLAY.width, DISPLAY.height, true, 0x00FFFFFF);
    101.                         canvasBmp = new Bitmap(canvas);
    102.                         canvasBmp.filters = [new DropShadowFilter(3, 45, 0x222222, 0.8, 3, 3, 1, 1)];
    103.                         containerAnts = new Sprite();
    104.                         containerFoods = new Sprite();
    105.                         containerFoods.mouseChildren = false;
    106.                         containerFoods.mouseEnabled = false;
    107.                         
    108.                         //画面下のメニュー
    109.                         var menu:Sprite = new Sprite();
    110.                         var blackBox:Sprite = menu.addChild(new Sprite()) as Sprite;
    111.                         blackBox.graphics.beginFill(0x000000, 0.5);
    112.                         blackBox.graphics.drawRect(0, 0, DISPLAY.width, 25);
    113.                         blackBox.graphics.endFill();
    114.                         new SwitchButton(menu, DISPLAY.width - 160, 5, ["PHEROMONE: OFF", "PHEROMONE: ON"], onSwitchPheromon);
    115.                         new PushButton(menu, DISPLAY.width - 55, 5, "RESET", onClickClear).setSize(50, 16);
    116.                         new PushButton(menu, 5, 5, "+" + ADDNUM + " ANTS", onClickAdd).setSize(70, 16);
    117.                         stats = new Label(menu, 85, 3, "");
    118.                         stats.transform.colorTransform = whiteColor;
    119.                         menu.y = DISPLAY.height - menu.height;
    120.                         
    121.                         //画面に色々配置
    122.                         addChild(loader.ground);
    123.                         addChild(pheromoneBmp);
    124.                         addChild(world.home);
    125.                         addChild(canvasBmp);
    126.                         addChild(containerFoods);
    127.                         addChild(menu);
    128.                         new Label(this, 5, 3, "CLICK TO FEED").transform.colorTransform = whiteColor;
    129.                         
    130.                         //メイン処理開始
    131.                         init(ANTSNUM);
    132.                         bg.addEventListener(MouseEvent.MOUSE_DOWN, onClickStage);
    133.                         addEventListener(Event.ENTER_FRAME, onEnter);
    134.                 }
    135.                 ///蟻の数を指定して初期化
    136.                 private function init(num:int):void {
    137.                         world.clear();
    138.                         var wait:Number = 10;
    139.                         for (var i:int = 0; i < num; i++) {
    140.                                 wait += Math.max(0.05, 15 / (i * i / 100 + 1));
    141.                                 containerAnts.addChild(world.addAnt(wait).body);
    142.                         }
    143.                         updateStats();
    144.                 }
    145.                 ///情報更新
    146.                 private function updateStats():void {
    147.                         stats.text = "ANTS: " + world.ants.length;
    148.                 }
    149.                 ///全てリセット
    150.                 private function onClickClear(e:MouseEvent):void{
    151.                         init(ANTSNUM);
    152.                 }
    153.                 ///アリ追加
    154.                 private function onClickAdd(e:MouseEvent):void {
    155.                         var num:int = Math.min(ADDNUM, ANTSMAX - world.ants.length);
    156.                         for (var i:int = 0; i < num; i++)
    157.                                 containerAnts.addChild(world.addAnt(i/2).body);
    158.                         updateStats();
    159.                 }
    160.                 ///フェロモン切り替え
    161.                 private function onSwitchPheromon(mode:int):void{
    162.                         pheromoneBmp.visible = !!mode;
    163.                 }
    164.                 ///土をクリック
    165.                 private function onClickStage(e:MouseEvent):void {
    166.                         var img:ImageData = loader.feeds[Math.random() * loader.feeds.length | 0];
    167.                         containerFoods.addChild(world.addFood(stage.mouseX, stage.mouseY, img).sprite);
    168.                 }
    169.                 //毎フレーム処理
    170.                 private function onEnter(e:Event):void {
    171.                         world.pheromone.map.lock();
    172.                         for each(var a:Ant in world.ants) a.action(world);
    173.                         world.pheromone.vaporize();
    174.                         world.pheromone.map.unlock();
    175.                         canvas.fillRect(DISPLAY, 0x00000000);
    176.                         canvas.draw(containerAnts);
    177.                 }
    178.         }
    179. }
    180. import com.bit101.components.PushButton;
    181. import flash.display.Bitmap;
    182. import flash.display.BitmapData;
    183. import flash.display.BlendMode;
    184. import flash.display.DisplayObjectContainer;
    185. import flash.display.Loader;
    186. import flash.display.Sprite;
    187. import flash.events.ErrorEvent;
    188. import flash.events.Event;
    189. import flash.events.IOErrorEvent;
    190. import flash.events.MouseEvent;
    191. import flash.events.SecurityErrorEvent;
    192. import flash.filters.DropShadowFilter;
    193. import flash.geom.ColorTransform;
    194. import flash.geom.Point;
    195. import flash.geom.Rectangle;
    196. import flash.net.URLRequest;
    197. import flash.system.LoaderContext;
    198. import flash.system.System;
    199. //角度変換用
    200. class Angle {
    201.         static public const ALL_RADIAN:Number = Math.PI * 2;
    202.         static public const TO_RADIAN:Number = Math.PI / 180;
    203.         static public const TO_ROTATION:Number = 180 / Math.PI;
    204.         //角度を合成する
    205.         static public function between(a1:Number, a2:Number, per:Number):Number {
    206.                 var minus:Number = a1 - a2;
    207.                 var r180:Number = (minus % Angle.ALL_RADIAN + Angle.ALL_RADIAN) % Angle.ALL_RADIAN;
    208.                 if (r180 > Math.PI) r180 -= Angle.ALL_RADIAN;
    209.                 var a0:Number = r180 + a2;
    210.                 return a0 * (1 - per) + a2 * (per);
    211.         }
    212. }
    213. /**
    214. * 全てのデータ
    215. */
    216. class World {
    217.         ///ワールドサイズ
    218.         public var area:Rectangle;
    219.         ///全てのアリ
    220.         public var ants:Vector.<Ant> = new Vector.<Ant>();
    221.         ///全てのエサ
    222.         public var foods:Vector.<Food> = new Vector.<Food>();
    223.         ///全ての障害物
    224.         public var obstacles:Vector.<Obstacle> = new Vector.<Obstacle>();
    225.         ///アリ塚
    226.         public var home:AntsHill = new AntsHill();
    227.         ///フェロモン
    228.         public var pheromone:Pheromone;
    229.         public function World() {
    230.         }
    231.         ///サイズを指定して初期化
    232.         public function init(width:Number, height:Number):void {
    233.                 area = new Rectangle(0, 0, width, height);
    234.                 pheromone = new Pheromone(width, height);
    235.         }
    236.         ///エサを削って無くなったら削除
    237.         public function cutFood(f:Food):void {
    238.                 if (f.cut()) {
    239.                         f.remove();
    240.                         foods.splice(foods.indexOf(f), 1);
    241.                 }
    242.         }
    243.         ///エサを追加
    244.         public function addFood(x:int, y:int, img:ImageData):Food {
    245.                 var f:Food = new Food(x, y, 50, 200, img);
    246.                 foods.push(f);
    247.                 return f;
    248.         }
    249.         ///アリを追加
    250.         public function addAnt(wait:int):Ant {
    251.                 var a:Ant = new Ant(home.position.x, home.position.y);
    252.                 a.thinkTime = wait;
    253.                 ants.push(a);
    254.                 return a;
    255.         }
    256.         ///色々リセット
    257.         public function clear():void {
    258.                 for each(var f:Food in foods) f.remove();
    259.                 for each(var a:Ant in ants) a.remove();
    260.                 ants.length = 0;
    261.                 foods.length = 0;
    262.                 pheromone.clear();
    263.                 System.gc();
    264.         }
    265. }
    266. /**
    267. * エサの画像
    268. */
    269. class ImageData {
    270.         private var _colors:Array;
    271.         public var bmd:BitmapData;
    272.         public function ImageData(bmd:BitmapData) {
    273.                 var px:int, py:int, rgba:uint;
    274.                 this.bmd = bmd;
    275.                 //画像のピクセルカラーを調べる(透明領域は無視)
    276.                 _colors = new Array();
    277.                 for (px = 0; px < bmd.width; px += 8) {
    278.                         for (py = 0; py < bmd.width; py += 8) {
    279.                                 rgba = bmd.getPixel32(px, py);
    280.                                 if (rgba >>> 24 == 255) _colors.push(rgba & 0xFFFFFF);
    281.                         }
    282.                 }
    283.         }
    284.         ///画像の色をランダムに取得
    285.         public function getRandomColor():uint {
    286.                 return _colors[Math.random() * _colors.length | 0];
    287.         }
    288. }
    289. /**
    290. * 画像をロードして分割
    291. */
    292. class ImageLoader {
    293.         ///背景画像
    294.         public var ground:Bitmap;
    295.         ///エサ画像リスト
    296.         public var feeds:Vector.<ImageData>;
    297.         ///エサ画像の数
    298.         private const FEED_NUM:int = 5;
    299.         private var _loader:Loader;
    300.         private var _completeFunc:Function;
    301.         private var _errorFunc:Function;
    302.         public function ImageLoader() {
    303.                 _loader = new Loader();
    304.         }
    305.         public function load(src:String, complete:Function, error:Function):void{
    306.                 _completeFunc = complete;
    307.                 _errorFunc = error;
    308.                 _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorImage);
    309.                 _loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onErrorImage);
    310.                 _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadImage);
    311.                 _loader.load(new URLRequest(src), new LoaderContext(true));
    312.         }
    313.         private function removeEvent():void {
    314.                 _loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErrorImage);
    315.                 _loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onErrorImage);
    316.                 _loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadImage);
    317.         }
    318.         private function onErrorImage(e:ErrorEvent):void {
    319.                 removeEvent();
    320.                 _errorFunc(e.text);
    321.         }
    322.         private function onLoadImage(e:Event):void {
    323.                 removeEvent();
    324.                 var bmp:BitmapData = Bitmap(_loader.content).bitmapData;
    325.                 feeds = new Vector.<ImageData>();
    326.                 for (var i:int = 0; i < FEED_NUM; i++) {
    327.                         var bmp2:BitmapData = new BitmapData(64, 64, true, 0x00FFFFFF);
    328.                         bmp2.copyPixels(bmp, new Rectangle(64 * i, 0, 64, 64), new Point(0, 0));
    329.                         feeds.push(new ImageData(bmp2));
    330.                 }
    331.                 ground = new Bitmap(new BitmapData(bmp.width, bmp.height-64, false));
    332.                 ground.bitmapData.copyPixels(bmp, new Rectangle(0, 64, ground.width, ground.height), new Point());
    333.                 ground.smoothing = true;
    334.                 _completeFunc();
    335.         }
    336. }
    337. /**
    338. * 切り替えボタン
    339. */
    340. class SwitchButton extends PushButton {
    341.         private var _mode:int = 0;
    342.         private var _labels:Array;
    343.         private var _clickFunc:Function;
    344.         public function SwitchButton(parent:DisplayObjectContainer, xpos:Number, ypos:Number, labels:Array = null, func:Function = null) {
    345.                 if(labels == null) labels = [""];
    346.                 _labels = labels;
    347.                 _clickFunc = func;
    348.                 super(parent, xpos, ypos, _labels[0], onClick);
    349.                 height = 16;
    350.         }
    351.         private function onClick(e:MouseEvent):void {
    352.                 _mode = ++_mode % _labels.length;
    353.                 label = _labels[_mode];
    354.                 if (_clickFunc != null) _clickFunc(_mode);
    355.         }
    356. }
    357. /**
    358. * アリ塚
    359. */
    360. class AntsHill extends Sprite {
    361.         public var position:Point;
    362.         public function AntsHill() {
    363.                 graphics.beginFill(0x000000, 0.3);
    364.                 graphics.drawCircle(0, 0, 9);
    365.                 graphics.beginFill(0x000000, 1);
    366.                 graphics.drawCircle(0, 0, 7);
    367.                 graphics.endFill();
    368.                 position = new Point();
    369.         }
    370.         ///位置変更
    371.         public function setPosition(x:Number, y:Number):void {
    372.                 this.x = position.x = x;
    373.                 this.y = position.y = y;
    374.         }
    375. }
    376. //フェロモン
    377. class Pheromone {
    378.         public var map:BitmapData;
    379.         private var _ct:ColorTransform;
    380.         private var _timeCount:int = 0;
    381.         public function Pheromone(width:Number, height:Number) {
    382.                 _ct = new ColorTransform(1, 0.99, 1, 1, 0, 0, 0, 0);
    383.                 map = new BitmapData(width, height, true, 0x00000000);
    384.         }
    385.         ///指定座標のフェロモンから進む角度を調べる
    386.         public function getGuidepost(x:int, y:int):Number {
    387.                 var isNone:Boolean = true, tx:Number = 0, ty:Number = 0, px:int, py:int;
    388.                 //周囲のフェロモン濃度を調べて濃い方向を調べる
    389.                 for (px = -2; px <= 2; px++) {
    390.                         for (py = -2; py <= 2; py++) {
    391.                                 if (px != 0 || py != 0) {
    392.                                         var per:Number = (map.getPixel32(x + px * 4, y + py * 4) >> 8 & 0xFF) / 255;
    393.                                         tx += per * px;
    394.                                         ty += per * py;
    395.                                         if (per) isNone = false;
    396.                                 }
    397.                         }
    398.                 }
    399.                 var angle:Number;
    400.                 if ((!tx && !ty) || isNone) {
    401.                         angle = NaN;
    402.                 } else {
    403.                         //足元のフェロモンから進む角度を調べる
    404.                         var angleRate:Number = (map.getPixel32(x, y) & 0xFF) / 255;
    405.                         var dx:Number, dy:Number;
    406.                         if (angleRate == 0) {
    407.                                 //フェロモンが無ければ濃い方へ
    408.                                 dx = tx;
    409.                                 dy = ty;
    410.                         } else {
    411.                                 //フェロモンがあれば先に進む(周囲濃度で若干角度補正)
    412.                                 var radian:Number = angleRate * Angle.ALL_RADIAN + Math.PI;
    413.                                 dx = Math.cos(radian) * 40 + tx;
    414.                                 dy = Math.sin(radian) * 40 + ty;
    415.                         }
    416.                         angle = Math.atan2(dy, dx);
    417.                 }
    418.                 return angle;
    419.         }
    420.         //フェロモンをつける
    421.         public function putPheromone(x:int, y:int, radianPer:Number):void {
    422.                 var rgb:uint = 0xF0 << 24 | 0xFF << 16 | 0xFF << 8 | uint(0xFF * radianPer);
    423.                 map.fillRect(new Rectangle(x-5, y-5, 11, 11), rgb);
    424.         }
    425.         //フェロモン拡散
    426.         public function vaporize():void {
    427.                 if (!(++_timeCount % 2)) map.colorTransform(map.rect, _ct);
    428.         }
    429.         //フェロモンリセット
    430.         public function clear():void {
    431.                 map.fillRect(map.rect, 0x00000000);
    432.         }
    433. }
    434. /**
    435. * アリのエサ
    436. */
    437. class Food {
    438.         public var sprite:Sprite;
    439.         ///位置
    440.         public var position:Point;
    441.         ///半径
    442.         public var size:Number;
    443.         ///残りの量
    444.         public var quantity:int;
    445.         ///画像データ
    446.         public var image:ImageData;
    447.         private var _max:int;
    448.         private var _mask:BitmapData;
    449.         private var _noise:BitmapData;
    450.         public function Food(x:Number = 0, y:Number = 0, size:Number = 10, num:int = 10, img:ImageData = null) {
    451.                 position = new Point(x, y);
    452.                 this.size = size;
    453.                 quantity = num;
    454.                 _max = num;
    455.                 sprite = new Sprite();
    456.                 sprite.x = x;
    457.                 sprite.y = y;
    458.                 sprite.scaleX = sprite.scaleY = 0.7;
    459.                 sprite.filters = [new DropShadowFilter(5, 45, 0x111111, 0.7, 8, 8, 1, 1)];
    460.                 image = img;
    461.                 var foodBmp:Bitmap = sprite.addChild(new Bitmap(image.bmd)) as Bitmap;
    462.                 foodBmp.smoothing = true;
    463.                 //徐々に削られるエフェクト用
    464.                 _mask = new BitmapData(image.bmd.width, image.bmd.height, true);
    465.                 _mask.fillRect(_mask.rect, 0x00888888);
    466.                 _noise = new BitmapData(image.bmd.width, image.bmd.height, false);
    467.                 _noise.perlinNoise(20, 20, 3, int(Math.random() * 100), false, true, 1, true);
    468.                 var maskBmp:Bitmap = sprite.addChild(new Bitmap(_mask)) as Bitmap;
    469.                 maskBmp.blendMode = BlendMode.ERASE;
    470.                 foodBmp.x = maskBmp.x = -foodBmp.width / 2;
    471.                 foodBmp.y = maskBmp.y = -foodBmp.height / 2;
    472.         }
    473.         ///削る
    474.         public function cut():Boolean {
    475.                 quantity--;
    476.                 var per:Number = quantity / _max * 0.4 + 0.4;
    477.                 _mask.fillRect(_mask.rect, 0xFF888888);
    478.                 _mask.threshold(_noise, _noise.rect, new Point(), "<", per * 255, 0x00000000, 255, false);
    479.                 return !quantity;
    480.         }
    481.         ///削除
    482.         public function remove():void {
    483.                 _mask.dispose();
    484.                 _noise.dispose();
    485.                 if (sprite.parent) sprite.parent.removeChild(sprite);
    486.         }
    487. }
    488. /**
    489. * 円形障害物
    490. */
    491. class Obstacle {
    492.         ///中心点
    493.         public var center:Point;
    494.         ///半径
    495.         public var radius:Number;
    496.         public function Obstacle(x:Number = 0, y:Number = 0, radius:Number = 50) {
    497.                 center = new Point(x, y);
    498.                 this.radius = radius;
    499.         }
    500. }
    501. /**
    502. * アリ
    503. */
    504. class Ant {
    505.         ///グラフィック
    506.         public var body:Sprite;
    507.         ///位置
    508.         public var position:Point;
    509.         ///停止時間
    510.         public var thinkTime:int = 0;
    511.         private var _locus:Vector.<Point>;        //数フレーム前までの位置リスト
    512.         private var _radian:Number = 0;        //角度
    513.         private var _speed:Number = 2;        //速度
    514.         private var _status:int = 0;        //状況 0:エサ探し 1:巣に帰る
    515.         private var _freeTime:int = 0;        //フェロモン無効時間
    516.         private var _view:Number = 20;        //視界範囲
    517.         private var _wanderCnt:int = 1;
    518.         private var _food:Sprite;
    519.         private var _randomRad:Number;
    520.         private var _startReturn:Boolean = false;
    521.         private var _searchCnt:int = -1;
    522.         private var _targetFood:Food = null;
    523.         public function Ant(x:Number = 0, y:Number = 0) {
    524.                 body = new Sprite();
    525.                 body.graphics.beginFill(0x000000, 1);
    526.                 body.graphics.drawRect(-4, -1, 4, 2);
    527.                 body.graphics.drawRect(1, -1, 1, 2);
    528.                 body.graphics.drawRect(3, -1, 2, 2);
    529.                 body.graphics.endFill();
    530.                 body.visible = false;
    531.                 _food = body.addChild(new Sprite()) as Sprite;
    532.                 _food.graphics.beginFill(0xFFFFFF, 1);
    533.                 _food.graphics.drawRect(-2, -2, 4, 4);
    534.                 _food.graphics.endFill();
    535.                 _food.x = 6;
    536.                 _food.visible = false;
    537.                 position = new Point(x, y);
    538.                 _locus = new Vector.<Point>();
    539.                 _radian = Math.random() * Angle.ALL_RADIAN;
    540.                 _randomRad = (Math.random() * 10 - 5) * Angle.TO_RADIAN;
    541.         }
    542.         ///行動
    543.         public function action(w:World):void {
    544.                 if (thinkTime) {
    545.                         if (--thinkTime == 0) {
    546.                                 if (_status == 0) startSearch();
    547.                                 if (_startReturn) {
    548.                                         _startReturn = false;
    549.                                         toFace(w.home.position);
    550.                                 }
    551.                         }
    552.                         return;
    553.                 } else {
    554.                         randomThink(0.015, Math.random() * 10 + 15);
    555.                 }
    556.                 //エサ探しモード
    557.                 if (_status == 0) {
    558.                         _searchCnt = ++_searchCnt % 3;
    559.                         if (!_searchCnt) {
    560.                                 var near:Number = Number.MAX_VALUE;
    561.                                 _targetFood = null;
    562.                                 for each(var f:Food in w.foods) {
    563.                                         var distance:Number = position.subtract(f.position).length;
    564.                                         //エサに接触したら持ち帰り始める
    565.                                         if (distance <= f.size/2 + 1) {
    566.                                                 thinkTime = Math.random() * 50 + 30;
    567.                                                 getFood(f.image.getRandomColor());
    568.                                                 w.cutFood(f);
    569.                                                 return;
    570.                                         }
    571.                                         //エサを見つけた
    572.                                         var d:Number = distance - f.size/2;
    573.                                         if (d <= _view && d < near) {
    574.                                                 near = d;
    575.                                                 _targetFood = f;
    576.                                         }
    577.                                 }
    578.                         }
    579.                         //エサを見つけているか
    580.                         if (_targetFood) {
    581.                                 toFace(_targetFood.position);
    582.                         } else {
    583.                                 if (_freeTime > 0) _freeTime--;
    584.                                 //フェロモンが近くにあるかチェック
    585.                                 var rad:Number = (_freeTime > 0)? NaN : w.pheromone.getGuidepost(position.x, position.y);
    586.                                 if (isNaN(rad)) {
    587.                                         //なければうろつく
    588.                                         wander();
    589.                                 } else {
    590.                                         //あればフォロモンの流れに向く
    591.                                         _radian = Angle.between(rad, _radian, 0.5) + Angle.TO_RADIAN + _randomRad;
    592.                                         checkStay();
    593.                                 }
    594.                         }
    595.                 }
    596.                
    597.                 //エサ持ち帰りモード
    598.                 if (_status == 1) {
    599.                         var per:Number = _radian / Angle.ALL_RADIAN;
    600.                         w.pheromone.putPheromone(position.x, position.y, per);
    601.                         goto(w.home.position);
    602.                         if (w.home.position.subtract(position).length < 5) backHome();
    603.                 }
    604.                
    605.                 //進行方向に進む
    606.                 position.x += Math.cos(_radian) * _speed;
    607.                 position.y += Math.sin(_radian) * _speed;
    608.                
    609.                 //障害物を避ける
    610.                 adjustPosition(w);
    611.                
    612.                 //表示更新
    613.                 body.x = position.x;
    614.                 body.y = position.y;
    615.                 body.rotation = _radian * Angle.TO_ROTATION;
    616.         }
    617.         ///ランダム回転
    618.         private function wander():void {
    619.                 _wanderCnt = ++_wanderCnt % 4;
    620.                 if (!_wanderCnt) _radian += (Math.random() * 60 - 30) * Angle.TO_RADIAN;
    621.         }
    622.         ///指定の座標を向く
    623.         private function toFace(target:Point):void {
    624.                 _radian = Math.atan2(target.y - position.y, target.x - position.x);
    625.         }
    626.         ///指定座標に向かいながらランダム回転
    627.         private function goto(target:Point):void {
    628.                 _wanderCnt = ++_wanderCnt % 4;
    629.                 if (!_wanderCnt) {
    630.                         var minus:Point = target.subtract(position);
    631.                         var rad:Number = Math.atan2(minus.y, minus.x);
    632.                         var per:Number = minus.length / 100;
    633.                         if (per > 1) per = 1;
    634.                         _radian = Angle.between(rad, _radian, 0.75 * per) + (Math.random() * 30 - 15) * Angle.TO_RADIAN;
    635.                 }
    636.         }
    637.         ///一定時間その場でうろついていたらフェロモン無効に
    638.         private function checkStay():void {
    639.                 _locus.unshift(position.clone());
    640.                 _locus.length = 4;
    641.                 if (_locus[3] && _locus[0].subtract(_locus[3]).length <= _speed * 2) _freeTime = 60;
    642.         }
    643.         ///障害物を避けるように位置と角度を調整
    644.         private function adjustPosition(w:World):void {
    645.                 //円形障害物を避ける角度を調べる
    646.                 var adjustRad1:Number = NaN;
    647.                 var plus:Point = new Point(Math.cos(_radian) * _speed, Math.sin(_radian) * _speed);
    648.                 var nextPos:Point = position.add(plus);
    649.                 for each(var obs:Obstacle in w.obstacles) {
    650.                         var distance:Number = Point.distance(nextPos, obs.center);
    651.                         if (distance < obs.radius) {
    652.                                 var radius:Point = nextPos.subtract(obs.center);
    653.                                 if (radius.length < obs.radius) {
    654.                                         radius.normalize(obs.radius);
    655.                                         var fixPos:Point = obs.center.add(radius);
    656.                                         adjustRad1 = Math.atan2(fixPos.y - position.y, fixPos.x - position.x);
    657.                                 }
    658.                                 break;
    659.                         }
    660.                 }
    661.                 if(!isNaN(adjustRad1)) _radian = Angle.between(adjustRad1, _radian, 0.85);
    662.                
    663.                 //ワールドエリア内に収まる位置と角度を調べる
    664.                 var rect:Rectangle = w.area;
    665.                 var adjustRad2:Number = NaN;
    666.                 var padding:int = 5;
    667.                 if (position.x < rect.left + padding) {
    668.                         position.x = rect.left + padding;
    669.                         adjustRad2 = 0;
    670.                 }
    671.                 if (position.x > rect.right - padding) {
    672.                         position.x = rect.right - padding;
    673.                         adjustRad2 = Math.PI;
    674.                 }
    675.                 if (position.y > rect.bottom - padding) {
    676.                         position.y = rect.bottom - padding;
    677.                         adjustRad2 = Math.PI * 1.5;
    678.                 }
    679.                 if (position.y < rect.top + padding) {
    680.                         position.y = rect.top + padding;
    681.                         adjustRad2 = Math.PI * 0.5;
    682.                 }
    683.                 if (!isNaN(adjustRad2)) _radian = Angle.between(adjustRad2, _radian, 0.9);
    684.         }
    685.         ///一定確率で立ち止まる
    686.         private function randomThink(per:Number, time:int):void {
    687.                 if (Math.random() <= per) thinkTime = time;
    688.         }
    689.         ///エサを探し始める
    690.         private function startSearch():void {
    691.                 body.visible = true;
    692.                 _status = 0;
    693.         }
    694.         ///巣に入れる
    695.         private function backHome():void {
    696.                 _status = 0;
    697.                 _food.visible = false;
    698.                 body.visible = false;
    699.                 thinkTime = Math.random() * 100 + 100;
    700.         }
    701.         ///エサを取得
    702.         private function getFood(color:uint = 0xFFFFFF):void {
    703.                 _status = 1;
    704.                 _startReturn = true;
    705.                 _food.visible = true;
    706.                 var ct:ColorTransform = new ColorTransform();
    707.                 ct.color = color;
    708.                 _food.transform.colorTransform = ct;
    709.         }
    710.         ///親から削除
    711.         public function remove():void {
    712.                 _targetFood = null;
    713.                 if (body.parent != null) body.parent.removeChild(body);
    714.         }
    715. }
    复制代码


    下载:


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?立即注册

    x
    守望者AIR技术交流社区(www.airmyth.com)
    回复 支持 反对

    使用道具 举报

  • TA的每日心情
    开心
    2019-2-24 01:34
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    0

    主题

    38

    帖子

    868

    积分

    上士

    Rank: 5Rank: 5

    威望
    20
    贡献
    0
    金币
    14
    钢镚
    0
    发表于 2017-10-26 01:04:25 | 显示全部楼层
    感谢分享!~
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2024-3-29 15:58 , Processed in 0.056664 second(s), 31 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表