-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
executable file
·1241 lines (1115 loc) · 43.7 KB
/
game.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
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#ifdef USE_MYSQL
#include <mysql/mysql.h>
#endif
#include "game.h"
#include "modules.h"
GameClass Game;
// Constructor
GameClass::GameClass()
{
Channel = "#gekinzuku";
#ifdef USE_MYSQL
LoadFromMySQL();
#else
LoadFromDatabase();
#endif
LoadHelp();
}
// Saves everything to the MySQL Database
bool GameClass::SaveToMySQL()
{
#ifdef USE_MYSQL
MYSQL conn;
MYSQL_ROW Row;
MYSQL_RES *Result;
stringstream Query;
mysql_init(&conn);
// Connects
if (!mysql_real_connect(&conn, "localhost", "GameServ", "3d656fd40f981de0b98b835c2c6ed772", "GameServ", 3306, NULL, 0)) {
mysql_close(&conn);
return false;
}
// Truncates all the data! :o
if (mysql_query(&conn, "TRUNCATE Players") != 0 || mysql_query(&conn, "TRUNCATE Items") != 0 ||
mysql_query(&conn, "TRUNCATE Classes") != 0 || mysql_query(&conn, "TRUNCATE Races") != 0) {
mysql_close(&conn);
return false;
}
// Saves the players
for (int ID = 0; ID < Player.size(); ++ID)
{
Query.str(""); // Get rid of previous query
Query << "INSERT INTO Players (UserName, Password, Level, Exp, Gold, Health, Race, Class, Equip, Current, Gender, Items, Nick, Host) VALUES ";
Query << "(\"" << Player[ID].Name << "\", \"" << Player[ID].Password << "\", " << Player[ID].Level << ", " << Player[ID].Exp << ", " << Player[ID].Gold << ", " << Player[ID].Health << ", " << Player[ID].Race << ", " << Player[ID].Class << ", \"";
for (int i = 0; i < 3; ++i) Query << Player[ID].Equip[i] << ","; // Puts equiped items in
Query << "\", " << Player[ID].Current << ", \"" << Player[ID].Gender << "\", \"";
for (int i = 0; i < Player[ID].Item.size(); ++i) Query << Player[ID].Item[i].ID << " " << Player[ID].Item[i].Amount << ",";
Query << "\", \"" << Player[ID].Nick << "\", \"" << Player[ID].Host << "\")";
mysql_query(&conn, Query.str().c_str()); // Add the data!
}
// Saves the items
for (int ID = 0; ID < Item.size(); ++ID)
{
Query.str(""); // Clears the query
Query << "INSERT INTO Items (Name, Type, Requires, Power, Strength, UseUp, Description) VALUES ";
Query << "(\"" << Item[ID].Name << "\", " << Item[ID].Type << ", " << Item[ID].Requires << ", " << Item[ID].Power << ", " << Item[ID].Strength << ", " << Item[ID].Use << ", \"" << Item[ID].Description << "\")";
mysql_query(&conn, Query.str().c_str());
}
// Saves the Races
for (int ID = 0; ID < Race.size(); ++ID)
{
Query.str("");
Query << "INSERT INTO Races (Name, Burn, Critical, Energy, Freeze, Immunity, Magic, Money, Poison, Shock, OperOnly) VALUES ";
Query << "(\"" << Race[ID].Name << "\", " << Race[ID].Burn << ", " << Race[ID].Critical << ", " << Race[ID].Energy << ", " << Race[ID].Freeze << ", " << Race[ID].Immunity << ", " << Race[ID].Magic << ", " << Race[ID].Money << ", " << Race[ID].Poison << ", " << Race[ID].Shock << ", " << Race[ID].OperOnly << ")";
mysql_query(&conn, Query.str().c_str());
}
// Saves the Classes
for (int ID = 0; ID < Class.size(); ++ID)
{
Query.str("");
Query << "INSERT INTO Classes (Name, Endurance, ExpRate, Luck, Magic, Speed, Stealth, Strength, Wit, OperOnly) VALUES ";
Query << "(\"" << Class[ID].Name << "\", " << Class[ID].Endurance << ", " << Class[ID].ExpRate << ", " << Class[ID].Luck << ", " << Class[ID].Magic << ", " << Class[ID].Speed << ", " << Class[ID].Stealth << ", " << Class[ID].Strength << ", " << Class[ID].Wit << ", " << Class[ID].OperOnly << ")";
mysql_query(&conn, Query.str().c_str());
}
mysql_close(&conn);
return true;
#else
return false;
#endif
}
// Reloads everything from the MySQL database
bool GameClass::LoadFromMySQL()
{
#ifdef USE_MYSQL
MYSQL conn;
MYSQL_ROW Row;
MYSQL_RES *Result;
string TempStr;
mysql_init(&conn);
// Connects to the database
if (!mysql_real_connect(&conn, "localhost", "GameServ", "3d656fd40f981de0b98b835c2c6ed772", "GameServ", 3306, NULL, 0)) {
mysql_close(&conn);
return false;
}
// Gets the players
if (mysql_query(&conn, "SELECT * FROM Players") != 0) {
mysql_close(&conn);
return false;
}
Result = mysql_store_result(&conn);
// Make sure there are the right number of fields!
if (mysql_num_fields(Result) != 15) {
mysql_close(&conn);
return false;
}
Player.clear();
PlayerStruct TempPlayer;
ItemIDStruct TempItemID;
while (Row = mysql_fetch_row(Result))
{
TempPlayer.Name = Row[1];
TempPlayer.Password = Row[2];
TempPlayer.Level = atoi(Row[3]);
TempPlayer.Exp = atoi(Row[4]);
TempPlayer.Gold = atoi(Row[5]);
TempPlayer.Health = atoi(Row[6]);
TempPlayer.Race = atoi(Row[7]);
TempPlayer.Class = atoi(Row[8]);
TempStr = Row[9];
for (int EquipID = 0; TempStr.length() != 0; ++EquipID) {
TempPlayer.Equip[EquipID] = atoi(TempStr.substr(0,TempStr.find(",")).c_str());
TempStr = TempStr.substr(TempStr.find(",")+1);
}
TempPlayer.Current = atoi(Row[10]);
TempPlayer.Gender = Row[11][0];
TempStr = Row[12];
TempPlayer.Item.clear();
for (int ItemID = 0; TempStr.length() != 0; ++ItemID) {
TempItemID.ID = atoi(TempStr.substr(0,TempStr.find(" ")).c_str()); TempStr = TempStr.substr(TempStr.find(" ")+1);
TempItemID.Amount = atoi(TempStr.substr(0,TempStr.find(",")).c_str()); TempStr = TempStr.substr(TempStr.find(",")+1);
TempPlayer.Item.push_back(TempItemID);
}
TempPlayer.Nick = Row[13];
TempPlayer.Host = Row[14];
Player.push_back(TempPlayer);
}
// Gets the Items
if (mysql_query(&conn, "SELECT * FROM Items") != 0) {
mysql_close(&conn);
return false;
}
Result = mysql_store_result(&conn);
// Make sure there are the right number of fields!
if (mysql_num_fields(Result) != 8) {
mysql_close(&conn);
return false;
}
Item.clear();
ItemStruct TempItem;
while (Row = mysql_fetch_row(Result))
{
TempItem.Name = Row[1];
TempItem.Type = atoi(Row[2]);
TempItem.Requires = atoi(Row[3]);
TempItem.Power = atoi(Row[4]);
TempItem.Strength = atoi(Row[5]);
TempItem.Use = atoi(Row[6]);
TempItem.Description = Row[7];
Item.push_back(TempItem);
}
// Gets the Classes
if (mysql_query(&conn, "SELECT * FROM Classes") != 0) {
mysql_close(&conn);
return false;
}
Result = mysql_store_result(&conn);
// Make sure there are the right number of fields!
if (mysql_num_fields(Result) != 11) {
mysql_close(&conn);
return false;
}
Class.clear();
ClassStruct TempClass;
while (Row = mysql_fetch_row(Result))
{
TempClass.Name = Row[1];
TempClass.Endurance = atof(Row[2]);
TempClass.ExpRate = atof(Row[3]);
TempClass.Luck = atof(Row[4]);
TempClass.Magic = atof(Row[5]);
TempClass.Speed = atof(Row[6]);
TempClass.Stealth = atof(Row[7]);
TempClass.Strength = atof(Row[8]);
TempClass.Wit = atof(Row[9]);
TempClass.OperOnly = atoi(Row[10]);
Class.push_back(TempClass);
}
// Gets the Classes
if (mysql_query(&conn, "SELECT * FROM Races") != 0) {
mysql_close(&conn);
return false;
}
Result = mysql_store_result(&conn);
// Make sure there are the right number of fields!
if (mysql_num_fields(Result) != 12) {
mysql_close(&conn);
return false;
}
Race.clear();
RaceStruct TempRace;
while (Row = mysql_fetch_row(Result))
{
TempRace.Name = Row[1];
TempRace.Burn = atof(Row[2]);
TempRace.Critical = atof(Row[3]);
TempRace.Energy = atof(Row[4]);
TempRace.Freeze = atof(Row[5]);
TempRace.Immunity = atof(Row[6]);
TempRace.Magic = atof(Row[7]);
TempRace.Money = atof(Row[8]);
TempRace.Poison = atof(Row[9]);
TempRace.Shock = atof(Row[10]);
TempRace.OperOnly = atoi(Row[11]);
Race.push_back(TempRace);
}
mysql_close(&conn);
return LoadHelp();
#else
return false;
#endif
}
// Saves the data to a file
void GameClass::SaveToDatabase()
{
// Save the players
ofstream File("data/players.db");
for (unsigned int i = 0; i < Player.size(); ++i)
{
File << Player[i].Name << " " << Player[i].Password << " " << Player[i].Level << " " << Player[i].Exp << " " << Player[i].Gold << " " << Player[i].Health << " " << Player[i].Race << " " << Player[i].Class << " ";
// Saves the equiped items
File << Player[i].Equip[WEP_MELEE] << " " << Player[i].Equip[WEP_RANGED] << " " << Player[i].Equip[WEP_AMMO] << " ";
File << Player[i].Current << " " << Player[i].Gender << "!";
// Saves the items!
for (unsigned int ItemNum = 0; ItemNum < Player[i].Item.size(); ++ItemNum) File << Player[i].Item[ItemNum].ID << " " << Player[i].Item[ItemNum].Amount << ",";
// Saves if the user is logged in or not
if (Player[i].Nick != "" && Player[i].Host != "") // User is logged in
File << Player[i].Nick << " " << Player[i].Host;
File << "\n";
}
File.close();
// Save the classes
File.open("data/classes.db");
for (unsigned int i = 0; i < Class.size(); ++i)
File << Class[i].Name << " " << Class[i].Endurance << " " << Class[i].ExpRate << " " << Class[i].Luck << " " << Class[i].Magic << " " << Class[i].Speed << " " << Class[i].Stealth << " " << Class[i].Strength << " " << Class[i].Wit << " " << Class[i].OperOnly << "\n";
File.close();
// Save the races
File.open("data/races.db");
for (unsigned int i = 0; i < Race.size(); ++i)
File << Race[i].Name << " " << Race[i].Burn << " " << Race[i].Critical << " " << Race[i].Energy << " " << Race[i].Freeze << " " << Race[i].Immunity << " " << Race[i].Magic << " " << Race[i].Money << " " << Race[i].Poison << " " << Race[i].Shock << " " << Race[i].OperOnly << "\n";
File.close();
// Save the item database
File.open("data/items.db");
for (unsigned int i = 0; i < Item.size(); ++i)
File << Item[i].Name << "!" << Item[i].Type << " " << Item[i].Requires << " " << Item[i].Power << " " << Item[i].Strength << " " << Item[i].Use << " " << Item[i].Description << "\n";
File.close();
}
// Loads the data to a file
bool GameClass::LoadFromDatabase()
{
ifstream File("data/players.db");
if (File.is_open()) // Make sure file is open
{
Player.clear(); // Delete our current records
string Line; // Used for storing the data while reading from file
// Get the number of lines
int Lines = 0;
for (;!File.eof(); Lines++) getline(File, Line);
File.clear(); File.seekg(0); // Starts file over
Player.reserve(Lines); // Reserves the number of lines needed ahead of time
PlayerStruct TempPlayer; // Temp player to hold the data
ItemIDStruct ItemIdTemp;
while (getline(File, Line)) { // Add the new records
if (Line == "") break;
TempPlayer.Name = Line.substr(0,Line.find(" ")); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Password = Line.substr(0,Line.find(" ")); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Level = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Exp = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Gold = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Health = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Race = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Class = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Equip[0] = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Equip[1] = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Equip[2] = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Current = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Gender = Line[0]; Line = Line.substr(Line.find("!")+1);
// Gets the items
TempPlayer.Item.clear();
for (int i = 0; Line.find(",") != string::npos; ++i) {
ItemIdTemp.ID = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
ItemIdTemp.Amount = atoi(Line.substr(0,Line.find(",")).c_str()); Line = Line.substr(Line.find(",")+1);
TempPlayer.Item.push_back(ItemIdTemp); // Push it back!
}
if (Line != "") // User is logged in!
{
TempPlayer.Nick = Line.substr(0,Line.find(" ")); Line = Line.substr(Line.find(" ")+1);
TempPlayer.Host = Line.substr(0,Line.find("\n"));
} // Not logged in
else TempPlayer.Nick = TempPlayer.Host = "";
Player.push_back(TempPlayer);
}
}
else { // Failure? :(
File.close();
return false;
}
File.close();
File.open("data/classes.db");
if (File.is_open()) // Make sure file is open
{
Class.clear(); // Delete our current records
string Line; // Used for storing the data while reading from file
// Get the number of lines
int Lines = 0;
for (;!File.eof(); Lines++) getline(File, Line);
File.clear(); File.seekg(0); // Starts file over
Class.reserve(Lines); // Reserves the number of lines needed ahead of time
ClassStruct TempClass; // Temp Class to hold the data
while (getline(File, Line)) { // Add the new records
if (Line == "") break;
TempClass.Name = Line.substr(0,Line.find(" ")); Line = Line.substr(Line.find(" ")+1);
TempClass.Endurance = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.ExpRate = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.Luck = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.Magic = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.Speed = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.Stealth = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.Strength = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.Wit = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempClass.OperOnly = atoi(Line.substr(0,Line.find("\n")).c_str());
Class.push_back(TempClass);
}
}
else { // Failure? :(
File.close();
return false;
}
File.close();
File.open("data/races.db");
if (File.is_open()) // Make sure file is open
{
Race.clear(); // Delete our current records
string Line; // Used for storing the data while reading from file
// Get the number of lines
int Lines = 0;
for (;!File.eof(); Lines++) getline(File, Line);
File.clear(); File.seekg(0); // Starts file over
Race.reserve(Lines); // Reserves the number of lines needed ahead of time
RaceStruct TempRace; // Temp Race to hold the data
while (getline(File, Line)) { // Add the new records
if (Line == "") break;
TempRace.Name = Line.substr(0,Line.find(" ")); Line = Line.substr(Line.find(" ")+1);
TempRace.Burn = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Critical = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Energy = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Freeze = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Immunity = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Magic = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Money = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Poison = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.Shock = atof(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempRace.OperOnly = atoi(Line.substr(0,Line.find("\n")).c_str());
Race.push_back(TempRace);
}
}
else { // Failure? :(
File.close();
return false;
}
File.close();
// Time to load the items
File.open("data/items.db");
if (File.is_open())
{
Item.clear(); // Clear all previous help
string Line;
// Get the number of lines
int Lines = 0;
for (;!File.eof(); Lines++) getline(File, Line);
File.clear(); File.seekg(0); // Starts file over
Item.reserve(Lines); // Reserves the number of lines needed ahead of time
ItemStruct TempItem; // Temp to push onto the item object once we have all the info we need
while (getline(File, Line)) { // Add the new records
if (Line == "") break;
TempItem.Name = Line.substr(0,Line.find("!")); Line = Line.substr(Line.find("!")+1);
TempItem.Type = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempItem.Requires = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempItem.Power = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempItem.Strength = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempItem.Use = atoi(Line.substr(0,Line.find(" ")).c_str()); Line = Line.substr(Line.find(" ")+1);
TempItem.Description = Line.substr(0,Line.find("\n"));
Item.push_back(TempItem);
}
}
else {
File.close();
return false;
}
File.close();
return LoadHelp();
}
// Loads the help database
bool GameClass::LoadHelp()
{
ifstream File;
// Time to load the help!
File.open("data/help.db");
if (File.is_open())
{
HelpObj.clear(); // Clear all previous help
string Line;
HelpStruct HelpTemp; // Temp to push onto the help object once we have all the info we need
while (getline(File,Line)) {
if (Line.find("help=") != string::npos) { // Looking for the start of help command
HelpTemp.Output.clear(); // Clear out the old one!
GetDataFromLine(Line, HelpTemp.Name, "help=",2); // Get the name
HelpTemp.Name = ConvertToLower(HelpTemp.Name); // Covert it to lowercase to save us time in the actual program!
getline(File,Line);
while (Line != "endhelp") { // Check for the end of the help!
if (Line != "") HelpTemp.Output.push_back(Line);
if(!getline(File,Line)) { // If the user omitted a 'endhelp'
File.close();
return false; // error
}
}
HelpObj.push_back(HelpTemp); // Push the data onto the actual vector!
}
}
}
else {
File.close();
return false;
}
File.close();
}
// Gets race from nick
string GameClass::GetRaceFromName(string &Name)
{
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Name) == ConvertToLower(Name)) return Race[Player[i].Race].Name;
}
return "";
}
// Gets the ID from the nick
int GameClass::GetIDFromNick(string &Nick)
{
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Nick) == ConvertToLower(Nick)) return i;
}
return -1;
}
// Gets the player's name from nick
string GameClass::GetNameFromNick(string &Nick)
{
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Nick) == ConvertToLower(Nick)) return Player[i].Name;
}
return "";
}
// Gets an item's ID from it's name
int GameClass::GetItemIDFromItemName(string &Name)
{
for (unsigned int i = 0; i < Item.size(); ++i)
{
if (ConvertToLower(Item[i].Name) == ConvertToLower(Name)) return i;
}
return -1;
}
// Returns the item slot if a player has the item, otherwise returns -1
int GameClass::DoesPlayerHaveItem(int PlayerID, int ItemID)
{
for (unsigned int i = 0; i < Player[PlayerID].Item.size(); ++i)
{
if (Player[PlayerID].Item[i].ID == ItemID) return i;
}
return -1;
}
// Sends the Name
void GameClass::ShowStats(string Name, string Recipient)
{
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Name) == ConvertToLower(Name)) {
stringstream Msg;
Msg << Player[i].Name << " (" << (Player[i].Gender == 'M' ? "Male" : "Female") << ") the " << Race[Player[i].Race].Name << " " << Class[Player[i].Class].Name << " is level " << Player[i].Level << " and has " << Player[i].Exp << " EXP. Health: " << Player[i].Health << ". GekinMonies: " << Player[i].Gold;
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
}
// Not found!
IRCServer.SendNotice("No warrior with that name found.", Recipient);
}
// Uses up an item
void GameClass::UseItem(int PlayerID, int ItemID)
{
// Avoid seg faults
if (PlayerID > Player.size()-1) {
FatalError("A playerID is larger than the vector in UseItem()!");
return;
}
if (ItemID > Item.size()-1) {
FatalError("A ItemID is larger than the vector in UseItem()!");
return;
}
int ID = Player[PlayerID].Item[ItemID].ID;
int Amount = Player[PlayerID].Item[ItemID].Amount;
// Are we using a ranged weapon?
if (Player[PlayerID].Equip[WEP_RANGED] == ItemID) {
int AmmoID = Player[PlayerID].Equip[WEP_AMMO];
// Is the ammo supposed to get used up?
if (Item[Player[PlayerID].Item[AmmoID].ID].Use) {
if (Player[PlayerID].Item[AmmoID].Amount > 1) { // If we have more than 1 just subtract one
--Player[PlayerID].Item[AmmoID].Amount;
cout << "Subtracting one from the amount list.\n";
}
else { // Otherwise we gotta delete the item and unequip it!
Player[PlayerID].Equip[WEP_AMMO] = -1;
Player[PlayerID].Current = -1;
Player[PlayerID].Item.erase(Player[PlayerID].Item.begin()+AmmoID);
cout << "Deleting the item!\n";
}
}
}
// Is the item supposed to get used up?
if (Item[ID].Use) {
if (Amount > 1) { // If we have more than 1 just subtract one
--Player[PlayerID].Item[ItemID].Amount;
cout << "Subtracting one from the amount list.\n";
}
else { // Otherwise we gotta delete the item and unequip it!
if (Player[PlayerID].Equip[WEP_MELEE] == ItemID)
Player[PlayerID].Equip[WEP_MELEE] = -1;
else if (Player[PlayerID].Equip[WEP_RANGED] == ItemID)
Player[PlayerID].Equip[WEP_RANGED] = Player[PlayerID].Equip[WEP_AMMO] = -1;
Player[PlayerID].Current = -1;
Player[PlayerID].Item.erase(Player[PlayerID].Item.begin()+ItemID);
cout << "Deleting the item!\n";
}
}
}
// Lists things
void GameClass::List(string Stat, string Recipient)
{
if (ConvertToLower(Stat) == "races") { // Shows all the races
stringstream Msg;
Msg << "Races: ";
for (unsigned int i = 0; i < Race.size(); ++i)
{
Msg << Race[i].Name;
if (i != Race.size()-1) Msg << ", ";
}
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
else if (ConvertToLower(Stat) == "classes") { // Shows all the classes
stringstream Msg;
Msg << "Classes: ";
for (unsigned int i = 0; i < Class.size(); ++i)
{
Msg << Class[i].Name;
if (i != Class.size()-1) Msg << ", ";
}
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
else if (ConvertToLower(Stat) == "usersonline") { // Shows who's online
stringstream Msg;
Msg << "Users Online: ";
bool FirstName = true;
for (unsigned int i = 0; i < Player.size(); ++i)
{ // Don't show users who aren't online
if (Player[i].Nick == "") continue;
if (FirstName) {
Msg << Player[i].Name;
FirstName = false;
}
else Msg << ", " << Player[i].Name;
}
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
else if (ConvertToLower(Stat) == "items") { // Shows items
stringstream Msg;
Msg << "Items: ";
for (unsigned int i = 0; i < Item.size(); ++i)
{
Msg << Item[i].Name;
if (i != Item.size()-1) Msg << ", ";
}
IRCServer.SendNotice(Msg.str(), Channel);
return;
}
else if (ConvertToLower(Stat) == "myitems") { // Shows what we have
stringstream Msg;
Msg << "You have: ";
int PlayerNum = GetIDFromNick(Recipient);
if (PlayerNum == -1) {
stringstream Msg;
Msg << "Sorry, but you don't seem to be logged in!";
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
for (unsigned int i = 0; i < Player[PlayerNum].Item.size(); ++i)
{
Msg << Item[Player[PlayerNum].Item[i].ID].Name << " (" << Player[PlayerNum].Item[i].Amount << ")";
if (i != Player[PlayerNum].Item.size()-1) Msg << ", ";
}
// If this player has nothing
if (Player[PlayerNum].Item.size() == 0) Msg << "nothing.";
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
else if (ConvertToLower(Stat) == "equip")
{
stringstream Msg;
int ID = GetIDFromNick(Recipient);
if (ID == -1) return;
Msg << "You are using your ";
if (Player[ID].Current == WEP_MELEE) Msg << "melee weapon";
else if (Player[ID].Current == WEP_RANGED) Msg << "ranged weapon";
else if (Player[ID].Current == -1) Msg << "none";
Msg << ", and you have equiped: ";
if (Player[ID].Equip[WEP_MELEE] != -1) Msg << " Melee: " << Item[Player[ID].Item[Player[ID].Equip[WEP_MELEE]].ID].Name;
if (Player[ID].Equip[WEP_RANGED] != -1) Msg << " Ranged: " << Item[Player[ID].Item[Player[ID].Equip[WEP_RANGED]].ID].Name;
if (Player[ID].Equip[WEP_AMMO] != -1) Msg << " Ammo: " << Item[Player[ID].Item[Player[ID].Equip[WEP_AMMO]].ID].Name;
if (Msg.str() == "") Msg << "You have nothing! :D:D:D:D:D:D:D:D";
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
// Not found!
stringstream Msg;
Msg << "Unable to list '" << Stat << "'.";
IRCServer.SendNotice(Msg.str(), Recipient);
}
// Attacking
void GameClass::Attack(string &PlayerNick, string &Victim, string &Recipient, string &Host)
{
// Sees if Player actually exists
int PlayerNum = -1, VictimNum = -1;
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Nick) == ConvertToLower(PlayerNick)) PlayerNum = i;
if (ConvertToLower(Player[i].Name) == ConvertToLower(Victim)) VictimNum = i;
}
// No player with this name?
if (PlayerNum == -1) {
IRCServer.SendNotice(ERROR_BADLOGIN, Recipient);
return;
}
// No victim with that name?
if (VictimNum == -1) {
IRCServer.SendNotice("There is no warrior with that name.", Recipient);
return;
}
// Attacking themseleves?
if (PlayerNum == VictimNum) {
IRCServer.SendNotice("You can't attack yourself!", Recipient);
return;
}
// We know we have a proper PlayerNum and VictimNum. But we need to verify the user is who they say they are still!
if (Player[PlayerNum].Host != Host)
{
IRCServer.SendNotice(ERROR_BADHOST, Recipient);
return;
}
// Is this player dead?
if (Player[PlayerNum].Health == 0) {
IRCServer.SendNotice("You are dead!", Recipient);
return;
}
// Is the enemy dead?
if (Player[VictimNum].Health == 0) {
stringstream Msg;
Msg << Player[VictimNum].Name << " is already dead!";
IRCServer.SendNotice(Msg.str(), Recipient);
return;
}
// Chance of hitting
if (rand()%1500 + (100*Class[Player[PlayerNum].Class].Speed*2) < (100*Class[Player[VictimNum].Class].Luck*0.75) + (100*Class[Player[VictimNum].Class].Speed*3)) {
stringstream Msg;
Msg << Player[VictimNum].Name << " dodged the attack!";
IRCServer.SendNotice(Msg.str(), Recipient);
if (Player[VictimNum].Nick != "") {
Msg.str("");
Msg << Player[PlayerNum].Name << " tried to attack you!";
IRCServer.SendNotice(Msg.str(), Player[VictimNum].Nick);
}
return;
}
// Weapon power
int PlayerWep = 0, VictimWep = 0;
// Melee weapons
if (Player[PlayerNum].Current == WEP_MELEE && Player[PlayerNum].Equip[WEP_MELEE] != -1) {
PlayerWep = Item[Player[PlayerNum].Item[Player[PlayerNum].Equip[WEP_MELEE]].ID].Power;
UseItem(PlayerNum, Player[PlayerNum].Equip[WEP_MELEE]);
} // Victim
if (Player[VictimNum].Current == WEP_MELEE && Player[VictimNum].Equip[WEP_MELEE] != -1) {
VictimWep = Item[Player[VictimNum].Item[Player[VictimNum].Equip[WEP_MELEE]].ID].Power;
UseItem(VictimNum, Player[VictimNum].Equip[WEP_MELEE]);
}
// Ranged weapons
if (Player[PlayerNum].Current == WEP_RANGED && Player[PlayerNum].Equip[WEP_RANGED] != -1 && Player[PlayerNum].Equip[WEP_AMMO] != -1) {
PlayerWep = Item[Player[PlayerNum].Item[Player[PlayerNum].Equip[WEP_RANGED]].ID].Power + Item[Player[PlayerNum].Item[Player[PlayerNum].Equip[WEP_AMMO]].ID].Power;
UseItem(PlayerNum, Player[PlayerNum].Equip[WEP_RANGED]);
} // Victim
if (Player[VictimNum].Current == WEP_RANGED && Player[VictimNum].Equip[WEP_RANGED] != -1 && Player[VictimNum].Equip[WEP_AMMO] != -1) {
VictimWep = Item[Player[VictimNum].Item[Player[VictimNum].Equip[WEP_RANGED]].ID].Power + Item[Player[VictimNum].Item[Player[VictimNum].Equip[WEP_AMMO]].ID].Power;
UseItem(VictimNum, Player[VictimNum].Equip[WEP_RANGED]);
}
// How much damage is dealt to each player
float DamageV = rand()%4+PlayerWep + 1, DamageP = rand()%3+VictimWep + 1;
// Adds the damage modifiers
DamageV += (Player[PlayerNum].Level * Class[Player[PlayerNum].Class].Wit * (Class[Player[PlayerNum].Class].Strength*2))*0.3 - (Player[VictimNum].Level * Class[Player[VictimNum].Class].Wit * (Class[Player[VictimNum].Class].Endurance*2))*0.1;
DamageP += (Player[VictimNum].Level * Class[Player[VictimNum].Class].Wit * (Class[Player[VictimNum].Class].Strength*2))*0.3 - (Player[PlayerNum].Level * Class[Player[PlayerNum].Class].Wit * (Class[Player[PlayerNum].Class].Endurance*2))*0.1;
// Determines chance of critical hit
if (rand()%1500 < (100*Race[Player[PlayerNum].Race].Critical) + (100*Class[Player[PlayerNum].Class].Wit*0.5)) DamageV *= 1.5;
// We don't want negative or '0' damage
if (DamageV < 1) DamageV = 1; if (DamageP < 1) DamageP = 1;
// We don't want negative health!
if (DamageV > Player[VictimNum].Health) DamageV = Player[VictimNum].Health;
if (DamageP > Player[PlayerNum].Health) DamageP = Player[PlayerNum].Health;
// Actually subtracts the health
Player[VictimNum].Health -= round(DamageV);
Player[PlayerNum].Health -= round(DamageP);
// Round the damage cause all that's left is the stats
DamageP = round(DamageP); DamageV = round(DamageV);
stringstream MsgP, MsgV;
MsgV << "You were attacked by " << Player[PlayerNum].Name << " for " << DamageV << " damage, and you countered for " << DamageP << " damage";
MsgP << "You attacked " << Player[VictimNum].Name << " for " << DamageV << " damage, and received " << DamageP << " damage";
if (Player[PlayerNum].Health > 0) {
// The attacker always gets cash unless they died in the prcoess! Amount they get depends on how well they fought.
int Ratio = 1;
if (Player[PlayerNum].Level > Player[VictimNum].Level) Ratio = (Player[PlayerNum].Level-Player[VictimNum].Level);
if (Ratio < 1) Ratio = 1;
if (Ratio > 10) Ratio = 10;
int Gold = (Player[VictimNum].Gold)*((Class[Player[PlayerNum].Class].Stealth*Ratio)/50)+ rand()%3;
if (Gold < 1) Gold = 1;
Player[PlayerNum].Gold += Gold;
Player[VictimNum].Gold -= Gold;
// Exp - (Race.ExpRate * 23 * EnemyLevel)/7
int Exp = (Class[Player[PlayerNum].Class].ExpRate * 23 * Player[VictimNum].Level)/7;
Player[PlayerNum].Exp += Exp;
if (Exp < 1) Exp = 1; // Always give at least one EXP!
MsgP << " gaining " << Exp << " EXP and stealing " << Gold << " GekinMonies.";
MsgV << " You lost " << Gold << " GekinMonies in the fight";
if (CheckLevelUp(PlayerNum)) MsgP << " You grew to level " << Player[PlayerNum].Level << "!";
}
else {
MsgV << ". " << Player[PlayerNum].Name << " died in combat.";
MsgP << ". You died in combat.";
// If the attack died and the victim didn't the victim gets a boost of gold!
if (Player[VictimNum].Health > 0) {
int Ratio = 1;
if (Player[VictimNum].Level > Player[PlayerNum].Level) Ratio = (Player[VictimNum].Level-Player[PlayerNum].Level);
if (Ratio < 1) Ratio = 1;
if (Ratio > 10) Ratio = 10;
int Gold = (Player[PlayerNum].Gold)*((Class[Player[VictimNum].Class].Stealth*Ratio)/47)+ rand()%4;
if (Gold < 1) Gold = 1;
Player[VictimNum].Gold += Gold;
Player[PlayerNum].Gold -= Gold;
MsgV << " You looted " << Gold << " GekinMonies as well";
MsgP << " You lost " << Gold << " GekinMonies!";
}
}
if (Player[VictimNum].Health > 0) {
// Exp - (Race.ExpRate * 23 * EnemyLevel)/7
int Exp = (Class[Player[VictimNum].Class].ExpRate * 23 * Player[PlayerNum].Level)/21;
Player[VictimNum].Exp += Exp;
if (Exp < 1) Exp = 1; // Always give at least one EXP!
MsgV << " and you gained " << Exp << " EXP.";
if (CheckLevelUp(VictimNum)) MsgV << " You grew to level " << Player[VictimNum].Level << "!";
}
else {
MsgV << " and you were slain!";
MsgP << " " << Player[VictimNum].Name << " has been slain.";
}
// Sends the player's the info of the battle
IRCServer.SendNotice(MsgP.str(), Player[PlayerNum].Nick);
if (Player[VictimNum].Nick != "") IRCServer.SendNotice(MsgV.str(), Player[VictimNum].Nick);
}
// Logs the user out regardless of their host
void GameClass::Ghost(string &PlayerName, string &Password, string &Speaker)
{
// Sees if Player actually exists
int PlayerNum = -1;
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Name) == ConvertToLower(PlayerName) && Player[i].Password == Password) PlayerNum = i;
}
// No player with this name?
if (PlayerNum == -1) {
IRCServer.SendNotice("Your ghosting credentials are invalid.", Speaker);
return;
}
// We can logout!
Player[PlayerNum].Nick = Player[PlayerNum].Host = "";
IRCServer.SendNotice("You have successfully logged out.", Speaker);
}
// Logs in, if they have the right password!
void GameClass::LogIn(string &PlayerName, string &PlayerNick, string &Password, string &Host)
{
// Makes sure we aren't already logged in as someone else
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Nick) == ConvertToLower(PlayerNick)) {
// Breaks if the names match
if (ConvertToLower(PlayerName) == ConvertToLower(Player[i].Name)) break;
stringstream Msg;
Msg << "Error: You are already logged in as " << Player[i].Name << ". If you want to login as " << PlayerName << " instead, please /msg GameServ logout first.";
IRCServer.SendNotice(Msg.str(), PlayerNick);
return;
}
}
// Loop through the players
for (unsigned int i = 0; i < Player.size(); ++i)
{
// Successful login?
if (ConvertToLower(Player[i].Name) == ConvertToLower(PlayerName) && Player[i].Password == Password) {
Player[i].Host = Host;
Player[i].Nick = PlayerNick;
stringstream Msg;
Msg << "You are now logged in as " << Player[i].Name;
IRCServer.SendNotice(Msg.str(), PlayerNick);
ShowStats(Player[i].Name, PlayerNick);
return;
}
}
IRCServer.SendNotice("Your login credentials are invalid.", PlayerNick);
}
// Logs out the player. (Assuming they are logged in!)
void GameClass::LogOut(string &PlayerNick, string &Host)
{
// Sees if Player actually exists
int PlayerNum = -1;
for (unsigned int i = 0; i < Player.size(); ++i)
{
if (ConvertToLower(Player[i].Nick) == ConvertToLower(PlayerNick)) PlayerNum = i;
}
// No player with this name?
if (PlayerNum == -1) {
IRCServer.SendNotice(ERROR_BADLOGIN, PlayerNick);
return;
}
// We know we have a proper PlayerNum and VictimNum. But we need to verify the user is who they say they are still!
if (Player[PlayerNum].Host != Host)
{
IRCServer.SendNotice(ERROR_BADHOST, PlayerNick);
return;
}
// We can logout!
Player[PlayerNum].Nick = Player[PlayerNum].Host = "";
IRCServer.SendNotice("You have successfully logged out.", PlayerNick);
}
// Levels up a player if they have enough EXP!
bool GameClass::CheckLevelUp(int PlayerID)
{
int PrevLvl = Player[PlayerID].Level;
int Exp = Player[PlayerID].Exp;
int NextLevel = (2 * pow((Player[PlayerID].Level+2), 3))/3;
do {
if (NextLevel < Exp) ++Player[PlayerID].Level;
NextLevel = (2 * pow((Player[PlayerID].Level+2), 3))/3;
} while (NextLevel < Exp);
if (PrevLvl < Player[PlayerID].Level) return true;
return false;
}
// Updates the player's nick
void GameClass::TrackUser(string OldNick, string NewNick)
{
int PlayerNum = GetIDFromNick(OldNick);
if (PlayerNum == -1) return; // Person isn't playing game
// Update the nick
Player[PlayerNum].Nick = NewNick;
}
// Logs a user out if they left
void GameClass::UserQuit(string Nick)
{
int PlayerNum = GetIDFromNick(Nick);
if (PlayerNum == -1) return; // Person isn't playing game
// Log them out
Player[PlayerNum].Nick = Player[PlayerNum].Host = "";
}
// Equips an item
void GameClass::Equip(string &Type, string &ItemName, string &Speaker, string &Host)