[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / Level2.cpp
blobebb9816dfdff36145788b84b35243c22620254fc
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
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"
34 #include "GameEvent.h"
35 #include "SpellMgr.h"
36 #include "AccountMgr.h"
37 #include "GMTicketMgr.h"
38 #include "WaypointManager.h"
39 #include "Util.h"
40 #include <cctype>
41 #include <iostream>
42 #include <fstream>
43 #include <map>
44 #include "GlobalEvents.h"
46 #include "TargetedMovementGenerator.h" // for HandleNpcUnFollowCommand
48 static uint32 ReputationRankStrIndex[MAX_REPUTATION_RANK] =
50 LANG_REP_HATED, LANG_REP_HOSTILE, LANG_REP_UNFRIENDLY, LANG_REP_NEUTRAL,
51 LANG_REP_FRIENDLY, LANG_REP_HONORED, LANG_REP_REVERED, LANG_REP_EXALTED
54 //mute player for some times
55 bool ChatHandler::HandleMuteCommand(const char* args)
57 if (!*args)
58 return false;
60 std::string name = extractPlayerNameFromLink((char*)args);
61 if(name.empty())
63 SendSysMessage(LANG_PLAYER_NOT_FOUND);
64 SetSentErrorMessage(true);
65 return false;
68 char *timetonotspeak = strtok(NULL, " ");
69 if(!timetonotspeak)
70 return false;
72 uint32 notspeaktime = (uint32) atoi(timetonotspeak);
74 uint64 guid = objmgr.GetPlayerGUIDByName(name);
75 if(!guid)
77 SendSysMessage(LANG_PLAYER_NOT_FOUND);
78 SetSentErrorMessage(true);
79 return false;
82 Player *chr = objmgr.GetPlayer(guid);
84 // must have strong lesser security level
85 if(HasLowerSecurity (chr,guid,true))
86 return false;
88 uint32 account_id = chr ? chr->GetSession()->GetAccountId() : objmgr.GetPlayerAccountIdByGUID(guid);
90 time_t mutetime = time(NULL) + notspeaktime*60;
92 if (chr)
93 chr->GetSession()->m_muteTime = mutetime;
95 loginDatabase.PExecute("UPDATE account SET mutetime = " I64FMTD " WHERE id = '%u'",uint64(mutetime), account_id );
97 if(chr)
98 ChatHandler(chr).PSendSysMessage(LANG_YOUR_CHAT_DISABLED, notspeaktime);
100 std::string nameLink = playerLink(name);
102 PSendSysMessage(LANG_YOU_DISABLE_CHAT, nameLink.c_str(), notspeaktime);
104 return true;
107 //unmute player
108 bool ChatHandler::HandleUnmuteCommand(const char* args)
110 if (!*args)
111 return false;
113 std::string name = extractPlayerNameFromLink((char*)args);
114 if(name.empty())
116 SendSysMessage(LANG_PLAYER_NOT_FOUND);
117 SetSentErrorMessage(true);
118 return false;
121 uint64 guid = objmgr.GetPlayerGUIDByName(name);
122 if(!guid)
124 SendSysMessage(LANG_PLAYER_NOT_FOUND);
125 SetSentErrorMessage(true);
126 return false;
129 Player *chr = objmgr.GetPlayer(guid);
131 // must have strong lesser security level
132 if(HasLowerSecurity (chr,guid,true))
133 return false;
135 uint32 account_id = chr ? chr->GetSession()->GetAccountId() : objmgr.GetPlayerAccountIdByGUID(guid);
137 if (chr)
139 if(chr->CanSpeak())
141 SendSysMessage(LANG_CHAT_ALREADY_ENABLED);
142 SetSentErrorMessage(true);
143 return false;
146 chr->GetSession()->m_muteTime = 0;
149 loginDatabase.PExecute("UPDATE account SET mutetime = '0' WHERE id = '%u'", account_id );
151 if(chr)
152 ChatHandler(chr).PSendSysMessage(LANG_YOUR_CHAT_ENABLED);
154 std::string nameLink = playerLink(name);
156 PSendSysMessage(LANG_YOU_ENABLE_CHAT, nameLink.c_str());
157 return true;
160 bool ChatHandler::HandleTargetObjectCommand(const char* args)
162 Player* pl = m_session->GetPlayer();
163 QueryResult *result;
164 GameEvent::ActiveEvents const& activeEventsList = gameeventmgr.GetActiveEventList();
165 if(*args)
167 int32 id = atoi((char*)args);
168 if(id)
169 result = WorldDatabase.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",
170 pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), pl->GetMapId(),id);
171 else
173 std::string name = args;
174 WorldDatabase.escape_string(name);
175 result = WorldDatabase.PQuery(
176 "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_ "
177 "FROM gameobject,gameobject_template WHERE gameobject_template.entry = gameobject.id AND map = %i AND name "_LIKE_" "_CONCAT3_("'%%'","'%s'","'%%'")" ORDER BY order_ ASC LIMIT 1",
178 pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), pl->GetMapId(),name.c_str());
181 else
183 std::ostringstream eventFilter;
184 eventFilter << " AND (event IS NULL ";
185 bool initString = true;
187 for (GameEvent::ActiveEvents::const_iterator itr = activeEventsList.begin(); itr != activeEventsList.end(); ++itr)
189 if (initString)
191 eventFilter << "OR event IN (" <<*itr;
192 initString =false;
194 else
195 eventFilter << "," << *itr;
198 if (!initString)
199 eventFilter << "))";
200 else
201 eventFilter << ")";
203 result = WorldDatabase.PQuery("SELECT gameobject.guid, id, position_x, position_y, position_z, orientation, map, "
204 "(POW(position_x - %f, 2) + POW(position_y - %f, 2) + POW(position_z - %f, 2)) AS order_ FROM gameobject "
205 "LEFT OUTER JOIN game_event_gameobject on gameobject.guid=game_event_gameobject.guid WHERE map = '%i' %s ORDER BY order_ ASC LIMIT 1",
206 m_session->GetPlayer()->GetPositionX(), m_session->GetPlayer()->GetPositionY(), m_session->GetPlayer()->GetPositionZ(), m_session->GetPlayer()->GetMapId(),eventFilter.str().c_str());
209 if (!result)
211 SendSysMessage(LANG_COMMAND_TARGETOBJNOTFOUND);
212 return true;
215 Field *fields = result->Fetch();
216 uint32 lowguid = fields[0].GetUInt32();
217 uint32 id = fields[1].GetUInt32();
218 float x = fields[2].GetFloat();
219 float y = fields[3].GetFloat();
220 float z = fields[4].GetFloat();
221 float o = fields[5].GetFloat();
222 int mapid = fields[6].GetUInt16();
223 delete result;
225 GameObjectInfo const* goI = objmgr.GetGameObjectInfo(id);
227 if (!goI)
229 PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST,id);
230 return false;
233 GameObject* target = ObjectAccessor::GetGameObject(*m_session->GetPlayer(),MAKE_NEW_GUID(lowguid,id,HIGHGUID_GAMEOBJECT));
235 PSendSysMessage(LANG_GAMEOBJECT_DETAIL, lowguid, goI->name, lowguid, id, x, y, z, mapid, o);
237 if(target)
239 int32 curRespawnDelay = target->GetRespawnTimeEx()-time(NULL);
240 if(curRespawnDelay < 0)
241 curRespawnDelay = 0;
243 std::string curRespawnDelayStr = secsToTimeString(curRespawnDelay,true);
244 std::string defRespawnDelayStr = secsToTimeString(target->GetRespawnDelay(),true);
246 PSendSysMessage(LANG_COMMAND_RAWPAWNTIMES, defRespawnDelayStr.c_str(),curRespawnDelayStr.c_str());
248 return true;
251 //teleport to gameobject
252 bool ChatHandler::HandleGoObjectCommand(const char* args)
254 if(!*args)
255 return false;
257 Player* _player = m_session->GetPlayer();
259 // number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
260 char* cId = extractKeyFromLink((char*)args,"Hgameobject");
261 if(!cId)
262 return false;
264 int32 guid = atoi(cId);
265 if(!guid)
266 return false;
268 float x, y, z, ort;
269 int mapid;
271 // by DB guid
272 if (GameObjectData const* go_data = objmgr.GetGOData(guid))
274 x = go_data->posX;
275 y = go_data->posY;
276 z = go_data->posZ;
277 ort = go_data->orientation;
278 mapid = go_data->mapid;
280 else
282 SendSysMessage(LANG_COMMAND_GOOBJNOTFOUND);
283 SetSentErrorMessage(true);
284 return false;
287 if(!MapManager::IsValidMapCoord(mapid,x,y,z,ort))
289 PSendSysMessage(LANG_INVALID_TARGET_COORD,x,y,mapid);
290 SetSentErrorMessage(true);
291 return false;
294 // stop flight if need
295 if(_player->isInFlight())
297 _player->GetMotionMaster()->MovementExpired();
298 _player->m_taxi.ClearTaxiDestinations();
300 // save only in non-flight case
301 else
302 _player->SaveRecallPosition();
304 _player->TeleportTo(mapid, x, y, z, ort);
305 return true;
308 bool ChatHandler::HandleGoTriggerCommand(const char* args)
310 Player* _player = m_session->GetPlayer();
312 if (!*args)
313 return false;
315 char *atId = strtok((char*)args, " ");
316 if (!atId)
317 return false;
319 int32 i_atId = atoi(atId);
321 if(!i_atId)
322 return false;
324 AreaTriggerEntry const* at = sAreaTriggerStore.LookupEntry(i_atId);
325 if (!at)
327 PSendSysMessage(LANG_COMMAND_GOAREATRNOTFOUND,i_atId);
328 SetSentErrorMessage(true);
329 return false;
332 if(!MapManager::IsValidMapCoord(at->mapid,at->x,at->y,at->z))
334 PSendSysMessage(LANG_INVALID_TARGET_COORD,at->x,at->y,at->mapid);
335 SetSentErrorMessage(true);
336 return false;
339 // stop flight if need
340 if(_player->isInFlight())
342 _player->GetMotionMaster()->MovementExpired();
343 _player->m_taxi.ClearTaxiDestinations();
345 // save only in non-flight case
346 else
347 _player->SaveRecallPosition();
349 _player->TeleportTo(at->mapid, at->x, at->y, at->z, _player->GetOrientation());
350 return true;
353 bool ChatHandler::HandleGoGraveyardCommand(const char* args)
355 Player* _player = m_session->GetPlayer();
357 if (!*args)
358 return false;
360 char *gyId = strtok((char*)args, " ");
361 if (!gyId)
362 return false;
364 int32 i_gyId = atoi(gyId);
366 if(!i_gyId)
367 return false;
369 WorldSafeLocsEntry const* gy = sWorldSafeLocsStore.LookupEntry(i_gyId);
370 if (!gy)
372 PSendSysMessage(LANG_COMMAND_GRAVEYARDNOEXIST,i_gyId);
373 SetSentErrorMessage(true);
374 return false;
377 if(!MapManager::IsValidMapCoord(gy->map_id,gy->x,gy->y,gy->z))
379 PSendSysMessage(LANG_INVALID_TARGET_COORD,gy->x,gy->y,gy->map_id);
380 SetSentErrorMessage(true);
381 return false;
384 // stop flight if need
385 if(_player->isInFlight())
387 _player->GetMotionMaster()->MovementExpired();
388 _player->m_taxi.ClearTaxiDestinations();
390 // save only in non-flight case
391 else
392 _player->SaveRecallPosition();
394 _player->TeleportTo(gy->map_id, gy->x, gy->y, gy->z, _player->GetOrientation());
395 return true;
398 /** \brief Teleport the GM to the specified creature
400 * .gocreature <GUID> --> TP using creature.guid
401 * .gocreature azuregos --> TP player to the mob with this name
402 * Warning: If there is more than one mob with this name
403 * you will be teleported to the first one that is found.
404 * .gocreature id 6109 --> TP player to the mob, that has this creature_template.entry
405 * Warning: If there is more than one mob with this "id"
406 * you will be teleported to the first one that is found.
408 //teleport to creature
409 bool ChatHandler::HandleGoCreatureCommand(const char* args)
411 if(!*args)
412 return false;
413 Player* _player = m_session->GetPlayer();
415 // "id" or number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
416 char* pParam1 = extractKeyFromLink((char*)args,"Hcreature");
417 if (!pParam1)
418 return false;
420 std::ostringstream whereClause;
422 // User wants to teleport to the NPC's template entry
423 if( strcmp(pParam1, "id") == 0 )
425 //sLog.outError("DEBUG: ID found");
427 // Get the "creature_template.entry"
428 // number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
429 char* tail = strtok(NULL,"");
430 if(!tail)
431 return false;
432 char* cId = extractKeyFromLink(tail,"Hcreature_entry");
433 if(!cId)
434 return false;
436 int32 tEntry = atoi(cId);
437 //sLog.outError("DEBUG: ID value: %d", tEntry);
438 if(!tEntry)
439 return false;
441 whereClause << "WHERE id = '" << tEntry << "'";
443 else
445 //sLog.outError("DEBUG: ID *not found*");
447 int32 guid = atoi(pParam1);
449 // Number is invalid - maybe the user specified the mob's name
450 if(!guid)
452 std::string name = pParam1;
453 WorldDatabase.escape_string(name);
454 whereClause << ", creature_template WHERE creature.id = creature_template.entry AND creature_template.name "_LIKE_" '" << name << "'";
456 else
458 whereClause << "WHERE guid = '" << guid << "'";
461 //sLog.outError("DEBUG: %s", whereClause.c_str());
463 QueryResult *result = WorldDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map FROM creature %s", whereClause.str().c_str() );
464 if (!result)
466 SendSysMessage(LANG_COMMAND_GOCREATNOTFOUND);
467 SetSentErrorMessage(true);
468 return false;
470 if( result->GetRowCount() > 1 )
472 SendSysMessage(LANG_COMMAND_GOCREATMULTIPLE);
475 Field *fields = result->Fetch();
476 float x = fields[0].GetFloat();
477 float y = fields[1].GetFloat();
478 float z = fields[2].GetFloat();
479 float ort = fields[3].GetFloat();
480 int mapid = fields[4].GetUInt16();
482 delete result;
484 if(!MapManager::IsValidMapCoord(mapid,x,y,z,ort))
486 PSendSysMessage(LANG_INVALID_TARGET_COORD,x,y,mapid);
487 SetSentErrorMessage(true);
488 return false;
491 // stop flight if need
492 if(_player->isInFlight())
494 _player->GetMotionMaster()->MovementExpired();
495 _player->m_taxi.ClearTaxiDestinations();
497 // save only in non-flight case
498 else
499 _player->SaveRecallPosition();
501 _player->TeleportTo(mapid, x, y, z, ort);
502 return true;
505 bool ChatHandler::HandleGUIDCommand(const char* /*args*/)
507 uint64 guid = m_session->GetPlayer()->GetSelection();
509 if (guid == 0)
511 SendSysMessage(LANG_NO_SELECTION);
512 SetSentErrorMessage(true);
513 return false;
516 PSendSysMessage(LANG_OBJECT_GUID, GUID_LOPART(guid), GUID_HIPART(guid));
517 return true;
520 bool ChatHandler::HandleLookupFactionCommand(const char* args)
522 if (!*args)
523 return false;
525 // Can be NULL at console call
526 Player *target = getSelectedPlayer ();
528 std::string namepart = args;
529 std::wstring wnamepart;
531 if (!Utf8toWStr (namepart,wnamepart))
532 return false;
534 // converting string that we try to find to lower case
535 wstrToLower (wnamepart);
537 uint32 counter = 0; // Counter for figure out that we found smth.
539 for (uint32 id = 0; id < sFactionStore.GetNumRows(); ++id)
541 FactionEntry const *factionEntry = sFactionStore.LookupEntry (id);
542 if (factionEntry)
544 FactionState const* repState = NULL;
545 if(target)
547 FactionStateList::const_iterator repItr = target->m_factions.find (factionEntry->reputationListID);
548 if(repItr != target->m_factions.end())
549 repState = &repItr->second;
553 int loc = m_session ? m_session->GetSessionDbcLocale() : sWorld.GetDefaultDbcLocale();
554 std::string name = factionEntry->name[loc];
555 if(name.empty())
556 continue;
558 if (!Utf8FitTo(name, wnamepart))
560 loc = 0;
561 for(; loc < MAX_LOCALE; ++loc)
563 if(m_session && loc==m_session->GetSessionDbcLocale())
564 continue;
566 name = factionEntry->name[loc];
567 if(name.empty())
568 continue;
570 if (Utf8FitTo(name, wnamepart))
571 break;
575 if(loc < MAX_LOCALE)
577 // send faction in "id - [faction] rank reputation [visible] [at war] [own team] [unknown] [invisible] [inactive]" format
578 // or "id - [faction] [no reputation]" format
579 std::ostringstream ss;
580 if (m_session)
581 ss << id << " - |cffffffff|Hfaction:" << id << "|h[" << name << " " << localeNames[loc] << "]|h|r";
582 else
583 ss << id << " - " << name << " " << localeNames[loc];
585 if (repState) // and then target!=NULL also
587 ReputationRank rank = target->GetReputationRank(factionEntry);
588 std::string rankName = GetMangosString(ReputationRankStrIndex[rank]);
590 ss << " " << rankName << "|h|r (" << target->GetReputation(factionEntry) << ")";
592 if(repState->Flags & FACTION_FLAG_VISIBLE)
593 ss << GetMangosString(LANG_FACTION_VISIBLE);
594 if(repState->Flags & FACTION_FLAG_AT_WAR)
595 ss << GetMangosString(LANG_FACTION_ATWAR);
596 if(repState->Flags & FACTION_FLAG_PEACE_FORCED)
597 ss << GetMangosString(LANG_FACTION_PEACE_FORCED);
598 if(repState->Flags & FACTION_FLAG_HIDDEN)
599 ss << GetMangosString(LANG_FACTION_HIDDEN);
600 if(repState->Flags & FACTION_FLAG_INVISIBLE_FORCED)
601 ss << GetMangosString(LANG_FACTION_INVISIBLE_FORCED);
602 if(repState->Flags & FACTION_FLAG_INACTIVE)
603 ss << GetMangosString(LANG_FACTION_INACTIVE);
605 else
606 ss << GetMangosString(LANG_FACTION_NOREPUTATION);
608 SendSysMessage(ss.str().c_str());
609 counter++;
614 if (counter == 0) // if counter == 0 then we found nth
615 SendSysMessage(LANG_COMMAND_FACTION_NOTFOUND);
616 return true;
619 bool ChatHandler::HandleModifyRepCommand(const char * args)
621 if (!*args) return false;
623 Player* target = NULL;
624 target = getSelectedPlayer();
626 if(!target)
628 SendSysMessage(LANG_PLAYER_NOT_FOUND);
629 SetSentErrorMessage(true);
630 return false;
633 // check online security
634 if (HasLowerSecurity(target, 0))
635 return false;
637 char* factionTxt = extractKeyFromLink((char*)args,"Hfaction");
638 if(!factionTxt)
639 return false;
641 uint32 factionId = atoi(factionTxt);
643 int32 amount = 0;
644 char *rankTxt = strtok(NULL, " ");
645 if (!factionTxt || !rankTxt)
646 return false;
648 amount = atoi(rankTxt);
649 if ((amount == 0) && (rankTxt[0] != '-') && !isdigit(rankTxt[0]))
651 std::string rankStr = rankTxt;
652 std::wstring wrankStr;
653 if(!Utf8toWStr(rankStr,wrankStr))
654 return false;
655 wstrToLower( wrankStr );
657 int r = 0;
658 amount = -42000;
659 for (; r < MAX_REPUTATION_RANK; ++r)
661 std::string rank = GetMangosString(ReputationRankStrIndex[r]);
662 if(rank.empty())
663 continue;
665 std::wstring wrank;
666 if(!Utf8toWStr(rank,wrank))
667 continue;
669 wstrToLower(wrank);
671 if(wrank.substr(0,wrankStr.size())==wrankStr)
673 char *deltaTxt = strtok(NULL, " ");
674 if (deltaTxt)
676 int32 delta = atoi(deltaTxt);
677 if ((delta < 0) || (delta > Player::ReputationRank_Length[r] -1))
679 PSendSysMessage(LANG_COMMAND_FACTION_DELTA, (Player::ReputationRank_Length[r]-1));
680 SetSentErrorMessage(true);
681 return false;
683 amount += delta;
685 break;
687 amount += Player::ReputationRank_Length[r];
689 if (r >= MAX_REPUTATION_RANK)
691 PSendSysMessage(LANG_COMMAND_FACTION_INVPARAM, rankTxt);
692 SetSentErrorMessage(true);
693 return false;
697 FactionEntry const *factionEntry = sFactionStore.LookupEntry(factionId);
699 if (!factionEntry)
701 PSendSysMessage(LANG_COMMAND_FACTION_UNKNOWN, factionId);
702 SetSentErrorMessage(true);
703 return false;
706 if (factionEntry->reputationListID < 0)
708 PSendSysMessage(LANG_COMMAND_FACTION_NOREP_ERROR, factionEntry->name[m_session->GetSessionDbcLocale()], factionId);
709 SetSentErrorMessage(true);
710 return false;
713 target->SetFactionReputation(factionEntry,amount);
714 PSendSysMessage(LANG_COMMAND_MODIFY_REP, factionEntry->name[m_session->GetSessionDbcLocale()], factionId, GetNameLink(target).c_str(), target->GetReputation(factionId));
715 return true;
718 bool ChatHandler::HandleNameCommand(const char* /*args*/)
720 /* Temp. disabled
721 if(!*args)
722 return false;
724 if(strlen((char*)args)>75)
726 PSendSysMessage(LANG_TOO_LONG_NAME, strlen((char*)args)-75);
727 return true;
730 for (uint8 i = 0; i < strlen(args); i++)
732 if(!isalpha(args[i]) && args[i]!=' ')
734 SendSysMessage(LANG_CHARS_ONLY);
735 return false;
739 uint64 guid;
740 guid = m_session->GetPlayer()->GetSelection();
741 if (guid == 0)
743 SendSysMessage(LANG_NO_SELECTION);
744 return true;
747 Creature* pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), guid);
749 if(!pCreature)
751 SendSysMessage(LANG_SELECT_CREATURE);
752 return true;
755 pCreature->SetName(args);
756 uint32 idname = objmgr.AddCreatureTemplate(pCreature->GetName());
757 pCreature->SetUInt32Value(OBJECT_FIELD_ENTRY, idname);
759 pCreature->SaveToDB();
762 return true;
765 bool ChatHandler::HandleSubNameCommand(const char* /*args*/)
767 /* Temp. disabled
769 if(!*args)
770 args = "";
772 if(strlen((char*)args)>75)
775 PSendSysMessage(LANG_TOO_LONG_SUBNAME, strlen((char*)args)-75);
776 return true;
779 for (uint8 i = 0; i < strlen(args); i++)
781 if(!isalpha(args[i]) && args[i]!=' ')
783 SendSysMessage(LANG_CHARS_ONLY);
784 return false;
787 uint64 guid;
788 guid = m_session->GetPlayer()->GetSelection();
789 if (guid == 0)
791 SendSysMessage(LANG_NO_SELECTION);
792 return true;
795 Creature* pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), guid);
797 if(!pCreature)
799 SendSysMessage(LANG_SELECT_CREATURE);
800 return true;
803 uint32 idname = objmgr.AddCreatureSubName(pCreature->GetName(),args,pCreature->GetUInt32Value(UNIT_FIELD_DISPLAYID));
804 pCreature->SetUInt32Value(OBJECT_FIELD_ENTRY, idname);
806 pCreature->SaveToDB();
808 return true;
811 //move item to other slot
812 bool ChatHandler::HandleItemMoveCommand(const char* args)
814 if(!*args)
815 return false;
816 uint8 srcslot, dstslot;
818 char* pParam1 = strtok((char*)args, " ");
819 if (!pParam1)
820 return false;
822 char* pParam2 = strtok(NULL, " ");
823 if (!pParam2)
824 return false;
826 srcslot = (uint8)atoi(pParam1);
827 dstslot = (uint8)atoi(pParam2);
829 if(srcslot==dstslot)
830 return true;
832 if(!m_session->GetPlayer()->IsValidPos(INVENTORY_SLOT_BAG_0,srcslot))
833 return false;
835 if(!m_session->GetPlayer()->IsValidPos(INVENTORY_SLOT_BAG_0,dstslot))
836 return false;
838 uint16 src = ((INVENTORY_SLOT_BAG_0 << 8) | srcslot);
839 uint16 dst = ((INVENTORY_SLOT_BAG_0 << 8) | dstslot);
841 m_session->GetPlayer()->SwapItem( src, dst );
843 return true;
846 //add spawn of creature
847 bool ChatHandler::HandleNpcAddCommand(const char* args)
849 if(!*args)
850 return false;
851 char* charID = extractKeyFromLink((char*)args,"Hcreature_entry");
852 if(!charID)
853 return false;
855 char* team = strtok(NULL, " ");
856 int32 teamval = 0;
857 if (team) { teamval = atoi(team); }
858 if (teamval < 0) { teamval = 0; }
860 uint32 id = atoi(charID);
862 Player *chr = m_session->GetPlayer();
863 float x = chr->GetPositionX();
864 float y = chr->GetPositionY();
865 float z = chr->GetPositionZ();
866 float o = chr->GetOrientation();
867 Map *map = chr->GetMap();
869 Creature* pCreature = new Creature;
870 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), map, chr->GetPhaseMaskForSpawn(), id, (uint32)teamval))
872 delete pCreature;
873 return false;
876 pCreature->Relocate(x,y,z,o);
878 if(!pCreature->IsPositionValid())
880 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",pCreature->GetGUIDLow(),pCreature->GetEntry(),pCreature->GetPositionX(),pCreature->GetPositionY());
881 delete pCreature;
882 return false;
885 pCreature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
887 uint32 db_guid = pCreature->GetDBTableGUIDLow();
889 // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
890 pCreature->LoadFromDB(db_guid, map);
892 map->Add(pCreature);
893 objmgr.AddCreatureToGrid(db_guid, objmgr.GetCreatureData(db_guid));
894 return true;
897 bool ChatHandler::HandleNpcDeleteCommand(const char* args)
899 Creature* unit = NULL;
901 if(*args)
903 // number or [name] Shift-click form |color|Hcreature:creature_guid|h[name]|h|r
904 char* cId = extractKeyFromLink((char*)args,"Hcreature");
905 if(!cId)
906 return false;
908 uint32 lowguid = atoi(cId);
909 if(!lowguid)
910 return false;
912 if (CreatureData const* cr_data = objmgr.GetCreatureData(lowguid))
913 unit = ObjectAccessor::GetCreature(*m_session->GetPlayer(), MAKE_NEW_GUID(lowguid, cr_data->id, HIGHGUID_UNIT));
915 else
916 unit = getSelectedCreature();
918 if(!unit || unit->isPet() || unit->isTotem() || unit->isVehicle())
920 SendSysMessage(LANG_SELECT_CREATURE);
921 SetSentErrorMessage(true);
922 return false;
925 // Delete the creature
926 unit->CombatStop();
927 unit->DeleteFromDB();
928 unit->CleanupsBeforeDelete();
929 unit->AddObjectToRemoveList();
931 SendSysMessage(LANG_COMMAND_DELCREATMESSAGE);
933 return true;
936 //delete object by selection or guid
937 bool ChatHandler::HandleDelObjectCommand(const char* args)
939 // number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
940 char* cId = extractKeyFromLink((char*)args,"Hgameobject");
941 if(!cId)
942 return false;
944 uint32 lowguid = atoi(cId);
945 if(!lowguid)
946 return false;
948 GameObject* obj = NULL;
950 // by DB guid
951 if (GameObjectData const* go_data = objmgr.GetGOData(lowguid))
952 obj = GetObjectGlobalyWithGuidOrNearWithDbGuid(lowguid,go_data->id);
954 if(!obj)
956 PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, lowguid);
957 SetSentErrorMessage(true);
958 return false;
961 uint64 owner_guid = obj->GetOwnerGUID();
962 if(owner_guid)
964 Unit* owner = ObjectAccessor::GetUnit(*m_session->GetPlayer(),owner_guid);
965 if(!owner && !IS_PLAYER_GUID(owner_guid))
967 PSendSysMessage(LANG_COMMAND_DELOBJREFERCREATURE, GUID_LOPART(owner_guid), obj->GetGUIDLow());
968 SetSentErrorMessage(true);
969 return false;
972 owner->RemoveGameObject(obj,false);
975 obj->SetRespawnTime(0); // not save respawn time
976 obj->Delete();
977 obj->DeleteFromDB();
979 PSendSysMessage(LANG_COMMAND_DELOBJMESSAGE, obj->GetGUIDLow());
981 return true;
984 //turn selected object
985 bool ChatHandler::HandleTurnObjectCommand(const char* args)
987 // number or [name] Shift-click form |color|Hgameobject:go_id|h[name]|h|r
988 char* cId = extractKeyFromLink((char*)args,"Hgameobject");
989 if(!cId)
990 return false;
992 uint32 lowguid = atoi(cId);
993 if(!lowguid)
994 return false;
996 GameObject* obj = NULL;
998 // by DB guid
999 if (GameObjectData const* go_data = objmgr.GetGOData(lowguid))
1000 obj = GetObjectGlobalyWithGuidOrNearWithDbGuid(lowguid,go_data->id);
1002 if(!obj)
1004 PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, lowguid);
1005 SetSentErrorMessage(true);
1006 return false;
1009 char* po = strtok(NULL, " ");
1010 float o;
1012 if (po)
1014 o = (float)atof(po);
1016 else
1018 Player *chr = m_session->GetPlayer();
1019 o = chr->GetOrientation();
1022 float rot2 = sin(o/2);
1023 float rot3 = cos(o/2);
1025 Map* map = obj->GetMap();
1026 map->Remove(obj,false);
1028 obj->Relocate(obj->GetPositionX(), obj->GetPositionY(), obj->GetPositionZ(), o);
1030 obj->SetFloatValue(GAMEOBJECT_FACING, o);
1031 obj->SetFloatValue(GAMEOBJECT_PARENTROTATION+2, rot2);
1032 obj->SetFloatValue(GAMEOBJECT_PARENTROTATION+3, rot3);
1034 map->Add(obj);
1036 obj->SaveToDB();
1037 obj->Refresh();
1039 PSendSysMessage(LANG_COMMAND_TURNOBJMESSAGE, obj->GetGUIDLow(), o);
1041 return true;
1044 //move selected creature
1045 bool ChatHandler::HandleNpcMoveCommand(const char* args)
1047 uint32 lowguid = 0;
1049 Creature* pCreature = getSelectedCreature();
1051 if(!pCreature)
1053 // number or [name] Shift-click form |color|Hcreature:creature_guid|h[name]|h|r
1054 char* cId = extractKeyFromLink((char*)args,"Hcreature");
1055 if(!cId)
1056 return false;
1058 uint32 lowguid = atoi(cId);
1060 /* FIXME: impossibel without entry
1061 if(lowguid)
1062 pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_GUID(lowguid,HIGHGUID_UNIT));
1065 // Attempting creature load from DB data
1066 if(!pCreature)
1068 CreatureData const* data = objmgr.GetCreatureData(lowguid);
1069 if(!data)
1071 PSendSysMessage(LANG_COMMAND_CREATGUIDNOTFOUND, lowguid);
1072 SetSentErrorMessage(true);
1073 return false;
1076 uint32 map_id = data->mapid;
1078 if(m_session->GetPlayer()->GetMapId()!=map_id)
1080 PSendSysMessage(LANG_COMMAND_CREATUREATSAMEMAP, lowguid);
1081 SetSentErrorMessage(true);
1082 return false;
1085 else
1087 lowguid = pCreature->GetDBTableGUIDLow();
1090 else
1092 lowguid = pCreature->GetDBTableGUIDLow();
1095 float x = m_session->GetPlayer()->GetPositionX();
1096 float y = m_session->GetPlayer()->GetPositionY();
1097 float z = m_session->GetPlayer()->GetPositionZ();
1098 float o = m_session->GetPlayer()->GetOrientation();
1100 if (pCreature)
1102 if(CreatureData const* data = objmgr.GetCreatureData(pCreature->GetDBTableGUIDLow()))
1104 const_cast<CreatureData*>(data)->posX = x;
1105 const_cast<CreatureData*>(data)->posY = y;
1106 const_cast<CreatureData*>(data)->posZ = z;
1107 const_cast<CreatureData*>(data)->orientation = o;
1109 pCreature->GetMap()->CreatureRelocation(pCreature,x, y, z,o);
1110 pCreature->GetMotionMaster()->Initialize();
1111 if(pCreature->isAlive()) // dead creature will reset movement generator at respawn
1113 pCreature->setDeathState(JUST_DIED);
1114 pCreature->Respawn();
1118 WorldDatabase.PExecuteLog("UPDATE creature SET position_x = '%f', position_y = '%f', position_z = '%f', orientation = '%f' WHERE guid = '%u'", x, y, z, o, lowguid);
1119 PSendSysMessage(LANG_COMMAND_CREATUREMOVED);
1120 return true;
1123 //move selected object
1124 bool ChatHandler::HandleMoveObjectCommand(const char* args)
1126 // number or [name] Shift-click form |color|Hgameobject:go_guid|h[name]|h|r
1127 char* cId = extractKeyFromLink((char*)args,"Hgameobject");
1128 if(!cId)
1129 return false;
1131 uint32 lowguid = atoi(cId);
1132 if(!lowguid)
1133 return false;
1135 GameObject* obj = NULL;
1137 // by DB guid
1138 if (GameObjectData const* go_data = objmgr.GetGOData(lowguid))
1139 obj = GetObjectGlobalyWithGuidOrNearWithDbGuid(lowguid,go_data->id);
1141 if(!obj)
1143 PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, lowguid);
1144 SetSentErrorMessage(true);
1145 return false;
1148 char* px = strtok(NULL, " ");
1149 char* py = strtok(NULL, " ");
1150 char* pz = strtok(NULL, " ");
1152 if (!px)
1154 Player *chr = m_session->GetPlayer();
1156 Map* map = obj->GetMap();
1157 map->Remove(obj,false);
1159 obj->Relocate(chr->GetPositionX(), chr->GetPositionY(), chr->GetPositionZ(), obj->GetOrientation());
1160 obj->SetFloatValue(GAMEOBJECT_POS_X, chr->GetPositionX());
1161 obj->SetFloatValue(GAMEOBJECT_POS_Y, chr->GetPositionY());
1162 obj->SetFloatValue(GAMEOBJECT_POS_Z, chr->GetPositionZ());
1164 map->Add(obj);
1166 else
1168 if(!py || !pz)
1169 return false;
1171 float x = (float)atof(px);
1172 float y = (float)atof(py);
1173 float z = (float)atof(pz);
1175 if(!MapManager::IsValidMapCoord(obj->GetMapId(),x,y,z))
1177 PSendSysMessage(LANG_INVALID_TARGET_COORD,x,y,obj->GetMapId());
1178 SetSentErrorMessage(true);
1179 return false;
1182 Map* map = obj->GetMap();
1183 map->Remove(obj,false);
1185 obj->Relocate(x, y, z, obj->GetOrientation());
1186 obj->SetFloatValue(GAMEOBJECT_POS_X, x);
1187 obj->SetFloatValue(GAMEOBJECT_POS_Y, y);
1188 obj->SetFloatValue(GAMEOBJECT_POS_Z, z);
1190 map->Add(obj);
1193 obj->SaveToDB();
1194 obj->Refresh();
1196 PSendSysMessage(LANG_COMMAND_MOVEOBJMESSAGE, obj->GetGUIDLow());
1198 return true;
1201 //demorph player or unit
1202 bool ChatHandler::HandleDeMorphCommand(const char* /*args*/)
1204 Unit *target = getSelectedUnit();
1205 if(!target)
1206 target = m_session->GetPlayer();
1209 // check online security
1210 else if (target->GetTypeId() == TYPEID_PLAYER && HasLowerSecurity((Player*)target, 0))
1211 return false;
1213 target->DeMorph();
1215 return true;
1218 //add item in vendorlist
1219 bool ChatHandler::HandleAddVendorItemCommand(const char* args)
1221 if (!*args)
1222 return false;
1224 char* pitem = extractKeyFromLink((char*)args,"Hitem");
1225 if (!pitem)
1227 SendSysMessage(LANG_COMMAND_NEEDITEMSEND);
1228 SetSentErrorMessage(true);
1229 return false;
1232 uint32 itemId = atol(pitem);
1234 char* fmaxcount = strtok(NULL, " "); //add maxcount, default: 0
1235 uint32 maxcount = 0;
1236 if (fmaxcount)
1237 maxcount = atol(fmaxcount);
1239 char* fincrtime = strtok(NULL, " "); //add incrtime, default: 0
1240 uint32 incrtime = 0;
1241 if (fincrtime)
1242 incrtime = atol(fincrtime);
1244 char* fextendedcost = strtok(NULL, " "); //add ExtendedCost, default: 0
1245 uint32 extendedcost = fextendedcost ? atol(fextendedcost) : 0;
1247 Creature* vendor = getSelectedCreature();
1249 uint32 vendor_entry = vendor ? vendor->GetEntry() : 0;
1251 if(!objmgr.IsVendorItemValid(vendor_entry,itemId,maxcount,incrtime,extendedcost,m_session->GetPlayer()))
1253 SetSentErrorMessage(true);
1254 return false;
1257 objmgr.AddVendorItem(vendor_entry,itemId,maxcount,incrtime,extendedcost);
1259 ItemPrototype const* pProto = objmgr.GetItemPrototype(itemId);
1261 PSendSysMessage(LANG_ITEM_ADDED_TO_LIST,itemId,pProto->Name1,maxcount,incrtime,extendedcost);
1262 return true;
1265 //del item from vendor list
1266 bool ChatHandler::HandleDelVendorItemCommand(const char* args)
1268 if (!*args)
1269 return false;
1271 Creature* vendor = getSelectedCreature();
1272 if (!vendor || !vendor->isVendor())
1274 SendSysMessage(LANG_COMMAND_VENDORSELECTION);
1275 SetSentErrorMessage(true);
1276 return false;
1279 char* pitem = extractKeyFromLink((char*)args,"Hitem");
1280 if (!pitem)
1282 SendSysMessage(LANG_COMMAND_NEEDITEMSEND);
1283 SetSentErrorMessage(true);
1284 return false;
1286 uint32 itemId = atol(pitem);
1288 if(!objmgr.RemoveVendorItem(vendor->GetEntry(),itemId))
1290 PSendSysMessage(LANG_ITEM_NOT_IN_LIST,itemId);
1291 SetSentErrorMessage(true);
1292 return false;
1295 ItemPrototype const* pProto = objmgr.GetItemPrototype(itemId);
1297 PSendSysMessage(LANG_ITEM_DELETED_FROM_LIST,itemId,pProto->Name1);
1298 return true;
1301 //add move for creature
1302 bool ChatHandler::HandleNpcAddMoveCommand(const char* args)
1304 if(!*args)
1305 return false;
1307 char* guid_str = strtok((char*)args, " ");
1308 char* wait_str = strtok((char*)NULL, " ");
1310 uint32 lowguid = atoi((char*)guid_str);
1312 Creature* pCreature = NULL;
1314 /* FIXME: impossible without entry
1315 if(lowguid)
1316 pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_GUID(lowguid,HIGHGUID_UNIT));
1319 // attempt check creature existence by DB data
1320 if(!pCreature)
1322 CreatureData const* data = objmgr.GetCreatureData(lowguid);
1323 if(!data)
1325 PSendSysMessage(LANG_COMMAND_CREATGUIDNOTFOUND, lowguid);
1326 SetSentErrorMessage(true);
1327 return false;
1330 else
1332 // obtain real GUID for DB operations
1333 lowguid = pCreature->GetDBTableGUIDLow();
1336 int wait = wait_str ? atoi(wait_str) : 0;
1338 if(wait < 0)
1339 wait = 0;
1341 Player* player = m_session->GetPlayer();
1343 WaypointMgr.AddLastNode(lowguid, player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), player->GetOrientation(), wait, 0);
1345 // update movement type
1346 WorldDatabase.PExecuteLog("UPDATE creature SET MovementType = '%u' WHERE guid = '%u'", WAYPOINT_MOTION_TYPE,lowguid);
1347 if(pCreature)
1349 pCreature->SetDefaultMovementType(WAYPOINT_MOTION_TYPE);
1350 pCreature->GetMotionMaster()->Initialize();
1351 if(pCreature->isAlive()) // dead creature will reset movement generator at respawn
1353 pCreature->setDeathState(JUST_DIED);
1354 pCreature->Respawn();
1356 pCreature->SaveToDB();
1359 SendSysMessage(LANG_WAYPOINT_ADDED);
1361 return true;
1365 * Set the movement type for an NPC.<br/>
1366 * <br/>
1367 * Valid movement types are:
1368 * <ul>
1369 * <li> stay - NPC wont move </li>
1370 * <li> random - NPC will move randomly according to the spawndist </li>
1371 * <li> way - NPC will move with given waypoints set </li>
1372 * </ul>
1373 * additional parameter: NODEL - so no waypoints are deleted, if you
1374 * change the movement type
1376 bool ChatHandler::HandleNpcSetMoveTypeCommand(const char* args)
1378 if(!*args)
1379 return false;
1381 // 3 arguments:
1382 // GUID (optional - you can also select the creature)
1383 // stay|random|way (determines the kind of movement)
1384 // NODEL (optional - tells the system NOT to delete any waypoints)
1385 // this is very handy if you want to do waypoints, that are
1386 // later switched on/off according to special events (like escort
1387 // quests, etc)
1388 char* guid_str = strtok((char*)args, " ");
1389 char* type_str = strtok((char*)NULL, " ");
1390 char* dontdel_str = strtok((char*)NULL, " ");
1392 bool doNotDelete = false;
1394 if(!guid_str)
1395 return false;
1397 uint32 lowguid = 0;
1398 Creature* pCreature = NULL;
1400 if( dontdel_str )
1402 //sLog.outError("DEBUG: All 3 params are set");
1404 // All 3 params are set
1405 // GUID
1406 // type
1407 // doNotDEL
1408 if( stricmp( dontdel_str, "NODEL" ) == 0 )
1410 //sLog.outError("DEBUG: doNotDelete = true;");
1411 doNotDelete = true;
1414 else
1416 // Only 2 params - but maybe NODEL is set
1417 if( type_str )
1419 sLog.outError("DEBUG: Only 2 params ");
1420 if( stricmp( type_str, "NODEL" ) == 0 )
1422 //sLog.outError("DEBUG: type_str, NODEL ");
1423 doNotDelete = true;
1424 type_str = NULL;
1429 if(!type_str) // case .setmovetype $move_type (with selected creature)
1431 type_str = guid_str;
1432 pCreature = getSelectedCreature();
1433 if(!pCreature || pCreature->isPet())
1434 return false;
1435 lowguid = pCreature->GetDBTableGUIDLow();
1437 else // case .setmovetype #creature_guid $move_type (with selected creature)
1439 lowguid = atoi((char*)guid_str);
1441 /* impossible without entry
1442 if(lowguid)
1443 pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_GUID(lowguid,HIGHGUID_UNIT));
1446 // attempt check creature existence by DB data
1447 if(!pCreature)
1449 CreatureData const* data = objmgr.GetCreatureData(lowguid);
1450 if(!data)
1452 PSendSysMessage(LANG_COMMAND_CREATGUIDNOTFOUND, lowguid);
1453 SetSentErrorMessage(true);
1454 return false;
1457 else
1459 lowguid = pCreature->GetDBTableGUIDLow();
1463 // now lowguid is low guid really existed creature
1464 // and pCreature point (maybe) to this creature or NULL
1466 MovementGeneratorType move_type;
1468 std::string type = type_str;
1470 if(type == "stay")
1471 move_type = IDLE_MOTION_TYPE;
1472 else if(type == "random")
1473 move_type = RANDOM_MOTION_TYPE;
1474 else if(type == "way")
1475 move_type = WAYPOINT_MOTION_TYPE;
1476 else
1477 return false;
1479 // update movement type
1480 if(doNotDelete == false)
1481 WaypointMgr.DeletePath(lowguid);
1483 if(pCreature)
1485 pCreature->SetDefaultMovementType(move_type);
1486 pCreature->GetMotionMaster()->Initialize();
1487 if(pCreature->isAlive()) // dead creature will reset movement generator at respawn
1489 pCreature->setDeathState(JUST_DIED);
1490 pCreature->Respawn();
1492 pCreature->SaveToDB();
1494 if( doNotDelete == false )
1496 PSendSysMessage(LANG_MOVE_TYPE_SET,type_str);
1498 else
1500 PSendSysMessage(LANG_MOVE_TYPE_SET_NODEL,type_str);
1503 return true;
1504 } // HandleNpcSetMoveTypeCommand
1506 //change level of creature or pet
1507 bool ChatHandler::HandleChangeLevelCommand(const char* args)
1509 if (!*args)
1510 return false;
1512 uint8 lvl = (uint8) atoi((char*)args);
1513 if ( lvl < 1 || lvl > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) + 3)
1515 SendSysMessage(LANG_BAD_VALUE);
1516 SetSentErrorMessage(true);
1517 return false;
1520 Creature* pCreature = getSelectedCreature();
1521 if(!pCreature)
1523 SendSysMessage(LANG_SELECT_CREATURE);
1524 SetSentErrorMessage(true);
1525 return false;
1528 if(pCreature->isPet())
1530 ((Pet*)pCreature)->GivePetLevel(lvl);
1532 else
1534 pCreature->SetMaxHealth( 100 + 30*lvl);
1535 pCreature->SetHealth( 100 + 30*lvl);
1536 pCreature->SetLevel( lvl);
1537 pCreature->SaveToDB();
1540 return true;
1543 //set npcflag of creature
1544 bool ChatHandler::HandleNpcFlagCommand(const char* args)
1546 if (!*args)
1547 return false;
1549 uint32 npcFlags = (uint32) atoi((char*)args);
1551 Creature* pCreature = getSelectedCreature();
1553 if(!pCreature)
1555 SendSysMessage(LANG_SELECT_CREATURE);
1556 SetSentErrorMessage(true);
1557 return false;
1560 pCreature->SetUInt32Value(UNIT_NPC_FLAGS, npcFlags);
1562 WorldDatabase.PExecuteLog("UPDATE creature_template SET npcflag = '%u' WHERE entry = '%u'", npcFlags, pCreature->GetEntry());
1564 SendSysMessage(LANG_VALUE_SAVED_REJOIN);
1566 return true;
1569 //set model of creature
1570 bool ChatHandler::HandleNpcSetModelCommand(const char* args)
1572 if (!*args)
1573 return false;
1575 uint32 displayId = (uint32) atoi((char*)args);
1577 Creature *pCreature = getSelectedCreature();
1579 if(!pCreature || pCreature->isPet())
1581 SendSysMessage(LANG_SELECT_CREATURE);
1582 SetSentErrorMessage(true);
1583 return false;
1586 pCreature->SetDisplayId(displayId);
1587 pCreature->SetNativeDisplayId(displayId);
1589 pCreature->SaveToDB();
1591 return true;
1594 //morph creature or player
1595 bool ChatHandler::HandleMorphCommand(const char* args)
1597 if (!*args)
1598 return false;
1600 uint16 display_id = (uint16)atoi((char*)args);
1602 Unit *target = getSelectedUnit();
1603 if(!target)
1604 target = m_session->GetPlayer();
1606 // check online security
1607 else if (target->GetTypeId() == TYPEID_PLAYER && HasLowerSecurity((Player*)target, 0))
1608 return false;
1610 target->SetDisplayId(display_id);
1612 return true;
1615 //set faction of creature
1616 bool ChatHandler::HandleNpcFactionIdCommand(const char* args)
1618 if (!*args)
1619 return false;
1621 uint32 factionId = (uint32) atoi((char*)args);
1623 if (!sFactionTemplateStore.LookupEntry(factionId))
1625 PSendSysMessage(LANG_WRONG_FACTION, factionId);
1626 SetSentErrorMessage(true);
1627 return false;
1630 Creature* pCreature = getSelectedCreature();
1632 if(!pCreature)
1634 SendSysMessage(LANG_SELECT_CREATURE);
1635 SetSentErrorMessage(true);
1636 return false;
1639 pCreature->setFaction(factionId);
1641 // faction is set in creature_template - not inside creature
1643 // update in memory
1644 if(CreatureInfo const *cinfo = pCreature->GetCreatureInfo())
1646 const_cast<CreatureInfo*>(cinfo)->faction_A = factionId;
1647 const_cast<CreatureInfo*>(cinfo)->faction_H = factionId;
1650 // and DB
1651 WorldDatabase.PExecuteLog("UPDATE creature_template SET faction_A = '%u', faction_H = '%u' WHERE entry = '%u'", factionId, factionId, pCreature->GetEntry());
1653 return true;
1656 //kick player
1657 bool ChatHandler::HandleKickPlayerCommand(const char *args)
1659 if (!args)
1661 Player* player = getSelectedPlayer();
1663 if(!player)
1665 SendSysMessage(LANG_NO_CHAR_SELECTED);
1666 SetSentErrorMessage(true);
1667 return false;
1670 if(player==m_session->GetPlayer())
1672 SendSysMessage(LANG_COMMAND_KICKSELF);
1673 SetSentErrorMessage(true);
1674 return false;
1677 // check online security
1678 if (HasLowerSecurity(player, 0))
1679 return false;
1681 player->GetSession()->KickPlayer();
1683 else
1685 std::string name = extractPlayerNameFromLink((char*)args);
1686 if(name.empty())
1688 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1689 SetSentErrorMessage(true);
1690 return false;
1693 if(m_session && name==m_session->GetPlayer()->GetName())
1695 SendSysMessage(LANG_COMMAND_KICKSELF);
1696 SetSentErrorMessage(true);
1697 return false;
1700 // check online security
1701 Player* player = ObjectAccessor::Instance().FindPlayerByName(name.c_str());
1702 if (player && HasLowerSecurity(player, 0))
1703 return false;
1705 std::string nameLink = playerLink(name);
1707 if(sWorld.KickPlayer(name))
1709 PSendSysMessage(LANG_COMMAND_KICKMESSAGE,nameLink.c_str());
1711 else
1712 PSendSysMessage(LANG_COMMAND_KICKNOTFOUNDPLAYER,nameLink.c_str());
1715 return true;
1718 //set temporary phase mask for player
1719 bool ChatHandler::HandleModifyPhaseCommand(const char* args)
1721 if (!*args)
1722 return false;
1724 uint32 phasemask = (uint32)atoi((char*)args);
1726 Unit *target = getSelectedUnit();
1727 if(!target)
1728 target = m_session->GetPlayer();
1730 // check online security
1731 else if (target->GetTypeId() == TYPEID_PLAYER && HasLowerSecurity((Player*)target, 0))
1732 return false;
1734 target->SetPhaseMask(phasemask,true);
1736 return true;
1739 //show info of player
1740 bool ChatHandler::HandlePInfoCommand(const char* args)
1742 Player* target = NULL;
1743 uint64 targetGUID = 0;
1745 char* px = strtok((char*)args, " ");
1746 char* py = NULL;
1748 std::string name;
1750 if (px)
1752 name = extractPlayerNameFromLink(px);
1753 if(name.empty())
1755 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1756 SetSentErrorMessage(true);
1757 return false;
1760 target = objmgr.GetPlayer(name.c_str());
1761 if (target)
1762 py = strtok(NULL, " ");
1763 else
1765 targetGUID = objmgr.GetPlayerGUIDByName(name);
1766 if(targetGUID)
1767 py = strtok(NULL, " ");
1768 else
1769 py = px;
1773 if(!target && !targetGUID)
1775 target = getSelectedPlayer();
1778 if(!target && !targetGUID)
1780 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1781 SetSentErrorMessage(true);
1782 return false;
1785 uint32 accId = 0;
1786 uint32 money = 0;
1787 uint32 total_player_time = 0;
1788 uint32 level = 0;
1789 uint32 latency = 0;
1791 // get additional information from Player object
1792 if(target)
1794 // check online security
1795 if (HasLowerSecurity(target, 0))
1796 return false;
1798 targetGUID = target->GetGUID();
1799 name = target->GetName(); // re-read for case getSelectedPlayer() target
1800 accId = target->GetSession()->GetAccountId();
1801 money = target->GetMoney();
1802 total_player_time = target->GetTotalPlayedTime();
1803 level = target->getLevel();
1804 latency = target->GetSession()->GetLatency();
1806 // get additional information from DB
1807 else
1809 // check offline security
1810 if (HasLowerSecurity(NULL, targetGUID))
1811 return false;
1813 // 0
1814 QueryResult *result = CharacterDatabase.PQuery("SELECT totaltime FROM characters WHERE guid = '%u'", GUID_LOPART(targetGUID));
1815 if (!result)
1817 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1818 SetSentErrorMessage(true);
1819 return false;
1821 Field *fields = result->Fetch();
1822 total_player_time = fields[0].GetUInt32();
1823 delete result;
1825 Tokens data;
1826 if (!Player::LoadValuesArrayFromDB(data,targetGUID))
1828 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1829 SetSentErrorMessage(true);
1830 return false;
1833 money = Player::GetUInt32ValueFromArray(data, PLAYER_FIELD_COINAGE);
1834 level = Player::GetUInt32ValueFromArray(data, UNIT_FIELD_LEVEL);
1836 accId = objmgr.GetPlayerAccountIdByGUID(targetGUID);
1839 std::string username = GetMangosString(LANG_ERROR);
1840 std::string last_ip = GetMangosString(LANG_ERROR);
1841 uint32 security = 0;
1842 std::string last_login = GetMangosString(LANG_ERROR);
1844 QueryResult* result = loginDatabase.PQuery("SELECT username,gmlevel,last_ip,last_login FROM account WHERE id = '%u'",accId);
1845 if(result)
1847 Field* fields = result->Fetch();
1848 username = fields[0].GetCppString();
1849 security = fields[1].GetUInt32();
1851 if(!m_session || m_session->GetSecurity() >= security)
1853 last_ip = fields[2].GetCppString();
1854 last_login = fields[3].GetCppString();
1856 else
1858 last_ip = "-";
1859 last_login = "-";
1862 delete result;
1865 std::string nameLink = playerLink(name);
1867 PSendSysMessage(LANG_PINFO_ACCOUNT, (target?"":GetMangosString(LANG_OFFLINE)), nameLink.c_str(), GUID_LOPART(targetGUID), username.c_str(), accId, security, last_ip.c_str(), last_login.c_str(), latency);
1869 std::string timeStr = secsToTimeString(total_player_time,true,true);
1870 uint32 gold = money /GOLD;
1871 uint32 silv = (money % GOLD) / SILVER;
1872 uint32 copp = (money % GOLD) % SILVER;
1873 PSendSysMessage(LANG_PINFO_LEVEL, timeStr.c_str(), level, gold,silv,copp );
1875 if ( py && strncmp(py, "rep", 3) == 0 )
1877 if(!target)
1879 // rep option not implemented for offline case
1880 SendSysMessage(LANG_PINFO_NO_REP);
1881 SetSentErrorMessage(true);
1882 return false;
1885 for(FactionStateList::const_iterator itr = target->m_factions.begin(); itr != target->m_factions.end(); ++itr)
1887 FactionEntry const *factionEntry = sFactionStore.LookupEntry(itr->second.ID);
1888 char const* factionName = factionEntry ? factionEntry->name[m_session->GetSessionDbcLocale()] : "#Not found#";
1889 ReputationRank rank = target->GetReputationRank(factionEntry);
1890 std::string rankName = GetMangosString(ReputationRankStrIndex[rank]);
1891 std::ostringstream ss;
1892 ss << itr->second.ID << ": |cffffffff|Hfaction:" << itr->second.ID << "|h[" << factionName << "]|h|r " << rankName << "|h|r (" << target->GetReputation(factionEntry) << ")";
1894 if(itr->second.Flags & FACTION_FLAG_VISIBLE)
1895 ss << GetMangosString(LANG_FACTION_VISIBLE);
1896 if(itr->second.Flags & FACTION_FLAG_AT_WAR)
1897 ss << GetMangosString(LANG_FACTION_ATWAR);
1898 if(itr->second.Flags & FACTION_FLAG_PEACE_FORCED)
1899 ss << GetMangosString(LANG_FACTION_PEACE_FORCED);
1900 if(itr->second.Flags & FACTION_FLAG_HIDDEN)
1901 ss << GetMangosString(LANG_FACTION_HIDDEN);
1902 if(itr->second.Flags & FACTION_FLAG_INVISIBLE_FORCED)
1903 ss << GetMangosString(LANG_FACTION_INVISIBLE_FORCED);
1904 if(itr->second.Flags & FACTION_FLAG_INACTIVE)
1905 ss << GetMangosString(LANG_FACTION_INACTIVE);
1907 SendSysMessage(ss.str().c_str());
1910 return true;
1913 //show tickets
1914 void ChatHandler::ShowTicket(uint64 guid, char const* text, char const* time)
1916 std::string name;
1917 if(!objmgr.GetPlayerNameByGUID(guid,name))
1918 name = GetMangosString(LANG_UNKNOWN);
1920 std::string nameLink = playerLink(name);
1922 PSendSysMessage(LANG_COMMAND_TICKETVIEW, nameLink.c_str(),time,text);
1925 //ticket commands
1926 bool ChatHandler::HandleTicketCommand(const char* args)
1928 char* px = strtok((char*)args, " ");
1930 // ticket<end>
1931 if (!px)
1933 if(!m_session)
1935 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1936 SetSentErrorMessage(true);
1937 return false;
1940 size_t count = ticketmgr.GetTicketCount();
1942 bool accept = m_session->GetPlayer()->isAcceptTickets();
1944 PSendSysMessage(LANG_COMMAND_TICKETCOUNT, count, accept ? GetMangosString(LANG_ON) : GetMangosString(LANG_OFF));
1945 return true;
1948 // ticket on
1949 if(strncmp(px,"on",3) == 0)
1951 if(!m_session)
1953 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1954 SetSentErrorMessage(true);
1955 return false;
1958 m_session->GetPlayer()->SetAcceptTicket(true);
1959 SendSysMessage(LANG_COMMAND_TICKETON);
1960 return true;
1963 // ticket off
1964 if(strncmp(px,"off",4) == 0)
1966 if(!m_session)
1968 SendSysMessage(LANG_PLAYER_NOT_FOUND);
1969 SetSentErrorMessage(true);
1970 return false;
1973 m_session->GetPlayer()->SetAcceptTicket(false);
1974 SendSysMessage(LANG_COMMAND_TICKETOFF);
1975 return true;
1978 // ticket #num
1979 int num = atoi(px);
1980 if(num > 0)
1982 QueryResult *result = CharacterDatabase.PQuery("SELECT guid,ticket_text,ticket_lastchange FROM character_ticket ORDER BY ticket_id ASC "_OFFSET_, num-1);
1984 if(!result)
1986 PSendSysMessage(LANG_COMMAND_TICKENOTEXIST, num);
1987 SetSentErrorMessage(true);
1988 return false;
1991 Field* fields = result->Fetch();
1993 uint32 guid = fields[0].GetUInt32();
1994 char const* text = fields[1].GetString();
1995 char const* time = fields[2].GetString();
1997 ShowTicket(MAKE_NEW_GUID(guid, 0, HIGHGUID_PLAYER),text,time);
1998 delete result;
1999 return true;
2002 std::string name = extractPlayerNameFromLink(px);
2003 if(name.empty())
2005 SendSysMessage(LANG_PLAYER_NOT_FOUND);
2006 SetSentErrorMessage(true);
2007 return false;
2010 uint64 guid = objmgr.GetPlayerGUIDByName(name);
2012 if(!guid)
2013 return false;
2015 // ticket $char_name
2016 GMTicket* ticket = ticketmgr.GetGMTicket(GUID_LOPART(guid));
2017 if(!ticket)
2018 return false;
2020 std::string time = TimeToTimestampStr(ticket->GetLastUpdate());
2022 ShowTicket(guid, ticket->GetText(), time.c_str());
2024 return true;
2027 //dell all tickets
2028 bool ChatHandler::HandleDelTicketCommand(const char *args)
2030 char* px = strtok((char*)args, " ");
2031 if (!px)
2032 return false;
2034 // delticket all
2035 if(strncmp(px,"all",4) == 0)
2037 ticketmgr.DeleteAll();
2038 SendSysMessage(LANG_COMMAND_ALLTICKETDELETED);
2039 return true;
2042 int num = (uint32)atoi(px);
2044 // delticket #num
2045 if(num > 0)
2047 QueryResult* result = CharacterDatabase.PQuery("SELECT guid FROM character_ticket ORDER BY ticket_id ASC "_OFFSET_,num-1);
2048 if(!result)
2050 PSendSysMessage(LANG_COMMAND_TICKENOTEXIST, num);
2051 SetSentErrorMessage(true);
2052 return false;
2054 Field* fields = result->Fetch();
2055 uint32 guid = fields[0].GetUInt32();
2056 delete result;
2058 ticketmgr.Delete(guid);
2060 //notify player
2061 if(Player* pl = objmgr.GetPlayer(MAKE_NEW_GUID(guid, 0, HIGHGUID_PLAYER)))
2063 pl->GetSession()->SendGMTicketGetTicket(0x0A, 0);
2064 PSendSysMessage(LANG_COMMAND_TICKETPLAYERDEL, GetNameLink(pl).c_str());
2066 else
2067 PSendSysMessage(LANG_COMMAND_TICKETDEL);
2069 return true;
2072 std::string name = extractPlayerNameFromLink(px);
2073 if(name.empty())
2075 SendSysMessage(LANG_PLAYER_NOT_FOUND);
2076 SetSentErrorMessage(true);
2077 return false;
2080 uint64 guid = objmgr.GetPlayerGUIDByName(name);
2082 if(!guid)
2083 return false;
2085 // delticket $char_name
2086 ticketmgr.Delete(GUID_LOPART(guid));
2088 // notify players about ticket deleting
2089 if(Player* sender = objmgr.GetPlayer(guid))
2090 sender->GetSession()->SendGMTicketGetTicket(0x0A,0);
2092 std::string nameLink = playerLink(name);
2094 PSendSysMessage(LANG_COMMAND_TICKETPLAYERDEL,nameLink.c_str());
2095 return true;
2098 //set spawn dist of creature
2099 bool ChatHandler::HandleNpcSpawnDistCommand(const char* args)
2101 if(!*args)
2102 return false;
2104 float option = atof((char*)args);
2105 if (option < 0.0f)
2107 SendSysMessage(LANG_BAD_VALUE);
2108 return false;
2111 MovementGeneratorType mtype = IDLE_MOTION_TYPE;
2112 if (option >0.0f)
2113 mtype = RANDOM_MOTION_TYPE;
2115 Creature *pCreature = getSelectedCreature();
2116 uint32 u_guidlow = 0;
2118 if (pCreature)
2119 u_guidlow = pCreature->GetDBTableGUIDLow();
2120 else
2121 return false;
2123 pCreature->SetRespawnRadius((float)option);
2124 pCreature->SetDefaultMovementType(mtype);
2125 pCreature->GetMotionMaster()->Initialize();
2126 if(pCreature->isAlive()) // dead creature will reset movement generator at respawn
2128 pCreature->setDeathState(JUST_DIED);
2129 pCreature->Respawn();
2132 WorldDatabase.PExecuteLog("UPDATE creature SET spawndist=%f, MovementType=%i WHERE guid=%u",option,mtype,u_guidlow);
2133 PSendSysMessage(LANG_COMMAND_SPAWNDIST,option);
2134 return true;
2137 bool ChatHandler::HandleNpcSpawnTimeCommand(const char* args)
2139 if(!*args)
2140 return false;
2142 char* stime = strtok((char*)args, " ");
2144 if (!stime)
2145 return false;
2147 int i_stime = atoi((char*)stime);
2149 if (i_stime < 0)
2151 SendSysMessage(LANG_BAD_VALUE);
2152 SetSentErrorMessage(true);
2153 return false;
2156 Creature *pCreature = getSelectedCreature();
2157 uint32 u_guidlow = 0;
2159 if (pCreature)
2160 u_guidlow = pCreature->GetDBTableGUIDLow();
2161 else
2162 return false;
2164 WorldDatabase.PExecuteLog("UPDATE creature SET spawntimesecs=%i WHERE guid=%u",i_stime,u_guidlow);
2165 pCreature->SetRespawnDelay((uint32)i_stime);
2166 PSendSysMessage(LANG_COMMAND_SPAWNTIME,i_stime);
2168 return true;
2172 * Add a waypoint to a creature.
2174 * The user can either select an npc or provide its GUID.
2176 * The user can even select a visual waypoint - then the new waypoint
2177 * is placed *after* the selected one - this makes insertion of new
2178 * waypoints possible.
2180 * eg:
2181 * .wp add 12345
2182 * -> adds a waypoint to the npc with the GUID 12345
2184 * .wp add
2185 * -> adds a waypoint to the currently selected creature
2188 * @param args if the user did not provide a GUID, it is NULL
2190 * @return true - command did succeed, false - something went wrong
2192 bool ChatHandler::HandleWpAddCommand(const char* args)
2194 sLog.outDebug("DEBUG: HandleWpAddCommand");
2196 // optional
2197 char* guid_str = NULL;
2199 if(*args)
2201 guid_str = strtok((char*)args, " ");
2204 uint32 lowguid = 0;
2205 uint32 point = 0;
2206 Creature* target = getSelectedCreature();
2207 // Did player provide a GUID?
2208 if (!guid_str)
2210 sLog.outDebug("DEBUG: HandleWpAddCommand - No GUID provided");
2212 // No GUID provided
2213 // -> Player must have selected a creature
2215 if(!target || target->isPet())
2217 SendSysMessage(LANG_SELECT_CREATURE);
2218 SetSentErrorMessage(true);
2219 return false;
2221 if (target->GetEntry() == VISUAL_WAYPOINT )
2223 sLog.outDebug("DEBUG: HandleWpAddCommand - target->GetEntry() == VISUAL_WAYPOINT (1) ");
2225 QueryResult *result =
2226 WorldDatabase.PQuery( "SELECT id, point FROM creature_movement WHERE wpguid = %u",
2227 target->GetGUIDLow() );
2228 if(!result)
2230 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDSEARCH, target->GetGUIDLow());
2231 // User selected a visual spawnpoint -> get the NPC
2232 // Select NPC GUID
2233 // Since we compare float values, we have to deal with
2234 // some difficulties.
2235 // Here we search for all waypoints that only differ in one from 1 thousand
2236 // (0.001) - There is no other way to compare C++ floats with mySQL floats
2237 // See also: http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
2238 const char* maxDIFF = "0.01";
2239 result = WorldDatabase.PQuery( "SELECT id, point FROM creature_movement WHERE (abs(position_x - %f) <= %s ) and (abs(position_y - %f) <= %s ) and (abs(position_z - %f) <= %s )",
2240 target->GetPositionX(), maxDIFF, target->GetPositionY(), maxDIFF, target->GetPositionZ(), maxDIFF);
2241 if(!result)
2243 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDDBPROBLEM, target->GetGUIDLow());
2244 SetSentErrorMessage(true);
2245 return false;
2250 Field *fields = result->Fetch();
2251 lowguid = fields[0].GetUInt32();
2252 point = fields[1].GetUInt32();
2253 }while( result->NextRow() );
2254 delete result;
2256 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2257 if(!data)
2259 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2260 SetSentErrorMessage(true);
2261 return false;
2264 target = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(lowguid,data->id,HIGHGUID_UNIT));
2265 if(!target)
2267 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDDBPROBLEM, lowguid);
2268 SetSentErrorMessage(true);
2269 return false;
2272 else
2274 lowguid = target->GetDBTableGUIDLow();
2277 else
2279 sLog.outDebug("DEBUG: HandleWpAddCommand - GUID provided");
2281 // GUID provided
2282 // Warn if player also selected a creature
2283 // -> Creature selection is ignored <-
2284 if(target)
2286 SendSysMessage(LANG_WAYPOINT_CREATSELECTED);
2288 lowguid = atoi((char*)guid_str);
2290 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2291 if(!data)
2293 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2294 SetSentErrorMessage(true);
2295 return false;
2298 target = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(lowguid,data->id,HIGHGUID_UNIT));
2299 if(!target || target->isPet())
2301 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2302 SetSentErrorMessage(true);
2303 return false;
2306 // lowguid -> GUID of the NPC
2307 // point -> number of the waypoint (if not 0)
2308 sLog.outDebug("DEBUG: HandleWpAddCommand - danach");
2310 sLog.outDebug("DEBUG: HandleWpAddCommand - point == 0");
2312 Player* player = m_session->GetPlayer();
2313 WaypointMgr.AddLastNode(lowguid, player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(), player->GetOrientation(), 0, 0);
2315 // update movement type
2316 if(target)
2318 target->SetDefaultMovementType(WAYPOINT_MOTION_TYPE);
2319 target->GetMotionMaster()->Initialize();
2320 if(target->isAlive()) // dead creature will reset movement generator at respawn
2322 target->setDeathState(JUST_DIED);
2323 target->Respawn();
2325 target->SaveToDB();
2327 else
2328 WorldDatabase.PExecuteLog("UPDATE creature SET MovementType = '%u' WHERE guid = '%u'", WAYPOINT_MOTION_TYPE,lowguid);
2330 PSendSysMessage(LANG_WAYPOINT_ADDED, point, lowguid);
2332 return true;
2333 } // HandleWpAddCommand
2336 * .wp modify emote | spell | text | del | move | add
2338 * add -> add a WP after the selected visual waypoint
2339 * User must select a visual waypoint and then issue ".wp modify add"
2341 * emote <emoteID>
2342 * User has selected a visual waypoint before.
2343 * <emoteID> is added to this waypoint. Everytime the
2344 * NPC comes to this waypoint, the emote is called.
2346 * emote <GUID> <WPNUM> <emoteID>
2347 * User has not selected visual waypoint before.
2348 * For the waypoint <WPNUM> for the NPC with <GUID>
2349 * an emote <emoteID> is added.
2350 * Everytime the NPC comes to this waypoint, the emote is called.
2353 * info <GUID> <WPNUM> -> User did not select a visual waypoint and
2355 bool ChatHandler::HandleWpModifyCommand(const char* args)
2357 sLog.outDebug("DEBUG: HandleWpModifyCommand");
2359 if(!*args)
2360 return false;
2362 // first arg: add del text emote spell waittime move
2363 char* show_str = strtok((char*)args, " ");
2364 if (!show_str)
2366 return false;
2369 std::string show = show_str;
2370 // Check
2371 // Remember: "show" must also be the name of a column!
2372 if( (show != "emote") && (show != "spell") && (show != "textid1") && (show != "textid2")
2373 && (show != "textid3") && (show != "textid4") && (show != "textid5")
2374 && (show != "waittime") && (show != "del") && (show != "move") && (show != "add")
2375 && (show != "model1") && (show != "model2") && (show != "orientation"))
2377 return false;
2380 // Next arg is: <GUID> <WPNUM> <ARGUMENT>
2382 // Did user provide a GUID
2383 // or did the user select a creature?
2384 // -> variable lowguid is filled with the GUID of the NPC
2385 uint32 lowguid = 0;
2386 uint32 point = 0;
2387 uint32 wpGuid = 0;
2388 Creature* target = getSelectedCreature();
2390 if(target)
2392 sLog.outDebug("DEBUG: HandleWpModifyCommand - User did select an NPC");
2394 // Did the user select a visual spawnpoint?
2395 if (target->GetEntry() != VISUAL_WAYPOINT )
2397 PSendSysMessage(LANG_WAYPOINT_VP_SELECT);
2398 SetSentErrorMessage(true);
2399 return false;
2402 wpGuid = target->GetGUIDLow();
2404 // The visual waypoint
2405 QueryResult *result =
2406 WorldDatabase.PQuery( "SELECT id, point FROM creature_movement WHERE wpguid = %u LIMIT 1",
2407 target->GetGUIDLow() );
2408 if(!result)
2410 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDDBPROBLEM, wpGuid);
2411 SetSentErrorMessage(true);
2412 return false;
2414 sLog.outDebug("DEBUG: HandleWpModifyCommand - After getting wpGuid");
2416 Field *fields = result->Fetch();
2417 lowguid = fields[0].GetUInt32();
2418 point = fields[1].GetUInt32();
2420 // Cleanup memory
2421 sLog.outDebug("DEBUG: HandleWpModifyCommand - Cleanup memory");
2422 delete result;
2424 else
2426 // User did provide <GUID> <WPNUM>
2428 char* guid_str = strtok((char*)NULL, " ");
2429 if( !guid_str )
2431 SendSysMessage(LANG_WAYPOINT_NOGUID);
2432 return false;
2434 lowguid = atoi((char*)guid_str);
2436 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2437 if(!data)
2439 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2440 SetSentErrorMessage(true);
2441 return false;
2444 PSendSysMessage("DEBUG: GUID provided: %d", lowguid);
2446 char* point_str = strtok((char*)NULL, " ");
2447 if( !point_str )
2449 SendSysMessage(LANG_WAYPOINT_NOWAYPOINTGIVEN);
2450 return false;
2452 point = atoi((char*)point_str);
2454 PSendSysMessage("DEBUG: wpNumber provided: %d", point);
2456 // Now we need the GUID of the visual waypoint
2457 // -> "del", "move", "add" command
2459 QueryResult *result = WorldDatabase.PQuery( "SELECT wpguid FROM creature_movement WHERE id = '%u' AND point = '%u' LIMIT 1", lowguid, point);
2460 if (!result)
2462 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDSEARCH, lowguid, point);
2463 SetSentErrorMessage(true);
2464 return false;
2467 Field *fields = result->Fetch();
2468 wpGuid = fields[0].GetUInt32();
2470 // Free memory
2471 delete result;
2474 char* arg_str = NULL;
2475 // Check for argument
2476 if( (show.find("text") == std::string::npos ) && (show != "del") && (show != "move") && (show != "add"))
2478 // Text is enclosed in "<>", all other arguments not
2479 if( show.find("text") != std::string::npos )
2480 arg_str = strtok((char*)NULL, "<>");
2481 else
2482 arg_str = strtok((char*)NULL, " ");
2484 if( !arg_str)
2486 PSendSysMessage(LANG_WAYPOINT_ARGUMENTREQ, show_str);
2487 return false;
2491 sLog.outDebug("DEBUG: HandleWpModifyCommand - Parameters parsed - now execute the command");
2493 // wpGuid -> GUID of the waypoint creature
2494 // lowguid -> GUID of the NPC
2495 // point -> waypoint number
2497 // Special functions:
2498 // add - move - del -> no args commands
2499 // Add a waypoint after the selected visual
2500 if(show == "add" && target)
2502 PSendSysMessage("DEBUG: wp modify add, GUID: %u", lowguid);
2504 // Get the creature for which we read the waypoint
2505 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2506 if(!data)
2508 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2509 SetSentErrorMessage(true);
2510 return false;
2513 Creature* npcCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), MAKE_NEW_GUID(lowguid, data->id, HIGHGUID_UNIT));
2515 if( !npcCreature )
2517 PSendSysMessage(LANG_WAYPOINT_NPCNOTFOUND);
2518 SetSentErrorMessage(true);
2519 return false;
2522 sLog.outDebug("DEBUG: HandleWpModifyCommand - add -- npcCreature");
2524 // What to do:
2525 // Add the visual spawnpoint (DB only)
2526 // Adjust the waypoints
2527 // Respawn the owner of the waypoints
2528 sLog.outDebug("DEBUG: HandleWpModifyCommand - add");
2530 Player* chr = m_session->GetPlayer();
2531 Map *map = chr->GetMap();
2533 if(npcCreature)
2535 npcCreature->GetMotionMaster()->Initialize();
2536 if(npcCreature->isAlive()) // dead creature will reset movement generator at respawn
2538 npcCreature->setDeathState(JUST_DIED);
2539 npcCreature->Respawn();
2543 // create the waypoint creature
2544 wpGuid = 0;
2545 Creature* wpCreature = new Creature;
2546 if (!wpCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), map, chr->GetPhaseMaskForSpawn(), VISUAL_WAYPOINT,0))
2548 PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, VISUAL_WAYPOINT);
2549 delete wpCreature;
2551 else
2553 wpCreature->Relocate(chr->GetPositionX(), chr->GetPositionY(), chr->GetPositionZ(), chr->GetOrientation());
2555 if(!wpCreature->IsPositionValid())
2557 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",wpCreature->GetGUIDLow(),wpCreature->GetEntry(),wpCreature->GetPositionX(),wpCreature->GetPositionY());
2558 delete wpCreature;
2560 else
2562 wpCreature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
2563 // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
2564 wpCreature->LoadFromDB(wpCreature->GetDBTableGUIDLow(), map);
2565 map->Add(wpCreature);
2566 wpGuid = wpCreature->GetGUIDLow();
2570 WaypointMgr.AddAfterNode(lowguid, point, chr->GetPositionX(), chr->GetPositionY(), chr->GetPositionZ(), 0, 0, wpGuid);
2572 if(!wpGuid)
2573 return false;
2575 PSendSysMessage(LANG_WAYPOINT_ADDED_NO, point+1);
2576 return true;
2577 } // add
2579 if(show == "del" && target)
2581 PSendSysMessage("DEBUG: wp modify del, GUID: %u", lowguid);
2583 // Get the creature for which we read the waypoint
2584 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2585 if(!data)
2587 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2588 SetSentErrorMessage(true);
2589 return false;
2592 Creature* npcCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), MAKE_NEW_GUID(lowguid, data->id, HIGHGUID_UNIT));
2594 // wpCreature
2595 Creature* wpCreature = NULL;
2596 if( wpGuid != 0 )
2598 wpCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(wpGuid, VISUAL_WAYPOINT, HIGHGUID_UNIT));
2599 wpCreature->DeleteFromDB();
2600 wpCreature->CleanupsBeforeDelete();
2601 wpCreature->AddObjectToRemoveList();
2604 // What to do:
2605 // Remove the visual spawnpoint
2606 // Adjust the waypoints
2607 // Respawn the owner of the waypoints
2609 WaypointMgr.DeleteNode(lowguid, point);
2611 if(npcCreature)
2613 // Any waypoints left?
2614 QueryResult *result2 = WorldDatabase.PQuery( "SELECT point FROM creature_movement WHERE id = '%u'",lowguid);
2615 if(!result2)
2617 npcCreature->SetDefaultMovementType(RANDOM_MOTION_TYPE);
2619 else
2621 npcCreature->SetDefaultMovementType(WAYPOINT_MOTION_TYPE);
2622 delete result2;
2624 npcCreature->GetMotionMaster()->Initialize();
2625 if(npcCreature->isAlive()) // dead creature will reset movement generator at respawn
2627 npcCreature->setDeathState(JUST_DIED);
2628 npcCreature->Respawn();
2630 npcCreature->SaveToDB();
2633 PSendSysMessage(LANG_WAYPOINT_REMOVED);
2634 return true;
2635 } // del
2637 if(show == "move" && target)
2639 PSendSysMessage("DEBUG: wp move, GUID: %u", lowguid);
2641 Player *chr = m_session->GetPlayer();
2642 Map *map = chr->GetMap();
2644 // Get the creature for which we read the waypoint
2645 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2646 if(!data)
2648 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2649 SetSentErrorMessage(true);
2650 return false;
2653 Creature* npcCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), MAKE_NEW_GUID(lowguid, data->id, HIGHGUID_UNIT));
2655 // wpCreature
2656 Creature* wpCreature = NULL;
2657 // What to do:
2658 // Move the visual spawnpoint
2659 // Respawn the owner of the waypoints
2660 if( wpGuid != 0 )
2662 wpCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(wpGuid, VISUAL_WAYPOINT, HIGHGUID_UNIT));
2663 wpCreature->DeleteFromDB();
2664 wpCreature->CleanupsBeforeDelete();
2665 wpCreature->AddObjectToRemoveList();
2666 // re-create
2667 Creature* wpCreature2 = new Creature;
2668 if (!wpCreature2->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), map, chr->GetPhaseMaskForSpawn(), VISUAL_WAYPOINT, 0))
2670 PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, VISUAL_WAYPOINT);
2671 delete wpCreature2;
2672 return false;
2675 wpCreature2->Relocate(chr->GetPositionX(), chr->GetPositionY(), chr->GetPositionZ(), chr->GetOrientation());
2677 if(!wpCreature2->IsPositionValid())
2679 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",wpCreature2->GetGUIDLow(),wpCreature2->GetEntry(),wpCreature2->GetPositionX(),wpCreature2->GetPositionY());
2680 delete wpCreature2;
2681 return false;
2684 wpCreature2->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
2685 // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
2686 wpCreature2->LoadFromDB(wpCreature2->GetDBTableGUIDLow(), map);
2687 map->Add(wpCreature2);
2688 //npcCreature->GetMap()->Add(wpCreature2);
2691 WaypointMgr.SetNodePosition(lowguid, point, chr->GetPositionX(), chr->GetPositionY(), chr->GetPositionZ());
2693 if(npcCreature)
2695 npcCreature->GetMotionMaster()->Initialize();
2696 if(npcCreature->isAlive()) // dead creature will reset movement generator at respawn
2698 npcCreature->setDeathState(JUST_DIED);
2699 npcCreature->Respawn();
2702 PSendSysMessage(LANG_WAYPOINT_CHANGED);
2704 return true;
2705 } // move
2707 // Create creature - npc that has the waypoint
2708 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2709 if(!data)
2711 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2712 SetSentErrorMessage(true);
2713 return false;
2716 // set in game textids not supported
2717 if( show == "textid1" || show == "textid2" || show == "textid3" ||
2718 show == "textid4" || show == "textid5" )
2720 return false;
2723 WaypointMgr.SetNodeText(lowguid, point, show_str, arg_str);
2725 Creature* npcCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), MAKE_NEW_GUID(lowguid, data->id, HIGHGUID_UNIT));
2726 if(npcCreature)
2728 npcCreature->SetDefaultMovementType(WAYPOINT_MOTION_TYPE);
2729 npcCreature->GetMotionMaster()->Initialize();
2730 if(npcCreature->isAlive()) // dead creature will reset movement generator at respawn
2732 npcCreature->setDeathState(JUST_DIED);
2733 npcCreature->Respawn();
2736 PSendSysMessage(LANG_WAYPOINT_CHANGED_NO, show_str);
2738 return true;
2742 * .wp show info | on | off
2744 * info -> User has selected a visual waypoint before
2746 * info <GUID> <WPNUM> -> User did not select a visual waypoint and
2747 * provided the GUID of the NPC and the number of
2748 * the waypoint.
2750 * on -> User has selected an NPC; all visual waypoints for this
2751 * NPC are added to the world
2753 * on <GUID> -> User did not select an NPC - instead the GUID of the
2754 * NPC is provided. All visual waypoints for this NPC
2755 * are added from the world.
2757 * off -> User has selected an NPC; all visual waypoints for this
2758 * NPC are removed from the world.
2760 * on <GUID> -> User did not select an NPC - instead the GUID of the
2761 * NPC is provided. All visual waypoints for this NPC
2762 * are removed from the world.
2766 bool ChatHandler::HandleWpShowCommand(const char* args)
2768 sLog.outDebug("DEBUG: HandleWpShowCommand");
2770 if(!*args)
2771 return false;
2773 // first arg: on, off, first, last
2774 char* show_str = strtok((char*)args, " ");
2775 if (!show_str)
2777 return false;
2779 // second arg: GUID (optional, if a creature is selected)
2780 char* guid_str = strtok((char*)NULL, " ");
2781 sLog.outDebug("DEBUG: HandleWpShowCommand: show_str: %s guid_str: %s", show_str, guid_str);
2782 //if (!guid_str) {
2783 // return false;
2786 // Did user provide a GUID
2787 // or did the user select a creature?
2788 // -> variable lowguid is filled with the GUID
2789 Creature* target = getSelectedCreature();
2790 // Did player provide a GUID?
2791 if (!guid_str)
2793 sLog.outDebug("DEBUG: HandleWpShowCommand: !guid_str");
2794 // No GUID provided
2795 // -> Player must have selected a creature
2797 if(!target)
2799 SendSysMessage(LANG_SELECT_CREATURE);
2800 SetSentErrorMessage(true);
2801 return false;
2804 else
2806 sLog.outDebug("DEBUG: HandleWpShowCommand: GUID provided");
2807 // GUID provided
2808 // Warn if player also selected a creature
2809 // -> Creature selection is ignored <-
2810 if(target)
2812 SendSysMessage(LANG_WAYPOINT_CREATSELECTED);
2815 uint32 lowguid = atoi((char*)guid_str);
2817 CreatureData const* data = objmgr.GetCreatureData(lowguid);
2818 if(!data)
2820 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2821 SetSentErrorMessage(true);
2822 return false;
2825 target = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(lowguid,data->id,HIGHGUID_UNIT));
2827 if(!target)
2829 PSendSysMessage(LANG_WAYPOINT_CREATNOTFOUND, lowguid);
2830 SetSentErrorMessage(true);
2831 return false;
2835 uint32 lowguid = target->GetDBTableGUIDLow();
2837 std::string show = show_str;
2838 uint32 Maxpoint;
2840 sLog.outDebug("DEBUG: HandleWpShowCommand: lowguid: %u show: %s", lowguid, show_str);
2842 // Show info for the selected waypoint
2843 if(show == "info")
2845 PSendSysMessage("DEBUG: wp info, GUID: %u", lowguid);
2847 // Check if the user did specify a visual waypoint
2848 if( target->GetEntry() != VISUAL_WAYPOINT )
2850 PSendSysMessage(LANG_WAYPOINT_VP_SELECT);
2851 SetSentErrorMessage(true);
2852 return false;
2855 //PSendSysMessage("wp on, GUID: %u", lowguid);
2857 //pCreature->GetPositionX();
2859 QueryResult *result =
2860 WorldDatabase.PQuery( "SELECT id, point, waittime, emote, spell, textid1, textid2, textid3, textid4, textid5, model1, model2 FROM creature_movement WHERE wpguid = %u",
2861 target->GetGUIDLow() );
2862 if(!result)
2864 // Since we compare float values, we have to deal with
2865 // some difficulties.
2866 // Here we search for all waypoints that only differ in one from 1 thousand
2867 // (0.001) - There is no other way to compare C++ floats with mySQL floats
2868 // See also: http://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html
2869 const char* maxDIFF = "0.01";
2870 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDSEARCH, target->GetGUID());
2872 result = WorldDatabase.PQuery( "SELECT id, point, waittime, emote, spell, textid1, textid2, textid3, textid4, textid5, model1, model2 FROM creature_movement WHERE (abs(position_x - %f) <= %s ) and (abs(position_y - %f) <= %s ) and (abs(position_z - %f) <= %s )",
2873 target->GetPositionX(), maxDIFF, target->GetPositionY(), maxDIFF, target->GetPositionZ(), maxDIFF);
2874 if(!result)
2876 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDDBPROBLEM, lowguid);
2877 SetSentErrorMessage(true);
2878 return false;
2883 Field *fields = result->Fetch();
2884 uint32 creGUID = fields[0].GetUInt32();
2885 uint32 point = fields[1].GetUInt32();
2886 int waittime = fields[2].GetUInt32();
2887 uint32 emote = fields[3].GetUInt32();
2888 uint32 spell = fields[4].GetUInt32();
2889 uint32 textid[MAX_WAYPOINT_TEXT];
2890 for(int i = 0; i < MAX_WAYPOINT_TEXT; ++i)
2891 textid[i] = fields[5+i].GetUInt32();
2892 uint32 model1 = fields[10].GetUInt32();
2893 uint32 model2 = fields[11].GetUInt32();
2895 // Get the creature for which we read the waypoint
2896 Creature* wpCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(creGUID,VISUAL_WAYPOINT,HIGHGUID_UNIT));
2898 PSendSysMessage(LANG_WAYPOINT_INFO_TITLE, point, (wpCreature ? wpCreature->GetName() : "<not found>"), creGUID);
2899 PSendSysMessage(LANG_WAYPOINT_INFO_WAITTIME, waittime);
2900 PSendSysMessage(LANG_WAYPOINT_INFO_MODEL, 1, model1);
2901 PSendSysMessage(LANG_WAYPOINT_INFO_MODEL, 2, model2);
2902 PSendSysMessage(LANG_WAYPOINT_INFO_EMOTE, emote);
2903 PSendSysMessage(LANG_WAYPOINT_INFO_SPELL, spell);
2904 for(int i = 0; i < MAX_WAYPOINT_TEXT; ++i)
2905 PSendSysMessage(LANG_WAYPOINT_INFO_TEXT, i+1, textid[i], (textid[i] ? GetMangosString(textid[i]) : ""));
2907 }while( result->NextRow() );
2908 // Cleanup memory
2909 delete result;
2910 return true;
2913 if(show == "on")
2915 PSendSysMessage("DEBUG: wp on, GUID: %u", lowguid);
2917 QueryResult *result = WorldDatabase.PQuery( "SELECT point, position_x,position_y,position_z FROM creature_movement WHERE id = '%u'",lowguid);
2918 if(!result)
2920 PSendSysMessage(LANG_WAYPOINT_NOTFOUND, lowguid);
2921 SetSentErrorMessage(true);
2922 return false;
2924 // Delete all visuals for this NPC
2925 QueryResult *result2 = WorldDatabase.PQuery( "SELECT wpguid FROM creature_movement WHERE id = '%u' and wpguid <> 0", lowguid);
2926 if(result2)
2928 bool hasError = false;
2931 Field *fields = result2->Fetch();
2932 uint32 wpguid = fields[0].GetUInt32();
2933 Creature* pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(wpguid,VISUAL_WAYPOINT,HIGHGUID_UNIT));
2935 if(!pCreature)
2937 PSendSysMessage(LANG_WAYPOINT_NOTREMOVED, wpguid);
2938 hasError = true;
2939 WorldDatabase.PExecuteLog("DELETE FROM creature WHERE guid = '%u'", wpguid);
2941 else
2943 pCreature->DeleteFromDB();
2944 pCreature->CleanupsBeforeDelete();
2945 pCreature->AddObjectToRemoveList();
2948 }while( result2->NextRow() );
2949 delete result2;
2950 if( hasError )
2952 PSendSysMessage(LANG_WAYPOINT_TOOFAR1);
2953 PSendSysMessage(LANG_WAYPOINT_TOOFAR2);
2954 PSendSysMessage(LANG_WAYPOINT_TOOFAR3);
2960 Field *fields = result->Fetch();
2961 uint32 point = fields[0].GetUInt32();
2962 float x = fields[1].GetFloat();
2963 float y = fields[2].GetFloat();
2964 float z = fields[3].GetFloat();
2966 uint32 id = VISUAL_WAYPOINT;
2968 Player *chr = m_session->GetPlayer();
2969 Map *map = chr->GetMap();
2970 float o = chr->GetOrientation();
2972 Creature* wpCreature = new Creature;
2973 if (!wpCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), map, chr->GetPhaseMaskForSpawn(), id, 0))
2975 PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, id);
2976 delete wpCreature;
2977 delete result;
2978 return false;
2981 wpCreature->Relocate(x, y, z, o);
2983 if(!wpCreature->IsPositionValid())
2985 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",wpCreature->GetGUIDLow(),wpCreature->GetEntry(),wpCreature->GetPositionX(),wpCreature->GetPositionY());
2986 delete wpCreature;
2987 delete result;
2988 return false;
2991 wpCreature->SetVisibility(VISIBILITY_OFF);
2992 sLog.outDebug("DEBUG: UPDATE creature_movement SET wpguid = '%u");
2993 // set "wpguid" column to the visual waypoint
2994 WorldDatabase.PExecuteLog("UPDATE creature_movement SET wpguid = '%u' WHERE id = '%u' and point = '%u'", wpCreature->GetGUIDLow(), lowguid, point);
2996 wpCreature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
2997 // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells();
2998 wpCreature->LoadFromDB(wpCreature->GetDBTableGUIDLow(),map);
2999 map->Add(wpCreature);
3000 //wpCreature->GetMap()->Add(wpCreature);
3001 }while( result->NextRow() );
3003 // Cleanup memory
3004 delete result;
3005 return true;
3008 if(show == "first")
3010 PSendSysMessage("DEBUG: wp first, GUID: %u", lowguid);
3012 QueryResult *result = WorldDatabase.PQuery( "SELECT position_x,position_y,position_z FROM creature_movement WHERE point='1' AND id = '%u'",lowguid);
3013 if(!result)
3015 PSendSysMessage(LANG_WAYPOINT_NOTFOUND, lowguid);
3016 SetSentErrorMessage(true);
3017 return false;
3020 Field *fields = result->Fetch();
3021 float x = fields[0].GetFloat();
3022 float y = fields[1].GetFloat();
3023 float z = fields[2].GetFloat();
3024 uint32 id = VISUAL_WAYPOINT;
3026 Player *chr = m_session->GetPlayer();
3027 float o = chr->GetOrientation();
3028 Map *map = chr->GetMap();
3030 Creature* pCreature = new Creature;
3031 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT),map, chr->GetPhaseMaskForSpawn(), id, 0))
3033 PSendSysMessage(LANG_WAYPOINT_VP_NOTCREATED, id);
3034 delete pCreature;
3035 delete result;
3036 return false;
3039 pCreature->Relocate(x, y, z, o);
3041 if(!pCreature->IsPositionValid())
3043 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",pCreature->GetGUIDLow(),pCreature->GetEntry(),pCreature->GetPositionX(),pCreature->GetPositionY());
3044 delete pCreature;
3045 delete result;
3046 return false;
3049 pCreature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
3050 pCreature->LoadFromDB(pCreature->GetDBTableGUIDLow(), map);
3051 map->Add(pCreature);
3052 //player->PlayerTalkClass->SendPointOfInterest(x, y, 6, 6, 0, "First Waypoint");
3054 // Cleanup memory
3055 delete result;
3056 return true;
3059 if(show == "last")
3061 PSendSysMessage("DEBUG: wp last, GUID: %u", lowguid);
3063 QueryResult *result = WorldDatabase.PQuery( "SELECT MAX(point) FROM creature_movement WHERE id = '%u'",lowguid);
3064 if( result )
3066 Maxpoint = (*result)[0].GetUInt32();
3068 delete result;
3070 else
3071 Maxpoint = 0;
3073 result = WorldDatabase.PQuery( "SELECT position_x,position_y,position_z FROM creature_movement WHERE point ='%u' AND id = '%u'",Maxpoint, lowguid);
3074 if(!result)
3076 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDLAST, lowguid);
3077 SetSentErrorMessage(true);
3078 return false;
3080 Field *fields = result->Fetch();
3081 float x = fields[0].GetFloat();
3082 float y = fields[1].GetFloat();
3083 float z = fields[2].GetFloat();
3084 uint32 id = VISUAL_WAYPOINT;
3086 Player *chr = m_session->GetPlayer();
3087 float o = chr->GetOrientation();
3088 Map *map = chr->GetMap();
3090 Creature* pCreature = new Creature;
3091 if (!pCreature->Create(objmgr.GenerateLowGuid(HIGHGUID_UNIT), map, chr->GetPhaseMaskForSpawn(), id, 0))
3093 PSendSysMessage(LANG_WAYPOINT_NOTCREATED, id);
3094 delete pCreature;
3095 delete result;
3096 return false;
3099 pCreature->Relocate(x, y, z, o);
3101 if(!pCreature->IsPositionValid())
3103 sLog.outError("ERROR: Creature (guidlow %d, entry %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",pCreature->GetGUIDLow(),pCreature->GetEntry(),pCreature->GetPositionX(),pCreature->GetPositionY());
3104 delete pCreature;
3105 delete result;
3106 return false;
3109 pCreature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMaskForSpawn());
3110 pCreature->LoadFromDB(pCreature->GetDBTableGUIDLow(), map);
3111 map->Add(pCreature);
3112 //player->PlayerTalkClass->SendPointOfInterest(x, y, 6, 6, 0, "Last Waypoint");
3113 // Cleanup memory
3114 delete result;
3115 return true;
3118 if(show == "off")
3120 QueryResult *result = WorldDatabase.PQuery("SELECT guid FROM creature WHERE id = '%d'", VISUAL_WAYPOINT);
3121 if(!result)
3123 SendSysMessage(LANG_WAYPOINT_VP_NOTFOUND);
3124 SetSentErrorMessage(true);
3125 return false;
3127 bool hasError = false;
3130 Field *fields = result->Fetch();
3131 uint32 guid = fields[0].GetUInt32();
3132 Creature* pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(),MAKE_NEW_GUID(guid,VISUAL_WAYPOINT,HIGHGUID_UNIT));
3134 //Creature* pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), guid);
3136 if(!pCreature)
3138 PSendSysMessage(LANG_WAYPOINT_NOTREMOVED, guid);
3139 hasError = true;
3140 WorldDatabase.PExecuteLog("DELETE FROM creature WHERE guid = '%u'", guid);
3142 else
3144 pCreature->DeleteFromDB();
3145 pCreature->CleanupsBeforeDelete();
3146 pCreature->AddObjectToRemoveList();
3148 }while(result->NextRow());
3149 // set "wpguid" column to "empty" - no visual waypoint spawned
3150 WorldDatabase.PExecuteLog("UPDATE creature_movement SET wpguid = '0' WHERE wpguid <> '0'");
3152 if( hasError )
3154 PSendSysMessage(LANG_WAYPOINT_TOOFAR1);
3155 PSendSysMessage(LANG_WAYPOINT_TOOFAR2);
3156 PSendSysMessage(LANG_WAYPOINT_TOOFAR3);
3159 SendSysMessage(LANG_WAYPOINT_VP_ALLREMOVED);
3160 // Cleanup memory
3161 delete result;
3163 return true;
3166 PSendSysMessage("DEBUG: wpshow - no valid command found");
3168 return true;
3169 } // HandleWpShowCommand
3171 bool ChatHandler::HandleWpExportCommand(const char *args)
3173 if(!*args)
3174 return false;
3176 // Next arg is: <GUID> <ARGUMENT>
3178 // Did user provide a GUID
3179 // or did the user select a creature?
3180 // -> variable lowguid is filled with the GUID of the NPC
3181 uint32 lowguid = 0;
3182 Creature* target = getSelectedCreature();
3183 char* arg_str = NULL;
3184 if (target)
3186 if (target->GetEntry() != VISUAL_WAYPOINT)
3187 lowguid = target->GetGUIDLow();
3188 else
3190 QueryResult *result = WorldDatabase.PQuery( "SELECT id FROM creature_movement WHERE wpguid = %u LIMIT 1", target->GetGUIDLow() );
3191 if (!result)
3193 PSendSysMessage(LANG_WAYPOINT_NOTFOUNDDBPROBLEM, target->GetGUIDLow());
3194 return true;
3196 Field *fields = result->Fetch();
3197 lowguid = fields[0].GetUInt32();;
3198 delete result;
3201 arg_str = strtok((char*)args, " ");
3203 else
3205 // user provided <GUID>
3206 char* guid_str = strtok((char*)args, " ");
3207 if( !guid_str )
3209 SendSysMessage(LANG_WAYPOINT_NOGUID);
3210 return false;
3212 lowguid = atoi((char*)guid_str);
3214 arg_str = strtok((char*)NULL, " ");
3217 if( !arg_str)
3219 PSendSysMessage(LANG_WAYPOINT_ARGUMENTREQ, "export");
3220 return false;
3223 PSendSysMessage("DEBUG: wp export, GUID: %u", lowguid);
3225 QueryResult *result = WorldDatabase.PQuery(
3226 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
3227 "SELECT point, position_x, position_y, position_z, orientation, model1, model2, waittime, emote, spell, textid1, textid2, textid3, textid4, textid5, id FROM creature_movement WHERE id = '%u' ORDER BY point", lowguid );
3229 if (!result)
3231 PSendSysMessage(LANG_WAYPOINT_NOTHINGTOEXPORT);
3232 SetSentErrorMessage(true);
3233 return false;
3236 std::ofstream outfile;
3237 outfile.open (arg_str);
3241 Field *fields = result->Fetch();
3243 outfile << "INSERT INTO creature_movement ";
3244 outfile << "( id, point, position_x, position_y, position_z, orientation, model1, model2, waittime, emote, spell, textid1, textid2, textid3, textid4, textid5 ) VALUES ";
3246 outfile << "( ";
3247 outfile << fields[15].GetUInt32(); // id
3248 outfile << ", ";
3249 outfile << fields[0].GetUInt32(); // point
3250 outfile << ", ";
3251 outfile << fields[1].GetFloat(); // position_x
3252 outfile << ", ";
3253 outfile << fields[2].GetFloat(); // position_y
3254 outfile << ", ";
3255 outfile << fields[3].GetUInt32(); // position_z
3256 outfile << ", ";
3257 outfile << fields[4].GetUInt32(); // orientation
3258 outfile << ", ";
3259 outfile << fields[5].GetUInt32(); // model1
3260 outfile << ", ";
3261 outfile << fields[6].GetUInt32(); // model2
3262 outfile << ", ";
3263 outfile << fields[7].GetUInt16(); // waittime
3264 outfile << ", ";
3265 outfile << fields[8].GetUInt32(); // emote
3266 outfile << ", ";
3267 outfile << fields[9].GetUInt32(); // spell
3268 outfile << ", ";
3269 outfile << fields[10].GetUInt32(); // textid1
3270 outfile << ", ";
3271 outfile << fields[11].GetUInt32(); // textid2
3272 outfile << ", ";
3273 outfile << fields[12].GetUInt32(); // textid3
3274 outfile << ", ";
3275 outfile << fields[13].GetUInt32(); // textid4
3276 outfile << ", ";
3277 outfile << fields[14].GetUInt32(); // textid5
3278 outfile << ");\n ";
3280 } while( result->NextRow() );
3281 delete result;
3283 PSendSysMessage(LANG_WAYPOINT_EXPORTED);
3284 outfile.close();
3286 return true;
3289 bool ChatHandler::HandleWpImportCommand(const char *args)
3291 if(!*args)
3292 return false;
3294 char* arg_str = strtok((char*)args, " ");
3295 if (!arg_str)
3296 return false;
3298 std::string line;
3299 std::ifstream infile (arg_str);
3300 if (infile.is_open())
3302 while (! infile.eof() )
3304 getline (infile,line);
3305 //cout << line << endl;
3306 QueryResult *result = WorldDatabase.Query(line.c_str());
3307 delete result;
3309 infile.close();
3311 PSendSysMessage(LANG_WAYPOINT_IMPORTED);
3313 return true;
3316 //rename characters
3317 bool ChatHandler::HandleRenameCommand(const char* args)
3319 Player* target = NULL;
3320 uint64 targetGUID = 0;
3321 std::string oldname;
3323 char* px = strtok((char*)args, " ");
3325 if(px)
3327 oldname = extractPlayerNameFromLink(px);
3328 if(oldname.empty())
3330 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3331 SetSentErrorMessage(true);
3332 return false;
3335 target = objmgr.GetPlayer(oldname.c_str());
3337 if (!target)
3338 targetGUID = objmgr.GetPlayerGUIDByName(oldname);
3341 if(!target && !targetGUID)
3343 target = getSelectedPlayer();
3346 if(!target && !targetGUID)
3348 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3349 SetSentErrorMessage(true);
3350 return false;
3353 if(target)
3355 // check online security
3356 if (HasLowerSecurity(target, 0))
3357 return false;
3359 PSendSysMessage(LANG_RENAME_PLAYER, GetNameLink(target).c_str());
3360 target->SetAtLoginFlag(AT_LOGIN_RENAME);
3361 CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '1' WHERE guid = '%u'", target->GetGUIDLow());
3363 else
3365 // check offline security
3366 if (HasLowerSecurity(NULL, targetGUID))
3367 return false;
3369 std::string oldNameLink = playerLink(oldname);
3371 PSendSysMessage(LANG_RENAME_PLAYER_GUID, oldNameLink.c_str(), GUID_LOPART(targetGUID));
3372 CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '1' WHERE guid = '%u'", GUID_LOPART(targetGUID));
3375 return true;
3378 // customize characters
3379 bool ChatHandler::HandleCustomizeCommand(const char* args)
3381 Player* target = NULL;
3382 uint64 targetGUID = 0;
3383 std::string oldname;
3385 char* px = strtok((char*)args, " ");
3387 if(px)
3389 oldname = extractPlayerNameFromLink(px);
3390 if(oldname.empty())
3392 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3393 SetSentErrorMessage(true);
3394 return false;
3397 target = objmgr.GetPlayer(oldname.c_str());
3399 if (!target)
3400 targetGUID = objmgr.GetPlayerGUIDByName(oldname);
3403 if(!target && !targetGUID)
3405 target = getSelectedPlayer();
3408 if(!target && !targetGUID)
3410 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3411 SetSentErrorMessage(true);
3412 return false;
3415 if(target)
3417 PSendSysMessage(LANG_CUSTOMIZE_PLAYER, GetNameLink(target).c_str());
3418 target->SetAtLoginFlag(AT_LOGIN_CUSTOMIZE);
3419 CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '8' WHERE guid = '%u'", target->GetGUIDLow());
3421 else
3423 std::string oldNameLink = playerLink(oldname);
3425 PSendSysMessage(LANG_CUSTOMIZE_PLAYER_GUID, oldNameLink.c_str(), GUID_LOPART(targetGUID));
3426 CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '8' WHERE guid = '%u'", GUID_LOPART(targetGUID));
3429 return true;
3432 //spawn go
3433 bool ChatHandler::HandleGameObjectCommand(const char* args)
3435 if (!*args)
3436 return false;
3438 char* pParam1 = strtok((char*)args, " ");
3439 if (!pParam1)
3440 return false;
3442 uint32 id = atoi((char*)pParam1);
3443 if(!id)
3444 return false;
3446 char* spawntimeSecs = strtok(NULL, " ");
3448 const GameObjectInfo *goI = objmgr.GetGameObjectInfo(id);
3450 if (!goI)
3452 PSendSysMessage(LANG_GAMEOBJECT_NOT_EXIST,id);
3453 SetSentErrorMessage(true);
3454 return false;
3457 Player *chr = m_session->GetPlayer();
3458 float x = float(chr->GetPositionX());
3459 float y = float(chr->GetPositionY());
3460 float z = float(chr->GetPositionZ());
3461 float o = float(chr->GetOrientation());
3462 Map *map = chr->GetMap();
3464 float rot2 = sin(o/2);
3465 float rot3 = cos(o/2);
3467 GameObject* pGameObj = new GameObject;
3468 uint32 db_lowGUID = objmgr.GenerateLowGuid(HIGHGUID_GAMEOBJECT);
3470 if(!pGameObj->Create(db_lowGUID, goI->id, map, chr->GetPhaseMaskForSpawn(), x, y, z, o, 0, 0, rot2, rot3, 0, 1))
3472 delete pGameObj;
3473 return false;
3476 if( spawntimeSecs )
3478 uint32 value = atoi((char*)spawntimeSecs);
3479 pGameObj->SetRespawnTime(value);
3480 //sLog.outDebug("*** spawntimeSecs: %d", value);
3483 // fill the gameobject data and save to the db
3484 pGameObj->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()),chr->GetPhaseMaskForSpawn());
3486 // this will generate a new guid if the object is in an instance
3487 if(!pGameObj->LoadFromDB(db_lowGUID, map))
3489 delete pGameObj;
3490 return false;
3493 sLog.outDebug(GetMangosString(LANG_GAMEOBJECT_CURRENT), goI->name, db_lowGUID, x, y, z, o);
3495 map->Add(pGameObj);
3497 // TODO: is it really necessary to add both the real and DB table guid here ?
3498 objmgr.AddGameobjectToGrid(db_lowGUID, objmgr.GetGOData(db_lowGUID));
3500 PSendSysMessage(LANG_GAMEOBJECT_ADD,id,goI->name,db_lowGUID,x,y,z);
3501 return true;
3504 //show animation
3505 bool ChatHandler::HandleAnimCommand(const char* args)
3507 if (!*args)
3508 return false;
3510 uint32 anim_id = atoi((char*)args);
3511 m_session->GetPlayer()->HandleEmoteCommand(anim_id);
3512 return true;
3515 //change standstate
3516 bool ChatHandler::HandleStandStateCommand(const char* args)
3518 if (!*args)
3519 return false;
3521 uint32 anim_id = atoi((char*)args);
3522 m_session->GetPlayer( )->SetUInt32Value( UNIT_NPC_EMOTESTATE , anim_id );
3524 return true;
3527 bool ChatHandler::HandleAddHonorCommand(const char* args)
3529 if (!*args)
3530 return false;
3532 Player *target = getSelectedPlayer();
3533 if(!target)
3535 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3536 SetSentErrorMessage(true);
3537 return false;
3540 // check online security
3541 if (HasLowerSecurity(target, 0))
3542 return false;
3544 uint32 amount = (uint32)atoi(args);
3545 target->RewardHonor(NULL, 1, amount);
3546 return true;
3549 bool ChatHandler::HandleHonorAddKillCommand(const char* /*args*/)
3551 Unit *target = getSelectedUnit();
3552 if(!target)
3554 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3555 SetSentErrorMessage(true);
3556 return false;
3559 // check online security
3560 if (target->GetTypeId() == TYPEID_PLAYER && HasLowerSecurity((Player*)target, 0))
3561 return false;
3563 m_session->GetPlayer()->RewardHonor(target, 1);
3564 return true;
3567 bool ChatHandler::HandleUpdateHonorFieldsCommand(const char* /*args*/)
3569 Player *target = getSelectedPlayer();
3570 if(!target)
3572 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3573 SetSentErrorMessage(true);
3574 return false;
3577 // check online security
3578 if (HasLowerSecurity(target, 0))
3579 return false;
3581 target->UpdateHonorFields();
3582 return true;
3585 bool ChatHandler::HandleLookupEventCommand(const char* args)
3587 if(!*args)
3588 return false;
3590 std::string namepart = args;
3591 std::wstring wnamepart;
3593 // converting string that we try to find to lower case
3594 if(!Utf8toWStr(namepart,wnamepart))
3595 return false;
3597 wstrToLower(wnamepart);
3599 uint32 counter = 0;
3601 GameEvent::GameEventDataMap const& events = gameeventmgr.GetEventMap();
3602 GameEvent::ActiveEvents const& activeEvents = gameeventmgr.GetActiveEventList();
3604 for(uint32 id = 0; id < events.size(); ++id )
3606 GameEventData const& eventData = events[id];
3608 std::string descr = eventData.description;
3609 if(descr.empty())
3610 continue;
3612 if (Utf8FitTo(descr, wnamepart))
3614 char const* active = activeEvents.find(id) != activeEvents.end() ? GetMangosString(LANG_ACTIVE) : "";
3616 if(m_session)
3617 PSendSysMessage(LANG_EVENT_ENTRY_LIST_CHAT,id,id,eventData.description.c_str(),active );
3618 else
3619 PSendSysMessage(LANG_EVENT_ENTRY_LIST_CONSOLE,id,eventData.description.c_str(),active );
3621 ++counter;
3625 if (counter==0)
3626 SendSysMessage(LANG_NOEVENTFOUND);
3628 return true;
3631 bool ChatHandler::HandleEventActiveListCommand(const char* /*args*/)
3633 uint32 counter = 0;
3635 GameEvent::GameEventDataMap const& events = gameeventmgr.GetEventMap();
3636 GameEvent::ActiveEvents const& activeEvents = gameeventmgr.GetActiveEventList();
3638 char const* active = GetMangosString(LANG_ACTIVE);
3640 for(GameEvent::ActiveEvents::const_iterator itr = activeEvents.begin(); itr != activeEvents.end(); ++itr )
3642 uint32 event_id = *itr;
3643 GameEventData const& eventData = events[event_id];
3645 if(m_session)
3646 PSendSysMessage(LANG_EVENT_ENTRY_LIST_CHAT,event_id,event_id,eventData.description.c_str(),active );
3647 else
3648 PSendSysMessage(LANG_EVENT_ENTRY_LIST_CONSOLE,event_id,eventData.description.c_str(),active );
3650 ++counter;
3653 if (counter==0)
3654 SendSysMessage(LANG_NOEVENTFOUND);
3656 return true;
3659 bool ChatHandler::HandleEventInfoCommand(const char* args)
3661 if(!*args)
3662 return false;
3664 // id or [name] Shift-click form |color|Hgameevent:id|h[name]|h|r
3665 char* cId = extractKeyFromLink((char*)args,"Hgameevent");
3666 if(!cId)
3667 return false;
3669 uint32 event_id = atoi(cId);
3671 GameEvent::GameEventDataMap const& events = gameeventmgr.GetEventMap();
3673 if(event_id >=events.size())
3675 SendSysMessage(LANG_EVENT_NOT_EXIST);
3676 SetSentErrorMessage(true);
3677 return false;
3680 GameEventData const& eventData = events[event_id];
3681 if(!eventData.isValid())
3683 SendSysMessage(LANG_EVENT_NOT_EXIST);
3684 SetSentErrorMessage(true);
3685 return false;
3688 GameEvent::ActiveEvents const& activeEvents = gameeventmgr.GetActiveEventList();
3689 bool active = activeEvents.find(event_id) != activeEvents.end();
3690 char const* activeStr = active ? GetMangosString(LANG_ACTIVE) : "";
3692 std::string startTimeStr = TimeToTimestampStr(eventData.start);
3693 std::string endTimeStr = TimeToTimestampStr(eventData.end);
3695 uint32 delay = gameeventmgr.NextCheck(event_id);
3696 time_t nextTime = time(NULL)+delay;
3697 std::string nextStr = nextTime >= eventData.start && nextTime < eventData.end ? TimeToTimestampStr(time(NULL)+delay) : "-";
3699 std::string occurenceStr = secsToTimeString(eventData.occurence * MINUTE);
3700 std::string lengthStr = secsToTimeString(eventData.length * MINUTE);
3702 PSendSysMessage(LANG_EVENT_INFO,event_id,eventData.description.c_str(),activeStr,
3703 startTimeStr.c_str(),endTimeStr.c_str(),occurenceStr.c_str(),lengthStr.c_str(),
3704 nextStr.c_str());
3705 return true;
3708 bool ChatHandler::HandleEventStartCommand(const char* args)
3710 if(!*args)
3711 return false;
3713 // id or [name] Shift-click form |color|Hgameevent:id|h[name]|h|r
3714 char* cId = extractKeyFromLink((char*)args,"Hgameevent");
3715 if(!cId)
3716 return false;
3718 int32 event_id = atoi(cId);
3720 GameEvent::GameEventDataMap const& events = gameeventmgr.GetEventMap();
3722 if(event_id < 1 || event_id >=events.size())
3724 SendSysMessage(LANG_EVENT_NOT_EXIST);
3725 SetSentErrorMessage(true);
3726 return false;
3729 GameEventData const& eventData = events[event_id];
3730 if(!eventData.isValid())
3732 SendSysMessage(LANG_EVENT_NOT_EXIST);
3733 SetSentErrorMessage(true);
3734 return false;
3737 GameEvent::ActiveEvents const& activeEvents = gameeventmgr.GetActiveEventList();
3738 if(activeEvents.find(event_id) != activeEvents.end())
3740 PSendSysMessage(LANG_EVENT_ALREADY_ACTIVE,event_id);
3741 SetSentErrorMessage(true);
3742 return false;
3745 gameeventmgr.StartEvent(event_id,true);
3746 return true;
3749 bool ChatHandler::HandleEventStopCommand(const char* args)
3751 if(!*args)
3752 return false;
3754 // id or [name] Shift-click form |color|Hgameevent:id|h[name]|h|r
3755 char* cId = extractKeyFromLink((char*)args,"Hgameevent");
3756 if(!cId)
3757 return false;
3759 int32 event_id = atoi(cId);
3761 GameEvent::GameEventDataMap const& events = gameeventmgr.GetEventMap();
3763 if(event_id < 1 || event_id >=events.size())
3765 SendSysMessage(LANG_EVENT_NOT_EXIST);
3766 SetSentErrorMessage(true);
3767 return false;
3770 GameEventData const& eventData = events[event_id];
3771 if(!eventData.isValid())
3773 SendSysMessage(LANG_EVENT_NOT_EXIST);
3774 SetSentErrorMessage(true);
3775 return false;
3778 GameEvent::ActiveEvents const& activeEvents = gameeventmgr.GetActiveEventList();
3780 if(activeEvents.find(event_id) == activeEvents.end())
3782 PSendSysMessage(LANG_EVENT_NOT_ACTIVE,event_id);
3783 SetSentErrorMessage(true);
3784 return false;
3787 gameeventmgr.StopEvent(event_id,true);
3788 return true;
3791 bool ChatHandler::HandleCombatStopCommand(const char* args)
3793 Player *player;
3795 if(*args)
3797 std::string playername = extractPlayerNameFromLink((char*)args);
3798 if(playername.empty())
3800 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3801 SetSentErrorMessage(true);
3802 return false;
3805 player = objmgr.GetPlayer(playername.c_str());
3807 if(!player)
3809 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3810 SetSentErrorMessage(true);
3811 return false;
3814 else
3816 player = getSelectedPlayer();
3818 if (!player)
3819 player = m_session->GetPlayer();
3822 // check online security
3823 if (HasLowerSecurity(player, 0))
3824 return false;
3826 player->CombatStop();
3827 player->getHostilRefManager().deleteReferences();
3828 return true;
3831 bool ChatHandler::HandleLearnAllCraftsCommand(const char* /*args*/)
3833 uint32 classmask = m_session->GetPlayer()->getClassMask();
3835 for (uint32 i = 0; i < sSkillLineStore.GetNumRows(); ++i)
3837 SkillLineEntry const *skillInfo = sSkillLineStore.LookupEntry(i);
3838 if( !skillInfo )
3839 continue;
3841 if( skillInfo->categoryId == SKILL_CATEGORY_PROFESSION || skillInfo->categoryId == SKILL_CATEGORY_SECONDARY )
3843 for (uint32 j = 0; j < sSkillLineAbilityStore.GetNumRows(); ++j)
3845 SkillLineAbilityEntry const *skillLine = sSkillLineAbilityStore.LookupEntry(j);
3846 if( !skillLine )
3847 continue;
3849 // skip racial skills
3850 if( skillLine->racemask != 0 )
3851 continue;
3853 // skip wrong class skills
3854 if( skillLine->classmask && (skillLine->classmask & classmask) == 0)
3855 continue;
3857 if( skillLine->skillId != i || skillLine->forward_spellid )
3858 continue;
3860 SpellEntry const* spellInfo = sSpellStore.LookupEntry(skillLine->spellId);
3861 if(!spellInfo || !SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer(),false))
3862 continue;
3864 m_session->GetPlayer()->learnSpell(skillLine->spellId,false);
3869 SendSysMessage(LANG_COMMAND_LEARN_ALL_CRAFT);
3870 return true;
3873 bool ChatHandler::HandleLearnAllRecipesCommand(const char* args)
3875 // Learns all recipes of specified profession and sets skill to max
3876 // Example: .learn all_recipes enchanting
3878 Player* target = getSelectedPlayer();
3879 if( !target )
3881 SendSysMessage(LANG_PLAYER_NOT_FOUND);
3882 return false;
3885 if(!*args)
3886 return false;
3888 std::wstring wnamepart;
3890 if(!Utf8toWStr(args,wnamepart))
3891 return false;
3893 // converting string that we try to find to lower case
3894 wstrToLower( wnamepart );
3896 uint32 classmask = m_session->GetPlayer()->getClassMask();
3898 for (uint32 i = 0; i < sSkillLineStore.GetNumRows(); ++i)
3900 SkillLineEntry const *skillInfo = sSkillLineStore.LookupEntry(i);
3901 if( !skillInfo )
3902 continue;
3904 if( skillInfo->categoryId != SKILL_CATEGORY_PROFESSION &&
3905 skillInfo->categoryId != SKILL_CATEGORY_SECONDARY )
3906 continue;
3908 int loc = m_session->GetSessionDbcLocale();
3909 std::string name = skillInfo->name[loc];
3911 if(Utf8FitTo(name, wnamepart))
3913 for (uint32 j = 0; j < sSkillLineAbilityStore.GetNumRows(); ++j)
3915 SkillLineAbilityEntry const *skillLine = sSkillLineAbilityStore.LookupEntry(j);
3916 if( !skillLine )
3917 continue;
3919 if( skillLine->skillId != i || skillLine->forward_spellid )
3920 continue;
3922 // skip racial skills
3923 if( skillLine->racemask != 0 )
3924 continue;
3926 // skip wrong class skills
3927 if( skillLine->classmask && (skillLine->classmask & classmask) == 0)
3928 continue;
3930 SpellEntry const* spellInfo = sSpellStore.LookupEntry(skillLine->spellId);
3931 if(!spellInfo || !SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer(),false))
3932 continue;
3934 if( !target->HasSpell(spellInfo->Id) )
3935 m_session->GetPlayer()->learnSpell(skillLine->spellId,false);
3938 uint16 maxLevel = target->GetPureMaxSkillValue(skillInfo->id);
3939 target->SetSkill(skillInfo->id, maxLevel, maxLevel);
3940 PSendSysMessage(LANG_COMMAND_LEARN_ALL_RECIPES, name.c_str());
3941 return true;
3945 return false;
3948 bool ChatHandler::HandleLookupPlayerIpCommand(const char* args)
3951 if (!*args)
3952 return false;
3954 std::string ip = strtok ((char*)args, " ");
3955 char* limit_str = strtok (NULL, " ");
3956 int32 limit = limit_str ? atoi (limit_str) : -1;
3958 loginDatabase.escape_string (ip);
3960 QueryResult* result = loginDatabase.PQuery ("SELECT id,username FROM account WHERE last_ip = '%s'", ip.c_str ());
3962 return LookupPlayerSearchCommand (result,limit);
3965 bool ChatHandler::HandleLookupPlayerAccountCommand(const char* args)
3967 if (!*args)
3968 return false;
3970 std::string account = strtok ((char*)args, " ");
3971 char* limit_str = strtok (NULL, " ");
3972 int32 limit = limit_str ? atoi (limit_str) : -1;
3974 if (!AccountMgr::normilizeString (account))
3975 return false;
3977 loginDatabase.escape_string (account);
3979 QueryResult* result = loginDatabase.PQuery ("SELECT id,username FROM account WHERE username = '%s'", account.c_str ());
3981 return LookupPlayerSearchCommand (result,limit);
3984 bool ChatHandler::HandleLookupPlayerEmailCommand(const char* args)
3987 if (!*args)
3988 return false;
3990 std::string email = strtok ((char*)args, " ");
3991 char* limit_str = strtok (NULL, " ");
3992 int32 limit = limit_str ? atoi (limit_str) : -1;
3994 loginDatabase.escape_string (email);
3996 QueryResult* result = loginDatabase.PQuery ("SELECT id,username FROM account WHERE email = '%s'", email.c_str ());
3998 return LookupPlayerSearchCommand (result,limit);
4001 bool ChatHandler::LookupPlayerSearchCommand(QueryResult* result, int32 limit)
4003 if(!result)
4005 PSendSysMessage(LANG_NO_PLAYERS_FOUND);
4006 SetSentErrorMessage(true);
4007 return false;
4010 int i =0;
4013 Field* fields = result->Fetch();
4014 uint32 acc_id = fields[0].GetUInt32();
4015 std::string acc_name = fields[1].GetCppString();
4017 QueryResult* chars = CharacterDatabase.PQuery("SELECT guid,name FROM characters WHERE account = '%u'", acc_id);
4018 if(chars)
4020 PSendSysMessage(LANG_LOOKUP_PLAYER_ACCOUNT,acc_name.c_str(),acc_id);
4022 uint64 guid = 0;
4023 std::string name;
4027 Field* charfields = chars->Fetch();
4028 guid = charfields[0].GetUInt64();
4029 name = charfields[1].GetCppString();
4031 PSendSysMessage(LANG_LOOKUP_PLAYER_CHARACTER,name.c_str(),guid);
4032 ++i;
4034 } while( chars->NextRow() && ( limit == -1 || i < limit ) );
4036 delete chars;
4038 } while(result->NextRow());
4040 delete result;
4042 return true;
4045 /// Triggering corpses expire check in world
4046 bool ChatHandler::HandleServerCorpsesCommand(const char* /*args*/)
4048 CorpsesErase();
4049 return true;
4052 bool ChatHandler::HandleRepairitemsCommand(const char* /*args*/)
4054 Player *target = getSelectedPlayer();
4056 if(!target)
4058 PSendSysMessage(LANG_NO_CHAR_SELECTED);
4059 SetSentErrorMessage(true);
4060 return false;
4063 // check online security
4064 if (HasLowerSecurity(target, 0))
4065 return false;
4067 // Repair items
4068 target->DurabilityRepairAll(false, 0, false);
4070 PSendSysMessage(LANG_YOU_REPAIR_ITEMS, GetNameLink(target).c_str());
4071 if(needReportToTarget(target))
4072 ChatHandler(target).PSendSysMessage(LANG_YOUR_ITEMS_REPAIRED, GetNameLink().c_str());
4073 return true;
4076 bool ChatHandler::HandleWaterwalkCommand(const char* args)
4078 if(!*args)
4079 return false;
4081 Player *player = getSelectedPlayer();
4083 if(!player)
4085 PSendSysMessage(LANG_NO_CHAR_SELECTED);
4086 SetSentErrorMessage(true);
4087 return false;
4090 // check online security
4091 if (HasLowerSecurity(player, 0))
4092 return false;
4094 if (strncmp(args, "on", 3) == 0)
4095 player->SetMovement(MOVE_WATER_WALK); // ON
4096 else if (strncmp(args, "off", 4) == 0)
4097 player->SetMovement(MOVE_LAND_WALK); // OFF
4098 else
4100 SendSysMessage(LANG_USE_BOL);
4101 return false;
4104 PSendSysMessage(LANG_YOU_SET_WATERWALK, args, GetNameLink(player).c_str());
4105 if(needReportToTarget(player))
4106 ChatHandler(player).PSendSysMessage(LANG_YOUR_WATERWALK_SET, args, GetNameLink().c_str());
4107 return true;
4110 bool ChatHandler::HandleNpcFollowCommand(const char* /*args*/)
4112 Player *player = m_session->GetPlayer();
4113 Creature *creature = getSelectedCreature();
4115 if(!creature)
4117 PSendSysMessage(LANG_SELECT_CREATURE);
4118 SetSentErrorMessage(true);
4119 return false;
4122 // Follow player - Using pet's default dist and angle
4123 creature->GetMotionMaster()->MoveFollow(player, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
4125 PSendSysMessage(LANG_CREATURE_FOLLOW_YOU_NOW, creature->GetName());
4126 return true;
4129 bool ChatHandler::HandleNpcUnFollowCommand(const char* /*args*/)
4131 Player *player = m_session->GetPlayer();
4132 Creature *creature = getSelectedCreature();
4134 if(!creature)
4136 PSendSysMessage(LANG_SELECT_CREATURE);
4137 SetSentErrorMessage(true);
4138 return false;
4141 if (creature->GetMotionMaster()->empty() ||
4142 creature->GetMotionMaster()->GetCurrentMovementGeneratorType ()!=TARGETED_MOTION_TYPE)
4144 PSendSysMessage(LANG_CREATURE_NOT_FOLLOW_YOU);
4145 SetSentErrorMessage(true);
4146 return false;
4149 TargetedMovementGenerator<Creature> const* mgen
4150 = static_cast<TargetedMovementGenerator<Creature> const*>((creature->GetMotionMaster()->top()));
4152 if(mgen->GetTarget()!=player)
4154 PSendSysMessage(LANG_CREATURE_NOT_FOLLOW_YOU);
4155 SetSentErrorMessage(true);
4156 return false;
4159 // reset movement
4160 creature->GetMotionMaster()->MovementExpired(true);
4162 PSendSysMessage(LANG_CREATURE_NOT_FOLLOW_YOU_NOW, creature->GetName());
4163 return true;
4166 bool ChatHandler::HandleNpcTameCommand(const char* /*args*/)
4168 Creature *creatureTarget = getSelectedCreature ();
4169 if (!creatureTarget || creatureTarget->isPet ())
4171 PSendSysMessage (LANG_SELECT_CREATURE);
4172 SetSentErrorMessage (true);
4173 return false;
4176 Player *player = m_session->GetPlayer ();
4178 if(player->GetPetGUID ())
4180 SendSysMessage (LANG_YOU_ALREADY_HAVE_PET);
4181 SetSentErrorMessage (true);
4182 return false;
4185 CreatureInfo const* cInfo = creatureTarget->GetCreatureInfo();
4187 if (!cInfo->isTameable ())
4189 PSendSysMessage (LANG_CREATURE_NON_TAMEABLE,cInfo->Entry);
4190 SetSentErrorMessage (true);
4191 return false;
4194 // Everything looks OK, create new pet
4195 Pet* pet = player->CreateTamedPetFrom (creatureTarget);
4196 if (!pet)
4198 PSendSysMessage (LANG_CREATURE_NON_TAMEABLE,cInfo->Entry);
4199 SetSentErrorMessage (true);
4200 return false;
4203 // place pet before player
4204 float x,y,z;
4205 player->GetClosePoint (x,y,z,creatureTarget->GetObjectSize (),CONTACT_DISTANCE);
4206 pet->Relocate (x,y,z,M_PI-player->GetOrientation ());
4208 // set pet to defensive mode by default (some classes can't control controlled pets in fact).
4209 pet->GetCharmInfo()->SetReactState(REACT_DEFENSIVE);
4211 // calculate proper level
4212 uint32 level = (creatureTarget->getLevel() < (player->getLevel() - 5)) ? (player->getLevel() - 5) : creatureTarget->getLevel();
4214 // prepare visual effect for levelup
4215 pet->SetUInt32Value(UNIT_FIELD_LEVEL, level - 1);
4217 // add to world
4218 pet->GetMap()->Add((Creature*)pet);
4220 // visual effect for levelup
4221 pet->SetUInt32Value(UNIT_FIELD_LEVEL, level);
4223 // caster have pet now
4224 player->SetPet(pet);
4226 pet->SavePetToDB(PET_SAVE_AS_CURRENT);
4227 player->PetSpellInitialize();
4229 return true;
4232 //change phasemask of creature or pet
4233 bool ChatHandler::HandleNpcSetPhaseCommand(const char* args)
4235 if (!*args)
4236 return false;
4238 uint32 phasemask = (uint32) atoi((char*)args);
4239 if ( phasemask == 0 )
4241 SendSysMessage(LANG_BAD_VALUE);
4242 SetSentErrorMessage(true);
4243 return false;
4246 Creature* pCreature = getSelectedCreature();
4247 if(!pCreature)
4249 SendSysMessage(LANG_SELECT_CREATURE);
4250 SetSentErrorMessage(true);
4251 return false;
4254 pCreature->SetPhaseMask(phasemask,true);
4256 if(!pCreature->isPet())
4257 pCreature->SaveToDB();
4259 return true;
4262 //set pahsemask for selected object
4263 bool ChatHandler::HandleGOPhaseCommand(const char* args)
4265 // number or [name] Shift-click form |color|Hgameobject:go_id|h[name]|h|r
4266 char* cId = extractKeyFromLink((char*)args,"Hgameobject");
4267 if(!cId)
4268 return false;
4270 uint32 lowguid = atoi(cId);
4271 if(!lowguid)
4272 return false;
4274 GameObject* obj = NULL;
4276 // by DB guid
4277 if (GameObjectData const* go_data = objmgr.GetGOData(lowguid))
4278 obj = GetObjectGlobalyWithGuidOrNearWithDbGuid(lowguid,go_data->id);
4280 if(!obj)
4282 PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, lowguid);
4283 SetSentErrorMessage(true);
4284 return false;
4287 char* phaseStr = strtok (NULL, " ");
4288 uint32 phasemask = phaseStr? atoi(phaseStr) : 0;
4289 if ( phasemask == 0 )
4291 SendSysMessage(LANG_BAD_VALUE);
4292 SetSentErrorMessage(true);
4293 return false;
4296 obj->SetPhaseMask(phasemask,true);
4297 obj->SaveToDB();
4298 return true;
4301 bool ChatHandler::HandleNpcSetDeathStateCommand(const char* args)
4303 if (!*args)
4304 return false;
4306 Creature* pCreature = getSelectedCreature();
4307 if(!pCreature || pCreature->isPet())
4309 SendSysMessage(LANG_SELECT_CREATURE);
4310 SetSentErrorMessage(true);
4311 return false;
4314 if (strncmp(args, "on", 3) == 0)
4315 pCreature->SetDeadByDefault(true);
4316 else if (strncmp(args, "off", 4) == 0)
4317 pCreature->SetDeadByDefault(false);
4318 else
4320 SendSysMessage(LANG_USE_BOL);
4321 SetSentErrorMessage(true);
4322 return false;
4325 pCreature->SaveToDB();
4326 pCreature->Respawn();
4328 return true;