CodingStyle: Document header include order.
[gemrb.git] / gemrb / core / Core.cpp
blobfc867118a847237737fa266bb04b3963d980ecfe
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 "Actor.h"
31 #include "Interface.h"
33 #include <cmath>
34 #ifndef WIN32
35 #include <ctype.h>
36 #include <sys/time.h>
37 #include <dirent.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #else
44 #include "win32def.h"
45 #include <sys\stat.h>
46 #ifdef _DEBUG
47 #include <stdlib.h>
48 #include <crtdbg.h>
49 #endif
51 BOOL WINAPI DllEntryPoint(HINSTANCE /*hinstDLL*/, DWORD /*fdwReason*/,
52 LPVOID /*lpvReserved*/)
54 return true;
56 #endif
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)
66 while(count--) {
67 *dest++ = pl_lowercase[(ieByte) *source];
68 if(!*source++) {
69 while(count--) *dest++=0;
70 break;
73 *dest=0;
76 void strnuprcpy(char* dest, const char *source, int count)
78 while(count--) {
79 *dest++ = pl_uppercase[(ieByte) *source];
80 if(!*source++) {
81 while(count--) *dest++=0;
82 break;
85 *dest=0;
88 // this one also filters spaces
89 void strnspccpy(char* dest, const char *source, int count)
91 memset(dest,0,count);
92 while(count--) {
93 char c = pl_lowercase[(ieByte) *source];
94 if (c!=' ') {
95 *dest++=c;
97 if(!*source++) {
98 return;
103 #ifndef HAVE_STRNLEN
104 int strnlen(const char* string, int maxlen)
106 if (!string) {
107 return -1;
109 int i = 0;
110 while (maxlen-- > 0) {
111 if (!string[i])
112 break;
113 i++;
115 return i;
117 #endif // ! HAVE_STRNLEN
119 static const unsigned char orientations[25]={
120 6,7,8,9,10,
121 5,6,8,10,11,
122 4,4,0,12,12,
123 3,2,0,14,13,
124 2,1,0,15,14
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
134 if(div>3) div/=2;
135 int aX=deltaX/div;
136 int aY=deltaY/div;
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) {
207 return EAR_FRIEND;
209 if (eat>=EA_EVILCUTOFF) {
210 return EAR_HOSTILE;
213 return EAR_NEUTRAL;
216 if (eao>=EA_EVILCUTOFF) {
218 if (eat<=EA_GOODCUTOFF) {
219 return EAR_HOSTILE;
221 if (eat>=EA_EVILCUTOFF) {
222 return EAR_FRIEND;
225 return EAR_NEUTRAL;
228 return EAR_NEUTRAL;
231 /** Returns the length of string (up to a delimiter) */
232 GEM_EXPORT int strlench(const char* string, char ch)
234 int i;
235 for (i = 0; string[i] && string[i] != ch; i++)
237 return i;
240 //// Compatibility functions
241 #ifndef HAVE_STRNDUP
242 GEM_EXPORT char* strndup(const char* s, size_t l)
244 size_t len = strlen( s );
245 if (len < l) {
246 l = len;
248 char* string = ( char* ) malloc( l + 1 );
249 strncpy( string, s, l );
250 string[l] = 0;
251 return string;
253 #endif
255 #ifdef WIN32
257 #else
259 char* strupr(char* string)
261 char* s;
262 if (string) {
263 for (s = string; *s; ++s)
264 *s = toupper( *s );
266 return string;
269 char* strlwr(char* string)
271 char* s;
272 if (string) {
273 for (s = string; *s; ++s)
274 *s = tolower( *s );
276 return string;
280 #endif // ! WIN32