melee / ranged effects
[gemrb.git] / gemrb / core / Core.cpp
blobf0c075366308e28d2fc4774e644b186d713a5bd1
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.
21 /**
22 * @file Core.cpp
23 * Some compatibility and utility functions
24 * @author The GemRB Project
27 #include "globals.h"
28 #include "exports.h"
30 #include "Interface.h"
31 #include "Scriptable/Actor.h"
33 #include <cmath>
34 #include <ctype.h>
35 #ifdef WIN32
36 #include "win32def.h"
37 #ifdef _DEBUG
38 #include <stdlib.h>
39 #include <crtdbg.h>
40 #endif
42 BOOL WINAPI DllEntryPoint(HINSTANCE /*hinstDLL*/, DWORD /*fdwReason*/,
43 LPVOID /*lpvReserved*/)
45 return true;
47 #endif
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)
57 while(count--) {
58 *dest++ = pl_lowercase[(ieByte) *source];
59 if(!*source++) {
60 while(count--) *dest++=0;
61 break;
64 *dest=0;
67 void strnuprcpy(char* dest, const char *source, int count)
69 while(count--) {
70 *dest++ = pl_uppercase[(ieByte) *source];
71 if(!*source++) {
72 while(count--) *dest++=0;
73 break;
76 *dest=0;
79 // this one also filters spaces
80 void strnspccpy(char* dest, const char *source, int count)
82 memset(dest,0,count);
83 while(count--) {
84 char c = pl_lowercase[(ieByte) *source];
85 if (c!=' ') {
86 *dest++=c;
88 if(!*source++) {
89 return;
94 #ifndef HAVE_STRNLEN
95 int strnlen(const char* string, int maxlen)
97 if (!string) {
98 return -1;
100 int i = 0;
101 while (maxlen-- > 0) {
102 if (!string[i])
103 break;
104 i++;
106 return i;
108 #endif // ! HAVE_STRNLEN
110 static const unsigned char orientations[25]={
111 6,7,8,9,10,
112 5,6,8,10,11,
113 4,4,0,12,12,
114 3,2,0,14,13,
115 2,1,0,15,14
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
125 if(div>3) div/=2;
126 int aX=deltaX/div;
127 int aY=deltaY/div;
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 );
171 int ret = x*x + y*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 );
223 int ret = x*x + y*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) {
249 return EAR_FRIEND;
251 if (eat>=EA_EVILCUTOFF) {
252 return EAR_HOSTILE;
255 return EAR_NEUTRAL;
258 if (eao>=EA_EVILCUTOFF) {
260 if (eat<=EA_GOODCUTOFF) {
261 return EAR_HOSTILE;
263 if (eat>=EA_EVILCUTOFF) {
264 return EAR_FRIEND;
267 return EAR_NEUTRAL;
270 return EAR_NEUTRAL;
273 /** Returns the length of string (up to a delimiter) */
274 GEM_EXPORT int strlench(const char* string, char ch)
276 int i;
277 for (i = 0; string[i] && string[i] != ch; i++)
279 return i;
282 //// Compatibility functions
283 #ifndef HAVE_STRNDUP
284 GEM_EXPORT char* strndup(const char* s, size_t l)
286 size_t len = strlen( s );
287 if (len < l) {
288 l = len;
290 char* string = ( char* ) malloc( l + 1 );
291 strncpy( string, s, l );
292 string[l] = 0;
293 return string;
295 #endif
297 #ifdef WIN32
299 #else
301 char* strupr(char* string)
303 char* s;
304 if (string) {
305 for (s = string; *s; ++s)
306 *s = toupper( *s );
308 return string;
311 char* strlwr(char* string)
313 char* s;
314 if (string) {
315 for (s = string; *s; ++s)
316 *s = tolower( *s );
318 return string;
322 #endif // ! WIN32