home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume15 / xtb / part01 / common.c next >
C/C++ Source or Header  |  1993-01-27  |  2KB  |  108 lines

  1. /*
  2.  * Common code.
  3.  *
  4.  * $Header: /morpork/home/bmh/xtest2/RCS/common.c,v 1.2 92/10/19 15:34:25 bmh Exp Locker: bmh $
  5.  *
  6.  * Bernard Hatt
  7.  * Camtec Electronics (Ericsson), Leicester, England, LE1 4SA
  8.  * bmh@terminus.ericsson.se
  9.  *
  10.  */
  11.          
  12. #include <stdio.h>
  13.  
  14.  
  15. #ifdef BSD
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <fcntl.h>
  19. #else
  20. #include <sys/types.h>
  21. #include <sys/timeb.h>
  22. #include <sys/stat.h>
  23. #include <fcntl.h>
  24.  
  25. #endif
  26.  
  27. #include <math.h>
  28. #include "defs.h"
  29. #include "comms.h"
  30.  
  31. #ifdef BSD
  32.  
  33. struct timezone tz={0,0};
  34. struct timeval tv;
  35. long lastsec=0;
  36.     /* return time in cs ( will fail if test runs for more than */
  37.     /* 240 days (2^31/(60*60*24*100)) */
  38.  
  39. int
  40. gettime()
  41. {
  42.     gettimeofday(&tv,&tz);
  43.     if(lastsec==0)
  44.         lastsec=tv.tv_sec;
  45.     return((tv.tv_sec-lastsec)*100+(tv.tv_usec/10000));
  46. }
  47.  
  48. #else
  49.  
  50. struct timeb tb;
  51. long lastsec=0;
  52.  
  53. int
  54. gettime()
  55. {
  56.         ftime(&tb);
  57.     if(lastsec==0)
  58.         lastsec=tb.time;
  59.     return((tb.time-lastsec)*100+tb.millitm/10);
  60. }
  61.  
  62. #endif
  63.  
  64. int
  65. rnd(n)  /* a random no. between 0 and n-1 */
  66. int n;
  67. {
  68.     return((rand()/153)%n);   
  69. }
  70.  
  71. void    /* because Ultrix doesn't provide the nice SunOS ualarm() */
  72. myualarm(val)
  73. int val;
  74. {
  75.     struct itimerval itv;
  76.     itv.it_interval.tv_sec=0; 
  77.     itv.it_interval.tv_usec=0;
  78.     itv.it_value.tv_sec=0;
  79.     itv.it_value.tv_usec=val;
  80.     setitimer(ITIMER_REAL,&itv,NULL);
  81. }
  82.  
  83. int
  84. GetAngle(x1,y1,x2,y2)    /* return an angle between two points */
  85. int x1,y1,x2,y2;
  86. {
  87.     double xc,yc,theta;
  88.     int ang;
  89.     xc=(double)(x1-x2);
  90.     yc=(double)(y1-y2);
  91.     if(xc==0.0)
  92.     {
  93.         if(yc>0)
  94.             theta=(PI/2.0);
  95.         else
  96.             theta=(-PI/2.0);
  97.     }
  98.     else
  99.     {
  100.         theta=atan2(yc,xc);
  101.     }
  102.     theta=(TANKROT*(theta/(2.0*PI)));
  103.     ang=(int)(theta+0.5+TANKROT)%TANKROT;
  104.     return(ang);
  105. }
  106.  
  107.  
  108.