Move EventMgr to GUI.
[gemrb.git] / gemrb / core / Core.cpp
blob7eed7c6c230fcc72091790f7fc9698f87c40f50f
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 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) {
198 return EAR_FRIEND;
200 if (eat>=EA_EVILCUTOFF) {
201 return EAR_HOSTILE;
204 return EAR_NEUTRAL;
207 if (eao>=EA_EVILCUTOFF) {
209 if (eat<=EA_GOODCUTOFF) {
210 return EAR_HOSTILE;
212 if (eat>=EA_EVILCUTOFF) {
213 return EAR_FRIEND;
216 return EAR_NEUTRAL;
219 return EAR_NEUTRAL;
222 /** Returns the length of string (up to a delimiter) */
223 GEM_EXPORT int strlench(const char* string, char ch)
225 int i;
226 for (i = 0; string[i] && string[i] != ch; i++)
228 return i;
231 //// Compatibility functions
232 #ifndef HAVE_STRNDUP
233 GEM_EXPORT char* strndup(const char* s, size_t l)
235 size_t len = strlen( s );
236 if (len < l) {
237 l = len;
239 char* string = ( char* ) malloc( l + 1 );
240 strncpy( string, s, l );
241 string[l] = 0;
242 return string;
244 #endif
246 #ifdef WIN32
248 #else
250 char* strupr(char* string)
252 char* s;
253 if (string) {
254 for (s = string; *s; ++s)
255 *s = toupper( *s );
257 return string;
260 char* strlwr(char* string)
262 char* s;
263 if (string) {
264 for (s = string; *s; ++s)
265 *s = tolower( *s );
267 return string;
271 #endif // ! WIN32