home *** CD-ROM | disk | FTP | other *** search
/ Black Art of 3D Game Programming / Black_Art_of_3D_Game_Programming.iso / source / borland / chap_7 / lockon.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  9.2 KB  |  387 lines

  1.  
  2. // LOCKON.C - A demo of tracking and evasion algorithms
  3.  
  4. // I N C L U D E S ///////////////////////////////////////////////////////////
  5.  
  6. #include <io.h>
  7. #include <conio.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include <bios.h>
  12. #include <fcntl.h>
  13. #include <memory.h>
  14. #include <malloc.h>
  15. #include <math.h>
  16. #include <string.h>
  17.  
  18. #include "black3.h"
  19. #include "black4.h"
  20. #include "black5.h"
  21.  
  22. // D E F I N E S /////////////////////////////////////////////////////////////
  23.  
  24. // these are the animation cell indices for the alien walking in all the
  25. // directions
  26.  
  27. #define ALIEN_START_RIGHT   0
  28. #define ALIEN_START_LEFT    4
  29. #define ALIEN_START_UP      8
  30. #define ALIEN_START_DOWN    12
  31.  
  32. #define ALIEN_END_RIGHT     3
  33. #define ALIEN_END_LEFT      7
  34. #define ALIEN_END_UP        11
  35. #define ALIEN_END_DOWN      15
  36.  
  37. // these are the directions the alien can move in
  38.  
  39. #define ALIEN_RIGHT         0
  40. #define ALIEN_LEFT          1
  41. #define ALIEN_UP            2
  42. #define ALIEN_DOWN          3
  43.  
  44. // G L O B A L S  ////////////////////////////////////////////////////////////
  45.  
  46. pcx_picture image_pcx;  // general PCX image used to load background and imagery
  47.  
  48. sprite alien, creature; // the players alien and the creature
  49.  
  50. // M A I N //////////////////////////////////////////////////////////////////
  51.  
  52. void main(int argc, char **argv)
  53. {
  54.  
  55. int index,    // loop variable
  56.     chase=1,  // used to select creatures mode of operation, 1=chase, 0=evade
  57.     done=0;   // event loop exit flag
  58.  
  59. char buffer[64]; // used to print srtrings
  60.  
  61. // set the graphics mode to mode 13h
  62.  
  63. Set_Graphics_Mode(GRAPHICS_MODE13);
  64.  
  65. // create the double buffer
  66.  
  67. Create_Double_Buffer(200);
  68.  
  69. // load the imagery for the players alien
  70.  
  71. PCX_Init((pcx_picture_ptr)&image_pcx);
  72. PCX_Load("lockaln.pcx", (pcx_picture_ptr)&image_pcx,1);
  73.  
  74. // intialize the alien sprite
  75.  
  76. Sprite_Init((sprite_ptr)&alien,32,164,8,12,0,0,0,0,0,0);
  77.  
  78. // start the alien walking down
  79.  
  80. alien.state      = ALIEN_DOWN;
  81. alien.curr_frame = ALIEN_START_DOWN;
  82.  
  83. // extract the bitmaps for the alien, there are 16 animation cells
  84.  
  85. for (index=0; index<16; index++)
  86.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&alien,index,index,0);
  87.  
  88. // done with this PCX file so delete memory associated with it
  89.  
  90. PCX_Delete((pcx_picture_ptr)&image_pcx);
  91.  
  92. // load the imagery for creature
  93.  
  94. PCX_Init((pcx_picture_ptr)&image_pcx);
  95. PCX_Load("lockcrt.pcx", (pcx_picture_ptr)&image_pcx,1);
  96.  
  97. // intialize the creature sprite
  98.  
  99. Sprite_Init((sprite_ptr)&creature,160,100,24,12,0,0,0,0,0,0);
  100.  
  101. // extract the bitmaps for the creature, there are 4 animation cells
  102.  
  103. for (index=0; index<4; index++)
  104.     PCX_Get_Sprite((pcx_picture_ptr)&image_pcx,(sprite_ptr)&creature,index,index,0);
  105.  
  106. // done with this PCX file so delete memory associated with it
  107.  
  108. PCX_Delete((pcx_picture_ptr)&image_pcx);
  109.  
  110. // now load the background image
  111.  
  112. PCX_Init((pcx_picture_ptr)&image_pcx);
  113. PCX_Load("lockbak.pcx",(pcx_picture_ptr)&image_pcx,1);
  114.  
  115. // copy PCX image to double buffer
  116.  
  117. PCX_Copy_To_Buffer((pcx_picture_ptr)&image_pcx,double_buffer);
  118. PCX_Delete((pcx_picture_ptr)&image_pcx);
  119.  
  120. // scan under alien and creature before entering the event loop, this must be
  121. // done or else on the first cycle the "erase" function will draw garbage
  122.  
  123. Sprite_Under_Clip((sprite_ptr)&alien,double_buffer);
  124. Sprite_Under_Clip((sprite_ptr)&creature,double_buffer);
  125.  
  126. // install new keyboard driver
  127.  
  128. Keyboard_Install_Driver();
  129.  
  130. // put up exit instructions
  131.  
  132. Print_String_DB(96,2,9,"Press Q to exit",0);
  133.  
  134. // main event loop, process until keyboard hit
  135.  
  136. while(!done)
  137.      {
  138.  
  139.      // do animation cycle, erase, move draw...
  140.  
  141.      // erase all objects by replacing what was under them
  142.  
  143.      Sprite_Erase_Clip((sprite_ptr)&alien,double_buffer);
  144.      Sprite_Erase_Clip((sprite_ptr)&creature,double_buffer);
  145.  
  146.      // move player
  147.  
  148.      // test for right motion
  149.  
  150.      if (keyboard_state[MAKE_RIGHT])
  151.         {
  152.         // move alien
  153.  
  154.         if ((alien.x+=2)>320)
  155.             alien.x=-8;
  156.  
  157.         // first test if alien was already moving right
  158.  
  159.         if (alien.state==ALIEN_RIGHT)
  160.            {
  161.            // animate and test for end of sequence
  162.  
  163.            if (++alien.curr_frame==ALIEN_END_RIGHT)
  164.               alien.curr_frame = ALIEN_START_RIGHT;
  165.            }
  166.         else
  167.            {
  168.            // set state and current frame to right
  169.  
  170.            alien.state      = ALIEN_RIGHT;
  171.            alien.curr_frame = ALIEN_START_RIGHT;
  172.  
  173.            } // end else
  174.  
  175.         } // end if right
  176.  
  177.      // test for left motion
  178.      else
  179.      if (keyboard_state[MAKE_LEFT])
  180.         {
  181.         // move alien
  182.  
  183.         if ((alien.x-=2)<-8)
  184.              alien.x=320;
  185.  
  186.         // first test if alien was already moving left
  187.  
  188.         if (alien.state==ALIEN_LEFT)
  189.            {
  190.            // animate and test for end of sequence
  191.  
  192.            if (++alien.curr_frame==ALIEN_END_LEFT)
  193.               alien.curr_frame = ALIEN_START_LEFT;
  194.            }
  195.         else
  196.            {
  197.            // set state and current frame to right
  198.  
  199.            alien.state      = ALIEN_LEFT;
  200.            alien.curr_frame = ALIEN_START_LEFT;
  201.  
  202.            } // end else
  203.  
  204.         } // end if left
  205.  
  206.      // test for upward motion
  207.      else
  208.      if (keyboard_state[MAKE_UP])
  209.         {
  210.         // move alien
  211.  
  212.         if ((alien.y-=2) < -12)
  213.            alien.y=200;
  214.  
  215.         // first test if alien was already moving up
  216.  
  217.         if (alien.state==ALIEN_UP)
  218.            {
  219.            // animate and test for end of sequence
  220.  
  221.            if (++alien.curr_frame==ALIEN_END_UP)
  222.               alien.curr_frame = ALIEN_START_UP;
  223.            }
  224.         else
  225.            {
  226.            // set state and current frame to right
  227.  
  228.            alien.state      = ALIEN_UP;
  229.            alien.curr_frame = ALIEN_START_UP;
  230.  
  231.            } // end else
  232.  
  233.         } // end if up
  234.  
  235.      // test for downward motion
  236.      else
  237.      if (keyboard_state[MAKE_DOWN])
  238.         {
  239.         // move alien
  240.  
  241.         if ((alien.y+=2)>200)
  242.             alien.y=-12;
  243.  
  244.         // first test if alien was already moving down
  245.  
  246.         if (alien.state==ALIEN_DOWN)
  247.            {
  248.            // animate and test for end of sequence
  249.  
  250.            if (++alien.curr_frame==ALIEN_END_DOWN)
  251.               alien.curr_frame = ALIEN_START_DOWN;
  252.  
  253.            }
  254.         else
  255.            {
  256.            // set state and current frame to right
  257.  
  258.            alien.state      = ALIEN_DOWN;
  259.            alien.curr_frame = ALIEN_START_DOWN;
  260.  
  261.            } // end else
  262.  
  263.         } // end if down
  264.  
  265.      // test for tracking toggle
  266.  
  267.      if (keyboard_state[MAKE_SPACE])
  268.         {
  269.         chase=-chase;
  270.         while(keyboard_state[MAKE_SPACE]);
  271.         }
  272.  
  273.      // test for exit key
  274.  
  275.      if (keyboard_state[MAKE_Q])
  276.         done=1;
  277.  
  278. // BEGIN TRACKING LOGIC ///////////////////////////////////////////////////////
  279.  
  280.      // move creature, test if creature is chasing or evading player
  281.  
  282.      if (chase==1)
  283.         {
  284.         // track on x coordinate
  285.  
  286.         if (alien.x > creature.x)
  287.            creature.x++;
  288.         else
  289.         if (alien.x < creature.x)
  290.            creature.x--;
  291.  
  292.         // now track on y coordinate
  293.  
  294.         if (alien.y > creature.y)
  295.            creature.y++;
  296.         else
  297.         if (alien.y < creature.y)
  298.            creature.y--;
  299.  
  300.         } // end if chase
  301.      else
  302.         {
  303.         // must be evading
  304.  
  305.         // evade on x coordinate
  306.  
  307.         if (alien.x < creature.x)
  308.            creature.x++;
  309.         else
  310.         if (alien.x > creature.x)
  311.            creature.x--;
  312.  
  313.         // now evade on y coordinate
  314.  
  315.         if (alien.y < creature.y)
  316.            creature.y++;
  317.         else
  318.         if (alien.y > creature.y)
  319.            creature.y--;
  320.  
  321.         } // end else
  322.  
  323.      // test of creature has moved off screen
  324.  
  325.      if (creature.x>310)
  326.         creature.x = 310;
  327.      else
  328.      if (creature.x<-14)
  329.         creature.x = -14;
  330.  
  331.       if (creature.y>190)
  332.          creature.y = 190;
  333.       else
  334.       if (creature.y<-2)
  335.          creature.y = -2;
  336.  
  337. // END TRACKING LOGIC /////////////////////////////////////////////////////////
  338.  
  339.      // do animation for creature
  340.  
  341.      if (++creature.curr_frame==4)
  342.         creature.curr_frame=0;
  343.  
  344.      // ready to draw objects, but first scan background under them
  345.  
  346.      Sprite_Under_Clip((sprite_ptr)&alien,double_buffer);
  347.      Sprite_Under_Clip((sprite_ptr)&creature,double_buffer);
  348.  
  349.      Sprite_Draw_Clip((sprite_ptr)&alien,double_buffer,1);
  350.      Sprite_Draw_Clip((sprite_ptr)&creature,double_buffer,1);
  351.  
  352.      // display text message of current tracking mode
  353.  
  354.      if (chase==1)
  355.         sprintf(buffer,"Creature is Chasing!!!");
  356.      else
  357.         sprintf(buffer,"Creature is Evading!!!");
  358.  
  359.      Print_String_DB(64,190,12,buffer,0);
  360.  
  361.      // display double buffer
  362.  
  363.      Display_Double_Buffer(double_buffer,0);
  364.  
  365.      // lock onto 18 frames per second max
  366.  
  367.      Time_Delay(1);
  368.  
  369.      } // end while
  370.  
  371. // exit in a very cool way
  372.  
  373. Screen_Transition(SCREEN_WHITENESS);
  374.  
  375. // free up all resources
  376.  
  377. Sprite_Delete((sprite_ptr)&alien);
  378. Sprite_Delete((sprite_ptr)&creature);
  379. Delete_Double_Buffer();
  380.  
  381. Set_Graphics_Mode(TEXT_MODE);
  382.  
  383. Keyboard_Remove_Driver();
  384.  
  385. } // end main
  386.  
  387.