1 /*****************************************************************************
2 ** This is part of the SpaceZero program
3 ** Copyright(C) 2006-2013 MRevenga
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License (version 3), or
7 ** (at your option) any later version, as published by the Free Software
8 ** Foundation.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ******************************************************************************/
19
20 /************* SpaceZero M.R.H. 2006-2013 ******************
21 Author: MRevenga
22 E-mail: mrevenga at users.sourceforge.net
23 version 0.86 December 2013
24 **************************************************************/
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <time.h>
28 #include <math.h>
29 #include "functions.h"
30 #include "general.h"
31 #include "sound.h"
32
Random(int a)33 float Random(int a){
34 /*
35 returns a random float between (0,1) from the table rtable
36 if a >= 0 set read index to a
37 if a= -2 return read index
38 if a= -3 recalc random values
39 */
40 static float rtable[1000];
41 static int n=0;
42 static int sw=0;
43
44 if(a>=0){
45 n=a;
46 if(n>=1000){
47 n=n%1000;
48 }
49 }
50 if(a==-2)return(n);
51 if(a==-3)sw=0;
52 if(sw==0){ /* create table */
53 int i;
54 for(i=0;i<1000;i++){
55 rtable[i]=(float)rand()/RAND_MAX;
56 }
57 n=0;
58 sw=1;
59 }
60 if(n>=1000)n=0;
61 return(rtable[n++]);
62 }
63
64
Proc(int option,int value)65 int Proc(int option,int value){
66 static int proc=0;
67
68 switch(option){
69
70 case SET:
71 proc=value;
72 break;
73 case GET:
74 return(proc);
75 break;
76 default:
77 fprintf(stderr,"ERROR in Proc(). Option not valid\n");
78 exit(-1);
79 break;
80 }
81 return(proc);
82 }
83
GetProc(void)84 int GetProc(void){
85 return(Proc(GET,0));
86 }
87
SetProc(int value)88 int SetProc(int value){
89 Proc(SET,value);
90 return(Proc(GET,0));
91 }
92
93
NProc(int option,int value)94 int NProc(int option,int value){
95 static int nproc=1;
96
97 switch(option){
98
99 case SET:
100 nproc=value;
101 break;
102 case GET:
103 return(nproc);
104 break;
105 default:
106 fprintf(stderr,"ERROR in NProc(). Option not valid\n");
107 exit(-1);
108 break;
109 }
110 return(nproc);
111 }
112
113
GetNProc(void)114 int GetNProc(void){
115 return(NProc(GET,0));
116 }
117
SetNProc(int value)118 int SetNProc(int value){
119 NProc(SET,value);
120 return(NProc(GET,0));
121 }
122
123
delay(int time)124 void delay(int time){
125 /*
126 time are 1/100 of seconds to wait
127 */
128
129 struct timespec req;
130 int sec;
131
132 if(time<=0)return;
133
134 sec=(int)((float)time/100);
135 time-=100*sec;
136 req.tv_sec=sec;
137 req.tv_nsec=time*1000000;
138 nanosleep(&req,NULL);
139 }
140
141
GameParametres(int option,int param,int value)142 int GameParametres(int option,int param,int value){
143 /*
144 Set or get global game parametres.
145 returns:
146 reading a parameter:
147 value asked.
148 -1 if there an error
149 setting a parameter:
150 0 if there no error.
151 -1 if there an error
152 */
153 static struct Game game;
154 int ret=0;
155
156 switch(option){
157
158 case SET:
159 switch(param){
160 case GULX: /* universe size */
161 game.ulx=value;
162 break;
163 case GULY: /* universe size */
164 game.uly=value;
165 break;
166 case GWIDTH: /* window size */
167 game.width=value;
168 break;
169 case GHEIGHT: /* window size */
170 game.height=value;
171 break;
172 case GPANEL: /* shell size */
173 game.panel_height=value;
174 break;
175 case GNET: /* TRUE : NET , FALSE : LOCAL */
176 game.net=value;
177 break;
178 case GMODE: /* LOCAL, SERVER, CLIENT */
179 game.mode=value;
180 break;
181 case GCOOPERATIVE:
182 game.cooperative=value;
183 break;
184 case GCOMPCOOPERATIVE:
185 game.compcooperative=value;
186 break;
187 case GQUEEN:
188 game.queen=value;
189 break;
190 case GPIRATES:
191 game.pirates=value;
192 break;
193 case GENEMYKNOWN:
194 game.enemyknown=value;
195 break;
196
197 case GNGALAXIES: /* number of galaxies */
198 game.ngalaxies=value;
199 break;
200 case GNPLAYERS: /* number of players */
201 game.nplayers=value;
202 break;
203 case GNPLANETS: /* number of planets */
204 game.nplanets=value;
205 break;
206 case GKPLANETS: /* TRUE FALSE planets known or unknown */
207 game.kplanets=value;
208 break;
209 case GMUSIC: /* TRUE FALSE music on/off */
210 game.music=value;
211 if(game.music==0){
212 SetMusicVolume(0,VOLSET);
213 }
214 break;
215 case GSOUND: /* TRUE FALSE sound on/off */
216 game.sound=value;
217 if(game.sound==0){
218 SetSoundVolume(0,VOLSET);
219 SetMusicVolume(0,VOLSET);
220 }
221
222 break;
223 case GMUSICVOL: /* 0..100 music volume */
224 game.musicvol=value;
225 break;
226 case GSOUNDVOL: /* 0..100 sound volume */
227 game.soundvol=value;
228 break;
229
230
231 case GPAUSED: /* TRUE FALSE game paused */
232 game.paused=value;
233 break;
234 case GQUIT: /* 0,1,2 really quit? */
235 game.quit=value;
236 break;
237 case DEFAULT:
238 game.ulx=ULX;
239 game.uly=ULY;
240 game.width=DEFAULTWIDTH;
241 game.height=DEFAULTHEIGHT;
242 game.panel_height=PANEL_HEIGHT;
243 game.net=FALSE;
244 game.mode=LOCAL;
245 game.cooperative=FALSE;
246 game.compcooperative=FALSE;
247 game.queen=FALSE;
248 game.pirates=TRUE;
249 game.enemyknown=FALSE;
250 game.ngalaxies=NUMGALAXIES;
251 game.nplayers=NUMPLAYERS;
252 game.nplanets=NUMPLANETS;
253 game.kplanets=PLANETSKNOWN;
254 game.music=100;
255 game.sound=100;
256 game.paused=FALSE;
257 game.quit=0; /* possible values 0,1,2 */
258 break;
259 default:
260 ret=-1;
261 break;
262 }
263 break;
264
265 case GET:
266 switch(param){
267 case GULX: /* universe size */
268 ret=game.ulx;
269 break;
270 case GULY: /* universe size */
271 ret=game.uly;
272 break;
273 case GWIDTH: /* window size */
274 ret=game.width;
275 break;
276 case GHEIGHT: /* window size */
277 ret=game.height;
278 break;
279 case GPANEL: /* shell size */
280 ret=game.panel_height;
281 break;
282 case GNET: /* TRUE : NET , FALSE : LOCAL */
283 ret=game.net;
284 break;
285 case GMODE: /* LOCAL, SERVER, CLIENT */
286 ret=game.mode;
287 break;
288 case GCOOPERATIVE:
289 ret=game.cooperative;
290 break;
291 case GCOMPCOOPERATIVE:
292 ret=game.compcooperative;
293 break;
294 case GQUEEN:
295 ret=game.queen;
296 break;
297 case GPIRATES:
298 ret=game.pirates;
299 break;
300 case GENEMYKNOWN:
301 ret=game.enemyknown;
302 break;
303 case GNGALAXIES: /* number of galaxies */
304 ret=game.ngalaxies;
305 break;
306 case GNPLAYERS: /* number of players */
307 ret=game.nplayers;
308 break;
309 case GNPLANETS: /* number of planets */
310 ret=game.nplanets;
311 break;
312 case GKPLANETS: /* TRUE FALSE planets known or unknown */
313 ret=game.kplanets;
314 break;
315 case GMUSIC: /* TRUE FALSE game paused */
316 ret=game.music;
317 break;
318 case GSOUND: /* TRUE FALSE game paused */
319 ret=game.sound;
320 break;
321 case GMUSICVOL: /* TRUE FALSE game paused */
322 ret=game.musicvol;
323 break;
324 case GSOUNDVOL: /* TRUE FALSE game paused */
325 ret=game.soundvol;
326 break;
327 case GPAUSED: /* TRUE FALSE game paused */
328 ret=game.paused;
329 break;
330 case GQUIT: /* 0,1,2 really quit? */
331 ret=game.quit;
332 break;
333 default:
334 fprintf(stderr,"ERROR: GameParametres(): param %d unknown\n",param);
335 ret=-1;
336 break;
337 }
338
339 break;
340 default:
341 ret=-1;
342 break;
343 }
344 return(ret);
345 }
346
347
GetTime(void)348 int GetTime(void){
349 return(sTime(2,0));
350 }
351
SetTime(int t)352 void SetTime(int t){
353 sTime(3,t);
354 }
355
IncTime(void)356 void IncTime(void){
357 sTime(1,0);
358 }
359
sTime(int action,int t)360 int sTime(int action,int t){
361 static int time=0;
362 switch(action){
363 case 0: /* RESET */
364 time=0;
365 break;
366 case 1: /* INCREMENT */
367 time++;
368 break;
369 case 2: /* RETURN VALUE */
370 return (time);
371 break;
372 case 3: /* SET */
373 time=t;
374 break;
375 default:
376 break;
377 }
378 return(time);
379 }
380
DelCharFromCad(char * cad,char * filter)381 void DelCharFromCad(char *cad,char *filter){
382 /*
383 Remove from cad the characters that are not in filter.
384
385 */
386
387 int n=0;
388 int m=0;
389
390 while(cad[n]!='\0'){
391 if(n>=MAXTEXTLEN-1)break;
392 if(strchr(filter,cad[n])==NULL){ /* not found */
393 }
394 else{ /* found */
395 strncpy(&cad[m],&cad[n],1);
396 m++;
397 }
398 n++;
399 }
400
401 strncpy(&cad[m],"\0",1);
402 }
403
Sqrt(int n)404 float Sqrt(int n){
405 static float rtable[20000];
406 static int sw=0;
407 float ret;
408
409 if(sw==0){ /* create table */
410 int i;
411 for(i=0;i<20000;i++){
412 rtable[i]=(float)sqrt(i);
413 }
414 sw=1;
415 }
416
417 if(n>=20000){ret=sqrt(n);}
418 else{
419 ret=rtable[n];
420 }
421 return(ret);
422 }
423
MemUsed(int action,size_t value)424 int MemUsed(int action,size_t value){
425
426 static int memused=0;
427
428 switch(action){
429 case MRESET:
430 memused=0;
431 break;
432 case MSET:
433 memused=value;
434 break;
435 case MADD:
436 memused+=value;
437 break;
438 case MGET:
439 return (memused);
440 break;
441 default:
442 fprintf(stderr,"MenUsed(): Not implemented\n");
443 exit(-1);
444 break;
445 }
446 return(memused);
447 }
448
449
450