home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / gblanker3.5.src.lha / GSource / Blankers / Goats / blank.c next >
Encoding:
C/C++ Source or Header  |  1994-10-08  |  5.8 KB  |  284 lines

  1. /*
  2.  *  Copyright (c) 1994 Michael D. Bayne.
  3.  *  All rights reserved.
  4.  *
  5.  *  Please see the documentation accompanying the distribution for distribution
  6.  *  and disclaimer information.
  7.  */
  8.  
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11.  
  12. #include <dos/dos.h>
  13.  
  14. #include <clib/exec_protos.h>
  15. #include <clib/intuition_protos.h>
  16. #include <clib/graphics_protos.h>
  17. #include <clib/alib_protos.h>
  18.  
  19. #include "/Garshnelib/Garshnelib_protos.h"
  20. #include "/Garshnelib/Garshnelib_pragmas.h"
  21.  
  22. #include "Goats.h"
  23. #include "//defs.h"
  24. #include "/main.h"
  25.  
  26. struct ModulePrefs
  27. {
  28.     LONG Mode;
  29.     LONG Delay;
  30.     LONG Herders;
  31.     LONG Goats;
  32.     LONG Reproduction;
  33.     LONG Screen;
  34. };
  35.  
  36. extern struct ModulePrefs nP;
  37.  
  38. #define GOAT 1
  39. #define HERDER 2
  40. #define GRASS 3
  41.  
  42. typedef struct _Position
  43. {
  44.     LONG x;
  45.     LONG y;
  46. } Position;
  47.  
  48. LONG myBlank(struct Screen *LScr, UWORD Width, UWORD Height);
  49. void doGoats(struct RastPort *r);
  50. void doHerders(struct RastPort *r);
  51. LONG neighbor(struct RastPort *r, LONG dir);
  52.  
  53. Position    herders[512];
  54. LONG        herderQ[512] = {0};
  55. LONG        numHerders, herderClr;
  56. Position    goats[512];
  57. LONG        goatQ[512] = {0};
  58. LONG        grassEaten[512] = {0};
  59. LONG        numGoats, goatClr, reproduction;
  60. LONG        x,y,tx,ty, backgroundClr, grassClr;
  61. LONG        Width, Height;
  62.  
  63. LONG myBlank(struct Screen *LScr, UWORD Width, UWORD Height)
  64. {
  65.     struct RastPort *r;
  66.     
  67.     r = &( LScr->RastPort );
  68.     doHerders(r);
  69.     doGoats(r);
  70.     
  71.     return 0;
  72. }
  73.  
  74. void doHerders(struct RastPort *r)
  75. {
  76.     LONG    ptr = numHerders-1;
  77.     LONG    i,n,newX,newY,startDir,tmpDir;
  78.     LONG    flag = 0;
  79.     
  80.     while( ptr >= 0 ) {
  81.         if( herderQ[ptr] ) {
  82.             flag = 1;
  83.             x = herders[ptr].x;
  84.             y = herders[ptr].y;
  85.             SetAPen(r, grassClr);
  86.             WritePixel(r, x, y);
  87.             startDir = tmpDir  = (LONG)RangeRand(8);
  88.             while (1) {
  89.                 n = neighbor(r, tmpDir);
  90.                 if (n == grassClr || n == -1) {
  91.                     tmpDir = (tmpDir+1) % 8;
  92.                     if (tmpDir == startDir) {
  93.                         herderQ[ptr] = 0;
  94.                         break;
  95.                     }
  96.                 }
  97.                 else {
  98.                     herders[ptr].x = newX = tx;
  99.                     herders[ptr].y = newY = ty;
  100.                     i = 0;
  101.                     while(herderQ[i] && i < numHerders)
  102.                         ++i;
  103.                     if (i != numHerders) {
  104.                         herders[i].x = newX;
  105.                         herders[i].y = newY;
  106.                         herderQ[i] = 1;
  107.                     }
  108.                     SetAPen(r,herderClr);
  109.                     WritePixel(r,newX,newY);
  110.                     break;
  111.                 }
  112.             }
  113.         }
  114.         --ptr;
  115.     }
  116.     if (!flag) {
  117.         herderQ[0] = 1;
  118.         herders[0].x = (LONG)RangeRand(Width-1);
  119.         herders[0].y = (LONG)RangeRand(Height-1);
  120.     }
  121. }
  122.  
  123. void doGoats(struct RastPort *r)
  124. {
  125.     LONG    ptr = numGoats-1;
  126.     LONG    i,n,newX,newY,startDir,tmpDir;
  127.     LONG    flag = 0;
  128.  
  129.     while (ptr >= 0) {
  130.         if (goatQ[ptr] ) {
  131.             flag = 1;
  132.             x = goats[ptr].x;
  133.             y = goats[ptr].y;
  134.             SetAPen(r, backgroundClr);
  135.             WritePixel(r, x, y);
  136.             startDir = tmpDir  = (LONG)RangeRand(8);
  137.             while (1) {
  138.                 n = neighbor(r, tmpDir);
  139.                 if (n != grassClr || n == -1) {
  140.                     tmpDir = (tmpDir+1) % 8;
  141.                     if (tmpDir == startDir) {
  142.                         goatQ[ptr] = 0;
  143.                         break;
  144.                     }
  145.                 }
  146.                 else {
  147.                     goats[ptr].x = newX = tx;
  148.                     goats[ptr].y = newY = ty;
  149.                     ++grassEaten[ptr];
  150.                     if (grassEaten[ptr] >= reproduction) {
  151.                         grassEaten[ptr] = 0;
  152.                         i = 0;
  153.                         while(goatQ[i] != 0 && i < numGoats)
  154.                             ++i;
  155.                         if (i != numGoats) {
  156.                             goats[i].x = newX;
  157.                             goats[i].y = newY;
  158.                             goatQ[i] = 1;
  159.                             grassEaten[i] = 0;
  160.                         }
  161.                     }
  162.                     SetAPen(r,goatClr);
  163.                     WritePixel(r,newX,newY);
  164.                     break;
  165.                 }
  166.             }
  167.         }
  168.         --ptr;
  169.     }
  170.     if (!flag) {
  171.         goatQ[0] = 1;
  172.         goats[0].x = (LONG)RangeRand(Width-1);
  173.         goats[0].y = (LONG)RangeRand(Height-1);
  174.         grassEaten[0] = 0;
  175.     }
  176. }
  177.  
  178. LONG neighbor(struct RastPort *r, LONG dir)
  179. {
  180.     switch (dir) {
  181.     case 0:tx = x;ty = y+1;break;
  182.     case 1:tx = x+1;ty = y+1;break;
  183.     case 2:tx = x+1;ty = y;break;
  184.     case 3:tx = x+1;ty = y-1;break;
  185.     case 4:tx = x;ty = y-1;break;
  186.     case 5:tx = x-1;ty = y-1;break;
  187.     case 6:tx = x-1;ty = y;break;
  188.     case 7:tx = x-1;ty = y+1;break;
  189.     default: return -1;break;
  190.     }
  191.     if (tx < 0 || tx >= Width || ty < 0 || ty >= Height)
  192.         return -1; 
  193.     
  194.     return (LONG)ReadPixel(r, tx, ty);
  195. }
  196.  
  197. LONG Blank( VOID *Prefs )
  198. {
  199.     struct ModulePrefs *lP;
  200.     struct Screen *LScr;
  201.     struct Window *Wnd;
  202.     LONG RetVal = OK,i,goatFlag = 0, ToFrontCount = 0;
  203.     
  204.     if( GoatsWnd )
  205.         lP = &nP;
  206.     else
  207.         lP = ( struct ModulePrefs * )Prefs;
  208.  
  209.     if (lP->Screen)
  210.         LScr = cloneTopScreen( FALSE, TRUE );
  211.     else
  212.         LScr = OpenScreenTags( 0l, SA_DisplayID, lP->Mode, SA_Depth, 2, SA_Overscan,
  213.                               OSCAN_STANDARD, SA_Quiet, TRUE, SA_Behind, TRUE, TAG_DONE );
  214.     
  215.     if( LScr ) {
  216.         if( GarshnelibBase->lib_Version < 39 || !(lP->Screen)) {
  217.             SetRGB4(&(LScr->ViewPort),0,0x0,0x0,0x0);
  218.             SetRGB4(&(LScr->ViewPort),1,0x8,0x8,0x8);
  219.             SetRGB4(&(LScr->ViewPort),2,0x7,0x4,0x2);
  220.             SetRGB4(&(LScr->ViewPort),3,0x0,0xa,0x0);
  221.             backgroundClr = 0;
  222.             grassClr = GRASS;
  223.             herderClr = HERDER;
  224.             goatClr = GOAT;
  225.         }
  226.         else {
  227.             backgroundClr = FindColor( LScr->ViewPort.ColorMap, 0, 0, 0, -1 );
  228.             grassClr = FindColor( LScr->ViewPort.ColorMap, 0, 0x9L<<28, 0, -1 );
  229.             goatClr = FindColor( LScr->ViewPort.ColorMap, 0xAL<<28, 0xAL<<28, 0xAL<<28, -1 );
  230.             herderClr = FindColor( LScr->ViewPort.ColorMap, 0xBL<<28, 0x2L<<28, 0x4L<<28, -1 );
  231.         }
  232.     
  233.         numGoats = lP->Goats;
  234.         numHerders = lP->Herders;
  235.         reproduction = lP->Reproduction;
  236.         Width = LScr->Width;
  237.         Height = LScr->Height;
  238.         
  239.         for (i=0;i<numHerders;++i)
  240.             herderQ[i] = 0;
  241.         herderQ[0] = 1;
  242.         herders[0].x = Width/2;
  243.         herders[0].y = Height/2;
  244.         for (i=0;i<numGoats;++i)
  245.             goatQ[i] = 0;
  246.         
  247.         Wnd = BlankMousePointer( LScr );
  248.         ScreenToFront( LScr );
  249.         i = 0;
  250.         while( RetVal == OK )
  251.         {
  252.             WaitTOF();
  253.  
  254.             if(!( ++ToFrontCount % 60 ))
  255.                 ScreenToFront( LScr );
  256.             
  257.             if( !lP->Delay || !( ToFrontCount % lP->Delay ))
  258.             {
  259.                 myBlank( LScr, LScr->Width, LScr->Height );
  260.                 if (!goatFlag && i == 20)
  261.                 {
  262.                     goatQ[0] = 1;
  263.                     goats[0].x = Width/2;
  264.                     goats[0].y = Height/2;
  265.                     grassEaten[0] = 0;
  266.                     goatFlag = 1;
  267.                 }
  268.                 else
  269.                     ++i;
  270.             }
  271.  
  272.             RetVal = ContinueBlanking();
  273.         }
  274.  
  275.         UnblankMousePointer( Wnd );
  276.         CloseScreen( LScr );
  277.     }
  278.     else
  279.         RetVal = FAILED;
  280.     
  281.     return RetVal;
  282. }
  283.  
  284.