rnd-19970921-src
[rocksndiamonds.git] / src / misc.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  ©1995 Artsoft Development                               *
5 *        Holger Schemel                                    *
6 *        33659 Bielefeld-Senne                             *
7 *        Telefon: (0521) 493245                            *
8 *        eMail: aeglos@valinor.owl.de                      *
9 *               aeglos@uni-paderborn.de                    *
10 *               q99492@pbhrzx.uni-paderborn.de             *
11 *----------------------------------------------------------*
12 *  misc.c                                                  *
13 ***********************************************************/
14
15 #include "misc.h"
16 #include "tools.h"
17 #include "sound.h"
18 #include "random.h"
19
20 #include <pwd.h>
21 #include <unistd.h>
22 #include <time.h>
23 #include <sys/time.h>
24 #include <sys/param.h>
25 #include <sys/types.h>
26
27 void microsleep(unsigned long usec)
28 {
29   struct timeval delay;
30
31   delay.tv_sec  = usec / 1000000;
32   delay.tv_usec = usec % 1000000;
33
34   if (select(0,NULL,NULL,NULL,&delay)!=0)
35     fprintf(stderr,"%s: in function microsleep: select failed!\n",
36             progname);
37 }
38
39 long mainCounter(int mode)
40 {
41   static struct timeval base_time = { 0, 0 };
42   struct timeval current_time;
43   long counter_ms;
44
45   gettimeofday(&current_time,NULL);
46   if (mode == INIT_COUNTER || current_time.tv_sec < base_time.tv_sec)
47     base_time = current_time;
48
49   counter_ms = (current_time.tv_sec - base_time.tv_sec)*1000
50              + (current_time.tv_usec - base_time.tv_usec)/1000;
51
52   if (mode == READ_COUNTER_100)
53     return(counter_ms/10);      /* return 1/100 secs since last init */
54   else  /*    READ_COUNTER_1000 */
55     return(counter_ms);         /* return 1/1000 secs since last init */
56 }
57
58 void InitCounter() /* set counter back to zero */
59 {
60   mainCounter(INIT_COUNTER);
61 }
62
63 long Counter()  /* returns 1/100 secs since last call of InitCounter() */
64 {
65   return(mainCounter(READ_COUNTER_100));
66 }
67
68 long Counter2() /* returns 1/1000 secs since last call of InitCounter() */
69 {
70   return(mainCounter(READ_COUNTER_1000));
71 }
72
73 void WaitCounter(long value)    /* wait for counter to reach value */
74 {
75   long wait;
76
77   while((wait=value-Counter())>0)
78     microsleep(wait*10000);
79 }
80
81 void WaitCounter2(long value)   /* wait for counter to reach value */
82 {
83   long wait;
84
85   while((wait=value-Counter2())>0)
86     microsleep(wait*1000);
87 }
88
89 void Delay(long value)
90 {
91   microsleep(value);
92 }
93
94 BOOL DelayReached(long *counter_var, int delay)
95 {
96   long actual_counter = Counter();
97
98   if (actual_counter >= *counter_var+delay || actual_counter < *counter_var)
99   {
100     *counter_var = actual_counter;
101     return(TRUE);
102   }
103   else
104     return(FALSE);
105 }
106
107 BOOL FrameReached(long *frame_counter_var, int frame_delay)
108 {
109   long actual_frame_counter = FrameCounter;
110
111   if (actual_frame_counter >= *frame_counter_var+frame_delay
112       || actual_frame_counter < *frame_counter_var)
113   {
114     *frame_counter_var = actual_frame_counter;
115     return(TRUE);
116   }
117   else
118     return(FALSE);
119 }
120
121 char *int2str(int ct, int nr)
122 {
123   static char str[20];
124
125   sprintf(str,"%09d",ct);
126   return(&str[strlen(str)-nr]);
127 }
128
129 unsigned int SimpleRND(unsigned int max)
130 {
131   static unsigned long root = 654321;
132   struct timeval current_time;
133
134   gettimeofday(&current_time,NULL);
135   root = root * 4253261 + current_time.tv_sec + current_time.tv_usec;
136   return(root % max);
137 }
138
139 unsigned int RND(unsigned int max)
140 {
141   return(random_linux_libc() % max);
142 }
143
144 unsigned int InitRND(long seed)
145 {
146   struct timeval current_time;
147
148   if (seed==NEW_RANDOMIZE)
149   {
150     gettimeofday(&current_time,NULL);
151     srandom_linux_libc((unsigned int) current_time.tv_usec);
152     return((unsigned int) current_time.tv_usec);
153   }
154   else
155   {
156     srandom_linux_libc((unsigned int) seed);
157     return((unsigned int) seed);
158   }
159 }
160
161 char *GetLoginName()
162 {
163   struct passwd *pwd;
164
165   if (!(pwd=getpwuid(getuid())))
166     return("ANONYMOUS");
167   else
168     return(pwd->pw_name);
169 }