1 //
2 // nazghul - an old-school RPG engine
3 // Copyright (C) 2002, 2003 Gordon McNutt
4 //
5 // This program is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 2 of the License, or (at your option)
8 // any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but WITHOUT
11 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 // more details.
14 //
15 // You should have received a copy of the GNU General Public License along with
16 // this program; if not, write to the Free Foundation, Inc., 59 Temple Place,
17 // Suite 330, Boston, MA 02111-1307 USA
18 //
19 // Gordon McNutt
20 // gmcnutt@users.sourceforge.net
21 //
22 #include "common.h"
23 #include "sky.h"
24 #include "screen.h"
25 #include "player.h"
26 #include "foogod.h"
27 #include "place.h"
28 #include "event.h"
29 #include "console.h"
30
31 #include <stdlib.h>
32 #include <time.h>
33
34 int Turn;
35 int Tick;
36 int AnimationTicks;
37 int TickMilliseconds;
38 // int WindDirection;
39 int ShowAllTerrain = 0;
40
41 struct los *LosEngine;
42
43
commonInit(void)44 int commonInit(void)
45 {
46 Turn = 0;
47 Tick = 0;
48 srand(0);
49 return 0;
50 }
51
52 // fixme -- obsolete, use the next one
53 static const char *dir_str[] = {
54 "Northwest", "North", "Northeast",
55 "West", "Here", "East",
56 "Southwest", "South", "Southeast",
57 "Up", "Down"
58 };
59
60 static unsigned char dir_facing[] = {
61 WEST, NORTH, EAST,
62 WEST, NORTH, EAST,
63 WEST, SOUTH, EAST,
64 };
65
66 static unsigned char dir_8facing[] = {
67 NORTHWEST, NORTH, NORTHEAST,
68 WEST, NORTH, EAST,
69 SOUTHWEST, SOUTH, SOUTHEAST,
70 };
71
72 static int direction_to_rotation_tbl[] = {
73 315, 0, 45,
74 270, 0, 90,
75 225, 180, 135,
76 };
77
78 static int directionToDxTable[] = {
79 -1, 0, +1,
80 -1, 0, +1,
81 -1, 0, +1,
82 0, 0,
83 };
84
85 static int directionToDyTable[] = {
86 -1, -1, -1,
87 0, 0, 0,
88 +1, +1, +1,
89 0, 0,
90 };
91
92 static int keyToDirectionTable[] = {
93 SOUTHWEST, SOUTH, SOUTHEAST,
94 WEST, HERE, EAST,
95 NORTHWEST, NORTH, NORTHEAST
96 };
97
directionToString(int dir)98 const char *directionToString(int dir)
99 {
100 if (dir < 0 || dir >= array_sz(dir_str))
101 return "*** invalid direction ***";
102 return dir_str[dir];
103 }
104
stringToDirection(char * str)105 int stringToDirection(char *str)
106 {
107 unsigned int i;
108 for (i = 0; i < array_sz(dir_str); i++) {
109 if (!strncasecmp(dir_str[i], str, strlen(dir_str[i])))
110 return i;
111 }
112 return -1;
113 }
114
directionToOpposite(int dir)115 int directionToOpposite(int dir)
116 {
117 static int tbl[] = {
118 SOUTHEAST,
119 SOUTH,
120 SOUTHWEST,
121 EAST,
122 HERE,
123 WEST,
124 NORTHEAST,
125 NORTH,
126 NORTHWEST,
127 DOWN,
128 UP
129 };
130
131 if (dir < 0 || dir >= array_sz(tbl))
132 return DIRECTION_NONE;
133
134 return tbl[dir];
135 }
136
keyToDirection(int key)137 int keyToDirection(int key)
138 {
139 return keyToDirectionTable[key - KEY_SOUTHWEST];
140 }
141
directionToDx(int dir)142 int directionToDx(int dir)
143 {
144 return directionToDxTable[dir];
145 }
146
directionToDy(int dir)147 int directionToDy(int dir)
148 {
149 return directionToDyTable[dir];
150 }
151
get_dir_str(int dx,int dy)152 const char *get_dir_str(int dx, int dy)
153 {
154 clamp(dx, -1, 1);
155 clamp(dy, -1, 1);
156 return dir_str[(dy + 1) * 3 + dx + 1];
157 }
158
vector_to_facing(int dx,int dy)159 int vector_to_facing(int dx, int dy)
160 {
161 clamp(dx, -1, 1);
162 clamp(dy, -1, 1);
163 return dir_facing[(dy + 1) * 3 + dx + 1];
164 }
165
vector_to_8facing(int dx,int dy)166 int vector_to_8facing(int dx, int dy)
167 {
168 if (abs(dx) > 2* (abs(dy)))
169 {
170 dy = 0;
171 }
172 else if (abs(dy) > 2* (abs(dx)))
173 {
174 dx = 0;
175 }
176 clamp(dx, -1, 1);
177 clamp(dy, -1, 1);
178 return dir_8facing[(dy + 1) * 3 + dx + 1];
179 }
180
vector_to_rotation(int dx,int dy)181 int vector_to_rotation(int dx, int dy)
182 {
183 clamp(dx, -1, 1);
184 clamp(dy, -1, 1);
185 return direction_to_rotation_tbl[(dy + 1) * 3 + dx + 1];
186 }
187
vector_to_dir(int dx,int dy)188 int vector_to_dir(int dx, int dy)
189 {
190 int adx = abs(dx);
191 int ady = abs(dy);
192
193 // Note: north is in the negative x direction, south in the positive
194
195 if (ady > adx) {
196 if (dy > 0)
197 return SOUTH;
198 else
199 return NORTH;
200 } else {
201 if (dx > 0)
202 return EAST;
203 else
204 return WEST;
205 }
206 }
207
isvowel(char c)208 bool isvowel(char c)
209 {
210 return (c == 'a' || c == 'A' ||
211 c == 'e' || c == 'E' ||
212 c == 'i' || c == 'I' ||
213 c == 'o' || c == 'O' ||
214 c == 'u' || c == 'U' || c == 'y' || c == 'Y');
215 }
216
point_in_rect(int x,int y,SDL_Rect * rect)217 bool point_in_rect(int x, int y, SDL_Rect *rect)
218 {
219 return (x >= rect->x &&
220 y >= rect->y &&
221 x < (rect->x + rect->w) &&
222 y < (rect->y + rect->h));
223 }
224
logBase2(int val)225 int logBase2(int val)
226 {
227 int ret = 0;
228
229 if (val<=0)
230 return 0; /* incorrect but safe */
231
232 val -= 1;
233
234 while (val) {
235 val>>=1;
236 ret++;
237 }
238
239 return ret;
240 }
241
242
243