home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 2 / goldfish_vol2_cd1.bin / files / util / blank / gblanker / source / blankers / aswarm / blank.c < prev    next >
C/C++ Source or Header  |  1994-10-08  |  10KB  |  382 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 <intuition/screens.h>
  13.  
  14. #include <dos/dos.h>
  15. #include <graphics/gfxbase.h>
  16. #include <graphics/gfxmacros.h>
  17. #include <graphics/copper.h>
  18. #include <graphics/videocontrol.h>
  19. #include <hardware/custom.h>
  20. #include <hardware/dmabits.h>
  21. #include <graphics/gfxmacros.h>
  22.  
  23. #include <clib/exec_protos.h>
  24. #include <clib/intuition_protos.h>
  25. #include <clib/graphics_protos.h>
  26. #include <clib/alib_protos.h>
  27. #include <clib/gadtools_protos.h>
  28. #include <clib/utility_protos.h>
  29. #include <clib/alib_protos.h>
  30.  
  31. #include "/Garshnelib/Garshnelib_protos.h"
  32. #include "/Garshnelib/Garshnelib_pragmas.h"
  33.  
  34. #include <stdlib.h>
  35.  
  36. #include "/main.h"
  37. #include "//defs.h"
  38. #include "ASwarm.h"
  39.  
  40. #define MAX_SPEED 6L
  41.  
  42. #define MAXBEEVEL ( 3 * mP->BeeAcc )
  43. #define MAXWASPVEL ( 4 * mP->WaspAcc )
  44.  
  45. #define BORDER 5
  46.  
  47. #define BEE_PEN 1
  48. #define WASP_PEN 2
  49.  
  50. #define BEE_COL_NUM 33
  51.  
  52. #define RAND( m ) ( RangeRand( m ) - ( m ) / 2 )
  53.  
  54. #define BXVel( I ) ( SP->ss_X[3][I] )
  55. #define BYVel( I ) ( SP->ss_Y[3][I] )
  56. #define BeeX( P, I ) ( SP->ss_X[P][I] )
  57. #define BeeY( P, I ) ( SP->ss_Y[P][I] )
  58. #define MyWasp( I ) ( SP->ss_MW[I] )
  59. #define WaXVel( I ) ( SP->ss_WX[3][I] )
  60. #define WaYVel( I ) ( SP->ss_WY[3][I] )
  61. #define WaspX( P, I ) ( SP->ss_WX[P][I] )
  62. #define WaspY( P, I ) ( SP->ss_WY[P][I] )
  63. #define SwarmSize( W, B ) ( sizeof( SwarmStruct ) + sizeof( LONG ) * ( W + B ) * 9L )
  64.  
  65. struct ModulePrefs
  66. {
  67.     LONG Mode;
  68.     LONG Wasps;
  69.     LONG Bees;
  70.     LONG WaspAcc;
  71.     LONG BeeAcc;
  72.     LONG Speed;
  73.     LONG Colorcycling;
  74.     LONG Aimmode;
  75. };
  76.  
  77. typedef struct _SwarmStruct
  78. {
  79.     LONG ss_Width;
  80.     LONG ss_Height;
  81.     LONG ss_NumWasps;
  82.     LONG *ss_WX[4];
  83.     LONG *ss_WY[4];
  84.     LONG *ss_NB;
  85.     LONG ss_NumBees;
  86.     LONG ss_BeeAcc;
  87.     LONG *ss_X[4];
  88.     LONG *ss_Y[4];
  89.     LONG *ss_MW;
  90. } SwarmStruct;
  91.  
  92. extern struct ModulePrefs nP;
  93.  
  94. ULONG BeeColors[] =
  95. {
  96.     0x000F, 0x000F, 0x004F, 0x008F, 0x00BF, 0x00FF, 0x00FB, 0x00F7, 0x00F3,
  97.     0x00F0, 0x04F0, 0x08F0, 0x09F0, 0x0AF0, 0x0BF0, 0x0CF0, 0x0DF0, 0x0EF0,
  98.     0x0FF0, 0x0FE0, 0x0FD0, 0x0FC0, 0x0FB0, 0x0F90, 0x0F80, 0x0F70, 0x0F60,
  99.     0x0F50, 0x0F40, 0x0F30, 0x0F20, 0x0F10, 0x0F00
  100. };
  101.  
  102. UWORD SwarmColors[4] = { 0x0000, 0x0000, 0x0FFF, 0x0000 };
  103.  
  104. BYTE sqrt_tab[256] =
  105. {
  106.     0,1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
  107.     6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,
  108.     8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,
  109.     10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,
  110.     11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,
  111.     12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,
  112.     13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,
  113.     14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
  114.     15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
  115.     15,15,15,15,15,15,15
  116.     };
  117.  
  118. LONG FastSQRT( LONG x )
  119. {
  120.     LONG sr = 1L;
  121.     
  122.     while( x > 255L )
  123.     {
  124.         x /= 4L;
  125.         sr *= 2L;
  126.     }
  127.     
  128.     return sr * ( LONG )sqrt_tab[x];
  129. }
  130.  
  131. SwarmStruct *CreateSwarms( struct Screen *Scr, struct ModulePrefs *mP )
  132. {
  133.     LONG Index, *Ptr;
  134.     SwarmStruct    *SP;
  135.  
  136.     if(( SP = AllocVec( SwarmSize( mP->Wasps, mP->Bees ), 0L )) == NULL )
  137.         return NULL;
  138.     
  139.     SP->ss_NumWasps = mP->Wasps;
  140.     SP->ss_NumBees = mP->Bees;
  141.     SP->ss_Width = Scr->Width;
  142.     SP->ss_Height = Scr->Height;
  143.     SP->ss_BeeAcc = mP->BeeAcc;
  144.     
  145.     Ptr = ( LONG * )( &SP[1] );
  146.     for( Index = 0L; Index < 4L; Index++ )
  147.     {
  148.         SP->ss_WX[Index] = Ptr;
  149.         Ptr += mP->Wasps;
  150.         SP->ss_WY[Index] = Ptr;
  151.         Ptr += mP->Wasps;
  152.         SP->ss_X[Index] = Ptr;
  153.         Ptr += mP->Bees;
  154.         SP->ss_Y[Index] = Ptr;
  155.         Ptr += mP->Bees;
  156.     }
  157.     SP->ss_NB = Ptr;
  158.     SP->ss_MW = Ptr + mP->Wasps;
  159.  
  160.     for( Index = 0L; Index < mP->Wasps; Index++ )
  161.     {
  162.         WaspX( 1, Index ) = WaspX( 0, Index ) =    BORDER + RangeRand( SP->ss_Width - 2 * BORDER );
  163.         WaspY( 1, Index ) = WaspY( 0, Index ) =    BORDER + RangeRand( SP->ss_Height - 2 * BORDER );
  164.         WaXVel( Index ) = RAND( mP->WaspAcc );
  165.         WaYVel( Index ) = RAND( mP->WaspAcc );
  166.         SP->ss_NB[Index]=0;
  167.     }
  168.  
  169.     for( Index = 0L; Index < SP->ss_NumBees; Index++ )
  170.     {
  171.         BeeX( 1, Index ) = BeeX( 0, Index ) = BORDER + RangeRand( SP->ss_Width - 2 * BORDER );
  172.         BeeY( 1, Index ) = BeeY( 0, Index ) = BORDER + RangeRand( SP->ss_Height - 2 * BORDER );
  173.         BXVel( Index ) = RAND( SP->ss_BeeAcc );
  174.         BYVel( Index ) = RAND( SP->ss_BeeAcc );
  175.         SP->ss_NB[MyWasp( Index ) = Index % SP->ss_NumWasps]++;
  176.     }
  177.  
  178.     return SP;
  179. }
  180.  
  181. VOID DrawSwarms( struct RastPort *RP, SwarmStruct *SP, struct ModulePrefs *mP )
  182. {
  183.     LONG Index, Aimmode = mP->Aimmode;
  184.  
  185.     for( Index = 0L; Index < SP->ss_NumWasps; Index++ )
  186.     {
  187.         WaspX( 2, Index ) = WaspX( 1, Index );
  188.         WaspX( 1, Index ) = WaspX( 0, Index );
  189.         WaspY( 2, Index ) = WaspY( 1, Index );
  190.         WaspY( 1, Index ) = WaspY( 0, Index );
  191.  
  192.         WaXVel( Index ) += RAND( mP->WaspAcc );
  193.         WaYVel( Index ) += RAND( mP->WaspAcc );
  194.  
  195.         if( WaXVel( Index ) > MAXWASPVEL )
  196.             WaXVel( Index ) = MAXWASPVEL;
  197.         if( WaXVel( Index ) < -MAXWASPVEL )
  198.             WaXVel( Index ) = -MAXWASPVEL;
  199.         if( WaYVel( Index ) > MAXWASPVEL )
  200.             WaYVel( Index ) = MAXWASPVEL;
  201.         if( WaYVel( Index ) < -MAXWASPVEL )
  202.             WaYVel( Index ) = -MAXWASPVEL;
  203.  
  204.         WaspX( 0, Index ) = WaspX( 1, Index ) + WaXVel( Index );
  205.         WaspY( 0, Index ) = WaspY( 1, Index ) + WaYVel( Index );
  206.  
  207.         if(( WaspX( 0, Index ) < BORDER )||( WaspX( 0, Index ) > SP->ss_Width-BORDER-1 ))
  208.         {
  209.             WaXVel( Index ) = -WaXVel( Index );
  210.             if( WaspX( 0, Index ) < BORDER )
  211.                 WaspX( 0, Index ) = BORDER;
  212.             else
  213.                 WaspX( 0, Index ) = SP->ss_Width-BORDER-1;
  214.         }
  215.  
  216.         if(( WaspY( 0, Index ) < BORDER )||( WaspY( 0, Index ) > SP->ss_Height-BORDER-1 ))
  217.         {
  218.             WaYVel( Index ) = -WaYVel( Index );
  219.             if( WaspY( 0, Index ) < BORDER )
  220.                 WaspY( 0, Index ) = BORDER;
  221.             else
  222.                 WaspY(0, Index ) = SP->ss_Height-BORDER-1;
  223.         }
  224.     }
  225.  
  226.     for( Index = 0L; Index < SP->ss_NumBees; Index++ )
  227.     {
  228.         LONG DX, DY, ChkIndex;
  229.         LONG Distance, NewDistance;
  230.  
  231.         BeeX( 2, Index ) = BeeX( 1, Index );
  232.         BeeX( 1, Index ) = BeeX( 0, Index );
  233.         BeeY( 2, Index ) = BeeY( 1, Index );
  234.         BeeY( 1, Index ) = BeeY( 0, Index );
  235.  
  236.         DX = WaspX( 1, MyWasp( Index )) - BeeX( 1, Index );
  237.         DY = WaspY( 1, MyWasp( Index )) - BeeY( 1, Index );
  238.         Distance = FastSQRT( DX * DX + DY * DY );
  239.         if( !Distance )
  240.             Distance = 1L;
  241.  
  242.         if( Aimmode )
  243.         {
  244.             for( ChkIndex = 0; ChkIndex <= SP->ss_NumWasps; ChkIndex++ )
  245.             {
  246.                 if( ChkIndex != MyWasp( Index ))
  247.                 {
  248.                     LONG NewDX, NewDY;
  249.  
  250.                     NewDX = WaspX( 1, ChkIndex ) - BeeX( 1, Index );
  251.                     NewDY = WaspY( 1, ChkIndex ) - BeeY( 1, Index );
  252.                     NewDistance = FastSQRT( NewDX * NewDX + NewDY * NewDY );
  253.                     if( Distance > NewDistance )
  254.                     {
  255.                         DX = NewDX;
  256.                         DY = NewDY;
  257.                         if(!( NewDistance ))
  258.                             Distance = 1L;
  259.                         else
  260.                             Distance = NewDistance;
  261.                         SP->ss_NB[MyWasp( Index )]--;
  262.                         SP->ss_NB[MyWasp( Index ) = ChkIndex]++;
  263.                     }
  264.                 }
  265.             }
  266.         }
  267.  
  268.         BXVel( Index ) +=( DX * SP->ss_BeeAcc ) / Distance + RAND( 3 );
  269.         BYVel( Index ) +=( DY * SP->ss_BeeAcc ) / Distance + RAND( 3 );
  270.  
  271.         if( BXVel( Index ) > MAXBEEVEL )
  272.             BXVel( Index ) = MAXBEEVEL;
  273.         if( BXVel( Index ) < -MAXBEEVEL )
  274.             BXVel( Index ) = -MAXBEEVEL;
  275.         if( BYVel( Index ) > MAXBEEVEL )
  276.             BYVel( Index ) = MAXBEEVEL;
  277.         if( BYVel( Index ) < -MAXBEEVEL )
  278.             BYVel( Index ) = -MAXBEEVEL;
  279.  
  280.         BeeX( 0, Index ) = BeeX( 1, Index ) + BXVel( Index );
  281.         BeeY( 0, Index ) = BeeY( 1, Index ) + BYVel( Index );
  282.  
  283.         if(( BeeX( 0, Index ) < BORDER )||( BeeX( 0, Index ) > SP->ss_Width-BORDER-1 ))
  284.         {
  285.             BXVel( Index ) = -BXVel( Index );
  286.             BeeX( 0, Index ) = BeeX( 1, Index ) + BXVel( Index );
  287.         }
  288.  
  289.         if(( BeeY( 0, Index ) < BORDER )||( BeeY( 0, Index ) > SP->ss_Height-BORDER-1 ))
  290.         {
  291.             BYVel( Index ) = -BYVel( Index );
  292.             BeeY( 0, Index ) = BeeY( 1, Index ) + BYVel( Index );
  293.         }
  294.     }
  295.  
  296.     for( Index = 0L; Index < SP->ss_NumWasps; Index++ )
  297.     {
  298.         SetAPen( RP, 0 );
  299.         Move( RP, WaspX( 2, Index ), WaspY( 2, Index ));
  300.         Draw( RP, WaspX( 1, Index ), WaspY( 1, Index ));
  301.         SetAPen( RP, WASP_PEN );
  302.         Draw( RP, WaspX( 0, Index ), WaspY( 0, Index ));
  303.     }
  304.  
  305.     for( Index = 0L; Index < SP->ss_NumBees; Index++ )
  306.     {
  307.         SetAPen( RP, 0 );
  308.         Move( RP, BeeX( 2, Index ), BeeY( 2, Index ));
  309.         Draw( RP, BeeX( 1, Index ), BeeY( 1, Index ));
  310.         SetAPen( RP, BEE_PEN );
  311.         Draw( RP, BeeX( 0, Index ), BeeY( 0, Index ));
  312.     }
  313. }
  314.  
  315. LONG Blank( VOID *Prefs )
  316. {
  317.     struct Screen *SwarmScreen;
  318.     struct Window *Wnd;
  319.     SwarmStruct *Swarms;
  320.     struct ModulePrefs *mP;
  321.     LONG Count, Color, DColor, ScrToFrontCnt = 0, RetVal = OK;
  322.  
  323.     if( ASwarmWnd )
  324.         mP = &nP;
  325.     else
  326.         mP = ( struct ModulePrefs * )Prefs;
  327.     
  328.     SwarmScreen = OpenScreenTags( 0L, SA_Depth, 2, SA_Overscan, OSCAN_STANDARD,
  329.                                  SA_DisplayID, mP->Mode, SA_Quiet, TRUE,
  330.                                  SA_Behind, TRUE, TAG_DONE );
  331.     if( SwarmScreen )
  332.     {
  333.         SetRGB4( &( SwarmScreen->ViewPort ), 0, 0, 0, 0 );
  334.         SetRast( &( SwarmScreen->RastPort ), 0 );
  335.         Wnd = BlankMousePointer( SwarmScreen );
  336.         ScreenToFront( SwarmScreen );
  337.         Swarms = CreateSwarms( SwarmScreen, mP );
  338.         if( Swarms )
  339.         {
  340.             Color = BEE_COL_NUM-1;
  341.             DColor = mP->Colorcycling ? 1 : 0;
  342.             Count = mP->Speed + 1;
  343.             
  344.             while( RetVal == OK )
  345.             {
  346.                 SwarmColors[BEE_PEN] = BeeColors[Color];
  347.                 LoadRGB4(&( SwarmScreen->ViewPort ), SwarmColors, 4 );
  348.                 Color += DColor;
  349.                 if(( Color == -1 )||( Color == BEE_COL_NUM ))
  350.                 {
  351.                     DColor = -DColor;
  352.                     Color += 2 * DColor;
  353.                 }
  354.                 
  355.                 WaitTOF();
  356.                 
  357.                 if(!( ScrToFrontCnt++ % 60 ))
  358.                     ScreenToFront( SwarmScreen );
  359.                     
  360.                 if( ++Count > MAX_SPEED )
  361.                 {
  362.                     Count = mP->Speed + 1;
  363.                     DrawSwarms(&( SwarmScreen->RastPort ), Swarms, mP );
  364.  
  365.                 }
  366.  
  367.                 RetVal = ContinueBlanking();
  368.             }
  369.             FreeVec( Swarms );
  370.         }
  371.         else
  372.             RetVal = FAILED;
  373.         
  374.         UnblankMousePointer( Wnd );
  375.         CloseScreen( SwarmScreen );
  376.     }
  377.     else
  378.         RetVal = FAILED;
  379.  
  380.     return RetVal;
  381. }
  382.