home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 04 Pathfinding and Movement / 01 Charla, Mika / SENSOR.C < prev    next >
Encoding:
C/C++ Source or Header  |  2001-08-19  |  4.1 KB  |  151 lines

  1. /* 
  2.  *    Simple 4 point sensor (whisker) that, when activated,
  3.  *    pushes the "bot".
  4.  *    This program demonstrates a simple coralling mechanic.
  5.  */
  6.  
  7. #define screen_w      320
  8. #define screen_h      200
  9.  
  10. #define solid_wall    5    /*Collision Properties*/
  11. #define empty_space   0
  12.  
  13. #define sradius       24   /*Sensor Radius*/
  14. #define distance      8    /*Sensor offset from Bot*/
  15. #define repel         .08  /*Sensor repel strength*/
  16. #define bots          32   /*Number of Bots*/
  17. #define northbias     0    /*Bias, used to push bot in desired direction*/
  18. #define southbias     0
  19. #define eastbias      0
  20. #define westbias      0
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <math.h>
  25.  
  26. #include "allegro.h"
  27.  
  28. BITMAP *vfb;
  29.  
  30. int main()
  31. {
  32.    int BotPosx[bots] ;
  33.    int BotPosy[bots] ;
  34.    float w_east[bots];
  35.    float w_west[bots];
  36.    float w_north[bots];
  37.    float w_south[bots];
  38.    int x;
  39.    int y;
  40.  
  41.    int r;
  42.    int c;
  43.    BITMAP *Bot_Image;
  44.    BITMAP *backdrop;
  45.  
  46.    allegro_init();
  47.    install_keyboard(); 
  48.    install_mouse();
  49.    install_timer();
  50.    set_gfx_mode(GFX_VGA, 320, 200, 0, 0);
  51.    set_pallete(desktop_pallete);
  52.  
  53.    Bot_Image = create_bitmap(32, 32);
  54.    backdrop = create_bitmap(320,200);
  55.    vfb = create_bitmap(320,200);
  56.    clear(Bot_Image); 
  57.    clear(backdrop);
  58.  
  59.    for (c=0; c<8; c++)                             /*Create Border*/
  60.        rect(backdrop,c,c,319-c,199-c,solid_wall);
  61.    for (c=0; c<4; c++)                             /*Create Bot image */
  62.       circle(Bot_Image, 16, 16, c*2, c);
  63.  
  64.       rectfill(backdrop,120,0,180,80,solid_wall);  /*Create "bridge"*/
  65.       rectfill(backdrop,120,120,180,200,solid_wall);
  66.  
  67.       for (c=0; c < bots; c++)    /*Clear Variables for Init*/
  68.       {
  69.         BotPosx[c] = 160;
  70.         BotPosy[c] = 100;
  71.         w_east[c] = 0;
  72.         w_west[c] = 0;
  73.         w_north[c] = 0;
  74.         w_south[c] = 0;
  75.       }
  76.  
  77.  
  78. /* Main Loop */
  79.  
  80.    do  {
  81.  
  82.    vsync();
  83.    clear(vfb);
  84.  
  85.    if (mouse_b & 1)       /*Left Mouse Button Draws a wall*/
  86.             rectfill(backdrop,(mouse_x)/8*8,(mouse_y)/8*8,(mouse_x)/8*8+7,(mouse_y)/8*8+7,solid_wall);
  87.    if (mouse_b & 2)       /*Right Mouse Button Removes a wall*/
  88.             rectfill(backdrop,(mouse_x)/8*8,(mouse_y)/8*8,(mouse_x)/8*8+7,(mouse_y)/8*8+7,empty_space);
  89.  
  90.    blit(backdrop,vfb,0,0,0,0,320,200);  /* Screen Building Process */
  91.    rectfill(vfb,mouse_x-4,mouse_y-4,mouse_x+4,mouse_y+4,2);
  92.  
  93.    for (c=0;c<bots;c++)     /* Update Array of Bots */
  94.    {
  95.  
  96.    for (x=0;x<sradius;x++)  /* Check West sensor on Bot */
  97.    {
  98.         for (y=0;y<sradius;y++)
  99.             if (getpixel(vfb,BotPosx[c]-distance+x,BotPosy[c]+y))
  100.                w_west[c]=w_west[c]+repel;
  101.    }
  102.  
  103.    for (x=0;x<sradius;x++) /* Check East sensor on Bot */
  104.    {
  105.         for (y=0;y<sradius;y++)
  106.             if (getpixel(vfb,BotPosx[c]+distance+x,BotPosy[c]+y))
  107.                w_east[c]=w_east[c]+repel;
  108.    }
  109.    for (x=0;x<sradius;x++) /* Check North sensor on Bot */
  110.    {
  111.         for (y=0;y<sradius;y++)
  112.             if (getpixel(vfb,BotPosx[c]+x,BotPosy[c]+y-distance))
  113.                w_north[c]=w_north[c]+repel;
  114.    }
  115.    for (x=0;x<sradius;x++) /* Check South sensor on Bot */
  116.    {
  117.         for (y=0;y<sradius;y++)
  118.             if (getpixel(vfb,BotPosx[c]+x,BotPosy[c]+y+distance))
  119.                w_south[c]=w_south[c]+repel;
  120.    }
  121.  
  122.  
  123.    BotPosx[c] = BotPosx[c]+(w_west[c]-w_east[c]);   /* Modify Bot position with Sensor data */
  124.    BotPosy[c] = BotPosy[c]+(w_north[c]-w_south[c]);
  125.  
  126.    w_east[c] = w_east[c] *.9;  /* Sensor bias erosion - decelerate */
  127.    w_west[c] = w_west[c] *.9;
  128.    w_north[c] = w_north[c] *.9;
  129.    w_south[c] = w_south[c] *.9;
  130.  
  131.    w_west[c] = w_west[c]+westbias; /* Trick sensor with a bias */
  132.    w_east[c] = w_east[c]+eastbias;
  133.    w_north[c] = w_north[c]+northbias;
  134.    w_south[c] = w_south[c]+southbias;
  135.  
  136.  
  137.    masked_blit(Bot_Image,vfb,0,0,BotPosx[c]-8,BotPosy[c]-8,32,32); /*Draw Bot*/
  138.    }
  139.  
  140.    blit(vfb,screen,0,0,0,0,320,200); /* Blit Virtual screen to actual screen */
  141.  
  142.    } while (!keypressed());
  143.  
  144.    destroy_bitmap(Bot_Image);
  145.    destroy_bitmap(vfb);
  146.    destroy_bitmap(backdrop);
  147.  
  148.    return 0;
  149. }
  150.  
  151.