GameScript: Move initialization out of constructor.
[gemrb.git] / gemrb / core / Core.cpp
blob6edd08949469f79db7f653557b1b8d0a0e1d6832
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 <cmath>
28 #ifndef WIN32
29 #include <ctype.h>
30 #include <sys/time.h>
31 #include <dirent.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #else
38 #include "win32def.h"
39 #include <sys\stat.h>
40 #ifdef _DEBUG
41 #include <stdlib.h>
42 #include <crtdbg.h>
43 #endif
45 BOOL WINAPI DllEntryPoint(HINSTANCE /*hinstDLL*/, DWORD /*fdwReason*/,
46 LPVOID /*lpvReserved*/)
48 return true;
50 #endif
52 #include "globals.h"
53 #include "exports.h"
54 #include "Interface.h"
55 #include "Actor.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)
65 while(count--) {
66 *dest++ = pl_lowercase[(ieByte) *source];
67 if(!*source++) {
68 while(count--) *dest++=0;
69 break;
72 *dest=0;
75 void strnuprcpy(char* dest, const char *source, int count)
77 while(count--) {
78 *dest++ = pl_uppercase[(ieByte) *source];
79 if(!*source++) {
80 while(count--) *dest++=0;
81 break;
84 *dest=0;
87 // this one also filters spaces
88 void strnspccpy(char* dest, const char *source, int count)
90 memset(dest,0,count);
91 while(count--) {
92 char c = pl_lowercase[(ieByte) *source];
93 if (c!=' ') {
94 *dest++=c;
96 if(!*source++) {
97 return;
102 #ifndef HAVE_STRNLEN
103 int strnlen(const char* string, int maxlen)
105 if (!string) {
106 return -1;
108 int i = 0;
109 while (maxlen-- > 0) {
110 if (!string[i])
111 break;
112 i++;
114 return i;
116 #endif // ! HAVE_STRNLEN
118 static const unsigned char orientations[25]={
119 6,7,8,9,10,
120 5,6,8,10,11,
121 4,4,0,12,12,
122 3,2,0,14,13,
123 2,1,0,15,14
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
133 if(div>3) div/=2;
134 int aX=deltaX/div;
135 int aY=deltaY/div;
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) {
206 return EAR_FRIEND;
208 if (eat>=EA_EVILCUTOFF) {
209 return EAR_HOSTILE;
212 return EAR_NEUTRAL;
215 if (eao>=EA_EVILCUTOFF) {
217 if (eat<=EA_GOODCUTOFF) {
218 return EAR_HOSTILE;
220 if (eat>=EA_EVILCUTOFF) {
221 return EAR_FRIEND;
224 return EAR_NEUTRAL;
227 return EAR_NEUTRAL;
230 /** Returns the length of string (up to a delimiter) */
231 GEM_EXPORT int strlench(const char* string, char ch)
233 int i;
234 for (i = 0; string[i] && string[i] != ch; i++)
236 return i;
239 //// Compatibility functions
240 #ifndef HAVE_STRNDUP
241 GEM_EXPORT char* strndup(const char* s, size_t l)
243 size_t len = strlen( s );
244 if (len < l) {
245 l = len;
247 char* string = ( char* ) malloc( l + 1 );
248 strncpy( string, s, l );
249 string[l] = 0;
250 return string;
252 #endif
254 #ifdef WIN32
256 #else
258 char* strupr(char* string)
260 char* s;
261 if (string) {
262 for (s = string; *s; ++s)
263 *s = toupper( *s );
265 return string;
268 char* strlwr(char* string)
270 char* s;
271 if (string) {
272 for (s = string; *s; ++s)
273 *s = tolower( *s );
275 return string;
279 #endif // ! WIN32