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 between 2 points */
140 unsigned int Distance(Point p
, Scriptable
*b
)
142 long x
= ( p
.x
- b
->Pos
.x
);
143 long y
= ( p
.y
- b
->Pos
.y
);
144 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
147 unsigned int PersonalDistance(Point p
, Scriptable
*b
)
149 long x
= ( p
.x
- b
->Pos
.x
);
150 long y
= ( p
.y
- b
->Pos
.y
);
151 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
152 if (b
->Type
==ST_ACTOR
) {
153 ret
-=((Actor
*)b
)->size
*10;
155 if (ret
<0) return (unsigned int) 0;
156 return (unsigned int) ret
;
159 /** Calculates distance between 2 scriptables */
160 unsigned int Distance(Scriptable
*a
, Scriptable
*b
)
162 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
163 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
164 return (unsigned int) sqrt( ( double ) ( x
* x
+ y
* y
) );
167 /** Calculates distance between 2 scriptables, including feet circle if applicable */
168 unsigned int PersonalDistance(Scriptable
*a
, Scriptable
*b
)
170 long x
= ( a
->Pos
.x
- b
->Pos
.x
);
171 long y
= ( a
->Pos
.y
- b
->Pos
.y
);
172 int ret
= (int) sqrt( ( double ) ( x
* x
+ y
* y
) );
173 if (a
->Type
==ST_ACTOR
) {
174 ret
-=((Actor
*)a
)->size
*10;
176 if (b
->Type
==ST_ACTOR
) {
177 ret
-=((Actor
*)b
)->size
*10;
179 if (ret
<0) return (unsigned int) 0;
180 return (unsigned int) ret
;
183 // returns EA relation between two scriptables (non actors are always enemies)
184 // it is used for protectile targeting/iwd ids targeting too!
185 int EARelation(Scriptable
* Owner
, Actor
* target
)
187 ieDword eao
= EA_ENEMY
;
189 if (Owner
&& Owner
->Type
==ST_ACTOR
) {
190 eao
= ((Actor
*) Owner
)->GetStat(IE_EA
);
193 ieDword eat
= target
->GetStat(IE_EA
);
195 if (eao
<=EA_GOODCUTOFF
) {
197 if (eat
<=EA_GOODCUTOFF
) {
200 if (eat
>=EA_EVILCUTOFF
) {
207 if (eao
>=EA_EVILCUTOFF
) {
209 if (eat
<=EA_GOODCUTOFF
) {
212 if (eat
>=EA_EVILCUTOFF
) {
222 /** Returns the length of string (up to a delimiter) */
223 GEM_EXPORT
int strlench(const char* string
, char ch
)
226 for (i
= 0; string
[i
] && string
[i
] != ch
; i
++)
231 //// Compatibility functions
233 GEM_EXPORT
char* strndup(const char* s
, size_t l
)
235 size_t len
= strlen( s
);
239 char* string
= ( char* ) malloc( l
+ 1 );
240 strncpy( string
, s
, l
);
250 char* strupr(char* string
)
254 for (s
= string
; *s
; ++s
)
260 char* strlwr(char* string
)
264 for (s
= string
; *s
; ++s
)