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
32 #include <sys/types.h>
45 BOOL WINAPI
DllEntryPoint(HINSTANCE
/*hinstDLL*/, DWORD
/*fdwReason*/,
46 LPVOID
/*lpvReserved*/)
54 #include "Interface.h"
57 //// Globally used functions
59 ieByte pl_uppercase
[256];
60 ieByte pl_lowercase
[256];
62 // these 3 functions will copy a string to a zero terminated string with a maximum length
63 void strnlwrcpy(char *dest
, const char *source
, int count
)
66 *dest
++ = pl_lowercase
[(ieByte
) *source
];
68 while(count
--) *dest
++=0;
75 void strnuprcpy(char* dest
, const char *source
, int count
)
78 *dest
++ = pl_uppercase
[(ieByte
) *source
];
80 while(count
--) *dest
++=0;
87 // this one also filters spaces
88 void strnspccpy(char* dest
, const char *source
, int count
)
92 char c
= pl_lowercase
[(ieByte
) *source
];
103 int strnlen(const char* string
, int maxlen
)
109 while (maxlen
-- > 0) {
116 #endif // ! HAVE_STRNLEN
118 static const unsigned char orientations
[25]={
126 /** Calculates the orientation of a character (or projectile) facing a point */
127 unsigned char GetOrient(Point
&s
, Point
&d
)
129 int deltaX
= s
.x
- d
.x
;
130 int deltaY
= s
.y
- d
.y
;
131 int div
= Distance(s
,d
);
132 if(!div
) return 0; //default
136 return orientations
[(aY
+2)*5+aX
+2];
139 /** Calculates distance between 2 points */
140 unsigned int Distance(Point p
, Point q
)
142 long x
= ( p
.x
- q
.x
);
143 long y
= ( p
.y
- q
.y
);
144 return (unsigned int) sqrt( ( double ) ( 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 /** Calculates distance between 2 scriptables */
168 unsigned int Distance(Scriptable
*a
, Scriptable
*b
)
170 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
171 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
172 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
175 /** Calculates distance between 2 scriptables, including feet circle if applicable */
176 unsigned int PersonalDistance(Scriptable
*a
, Scriptable
*b
)
178 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
179 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
180 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
181 if (a
->Type
==ST_ACTOR
) {
182 ret
-=((Actor
*)a
)->size
*10;
184 if (b
->Type
==ST_ACTOR
) {
185 ret
-=((Actor
*)b
)->size
*10;
187 if (ret
<0) return (unsigned int) 0;
188 return (unsigned int) ret
;
191 // returns EA relation between two scriptables (non actors are always enemies)
192 // it is used for protectile targeting/iwd ids targeting too!
193 int EARelation(Scriptable
* Owner
, Actor
* target
)
195 ieDword eao
= EA_ENEMY
;
197 if (Owner
&& Owner
->Type
==ST_ACTOR
) {
198 eao
= ((Actor
*) Owner
)->GetStat(IE_EA
);
201 ieDword eat
= target
->GetStat(IE_EA
);
203 if (eao
<=EA_GOODCUTOFF
) {
205 if (eat
<=EA_GOODCUTOFF
) {
208 if (eat
>=EA_EVILCUTOFF
) {
215 if (eao
>=EA_EVILCUTOFF
) {
217 if (eat
<=EA_GOODCUTOFF
) {
220 if (eat
>=EA_EVILCUTOFF
) {
230 /** Returns the length of string (up to a delimiter) */
231 GEM_EXPORT
int strlench(const char* string
, char ch
)
234 for (i
= 0; string
[i
] && string
[i
] != ch
; i
++)
239 //// Compatibility functions
241 GEM_EXPORT
char* strndup(const char* s
, size_t l
)
243 size_t len
= strlen( s
);
247 char* string
= ( char* ) malloc( l
+ 1 );
248 strncpy( string
, s
, l
);
258 char* strupr(char* string
)
262 for (s
= string
; *s
; ++s
)
268 char* strlwr(char* string
)
272 for (s
= string
; *s
; ++s
)