[2771] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / Level2.cpp
blob9cbb8137e9e2d2080d31a976ee2d653254f9e1fb
1 /*
2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Database/DatabaseEnv.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "World.h"
24 #include "ObjectMgr.h"
25 #include "Player.h"
26 #include "Item.h"
27 #include "GameObject.h"
28 #include "Opcodes.h"
29 #include "Chat.h"
30 #include "ObjectAccessor.h"
31 #include "MapManager.h"
32 #include "Language.h"
33 #include "World.h"
35 bool ChatHandler::HandleTargetObjectCommand(const char* args)
38 Player* pl = m_session->GetPlayer();
39 QueryResult *result;
41 if(*args)
43 int32 id = atoi((char*)args);
44 if(id)
45 result = sDatabase.PQuery("SELECT `guid`, `id`, `position_x`, `position_y`, `position_z`, `orientation`, `map`, (POW(`position_x` - '%f', 2) + POW(`position_y` - '%f', 2) + POW(`position_z` - '%f', 2)) as `order` FROM `gameobject` WHERE `map` = '%i' AND `id` = '%u' ORDER BY `order` ASC LIMIT 1",
46 pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), pl->GetMapId(),id);
47 else
49 std::string name = args;
50 sDatabase.escape_string(name);
51 result = sDatabase.PQuery(
52 "SELECT `guid`, `id`, `position_x`, `position_y`, `position_z`, `orientation`, `map`, (POW(`position_x` - %f, 2) + POW(`position_y` - %f, 2) + POW(`position_z` - %f, 2)) as `order` "
53 "FROM `gameobject`,`gameobject_template` WHERE `gameobject_template`.`entry` = `gameobject`.`id` AND `map` = %i AND `name` LIKE '%%%s%%' ORDER BY `order` ASC LIMIT 1",
54 pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), pl->GetMapId(),name.c_str());
57 else
58 result = sDatabase.PQuery("SELECT `guid`, `id`, `position_x`, `position_y`, `position_z`, `orientation`, `map`, (POW(`position_x` - %f, 2) + POW(`position_y` - %f, 2) + POW(`position_z` - %f, 2)) as `order` FROM `gameobject` WHERE `map` = %i ORDER BY `order` ASC LIMIT 1", m_session->GetPlayer()->GetPositionX(), m_session->GetPlayer()->GetPositionY(), m_session->GetPlayer()->GetPositionZ(), m_session->GetPlayer()->GetMapId());
60 if (!result)
62 SendSysMessage("Nothing found!");
63 return true;
66 Field *fields = result->Fetch();
67 uint32 guid = fields[0].GetUInt32();
68 uint32 id = fields[1].GetUInt32();
69 float x = fields[2].GetFloat();
70 float y = fields[3].GetFloat();
71 float z = fields[4].GetFloat();
72 float o = fields[5].GetFloat();
73 int mapid = fields[6].GetUInt16();
74 delete result;
76 const GameObjectInfo *goI = objmgr.GetGameObjectInfo(id);
78 if (!goI)
80 PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST,id);
81 return false;
84 PSendSysMessage("Selected object:\n%s\nGUID: %u ID: %u\nX: %f Y: %f Z: %f MapId: %u\nOrientation: %f", goI->name, guid, id, x, y, z, mapid, o);
86 return true;
89 bool ChatHandler::HandleGoObjectCommand(const char* args)
91 if(m_session->GetPlayer()->isInFlight())
93 SendSysMessage(LANG_YOU_IN_FLIGHT);
94 return true;
97 if(!*args)
98 return false;
100 int32 guid = atoi((char*)args);
101 if(!guid)
102 return false;
104 QueryResult *result = sDatabase.PQuery("SELECT `position_x`,`position_y`,`position_z`,`orientation`,`map` FROM `gameobject` WHERE `guid` = '%i'",guid);
105 if (!result)
107 SendSysMessage("Object not found!");
108 return true;
111 Field *fields = result->Fetch();
112 float x = fields[0].GetFloat();
113 float y = fields[1].GetFloat();
114 float z = fields[2].GetFloat();
115 float ort = fields[3].GetFloat();
116 int mapid = fields[4].GetUInt16();
117 delete result;
119 if(!MapManager::ExistMAP(mapid,x,y))
121 PSendSysMessage("target map not exist (X: %f Y: %f MapId:%u)",x,y,mapid);
122 return true;
125 m_session->GetPlayer()->TeleportTo(mapid, x, y, z, ort);
126 return true;
129 bool ChatHandler::HandleGoCreatureCommand(const char* args)
131 if(m_session->GetPlayer()->isInFlight())
133 SendSysMessage(LANG_YOU_IN_FLIGHT);
134 return true;
137 if(!*args)
138 return false;
140 int32 guid = atoi((char*)args);
141 if(!guid)
142 return false;
144 QueryResult *result = sDatabase.PQuery("SELECT `position_x`,`position_y`,`position_z`,`orientation`,`map` FROM `creature` WHERE `guid` = '%i'",guid);
145 if (!result)
147 SendSysMessage("Creature not found!");
148 return true;
151 Field *fields = result->Fetch();
152 float x = fields[0].GetFloat();
153 float y = fields[1].GetFloat();
154 float z = fields[2].GetFloat();
155 float ort = fields[3].GetFloat();
156 int mapid = fields[4].GetUInt16();
158 delete result;
160 if(!MapManager::ExistMAP(mapid,x,y))
162 PSendSysMessage("target map not exist (X: %f Y: %f MapId:%u)",x,y,mapid);
163 return true;
166 m_session->GetPlayer()->TeleportTo(mapid, x, y, z, ort);
167 return true;
170 bool ChatHandler::HandleGUIDCommand(const char* args)
172 uint64 guid = m_session->GetPlayer()->GetSelection();
174 if (guid == 0)
176 SendSysMessage(LANG_NO_SELECTION);
177 return true;
180 PSendSysMessage(LANG_OBJECT_GUID, GUID_LOPART(guid), GUID_HIPART(guid));
181 return true;
184 bool ChatHandler::HandleNameCommand(const char* args)
186 /* Temp. disabled
187 if(!*args)
188 return false;
190 if(strlen((char*)args)>75)
192 PSendSysMessage(LANG_TOO_LONG_NAME, strlen((char*)args)-75);
193 return true;
196 for (uint8 i = 0; i < strlen(args); i++)
198 if(!isalpha(args[i]) && args[i]!=' ')
200 SendSysMessage(LANG_CHARS_ONLY);
201 return false;
205 uint64 guid;
206 guid = m_session->GetPlayer()->GetSelection();
207 if (guid == 0)
209 SendSysMessage(LANG_NO_SELECTION);
210 return true;
213 Creature* pCreature = ObjectAccessor::Instance().GetCreature(*m_session->GetPlayer(), guid);
215 if(!pCreature)
217 SendSysMessage(LANG_SELECT_CREATURE);
218 return true;
221 pCreature->SetName(args);
222 uint32 idname = objmgr.AddCreatureTemplate(pCreature->GetName());
223 pCreature->SetUInt32Value(OBJECT_FIELD_ENTRY, idname);
225 pCreature->SaveToDB();
228 return true;
231 bool ChatHandler::HandleSubNameCommand(const char* args)
233 /* Temp. disabled
235 if(!*args)
236 args = "";
238 if(strlen((char*)args)>75)
241 PSendSysMessage(LANG_TOO_LONG_SUBNAME, strlen((char*)args)-75);
242 return true;
245 for (uint8 i = 0; i < strlen(args); i++)
247 if(!isalpha(args[i]) && args[i]!=' ')
249 SendSysMessage(LANG_CHARS_ONLY);
250 return false;
253 uint64 guid;
254 guid = m_session->GetPlayer()->GetSelection();
255 if (guid == 0)
257 SendSysMessage(LANG_NO_SELECTION);
258 return true;
261 Creature* pCreature = ObjectAccessor::Instance().GetCreature(*m_session->GetPlayer(), guid);
263 if(!pCreature)
265 SendSysMessage(LANG_SELECT_CREATURE);
266 return true;
269 uint32 idname = objmgr.AddCreatureSubName(pCreature->GetName(),args,pCreature->GetUInt32Value(UNIT_FIELD_DISPLAYID));
270 pCreature->SetUInt32Value(OBJECT_FIELD_ENTRY, idname);
272 pCreature->SaveToDB();
274 return true;
277 bool ChatHandler::HandleNYICommand(const char* args)
279 SendSysMessage(LANG_NOT_IMPLEMENTED);
280 return true;
283 bool ChatHandler::HandleProgCommand(const char* args)
285 if(m_session->GetPlayer()->isInFlight())
287 SendSysMessage(LANG_YOU_IN_FLIGHT);
288 return true;
291 m_session->GetPlayer()->TeleportTo(451, 16391.80f, 16341.20f, 69.44f,0.0f);
293 return true;
296 bool ChatHandler::HandleItemMoveCommand(const char* args)
298 uint8 srcslot, dstslot;
300 char* pParam1 = strtok((char*)args, " ");
301 if (!pParam1)
302 return false;
304 char* pParam2 = strtok(NULL, " ");
305 if (!pParam2)
306 return false;
308 srcslot = (uint8)atoi(pParam1);
309 dstslot = (uint8)atoi(pParam2);
311 uint16 src = ((INVENTORY_SLOT_BAG_0 << 8) | srcslot);
312 uint16 dst = ((INVENTORY_SLOT_BAG_0 << 8) | dstslot);
314 m_session->GetPlayer()->SwapItem( src, dst );
316 return true;
319 bool ChatHandler::HandleSpawnCommand(const char* args)
321 char* pEntry = strtok((char*)args, " ");
322 if (!pEntry)
323 return false;
325 char* pFlags = strtok(NULL, " ");
326 if (!pFlags)
327 return false;
329 char* pLevel = strtok(NULL, " ");
330 if (!pLevel)
331 return false;
333 char* pName = strtok(NULL, "%");
334 if (!pName)
335 return false;
337 uint32 level = atoi(pLevel);
339 for (uint8 i = 0; i < strlen(pName); i++)
341 if(!isalpha(pName[i]) && pName[i]!=' ')
343 SendSysMessage(LANG_CHARS_ONLY);
344 return false;
347 SpawnCreature(m_session, pName, level);
349 return true;
352 bool ChatHandler::HandleAddSpwCommand(const char* args)
354 char* charID = strtok((char*)args, " ");
355 if (!charID)
356 return false;
358 uint32 id = atoi(charID);
360 Player *chr = m_session->GetPlayer();
361 float x = chr->GetPositionX();
362 float y = chr->GetPositionY();
363 float z = chr->GetPositionZ();
364 float o = chr->GetOrientation();
366 Creature* pCreature = new Creature;
367 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), chr->GetMapId(), x, y, z, o, id))
369 delete pCreature;
370 return false;
373 pCreature->SaveToDB();
374 pCreature->LoadFromDB(pCreature->GetGUIDLow()); // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
375 MapManager::Instance().GetMap(pCreature->GetMapId())->Add(pCreature);
377 sLog.outDebug(LANG_ADD_OBJ);
379 return true;
382 bool ChatHandler::HandleDeleteCommand(const char* args)
384 Creature *unit = getSelectedCreature();
385 if(!unit)
387 SendSysMessage(LANG_SELECT_CREATURE);
388 return true;
391 unit->CombatStop();
393 unit->DeleteFromDB();
395 ObjectAccessor::Instance().AddObjectToRemoveList(unit);
397 SendSysMessage("Creature Removed");
399 return true;
402 bool ChatHandler::HandleDelObjectCommand(const char* args)
404 if(!*args)
405 return false;
407 uint32 lowguid = atoi((char*)args);
408 if(!lowguid)
409 return false;
411 GameObject* obj = ObjectAccessor::Instance().GetGameObject(*m_session->GetPlayer(), MAKE_GUID(lowguid, HIGHGUID_GAMEOBJECT));
413 if(!obj)
415 PSendSysMessage("Game Object (GUID: %u) not found", lowguid);
416 return true;
419 uint64 owner_guid = obj->GetOwnerGUID();
420 if(owner_guid)
422 Unit* owner = ObjectAccessor::Instance().GetUnit(*m_session->GetPlayer(),owner_guid);
423 if(!owner && GUID_HIPART(owner_guid)!=HIGHGUID_PLAYER)
425 PSendSysMessage("Game Object (GUID: %u) have references in not found creature %u GO list, can't be deleted.", GUID_LOPART(owner_guid), obj->GetGUIDLow());
426 return true;
429 owner->RemoveGameObject(obj,false);
432 obj->Delete();
433 obj->DeleteFromDB();
435 PSendSysMessage("Game Object (GUID: %u) removed", obj->GetGUIDLow());
437 return true;
440 bool ChatHandler::HandleTurnObjectCommand(const char* args)
442 if(!*args)
443 return false;
445 char* plowguid = strtok((char*)args, " ");
447 if(!plowguid)
448 return false;
450 uint32 lowguid = (uint32)atoi(plowguid);
452 GameObject* obj = ObjectAccessor::Instance().GetGameObject(*m_session->GetPlayer(), MAKE_GUID(lowguid, HIGHGUID_GAMEOBJECT));
454 if(!obj)
456 PSendSysMessage("Game Object (GUID: %u) not found", lowguid);
457 return true;
460 char* po = strtok(NULL, " ");
461 float o;
463 if (po)
465 o = (float)atof(po);
467 else
469 Player *chr = m_session->GetPlayer();
470 o = chr->GetOrientation();
473 float rot2 = sin(o/2);
474 float rot3 = cos(o/2);
476 obj->Relocate(obj->GetPositionX(), obj->GetPositionY(), obj->GetPositionZ(), o);
478 obj->SetFloatValue(GAMEOBJECT_FACING, o);
479 obj->SetFloatValue(GAMEOBJECT_ROTATION+2, rot2);
480 obj->SetFloatValue(GAMEOBJECT_ROTATION+3, rot3);
482 obj->SaveToDB();
483 obj->Refresh();
485 PSendSysMessage("Game Object (GUID: %u) turned", obj->GetGUIDLow(), o);
487 return true;
490 bool ChatHandler::HandleMoveObjectCommand(const char* args)
492 if(!*args)
493 return false;
495 char* plowguid = strtok((char*)args, " ");
497 if(!plowguid)
498 return false;
500 uint32 lowguid = (uint32)atoi(plowguid);
502 GameObject* obj = ObjectAccessor::Instance().GetGameObject(*m_session->GetPlayer(), MAKE_GUID(lowguid, HIGHGUID_GAMEOBJECT));
504 if(!obj)
506 PSendSysMessage("Game Object (GUID: %u) not found", lowguid);
507 return true;
510 char* px = strtok(NULL, " ");
511 char* py = strtok(NULL, " ");
512 char* pz = strtok(NULL, " ");
514 if (!px)
516 Player *chr = m_session->GetPlayer();
518 obj->Relocate(chr->GetPositionX(), chr->GetPositionY(), chr->GetPositionZ(), obj->GetOrientation());
520 obj->SetFloatValue(GAMEOBJECT_POS_X, chr->GetPositionX());
521 obj->SetFloatValue(GAMEOBJECT_POS_Y, chr->GetPositionY());
522 obj->SetFloatValue(GAMEOBJECT_POS_Z, chr->GetPositionZ());
524 else
526 if(!py || !pz)
527 return false;
529 float x = (float)atof(px);
530 float y = (float)atof(py);
531 float z = (float)atof(pz);
533 if(!MapManager::ExistMAP(obj->GetMapId(),x,y))
535 PSendSysMessage(".move target map not exist (X: %f Y: %f MapId:%u)",x,y,obj->GetMapId());
536 return true;
539 obj->Relocate(x, y, z, obj->GetOrientation());
541 obj->SetFloatValue(GAMEOBJECT_POS_X, x);
542 obj->SetFloatValue(GAMEOBJECT_POS_Y, y);
543 obj->SetFloatValue(GAMEOBJECT_POS_Z, z);
546 obj->SaveToDB();
547 obj->Refresh();
549 PSendSysMessage("Game Object (GUID: %u) moved", obj->GetGUIDLow());
551 return true;
554 bool ChatHandler::HandleDeMorphCommand(const char* args)
556 sLog.outError(LANG_DEMORPHED,m_session->GetPlayer()->GetName());
557 m_session->GetPlayer()->DeMorph();
558 return true;
561 bool ChatHandler::HandleItemCommand(const char* args)
564 char* pitem = strtok((char*)args, " ");
565 if (!pitem)
566 return false;
568 uint64 guid = m_session->GetPlayer()->GetSelection();
569 if (guid == 0)
571 SendSysMessage(LANG_NO_SELECTION);
572 return true;
575 Creature* pCreature = ObjectAccessor::Instance().GetCreature(*m_session->GetPlayer(), guid);
577 if(!pCreature)
579 SendSysMessage(LANG_SELECT_CREATURE);
580 return true;
583 uint32 item = atoi(pitem);
584 int amount = -1;
586 char* pamount = strtok(NULL, " ");
587 if (pamount)
588 amount = atoi(pamount);
590 ItemPrototype* tmpItem = objmgr.GetItemPrototype(item);
592 if(tmpItem)
594 QueryResult *result = sDatabase.PQuery("INSERT INTO `npc_vendor` (`entry`,`itemguid`,`amount`) VALUES('%u','%u','%d')",pCreature->GetEntry(), item, amount);
596 uint8 itemscount = pCreature->GetItemCount();
597 pCreature->setItemId(itemscount , item);
598 pCreature->setItemAmount(itemscount , amount);
599 pCreature->IncrItemCount();
600 PSendSysMessage(LANG_ITEM_ADDED_TO_LIST,item,tmpItem->Name1);
601 delete result;
603 else
605 PSendSysMessage(LANG_ITEM_NOT_FOUND,item);
608 return true;
611 bool ChatHandler::HandleItemRemoveCommand(const char* args)
614 char* iguid = strtok((char*)args, " ");
615 if (!iguid)
616 return false;
618 uint64 guid = m_session->GetPlayer()->GetSelection();
619 if (guid == 0)
621 SendSysMessage(LANG_NO_SELECTION);
622 return true;
625 Creature *pCreature = ObjectAccessor::Instance().GetCreature(*m_session->GetPlayer(), guid);
627 if(!pCreature)
629 SendSysMessage(LANG_SELECT_CREATURE);
630 return true;
633 uint32 itemguid = atoi(iguid);
634 int slot = pCreature->GetItemSlot(itemguid);
636 if(slot != -1)
638 uint32 guidlow = GUID_LOPART(guid);
640 sDatabase.PExecute("DELETE FROM `npc_vendor` WHERE `entry` = '%u' AND `itemguid` = '%u'",pCreature->GetEntry(),itemguid);
642 pCreature->setItemId(slot , 0);
643 pCreature->setItemAmount(slot , 0);
644 ItemPrototype* tmpItem = objmgr.GetItemPrototype(itemguid);
645 if(tmpItem)
647 PSendSysMessage(LANG_ITEM_DELETED_FROM_LIST,itemguid,tmpItem->Name1);
649 else
651 PSendSysMessage(LANG_ITEM_DELETED_FROM_LIST,itemguid,"<unknonwn>");
655 else
657 PSendSysMessage(LANG_ITEM_NOT_IN_LIST,itemguid);
660 return true;
663 bool ChatHandler::HandleAddMoveCommand(const char* args)
665 if(!*args)
666 return false;
668 char* guid_str = strtok((char*)args, " ");
669 char* wait_str = strtok((char*)NULL, " ");
671 uint32 lowguid = atoi((char*)guid_str);
673 Creature* pCreature = NULL;
675 if(lowguid)
676 pCreature = ObjectAccessor::Instance().GetCreature(*m_session->GetPlayer(),MAKE_GUID(lowguid,HIGHGUID_UNIT));
678 if(!pCreature)
680 PSendSysMessage("Creature (GUID: %u) not found", lowguid);
681 return true;
684 int wait = wait_str ? atoi(wait_str) : 0;
686 if(wait < 0)
687 wait = 0;
689 uint32 point;
691 QueryResult *result = sDatabase.PQuery( "SELECT MAX(`point`) FROM `creature_movement` WHERE `id` = '%u'",pCreature->GetGUIDLow());
692 if( result )
694 point = (*result)[0].GetUInt32()+1;
696 delete result;
698 else
699 point = 0;
701 Player* player = m_session->GetPlayer();
703 sDatabase.PExecute("INSERT INTO `creature_movement` (`id`,`point`,`position_x`,`position_y`,`position_z`,`waittime`) VALUES ('%u','%u','%f', '%f', '%f','%u')",
704 pCreature->GetGUIDLow(), point, player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), wait);
706 // update movement type
707 if(pCreature->GetDefaultMovementType()!=WAYPOINT_MOTION_TYPE)
709 pCreature->SetDefaultMovementType(WAYPOINT_MOTION_TYPE);
710 sDatabase.PExecute("UPDATE `creature` SET `MovementType` = '%u' WHERE `guid` = '%u'", pCreature->GetDefaultMovementType(),pCreature->GetGUIDLow());
713 SendSysMessage(LANG_WAYPOINT_ADDED);
715 return true;
718 bool ChatHandler::HandleRandomCommand(const char* args)
720 if(!*args)
721 return false;
723 int option = atoi((char*)args);
725 if (option != 0 && option != 1)
727 //m_session->GetPlayer( )->SendMessageToSet( &data, true );
728 SendSysMessage(LANG_USE_BOL);
729 return true;
732 Creature* pCreature = getSelectedCreature();
734 if(!pCreature)
736 SendSysMessage(LANG_SELECT_CREATURE);
737 return true;
740 // fix me : 'moverandom' doesn't exist in https://svn.mangosproject.org/trac/MaNGOS/wiki/Database/creature ?
741 // perhaps it should be 'state'?
742 sDatabase.PExecute("UPDATE `creature` SET `moverandom` = '%i' WHERE `guid` = '%u'", option, pCreature->GetGUIDLow());
744 pCreature->setMoveRandomFlag(option > 0);
746 SendSysMessage(LANG_VALUE_SAVED);
748 return true;
751 bool ChatHandler::HandleRunCommand(const char* args)
753 if(!*args)
754 return false;
756 int option = atoi((char*)args);
758 if(option != 0 && option != 1)
760 SendSysMessage(LANG_USE_BOL);
761 return true;
764 Creature* pCreature = getSelectedCreature();
766 if(!pCreature)
768 SendSysMessage(LANG_SELECT_CREATURE);
769 return true;
772 // fix me : 'running' doesn't exist in https://svn.mangosproject.org/trac/MaNGOS/wiki/Database/creatures ?
773 // perhaps it should be 'state'?
774 sDatabase.PExecute("UPDATE `creature` SET `running` = '%i' WHERE `guid` = '%u'", option, pCreature->GetGUIDLow());
776 pCreature->setMoveRunFlag(option > 0);
778 SendSysMessage(LANG_VALUE_SAVED);
779 return true;
782 bool ChatHandler::HandleChangeLevelCommand(const char* args)
784 if (!*args)
785 return false;
787 uint8 lvl = (uint8) atoi((char*)args);
788 if ( lvl < 1 || lvl > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) + 3)
790 SendSysMessage(LANG_BAD_VALUE);
791 return true;
794 Creature* pCreature = getSelectedCreature();
795 if(!pCreature)
797 SendSysMessage(LANG_SELECT_CREATURE);
798 return true;
801 pCreature->SetHealth( 100 + 30*lvl);
802 pCreature->SetMaxHealth( 100 + 30*lvl);
803 pCreature->SetLevel( lvl);
805 pCreature->SaveToDB();
807 return true;
810 bool ChatHandler::HandleNPCFlagCommand(const char* args)
812 if (!*args)
813 return false;
815 uint32 npcFlags = (uint32) atoi((char*)args);
817 Creature* pCreature = getSelectedCreature();
819 if(!pCreature)
821 SendSysMessage(LANG_SELECT_CREATURE);
822 return true;
825 pCreature->SetUInt32Value(UNIT_NPC_FLAGS, npcFlags);
827 sDatabase.PExecute("UPDATE `creature_template` SET `npcflag` = '%u' WHERE `entry` = '%u'", npcFlags, pCreature->GetEntry());
829 SendSysMessage(LANG_VALUE_SAVED_REJOIN);
831 uint32 entry = pCreature->GetUInt32Value( OBJECT_FIELD_ENTRY );
832 m_session->SendCreatureQuery( entry, pCreature->GetGUID() );
834 return true;
837 bool ChatHandler::HandleDisplayIdCommand(const char* args)
839 if (!*args)
840 return false;
842 uint32 displayId = (uint32) atoi((char*)args);
844 Creature *pCreature = getSelectedCreature();
846 if(!pCreature)
848 SendSysMessage(LANG_SELECT_CREATURE);
849 return true;
852 pCreature->SetUInt32Value(UNIT_FIELD_DISPLAYID, displayId);
854 pCreature->SaveToDB();
856 return true;
859 bool ChatHandler::HandleFactionIdCommand(const char* args)
861 if (!*args)
862 return false;
864 uint32 factionId = (uint32) atoi((char*)args);
866 Creature* pCreature = getSelectedCreature();
868 if(!pCreature)
870 SendSysMessage(LANG_SELECT_CREATURE);
871 return true;
874 pCreature->setFaction(factionId);
876 pCreature->SaveToDB();
878 return true;
881 bool ChatHandler::HandleKickPlayerCommand(const char *args)
883 char* kickName = strtok((char*)args, " ");
884 if (!kickName)
886 Player* player = getSelectedPlayer();
887 if(player==m_session->GetPlayer())
889 SendSysMessage("You can't kick self by selecting, use .kick name ;)");
890 return true;
893 player->GetSession()->KickPlayer();
895 else
897 std::string name = kickName;
898 normalizePlayerName(name);
899 if(sWorld.KickPlayer(name))
900 PSendSysMessage("Player %s kicked.",name.c_str());
901 else
902 PSendSysMessage("Player %s not found.",name.c_str());
905 return true;
908 bool ChatHandler::HandlePInfoCommand(const char* args)
910 Player* target = NULL;
912 char* px = strtok((char*)args, " ");
913 if (px)
915 std::string name = px;
916 normalizePlayerName(name);
917 target = objmgr.GetPlayer(name.c_str());
919 else
920 target = getSelectedPlayer();
922 if(!target)
924 SendSysMessage(LANG_PLAYER_NOT_FOUND);
925 return true;
928 std::string username = "<error>";
929 std::string last_ip = "<error>";
931 QueryResult* result = loginDatabase.PQuery("SELECT `username`, `last_ip` FROM `account` WHERE `id` = '%u'",target->GetSession()->GetAccountId());
932 if(result)
934 Field* fields = result->Fetch();
935 username = fields[0].GetCppString();
936 if(m_session->GetSecurity() >= target->GetSession()->GetSecurity())
937 last_ip = fields[1].GetCppString();
938 else
939 last_ip = "-";
941 delete result;
944 PSendSysMessage(LANG_PINFO_ACCOUNT, target->GetName(), target->GetGUIDLow(), username.c_str(), target->GetSession()->GetAccountId(), target->GetSession()->GetSecurity(), last_ip.c_str());
946 uint32 days = target->GetTotalPlayedTime() / (60*60*24);
947 uint32 hours = (target->GetTotalPlayedTime() % (60*60*24)) / (60*60);
948 uint32 gold = target->GetMoney() /(100*100);
949 uint32 silv = (target->GetMoney() % (100*100)) / 100;
950 uint32 copp = (target->GetMoney() % (100*100)) % 100;
951 PSendSysMessage(LANG_PINFO_LEVEL, days, hours, target->getLevel(), gold,silv,copp );
953 return true;
956 void ChatHandler::ShowTicket(uint64 guid, uint32 category, char const* text)
958 std::string name;
959 objmgr.GetPlayerNameByGUID(guid,name);
961 if(name=="") name = " <unknown> ";
963 PSendSysMessage("Ticket of %s (Category: %i):\n%s\n", name.c_str(),category,text);
966 bool ChatHandler::HandleTicketCommand(const char* args)
968 char* px = strtok((char*)args, " ");
970 // ticket<end>
971 if (!px)
973 QueryResult *result = sDatabase.Query("SELECT `ticket_id` FROM `character_ticket`");
974 size_t count = result ? result->GetRowCount() : 0;
976 PSendSysMessage("Tickets count: %i show new tickets: %s\n", count,m_session->GetPlayer()->isAcceptTickets() ? "on" : "off");
977 delete result;
978 return true;
981 // ticket on
982 if(strncmp(px,"on",3) == 0)
984 m_session->GetPlayer()->SetAcceptTicket(true);
985 SendSysMessage("New ticket show: on");
986 return true;
989 // ticket off
990 if(strncmp(px,"off",4) == 0)
992 m_session->GetPlayer()->SetAcceptTicket(false);
993 SendSysMessage("New ticket show: off");
994 return true;
997 // ticket #num
998 int num = atoi(px);
999 if(num > 0)
1001 QueryResult *result = sDatabase.Query("SELECT `guid`,`ticket_category`,`ticket_text` FROM `character_ticket`");
1003 if(!result || uint64(num) > result->GetRowCount())
1005 PSendSysMessage("Ticket %i doesn't exist", num);
1006 delete result;
1007 return true;
1010 for(int i = 1; i < num; ++i)
1011 result->NextRow();
1013 Field* fields = result->Fetch();
1015 uint64 guid = fields[0].GetUInt64();
1016 uint32 category = fields[1].GetUInt32();
1017 char const* text = fields[2].GetString();
1019 ShowTicket(guid,category,text);
1020 delete result;
1021 return true;
1024 std::string name = px;
1025 normalizePlayerName(name);
1026 sDatabase.escape_string(name); // prevent SQL injection - normal name don't must changed by this call
1028 uint64 guid = objmgr.GetPlayerGUIDByName(name.c_str());
1030 if(!guid)
1031 return false;
1033 // ticket $char_name
1034 QueryResult *result = sDatabase.PQuery("SELECT `guid`,`ticket_category`,`ticket_text` FROM `character_ticket` WHERE `guid` = '%u'",GUID_LOPART(guid));
1036 if(!result)
1037 return false;
1039 Field* fields = result->Fetch();
1041 uint32 category = fields[1].GetUInt32();
1042 char const* text = fields[2].GetString();
1044 ShowTicket(guid,category,text);
1045 delete result;
1047 return true;
1050 uint32 ChatHandler::GetTicketIDByNum(uint32 num)
1052 QueryResult *result = sDatabase.Query("SELECT `ticket_id` FROM `character_ticket`");
1054 if(!result || num > result->GetRowCount())
1056 PSendSysMessage("Ticket %i doesn't exist", num);
1057 delete result;
1058 return 0;
1061 for(uint32 i = 1; i < num; ++i)
1062 result->NextRow();
1064 Field* fields = result->Fetch();
1066 uint32 id = fields[0].GetUInt32();
1067 delete result;
1068 return id;
1071 bool ChatHandler::HandleDelTicketCommand(const char *args)
1073 char* px = strtok((char*)args, " ");
1074 if (!px)
1075 return false;
1077 // delticket all
1078 if(strncmp(px,"all",4) == 0)
1080 QueryResult *result = sDatabase.Query("SELECT `guid` FROM `character_ticket`");
1082 if(!result)
1083 return true;
1085 // notify players about ticket deleting
1088 Field* fields = result->Fetch();
1090 uint64 guid = fields[0].GetUInt64();
1092 if(Player* sender = objmgr.GetPlayer(guid))
1093 sender->GetSession()->SendGMTicketGetTicket(1,0);
1095 }while(result->NextRow());
1097 delete result;
1099 sDatabase.PExecute("DELETE FROM `character_ticket`");
1100 SendSysMessage("All tickets deleted.");
1101 return true;
1104 int num = (uint32)atoi(px);
1106 // delticket #num
1107 if(num > 0)
1109 QueryResult *result = sDatabase.PQuery("SELECT `ticket_id`,`guid` FROM `character_ticket` LIMIT '%i'",num);
1111 if(!result || uint64(num) > result->GetRowCount())
1113 PSendSysMessage("Ticket %i doesn't exist", num);
1114 delete result;
1115 return true;
1118 for(int i = 1; i < num; ++i)
1119 result->NextRow();
1121 Field* fields = result->Fetch();
1123 uint32 id = fields[0].GetUInt32();
1124 uint64 guid = fields[1].GetUInt64();
1125 delete result;
1127 sDatabase.PExecute("DELETE FROM `character_ticket` WHERE `ticket_id` = '%u'", id);
1129 // notify players about ticket deleting
1130 if(Player* sender = objmgr.GetPlayer(guid))
1132 sender->GetSession()->SendGMTicketGetTicket(1,0);
1133 PSendSysMessage("Character %s ticket deleted.",sender->GetName());
1135 else
1136 SendSysMessage("Ticket deleted.");
1138 return true;
1141 std::string name = px;
1142 normalizePlayerName(name);
1143 sDatabase.escape_string(name); // prevent SQL injection - normal name don't must changed by this call
1145 uint64 guid = objmgr.GetPlayerGUIDByName(name.c_str());
1147 if(!guid)
1148 return false;
1150 // delticket $char_name
1151 sDatabase.PExecute("DELETE FROM `character_ticket` WHERE `guid` = '%u'",GUID_LOPART(guid));
1153 // notify players about ticket deleting
1154 if(Player* sender = objmgr.GetPlayer(guid))
1155 sender->GetSession()->SendGMTicketGetTicket(1,0);
1157 PSendSysMessage("Character %s ticket deleted.",px);
1158 return true;