[7560] Implement IconName for gameobject
[getmangos.git] / src / game / QueryHandler.cpp
blob4e31bfa4272bd1c6a7b9ed770bccc94ea1adb0f8
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 "Language.h"
21 #include "Database/DatabaseEnv.h"
22 #include "Database/DatabaseImpl.h"
23 #include "WorldPacket.h"
24 #include "WorldSession.h"
25 #include "Opcodes.h"
26 #include "Log.h"
27 #include "World.h"
28 #include "ObjectMgr.h"
29 #include "Player.h"
30 #include "UpdateMask.h"
31 #include "NPCHandler.h"
32 #include "Pet.h"
33 #include "MapManager.h"
35 void WorldSession::SendNameQueryOpcode(Player *p)
37 if(!p)
38 return;
40 // guess size
41 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+4+4+4+10) );
42 data << p->GetGUID();
43 data << p->GetName();
44 data << uint8(0); // realm name for cross realm BG usage
45 data << uint32(p->getRace());
46 data << uint32(p->getGender());
47 data << uint32(p->getClass());
48 if(DeclinedName const* names = p->GetDeclinedNames())
50 data << uint8(1); // is declined
51 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
52 data << names->name[i];
54 else
55 data << uint8(0); // is not declined
57 SendPacket(&data);
60 void WorldSession::SendNameQueryOpcodeFromDB(uint64 guid)
62 CharacterDatabase.AsyncPQuery(&WorldSession::SendNameQueryOpcodeFromDBCallBack, GetAccountId(),
63 !sWorld.getConfig(CONFIG_DECLINED_NAMES_USED) ?
64 // ------- Query Without Declined Names --------
65 // 0 1 2
66 "SELECT guid, name, SUBSTRING(data, LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))+2, LENGTH(SUBSTRING_INDEX(data, ' ', '%u')) - LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))-1) "
67 "FROM characters WHERE guid = '%u'"
69 // --------- Query With Declined Names ---------
70 // 0 1 2
71 "SELECT characters.guid, name, SUBSTRING(data, LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))+2, LENGTH(SUBSTRING_INDEX(data, ' ', '%u')) - LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))-1), "
72 // 3 4 5 6 7
73 "genitive, dative, accusative, instrumental, prepositional "
74 "FROM characters LEFT JOIN character_declinedname ON characters.guid = character_declinedname.guid WHERE characters.guid = '%u'",
75 UNIT_FIELD_BYTES_0, UNIT_FIELD_BYTES_0+1, UNIT_FIELD_BYTES_0, GUID_LOPART(guid));
78 void WorldSession::SendNameQueryOpcodeFromDBCallBack(QueryResult *result, uint32 accountId)
80 if(!result)
81 return;
83 WorldSession * session = sWorld.FindSession(accountId);
84 if(!session)
86 delete result;
87 return;
90 Field *fields = result->Fetch();
91 uint32 guid = fields[0].GetUInt32();
92 std::string name = fields[1].GetCppString();
93 uint32 field = 0;
94 if(name == "")
95 name = session->GetMangosString(LANG_NON_EXIST_CHARACTER);
96 else
97 field = fields[2].GetUInt32();
99 // guess size
100 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+4+4+4+10) );
101 data << MAKE_NEW_GUID(guid, 0, HIGHGUID_PLAYER);
102 data << name;
103 data << uint8(0);
104 data << uint32(field & 0xFF);
105 data << uint32((field >> 16) & 0xFF);
106 data << uint32((field >> 8) & 0xFF);
108 // if the first declined name field (3) is empty, the rest must be too
109 if(sWorld.getConfig(CONFIG_DECLINED_NAMES_USED) && fields[3].GetCppString() != "")
111 data << uint8(1); // is declined
112 for(int i = 3; i < MAX_DECLINED_NAME_CASES+3; ++i)
113 data << fields[i].GetCppString();
115 else
116 data << uint8(0); // is declined
118 session->SendPacket( &data );
119 delete result;
122 void WorldSession::HandleNameQueryOpcode( WorldPacket & recv_data )
124 CHECK_PACKET_SIZE(recv_data, 8);
126 uint64 guid;
128 recv_data >> guid;
130 Player *pChar = objmgr.GetPlayer(guid);
132 if (pChar)
133 SendNameQueryOpcode(pChar);
134 else
135 SendNameQueryOpcodeFromDB(guid);
138 void WorldSession::HandleQueryTimeOpcode( WorldPacket & /*recv_data*/ )
140 WorldPacket data( SMSG_QUERY_TIME_RESPONSE, 4+4 );
141 data << (uint32)time(NULL);
142 data << (uint32)0;
143 SendPacket( &data );
146 /// Only _static_ data send in this packet !!!
147 void WorldSession::HandleCreatureQueryOpcode( WorldPacket & recv_data )
149 CHECK_PACKET_SIZE(recv_data,4+8);
151 uint32 entry;
152 recv_data >> entry;
154 CreatureInfo const *ci = objmgr.GetCreatureTemplate(entry);
155 if (ci)
158 std::string Name, SubName;
159 Name = ci->Name;
160 SubName = ci->SubName;
162 int loc_idx = GetSessionDbLocaleIndex();
163 if (loc_idx >= 0)
165 CreatureLocale const *cl = objmgr.GetCreatureLocale(entry);
166 if (cl)
168 if (cl->Name.size() > size_t(loc_idx) && !cl->Name[loc_idx].empty())
169 Name = cl->Name[loc_idx];
170 if (cl->SubName.size() > size_t(loc_idx) && !cl->SubName[loc_idx].empty())
171 SubName = cl->SubName[loc_idx];
174 sLog.outDetail("WORLD: CMSG_CREATURE_QUERY '%s' - Entry: %u.", ci->Name, entry);
175 // guess size
176 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 100 );
177 data << uint32(entry); // creature entry
178 data << Name;
179 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4, always empty
180 data << SubName;
181 data << ci->IconName; // "Directions" for guard, string for Icons 2.3.0
182 data << uint32(ci->type_flags); // flags wdbFeild7=wad flags1
183 data << uint32(ci->type);
184 data << uint32(ci->family); // family wdbFeild9
185 data << uint32(ci->rank); // rank wdbFeild10
186 data << uint32(ci->PetSpellDataId); // Id from CreatureSpellData.dbc wdbField12
187 data << uint32(ci->DisplayID_A); // modelid_male1
188 data << uint32(ci->DisplayID_H); // modelid_female1 ?
189 data << uint32(ci->DisplayID_A2); // modelid_male2 ?
190 data << uint32(ci->DisplayID_H2); // modelid_femmale2 ?
191 data << float(ci->unk16); // unk
192 data << float(ci->unk17); // unk
193 data << uint8(ci->RacialLeader);
194 SendPacket( &data );
195 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE " );
197 else
199 uint64 guid;
200 recv_data >> guid;
202 sLog.outDebug("WORLD: CMSG_CREATURE_QUERY - NO CREATURE INFO! (GUID: %u, ENTRY: %u)",
203 GUID_LOPART(guid), entry);
204 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 4 );
205 data << uint32(entry | 0x80000000);
206 SendPacket( &data );
207 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE " );
211 /// Only _static_ data send in this packet !!!
212 void WorldSession::HandleGameObjectQueryOpcode( WorldPacket & recv_data )
214 CHECK_PACKET_SIZE(recv_data,4+8);
216 uint32 entryID;
217 recv_data >> entryID;
219 const GameObjectInfo *info = objmgr.GetGameObjectInfo(entryID);
220 if(info)
223 std::string Name;
224 std::string IconName;
225 std::string CastBarCaption;
227 Name = info->name;
228 IconName = info->IconName;
229 CastBarCaption = info->castBarCaption;
231 int loc_idx = GetSessionDbLocaleIndex();
232 if (loc_idx >= 0)
234 GameObjectLocale const *gl = objmgr.GetGameObjectLocale(entryID);
235 if (gl)
237 if (gl->Name.size() > size_t(loc_idx) && !gl->Name[loc_idx].empty())
238 Name = gl->Name[loc_idx];
239 if (gl->CastBarCaption.size() > size_t(loc_idx) && !gl->CastBarCaption[loc_idx].empty())
240 CastBarCaption = gl->CastBarCaption[loc_idx];
243 sLog.outDetail("WORLD: CMSG_GAMEOBJECT_QUERY '%s' - Entry: %u. ", info->name, entryID);
244 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 150 );
245 data << uint32(entryID);
246 data << uint32(info->type);
247 data << uint32(info->displayId);
248 data << Name;
249 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4
250 data << IconName; // 2.0.3, string. Icon name to use instead of default icon for go's (ex: "Attack" makes sword)
251 data << CastBarCaption; // 2.0.3, string. Text will appear in Cast Bar when using GO (ex: "Collecting")
252 data << uint8(0); // 2.0.3, string
253 data.append(info->raw.data, 24);
254 data << float(info->size); // go size
255 SendPacket( &data );
256 sLog.outDebug( "WORLD: Sent CMSG_GAMEOBJECT_QUERY " );
258 else
261 uint64 guid;
262 recv_data >> guid;
264 sLog.outDebug( "WORLD: CMSG_GAMEOBJECT_QUERY - Missing gameobject info for (GUID: %u, ENTRY: %u)",
265 GUID_LOPART(guid), entryID );
266 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 4 );
267 data << uint32(entryID | 0x80000000);
268 SendPacket( &data );
269 sLog.outDebug( "WORLD: Sent CMSG_GAMEOBJECT_QUERY " );
273 void WorldSession::HandleCorpseQueryOpcode(WorldPacket & /*recv_data*/)
275 sLog.outDetail("WORLD: Received MSG_CORPSE_QUERY");
277 Corpse *corpse = GetPlayer()->GetCorpse();
279 if(!corpse)
281 WorldPacket data(MSG_CORPSE_QUERY, 1);
282 data << uint8(0); // corpse not found
283 SendPacket(&data);
284 return;
287 int32 mapid = corpse->GetMapId();
288 float x = corpse->GetPositionX();
289 float y = corpse->GetPositionY();
290 float z = corpse->GetPositionZ();
291 int32 corpsemapid = _player->GetMapId();
293 if(Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId()))
295 if(map->IsDungeon())
297 if(!map->GetEntrancePos(mapid, x, y))
298 return;
300 Map *entrance_map = MapManager::Instance().GetMap(mapid, _player);
301 if(!entrance_map)
302 return;
304 z = entrance_map->GetHeight(x, y, MAX_HEIGHT);
305 corpsemapid = corpse->GetMapId();
309 WorldPacket data(MSG_CORPSE_QUERY, 1+(5*4));
310 data << uint8(1); // corpse found
311 data << int32(mapid);
312 data << float(x);
313 data << float(y);
314 data << float(z);
315 data << int32(corpsemapid);
316 SendPacket(&data);
319 void WorldSession::HandleNpcTextQueryOpcode( WorldPacket & recv_data )
321 CHECK_PACKET_SIZE(recv_data,4+8);
323 uint32 textID;
324 uint64 guid;
326 recv_data >> textID;
327 sLog.outDetail("WORLD: CMSG_NPC_TEXT_QUERY ID '%u'", textID);
329 recv_data >> guid;
330 GetPlayer()->SetUInt64Value(UNIT_FIELD_TARGET, guid);
332 GossipText const* pGossip = objmgr.GetGossipText(textID);
334 WorldPacket data( SMSG_NPC_TEXT_UPDATE, 100 ); // guess size
335 data << textID;
337 if (!pGossip)
339 for(uint32 i = 0; i < 8; ++i)
341 data << float(0);
342 data << "Greetings $N";
343 data << "Greetings $N";
344 data << uint32(0);
345 data << uint32(0);
346 data << uint32(0);
347 data << uint32(0);
348 data << uint32(0);
349 data << uint32(0);
350 data << uint32(0);
353 else
355 std::string Text_0[8], Text_1[8];
356 for (int i=0;i<8;i++)
358 Text_0[i]=pGossip->Options[i].Text_0;
359 Text_1[i]=pGossip->Options[i].Text_1;
362 int loc_idx = GetSessionDbLocaleIndex();
363 if (loc_idx >= 0)
365 NpcTextLocale const *nl = objmgr.GetNpcTextLocale(textID);
366 if (nl)
368 for (int i=0;i<8;i++)
370 if (nl->Text_0[i].size() > size_t(loc_idx) && !nl->Text_0[i][loc_idx].empty())
371 Text_0[i]=nl->Text_0[i][loc_idx];
372 if (nl->Text_1[i].size() > size_t(loc_idx) && !nl->Text_1[i][loc_idx].empty())
373 Text_1[i]=nl->Text_1[i][loc_idx];
378 for (int i=0; i<8; i++)
380 data << pGossip->Options[i].Probability;
382 if ( Text_0[i].empty() )
383 data << Text_1[i];
384 else
385 data << Text_0[i];
387 if ( Text_1[i].empty() )
388 data << Text_0[i];
389 else
390 data << Text_1[i];
392 data << pGossip->Options[i].Language;
394 for(int j = 0; j < 3; ++j)
396 data << pGossip->Options[i].Emotes[j]._Delay;
397 data << pGossip->Options[i].Emotes[j]._Emote;
402 SendPacket( &data );
404 sLog.outDebug( "WORLD: Sent SMSG_NPC_TEXT_UPDATE " );
407 void WorldSession::HandlePageQueryOpcode( WorldPacket & recv_data )
409 CHECK_PACKET_SIZE(recv_data,4);
411 uint32 pageID;
413 recv_data >> pageID;
414 sLog.outDetail("WORLD: Received CMSG_PAGE_TEXT_QUERY for pageID '%u'", pageID);
416 while (pageID)
418 PageText const *pPage = sPageTextStore.LookupEntry<PageText>( pageID );
419 // guess size
420 WorldPacket data( SMSG_PAGE_TEXT_QUERY_RESPONSE, 50 );
421 data << pageID;
423 if (!pPage)
425 data << "Item page missing.";
426 data << uint32(0);
427 pageID = 0;
429 else
431 std::string Text = pPage->Text;
433 int loc_idx = GetSessionDbLocaleIndex();
434 if (loc_idx >= 0)
436 PageTextLocale const *pl = objmgr.GetPageTextLocale(pageID);
437 if (pl)
439 if (pl->Text.size() > size_t(loc_idx) && !pl->Text[loc_idx].empty())
440 Text = pl->Text[loc_idx];
444 data << Text;
445 data << uint32(pPage->Next_Page);
446 pageID = pPage->Next_Page;
448 SendPacket( &data );
450 sLog.outDebug( "WORLD: Sent SMSG_PAGE_TEXT_QUERY_RESPONSE " );