1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * Some compatibility and utility functions
24 * @author The GemRB Project
30 #include "Interface.h"
31 #include "Scriptable/Actor.h"
42 BOOL WINAPI
DllEntryPoint(HINSTANCE
/*hinstDLL*/, DWORD
/*fdwReason*/,
43 LPVOID
/*lpvReserved*/)
49 //// Globally used functions
51 ieByte pl_uppercase
[256];
52 ieByte pl_lowercase
[256];
54 // these 3 functions will copy a string to a zero terminated string with a maximum length
55 void strnlwrcpy(char *dest
, const char *source
, int count
)
58 *dest
++ = pl_lowercase
[(ieByte
) *source
];
60 while(count
--) *dest
++=0;
67 void strnuprcpy(char* dest
, const char *source
, int count
)
70 *dest
++ = pl_uppercase
[(ieByte
) *source
];
72 while(count
--) *dest
++=0;
79 // this one also filters spaces
80 void strnspccpy(char* dest
, const char *source
, int count
)
84 char c
= pl_lowercase
[(ieByte
) *source
];
95 int strnlen(const char* string
, int maxlen
)
101 while (maxlen
-- > 0) {
108 #endif // ! HAVE_STRNLEN
110 static const unsigned char orientations
[25]={
118 /** Calculates the orientation of a character (or projectile) facing a point */
119 unsigned char GetOrient(const Point
&s
, const Point
&d
)
121 int deltaX
= s
.x
- d
.x
;
122 int deltaY
= s
.y
- d
.y
;
123 int div
= Distance(s
,d
);
124 if(!div
) return 0; //default
128 return orientations
[(aY
+2)*5+aX
+2];
131 /** Calculates distance between 2 points */
132 unsigned int Distance(Point p
, Point q
)
134 long x
= ( p
.x
- q
.x
);
135 long y
= ( p
.y
- q
.y
);
136 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
139 /** Calculates distance squared from a point to a scriptable */
140 unsigned int SquaredMapDistance(Point p
, Scriptable
*b
)
142 long x
= ( p
.x
/16 - b
->Pos
.x
/16 );
143 long y
= ( p
.y
/12 - b
->Pos
.y
/12 );
144 return (unsigned int)(x
*x
+ y
*y
);
147 /** Calculates distance between 2 points */
148 unsigned int Distance(Point p
, Scriptable
*b
)
150 long x
= ( p
.x
- b
->Pos
.x
);
151 long y
= ( p
.y
- b
->Pos
.y
);
152 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
155 unsigned int PersonalDistance(Point p
, Scriptable
*b
)
157 long x
= ( p
.x
- b
->Pos
.x
);
158 long y
= ( p
.y
- b
->Pos
.y
);
159 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
160 if (b
->Type
==ST_ACTOR
) {
161 ret
-=((Actor
*)b
)->size
*10;
163 if (ret
<0) return (unsigned int) 0;
164 return (unsigned int) ret
;
167 unsigned int SquaredPersonalDistance(Point p
, Scriptable
*b
)
169 long x
= ( p
.x
- b
->Pos
.x
);
170 long y
= ( p
.y
- b
->Pos
.y
);
172 if (b
->Type
==ST_ACTOR
) {
173 ret
-=((Actor
*)b
)->size
*100;
175 if (ret
<0) return (unsigned int) 0;
176 return (unsigned int) ret
;
179 /** Calculates map distance between 2 scriptables */
180 unsigned int SquaredMapDistance(Scriptable
*a
, Scriptable
*b
)
182 long x
= (a
->Pos
.x
/16 - b
->Pos
.x
/16 );
183 long y
= (a
->Pos
.y
/12 - b
->Pos
.y
/12 );
184 return (unsigned int)(x
*x
+ y
*y
);
187 /** Calculates distance between 2 scriptables */
188 unsigned int Distance(Scriptable
*a
, Scriptable
*b
)
190 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
191 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
192 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
195 /** Calculates distance squared between 2 scriptables */
196 unsigned int SquaredDistance(Scriptable
*a
, Scriptable
*b
)
198 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
199 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
200 return (unsigned int) ( x
* x
+ y
* y
);
203 /** Calculates distance between 2 scriptables, including feet circle if applicable */
204 unsigned int PersonalDistance(Scriptable
*a
, Scriptable
*b
)
206 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
207 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
208 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
209 if (a
->Type
==ST_ACTOR
) {
210 ret
-=((Actor
*)a
)->size
*10;
212 if (b
->Type
==ST_ACTOR
) {
213 ret
-=((Actor
*)b
)->size
*10;
215 if (ret
<0) return (unsigned int) 0;
216 return (unsigned int) ret
;
219 unsigned int SquaredPersonalDistance(Scriptable
*a
, Scriptable
*b
)
221 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
222 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
224 if (a
->Type
==ST_ACTOR
) {
225 ret
-=((Actor
*)a
)->size
*100;
227 if (b
->Type
==ST_ACTOR
) {
228 ret
-=((Actor
*)b
)->size
*100;
230 if (ret
<0) return (unsigned int) 0;
231 return (unsigned int) ret
;
234 // returns EA relation between two scriptables (non actors are always enemies)
235 // it is used for protectile targeting/iwd ids targeting too!
236 int EARelation(Scriptable
* Owner
, Actor
* target
)
238 ieDword eao
= EA_ENEMY
;
240 if (Owner
&& Owner
->Type
==ST_ACTOR
) {
241 eao
= ((Actor
*) Owner
)->GetStat(IE_EA
);
244 ieDword eat
= target
->GetStat(IE_EA
);
246 if (eao
<=EA_GOODCUTOFF
) {
248 if (eat
<=EA_GOODCUTOFF
) {
251 if (eat
>=EA_EVILCUTOFF
) {
258 if (eao
>=EA_EVILCUTOFF
) {
260 if (eat
<=EA_GOODCUTOFF
) {
263 if (eat
>=EA_EVILCUTOFF
) {
273 /** Returns the length of string (up to a delimiter) */
274 GEM_EXPORT
int strlench(const char* string
, char ch
)
277 for (i
= 0; string
[i
] && string
[i
] != ch
; i
++)
282 //// Compatibility functions
284 GEM_EXPORT
char* strndup(const char* s
, size_t l
)
286 size_t len
= strlen( s
);
290 char* string
= ( char* ) malloc( l
+ 1 );
291 strncpy( string
, s
, l
);
301 char* strupr(char* string
)
305 for (s
= string
; *s
; ++s
)
311 char* strlwr(char* string
)
315 for (s
= string
; *s
; ++s
)