1 /* $Id: math.c,v 5.2 2003/09/16 21:00:16 bertg Exp $
2  *
3  * XPilot, a multiplayer gravity war game.  Copyright (C) 1991-2001 by
4  *
5  *      Bj�rn Stabell        <bjoern@xpilot.org>
6  *      Ken Ronny Schouten   <ken@xpilot.org>
7  *      Bert Gijsbers        <bert@xpilot.org>
8  *      Dick Balaska         <dick@xpilot.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <math.h>
30 
31 #include "version.h"
32 #include "config.h"
33 #include "const.h"
34 #include "error.h"
35 #include "commonproto.h"
36 
37 
38 char math_version[] = VERSION;
39 
40 
41 
42 DFLOAT		tbl_sin[TABLE_SIZE];
43 DFLOAT		tbl_cos[TABLE_SIZE];
44 
45 int ON(char *optval);
46 int OFF(char *optval);
47 int f2i(DFLOAT f);
48 DFLOAT findDir(DFLOAT x, DFLOAT y);
49 void Make_table(void);
50 
ON(char * optval)51 int ON(char *optval)
52 {
53     return (strncasecmp(optval, "true", 4) == 0
54 	    || strncasecmp(optval, "on", 2) == 0
55 	    || strncasecmp(optval, "yes", 3) == 0);
56 }
57 
58 
OFF(char * optval)59 int OFF(char *optval)
60 {
61     return (strncasecmp(optval, "false", 5) == 0
62 	    || strncasecmp(optval, "off", 3) == 0
63 	    || strncasecmp(optval, "no", 2) == 0);
64 }
65 
66 
mod(int x,int y)67 int mod(int x, int y)
68 {
69     if (x >= y || x < 0)
70 	x = x - y*(x/y);
71 
72     if (x < 0)
73 	x += y;
74 
75     return x;
76 }
77 
78 
f2i(DFLOAT f)79 int f2i(DFLOAT f)
80 {
81     return (f < 0) ? -(int)(0.5f - f) : (int)(f + 0.5f);
82 }
83 
findDir(DFLOAT x,DFLOAT y)84 DFLOAT findDir(DFLOAT x, DFLOAT y)
85 {
86     DFLOAT angle;
87 
88     if (x != 0.0 || y != 0.0)
89 	angle = atan2(y, x) / (2 * PI);
90     else
91 	angle = 0.0;
92 
93     if (angle < 0)
94 	angle++;
95     return angle * RES;
96 }
97 
98 
rfrac(void)99 double rfrac(void)
100 {
101     /*
102      * Return a pseudo-random value in the range { 0.0 <= x < 1.0 }.
103      * Use randomMT() which returns a 32 bit PRN and multiply by 1/(1<<32).
104      */
105     return (double) randomMT() * 0.00000000023283064365386962890625;
106 }
107 
108 
Make_table(void)109 void Make_table(void)
110 {
111     int i;
112 
113     for (i = 0; i < TABLE_SIZE; i++) {
114 	tbl_sin[i] = sin(i * (2.0 * PI / TABLE_SIZE));
115 	tbl_cos[i] = cos(i * (2.0 * PI / TABLE_SIZE));
116     }
117 }
118 
119