home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* mpthdex2.c - THREAD CLUSTER EXAMPLE 2 */
- /* Created: 10/31/87 Release: 0.7 Version: 06/27/88 */
- /****************************************************************************
- (c) Copyright 1987, 1988 by Michael Benjamin Parker (USA SS# 557-49-4130)
-
- All Rights Reserved unless specified in the following include files: */
- #include "mptsk.cpy" /*
-
- DO NOT REMOVE OR ALTER THIS NOTICE AND ITS PROVISIONS.
- ****************************************************************************/
- #include "mpthd.h"
- #include <stdio.h>
- #include <math.h>
- /****************************************************************************/
- /* OVERVIEW: - Idea Suggested by Terry M. Donahue
- This code illustrates how a multiprogram tank game could be built.
- Entrants submit "C" programs controlling their tanks. The programs
- are each autonomous and think they own the whole machine. They call
- primitive, universal tank functions to move their tanks, and check the
- status of other tanks. Through the memory shared by these functions,
- it's easy for the tanks to communicate.
-
- When they make these calls, the functions switch control to the refree
- thread. The refree can start up other tanks which have had less than
- their share of CPU time. This way, the tanks effectively have the same
- time to execute, yet can be thought of independently.
-
- Of course, in a real game, the refree would also have to stop and display
- the game, keep score (damage), and stop tanks which were destroyed.
-
- Want to make a tank game? Well here's the start of one...! */
- /****************************************************************************/
- /****************************************************************************/
- typedef struct _TANK TANK, PTR TANKID;
-
- struct _TANK {
- SINT x,y; /* TANK'S COORDINANTS */
- double heading; /* HEADING OF TANK (in radians) */
- long timespent; /* TIME THE TANK HAS SPENT */
- SINT damage; /* TANK'S DAMAGE */
- /* ETC */
-
- MPTHDID mpthd; /* STATE OF THE TANK PROGRAM */
- };
-
- MPTHDID tank_referee; /* REFEREE FOR ALL TANKS */
- TANKID _tank_me; /* CURRENT TANK EVALUATING */
-
- #define tank_me() (_tank_me)
- /****************************************************************************/
- void tank_move(distance)
- int distance;
- {
- printf("\ttank_move(%d);",distance);
- tank_me()->x+= distance*cos(tank_me()->heading);
- tank_me()->y+= distance*sin(tank_me()->heading);
- mpthd_switch(tank_referee);
- }
- /****************************************************************************/
- void tank_turn(radians)
- double radians;
- {
- printf("\ttank_turn(%d);",radians);
- tank_me()->heading+= radians;
- mpthd_switch(tank_referee);
- }
- /****************************************************************************/
- MPTHDID tank0fn(camefrom)
- MPTHDID camefrom;
- {
- tank_turn((double).23);
- tank_move(4);
- tank_turn((double)-.3);
- tank_move(3);
- return(mpthd_me());
- }
- /****************************************************************************/
- MPTHDID tank1fn(camefrom)
- MPTHDID camefrom;
- {
- tank_turn((double).45);
- tank_move(-3);
- return(mpthd_me());
- }
- /****************************************************************************/
- /****************************************************************************/
- #define NOOFTANKS (2)
-
- void main()
- {
- int tankno;
- TANK tanks[NOOFTANKS];
- char buffs[NOOFTANKS][1000]; /* NOTE STACK BUFFER */
-
- printf("\nmpthdex2.c - MPTHD EXAMPLE PROGRAM 2 - TANK BATTLES!\n");
-
- printf("\nInitializing %d Tanks...",NOOFTANKS);
- mpthd_setup();
- tank_referee= mpthd_me();
- printf("setup: %ld\n",ptr_2int(tank_referee));
- tanks[0].mpthd= mpthd_init(buffs[0],1000,tank0fn);
- tanks[1].mpthd= mpthd_init(buffs[1],1000,tank1fn);
-
- for (tankno= 0; tankno < NOOFTANKS; ++tankno) {
- tanks[tankno].x= tanks[tankno].y= 0;
- tanks[tankno].heading= 0;
- tanks[tankno].timespent= 0;
- tanks[tankno].damage= 0;
- }
-
- printf("\nBattle Begins...(^C to Stop)");
- {
- long starttime= time(0);
- long stoptime;
- long difftime;
- long totaltime= 0;
-
- for (EVER) {
- for (tankno= 0; tankno < NOOFTANKS; ++tankno) {
- printf("\n\tRunning Tank %d:",tankno);
- tank_me()= &tanks[tankno];
- if (tank_me()->timespent > /* AVG TIME */
- (totaltime/NOOFTANKS))
- continue;
- tank_me()->mpthd=
- mpthd_switch(tank_me()->mpthd);
- stoptime= time(0);
- difftime= stoptime - starttime;
- tank_me()->timespent+= difftime;
- totaltime+= difftime;
- starttime= stoptime;
- }
- }
-
- }
- }
- /****************************************************************************/
- /****************************************************************************/
- /****************************************************************************/