/
tools.cpp
1722 lines (1475 loc) · 39.4 KB
/
tools.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "tools.h"
#include <iostream>
#include <iomanip>
#include <cryptopp/sha.h>
#include <cryptopp/md5.h>
#include <cryptopp/adler32.h>
#include <cryptopp/hmac.h>
#include <cryptopp/hex.h>
#include <cryptopp/base64.h>
#include <cryptopp/cryptlib.h>
#include "vocation.h"
#include "configmanager.h"
extern ConfigManager g_config;
std::string transformToMD5(std::string plainText, bool upperCase)
{
// Crypto++ MD5 object
CryptoPP::Weak::MD5 hash;
// Use native byte instead of casting chars
byte digest[CryptoPP::Weak::MD5::DIGESTSIZE];
// Do the actual calculation, require a byte value so we need a cast
hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
// Crypto++ HexEncoder object
CryptoPP::HexEncoder encoder;
// Our output
std::string output;
// Drop internal hex encoder and use this, returns uppercase by default
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
// Make sure we want uppercase
if(upperCase)
return output;
// Convert to lowercase if needed
return asLowerCaseString(output);
}
std::string transformToSHA1(std::string plainText, bool upperCase)
{
// Crypto++ SHA1 object
CryptoPP::SHA1 hash;
// Use native byte instead of casting chars
byte digest[CryptoPP::SHA1::DIGESTSIZE];
// Do the actual calculation, require a byte value so we need a cast
hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
// Crypto++ HexEncoder object
CryptoPP::HexEncoder encoder;
// Our output
std::string output;
// Drop internal hex encoder and use this, returns uppercase by default
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
// Make sure we want uppercase
if(upperCase)
return output;
// Convert to lowercase if needed
return asLowerCaseString(output);
}
std::string transformToSHA256(std::string plainText, bool upperCase)
{
// Crypto++ SHA256 object
CryptoPP::SHA256 hash;
// Use native byte instead of casting chars
byte digest[CryptoPP::SHA256::DIGESTSIZE];
// Do the actual calculation, require a byte value so we need a cast
hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
// Crypto++ HexEncoder object
CryptoPP::HexEncoder encoder;
// Our output
std::string output;
// Drop internal hex encoder and use this, returns uppercase by default
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
// Make sure we want uppercase
if(upperCase)
return output;
// Convert to lowercase if needed
return asLowerCaseString(output);
}
std::string transformToSHA512(std::string plainText, bool upperCase)
{
// Crypto++ SHA512 object
CryptoPP::SHA512 hash;
// Use native byte instead of casting chars
byte digest[CryptoPP::SHA512::DIGESTSIZE];
// Do the actual calculation, require a byte value so we need a cast
hash.CalculateDigest(digest, (const byte*)plainText.c_str(), plainText.length());
// Crypto++ HexEncoder object
CryptoPP::HexEncoder encoder;
// Our output
std::string output;
// Drop internal hex encoder and use this, returns uppercase by default
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
// Make sure we want uppercase
if(upperCase)
return output;
// Convert to lowercase if needed
return asLowerCaseString(output);
}
std::string transformToVAHash(std::string plainText, bool upperCase)
{
std::string key = g_config.getString(ConfigManager::ENCRYPTION_KEY);
// This is basicaly a base64 string out of a sha512 lowcase string of the HMAC of the plaintext sha256 string with a configurated key
// Currently this removes all known weaknesses in the sha-2 implantation
// base64(HMAC<SHA512>(key, SHA256(plainText)));
// Get SHA256
std::string sha256 = transformToSHA256(plainText, false);
// This holds the HMAC
// Use native byte instead of casting chars
byte digest[CryptoPP::SHA512::DIGESTSIZE];
// Do the actual calculation and setup, require a byte value so we need a cast on the key and the input
CryptoPP::HMAC<CryptoPP::SHA512>((const byte*)key.c_str(), key.length()).CalculateDigest(
digest, (const byte*)sha256.c_str(), CryptoPP::SHA256::DIGESTSIZE);
// Crypto++ Base64Encoder object
CryptoPP::Base64Encoder encoder;
// Our output
std::string output;
// Encode to base64
encoder.Attach(new CryptoPP::StringSink(output));
encoder.Put(digest, sizeof(digest));
encoder.MessageEnd();
// Make sure we want uppercase
if(upperCase)
return output;
// Convert to lowercase if needed
return asLowerCaseString(output);
}
void _encrypt(std::string& str, bool upperCase)
{
switch(g_config.getNumber(ConfigManager::ENCRYPTION))
{
case ENCRYPTION_MD5:
str = transformToMD5(str, upperCase);
break;
case ENCRYPTION_SHA1:
str = transformToSHA1(str, upperCase);
break;
case ENCRYPTION_SHA256:
str = transformToSHA256(str, upperCase);
break;
case ENCRYPTION_SHA512:
str = transformToSHA512(str, upperCase);
break;
case ENCRYPTION_VAHASH:
str = transformToVAHash(str, upperCase);
break;
default:
{
if(upperCase)
std::transform(str.begin(), str.end(), str.begin(), upchar);
break;
}
}
}
bool encryptTest(std::string plain, std::string& hash)
{
std::transform(hash.begin(), hash.end(), hash.begin(), upchar);
_encrypt(plain, true);
return plain == hash;
}
bool replaceString(std::string& text, const std::string& key, const std::string& value)
{
if(text.find(key) == std::string::npos)
return false;
std::string::size_type start = 0, pos = 0;
while((start = text.find(key, pos)) != std::string::npos)
{
text.replace(start, key.size(), value);
//text = text.substr(0, start) + value + text.substr(start + key.size());
pos = start + value.size();
}
return true;
}
void trim_right(std::string& source, const std::string& t)
{
source.erase(source.find_last_not_of(t)+1);
}
void trim_left(std::string& source, const std::string& t)
{
source.erase(0, source.find_first_not_of(t));
}
void toLowerCaseString(std::string& source)
{
std::transform(source.begin(), source.end(), source.begin(), tolower);
}
void toUpperCaseString(std::string& source)
{
std::transform(source.begin(), source.end(), source.begin(), upchar);
}
std::string asLowerCaseString(const std::string& source)
{
std::string s = source;
toLowerCaseString(s);
return s;
}
std::string asUpperCaseString(const std::string& source)
{
std::string s = source;
toUpperCaseString(s);
return s;
}
bool booleanString(std::string source)
{
toLowerCaseString(source);
return (source == "yes" || source == "true" || atoi(source.c_str()) > 0);
}
bool readXMLInteger(xmlNodePtr node, const char* tag, int32_t& value)
{
char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
if(!nodeValue)
return false;
value = atoi(nodeValue);
xmlFree(nodeValue);
return true;
}
bool readXMLInteger64(xmlNodePtr node, const char* tag, int64_t& value)
{
char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
if(!nodeValue)
return false;
value = atoll(nodeValue);
xmlFree(nodeValue);
return true;
}
bool readXMLFloat(xmlNodePtr node, const char* tag, float& value)
{
char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
if(!nodeValue)
return false;
value = atof(nodeValue);
xmlFree(nodeValue);
return true;
}
bool readXMLString(xmlNodePtr node, const char* tag, std::string& value)
{
char* nodeValue = (char*)xmlGetProp(node, (xmlChar*)tag);
if(!nodeValue)
return false;
if(!utf8ToLatin1(nodeValue, value))
value = nodeValue;
xmlFree(nodeValue);
return true;
}
bool readXMLContentString(xmlNodePtr node, std::string& value)
{
char* nodeValue = (char*)xmlNodeGetContent(node);
if(!nodeValue)
return false;
if(!utf8ToLatin1(nodeValue, value))
value = nodeValue;
xmlFree(nodeValue);
return true;
}
bool parseXMLContentString(xmlNodePtr node, std::string& value)
{
bool result = false;
std::string compareValue;
while(node)
{
if(xmlStrcmp(node->name, (const xmlChar*)"text") && node->type != XML_CDATA_SECTION_NODE)
{
node = node->next;
continue;
}
if(!readXMLContentString(node, compareValue))
{
node = node->next;
continue;
}
trim_left(compareValue, "\r");
trim_left(compareValue, "\n");
trim_left(compareValue, " ");
if(compareValue.length() > value.length())
{
value = compareValue;
if(!result)
result = true;
}
node = node->next;
}
return result;
}
std::string getLastXMLError()
{
std::stringstream ss;
xmlErrorPtr lastError = xmlGetLastError();
if(lastError->line)
ss << "Line: " << lastError->line << ", ";
ss << "Info: " << lastError->message << std::endl;
return ss.str();
}
bool utf8ToLatin1(char* intext, std::string& outtext)
{
outtext = "";
if(!intext)
return false;
int32_t inlen = strlen(intext);
if(!inlen)
return false;
int32_t outlen = inlen * 2;
uint8_t* outbuf = new uint8_t[outlen];
int32_t res = UTF8Toisolat1(outbuf, &outlen, (uint8_t*)intext, &inlen);
if(res < 0)
{
delete[] outbuf;
return false;
}
outbuf[outlen] = '\0';
outtext = (char*)outbuf;
delete[] outbuf;
return true;
}
StringVec explodeString(const std::string& string, const std::string& separator)
{
StringVec returnVector;
size_t start = 0, end = 0;
while((end = string.find(separator, start)) != std::string::npos)
{
returnVector.push_back(string.substr(start, end - start));
start = end + separator.size();
}
returnVector.push_back(string.substr(start));
return returnVector;
}
IntegerVec vectorAtoi(StringVec stringVector)
{
IntegerVec returnVector;
for(StringVec::iterator it = stringVector.begin(); it != stringVector.end(); ++it)
returnVector.push_back(atoi((*it).c_str()));
return returnVector;
}
bool hasBitSet(uint32_t flag, uint32_t flags)
{
return ((flags & flag) == flag);
}
int32_t round(float v)
{
int32_t t = (int32_t)std::floor(v);
if((v - t) > 0.5)
return t + 1;
return t;
}
uint32_t rand24b()
{
return ((rand() << 12) ^ (rand())) & (0xFFFFFF);
}
float box_muller(float m, float s)
{
// normal random variate generator
// mean m, standard deviation s
float x1, x2, w, y1;
static float y2;
static bool useLast = false;
if(useLast) // use value from previous call
{
y1 = y2;
useLast = false;
return (m + y1 * s);
}
do
{
double r1 = (((float)(rand()) / RAND_MAX));
double r2 = (((float)(rand()) / RAND_MAX));
x1 = 2.0 * r1 - 1.0;
x2 = 2.0 * r2 - 1.0;
w = x1 * x1 + x2 * x2;
}
while(w >= 1.0);
w = sqrt((-2.0 * log(w)) / w);
y1 = x1 * w;
y2 = x2 * w;
useLast = true;
return (m + y1 * s);
}
int32_t random_range(int32_t lowestNumber, int32_t highestNumber, DistributionType_t type /*= DISTRO_UNIFORM*/)
{
if(highestNumber == lowestNumber)
return lowestNumber;
if(lowestNumber > highestNumber)
std::swap(lowestNumber, highestNumber);
switch(type)
{
case DISTRO_UNIFORM:
return (lowestNumber + ((int32_t)rand24b() % (highestNumber - lowestNumber + 1)));
case DISTRO_NORMAL:
return (lowestNumber + int32_t(float(highestNumber - lowestNumber) * (float)std::min((float)1, std::max((float)0, box_muller(0.5, 0.25)))));
default:
break;
}
const float randMax = 16777216;
return (lowestNumber + int32_t(float(highestNumber - lowestNumber) * float(1.f - sqrt((1.f * rand24b()) / randMax))));
}
char upchar(char character)
{
if((character >= 97 && character <= 122) || (character <= -1 && character >= -32))
character -= 32;
return character;
}
bool isNumber(char character)
{
return (character >= 48 && character <= 57);
}
bool isLowercaseLetter(char character)
{
return (character >= 97 && character <= 122);
}
bool isUppercaseLetter(char character)
{
return (character >= 65 && character <= 90);
}
bool isPasswordCharacter(char character)
{
return ((character >= 33 && character <= 47) || (character >= 58 && character <= 64) || (character >= 91 && character <= 96) || (character >= 123 && character <= 126));
}
bool isValidAccountName(std::string text)
{
toLowerCaseString(text);
uint32_t textLength = text.length();
for(uint32_t size = 0; size < textLength; size++)
{
if(!isLowercaseLetter(text[size]) && !isNumber(text[size]))
return false;
}
return true;
}
bool isValidPassword(std::string text)
{
toLowerCaseString(text);
uint32_t textLength = text.length();
for(uint32_t size = 0; size < textLength; size++)
{
if(!isLowercaseLetter(text[size]) && !isNumber(text[size]) && !isPasswordCharacter(text[size]))
return false;
}
return true;
}
bool isValidName(std::string text, bool forceUppercaseOnFirstLetter/* = true*/)
{
uint32_t textLength = text.length(), lenBeforeSpace = 1, lenBeforeQuote = 1, lenBeforeDash = 1, repeatedCharacter = 0;
char lastChar = 32;
if(forceUppercaseOnFirstLetter)
{
if(!isUppercaseLetter(text[0]))
return false;
}
else if(!isLowercaseLetter(text[0]) && !isUppercaseLetter(text[0]))
return false;
for(uint32_t size = 1; size < textLength; size++)
{
if(text[size] != 32)
{
lenBeforeSpace++;
if(text[size] != 39)
lenBeforeQuote++;
else
{
if(lenBeforeQuote <= 1 || size == textLength - 1 || text[size + 1] == 32)
return false;
lenBeforeQuote = 0;
}
if(text[size] != 45)
lenBeforeDash++;
else
{
if(lenBeforeDash <= 1 || size == textLength - 1 || text[size + 1] == 32)
return false;
lenBeforeDash = 0;
}
if(text[size] == lastChar)
{
repeatedCharacter++;
if(repeatedCharacter > 2)
return false;
}
else
repeatedCharacter = 0;
lastChar = text[size];
}
else
{
if(lenBeforeSpace <= 1 || size == textLength - 1 || text[size + 1] == 32)
return false;
lenBeforeSpace = lenBeforeQuote = lenBeforeDash = 0;
}
if(!(isLowercaseLetter(text[size]) || text[size] == 32 || text[size] == 39 || text[size] == 45
|| (isUppercaseLetter(text[size]) && text[size - 1] == 32)))
return false;
}
return true;
}
bool isNumbers(std::string text)
{
uint32_t textLength = text.length();
for(uint32_t size = 0; size < textLength; size++)
{
if(!isNumber(text[size]))
return false;
}
return true;
}
bool checkText(std::string text, std::string str)
{
trimString(text);
return asLowerCaseString(text) == str;
}
std::string generateRecoveryKey(int32_t fieldCount, int32_t fieldLenght, bool mixCase/* = false*/)
{
std::stringstream key;
int32_t i = 0, j = 0, lastNumber = 99, number = 0;
char character = 0, lastCharacter = 0;
bool madeNumber = false, madeCharacter = false;
do
{
do
{
madeNumber = madeCharacter = false;
if((mixCase && !random_range(0, 2)) || (!mixCase && (bool)random_range(0, 1)))
{
number = random_range(2, 9);
if(number != lastNumber)
{
key << number;
lastNumber = number;
madeNumber = true;
}
}
else
{
if(mixCase && (bool)random_range(0,1) )
character = (char)random_range(97, 122);
else
character = (char)random_range(65, 90);
if(character != lastCharacter)
{
key << character;
lastCharacter = character;
madeCharacter = true;
}
}
}
while((!madeCharacter && !madeNumber) ? true : (++j && j < fieldLenght));
lastCharacter = character = number = j = 0;
lastNumber = 99;
if(i < fieldCount - 1)
key << "-";
}
while(++i && i < fieldCount);
return key.str();
}
std::string trimString(std::string& str)
{
str.erase(str.find_last_not_of(" ") + 1);
return str.erase(0, str.find_first_not_of(" "));
}
std::string parseParams(tokenizer::iterator &it, tokenizer::iterator end)
{
if(it == end)
return "";
std::string tmp = (*it);
++it;
if(tmp[0] == '"')
{
tmp.erase(0, 1);
while(it != end && tmp[tmp.length() - 1] != '"')
{
tmp += " " + (*it);
++it;
}
if(tmp.length() > 0 && tmp[tmp.length() - 1] == '"')
tmp.erase(tmp.length() - 1);
}
return tmp;
}
std::string formatDate(time_t _time/* = 0*/)
{
if(!_time)
_time = time(NULL);
const tm* tms = localtime(&_time);
std::stringstream s;
if(tms)
s << tms->tm_mday << "/" << (tms->tm_mon + 1) << "/" << (tms->tm_year + 1900) << " " << tms->tm_hour << ":" << tms->tm_min << ":" << tms->tm_sec;
else
s << "UNIX Time: " << (int32_t)_time;
return s.str();
}
std::string formatDateEx(time_t _time/* = 0*/, std::string format/* = "%d %b %Y, %H:%M:%S"*/)
{
if(!_time)
_time = time(NULL);
const tm* tms = localtime(&_time);
char buffer[100];
if(tms)
strftime(buffer, 25, format.c_str(), tms);
else
sprintf(buffer, "UNIX Time: %d", (int32_t)_time);
return buffer;
}
std::string formatTime(time_t _time/* = 0*/, bool ms/* = false*/)
{
if(!_time)
_time = time(NULL);
else if(ms)
ms = false;
const tm* tms = localtime(&_time);
std::stringstream s;
if(tms)
{
s << tms->tm_hour << ":" << tms->tm_min << ":";
if(tms->tm_sec < 10)
s << "0";
s << tms->tm_sec;
if(ms)
{
timeb t;
ftime(&t);
s << "."; // make it format zzz
if(t.millitm < 10)
s << "0";
if(t.millitm < 100)
s << "0";
s << t.millitm;
}
}
else
s << "UNIX Time: " << (int32_t)_time;
return s.str();
}
std::string convertIPAddress(uint32_t ip)
{
char buffer[17];
sprintf(buffer, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24));
return buffer;
}
Skulls_t getSkulls(std::string strValue)
{
std::string tmpStrValue = asLowerCaseString(strValue);
if(tmpStrValue == "black" || tmpStrValue == "5")
return SKULL_BLACK;
if(tmpStrValue == "red" || tmpStrValue == "4")
return SKULL_RED;
if(tmpStrValue == "white" || tmpStrValue == "3")
return SKULL_WHITE;
if(tmpStrValue == "green" || tmpStrValue == "2")
return SKULL_GREEN;
if(tmpStrValue == "yellow" || tmpStrValue == "1")
return SKULL_YELLOW;
return SKULL_NONE;
}
PartyShields_t getShields(std::string strValue)
{
std::string tmpStrValue = asLowerCaseString(strValue);
if(tmpStrValue == "whitenoshareoff" || tmpStrValue == "10")
return SHIELD_YELLOW_NOSHAREDEXP;
if(tmpStrValue == "blueshareoff" || tmpStrValue == "9")
return SHIELD_BLUE_NOSHAREDEXP;
if(tmpStrValue == "yellowshareblink" || tmpStrValue == "8")
return SHIELD_YELLOW_NOSHAREDEXP_BLINK;
if(tmpStrValue == "blueshareblink" || tmpStrValue == "7")
return SHIELD_BLUE_NOSHAREDEXP_BLINK;
if(tmpStrValue == "yellowshareon" || tmpStrValue == "6")
return SHIELD_YELLOW_SHAREDEXP;
if(tmpStrValue == "blueshareon" || tmpStrValue == "5")
return SHIELD_BLUE_SHAREDEXP;
if(tmpStrValue == "yellow" || tmpStrValue == "4")
return SHIELD_YELLOW;
if(tmpStrValue == "blue" || tmpStrValue == "3")
return SHIELD_BLUE;
if(tmpStrValue == "whiteyellow" || tmpStrValue == "2")
return SHIELD_WHITEYELLOW;
if(tmpStrValue == "whiteblue" || tmpStrValue == "1")
return SHIELD_WHITEBLUE;
return SHIELD_NONE;
}
GuildEmblems_t getEmblems(std::string strValue)
{
std::string tmpStrValue = asLowerCaseString(strValue);
if(tmpStrValue == "blue" || tmpStrValue == "3")
return EMBLEM_BLUE;
if(tmpStrValue == "red" || tmpStrValue == "2")
return EMBLEM_RED;
if(tmpStrValue == "green" || tmpStrValue == "1")
return EMBLEM_GREEN;
return EMBLEM_NONE;
}
Direction getDirection(std::string string)
{
if(string == "north" || string == "n" || string == "0")
return NORTH;
if(string == "east" || string == "e" || string == "1")
return EAST;
if(string == "south" || string == "s" || string == "2")
return SOUTH;
if(string == "west" || string == "w" || string == "3")
return WEST;
if(string == "southwest" || string == "south west" || string == "south-west" || string == "sw" || string == "4")
return SOUTHWEST;
if(string == "southeast" || string == "south east" || string == "south-east" || string == "se" || string == "5")
return SOUTHEAST;
if(string == "northwest" || string == "north west" || string == "north-west" || string == "nw" || string == "6")
return NORTHWEST;
if(string == "northeast" || string == "north east" || string == "north-east" || string == "ne" || string == "7")
return NORTHEAST;
return SOUTH;
}
Direction getDirectionTo(Position pos1, Position pos2, bool extended/* = true*/)
{
Direction direction = NORTH;
if(pos1.x > pos2.x)
{
direction = WEST;
if(extended)
{
if(pos1.y > pos2.y)
direction = NORTHWEST;
else if(pos1.y < pos2.y)
direction = SOUTHWEST;
}
}
else if(pos1.x < pos2.x)
{
direction = EAST;
if(extended)
{
if(pos1.y > pos2.y)
direction = NORTHEAST;
else if(pos1.y < pos2.y)
direction = SOUTHEAST;
}
}
else
{
if(pos1.y > pos2.y)
direction = NORTH;
else if(pos1.y < pos2.y)
direction = SOUTH;
}
return direction;
}
Direction getReverseDirection(Direction dir)
{
switch(dir)
{
case NORTH:
return SOUTH;
case SOUTH:
return NORTH;
case WEST:
return EAST;
case EAST:
return WEST;
case SOUTHWEST:
return NORTHEAST;
case NORTHWEST:
return SOUTHEAST;
case NORTHEAST:
return SOUTHWEST;
case SOUTHEAST:
return NORTHWEST;
}
return SOUTH;
}
Position getNextPosition(Direction direction, Position pos)
{
switch(direction)
{
case NORTH:
pos.y--;
break;
case SOUTH:
pos.y++;
break;
case WEST:
pos.x--;
break;
case EAST:
pos.x++;
break;
case SOUTHWEST:
pos.x--;
pos.y++;
break;
case NORTHWEST:
pos.x--;
pos.y--;
break;
case SOUTHEAST:
pos.x++;
pos.y++;
break;
case NORTHEAST:
pos.x++;
pos.y--;
break;
}
return pos;