File: stuff.c

package info (click to toggle)
overgod 1.0-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 4,872 kB
  • sloc: ansic: 31,576; makefile: 22; sh: 10
file content (188 lines) | stat: -rw-r--r-- 4,265 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Overgod
Copyright (C) 2005 Linley Henzell

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public Licence as published by
    the Free Software Foundation; either version 2 of the Licence, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public Licence for more details.

    You should have received a copy of the GNU General Public Licence
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    The GPL version 2 is included in this distribution in a file called
    LICENCE.TXT. Use any text editor or the TYPE command to read it.

    You should be able to reach me by sending an email to
    l_henzell@yahoo.com.au.
    
File: stuff.c
History:
11/9/2005 - Version 1.0 finalised

This file contains:
 - a few common functions

*/

#include "allegro.h"

#include <math.h>
//#include <stdlib.h>

#include "config.h"

int turn_towards_angle(int angle, int tangle, int turning);

float lcos(int angle);
float lsin(int angle);
float angle_to_radians(int angle);

// I have no idea why, but the first few elements of cos_table always get corrupted
//  unless I put a big fat decoy array just above. A similar thing happens to the
//  palette arrays; there seems to be a problem with global arrays like these.
float decoy_table [ANGLE_FULL]; // not used
float cos_table [ANGLE_FULL];
float sin_table [ANGLE_FULL];

inline int xpart(int angle, int length);

void init_trig(void)
{
 int i;
 
 for (i = 0; i < ANGLE_FULL; i ++)
 {
   cos_table [i] = cos(angle_to_radians(i));// * ANGLE_FULL;
   sin_table [i] = sin(angle_to_radians(i));// * ANGLE_FULL;
 }
 
 
}

inline int xpart(int angle, int length)
{
// return (lcos(angle) * length);// / ANGLE_FULL;
 return (cos_table [angle & 1023] * length);// / ANGLE_FULL;
}

inline int ypart(int angle, int length)
{
 return (sin_table [angle & 1023] * length);// / ANGLE_FULL;
}

float lcos(int angle)
{
 return cos_table [angle & 1023]; //0x4ffffffff];
 
}

float lsin(int angle)
{
 return sin_table [angle & 1023]; //0x4ffffffff];
 
}


float angle_to_radians(int angle)
{
 return ((float) angle * PI * 2) / ANGLE_FULL;
// return ((float) angle / ANGLE_FULL) * PI * 2;
}

int radians_to_angle(float angle)
{
 return (int) ((angle * ANGLE_FULL) / (PI * 2));
}

fixed angle_to_fixed(int angle)
{
 return itofix(angle / ANGLE_TO_FIXED);
}

int grand(int number)
{
 if (number == 0)
  return 0;
 return ((rand() + (rand() << 16)) & 0x7fffffff) % number; 
}

int crandom(int number)
{
 if (number == 0)
  return 0;
 return ((rand() + (rand() << 16)) & 0x7fffffff) % number; 
// return (rand() + (rand() << 16)) % number; 
}

int turn_towards_angle(int angle, int tangle, int turning)
{

 if ((angle < tangle && tangle > angle + ANGLE_HALF)
     || (angle > tangle && tangle > angle - ANGLE_HALF))
 {
  angle -= turning;
  if (angle < 0)
   angle += ANGLE_FULL;
 }
  else
  {
   angle += turning;
   if (angle > ANGLE_FULL)
    angle -= ANGLE_FULL;
  }
  
 return angle;
 
}


int turn_towards_xy(int x1, int y1, int x2, int y2, int angle, int turning)
{

  int tangle =
    radians_to_angle(atan2((y2 - y1), (x2 - x1)));
   if (tangle < 0)
    tangle += ANGLE_FULL;
   if (tangle > ANGLE_FULL)
    tangle -= ANGLE_FULL;

  return turn_towards_angle(angle, tangle, turning);

}


// speed must be at least 4, and a factor of 1024
int pulsate(int speed, int amount, int county)
{
 return xpart((county * speed) & 1023, amount);
}

void error_message_out(const char *errm)
{
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("%s", errm);
      exit(1);
}

  
int angle_difference(int a1, int a2)
{
 int d1, d2;
 
 d1 = (a1 - a2 + ANGLE_FULL) % ANGLE_FULL;
 d2 = (a2 - a1 + ANGLE_FULL) % ANGLE_FULL;

 if (d1 < d2)
  return abs(d1);
  
 return abs(d2);
}