forked from otland/tfs-old-svn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.cpp
1514 lines (1352 loc) · 44.2 KB
/
commands.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 2
// 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, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include <string>
#include <fstream>
#include <utility>
#include <cstring>
#include <cerrno>
#include "commands.h"
#include "player.h"
#include "npc.h"
#include "monsters.h"
#include "game.h"
#include "actions.h"
#include "house.h"
#include "iologindata.h"
#include "tools.h"
#include "ban.h"
#include "configmanager.h"
#include "town.h"
#include "spells.h"
#include "talkaction.h"
#include "movement.h"
#include "spells.h"
#include "weapons.h"
#include "raids.h"
#include "chat.h"
#include "quests.h"
#include "mounts.h"
#include "globalevent.h"
#ifdef __ENABLE_SERVER_DIAGNOSTIC__
#include "outputmessage.h"
#include "connection.h"
#include "admin.h"
#include "status.h"
#include "protocollogin.h"
#endif
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
extern ConfigManager g_config;
extern Actions* g_actions;
extern Monsters g_monsters;
extern Npcs g_npcs;
extern TalkActions* g_talkActions;
extern MoveEvents* g_moveEvents;
extern Spells* g_spells;
extern Weapons* g_weapons;
extern Game g_game;
extern Chat g_chat;
extern CreatureEvents* g_creatureEvents;
extern GlobalEvents* g_globalEvents;
s_defcommands Commands::defined_commands[] =
{
//admin commands
{"/s", &Commands::placeNpc},
{"/m", &Commands::placeMonster},
{"/summon", &Commands::placeSummon},
{"/B", &Commands::broadcastMessage},
{"/b", &Commands::banPlayer},
{"/t", &Commands::teleportMasterPos},
{"/c", &Commands::teleportHere},
{"/i", &Commands::createItemById},
{"/n", &Commands::createItemByName},
{"/q", &Commands::subtractMoney},
{"/reload", &Commands::reloadInfo},
{"/goto", &Commands::teleportTo},
{"/info", &Commands::getInfo},
{"/closeserver", &Commands::closeServer},
{"/openserver", &Commands::openServer},
{"/a", &Commands::teleportNTiles},
{"/kick", &Commands::kickPlayer},
{"/owner", &Commands::setHouseOwner},
{"/gethouse", &Commands::getHouse},
{"/town", &Commands::teleportToTown},
{"/up", &Commands::changeFloor},
{"/down", &Commands::changeFloor},
{"/pos", &Commands::showPosition},
{"/r", &Commands::removeThing},
{"/newtype", &Commands::newType},
{"/raid", &Commands::forceRaid},
{"/addskill", &Commands::addSkill},
{"/ban", &Commands::ban},
{"/unban", &Commands::unban},
{"/ghost", &Commands::ghost},
{"/clean", &Commands::clean},
{"/mccheck", &Commands::multiClientCheck},
{"/serverdiag", &Commands::serverDiag},
// player commands - TODO: make them talkactions
{"!online", &Commands::whoIsOnline},
{"!buyhouse", &Commands::buyHouse},
{"!sellhouse", &Commands::sellHouse},
{"!serverinfo", &Commands::serverInfo},
{"!kills", &Commands::playerKills},
{"!createguild", &Commands::createGuild},
{"!joinguild", &Commands::joinGuild}
};
Commands::Commands()
{
loaded = false;
//setup command map
for(uint32_t i = 0; i < sizeof(defined_commands) / sizeof(defined_commands[0]); i++)
{
Command* cmd = new Command;
cmd->loadedGroupId = false;
cmd->loadedAccountType = false;
cmd->loadedLogging = false;
cmd->logged = true;
cmd->groupId = 1;
cmd->f = defined_commands[i].f;
std::string key = defined_commands[i].name;
commandMap[key] = cmd;
}
}
bool Commands::loadFromXml()
{
std::string filename = "data/XML/commands.xml";
xmlDocPtr doc = xmlParseFile(filename.c_str());
if(doc)
{
loaded = true;
xmlNodePtr root, p;
root = xmlDocGetRootElement(doc);
if(xmlStrcmp(root->name,(const xmlChar*)"commands"))
{
std::cout << "[Error - Commands::loadFromXml] Malformed commands file." << std::endl;
xmlFreeDoc(doc);
return false;
}
std::string strCmd;
p = root->children;
while(p)
{
if(xmlStrcmp(p->name, (const xmlChar*)"command") == 0)
{
if(readXMLString(p, "cmd", strCmd))
{
CommandMap::iterator it = commandMap.find(strCmd);
int32_t gId;
int32_t aTypeLevel;
std::string strValue;
if(it != commandMap.end())
{
Command* command = it->second;
if(readXMLInteger(p, "group", gId))
{
if(!command->loadedGroupId)
{
command->groupId = gId;
command->loadedGroupId = true;
}
else
std::cout << "Duplicated command " << strCmd << std::endl;
}
else
std::cout << "missing group tag for " << strCmd << std::endl;
if(readXMLInteger(p, "acctype", aTypeLevel))
{
if(!command->loadedAccountType)
{
command->accountType = (AccountType_t)aTypeLevel;
command->loadedAccountType = true;
}
else
std::cout << "Duplicated command " << strCmd << std::endl;
}
else
std::cout << "missing acctype tag for " << strCmd << std::endl;
if(readXMLString(p, "log", strValue))
{
if(!command->loadedLogging)
{
command->logged = booleanString(strValue);
command->loadedLogging = true;
}
else
std::cout << "Duplicated log tag for " << strCmd << std::endl;
}
else
std::cout << "Missing log tag for " << strCmd << std::endl;
}
else
std::cout << "Unknown command " << strCmd << std::endl;
}
else
std::cout << "missing cmd." << std::endl;
}
p = p->next;
}
xmlFreeDoc(doc);
}
for(CommandMap::iterator it = commandMap.begin(), end = commandMap.end(); it != end; ++it)
{
Command* command = it->second;
if(!command->loadedGroupId)
std::cout << "Warning: Missing group id for command " << it->first << std::endl;
if(!command->loadedAccountType)
std::cout << "Warning: Missing acctype level for command " << it->first << std::endl;
if(!command->loadedLogging)
std::cout << "Warning: Missing log command " << it->first << std::endl;
g_game.addCommandTag(it->first.substr(0, 1));
}
return loaded;
}
bool Commands::reload()
{
loaded = false;
for(CommandMap::iterator it = commandMap.begin(), end = commandMap.end(); it != end; ++it)
{
Command* command = it->second;
command->groupId = 1;
command->accountType = ACCOUNT_TYPE_GOD;
command->loadedGroupId = false;
command->loadedAccountType = false;
command->logged = true;
command->loadedLogging = false;
}
g_game.resetCommandTag();
return loadFromXml();
}
bool Commands::exeCommand(Creature* creature, const std::string& cmd)
{
Player* player = creature->getPlayer();
if(!player)
return false;
std::string str_command;
std::string str_param;
std::string::size_type loc = cmd.find(' ', 0);
if(loc != std::string::npos)
{
str_command = std::string(cmd, 0, loc);
str_param = std::string(cmd, (loc + 1), cmd.size() - loc - 1);
}
else
{
str_command = cmd;
str_param = std::string("");
}
//find command
CommandMap::iterator it = commandMap.find(str_command);
if(it == commandMap.end())
return false;
Command* command = it->second;
if(command->groupId > player->groupId || command->accountType > player->accountType || player->isAccountManager())
{
if(player->accessLevel)
player->sendTextMessage(MSG_STATUS_SMALL, "You can not execute this command.");
return false;
}
//execute command
CommandFunc cfunc = command->f;
(this->*cfunc)(player, str_command, str_param);
if(command->logged)
{
player->sendTextMessage(MSG_STATUS_CONSOLE_RED, cmd.c_str());
if(dirExists("data/logs") || createDir("data/logs"))
{
std::ostringstream ss;
ss << "data/logs/" << player->getName() << " commands.log";
std::ofstream out(ss.str().c_str(), std::ios::app);
time_t ticks = time(NULL);
const tm* now = localtime(&ticks);
char buf[32];
strftime(buf, sizeof(buf), "%d/%m/%Y %H:%M", now);
out << "[" << buf << "] " << cmd << std::endl;
out.close();
}
else
std::cout << "[Warning - Commands::exeCommand] Cannot access \"data/logs\" for writing: " << strerror(errno) << "." << std::endl;
}
return true;
}
void Commands::placeNpc(Player* player, const std::string& cmd, const std::string& param)
{
Npc* npc = Npc::createNpc(param);
if(!npc)
return;
// Place the npc
if(g_game.placeCreature(npc, player->getPosition()))
{
g_game.addMagicEffect(player->getPosition(), NM_ME_MAGIC_BLOOD);
npc->setMasterPos(npc->getPosition());
}
else
{
delete npc;
player->sendCancelMessage(RET_NOTENOUGHROOM);
g_game.addMagicEffect(player->getPosition(), NM_ME_POFF);
}
}
void Commands::placeMonster(Player* player, const std::string& cmd, const std::string& param)
{
Monster* monster = Monster::createMonster(param);
if(!monster)
{
player->sendCancelMessage(RET_NOTPOSSIBLE);
g_game.addMagicEffect(player->getPosition(), NM_ME_POFF);
return;
}
// Place the monster
if(g_game.placeCreature(monster, player->getPosition()))
{
g_game.addMagicEffect(monster->getPosition(), NM_ME_TELEPORT);
g_game.addMagicEffect(player->getPosition(), NM_ME_MAGIC_BLOOD);
}
else
{
delete monster;
player->sendCancelMessage(RET_NOTENOUGHROOM);
g_game.addMagicEffect(player->getPosition(), NM_ME_POFF);
}
}
void Commands::placeSummon(Player* player, const std::string& cmd, const std::string& param)
{
Monster* monster = Monster::createMonster(param);
if(!monster)
{
player->sendCancelMessage(RET_NOTPOSSIBLE);
g_game.addMagicEffect(player->getPosition(), NM_ME_POFF);
return;
}
// Place the monster
player->addSummon(monster);
if(!g_game.placeCreature(monster, player->getPosition()))
{
player->removeSummon(monster);
player->sendCancelMessage(RET_NOTENOUGHROOM);
g_game.addMagicEffect(player->getPosition(), NM_ME_POFF);
}
else
g_game.addMagicEffect(monster->getPosition(), NM_ME_TELEPORT);
}
void Commands::broadcastMessage(Player* player, const std::string& cmd, const std::string& param)
{
g_game.playerBroadcastMessage(player, param);
}
void Commands::banPlayer(Player* player, const std::string& cmd, const std::string& param)
{
Player* playerBan = g_game.getPlayerByName(param);
if(playerBan)
{
if(playerBan->hasFlag(PlayerFlag_CannotBeBanned))
{
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "You cannot ban this player.");
return;
}
playerBan->sendTextMessage(MSG_STATUS_CONSOLE_RED, "You have been banned.");
uint32_t ip = playerBan->lastIP;
if(ip > 0)
IOBan::getInstance()->addIpBan(ip, 0xFFFFFFFF, (time(NULL) + 86400));
playerBan->kickPlayer(true);
}
}
void Commands::teleportMasterPos(Player* player, const std::string& cmd, const std::string& param)
{
Position oldPosition = player->getPosition();
Position destPos = player->masterPos;
Position newPosition = g_game.getClosestFreeTile(player, 0, destPos, true);
if(player->getPosition() != destPos)
{
if(newPosition.x == 0)
player->sendCancel("You can not teleport there.");
else if(g_game.internalTeleport(player, newPosition) == RET_NOERROR)
{
g_game.addMagicEffect(oldPosition, NM_ME_POFF, player->isInGhostMode());
g_game.addMagicEffect(newPosition, NM_ME_TELEPORT, player->isInGhostMode());
}
}
}
void Commands::teleportHere(Player* player, const std::string& cmd, const std::string& param)
{
Creature* paramCreature = g_game.getCreatureByName(param);
if(paramCreature)
{
Position oldPosition = paramCreature->getPosition();
Position destPos = paramCreature->getPosition();
Position newPosition = g_game.getClosestFreeTile(player, paramCreature, player->getPosition(), false);
if(newPosition.x == 0)
{
std::ostringstream ss;
ss << "You can not teleport " << paramCreature->getName() << std::endl;
player->sendCancel(ss.str());
}
else if(g_game.internalTeleport(paramCreature, newPosition) == RET_NOERROR)
{
g_game.addMagicEffect(oldPosition, NM_ME_POFF, paramCreature->isInGhostMode());
g_game.addMagicEffect(newPosition, NM_ME_TELEPORT, paramCreature->isInGhostMode());
}
}
else
player->sendCancel("A creature with that name could not be found.");
}
void Commands::createItemById(Player* player, const std::string& cmd, const std::string& param)
{
std::string tmp = param;
std::string::size_type pos = tmp.find(' ', 0);
if(pos == std::string::npos)
pos = tmp.size();
int32_t type = atoi(tmp.substr(0, pos).c_str());
int32_t count = 1;
if(pos < tmp.size())
{
tmp.erase(0, pos + 1);
count = std::max(1, std::min(atoi(tmp.c_str()), 100));
}
Item* newItem = Item::CreateItem(type, count);
if(!newItem)
return;
ReturnValue ret = g_game.internalAddItem(player, newItem);
if(ret != RET_NOERROR)
{
ret = g_game.internalAddItem(player->getTile(), newItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
if(ret != RET_NOERROR)
{
delete newItem;
return;
}
}
g_game.startDecay(newItem);
g_game.addMagicEffect(player->getPosition(), NM_ME_MAGIC_POISON);
}
void Commands::createItemByName(Player* player, const std::string& cmd, const std::string& param)
{
std::string::size_type pos1 = param.find("\"");
pos1 = (std::string::npos == pos1 ? 0 : pos1 + 1);
std::string::size_type pos2 = param.rfind("\"");
if(pos2 == pos1 || pos2 == std::string::npos)
{
pos2 = param.rfind(' ');
if(pos2 == std::string::npos)
pos2 = param.size();
}
std::string itemName = param.substr(pos1, pos2 - pos1);
int32_t count = 1;
if(pos2 < param.size())
{
std::string itemCount = param.substr(pos2 + 1, param.size() - (pos2 + 1));
count = std::min(atoi(itemCount.c_str()), 100);
}
int32_t itemId = Item::items.getItemIdByName(itemName);
if(itemId == -1)
{
player->sendTextMessage(MSG_STATUS_CONSOLE_RED, "Item could not be summoned.");
return;
}
Item* newItem = Item::CreateItem(itemId, count);
if(!newItem)
return;
ReturnValue ret = g_game.internalAddItem(player, newItem);
if(ret != RET_NOERROR)
{
ret = g_game.internalAddItem(player->getTile(), newItem, INDEX_WHEREEVER, FLAG_NOLIMIT);
if(ret != RET_NOERROR)
{
delete newItem;
return;
}
}
g_game.startDecay(newItem);
g_game.addMagicEffect(player->getPosition(), NM_ME_MAGIC_POISON);
}
void Commands::subtractMoney(Player* player, const std::string& cmd, const std::string& param)
{
int32_t count = atoi(param.c_str());
uint32_t money = g_game.getMoney(player);
if(count <= 0)
{
std::ostringstream ss;
ss << "You have " << money << " gold.";
player->sendCancel(ss.str());
}
else if(count > (int32_t)money)
{
std::ostringstream ss;
ss << "You have " << money << " gold which is not sufficient.";
player->sendCancel(ss.str());
}
else if(!g_game.removeMoney(player, count))
player->sendCancel("Can not subtract money!");
}
void Commands::reloadInfo(Player* player, const std::string& cmd, const std::string& param)
{
std::string tmpParam = asLowerCaseString(param);
if(tmpParam == "action" || tmpParam == "actions")
{
g_actions->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded actions.");
}
else if(tmpParam == "config" || tmpParam == "configuration")
{
g_config.reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded config.");
}
else if(tmpParam == "command" || tmpParam == "commands")
{
reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded commands.");
}
else if(tmpParam == "creaturescript" || tmpParam == "creaturescripts")
{
g_creatureEvents->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded creature scripts.");
}
else if(tmpParam == "monster" || tmpParam == "monsters")
{
g_monsters.reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded monsters.");
}
else if(tmpParam == "move" || tmpParam == "movement" || tmpParam == "movements"
|| tmpParam == "moveevents" || tmpParam == "moveevent")
{
g_moveEvents->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded movements.");
}
else if(tmpParam == "npc" || tmpParam == "npcs")
{
g_npcs.reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded npcs.");
}
else if(tmpParam == "raid" || tmpParam == "raids")
{
Raids::getInstance()->reload();
Raids::getInstance()->startup();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded raids.");
}
else if(tmpParam == "spell" || tmpParam == "spells")
{
g_spells->reload();
g_monsters.reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded spells.");
}
else if(tmpParam == "talk" || tmpParam == "talkaction" || tmpParam == "talkactions")
{
g_talkActions->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded talk actions.");
}
else if(tmpParam == "items")
{
Item::items.reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded items.");
}
else if(tmpParam == "weapon" || tmpParam == "weapons")
{
g_weapons->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded weapons.");
}
else if(tmpParam == "quest" || tmpParam == "quests")
{
Quests::getInstance()->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded quests.");
}
else if(tmpParam == "mount" || tmpParam == "mounts")
{
Mounts::getInstance()->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded mounts.");
}
else if(tmpParam == "globalevents" || tmpParam == "globalevent")
{
g_globalEvents->reload();
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reloaded globalevents.");
}
else
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Reload type not found.");
}
void Commands::teleportToTown(Player* player, const std::string& cmd, const std::string& param)
{
std::string tmp = param;
Town* town = Towns::getInstance().getTown(tmp);
if(town)
{
Position oldPosition = player->getPosition();
Position newPosition = g_game.getClosestFreeTile(player, 0, town->getTemplePosition(), true);
if(player->getPosition() != town->getTemplePosition())
{
if(newPosition.x == 0)
player->sendCancel("You can not teleport there.");
else if(g_game.internalTeleport(player, newPosition) == RET_NOERROR)
{
g_game.addMagicEffect(oldPosition, NM_ME_POFF, player->isInGhostMode());
g_game.addMagicEffect(newPosition, NM_ME_TELEPORT, player->isInGhostMode());
}
}
}
else
player->sendCancel("Could not find the town.");
}
void Commands::teleportTo(Player* player, const std::string& cmd, const std::string& param)
{
Creature* paramCreature = g_game.getCreatureByName(param);
if(paramCreature)
{
Position oldPosition = player->getPosition();
Position newPosition = g_game.getClosestFreeTile(player, 0, paramCreature->getPosition(), true);
if(newPosition.x > 0)
{
if(g_game.internalTeleport(player, newPosition) == RET_NOERROR)
{
bool ghostMode = false;
if(player->isInGhostMode() || paramCreature->isInGhostMode())
ghostMode = true;
g_game.addMagicEffect(oldPosition, NM_ME_POFF, ghostMode);
g_game.addMagicEffect(player->getPosition(), NM_ME_TELEPORT, ghostMode);
}
}
else
{
std::ostringstream ss;
ss << "You can not teleport to " << paramCreature->getName() << ".";
player->sendCancel(ss.str());
}
}
}
void Commands::getInfo(Player* player, const std::string& cmd, const std::string& param)
{
Player* paramPlayer = g_game.getPlayerByName(param);
if(paramPlayer)
{
if(paramPlayer->isAccessPlayer() && player != paramPlayer)
{
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "You can not get info about this player.");
return;
}
Account account = IOLoginData::getInstance()->loadAccount(paramPlayer->getAccount());
std::ostringstream info;
info << "name: " << paramPlayer->name << std::endl <<
"access: " << paramPlayer->accessLevel << std::endl <<
"level: " << paramPlayer->level << std::endl <<
"maglvl: " << paramPlayer->magLevel << std::endl <<
"speed: " << paramPlayer->getSpeed() <<std::endl <<
"position: " << paramPlayer->getPosition() << std::endl <<
"notations: " << IOBan::getInstance()->getNotationsCount(paramPlayer->getAccount()) << std::endl <<
"warnings: " << account.warnings << std::endl <<
"ip: " << convertIPToString(paramPlayer->getIP()) << std::endl;
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, info.str().c_str());
}
else
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Player not found.");
}
void Commands::closeServer(Player* player, const std::string& cmd, const std::string& param)
{
if(param == "shutdown")
{
g_game.setGameState(GAME_STATE_SHUTDOWN);
}
else
{
g_game.setGameState(GAME_STATE_CLOSED);
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Server is now closed.");
}
}
void Commands::openServer(Player* player, const std::string& cmd, const std::string& param)
{
g_game.setGameState(GAME_STATE_NORMAL);
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Server is now open.");
}
void Commands::teleportNTiles(Player* player, const std::string& cmd, const std::string& param)
{
int32_t ntiles = atoi(param.c_str());
if(ntiles != 0)
{
Position oldPosition = player->getPosition();
Position newPos = player->getPosition();
switch(player->direction)
{
case NORTH: newPos.y -= ntiles; break;
case SOUTH: newPos.y += ntiles; break;
case EAST: newPos.x += ntiles; break;
case WEST: newPos.x -= ntiles; break;
default: break;
}
Position newPosition = g_game.getClosestFreeTile(player, 0, newPos, true);
if(newPosition.x == 0)
player->sendCancel("You can not teleport there.");
else if(g_game.internalTeleport(player, newPosition) == RET_NOERROR)
{
if(ntiles != 1)
{
g_game.addMagicEffect(oldPosition, NM_ME_POFF, player->isInGhostMode());
g_game.addMagicEffect(newPosition, NM_ME_TELEPORT, player->isInGhostMode());
}
}
}
}
void Commands::kickPlayer(Player* player, const std::string& cmd, const std::string& param)
{
Player* playerKick = g_game.getPlayerByName(param);
if(playerKick)
{
if(playerKick->accessLevel)
{
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "You cannot kick this player.");
return;
}
playerKick->kickPlayer(true);
}
}
void Commands::setHouseOwner(Player* player, const std::string& cmd, const std::string& param)
{
if(player->getTile()->hasFlag(TILESTATE_HOUSE))
{
HouseTile* houseTile = dynamic_cast<HouseTile*>(player->getTile());
if(houseTile)
{
uint32_t guid;
std::string name = param;
if(name == "none")
houseTile->getHouse()->setHouseOwner(0);
else if(IOLoginData::getInstance()->getGuidByName(guid, name))
houseTile->getHouse()->setHouseOwner(guid);
else
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, "Player not found.");
}
}
}
void Commands::sellHouse(Player* player, const std::string& cmd, const std::string& param)
{
House* house = Houses::getInstance().getHouseByPlayerId(player->guid);
if(!house)
{
player->sendCancel("You do not own any house.");
return;
}
Player* tradePartner = g_game.getPlayerByName(param);
if(!(tradePartner && tradePartner != player))
{
player->sendCancel("Trade player not found.");
return;
}
if(tradePartner->level < 1)
{
player->sendCancel("Trade player level is too low.");
return;
}
if(!Position::areInRange<2,2,0>(tradePartner->getPosition(), player->getPosition()))
{
player->sendCancel("Trade player is too far away.");
return;
}
if(!tradePartner->isPremium())
{
player->sendCancel("Trade player does not have a premium account.");
return;
}
if(Houses::getInstance().getHouseByPlayerId(tradePartner->guid))
{
player->sendCancel("Trade player already owns a house.");
return;
}
Item* transferItem = house->getTransferItem();
if(!transferItem)
{
player->sendCancel("You can not trade this house.");
return;
}
transferItem->getParent()->setParent(player);
if(!g_game.internalStartTrade(player, tradePartner, transferItem))
house->resetTransferItem();
}
void Commands::getHouse(Player* player, const std::string& cmd, const std::string& param)
{
std::string name = param;
uint32_t guid;
if(!IOLoginData::getInstance()->getGuidByName(guid, name))
return;
std::ostringstream str;
str << name;
House* house = Houses::getInstance().getHouseByPlayerId(guid);
if(house)
str << " owns house: " << house->getName() << ".";
else
str << " does not own any house.";
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, str.str().c_str());
}
void Commands::serverInfo(Player* player, const std::string& cmd, const std::string& param)
{
std::ostringstream text;
text << "Server Info:";
text << "\nExp Rate: " << g_game.getExperienceStage(player->level);
text << "\nSkill Rate: " << g_config.getNumber(ConfigManager::RATE_SKILL);
text << "\nMagic Rate: " << g_config.getNumber(ConfigManager::RATE_MAGIC);
text << "\nLoot Rate: " << g_config.getNumber(ConfigManager::RATE_LOOT);
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, text.str().c_str());
}
void Commands::buyHouse(Player* player, const std::string& cmd, const std::string& param)
{
if(!player->isPremium())
{
player->sendCancelMessage(RET_YOUNEEDPREMIUMACCOUNT);
return;
}
Position pos = player->getPosition();
pos = getNextPosition(player->direction, pos);
Tile* tile = g_game.getTile(pos.x, pos.y, pos.z);
if(!tile)
{
player->sendCancel("You have to be looking at the door of the house you would like to buy.");
return;
}
HouseTile* houseTile = dynamic_cast<HouseTile*>(tile);
if(!houseTile)
{
player->sendCancel("You have to be looking at the door of the house you would like to buy.");
return;
}
House* house = houseTile->getHouse();
if(!house || !house->getDoorByPosition(pos))
{
player->sendCancel("You have to be looking at the door of the house you would like to buy.");
return;
}
if(house->getHouseOwner())
{
player->sendCancel("This house alreadly has an owner.");
return;
}
for(HouseMap::iterator it = Houses::getInstance().getHouseBegin(), end = Houses::getInstance().getHouseEnd(); it != end; ++it)
{
if(it->second->getHouseOwner() == player->guid)
{
player->sendCancel("You are already the owner of a house.");
return;
}
}
uint64_t price = 0;
for(HouseTileList::iterator it = house->getHouseTileBegin(), end = house->getHouseTileEnd(); it != end; ++it)
price += g_config.getNumber(ConfigManager::HOUSE_PRICE);
if(!g_game.removeMoney(player, price))
{
player->sendCancel("You do not have enough money.");
return;
}
house->setHouseOwner(player->guid);
player->sendTextMessage(MSG_INFO_DESCR, "You have successfully bought this house, be sure to have the money for the rent in your depot of this city.");
}
void Commands::whoIsOnline(Player* player, const std::string& cmd, const std::string& param)
{
std::ostringstream ss;
ss << Player::listPlayer.list.size() << " players online:";
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, ss.str());
ss.str("");
uint32_t i = 0;
AutoList<Player>::listiterator it = Player::listPlayer.list.begin();
if(!g_config.getBoolean(ConfigManager::SHOW_GAMEMASTERS_ONLINE))
{
while(it != Player::listPlayer.list.end())
{
if(!(*it).second->isAccessPlayer() || player->isAccessPlayer())
{
ss << (i > 0 ? ", " : "") << (*it).second->name << " [" << (*it).second->level << "]";
++i;
}
++it;
if(i == 10)
{
ss << (it != Player::listPlayer.list.end() ? "," : ".");
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, ss.str());
ss.str("");
i = 0;
}
}
}
else
{
while(it != Player::listPlayer.list.end())
{
ss << (i > 0 ? ", " : "") << (*it).second->name << " [" << (*it).second->level << "]";
++it;
++i;
if(i == 10)
{
ss << (it != Player::listPlayer.list.end() ? "," : ".");
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, ss.str());
ss.str("");
i = 0;
}
}
}
if(i > 0)
{
ss << ".";
player->sendTextMessage(MSG_STATUS_CONSOLE_BLUE, ss.str());
}
}
void Commands::changeFloor(Player* player, const std::string& cmd, const std::string& param)
{
Position newPos = player->getPosition();