home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / MAILBOX.ZIP / MPTHDEX2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-27  |  4.8 KB  |  141 lines

  1. /****************************************************************************/
  2. /* mpthdex2.c - THREAD CLUSTER EXAMPLE 2                    */
  3. /* Created:  10/31/87        Release:  0.7        Version:  06/27/88  */
  4. /****************************************************************************
  5. (c) Copyright 1987, 1988 by Michael Benjamin Parker     (USA SS# 557-49-4130)
  6.  
  7. All Rights Reserved unless specified in the following include files: */
  8. #include "mptsk.cpy" /*
  9.  
  10. DO NOT REMOVE OR ALTER THIS NOTICE AND ITS PROVISIONS.
  11. ****************************************************************************/
  12. #include "mpthd.h"
  13. #include <stdio.h>
  14. #include <math.h>
  15. /****************************************************************************/
  16. /* OVERVIEW:            - Idea Suggested by Terry M. Donahue
  17. This code illustrates how a multiprogram tank game could be built.
  18. Entrants submit "C" programs controlling their tanks.  The programs
  19. are each autonomous and think they own the whole machine.  They call
  20. primitive, universal tank functions to move their tanks, and check the
  21. status of other tanks.  Through the memory shared by these functions,
  22. it's easy for the tanks to communicate.
  23.  
  24. When they make these calls, the functions switch control to the refree
  25. thread.  The refree can start up other tanks which have had less than
  26. their share of CPU time.  This way, the tanks effectively have the same
  27. time to execute, yet can be thought of independently.
  28.  
  29. Of course, in a real game, the refree would also have to stop and display
  30. the game, keep score (damage), and stop tanks which were destroyed.
  31.  
  32. Want to make a tank game?  Well here's the start of one...!  */
  33. /****************************************************************************/
  34. /****************************************************************************/
  35. typedef    struct _TANK    TANK, PTR TANKID;
  36.  
  37. struct _TANK {
  38.     SINT    x,y;        /* TANK'S COORDINANTS */
  39.     double    heading;    /* HEADING OF TANK (in radians) */
  40.     long    timespent;    /* TIME THE TANK HAS SPENT */
  41.     SINT    damage;        /* TANK'S DAMAGE */
  42.     /* ETC */
  43.  
  44.     MPTHDID    mpthd;        /* STATE OF THE TANK PROGRAM */
  45. };
  46.  
  47. MPTHDID    tank_referee;        /* REFEREE FOR ALL TANKS */
  48. TANKID    _tank_me;        /* CURRENT TANK EVALUATING */
  49.  
  50. #define    tank_me()        (_tank_me)
  51. /****************************************************************************/
  52. void    tank_move(distance)
  53.     int    distance;
  54. {
  55.     printf("\ttank_move(%d);",distance);
  56.     tank_me()->x+=    distance*cos(tank_me()->heading);
  57.     tank_me()->y+=    distance*sin(tank_me()->heading);
  58.     mpthd_switch(tank_referee);
  59. }
  60. /****************************************************************************/
  61. void    tank_turn(radians)
  62.     double    radians;
  63. {
  64.     printf("\ttank_turn(%d);",radians);
  65.     tank_me()->heading+=    radians;
  66.     mpthd_switch(tank_referee);
  67. }
  68. /****************************************************************************/
  69. MPTHDID    tank0fn(camefrom)
  70.     MPTHDID    camefrom;
  71. {
  72.     tank_turn((double).23);
  73.     tank_move(4);
  74.     tank_turn((double)-.3);
  75.     tank_move(3);
  76.     return(mpthd_me());
  77. }
  78. /****************************************************************************/
  79. MPTHDID    tank1fn(camefrom)
  80.     MPTHDID    camefrom;
  81. {
  82.     tank_turn((double).45);
  83.     tank_move(-3);
  84.     return(mpthd_me());
  85. }
  86. /****************************************************************************/
  87. /****************************************************************************/
  88. #define    NOOFTANKS    (2)
  89.  
  90. void    main()
  91. {
  92.     int    tankno;
  93.     TANK    tanks[NOOFTANKS];
  94.     char    buffs[NOOFTANKS][1000];    /* NOTE STACK BUFFER */
  95.  
  96.     printf("\nmpthdex2.c - MPTHD EXAMPLE PROGRAM 2 - TANK BATTLES!\n");
  97.  
  98.     printf("\nInitializing %d Tanks...",NOOFTANKS);
  99.     mpthd_setup();
  100.     tank_referee=    mpthd_me();
  101.     printf("setup: %ld\n",ptr_2int(tank_referee));
  102.     tanks[0].mpthd=    mpthd_init(buffs[0],1000,tank0fn);
  103.     tanks[1].mpthd=    mpthd_init(buffs[1],1000,tank1fn);
  104.  
  105.     for (tankno= 0; tankno < NOOFTANKS; ++tankno) {
  106.         tanks[tankno].x=    tanks[tankno].y=    0;
  107.         tanks[tankno].heading=                0;
  108.         tanks[tankno].timespent=            0;
  109.         tanks[tankno].damage=                0;
  110.     }
  111.  
  112.     printf("\nBattle Begins...(^C to Stop)");
  113.     {
  114.         long    starttime=    time(0);
  115.         long    stoptime;
  116.         long    difftime;
  117.         long    totaltime=    0;
  118.  
  119.         for (EVER) {
  120.             for (tankno= 0; tankno < NOOFTANKS; ++tankno) {
  121.                 printf("\n\tRunning Tank %d:",tankno);
  122.                 tank_me()=    &tanks[tankno];
  123.                 if (tank_me()->timespent > /* AVG TIME */
  124.                     (totaltime/NOOFTANKS))
  125.                     continue;
  126.                 tank_me()->mpthd=
  127.                     mpthd_switch(tank_me()->mpthd);
  128.                 stoptime=    time(0);
  129.                 difftime=    stoptime - starttime;
  130.                 tank_me()->timespent+=    difftime;
  131.                 totaltime+=        difftime;
  132.                 starttime=        stoptime;
  133.             }
  134.         }
  135.  
  136.     }
  137. }
  138. /****************************************************************************/
  139. /****************************************************************************/
  140. /****************************************************************************/
  141.