home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xpool-10.zip / Xpool / coordinator.c < prev    next >
C/C++ Source or Header  |  1992-05-26  |  14KB  |  471 lines

  1.  
  2. /* I. ARIT 1992 Hidirbeyli,AYDIN,TR.  09400
  3.                 Golden,    CO,   USA. 80401
  4.  
  5.  
  6.   Copyright (C) 1992 Ismail ARIT
  7.  
  8.   This file  is distributed in the hope that it will be useful,
  9.   but without any warranty.  No author or distributor accepts
  10.   responsibility to anyone for the consequences of using it or for
  11.   whether it serves any particular purpose or works at all.
  12.  
  13.  
  14.   Everyone is granted permission to copy, modify and redistribute
  15.   this file under the following conditions:
  16.  
  17.      Permission is granted to anyone to make or distribute copies
  18.      of the source code, either as received or modified, in any
  19.      medium, provided that all copyright notices, permission and
  20.      nonwarranty notices are preserved, and that the distributor
  21.      grants the recipient permission for further redistribution as
  22.      permitted by this document.
  23.      No part of this program can be used in any commercial product.
  24.  
  25.  
  26. */
  27.  
  28.  
  29.  
  30. #include <stdio.h>
  31. #include <math.h>
  32. #include <malloc.h>
  33. #include    <X11/Xlib.h>
  34. #include    <X11/Xutil.h>
  35.  
  36. #include "definition.h"
  37.  
  38. extern int  WhiteBallOut,
  39.             BlackBallOut;
  40. extern  Display disp;
  41. extern  Window Pane;
  42. extern  GC gc;
  43. extern  void ClearTable (void);
  44. extern  Distance ThisMuch;
  45.  
  46.  
  47. extern  void Force_ToObject (Ball * first, Distance * TheirDistance, int type);
  48. extern  Distance * DistanceChecker_Between (Ball * ThisBall, Ball * ThatBall);
  49. extern  setbkcolor (int color);
  50. extern int  get_color (char *name);
  51. extern  void setcolor (int color);
  52. extern  Fillcircle (int x, int y, int rad);
  53. extern  void Fillrectangle (int x, int y, int xx, int yy);
  54. extern  void SyncDisplay (void);
  55. extern  Ball * new__Ball (double Mass, double radious, float initX, float initY, double initVx, double initVy, double initVr);
  56. extern  void LayTheFloor (void);
  57.  
  58. static  void
  59.         Coordinator__GetGameType (Coordinator * this, int Type) {
  60.             this -> GameType = Type;
  61. };
  62.  
  63.  
  64. static  void
  65.         Coordinator__ShowStaticPosition (Coordinator * this) {
  66.     int     i;
  67.     for (i = 0; i < this -> NumberOfObjects; i++)
  68.     this -> Objects[i] -> Show (this -> Objects[i]);
  69. }
  70.  
  71. static  void
  72.         Coordinator__HideStaticPosition (Coordinator * this) {
  73.     int     i;
  74.     for (i = 0; i < this -> NumberOfObjects; i++)
  75.     this -> Objects[i] -> Hide (this -> Objects[i]);
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. static  void
  83.         Coordinator__OpenSpaceForObjects (Coordinator * this) {
  84. /* eliminate this */
  85. /* this -> Objects = (Ball *) malloc (this -> NumberOfObjects * sizeof (Ball));
  86. */
  87. };
  88.  
  89.  
  90. static int  Coordinator__RegisterThisObject (Coordinator * this, Ball * AnotherBall) {
  91.                 this -> ObjectCounter++;
  92.     if (this -> ObjectCounter > this -> NumberOfObjects) {
  93.     printf ("To many Objects\n");
  94.     exit (0);
  95.     }
  96.     this -> Objects[this -> ObjectCounter] = AnotherBall;
  97.     this -> Objects[this -> ObjectCounter] -> MyID = this -> ObjectCounter;
  98.     return (this -> ObjectCounter);
  99. };
  100.  
  101. static  void Coordinator__GetTime (Coordinator * this, Timing time) {
  102.     int     i;
  103.     this -> mytime.currenttime = 0.000000000;
  104.     this -> mytime.totaltime = time.totaltime;
  105.     this -> mytime.dt = time.dt;
  106.  
  107.     for (i = 0; i < this -> NumberOfObjects; i++) {
  108.     this -> Objects[i] -> mytime.currenttime = this -> mytime.currenttime;
  109.     this -> Objects[i] -> mytime.totaltime = this -> mytime.totaltime;
  110.     this -> Objects[i] -> mytime.dt = this -> mytime.dt;
  111.     }
  112.  
  113. };
  114.  
  115.  
  116.  
  117.  
  118.  
  119. static  void Coordinator__AllowableBump (Coordinator * this, float bump) {
  120.             this -> AllowableDmax = bump;
  121. };
  122.  
  123.  
  124.  
  125. static  void Coordinator__UpDateObjectsInitials (Coordinator * this) {
  126.     int     i;
  127.  
  128.     /* dprintf ("Coordinator::UpDateObjectsInitials()..\n"); */
  129.     for (i = 0; i < this -> NumberOfObjects; i++) {
  130.     this -> Objects[i] -> CurrentF.x = 0.00000000;
  131.     this -> Objects[i] -> CurrentF.y = 0.00000000;
  132.     }
  133. };
  134.  
  135.  
  136.  
  137. static  void Coordinator__GetBoundary (Coordinator * this, Space limits) {
  138.             this -> myspace.xmin = limits.xmin;
  139.     this -> myspace.ymin = limits.ymin;
  140.     this -> myspace.xmax = limits.xmax;
  141.     this -> myspace.ymax = limits.ymax;
  142.  
  143.  
  144.  
  145. /* I 'll just use ball class to create pockets*/
  146.     this -> Pocket[0] = new__Ball (0, POCKETRADIOUS, this -> myspace.xmin, this -> myspace.ymin, 0, 0, 0);
  147.     this -> Pocket[1] = new__Ball (0, POCKETRADIOUS, this -> myspace.xmin, this -> myspace.ymax, 0, 0, 0);
  148.  
  149.     this -> Pocket[2] = new__Ball (0, POCKETRADIOUS, this -> myspace.xmax, this -> myspace.ymin, 0, 0, 0);
  150.     this -> Pocket[3] = new__Ball (0, POCKETRADIOUS, this -> myspace.xmax, this -> myspace.ymax, 0, 0, 0);
  151.     this -> Pocket[4] = new__Ball (0, POCKETRADIOUS, this -> myspace.xmin + (this -> myspace.xmax - this -> myspace.xmin) / 2, this -> myspace.ymin, 0, 0, 0);
  152.     this -> Pocket[5] = new__Ball (0, POCKETRADIOUS, this -> myspace.xmin + (this -> myspace.xmax - this -> myspace.xmin) / 2, this -> myspace.ymax, 0, 0, 0);
  153.  
  154. /* these are for debuging , they don't mean anything*/
  155.     this -> Pocket[0] -> MyID = 20;
  156.     this -> Pocket[1] -> MyID = 21;
  157.     this -> Pocket[2] -> MyID = 22;
  158.     this -> Pocket[3] -> MyID = 23;
  159.     this -> Pocket[4] -> MyID = 24;
  160.     this -> Pocket[5] -> MyID = 25;
  161.  
  162.  
  163. };
  164.  
  165. static  void Coordinator__DrawPockets (Coordinator * this) {
  166.     int     i;
  167.  LayTheFloor();
  168. /* let's draw table */
  169.     Fillrectangle (this -> Pocket[0] -> CurrentL.x - POCKETRADIOUS,
  170.         this -> Pocket[0] -> CurrentL.y - POCKETRADIOUS,
  171.         this -> Pocket[3] -> CurrentL.x + POCKETRADIOUS * 2,
  172.         this -> Pocket[3] -> CurrentL.y + POCKETRADIOUS * 2);
  173.  
  174.     for (i = 0; i < 6; i++)
  175.     Fillcircle (this -> Pocket[i] -> CurrentL.x, this -> Pocket[i] -> CurrentL.y, this -> Pocket[i] -> radius);
  176.  
  177.     SyncDisplay ();
  178.  
  179. };
  180.  
  181.  
  182. static  void Coordinator__CheckPockets (Coordinator * this) {
  183.     int     i,
  184.             j;
  185.     Distance * theDistance;
  186.     for (i = 0; i < 6; i++)    /* we got 6 pockets */
  187.     for (j = 0; j < this -> NumberOfObjects; j++) {
  188.         theDistance = DistanceChecker_Between (this -> Pocket[i], this -> Objects[j]);
  189.  
  190. /* this is where we check the balls with the pockets */
  191.         if (theDistance -> amount > this -> Objects[j] -> radius) {
  192.         this -> Objects[j] -> In = NO;/* you are out Pal!.. */
  193. /* just put it away */
  194. this -> Objects[j] -> CurrentL.x = this -> Objects[j] -> MyID * 40 + 1000;
  195.         this -> Objects[j] -> CurrentL.y = 1000;
  196.         this -> Objects[j] -> CurrentV.x = 0.000;
  197.         this -> Objects[j] -> CurrentV.y = 0.000;
  198.  
  199.  
  200.         }
  201.     }
  202. };
  203.  
  204.  
  205. static  void Coordinator__CheckInitialLocations (Coordinator * this) {
  206.     int     j,
  207.             i;
  208.     Distance * theDistance;
  209.  
  210.     for (i = 0; i < this -> NumberOfObjects - 1; i++)
  211.     for (j = i + 1; j < this -> NumberOfObjects; j++) {
  212.         theDistance = DistanceChecker_Between (this -> Objects[i], this -> Objects[j]);
  213.         if (theDistance -> amount > 0) {
  214.         printf ("Overlaping objects %d and %d \n", i, j);
  215.         exit (0);
  216.         }
  217.     }
  218.  
  219. };
  220.  
  221.  
  222. static  void Coordinator__AdvanceOneStep (Coordinator * this) {
  223.  
  224.     int     j = 0,
  225.             i;
  226.     Distance * theDistance;
  227.  
  228. /*    dprintf ("Coordinator::AdvanceOneStep()..\n"); */
  229.     for (i = 0; i < this -> NumberOfObjects - 1; i++)
  230.     for (j = i + 1; j < this -> NumberOfObjects; j++) {
  231.         theDistance = DistanceChecker_Between (this -> Objects[i], this -> Objects[j]);
  232.         if (theDistance -> amount > 0) {
  233.  
  234.         if (theDistance -> amount > 5)
  235.             printf ("too much overlap %d %d \n", i, j);
  236.  
  237.         /* apply reaction */
  238.         Force_ToObject (this -> Objects[i], theDistance, REACTION);
  239.         /* apply action */
  240.         Force_ToObject (this -> Objects[j], theDistance, ACTION);
  241.  
  242.         }
  243.     }
  244.  
  245.  
  246.  
  247. };
  248.  
  249.  
  250.  
  251. static  void Coordinator__CheckObjectsForTheSpace (Coordinator * this) {
  252.     int     i;
  253. /*  don't get confused, this is just to confine balls on the table */
  254.  
  255.     for (i = 0; i < this -> NumberOfObjects; i++) {
  256.     if (
  257.         (this -> Objects[i] -> CurrentL.x + this -> Objects[i] -> radius) >= this -> myspace.xmax ||
  258.         (this -> Objects[i] -> CurrentL.x - this -> Objects[i] -> radius) < this -> myspace.xmin
  259.         )
  260.         this -> Objects[i] -> CurrentV.x *= -1;
  261.     if (
  262.         (this -> Objects[i] -> CurrentL.y + this -> Objects[i] -> radius) >= this -> myspace.ymax ||
  263.         (this -> Objects[i] -> CurrentL.y - this -> Objects[i] -> radius) < this -> myspace.ymin
  264.         )
  265.         this -> Objects[i] -> CurrentV.y *= -1;
  266.  
  267.     }
  268.  
  269.  
  270. };
  271.  
  272.  
  273.  
  274.  
  275.  
  276. static  void Coordinator__CheckSpeedsAndCalculateK (Coordinator * this) {
  277.     double  MaxSpeed = 0.00000,
  278.             CorrespondingMass = 0.00000;
  279.     int     i;
  280.  
  281.  
  282.     if (this -> AllowableDmax == 0.0000000) {
  283.     printf ("\n Missing Dmax, Not able to calculate K, Exiting....\n");
  284.     exit (0);
  285.     }
  286.     MaxSpeed = SPEEDLIMIT;
  287.     CorrespondingMass = PAYLOAD;
  288.  
  289.  
  290.     this -> kConstant = CorrespondingMass * pow (MaxSpeed, 2.) / pow (this -> AllowableDmax, 2.);
  291.  
  292.     for (i = 0; i < this -> NumberOfObjects; i++)
  293.     this -> Objects[i] -> kConstant = this -> kConstant;
  294.  
  295.  
  296.     dprintf ("Max Speed = %f  kKons %f Dmax %f \n", MaxSpeed,
  297.         this -> kConstant,
  298.         this -> AllowableDmax);
  299.  
  300. };
  301.  
  302.  
  303.  
  304. static  void Coordinator__ShowObjects (Coordinator * this) {
  305.     int     i;
  306.     for (i = 0; i < this -> NumberOfObjects; i++)
  307.     this -> Objects[i] -> MoveToNextLocation (this -> Objects[i]);
  308. };
  309.  
  310. /* another quick fix to mix the balls */
  311. int which[15]={2,15,3,14,4,13,5,12,1,11,7,10,8,9,0};
  312.  
  313. static  void Coordinator__ArrangeBalls (Coordinator * this) {
  314.     int     i,
  315.             j,
  316.             counter = 0,
  317.             ii;
  318.  
  319.  
  320. /* this is what happens when you click on 'newgame' */
  321.  
  322. /* for white ball */
  323.     this -> Objects[0] -> CurrentL.x = 600;
  324.     this -> Objects[0] -> CurrentL.y = 230;
  325. /* black ball  will be somewhere in the middle*/
  326.     this -> Objects[6] -> CurrentL.x = 200;
  327.     this -> Objects[6] -> CurrentL.y = 230;
  328.  
  329. /* for the others */
  330.     for (j = 0; j < 2; j++)
  331.     for (i = 0; i < 4; i++) {
  332.  
  333.         ii = (j == 0) ? (i + 1) : (-i - 1);
  334.         this -> Objects[which[counter]] -> CurrentL.x = 200 - (i + 1) * 27;
  335.         this -> Objects[which[counter]] -> CurrentL.y = 230 - (ii) * 20;
  336.         counter++;
  337.  
  338.     }
  339.  
  340.     this -> Objects[which[counter]] -> CurrentL.x = 200 - 54;
  341.     this -> Objects[which[counter]] -> CurrentL.y = 230;
  342.     counter++;
  343.  
  344.     for (j = 0; j < 2; j++)
  345.     for (i = 0; i < 2; i++) {
  346.  
  347.         ii = (j == 0) ? (i + 1) : (-i - 1);
  348.         this -> Objects[which[counter]] -> CurrentL.x = 145 - (i + 1) * 27;
  349.         this -> Objects[which[counter]] -> CurrentL.y = 230 - (ii) * 20;
  350.         counter++;
  351.  
  352.     }
  353.  
  354.     this -> Objects[which[counter]] -> CurrentL.x = 90;
  355.     this -> Objects[which[counter]] -> CurrentL.y = 230;
  356.  
  357.  
  358.  
  359.  
  360.  
  361. };
  362.  
  363. static  void Coordinator__StillMoving (Coordinator * this) {
  364.     int     i;
  365.     int     Movelist = 0;
  366.     for (i = 0; i < this -> NumberOfObjects; i++) {
  367.     if (this -> Objects[i] -> Stopped == NO)
  368.         Movelist++;
  369.     }
  370.  
  371.     if (Movelist)
  372.     this -> AllStopped = NO;
  373.     else
  374.     this -> AllStopped = YES;
  375.  
  376. };
  377.  
  378. static  void Coordinator__InitConfig (Coordinator * this) {
  379.     int     i;
  380.     this->DrawPockets(this);
  381.     ClearTable ();
  382.     setbkcolor (get_color (PANE_BG));
  383.     if (this -> GameType == SIXTEENBALL)
  384.     this -> ArrangeBalls (this);
  385.     this -> CheckInitialLocations (this);
  386.     this -> ShowStaticPosition (this);
  387.     for (i = 0; i < this -> NumberOfObjects; i++) {
  388.     this -> Objects[i] -> Stopped = NO;
  389.     this -> Objects[i] -> In = YES;
  390.     this -> Objects[i] -> ShowOut = NO;
  391.     this -> Objects[i] -> NeedOneMoreDrawing = YES;
  392.     }
  393.     WhiteBallOut = BlackBallOut = NO;
  394.  
  395. };
  396.  
  397.  
  398.  
  399. static  void Coordinator__StartAction (Coordinator * this) {
  400.  
  401.     int Counter = 0;
  402.  
  403.     this -> mytime.currenttime = 0.000;
  404.     this -> AllStopped = NO;
  405.  
  406.       /*  dprintf ("Coordinator::StartAction()..\n");*/
  407.     while (!this -> AllStopped) {
  408.     this -> CheckObjectsForTheSpace (this);/* table boundaries */
  409.     this -> UpDateObjectsInitials (this);/* init forces */
  410.     this -> AdvanceOneStep (this);/* do checkups and advance */
  411.     this -> mytime.currenttime += this -> mytime.dt;
  412.     this -> ShowObjects (this);/* show balls */
  413.     this -> CheckPockets (this);/* new locations in pockets ? */
  414.     Counter++;
  415.     if (!(Counter % CHECKMOVEDENSITY))
  416.         this -> StillMoving (this);/* make sure everybody stopped */
  417.  
  418.     }
  419.  
  420. };
  421.  
  422.  
  423.  
  424.  
  425.  
  426. Coordinator * new__Coordinator (int HowManyObjects) {
  427.     Coordinator * A_Coordinator;
  428.  
  429.     if ((A_Coordinator = (Coordinator *) malloc (sizeof (Coordinator))) == NULL) {
  430.     printf ("problem..\n");
  431.     exit (0);
  432.     };
  433.  
  434.     A_Coordinator -> NumberOfObjects = HowManyObjects;
  435.     A_Coordinator -> AllStopped = NO;
  436.     A_Coordinator -> ObjectCounter = -1;
  437.     A_Coordinator -> Counter = 0;
  438.  
  439.  
  440.     A_Coordinator -> AllowableDmax = 2.000000;
  441.     A_Coordinator -> GetGameType = Coordinator__GetGameType;
  442.     A_Coordinator -> UpDateObjectsInitials = Coordinator__UpDateObjectsInitials;
  443.     A_Coordinator -> GetTime = Coordinator__GetTime;
  444.     A_Coordinator -> OpenSpaceForObjects = Coordinator__OpenSpaceForObjects;
  445.     A_Coordinator -> RegisterThisObject = Coordinator__RegisterThisObject;
  446.     A_Coordinator -> CheckInitialLocations = Coordinator__CheckInitialLocations;
  447.     A_Coordinator -> AdvanceOneStep = Coordinator__AdvanceOneStep;
  448.     A_Coordinator -> StartAction = Coordinator__StartAction;
  449.     A_Coordinator -> GetBoundary = Coordinator__GetBoundary;
  450.     A_Coordinator -> CheckSpeedsAndCalculateK = Coordinator__CheckSpeedsAndCalculateK;
  451.     A_Coordinator -> CheckObjectsForTheSpace = Coordinator__CheckObjectsForTheSpace;
  452.     A_Coordinator -> ShowObjects = Coordinator__ShowObjects;
  453.     A_Coordinator -> AllowableBump = Coordinator__AllowableBump;
  454.     A_Coordinator -> ArrangeBalls = Coordinator__ArrangeBalls;
  455.     A_Coordinator -> ShowStaticPosition = Coordinator__ShowStaticPosition;
  456.     A_Coordinator -> HideStaticPosition = Coordinator__HideStaticPosition;
  457.     A_Coordinator -> DrawPockets = Coordinator__DrawPockets;
  458.     A_Coordinator -> CheckPockets = Coordinator__CheckPockets;
  459.     A_Coordinator -> StillMoving = Coordinator__StillMoving;
  460.     A_Coordinator -> InitConfig = Coordinator__InitConfig;
  461.  
  462.  
  463.     return (A_Coordinator);
  464.  
  465. };
  466.  
  467.  
  468.  
  469.  
  470.  
  471.