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
31 #include "Interface.h"
38 #include <sys/types.h>
51 BOOL WINAPI
DllEntryPoint(HINSTANCE
/*hinstDLL*/, DWORD
/*fdwReason*/,
52 LPVOID
/*lpvReserved*/)
58 //// Globally used functions
60 ieByte pl_uppercase
[256];
61 ieByte pl_lowercase
[256];
63 // these 3 functions will copy a string to a zero terminated string with a maximum length
64 void strnlwrcpy(char *dest
, const char *source
, int count
)
67 *dest
++ = pl_lowercase
[(ieByte
) *source
];
69 while(count
--) *dest
++=0;
76 void strnuprcpy(char* dest
, const char *source
, int count
)
79 *dest
++ = pl_uppercase
[(ieByte
) *source
];
81 while(count
--) *dest
++=0;
88 // this one also filters spaces
89 void strnspccpy(char* dest
, const char *source
, int count
)
93 char c
= pl_lowercase
[(ieByte
) *source
];
104 int strnlen(const char* string
, int maxlen
)
110 while (maxlen
-- > 0) {
117 #endif // ! HAVE_STRNLEN
119 static const unsigned char orientations
[25]={
127 /** Calculates the orientation of a character (or projectile) facing a point */
128 unsigned char GetOrient(const Point
&s
, const Point
&d
)
130 int deltaX
= s
.x
- d
.x
;
131 int deltaY
= s
.y
- d
.y
;
132 int div
= Distance(s
,d
);
133 if(!div
) return 0; //default
137 return orientations
[(aY
+2)*5+aX
+2];
140 /** Calculates distance between 2 points */
141 unsigned int Distance(Point p
, Point q
)
143 long x
= ( p
.x
- q
.x
);
144 long y
= ( p
.y
- q
.y
);
145 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
148 /** Calculates distance between 2 points */
149 unsigned int Distance(Point p
, Scriptable
*b
)
151 long x
= ( p
.x
- b
->Pos
.x
);
152 long y
= ( p
.y
- b
->Pos
.y
);
153 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
156 unsigned int PersonalDistance(Point p
, Scriptable
*b
)
158 long x
= ( p
.x
- b
->Pos
.x
);
159 long y
= ( p
.y
- b
->Pos
.y
);
160 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
161 if (b
->Type
==ST_ACTOR
) {
162 ret
-=((Actor
*)b
)->size
*10;
164 if (ret
<0) return (unsigned int) 0;
165 return (unsigned int) ret
;
168 /** Calculates distance between 2 scriptables */
169 unsigned int Distance(Scriptable
*a
, Scriptable
*b
)
171 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
172 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
173 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
176 /** Calculates distance between 2 scriptables, including feet circle if applicable */
177 unsigned int PersonalDistance(Scriptable
*a
, Scriptable
*b
)
179 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
180 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
181 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
182 if (a
->Type
==ST_ACTOR
) {
183 ret
-=((Actor
*)a
)->size
*10;
185 if (b
->Type
==ST_ACTOR
) {
186 ret
-=((Actor
*)b
)->size
*10;
188 if (ret
<0) return (unsigned int) 0;
189 return (unsigned int) ret
;
192 // returns EA relation between two scriptables (non actors are always enemies)
193 // it is used for protectile targeting/iwd ids targeting too!
194 int EARelation(Scriptable
* Owner
, Actor
* target
)
196 ieDword eao
= EA_ENEMY
;
198 if (Owner
&& Owner
->Type
==ST_ACTOR
) {
199 eao
= ((Actor
*) Owner
)->GetStat(IE_EA
);
202 ieDword eat
= target
->GetStat(IE_EA
);
204 if (eao
<=EA_GOODCUTOFF
) {
206 if (eat
<=EA_GOODCUTOFF
) {
209 if (eat
>=EA_EVILCUTOFF
) {
216 if (eao
>=EA_EVILCUTOFF
) {
218 if (eat
<=EA_GOODCUTOFF
) {
221 if (eat
>=EA_EVILCUTOFF
) {
231 /** Returns the length of string (up to a delimiter) */
232 GEM_EXPORT
int strlench(const char* string
, char ch
)
235 for (i
= 0; string
[i
] && string
[i
] != ch
; i
++)
240 //// Compatibility functions
242 GEM_EXPORT
char* strndup(const char* s
, size_t l
)
244 size_t len
= strlen( s
);
248 char* string
= ( char* ) malloc( l
+ 1 );
249 strncpy( string
, s
, l
);
259 char* strupr(char* string
)
263 for (s
= string
; *s
; ++s
)
269 char* strlwr(char* string
)
273 for (s
= string
; *s
; ++s
)