home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / ROADS15.ARJ / ANIM.C next >
C/C++ Source or Header  |  1994-04-05  |  10KB  |  345 lines

  1. #define ANIM_C
  2.  
  3. #include <fastgraf.h>
  4. #include <stdlib.h>
  5. #include "roads.h"
  6. #include "tiles.h"
  7.  
  8. extern int far *topography;      /* BACKGROUND TILE LIST (ARRAY) */
  9. extern int far *terrain;      /* FOREGROUND TILE LIST (ARRAY) */
  10. extern int view_x, view_y;     /* VIEW AREA (UPPER LEFT CORNER) */
  11. extern int viewpage;           /* CURRENTLY VIEWED PAGE */
  12.  
  13. int frogmode;
  14. int frogwatchmode=0;
  15. int animatemode=1;
  16.  
  17. /* ANIMATION VARIABLES */
  18. struct ANIMATION fire, water1, water2, uranium, frog;
  19. struct ANIMATION *anim_list[ANIM_LIST_TOTAL];
  20.  
  21. /*
  22.  *
  23.  * Animates all cells to the next frame.
  24.  *
  25.  */
  26. void animate (void)
  27. {
  28.     register int x, y;
  29.     int a, i, tile;
  30.     long time;
  31.     int total_updates=0;
  32.  
  33.     gogofrog();
  34.     time=fg_getclock();
  35.  
  36.     /* UPDATE ALL ANIM TYPES FOR NEXT FRAME IF TIME TO DO SO */
  37.     for (a=0; a<ANIM_LIST_TOTAL; a++) /* CHECK ALL ANIM TYPES */
  38.     {
  39.         if (anim_list[a]->next<1) /* EVENT_DRIVEN ANIMATION */
  40.         {
  41.             if (anim_list[a]->next==-1) /* EVENT OCCURING! */
  42.             {
  43.                 anim_list[a]->next=0; /* TURN EVENT OFF */
  44.                 total_updates++;
  45.             }
  46.         }
  47.  
  48.         else if (anim_list[a]->next<=time) /* IS ANIM READY FOR NEXT FRAME? */
  49.         {
  50.             anim_list[a]->next=time+anim_list[a]->delay; /* SET NEXT FRAME TIME */
  51.             anim_list[a]->current+=1;
  52.             if (anim_list[a]->current>=anim_list[a]->first+anim_list[a]->total)
  53.                 anim_list[a]->current=anim_list[a]->first;
  54.             total_updates++;
  55.         }
  56.     }
  57.  
  58.     if (total_updates==0) return; /* NO ANIMATION TODAY.  SORRY */
  59.  
  60.     /* VISUALLY UPDATE ALL VIEWABLE ANIMATIONS */
  61.     for (y=0; y<VIEW_HEIGHT; y++)
  62.     for (x=0; x<VIEW_WIDTH; x++)
  63.     {
  64.         i=VIEW_TO_WORLD(VIEW_TILE(x,y)); /* CONVERT VIEW COORDS TO WORLD */
  65.  
  66.         if (is_anim(terrain[i]))
  67.         {
  68.             a=0;
  69.             while (anim_list[a]->anm!=terrain[i]) a++;
  70.             tile=anim_list[a]->current;
  71.  
  72.                 /* COPY TILE TO MIX THEN TO CURRENT SCREEN */
  73.             pagecopy_tile_op (topography[i], MIXING_TILE, TILEPAGE, TILEPAGE);
  74.             pagecopy_tile_tr (tile, MIXING_TILE, TILEPAGE, TILEPAGE);
  75.             pagecopy_tile_op (MIXING_TILE, VIEW_TILE(x,y), TILEPAGE, viewpage);
  76.         }
  77.     }
  78. }
  79.  
  80. #define UP    1
  81. #define DOWN  2
  82. #define LEFT  3
  83. #define RIGHT 4
  84. #define CHANCE_FROG_NOT_TURN 95
  85.  
  86. /*
  87.  *
  88.  * Move frog somewhere new.
  89.  *
  90.  */
  91. void gogofrog (void)
  92. {
  93.     int fails=0, r, suc;
  94.     int newx, newy;
  95.     static int x, y;
  96.     static int facing=RIGHT;
  97.     static int walking=FALSE;
  98.     static long nextfrog;
  99.  
  100.     if (frogmode==3) return; /* NO FROG!  GO AWAY */
  101.  
  102.     if (frogmode==2)
  103.     {
  104.         terrain[WORLD_TILE(x,y)]=EMPTY_TILE;
  105.         update_tile (WORLD_TILE(x,y));
  106.         frogmode=3;
  107.         return;
  108.     }
  109.  
  110.     if (frogmode==1)
  111.     {
  112.         if (nextfrog>fg_getclock())
  113.             return; /* NOT TIME TO ANIMATE OUR FROGGIE */
  114.  
  115.         frog.next=-1; /* TURN ON ANIMATION EVENT FLAG */
  116.  
  117.         /* DETERMINE IF FROG CHANGES FACING */
  118.         if (random(100)>CHANCE_FROG_NOT_TURN)
  119.         {
  120.             walking=FALSE;
  121.  
  122.             NEW_FACING:
  123.  
  124.             /* CHANGE FACING */
  125.             r=random(100);
  126.             switch (facing)
  127.             {
  128.                 case RIGHT:
  129.                 case LEFT:
  130.                 if (r<50) facing=UP;
  131.                 else facing=DOWN;
  132.                 break;
  133.  
  134.                 case DOWN:
  135.                 case UP:
  136.                 if (r<50) facing=RIGHT;
  137.                 else facing=LEFT;
  138.                 break;
  139.             }
  140.  
  141.             /* UPDATE FACING IMAGE */
  142.             switch (facing)
  143.             {
  144.                 case RIGHT: frog.current=FROG_FACE_RIGHT; break;
  145.                 case LEFT:  frog.current=FROG_FACE_LEFT;  break;
  146.                 case DOWN:  frog.current=FROG_FACE_DOWN;  break;
  147.                 case UP:    frog.current=FROG_FACE_UP;    break;
  148.             }
  149.             nextfrog=fg_getclock()+frog.delay;
  150.             return;
  151.         }
  152.  
  153.         /* DETERMINE IF FROG STARTS/STOPS WALKING */
  154.         if (walking==FALSE)
  155.         {
  156.             walking=TRUE;
  157.             switch (facing)
  158.             {
  159.                 case RIGHT: frog.current=FROG_WALK_RIGHT; break;
  160.                 case LEFT:  frog.current=FROG_WALK_LEFT;  break;
  161.                 case DOWN:  frog.current=FROG_WALK_DOWN;  break;
  162.                 case UP:    frog.current=FROG_WALK_UP;    break;
  163.             }
  164.  
  165.             frog.next=-1; /* TURN ON ANIMATION EVENT FLAG */
  166.             nextfrog=fg_getclock()+frog.delay;
  167.             return;
  168.         }
  169.  
  170.         /* DETERMINE IF WE CAN WALK OUR FROGGIE THIS WAY! */
  171.         newx=x;
  172.         newy=y;
  173.         switch (facing)
  174.         {
  175.             case RIGHT: newx++; break;
  176.             case LEFT:  newx--; break;
  177.             case DOWN:  newy++; break;
  178.             case UP:    newy--; break;
  179.         }
  180.  
  181.         /* CAN'T MOVE -- OBSTRUCTION OR END OF WORLD! */
  182.         if (newx<0 || newy<0 || newx>=WORLD_WIDTH || newy>=WORLD_HEIGHT
  183.             || terrain[WORLD_TILE(newx,newy)]!=EMPTY_TILE)
  184.         {
  185.             /* UPDATE FACING IMAGE TO REFLECT FROG STANDS FAST */
  186.             if (random(100)<50) switch (facing)
  187.             {
  188.                 case RIGHT: frog.current=FROG_FACE_RIGHT; break;
  189.                 case LEFT:  frog.current=FROG_FACE_LEFT;  break;
  190.                 case DOWN:  frog.current=FROG_FACE_DOWN;  break;
  191.                 case UP:    frog.current=FROG_FACE_UP;    break;
  192.             }
  193.             else
  194.             {
  195.                 frog.next=-1;
  196.                 goto NEW_FACING;
  197.             }
  198.             nextfrog=fg_getclock()+frog.delay;
  199.             return;
  200.         }
  201.  
  202.         /* CAN MOVE!  MOVE FROG ALONG */
  203.         switch (facing)
  204.         {
  205.             case RIGHT:
  206.             if (++frog.current>=FROG_NUM_WALKS+FROG_WALK_RIGHT)
  207.                 frog.current=FROG_WALK_RIGHT;
  208.             break;
  209.  
  210.             case LEFT:
  211.             if (++frog.current>=FROG_NUM_WALKS+FROG_WALK_LEFT)
  212.                 frog.current=FROG_WALK_LEFT;
  213.             break;
  214.  
  215.             case DOWN:
  216.             if (++frog.current>=FROG_NUM_WALKS+FROG_WALK_DOWN)
  217.                 frog.current=FROG_WALK_DOWN;
  218.             break;
  219.  
  220.             case UP:
  221.             if (++frog.current>=FROG_NUM_WALKS+FROG_WALK_UP)
  222.                 frog.current=FROG_WALK_UP;
  223.             break;
  224.  
  225.         }
  226.  
  227.             /* DON'T MOVE FROG'S X/Y AT CERTAIN FRAMES */
  228.         if (frog.current==FROG_WALK_UP+1 ||
  229.             frog.current==FROG_WALK_UP+3 ||
  230.             frog.current==FROG_WALK_LEFT+1 ||
  231.             frog.current==FROG_WALK_LEFT+3 ||
  232.             frog.current==FROG_WALK_RIGHT+1 ||
  233.             frog.current==FROG_WALK_RIGHT+3 ||
  234.             frog.current==FROG_WALK_DOWN+1 ||
  235.             frog.current==FROG_WALK_DOWN+3 )
  236.         {
  237.             frog.next=-1;
  238.             nextfrog=fg_getclock()+frog.delay;
  239.             return;
  240.         }
  241.  
  242.         terrain[WORLD_TILE(x,y)]=EMPTY_TILE;
  243.         terrain[WORLD_TILE(newx,newy)]=ANM_FROG;
  244.         frog.next=-1;
  245.  
  246. /*
  247.  *
  248.  * Simply put, frog watch mode doesn't work.
  249.  * I got to the point where I said, gosh darnit, who needs
  250.  * to watch a stinking frog anyway?  It's left as is...
  251.  *
  252.  */
  253.         if (frogwatchmode)
  254.         {
  255.  
  256.             if (newx>x)      if (view_x<WORLD_WIDTH-view_x) view_x++;
  257.             else if (newx<x) if (view_x>0) view_x--;
  258.             else if (newy>y) if (view_y<WORLD_HEIGHT-view_y) view_y++;
  259.             else if (newy<y) if (view_y>0) view_y--;
  260.             redraw (NONFLIP_REFRESH);
  261. /*            if (newx>x)      suc=redraw (NONFLIP_SCROLL_RIGHT);
  262.             else if (newx<x) suc=redraw (NONFLIP_SCROLL_LEFT);
  263.             else if (newy>y) suc=redraw (NONFLIP_SCROLL_DOWN);
  264.             else if (newy<y) suc=redraw (NONFLIP_SCROLL_UP);*/
  265.             viewpage=!viewpage; /* SWAP PAGES */
  266.             update_tile (WORLD_TILE(x,y));
  267.             fg_setvpage (viewpage); /* VIEW CORRECT PAGE */
  268.         } else update_tile (WORLD_TILE(x,y));
  269.  
  270.         x=newx;
  271.         y=newy;
  272.         nextfrog=fg_getclock()+frog.delay;
  273.         return;
  274.     }
  275.  
  276. /********************* ADD NEW FROG ************************/
  277.  
  278.     /* LOCATE VIEWABLE TILE TO PLACE FROG */
  279.     do {
  280.         x=random(VIEW_WIDTH)+view_x;
  281.         y=random(VIEW_HEIGHT)+view_y;
  282.         fails++;
  283.     } while (terrain[WORLD_TILE(x,y)]!=EMPTY_TILE && fails<50);
  284.  
  285.     if (fails>=50) /* COULDN'T PLACE FROG */
  286.     {
  287.         fg_music ("L64EC.DE.C$");
  288.         frogmode=3;
  289.         frog.next=0;
  290.         return;
  291.     }
  292.  
  293.     terrain[WORLD_TILE(x,y)]=ANM_FROG; /* INSTALL FROG! */
  294.     frog.next=-1; /* UPDATE EVENT */
  295.     frogmode=1;
  296.     nextfrog=fg_getclock()+frog.delay;
  297.  
  298.     return;
  299. }
  300.  
  301. /*
  302.  *
  303.  * Initializes any animation information, as in at the program start.
  304.  *
  305.  */
  306. void init_anim (void)
  307. {
  308.     fire.first=fire.current=34;
  309.     fire.total=6;
  310.     fire.delay=1;
  311.     fire.next=fg_getclock()+fire.delay;
  312.     fire.anm=ANM_FIRE;
  313.  
  314.     water1.first=water1.current=60;
  315.     water1.total=3;
  316.     water1.delay=3;
  317.     water1.next=fg_getclock()+water1.delay;
  318.     water1.anm=ANM_WATER1;
  319.  
  320.     water2.first=water2.current=63;
  321.     water2.total=3;
  322.     water2.delay=4;
  323.     water2.next=fg_getclock()+water2.delay;
  324.     water2.anm=ANM_WATER2;
  325.  
  326.     uranium.first=uranium.current=66;
  327.     uranium.total=10;
  328.     uranium.delay=4;
  329.     uranium.next=fg_getclock()+uranium.delay;
  330.     uranium.anm=ANM_URANIUM;
  331.  
  332.     frog.first=frog.current=FROG_FACE_RIGHT;
  333.     frog.total=FROG_NUM_WALKS;
  334.     frog.delay=3;
  335.     frog.next=-1;
  336.     frog.anm=ANM_FROG;
  337.  
  338.     anim_list[0]=&fire;
  339.     anim_list[1]=&water1;
  340.     anim_list[2]=&water2;
  341.     anim_list[3]=&uranium;
  342.     anim_list[4]=&frog;
  343. }
  344.  
  345.