home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1999 February / VPR9902A.BIN / FFILLY / BOID / boid3.tfy < prev    next >
Text File  |  1997-11-23  |  4KB  |  222 lines

  1. #info IART "もりきゅう,VZF07161@niftyserve.or.jp"
  2. #info ICMT "Boid 3"
  3. #info INAM "Boid 3"
  4. #info ICOP "(c)Kazuhiro.Yoshida"
  5. #info ISBJ "Boid 3"
  6. #info VIDO "640x480; 256"
  7. #info GRPC "もりきゅう,VZF07161@niftyserve.or.jp"
  8.  
  9. #include "local.tfy"
  10.  
  11. int distance( int nPx1, int nPy1, int nPx2, int nPy2 )
  12. {
  13.     int nDistance;
  14.     real rPx1, rPy1, rPx2, rPy2, rDistance;
  15.     
  16.     rPx1 = InttoReal(nPx1);
  17.     rPy1 = InttoReal(nPy1);
  18.     rPx2 = InttoReal(nPx2);
  19.     rPy2 = InttoReal(nPy2);
  20.     
  21.     rDistance = Sqrt( Pow( rPx2 - rPx1, 2.0 ) + Pow( rPy2 - rPy1, 2.0 ) );
  22.     nDistance = RealtoInt( rDistance );
  23.     
  24.     return nDistance;
  25. }
  26. main()
  27. {
  28.     int i,j;
  29.     int x,y;
  30.     int sum; // 物体(岩と鳥)の数
  31.     int in; // ここまで岩。ここから鳥。
  32.     
  33.     int wMain;
  34.     int pBase,pBack,pBoid,pRock;
  35.     int cBack;
  36.     int nTransColor; // 透明色
  37.     
  38.     int cBoid[];
  39.     int bw2,bh2;
  40.     
  41.     int nx[],ny[]; // 位置
  42.     int vx[],vy[]; // 速度
  43.     int ax[],ay[]; // 加速度
  44.     
  45.     pBase = LoadPic("base.bmp");
  46.     pBoid = LoadPic("boid.bmp");
  47.     pRock = LoadPic("rock.bmp");
  48.     pBack = copyPic(pBase);
  49.     
  50.     nTransColor = GetColor(pBoid,0,0);
  51.     cBack = PutCast(pBack,pBase);
  52.     wMain = OpenWin(pBase);
  53.     
  54.     // ボイド表示修正用
  55.     bw2 = PicWidth(pBoid) / 2;
  56.     bh2 = PicHeight(pBoid) / 2;
  57.     
  58.     sum = 20;
  59.     in = 10;
  60.     
  61.     // 岩の状態
  62.     for( i=0 ; i<in ; i=i+1 )
  63.     {
  64.         nx[i] = PicWidth(pBase)/2;
  65.         ny[i] = PicHeight(pBase)/2;
  66.         vx[i] = vy[i] = 0;
  67.         ax[i] = ay[i] = 0;
  68.         cBoid[i] = PutCast(pRock,pBase,nx[i]-bw2,ny[i]-bh2,nTransColor);
  69.     }
  70.     
  71.     // 鳥の状態
  72.     for( i=in ; i<sum ; i=i+1 )
  73.     {
  74.         nx[i] = Random(PicWidth(pBase));
  75.         ny[i] = Random(PicHeight(pBase));
  76.         vx[i] = Random(11)-5;
  77.         vy[i] = Random(11)-5;
  78.         ax[i] = ay[i] = 0;
  79.         cBoid[i] = PutCast(pBoid,pBase,nx[i]-bw2,ny[i]-bh2,nTransColor);
  80.     }
  81.     
  82.     int mesTIME;
  83.     
  84.     mes(TIME)
  85.     {
  86.     step(5)
  87.     {
  88.         // 質量中心を求める
  89.         x=0;
  90.         y=0;
  91.         for(i=0;i<sum;i=i+1)
  92.         {
  93.             x = x + nx[i];
  94.             y = y + ny[i];
  95.         }
  96.         x = x/sum;
  97.         y = y/sum;
  98.         
  99.         // 規則1
  100.         // 鳥は質量中心に向かう
  101.         for(i=in;i<sum;i=i+1)
  102.         {
  103.             if ( nx[i] < x )
  104.             {
  105.                 ax[i] =  1;
  106.             }
  107.             else if ( nx[i] > x )
  108.             {
  109.                 ax[i] = -1;
  110.             }
  111.             
  112.             if ( ny[i] < y )
  113.             {
  114.                 ay[i] =  1;
  115.             }
  116.             else if ( ny[i] > y )
  117.             {
  118.                 ay[i] = -1;
  119.             }
  120.         }
  121.         
  122.         // 規則2
  123.         // 鳥は物体と一定の間隔を空ける
  124.         for(i=in;i<sum;i=i+1)
  125.         {
  126.             for(j=0;j<sum;j=j+1)
  127.             {
  128.                 if ( j==i )
  129.                 {
  130.                     break;
  131.                 }
  132.                 
  133.                 if ( distance(nx[i],ny[i],nx[j],ny[j]) < 60 )
  134.                 {
  135.                     if ( nx[i] < nx[j] )
  136.                     {
  137.                         ax[i] = -1;
  138.                     }
  139.                     else if ( nx[i] > nx[j] )
  140.                     {
  141.                         ax[i] =  1;
  142.                     }
  143.                     
  144.                     if ( ny[i] < ny[j] )
  145.                     {
  146.                         ay[i] = -1;
  147.                     }
  148.                     else if ( ny[i] > ny[j] )
  149.                     {
  150.                         ay[i] =  1;
  151.                     }
  152.                 }
  153.             }
  154.         }
  155.         
  156.         // 規則3
  157.         // 鳥は周りの鳥と速度を合わせる
  158.         for(i=in;i<sum;i=i+1)
  159.         {
  160.             for(j=in;j<sum;j=j+1)
  161.             {
  162.                 if ( j==i )
  163.                 {
  164.                     break;
  165.                 }
  166.                 
  167.                 if ( distance(nx[i],ny[i],nx[j],ny[j]) < 60 )
  168.                 {
  169.                     if ( vx[i] < vx[j] )
  170.                     {
  171.                         ax[i] =  1;
  172.                     }
  173.                     else if ( vx[i] > vx[j] )
  174.                     {
  175.                         ax[i] = -1;
  176.                     }
  177.                     
  178.                     if ( vy[i] < vy[j] )
  179.                     {
  180.                         ay[i] =  1;
  181.                     }
  182.                     else if ( vy[i] > vy[j] )
  183.                     {
  184.                         ay[i] = -1;
  185.                     }
  186.                 }
  187.             }
  188.         }
  189.         
  190.         // 鳥の状態を変化
  191.         for(i=in;i<sum;i=i+1)
  192.         {
  193.             vx[i] = vx[i] + ax[i];
  194.             vy[i] = vy[i] + ay[i];
  195.             nx[i] = nx[i] + vx[i] / 2;
  196.             ny[i] = ny[i] + vy[i] / 2;
  197.         }
  198.         
  199.         // 移動
  200.         for(i=in;i<sum;i=i+1)
  201.         {
  202.             MoveCast(cBoid[i],pBoid,nx[i]-bw2,ny[i]-bh2);
  203.         }
  204.     }
  205.     }
  206.     mesTIME = GetMesNo(0);
  207.     
  208.     int pause;
  209.     pause = 0;
  210.     
  211.     mes(LBUP)
  212.     {
  213.         if ( pause == 0 ) {
  214.             FreezeMes(mesTIME);
  215.             pause = 1;
  216.         } else {
  217.             ActivateMes(mesTIME);
  218.             pause = 0;
  219.         }
  220.     }
  221. }
  222.