[2] | 1 | /* |
---|
| 2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
| 3 | * |
---|
| 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. |
---|
| 8 | * |
---|
| 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. |
---|
| 13 | * |
---|
| 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 |
---|
| 17 | */ |
---|
| 18 | |
---|
| 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 "SpellMgr.h" |
---|
| 26 | #include "Creature.h" |
---|
| 27 | #include "QuestDef.h" |
---|
| 28 | #include "GossipDef.h" |
---|
| 29 | #include "Player.h" |
---|
| 30 | #include "Opcodes.h" |
---|
| 31 | #include "Log.h" |
---|
| 32 | #include "LootMgr.h" |
---|
| 33 | #include "MapManager.h" |
---|
| 34 | #include "CreatureAI.h" |
---|
| 35 | #include "CreatureAISelector.h" |
---|
| 36 | #include "Formulas.h" |
---|
| 37 | #include "SpellAuras.h" |
---|
| 38 | #include "WaypointMovementGenerator.h" |
---|
| 39 | #include "InstanceData.h" |
---|
| 40 | #include "BattleGround.h" |
---|
| 41 | #include "Util.h" |
---|
| 42 | #include "GridNotifiers.h" |
---|
| 43 | #include "GridNotifiersImpl.h" |
---|
| 44 | #include "CellImpl.h" |
---|
| 45 | |
---|
| 46 | // apply implementation of the singletons |
---|
| 47 | #include "Policies/SingletonImp.h" |
---|
| 48 | |
---|
| 49 | Creature::Creature() : |
---|
| 50 | Unit(), i_AI(NULL), |
---|
| 51 | lootForPickPocketed(false), lootForBody(false), m_groupLootTimer(0), lootingGroupLeaderGUID(0), |
---|
| 52 | m_itemsLoaded(false), m_trainerSpellsLoaded(false), m_trainer_type(0), m_lootMoney(0), m_lootRecipient(0), |
---|
| 53 | m_deathTimer(0), m_respawnTime(0), m_respawnDelay(25), m_corpseDelay(60), m_respawnradius(0.0f), |
---|
| 54 | m_gossipOptionLoaded(false),m_NPCTextId(0), m_emoteState(0), m_isPet(false), m_isTotem(false), |
---|
| 55 | m_regenTimer(2000), m_defaultMovementType(IDLE_MOTION_TYPE), m_equipmentId(0), |
---|
| 56 | m_AlreadyCallAssistence(false), m_regenHealth(true), m_AI_locked(false), m_isDeadByDefault(false), |
---|
| 57 | m_meleeDamageSchoolMask(SPELL_SCHOOL_MASK_NORMAL),m_creatureInfo(NULL) |
---|
| 58 | { |
---|
| 59 | m_valuesCount = UNIT_END; |
---|
| 60 | |
---|
| 61 | for(int i =0; i<4; ++i) |
---|
| 62 | m_spells[i] = 0; |
---|
| 63 | |
---|
| 64 | m_CreatureSpellCooldowns.clear(); |
---|
| 65 | m_CreatureCategoryCooldowns.clear(); |
---|
| 66 | m_GlobalCooldown = 0; |
---|
| 67 | m_unit_movement_flags = MOVEMENTFLAG_WALK_MODE; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | Creature::~Creature() |
---|
| 71 | { |
---|
| 72 | CleanupsBeforeDelete(); |
---|
| 73 | |
---|
| 74 | m_trainer_spells.clear(); |
---|
| 75 | m_vendor_items.clear(); |
---|
| 76 | |
---|
| 77 | delete i_AI; |
---|
| 78 | i_AI = NULL; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void Creature::AddToWorld() |
---|
| 82 | { |
---|
| 83 | ///- Register the creature for guid lookup |
---|
| 84 | if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this); |
---|
| 85 | Unit::AddToWorld(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void Creature::RemoveFromWorld() |
---|
| 89 | { |
---|
| 90 | ///- Remove the creature from the accessor |
---|
| 91 | if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this); |
---|
| 92 | Unit::RemoveFromWorld(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void Creature::LoadTrainerSpells() |
---|
| 96 | { |
---|
| 97 | if(m_trainerSpellsLoaded) |
---|
| 98 | return; |
---|
| 99 | |
---|
| 100 | m_trainer_spells.clear(); |
---|
| 101 | m_trainer_type = 0; |
---|
| 102 | |
---|
| 103 | Field *fields; |
---|
| 104 | QueryResult *result = WorldDatabase.PQuery("SELECT spell,spellcost,reqskill,reqskillvalue,reqlevel FROM npc_trainer WHERE entry = '%u'", GetEntry()); |
---|
| 105 | |
---|
| 106 | if(!result) |
---|
| 107 | return; |
---|
| 108 | |
---|
| 109 | do |
---|
| 110 | { |
---|
| 111 | fields = result->Fetch(); |
---|
| 112 | |
---|
| 113 | uint32 spellid = fields[0].GetUInt32(); |
---|
| 114 | SpellEntry const *spellinfo = sSpellStore.LookupEntry(spellid); |
---|
| 115 | |
---|
| 116 | if(!spellinfo) |
---|
| 117 | { |
---|
| 118 | sLog.outErrorDb("Trainer (Entry: %u ) has non existing spell %u",GetEntry(),spellid); |
---|
| 119 | continue; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | if(!SpellMgr::IsSpellValid(spellinfo)) |
---|
| 123 | { |
---|
| 124 | sLog.outErrorDb("LoadTrainerSpells: Trainer (Entry: %u) has broken learning spell %u.", GetEntry(), spellid); |
---|
| 125 | continue; |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | if(SpellMgr::IsProfessionSpell(spellinfo->Id)) |
---|
| 129 | m_trainer_type = 2; |
---|
| 130 | |
---|
| 131 | TrainerSpell tspell; |
---|
| 132 | tspell.spell = spellinfo; |
---|
| 133 | tspell.spellcost = fields[1].GetUInt32(); |
---|
| 134 | tspell.reqskill = fields[2].GetUInt32(); |
---|
| 135 | tspell.reqskillvalue= fields[3].GetUInt32(); |
---|
| 136 | tspell.reqlevel = fields[4].GetUInt32(); |
---|
| 137 | |
---|
| 138 | m_trainer_spells.push_back(tspell); |
---|
| 139 | |
---|
| 140 | } while( result->NextRow() ); |
---|
| 141 | |
---|
| 142 | delete result; |
---|
| 143 | |
---|
| 144 | m_trainerSpellsLoaded = true; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | void Creature::RemoveCorpse() |
---|
| 148 | { |
---|
| 149 | if( getDeathState()!=CORPSE && !m_isDeadByDefault || getDeathState()!=ALIVE && m_isDeadByDefault ) |
---|
| 150 | return; |
---|
| 151 | |
---|
| 152 | m_deathTimer = 0; |
---|
| 153 | setDeathState(DEAD); |
---|
| 154 | ObjectAccessor::UpdateObjectVisibility(this); |
---|
| 155 | loot.clear(); |
---|
| 156 | m_respawnTime = time(NULL) + m_respawnDelay; |
---|
| 157 | |
---|
| 158 | float x,y,z,o; |
---|
| 159 | GetRespawnCoord(x, y, z, &o); |
---|
| 160 | MapManager::Instance().GetMap(GetMapId(), this)->CreatureRelocation(this,x,y,z,o); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /** |
---|
| 164 | * change the entry of creature until respawn |
---|
| 165 | */ |
---|
| 166 | bool Creature::InitEntry(uint32 Entry, uint32 team, const CreatureData *data ) |
---|
| 167 | { |
---|
| 168 | CreatureInfo const *normalInfo = objmgr.GetCreatureTemplate(Entry); |
---|
| 169 | if(!normalInfo) |
---|
| 170 | { |
---|
| 171 | sLog.outErrorDb("Creature::UpdateEntry creature entry %u does not exist.", Entry); |
---|
| 172 | return false; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | // get heroic mode entry |
---|
| 176 | uint32 actualEntry = Entry; |
---|
| 177 | CreatureInfo const *cinfo = normalInfo; |
---|
| 178 | if(normalInfo->HeroicEntry) |
---|
| 179 | { |
---|
| 180 | Map *map = MapManager::Instance().FindMap(GetMapId(), GetInstanceId()); |
---|
| 181 | if(map && map->IsHeroic()) |
---|
| 182 | { |
---|
| 183 | cinfo = objmgr.GetCreatureTemplate(normalInfo->HeroicEntry); |
---|
| 184 | if(!cinfo) |
---|
| 185 | { |
---|
| 186 | sLog.outErrorDb("Creature::UpdateEntry creature heroic entry %u does not exist.", actualEntry); |
---|
| 187 | return false; |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | SetUInt32Value(OBJECT_FIELD_ENTRY, Entry); // normal entry always |
---|
| 193 | m_creatureInfo = cinfo; // map mode related always |
---|
| 194 | |
---|
| 195 | if (cinfo->DisplayID_A == 0 || cinfo->DisplayID_H == 0) // Cancel load if no model defined |
---|
| 196 | { |
---|
| 197 | sLog.outErrorDb("Creature (Entry: %u) has no model defined for Horde or Alliance in table `creature_template`, can't load. ",Entry); |
---|
| 198 | return false; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | uint32 display_id = objmgr.ChooseDisplayId(team, GetCreatureInfo(), data); |
---|
| 202 | CreatureModelInfo const *minfo = objmgr.GetCreatureModelRandomGender(display_id); |
---|
| 203 | if (!minfo) |
---|
| 204 | { |
---|
| 205 | sLog.outErrorDb("Creature (Entry: %u) has model %u not found in table `creature_model_info`, can't load. ", Entry, display_id); |
---|
| 206 | return false; |
---|
| 207 | } |
---|
| 208 | else |
---|
| 209 | display_id = minfo->modelid; // it can be different (for another gender) |
---|
| 210 | |
---|
| 211 | SetDisplayId(display_id); |
---|
| 212 | SetNativeDisplayId(display_id); |
---|
| 213 | SetByteValue(UNIT_FIELD_BYTES_0, 2, minfo->gender); |
---|
| 214 | |
---|
| 215 | // Load creature equipment |
---|
| 216 | if(!data || data->equipmentId == 0) |
---|
| 217 | { // use default from the template |
---|
| 218 | LoadEquipment(cinfo->equipmentId); |
---|
| 219 | } |
---|
| 220 | else if(data && data->equipmentId != -1) |
---|
| 221 | { // override, -1 means no equipment |
---|
| 222 | LoadEquipment(data->equipmentId); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | SetName(normalInfo->Name); // at normal entry always |
---|
| 226 | |
---|
| 227 | SetFloatValue(UNIT_FIELD_BOUNDINGRADIUS,minfo->bounding_radius); |
---|
| 228 | SetFloatValue(UNIT_FIELD_COMBATREACH,minfo->combat_reach ); |
---|
| 229 | |
---|
| 230 | SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0f); |
---|
| 231 | |
---|
| 232 | SetSpeed(MOVE_WALK, cinfo->speed ); |
---|
| 233 | SetSpeed(MOVE_RUN, cinfo->speed ); |
---|
| 234 | SetSpeed(MOVE_SWIM, cinfo->speed ); |
---|
| 235 | |
---|
| 236 | SetFloatValue(OBJECT_FIELD_SCALE_X, cinfo->scale); |
---|
| 237 | |
---|
| 238 | // checked at loading |
---|
| 239 | m_defaultMovementType = MovementGeneratorType(cinfo->MovementType); |
---|
| 240 | if(!m_respawnradius && m_defaultMovementType==RANDOM_MOTION_TYPE) |
---|
| 241 | m_defaultMovementType = IDLE_MOTION_TYPE; |
---|
| 242 | |
---|
| 243 | return true; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | bool Creature::UpdateEntry(uint32 Entry, uint32 team, const CreatureData *data ) |
---|
| 247 | { |
---|
| 248 | if(!InitEntry(Entry,team,data)) |
---|
| 249 | return false; |
---|
| 250 | |
---|
| 251 | m_regenHealth = GetCreatureInfo()->RegenHealth; |
---|
| 252 | |
---|
| 253 | // creatures always have melee weapon ready if any |
---|
| 254 | SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE ); |
---|
| 255 | SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_AURAS ); |
---|
| 256 | |
---|
| 257 | SelectLevel(GetCreatureInfo()); |
---|
| 258 | if (team == HORDE) |
---|
| 259 | SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, GetCreatureInfo()->faction_H); |
---|
| 260 | else |
---|
| 261 | SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, GetCreatureInfo()->faction_A); |
---|
| 262 | |
---|
| 263 | SetUInt32Value(UNIT_NPC_FLAGS,GetCreatureInfo()->npcflag); |
---|
| 264 | |
---|
| 265 | SetAttackTime(BASE_ATTACK, GetCreatureInfo()->baseattacktime); |
---|
| 266 | SetAttackTime(OFF_ATTACK, GetCreatureInfo()->baseattacktime); |
---|
| 267 | SetAttackTime(RANGED_ATTACK,GetCreatureInfo()->rangeattacktime); |
---|
| 268 | |
---|
| 269 | SetUInt32Value(UNIT_FIELD_FLAGS,GetCreatureInfo()->Flags); |
---|
| 270 | SetUInt32Value(UNIT_DYNAMIC_FLAGS,GetCreatureInfo()->dynamicflags); |
---|
| 271 | |
---|
| 272 | SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(GetCreatureInfo()->armor)); |
---|
| 273 | SetModifierValue(UNIT_MOD_RESISTANCE_HOLY, BASE_VALUE, float(GetCreatureInfo()->resistance1)); |
---|
| 274 | SetModifierValue(UNIT_MOD_RESISTANCE_FIRE, BASE_VALUE, float(GetCreatureInfo()->resistance2)); |
---|
| 275 | SetModifierValue(UNIT_MOD_RESISTANCE_NATURE, BASE_VALUE, float(GetCreatureInfo()->resistance3)); |
---|
| 276 | SetModifierValue(UNIT_MOD_RESISTANCE_FROST, BASE_VALUE, float(GetCreatureInfo()->resistance4)); |
---|
| 277 | SetModifierValue(UNIT_MOD_RESISTANCE_SHADOW, BASE_VALUE, float(GetCreatureInfo()->resistance5)); |
---|
| 278 | SetModifierValue(UNIT_MOD_RESISTANCE_ARCANE, BASE_VALUE, float(GetCreatureInfo()->resistance6)); |
---|
| 279 | |
---|
| 280 | SetCanModifyStats(true); |
---|
| 281 | UpdateAllStats(); |
---|
| 282 | |
---|
| 283 | FactionTemplateEntry const* factionTemplate = sFactionTemplateStore.LookupEntry(GetCreatureInfo()->faction_A); |
---|
| 284 | if (factionTemplate) // check and error show at loading templates |
---|
| 285 | { |
---|
| 286 | FactionEntry const* factionEntry = sFactionStore.LookupEntry(factionTemplate->faction); |
---|
| 287 | if (factionEntry) |
---|
| 288 | if( !(GetCreatureInfo()->flags_extra & CREATURE_FLAG_EXTRA_CIVILIAN) && |
---|
| 289 | (factionEntry->team == ALLIANCE || factionEntry->team == HORDE) ) |
---|
| 290 | SetPvP(true); |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | m_spells[0] = GetCreatureInfo()->spell1; |
---|
| 294 | m_spells[1] = GetCreatureInfo()->spell2; |
---|
| 295 | m_spells[2] = GetCreatureInfo()->spell3; |
---|
| 296 | m_spells[3] = GetCreatureInfo()->spell4; |
---|
| 297 | |
---|
| 298 | return true; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | void Creature::Update(uint32 diff) |
---|
| 302 | { |
---|
| 303 | if(m_GlobalCooldown <= diff) |
---|
| 304 | m_GlobalCooldown = 0; |
---|
| 305 | else |
---|
| 306 | m_GlobalCooldown -= diff; |
---|
| 307 | |
---|
| 308 | switch( m_deathState ) |
---|
| 309 | { |
---|
| 310 | case JUST_ALIVED: |
---|
| 311 | // Dont must be called, see Creature::setDeathState JUST_ALIVED -> ALIVE promoting. |
---|
| 312 | sLog.outError("Creature (GUIDLow: %u Entry: %u ) in wrong state: JUST_ALIVED (4)",GetGUIDLow(),GetEntry()); |
---|
| 313 | break; |
---|
| 314 | case JUST_DIED: |
---|
| 315 | // Dont must be called, see Creature::setDeathState JUST_DIED -> CORPSE promoting. |
---|
| 316 | sLog.outError("Creature (GUIDLow: %u Entry: %u ) in wrong state: JUST_DEAD (1)",GetGUIDLow(),GetEntry()); |
---|
| 317 | break; |
---|
| 318 | case DEAD: |
---|
| 319 | { |
---|
| 320 | if( m_respawnTime <= time(NULL) ) |
---|
| 321 | { |
---|
| 322 | DEBUG_LOG("Respawning..."); |
---|
| 323 | m_respawnTime = 0; |
---|
| 324 | lootForPickPocketed = false; |
---|
| 325 | lootForBody = false; |
---|
| 326 | |
---|
| 327 | if(m_originalEntry != GetUInt32Value(OBJECT_FIELD_ENTRY)) |
---|
| 328 | UpdateEntry(m_originalEntry); |
---|
| 329 | |
---|
| 330 | CreatureInfo const *cinfo = GetCreatureInfo(); |
---|
| 331 | |
---|
| 332 | SelectLevel(cinfo); |
---|
| 333 | SetUInt32Value(UNIT_DYNAMIC_FLAGS, 0); |
---|
| 334 | if (m_isDeadByDefault) |
---|
| 335 | { |
---|
| 336 | setDeathState(JUST_DIED); |
---|
| 337 | SetHealth(0); |
---|
| 338 | i_motionMaster.Clear(); |
---|
| 339 | clearUnitState(UNIT_STAT_ALL_STATE); |
---|
| 340 | LoadCreaturesAddon(true); |
---|
| 341 | } |
---|
| 342 | else |
---|
| 343 | setDeathState( JUST_ALIVED ); |
---|
| 344 | |
---|
| 345 | //Call AI respawn virtual function |
---|
| 346 | i_AI->JustRespawned(); |
---|
| 347 | |
---|
| 348 | MapManager::Instance().GetMap(GetMapId(), this)->Add(this); |
---|
| 349 | } |
---|
| 350 | break; |
---|
| 351 | } |
---|
| 352 | case CORPSE: |
---|
| 353 | { |
---|
| 354 | if (m_isDeadByDefault) |
---|
| 355 | break; |
---|
| 356 | |
---|
| 357 | if( m_deathTimer <= diff ) |
---|
| 358 | { |
---|
| 359 | RemoveCorpse(); |
---|
| 360 | DEBUG_LOG("Removing corpse... %u ", GetUInt32Value(OBJECT_FIELD_ENTRY)); |
---|
| 361 | } |
---|
| 362 | else |
---|
| 363 | { |
---|
| 364 | m_deathTimer -= diff; |
---|
| 365 | if (m_groupLootTimer && lootingGroupLeaderGUID) |
---|
| 366 | { |
---|
| 367 | if(diff <= m_groupLootTimer) |
---|
| 368 | { |
---|
| 369 | m_groupLootTimer -= diff; |
---|
| 370 | } |
---|
| 371 | else |
---|
| 372 | { |
---|
| 373 | Group* group = objmgr.GetGroupByLeader(lootingGroupLeaderGUID); |
---|
| 374 | if (group) |
---|
| 375 | group->EndRoll(); |
---|
| 376 | m_groupLootTimer = 0; |
---|
| 377 | lootingGroupLeaderGUID = 0; |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | break; |
---|
| 383 | } |
---|
| 384 | case ALIVE: |
---|
| 385 | { |
---|
| 386 | if (m_isDeadByDefault) |
---|
| 387 | { |
---|
| 388 | if( m_deathTimer <= diff ) |
---|
| 389 | { |
---|
| 390 | RemoveCorpse(); |
---|
| 391 | DEBUG_LOG("Removing alive corpse... %u ", GetUInt32Value(OBJECT_FIELD_ENTRY)); |
---|
| 392 | } |
---|
| 393 | else |
---|
| 394 | { |
---|
| 395 | m_deathTimer -= diff; |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | Unit::Update( diff ); |
---|
| 400 | |
---|
| 401 | // creature can be dead after Unit::Update call |
---|
| 402 | // CORPSE/DEAD state will processed at next tick (in other case death timer will be updated unexpectedly) |
---|
| 403 | if(!isAlive()) |
---|
| 404 | break; |
---|
| 405 | |
---|
| 406 | if(!IsInEvadeMode()) |
---|
| 407 | { |
---|
| 408 | // do not allow the AI to be changed during update |
---|
| 409 | m_AI_locked = true; |
---|
| 410 | i_AI->UpdateAI(diff); |
---|
| 411 | m_AI_locked = false; |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | // creature can be dead after UpdateAI call |
---|
| 415 | // CORPSE/DEAD state will processed at next tick (in other case death timer will be updated unexpectedly) |
---|
| 416 | if(!isAlive()) |
---|
| 417 | break; |
---|
| 418 | if(m_regenTimer > 0) |
---|
| 419 | { |
---|
| 420 | if(diff >= m_regenTimer) |
---|
| 421 | m_regenTimer = 0; |
---|
| 422 | else |
---|
| 423 | m_regenTimer -= diff; |
---|
| 424 | } |
---|
| 425 | if (m_regenTimer != 0) |
---|
| 426 | break; |
---|
| 427 | |
---|
| 428 | if (!isInCombat() || IsPolymorphed()) |
---|
| 429 | RegenerateHealth(); |
---|
| 430 | |
---|
| 431 | RegenerateMana(); |
---|
| 432 | |
---|
| 433 | m_regenTimer = 2000; |
---|
| 434 | break; |
---|
| 435 | } |
---|
| 436 | default: |
---|
| 437 | break; |
---|
| 438 | } |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | void Creature::RegenerateMana() |
---|
| 442 | { |
---|
| 443 | uint32 curValue = GetPower(POWER_MANA); |
---|
| 444 | uint32 maxValue = GetMaxPower(POWER_MANA); |
---|
| 445 | |
---|
| 446 | if (curValue >= maxValue) |
---|
| 447 | return; |
---|
| 448 | |
---|
| 449 | uint32 addvalue = 0; |
---|
| 450 | |
---|
| 451 | // Combat and any controlled creature |
---|
| 452 | if (isInCombat() || GetCharmerOrOwnerGUID()) |
---|
| 453 | { |
---|
| 454 | if(!IsUnderLastManaUseEffect()) |
---|
| 455 | { |
---|
| 456 | float ManaIncreaseRate = sWorld.getRate(RATE_POWER_MANA); |
---|
| 457 | float Spirit = GetStat(STAT_SPIRIT); |
---|
| 458 | |
---|
| 459 | addvalue = uint32((Spirit/5.0f + 17.0f) * ManaIncreaseRate); |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | else |
---|
| 463 | addvalue = maxValue/3; |
---|
| 464 | |
---|
| 465 | ModifyPower(POWER_MANA, addvalue); |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | void Creature::RegenerateHealth() |
---|
| 469 | { |
---|
| 470 | if (!isRegeneratingHealth()) |
---|
| 471 | return; |
---|
| 472 | |
---|
| 473 | uint32 curValue = GetHealth(); |
---|
| 474 | uint32 maxValue = GetMaxHealth(); |
---|
| 475 | |
---|
| 476 | if (curValue >= maxValue) |
---|
| 477 | return; |
---|
| 478 | |
---|
| 479 | uint32 addvalue = 0; |
---|
| 480 | |
---|
| 481 | // Not only pet, but any controelled creature |
---|
| 482 | if(GetCharmerOrOwnerGUID()) |
---|
| 483 | { |
---|
| 484 | float HealthIncreaseRate = sWorld.getRate(RATE_HEALTH); |
---|
| 485 | float Spirit = GetStat(STAT_SPIRIT); |
---|
| 486 | |
---|
| 487 | if( GetPower(POWER_MANA) > 0 ) |
---|
| 488 | addvalue = uint32(Spirit * 0.25 * HealthIncreaseRate); |
---|
| 489 | else |
---|
| 490 | addvalue = uint32(Spirit * 0.80 * HealthIncreaseRate); |
---|
| 491 | } |
---|
| 492 | else |
---|
| 493 | addvalue = maxValue/3; |
---|
| 494 | |
---|
| 495 | ModifyHealth(addvalue); |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | bool Creature::AIM_Initialize() |
---|
| 499 | { |
---|
| 500 | // make sure nothing can change the AI during AI update |
---|
| 501 | if(m_AI_locked) |
---|
| 502 | { |
---|
| 503 | sLog.outDebug("AIM_Initialize: failed to init, locked."); |
---|
| 504 | return false; |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | CreatureAI * oldAI = i_AI; |
---|
| 508 | i_motionMaster.Initialize(); |
---|
| 509 | i_AI = FactorySelector::selectAI(this); |
---|
| 510 | if (oldAI) |
---|
| 511 | delete oldAI; |
---|
| 512 | return true; |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | bool Creature::Create (uint32 guidlow, Map *map, uint32 Entry, uint32 team, const CreatureData *data) |
---|
| 516 | { |
---|
| 517 | SetMapId(map->GetId()); |
---|
| 518 | SetInstanceId(map->GetInstanceId()); |
---|
| 519 | |
---|
| 520 | //oX = x; oY = y; dX = x; dY = y; m_moveTime = 0; m_startMove = 0; |
---|
| 521 | const bool bResult = CreateFromProto(guidlow, Entry, team, data); |
---|
| 522 | |
---|
| 523 | if (bResult) |
---|
| 524 | { |
---|
| 525 | switch (GetCreatureInfo()->rank) |
---|
| 526 | { |
---|
| 527 | case CREATURE_ELITE_RARE: |
---|
| 528 | m_corpseDelay = sWorld.getConfig(CONFIG_CORPSE_DECAY_RARE); |
---|
| 529 | break; |
---|
| 530 | case CREATURE_ELITE_ELITE: |
---|
| 531 | m_corpseDelay = sWorld.getConfig(CONFIG_CORPSE_DECAY_ELITE); |
---|
| 532 | break; |
---|
| 533 | case CREATURE_ELITE_RAREELITE: |
---|
| 534 | m_corpseDelay = sWorld.getConfig(CONFIG_CORPSE_DECAY_RAREELITE); |
---|
| 535 | break; |
---|
| 536 | case CREATURE_ELITE_WORLDBOSS: |
---|
| 537 | m_corpseDelay = sWorld.getConfig(CONFIG_CORPSE_DECAY_WORLDBOSS); |
---|
| 538 | break; |
---|
| 539 | default: |
---|
| 540 | m_corpseDelay = sWorld.getConfig(CONFIG_CORPSE_DECAY_NORMAL); |
---|
| 541 | break; |
---|
| 542 | } |
---|
| 543 | } |
---|
| 544 | |
---|
| 545 | return bResult; |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | bool Creature::isCanTrainingOf(Player* pPlayer, bool msg) const |
---|
| 549 | { |
---|
| 550 | if(!isTrainer()) |
---|
| 551 | return false; |
---|
| 552 | |
---|
| 553 | if(m_trainer_spells.empty()) |
---|
| 554 | { |
---|
| 555 | sLog.outErrorDb("Creature %u (Entry: %u) have UNIT_NPC_FLAG_TRAINER but have empty trainer spell list.", |
---|
| 556 | GetGUIDLow(),GetEntry()); |
---|
| 557 | return false; |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | switch(GetCreatureInfo()->trainer_type) |
---|
| 561 | { |
---|
| 562 | case TRAINER_TYPE_CLASS: |
---|
| 563 | if(pPlayer->getClass()!=GetCreatureInfo()->classNum) |
---|
| 564 | { |
---|
| 565 | if(msg) |
---|
| 566 | { |
---|
| 567 | pPlayer->PlayerTalkClass->ClearMenus(); |
---|
| 568 | switch(GetCreatureInfo()->classNum) |
---|
| 569 | { |
---|
| 570 | case CLASS_DRUID: pPlayer->PlayerTalkClass->SendGossipMenu( 4913,GetGUID()); break; |
---|
| 571 | case CLASS_HUNTER: pPlayer->PlayerTalkClass->SendGossipMenu(10090,GetGUID()); break; |
---|
| 572 | case CLASS_MAGE: pPlayer->PlayerTalkClass->SendGossipMenu( 328,GetGUID()); break; |
---|
| 573 | case CLASS_PALADIN:pPlayer->PlayerTalkClass->SendGossipMenu( 1635,GetGUID()); break; |
---|
| 574 | case CLASS_PRIEST: pPlayer->PlayerTalkClass->SendGossipMenu( 4436,GetGUID()); break; |
---|
| 575 | case CLASS_ROGUE: pPlayer->PlayerTalkClass->SendGossipMenu( 4797,GetGUID()); break; |
---|
| 576 | case CLASS_SHAMAN: pPlayer->PlayerTalkClass->SendGossipMenu( 5003,GetGUID()); break; |
---|
| 577 | case CLASS_WARLOCK:pPlayer->PlayerTalkClass->SendGossipMenu( 5836,GetGUID()); break; |
---|
| 578 | case CLASS_WARRIOR:pPlayer->PlayerTalkClass->SendGossipMenu( 4985,GetGUID()); break; |
---|
| 579 | } |
---|
| 580 | } |
---|
| 581 | return false; |
---|
| 582 | } |
---|
| 583 | break; |
---|
| 584 | case TRAINER_TYPE_PETS: |
---|
| 585 | if(pPlayer->getClass()!=CLASS_HUNTER) |
---|
| 586 | { |
---|
| 587 | pPlayer->PlayerTalkClass->ClearMenus(); |
---|
| 588 | pPlayer->PlayerTalkClass->SendGossipMenu(3620,GetGUID()); |
---|
| 589 | return false; |
---|
| 590 | } |
---|
| 591 | break; |
---|
| 592 | case TRAINER_TYPE_MOUNTS: |
---|
| 593 | if(GetCreatureInfo()->race && pPlayer->getRace() != GetCreatureInfo()->race) |
---|
| 594 | { |
---|
| 595 | if(msg) |
---|
| 596 | { |
---|
| 597 | pPlayer->PlayerTalkClass->ClearMenus(); |
---|
| 598 | switch(GetCreatureInfo()->classNum) |
---|
| 599 | { |
---|
| 600 | case RACE_DWARF: pPlayer->PlayerTalkClass->SendGossipMenu(5865,GetGUID()); break; |
---|
| 601 | case RACE_GNOME: pPlayer->PlayerTalkClass->SendGossipMenu(4881,GetGUID()); break; |
---|
| 602 | case RACE_HUMAN: pPlayer->PlayerTalkClass->SendGossipMenu(5861,GetGUID()); break; |
---|
| 603 | case RACE_NIGHTELF: pPlayer->PlayerTalkClass->SendGossipMenu(5862,GetGUID()); break; |
---|
| 604 | case RACE_ORC: pPlayer->PlayerTalkClass->SendGossipMenu(5863,GetGUID()); break; |
---|
| 605 | case RACE_TAUREN: pPlayer->PlayerTalkClass->SendGossipMenu(5864,GetGUID()); break; |
---|
| 606 | case RACE_TROLL: pPlayer->PlayerTalkClass->SendGossipMenu(5816,GetGUID()); break; |
---|
| 607 | case RACE_UNDEAD_PLAYER:pPlayer->PlayerTalkClass->SendGossipMenu( 624,GetGUID()); break; |
---|
| 608 | case RACE_BLOODELF: pPlayer->PlayerTalkClass->SendGossipMenu(5862,GetGUID()); break; |
---|
| 609 | case RACE_DRAENEI: pPlayer->PlayerTalkClass->SendGossipMenu(5864,GetGUID()); break; |
---|
| 610 | } |
---|
| 611 | } |
---|
| 612 | return false; |
---|
| 613 | } |
---|
| 614 | break; |
---|
| 615 | case TRAINER_TYPE_TRADESKILLS: |
---|
| 616 | if(GetCreatureInfo()->trainer_spell && !pPlayer->HasSpell(GetCreatureInfo()->trainer_spell)) |
---|
| 617 | { |
---|
| 618 | if(msg) |
---|
| 619 | { |
---|
| 620 | pPlayer->PlayerTalkClass->ClearMenus(); |
---|
| 621 | pPlayer->PlayerTalkClass->SendGossipMenu(11031,GetGUID()); |
---|
| 622 | } |
---|
| 623 | return false; |
---|
| 624 | } |
---|
| 625 | break; |
---|
| 626 | default: |
---|
| 627 | return false; // checked and error output at creature_template loading |
---|
| 628 | } |
---|
| 629 | return true; |
---|
| 630 | } |
---|
| 631 | |
---|
| 632 | bool Creature::isCanIneractWithBattleMaster(Player* pPlayer, bool msg) const |
---|
| 633 | { |
---|
| 634 | if(!isBattleMaster()) |
---|
| 635 | return false; |
---|
| 636 | |
---|
| 637 | uint32 bgTypeId = objmgr.GetBattleMasterBG(GetEntry()); |
---|
| 638 | if(!msg) |
---|
| 639 | return pPlayer->GetBGAccessByLevel(bgTypeId); |
---|
| 640 | |
---|
| 641 | if(!pPlayer->GetBGAccessByLevel(bgTypeId)) |
---|
| 642 | { |
---|
| 643 | pPlayer->PlayerTalkClass->ClearMenus(); |
---|
| 644 | switch(bgTypeId) |
---|
| 645 | { |
---|
| 646 | case BATTLEGROUND_AV: pPlayer->PlayerTalkClass->SendGossipMenu(7616,GetGUID()); break; |
---|
| 647 | case BATTLEGROUND_WS: pPlayer->PlayerTalkClass->SendGossipMenu(7599,GetGUID()); break; |
---|
| 648 | case BATTLEGROUND_AB: pPlayer->PlayerTalkClass->SendGossipMenu(7642,GetGUID()); break; |
---|
| 649 | case BATTLEGROUND_EY: |
---|
| 650 | case BATTLEGROUND_NA: |
---|
| 651 | case BATTLEGROUND_BE: |
---|
| 652 | case BATTLEGROUND_AA: |
---|
| 653 | case BATTLEGROUND_RL: pPlayer->PlayerTalkClass->SendGossipMenu(10024,GetGUID()); break; |
---|
| 654 | break; |
---|
| 655 | } |
---|
| 656 | return false; |
---|
| 657 | } |
---|
| 658 | return true; |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | bool Creature::isCanTrainingAndResetTalentsOf(Player* pPlayer) const |
---|
| 662 | { |
---|
| 663 | return pPlayer->getLevel() >= 10 |
---|
| 664 | && GetCreatureInfo()->trainer_type == TRAINER_TYPE_CLASS |
---|
| 665 | && pPlayer->getClass() == GetCreatureInfo()->classNum; |
---|
| 666 | } |
---|
| 667 | |
---|
| 668 | void Creature::prepareGossipMenu( Player *pPlayer,uint32 gossipid ) |
---|
| 669 | { |
---|
| 670 | PlayerMenu* pm=pPlayer->PlayerTalkClass; |
---|
| 671 | pm->ClearMenus(); |
---|
| 672 | |
---|
| 673 | // lazy loading single time at use |
---|
| 674 | LoadGossipOptions(); |
---|
| 675 | |
---|
| 676 | GossipOption* gso; |
---|
| 677 | GossipOption* ingso; |
---|
| 678 | |
---|
| 679 | for( GossipOptionList::iterator i = m_goptions.begin( ); i != m_goptions.end( ); i++ ) |
---|
| 680 | { |
---|
| 681 | gso=&*i; |
---|
| 682 | if(gso->GossipId == gossipid) |
---|
| 683 | { |
---|
| 684 | bool cantalking=true; |
---|
| 685 | if(gso->Id==1) |
---|
| 686 | { |
---|
| 687 | uint32 textid=GetNpcTextId(); |
---|
| 688 | GossipText * gossiptext=objmgr.GetGossipText(textid); |
---|
| 689 | if(!gossiptext) |
---|
| 690 | cantalking=false; |
---|
| 691 | } |
---|
| 692 | else |
---|
| 693 | { |
---|
| 694 | switch (gso->Action) |
---|
| 695 | { |
---|
| 696 | case GOSSIP_OPTION_QUESTGIVER: |
---|
| 697 | pPlayer->PrepareQuestMenu(GetGUID()); |
---|
| 698 | //if (pm->GetQuestMenu()->MenuItemCount() == 0) |
---|
| 699 | cantalking=false; |
---|
| 700 | //pm->GetQuestMenu()->ClearMenu(); |
---|
| 701 | break; |
---|
| 702 | case GOSSIP_OPTION_ARMORER: |
---|
| 703 | cantalking=false; // added in special mode |
---|
| 704 | break; |
---|
| 705 | case GOSSIP_OPTION_SPIRITHEALER: |
---|
| 706 | if( !pPlayer->isDead() ) |
---|
| 707 | cantalking=false; |
---|
| 708 | break; |
---|
| 709 | case GOSSIP_OPTION_VENDOR: |
---|
| 710 | // load vendor items if not yet |
---|
| 711 | LoadGoods(); |
---|
| 712 | |
---|
| 713 | if(!GetItemCount()) |
---|
| 714 | { |
---|
| 715 | sLog.outErrorDb("Creature %u (Entry: %u) have UNIT_NPC_FLAG_VENDOR but have empty trading item list.", |
---|
| 716 | GetGUIDLow(),GetEntry()); |
---|
| 717 | cantalking=false; |
---|
| 718 | } |
---|
| 719 | break; |
---|
| 720 | case GOSSIP_OPTION_TRAINER: |
---|
| 721 | // Lazy loading at first access |
---|
| 722 | LoadTrainerSpells(); |
---|
| 723 | |
---|
| 724 | if(!isCanTrainingOf(pPlayer,false)) |
---|
| 725 | cantalking=false; |
---|
| 726 | break; |
---|
| 727 | case GOSSIP_OPTION_UNLEARNTALENTS: |
---|
| 728 | if(!isCanTrainingAndResetTalentsOf(pPlayer)) |
---|
| 729 | cantalking=false; |
---|
| 730 | break; |
---|
| 731 | case GOSSIP_OPTION_UNLEARNPETSKILLS: |
---|
| 732 | if(!pPlayer->GetPet() || pPlayer->GetPet()->getPetType() != HUNTER_PET || pPlayer->GetPet()->m_spells.size() <= 1 || GetCreatureInfo()->trainer_type != TRAINER_TYPE_PETS || GetCreatureInfo()->classNum != CLASS_HUNTER) |
---|
| 733 | cantalking=false; |
---|
| 734 | break; |
---|
| 735 | case GOSSIP_OPTION_TAXIVENDOR: |
---|
| 736 | if ( pPlayer->GetSession()->SendLearnNewTaxiNode(this) ) |
---|
| 737 | return; |
---|
| 738 | break; |
---|
| 739 | case GOSSIP_OPTION_BATTLEFIELD: |
---|
| 740 | if(!isCanIneractWithBattleMaster(pPlayer,false)) |
---|
| 741 | cantalking=false; |
---|
| 742 | break; |
---|
| 743 | case GOSSIP_OPTION_SPIRITGUIDE: |
---|
| 744 | case GOSSIP_OPTION_INNKEEPER: |
---|
| 745 | case GOSSIP_OPTION_BANKER: |
---|
| 746 | case GOSSIP_OPTION_PETITIONER: |
---|
| 747 | case GOSSIP_OPTION_STABLEPET: |
---|
| 748 | case GOSSIP_OPTION_TABARDDESIGNER: |
---|
| 749 | case GOSSIP_OPTION_AUCTIONEER: |
---|
| 750 | break; // no checks |
---|
| 751 | default: |
---|
| 752 | sLog.outErrorDb("Creature %u (entry: %u) have unknown gossip option %u",GetGUIDLow(),GetEntry(),gso->Action); |
---|
| 753 | break; |
---|
| 754 | } |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | if(!gso->Option.empty() && cantalking ) |
---|
| 758 | { //note for future dev: should have database fields for BoxMessage & BoxMoney |
---|
| 759 | pm->GetGossipMenu()->AddMenuItem((uint8)gso->Icon,gso->Option, gossipid,gso->Action,"",0,false); |
---|
| 760 | ingso=gso; |
---|
| 761 | } |
---|
| 762 | } |
---|
| 763 | } |
---|
| 764 | |
---|
| 765 | ///some gossips aren't handled in normal way ... so we need to do it this way .. TODO: handle it in normal way ;-) |
---|
| 766 | if(pm->GetGossipMenu()->MenuItemCount()==0 && !pm->GetQuestMenu()->MenuItemCount()) |
---|
| 767 | { |
---|
| 768 | if(HasFlag(UNIT_NPC_FLAGS,UNIT_NPC_FLAG_TRAINER)) |
---|
| 769 | { |
---|
| 770 | LoadTrainerSpells(); // Lazy loading at first access |
---|
| 771 | isCanTrainingOf(pPlayer,true); // output error message if need |
---|
| 772 | } |
---|
| 773 | if(HasFlag(UNIT_NPC_FLAGS,UNIT_NPC_FLAG_BATTLEMASTER)) |
---|
| 774 | { |
---|
| 775 | isCanIneractWithBattleMaster(pPlayer,true); // output error message if need |
---|
| 776 | } |
---|
| 777 | } |
---|
| 778 | } |
---|
| 779 | |
---|
| 780 | void Creature::sendPreparedGossip(Player* player) |
---|
| 781 | { |
---|
| 782 | if(!player) |
---|
| 783 | return; |
---|
| 784 | |
---|
| 785 | GossipMenu* gossipmenu = player->PlayerTalkClass->GetGossipMenu(); |
---|
| 786 | |
---|
| 787 | // in case empty gossip menu open quest menu if any |
---|
| 788 | if (gossipmenu->MenuItemCount() == 0 && GetNpcTextId() == 0) |
---|
| 789 | { |
---|
| 790 | player->SendPreparedQuest(GetGUID()); |
---|
| 791 | return; |
---|
| 792 | } |
---|
| 793 | |
---|
| 794 | // in case non empty gossip menu (that not included quests list size) show it |
---|
| 795 | // (quest entries from quest menu wiill be included in list) |
---|
| 796 | player->PlayerTalkClass->SendGossipMenu(GetNpcTextId(), GetGUID()); |
---|
| 797 | } |
---|
| 798 | |
---|
| 799 | void Creature::OnGossipSelect(Player* player, uint32 option) |
---|
| 800 | { |
---|
| 801 | GossipMenu* gossipmenu = player->PlayerTalkClass->GetGossipMenu(); |
---|
| 802 | uint32 action=gossipmenu->GetItem(option).m_gAction; |
---|
| 803 | uint32 zoneid=GetZoneId(); |
---|
| 804 | uint64 guid=GetGUID(); |
---|
| 805 | GossipOption const *gossip=GetGossipOption( action ); |
---|
| 806 | uint32 textid; |
---|
| 807 | if(!gossip) |
---|
| 808 | { |
---|
| 809 | zoneid=0; |
---|
| 810 | gossip=GetGossipOption( action ); |
---|
| 811 | if(!gossip) |
---|
| 812 | return; |
---|
| 813 | } |
---|
| 814 | textid=GetGossipTextId( action, zoneid); |
---|
| 815 | if(textid==0) |
---|
| 816 | textid=GetNpcTextId(); |
---|
| 817 | |
---|
| 818 | switch (gossip->Action) |
---|
| 819 | { |
---|
| 820 | case GOSSIP_OPTION_GOSSIP: |
---|
| 821 | player->PlayerTalkClass->CloseGossip(); |
---|
| 822 | player->PlayerTalkClass->SendTalking( textid ); |
---|
| 823 | break; |
---|
| 824 | case GOSSIP_OPTION_SPIRITHEALER: |
---|
| 825 | if( player->isDead() ) |
---|
| 826 | CastSpell(this,17251,true,NULL,NULL,player->GetGUID()); |
---|
| 827 | break; |
---|
| 828 | case GOSSIP_OPTION_QUESTGIVER: |
---|
| 829 | player->PrepareQuestMenu( guid ); |
---|
| 830 | player->SendPreparedQuest( guid ); |
---|
| 831 | break; |
---|
| 832 | case GOSSIP_OPTION_VENDOR: |
---|
| 833 | case GOSSIP_OPTION_ARMORER: |
---|
| 834 | player->GetSession()->SendListInventory(guid); |
---|
| 835 | break; |
---|
| 836 | case GOSSIP_OPTION_STABLEPET: |
---|
| 837 | player->GetSession()->SendStablePet(guid); |
---|
| 838 | break; |
---|
| 839 | case GOSSIP_OPTION_TRAINER: |
---|
| 840 | player->GetSession()->SendTrainerList(guid); |
---|
| 841 | break; |
---|
| 842 | case GOSSIP_OPTION_UNLEARNTALENTS: |
---|
| 843 | player->PlayerTalkClass->CloseGossip(); |
---|
| 844 | player->SendTalentWipeConfirm(guid); |
---|
| 845 | break; |
---|
| 846 | case GOSSIP_OPTION_UNLEARNPETSKILLS: |
---|
| 847 | player->PlayerTalkClass->CloseGossip(); |
---|
| 848 | player->SendPetSkillWipeConfirm(); |
---|
| 849 | break; |
---|
| 850 | case GOSSIP_OPTION_TAXIVENDOR: |
---|
| 851 | player->GetSession()->SendTaxiMenu(this); |
---|
| 852 | break; |
---|
| 853 | case GOSSIP_OPTION_INNKEEPER: |
---|
| 854 | player->PlayerTalkClass->CloseGossip(); |
---|
| 855 | player->SetBindPoint( guid ); |
---|
| 856 | break; |
---|
| 857 | case GOSSIP_OPTION_BANKER: |
---|
| 858 | player->GetSession()->SendShowBank( guid ); |
---|
| 859 | break; |
---|
| 860 | case GOSSIP_OPTION_PETITIONER: |
---|
| 861 | player->PlayerTalkClass->CloseGossip(); |
---|
| 862 | player->GetSession()->SendPetitionShowList( guid ); |
---|
| 863 | break; |
---|
| 864 | case GOSSIP_OPTION_TABARDDESIGNER: |
---|
| 865 | player->PlayerTalkClass->CloseGossip(); |
---|
| 866 | player->GetSession()->SendTabardVendorActivate( guid ); |
---|
| 867 | break; |
---|
| 868 | case GOSSIP_OPTION_AUCTIONEER: |
---|
| 869 | player->GetSession()->SendAuctionHello( guid, this ); |
---|
| 870 | break; |
---|
| 871 | case GOSSIP_OPTION_SPIRITGUIDE: |
---|
| 872 | case GOSSIP_GUARD_SPELLTRAINER: |
---|
| 873 | case GOSSIP_GUARD_SKILLTRAINER: |
---|
| 874 | prepareGossipMenu( player,gossip->Id ); |
---|
| 875 | sendPreparedGossip( player ); |
---|
| 876 | break; |
---|
| 877 | case GOSSIP_OPTION_BATTLEFIELD: |
---|
| 878 | { |
---|
| 879 | uint32 bgTypeId = objmgr.GetBattleMasterBG(GetEntry()); |
---|
| 880 | player->GetSession()->SendBattlegGroundList( GetGUID(), bgTypeId ); |
---|
| 881 | break; |
---|
| 882 | } |
---|
| 883 | default: |
---|
| 884 | OnPoiSelect( player, gossip ); |
---|
| 885 | break; |
---|
| 886 | } |
---|
| 887 | |
---|
| 888 | } |
---|
| 889 | |
---|
| 890 | void Creature::OnPoiSelect(Player* player, GossipOption const *gossip) |
---|
| 891 | { |
---|
| 892 | if(gossip->GossipId==GOSSIP_GUARD_SPELLTRAINER || gossip->GossipId==GOSSIP_GUARD_SKILLTRAINER) |
---|
| 893 | { |
---|
| 894 | //float x,y; |
---|
| 895 | //bool findnpc=false; |
---|
| 896 | Poi_Icon icon = ICON_POI_0; |
---|
| 897 | //QueryResult *result; |
---|
| 898 | //Field *fields; |
---|
| 899 | uint32 mapid=GetMapId(); |
---|
| 900 | Map const* map=MapManager::Instance().GetBaseMap( mapid ); |
---|
| 901 | uint16 areaflag=map->GetAreaFlag(GetPositionX(),GetPositionY()); |
---|
| 902 | uint32 zoneid=Map::GetZoneId(areaflag,mapid); |
---|
| 903 | std::string areaname= gossip->Option; |
---|
| 904 | /* |
---|
| 905 | uint16 pflag; |
---|
| 906 | |
---|
| 907 | // use the action relate to creaturetemplate.trainer_type ? |
---|
| 908 | result= WorldDatabase.PQuery("SELECT creature.position_x,creature.position_y FROM creature,creature_template WHERE creature.map = '%u' AND creature.id = creature_template.entry AND creature_template.trainer_type = '%u'", mapid, gossip->Action ); |
---|
| 909 | if(!result) |
---|
| 910 | return; |
---|
| 911 | do |
---|
| 912 | { |
---|
| 913 | fields = result->Fetch(); |
---|
| 914 | x=fields[0].GetFloat(); |
---|
| 915 | y=fields[1].GetFloat(); |
---|
| 916 | pflag=map->GetAreaFlag(GetPositionX(),GetPositionY()); |
---|
| 917 | if(pflag==areaflag) |
---|
| 918 | { |
---|
| 919 | findnpc=true; |
---|
| 920 | break; |
---|
| 921 | } |
---|
| 922 | }while(result->NextRow()); |
---|
| 923 | |
---|
| 924 | delete result; |
---|
| 925 | |
---|
| 926 | if(!findnpc) |
---|
| 927 | { |
---|
| 928 | player->PlayerTalkClass->SendTalking( "$NSorry", "Here no this person."); |
---|
| 929 | return; |
---|
| 930 | }*/ |
---|
| 931 | |
---|
| 932 | //need add more case. |
---|
| 933 | switch(gossip->Action) |
---|
| 934 | { |
---|
| 935 | case GOSSIP_GUARD_BANK: |
---|
| 936 | icon=ICON_POI_HOUSE; |
---|
| 937 | break; |
---|
| 938 | case GOSSIP_GUARD_RIDE: |
---|
| 939 | icon=ICON_POI_RWHORSE; |
---|
| 940 | break; |
---|
| 941 | case GOSSIP_GUARD_GUILD: |
---|
| 942 | icon=ICON_POI_BLUETOWER; |
---|
| 943 | break; |
---|
| 944 | default: |
---|
| 945 | icon=ICON_POI_TOWER; |
---|
| 946 | break; |
---|
| 947 | } |
---|
| 948 | uint32 textid=GetGossipTextId( gossip->Action, zoneid ); |
---|
| 949 | player->PlayerTalkClass->SendTalking( textid ); |
---|
| 950 | // how this could worked player->PlayerTalkClass->SendPointOfInterest( x, y, icon, 2, 15, areaname.c_str() ); |
---|
| 951 | } |
---|
| 952 | } |
---|
| 953 | |
---|
| 954 | uint32 Creature::GetGossipTextId(uint32 action, uint32 zoneid) |
---|
| 955 | { |
---|
| 956 | QueryResult *result= WorldDatabase.PQuery("SELECT textid FROM npc_gossip_textid WHERE action = '%u' AND zoneid ='%u'", action, zoneid ); |
---|
| 957 | |
---|
| 958 | if(!result) |
---|
| 959 | return 0; |
---|
| 960 | |
---|
| 961 | Field *fields = result->Fetch(); |
---|
| 962 | uint32 id = fields[0].GetUInt32(); |
---|
| 963 | |
---|
| 964 | delete result; |
---|
| 965 | |
---|
| 966 | return id; |
---|
| 967 | } |
---|
| 968 | |
---|
| 969 | uint32 Creature::GetNpcTextId() |
---|
| 970 | { |
---|
| 971 | // already loaded and cached |
---|
| 972 | if(m_NPCTextId) |
---|
| 973 | return m_NPCTextId; |
---|
| 974 | |
---|
| 975 | QueryResult* result = WorldDatabase.PQuery("SELECT textid FROM npc_gossip WHERE npc_guid= '%u'", m_DBTableGuid); |
---|
| 976 | if(result) |
---|
| 977 | { |
---|
| 978 | Field *fields = result->Fetch(); |
---|
| 979 | m_NPCTextId = fields[0].GetUInt32(); |
---|
| 980 | delete result; |
---|
| 981 | } |
---|
| 982 | else |
---|
| 983 | m_NPCTextId = DEFAULT_GOSSIP_MESSAGE; |
---|
| 984 | |
---|
| 985 | return m_NPCTextId; |
---|
| 986 | } |
---|
| 987 | |
---|
| 988 | GossipOption const* Creature::GetGossipOption( uint32 id ) const |
---|
| 989 | { |
---|
| 990 | for( GossipOptionList::const_iterator i = m_goptions.begin( ); i != m_goptions.end( ); i++ ) |
---|
| 991 | { |
---|
| 992 | if(i->Action==id ) |
---|
| 993 | return &*i; |
---|
| 994 | } |
---|
| 995 | return NULL; |
---|
| 996 | } |
---|
| 997 | |
---|
| 998 | void Creature::LoadGossipOptions() |
---|
| 999 | { |
---|
| 1000 | if(m_gossipOptionLoaded) |
---|
| 1001 | return; |
---|
| 1002 | |
---|
| 1003 | uint32 npcflags=GetUInt32Value(UNIT_NPC_FLAGS); |
---|
| 1004 | |
---|
| 1005 | QueryResult *result = WorldDatabase.PQuery( "SELECT id,gossip_id,npcflag,icon,action,option_text FROM npc_option WHERE (npcflag & %u)<>0", npcflags ); |
---|
| 1006 | |
---|
| 1007 | if(!result) |
---|
| 1008 | return; |
---|
| 1009 | |
---|
| 1010 | GossipOption go; |
---|
| 1011 | do |
---|
| 1012 | { |
---|
| 1013 | Field *fields = result->Fetch(); |
---|
| 1014 | go.Id= fields[0].GetUInt32(); |
---|
| 1015 | go.GossipId = fields[1].GetUInt32(); |
---|
| 1016 | go.NpcFlag=fields[2].GetUInt32(); |
---|
| 1017 | go.Icon=fields[3].GetUInt32(); |
---|
| 1018 | go.Action=fields[4].GetUInt32(); |
---|
| 1019 | go.Option=fields[5].GetCppString(); |
---|
| 1020 | addGossipOption(go); |
---|
| 1021 | }while( result->NextRow() ); |
---|
| 1022 | delete result; |
---|
| 1023 | |
---|
| 1024 | m_gossipOptionLoaded = true; |
---|
| 1025 | } |
---|
| 1026 | |
---|
| 1027 | void Creature::AI_SendMoveToPacket(float x, float y, float z, uint32 time, uint32 MovementFlags, uint8 type) |
---|
| 1028 | { |
---|
| 1029 | /* uint32 timeElap = getMSTime(); |
---|
| 1030 | if ((timeElap - m_startMove) < m_moveTime) |
---|
| 1031 | { |
---|
| 1032 | oX = (dX - oX) * ( (timeElap - m_startMove) / m_moveTime ); |
---|
| 1033 | oY = (dY - oY) * ( (timeElap - m_startMove) / m_moveTime ); |
---|
| 1034 | } |
---|
| 1035 | else |
---|
| 1036 | { |
---|
| 1037 | oX = dX; |
---|
| 1038 | oY = dY; |
---|
| 1039 | } |
---|
| 1040 | |
---|
| 1041 | dX = x; |
---|
| 1042 | dY = y; |
---|
| 1043 | m_orientation = atan2((oY - dY), (oX - dX)); |
---|
| 1044 | |
---|
| 1045 | m_startMove = getMSTime(); |
---|
| 1046 | m_moveTime = time;*/ |
---|
| 1047 | SendMonsterMove(x, y, z, type, MovementFlags, time); |
---|
| 1048 | } |
---|
| 1049 | |
---|
| 1050 | Player *Creature::GetLootRecipient() const |
---|
| 1051 | { |
---|
| 1052 | if (!m_lootRecipient) return NULL; |
---|
| 1053 | else return ObjectAccessor::FindPlayer(m_lootRecipient); |
---|
| 1054 | } |
---|
| 1055 | |
---|
| 1056 | void Creature::SetLootRecipient(Unit *unit) |
---|
| 1057 | { |
---|
| 1058 | // set the player whose group should receive the right |
---|
| 1059 | // to loot the creature after it dies |
---|
| 1060 | // should be set to NULL after the loot disappears |
---|
| 1061 | |
---|
| 1062 | if (!unit) |
---|
| 1063 | { |
---|
| 1064 | m_lootRecipient = 0; |
---|
| 1065 | RemoveFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_OTHER_TAGGER); |
---|
| 1066 | return; |
---|
| 1067 | } |
---|
| 1068 | |
---|
| 1069 | Player* player = unit->GetCharmerOrOwnerPlayerOrPlayerItself(); |
---|
| 1070 | if(!player) // normal creature, no player involved |
---|
| 1071 | return; |
---|
| 1072 | |
---|
| 1073 | m_lootRecipient = player->GetGUID(); |
---|
| 1074 | SetFlag(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_OTHER_TAGGER); |
---|
| 1075 | } |
---|
| 1076 | |
---|
| 1077 | void Creature::SaveToDB() |
---|
| 1078 | { |
---|
| 1079 | // this should only be used when the creature has already been loaded |
---|
| 1080 | // perferably after adding to map, because mapid may not be valid otherwise |
---|
| 1081 | CreatureData const *data = objmgr.GetCreatureData(m_DBTableGuid); |
---|
| 1082 | if(!data) |
---|
| 1083 | { |
---|
| 1084 | sLog.outError("Creature::SaveToDB failed, cannot get creature data!"); |
---|
| 1085 | return; |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | SaveToDB(GetMapId(), data->spawnMask); |
---|
| 1089 | } |
---|
| 1090 | |
---|
| 1091 | void Creature::SaveToDB(uint32 mapid, uint8 spawnMask) |
---|
| 1092 | { |
---|
| 1093 | // update in loaded data |
---|
| 1094 | CreatureData& data = objmgr.NewOrExistCreatureData(m_DBTableGuid); |
---|
| 1095 | |
---|
| 1096 | uint32 displayId = GetNativeDisplayId(); |
---|
| 1097 | |
---|
| 1098 | // check if it's a custom model and if not, use 0 for displayId |
---|
| 1099 | CreatureInfo const *cinfo = GetCreatureInfo(); |
---|
| 1100 | if(cinfo) |
---|
| 1101 | { |
---|
| 1102 | if(displayId != cinfo->DisplayID_A && displayId != cinfo->DisplayID_H) |
---|
| 1103 | { |
---|
| 1104 | CreatureModelInfo const *minfo = objmgr.GetCreatureModelInfo(cinfo->DisplayID_A); |
---|
| 1105 | if(!minfo || displayId != minfo->modelid_other_gender) |
---|
| 1106 | { |
---|
| 1107 | minfo = objmgr.GetCreatureModelInfo(cinfo->DisplayID_H); |
---|
| 1108 | if(minfo && displayId == minfo->modelid_other_gender) |
---|
| 1109 | displayId = 0; |
---|
| 1110 | } |
---|
| 1111 | else |
---|
| 1112 | displayId = 0; |
---|
| 1113 | } |
---|
| 1114 | else |
---|
| 1115 | displayId = 0; |
---|
| 1116 | } |
---|
| 1117 | |
---|
| 1118 | // data->guid = guid don't must be update at save |
---|
| 1119 | data.id = GetEntry(); |
---|
| 1120 | data.mapid = mapid; |
---|
| 1121 | data.displayid = displayId; |
---|
| 1122 | data.equipmentId = GetEquipmentId(); |
---|
| 1123 | data.posX = GetPositionX(); |
---|
| 1124 | data.posY = GetPositionY(); |
---|
| 1125 | data.posZ = GetPositionZ(); |
---|
| 1126 | data.orientation = GetOrientation(); |
---|
| 1127 | data.spawntimesecs = m_respawnDelay; |
---|
| 1128 | // prevent add data integrity problems |
---|
| 1129 | data.spawndist = GetDefaultMovementType()==IDLE_MOTION_TYPE ? 0 : m_respawnradius; |
---|
| 1130 | data.currentwaypoint = 0; |
---|
| 1131 | data.curhealth = GetHealth(); |
---|
| 1132 | data.curmana = GetPower(POWER_MANA); |
---|
| 1133 | data.is_dead = m_isDeadByDefault; |
---|
| 1134 | // prevent add data integrity problems |
---|
| 1135 | data.movementType = !m_respawnradius && GetDefaultMovementType()==RANDOM_MOTION_TYPE |
---|
| 1136 | ? IDLE_MOTION_TYPE : GetDefaultMovementType(); |
---|
| 1137 | data.spawnMask = spawnMask; |
---|
| 1138 | |
---|
| 1139 | // updated in DB |
---|
| 1140 | WorldDatabase.BeginTransaction(); |
---|
| 1141 | |
---|
| 1142 | WorldDatabase.PExecuteLog("DELETE FROM creature WHERE guid = '%u'", m_DBTableGuid); |
---|
| 1143 | |
---|
| 1144 | std::ostringstream ss; |
---|
| 1145 | ss << "INSERT INTO creature VALUES (" |
---|
| 1146 | << m_DBTableGuid << "," |
---|
| 1147 | << GetEntry() << "," |
---|
| 1148 | << mapid <<"," |
---|
| 1149 | << (uint32)spawnMask << "," |
---|
| 1150 | << displayId <<"," |
---|
| 1151 | << GetEquipmentId() <<"," |
---|
| 1152 | << GetPositionX() << "," |
---|
| 1153 | << GetPositionY() << "," |
---|
| 1154 | << GetPositionZ() << "," |
---|
| 1155 | << GetOrientation() << "," |
---|
| 1156 | << m_respawnDelay << "," //respawn time |
---|
| 1157 | << (float) m_respawnradius << "," //spawn distance (float) |
---|
| 1158 | << (uint32) (0) << "," //currentwaypoint |
---|
| 1159 | << GetHealth() << "," //curhealth |
---|
| 1160 | << GetPower(POWER_MANA) << "," //curmana |
---|
| 1161 | << (m_isDeadByDefault ? 1 : 0) << "," //is_dead |
---|
| 1162 | << GetDefaultMovementType() << ")"; //default movement generator type |
---|
| 1163 | |
---|
| 1164 | WorldDatabase.PExecuteLog( ss.str( ).c_str( ) ); |
---|
| 1165 | |
---|
| 1166 | WorldDatabase.CommitTransaction(); |
---|
| 1167 | } |
---|
| 1168 | |
---|
| 1169 | void Creature::SelectLevel(const CreatureInfo *cinfo) |
---|
| 1170 | { |
---|
| 1171 | uint32 rank = isPet()? 0 : cinfo->rank; |
---|
| 1172 | |
---|
| 1173 | // level |
---|
| 1174 | uint32 minlevel = std::min(cinfo->maxlevel, cinfo->minlevel); |
---|
| 1175 | uint32 maxlevel = std::max(cinfo->maxlevel, cinfo->minlevel); |
---|
| 1176 | uint32 level = minlevel == maxlevel ? minlevel : urand(minlevel, maxlevel); |
---|
| 1177 | SetLevel(level); |
---|
| 1178 | |
---|
| 1179 | float rellevel = maxlevel == minlevel ? 0 : (float(level - minlevel))/(maxlevel - minlevel); |
---|
| 1180 | |
---|
| 1181 | // health |
---|
| 1182 | float healthmod = _GetHealthMod(rank); |
---|
| 1183 | |
---|
| 1184 | uint32 minhealth = std::min(cinfo->maxhealth, cinfo->minhealth); |
---|
| 1185 | uint32 maxhealth = std::max(cinfo->maxhealth, cinfo->minhealth); |
---|
| 1186 | uint32 health = uint32(healthmod * (minhealth + uint32(rellevel*(maxhealth - minhealth)))); |
---|
| 1187 | |
---|
| 1188 | SetCreateHealth(health); |
---|
| 1189 | SetMaxHealth(health); |
---|
| 1190 | SetHealth(health); |
---|
| 1191 | |
---|
| 1192 | // mana |
---|
| 1193 | uint32 minmana = std::min(cinfo->maxmana, cinfo->minmana); |
---|
| 1194 | uint32 maxmana = std::max(cinfo->maxmana, cinfo->minmana); |
---|
| 1195 | uint32 mana = minmana + uint32(rellevel*(maxmana - minmana)); |
---|
| 1196 | |
---|
| 1197 | SetCreateMana(mana); |
---|
| 1198 | SetMaxPower(POWER_MANA, mana); //MAX Mana |
---|
| 1199 | SetPower(POWER_MANA, mana); |
---|
| 1200 | |
---|
| 1201 | SetModifierValue(UNIT_MOD_HEALTH, BASE_VALUE, health); |
---|
| 1202 | SetModifierValue(UNIT_MOD_MANA, BASE_VALUE, mana); |
---|
| 1203 | |
---|
| 1204 | // damage |
---|
| 1205 | float damagemod = _GetDamageMod(rank); |
---|
| 1206 | |
---|
| 1207 | SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, cinfo->mindmg * damagemod); |
---|
| 1208 | SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, cinfo->maxdmg * damagemod); |
---|
| 1209 | |
---|
| 1210 | SetFloatValue(UNIT_FIELD_MINRANGEDDAMAGE,cinfo->minrangedmg * damagemod); |
---|
| 1211 | SetFloatValue(UNIT_FIELD_MAXRANGEDDAMAGE,cinfo->maxrangedmg * damagemod); |
---|
| 1212 | |
---|
| 1213 | SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, cinfo->attackpower * damagemod); |
---|
| 1214 | } |
---|
| 1215 | |
---|
| 1216 | float Creature::_GetHealthMod(int32 Rank) |
---|
| 1217 | { |
---|
| 1218 | switch (Rank) // define rates for each elite rank |
---|
| 1219 | { |
---|
| 1220 | case CREATURE_ELITE_NORMAL: |
---|
| 1221 | return sWorld.getRate(RATE_CREATURE_NORMAL_HP); |
---|
| 1222 | case CREATURE_ELITE_ELITE: |
---|
| 1223 | return sWorld.getRate(RATE_CREATURE_ELITE_ELITE_HP); |
---|
| 1224 | case CREATURE_ELITE_RAREELITE: |
---|
| 1225 | return sWorld.getRate(RATE_CREATURE_ELITE_RAREELITE_HP); |
---|
| 1226 | case CREATURE_ELITE_WORLDBOSS: |
---|
| 1227 | return sWorld.getRate(RATE_CREATURE_ELITE_WORLDBOSS_HP); |
---|
| 1228 | case CREATURE_ELITE_RARE: |
---|
| 1229 | return sWorld.getRate(RATE_CREATURE_ELITE_RARE_HP); |
---|
| 1230 | default: |
---|
| 1231 | return sWorld.getRate(RATE_CREATURE_ELITE_ELITE_HP); |
---|
| 1232 | } |
---|
| 1233 | } |
---|
| 1234 | |
---|
| 1235 | float Creature::_GetDamageMod(int32 Rank) |
---|
| 1236 | { |
---|
| 1237 | switch (Rank) // define rates for each elite rank |
---|
| 1238 | { |
---|
| 1239 | case CREATURE_ELITE_NORMAL: |
---|
| 1240 | return sWorld.getRate(RATE_CREATURE_NORMAL_DAMAGE); |
---|
| 1241 | case CREATURE_ELITE_ELITE: |
---|
| 1242 | return sWorld.getRate(RATE_CREATURE_ELITE_ELITE_DAMAGE); |
---|
| 1243 | case CREATURE_ELITE_RAREELITE: |
---|
| 1244 | return sWorld.getRate(RATE_CREATURE_ELITE_RAREELITE_DAMAGE); |
---|
| 1245 | case CREATURE_ELITE_WORLDBOSS: |
---|
| 1246 | return sWorld.getRate(RATE_CREATURE_ELITE_WORLDBOSS_DAMAGE); |
---|
| 1247 | case CREATURE_ELITE_RARE: |
---|
| 1248 | return sWorld.getRate(RATE_CREATURE_ELITE_RARE_DAMAGE); |
---|
| 1249 | default: |
---|
| 1250 | return sWorld.getRate(RATE_CREATURE_ELITE_ELITE_DAMAGE); |
---|
| 1251 | } |
---|
| 1252 | } |
---|
| 1253 | |
---|
| 1254 | float Creature::GetSpellDamageMod(int32 Rank) |
---|
| 1255 | { |
---|
| 1256 | switch (Rank) // define rates for each elite rank |
---|
| 1257 | { |
---|
| 1258 | case CREATURE_ELITE_NORMAL: |
---|
| 1259 | return sWorld.getRate(RATE_CREATURE_NORMAL_SPELLDAMAGE); |
---|
| 1260 | case CREATURE_ELITE_ELITE: |
---|
| 1261 | return sWorld.getRate(RATE_CREATURE_ELITE_ELITE_SPELLDAMAGE); |
---|
| 1262 | case CREATURE_ELITE_RAREELITE: |
---|
| 1263 | return sWorld.getRate(RATE_CREATURE_ELITE_RAREELITE_SPELLDAMAGE); |
---|
| 1264 | case CREATURE_ELITE_WORLDBOSS: |
---|
| 1265 | return sWorld.getRate(RATE_CREATURE_ELITE_WORLDBOSS_SPELLDAMAGE); |
---|
| 1266 | case CREATURE_ELITE_RARE: |
---|
| 1267 | return sWorld.getRate(RATE_CREATURE_ELITE_RARE_SPELLDAMAGE); |
---|
| 1268 | default: |
---|
| 1269 | return sWorld.getRate(RATE_CREATURE_ELITE_ELITE_SPELLDAMAGE); |
---|
| 1270 | } |
---|
| 1271 | } |
---|
| 1272 | |
---|
| 1273 | bool Creature::CreateFromProto(uint32 guidlow, uint32 Entry, uint32 team, const CreatureData *data) |
---|
| 1274 | { |
---|
| 1275 | CreatureInfo const *cinfo = objmgr.GetCreatureTemplate(Entry); |
---|
| 1276 | if(!cinfo) |
---|
| 1277 | { |
---|
| 1278 | sLog.outErrorDb("Error: creature entry %u does not exist.", Entry); |
---|
| 1279 | return false; |
---|
| 1280 | } |
---|
| 1281 | m_originalEntry = Entry; |
---|
| 1282 | |
---|
| 1283 | Object::_Create(guidlow, Entry, HIGHGUID_UNIT); |
---|
| 1284 | |
---|
| 1285 | m_DBTableGuid = guidlow; |
---|
| 1286 | if(!UpdateEntry(Entry, team, data)) |
---|
| 1287 | return false; |
---|
| 1288 | |
---|
| 1289 | //Notify the map's instance data. |
---|
| 1290 | //Only works if you create the object in it, not if it is moves to that map. |
---|
| 1291 | //Normally non-players do not teleport to other maps. |
---|
| 1292 | Map *map = MapManager::Instance().FindMap(GetMapId(), GetInstanceId()); |
---|
| 1293 | if(map && map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData()) |
---|
| 1294 | { |
---|
| 1295 | ((InstanceMap*)map)->GetInstanceData()->OnCreatureCreate(this, Entry); |
---|
| 1296 | } |
---|
| 1297 | |
---|
| 1298 | return true; |
---|
| 1299 | } |
---|
| 1300 | |
---|
| 1301 | bool Creature::LoadFromDB(uint32 guid, Map *map) |
---|
| 1302 | { |
---|
| 1303 | CreatureData const* data = objmgr.GetCreatureData(guid); |
---|
| 1304 | |
---|
| 1305 | if(!data) |
---|
| 1306 | { |
---|
| 1307 | sLog.outErrorDb("Creature (GUID: %u) not found in table `creature`, can't load. ",guid); |
---|
| 1308 | return false; |
---|
| 1309 | } |
---|
| 1310 | |
---|
| 1311 | uint32 stored_guid = guid; |
---|
| 1312 | if (map->GetInstanceId() != 0) guid = objmgr.GenerateLowGuid(HIGHGUID_UNIT); |
---|
| 1313 | |
---|
| 1314 | uint16 team = 0; |
---|
| 1315 | if(!Create(guid,map,data->id,team,data)) |
---|
| 1316 | return false; |
---|
| 1317 | |
---|
| 1318 | Relocate(data->posX,data->posY,data->posZ,data->orientation); |
---|
| 1319 | |
---|
| 1320 | if(!IsPositionValid()) |
---|
| 1321 | { |
---|
| 1322 | sLog.outError("ERROR: Creature (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",GetGUIDLow(),GetEntry(),GetPositionX(),GetPositionY()); |
---|
| 1323 | return false; |
---|
| 1324 | } |
---|
| 1325 | |
---|
| 1326 | m_DBTableGuid = stored_guid; |
---|
| 1327 | LoadCreaturesAddon(); |
---|
| 1328 | |
---|
| 1329 | m_respawnradius = data->spawndist; |
---|
| 1330 | |
---|
| 1331 | m_respawnDelay = data->spawntimesecs; |
---|
| 1332 | m_isDeadByDefault = data->is_dead; |
---|
| 1333 | m_deathState = m_isDeadByDefault ? DEAD : ALIVE; |
---|
| 1334 | |
---|
| 1335 | m_respawnTime = objmgr.GetCreatureRespawnTime(m_DBTableGuid,GetInstanceId()); |
---|
| 1336 | if(m_respawnTime > time(NULL)) // not ready to respawn |
---|
| 1337 | m_deathState = DEAD; |
---|
| 1338 | else if(m_respawnTime) // respawn time set but expired |
---|
| 1339 | { |
---|
| 1340 | m_respawnTime = 0; |
---|
| 1341 | objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),0); |
---|
| 1342 | } |
---|
| 1343 | |
---|
| 1344 | uint32 curhealth = data->curhealth; |
---|
| 1345 | if(curhealth) |
---|
| 1346 | { |
---|
| 1347 | curhealth = uint32(curhealth*_GetHealthMod(GetCreatureInfo()->rank)); |
---|
| 1348 | if(curhealth < 1) |
---|
| 1349 | curhealth = 1; |
---|
| 1350 | } |
---|
| 1351 | |
---|
| 1352 | SetHealth(m_deathState == ALIVE ? curhealth : 0); |
---|
| 1353 | SetPower(POWER_MANA,data->curmana); |
---|
| 1354 | |
---|
| 1355 | SetMeleeDamageSchool(SpellSchools(GetCreatureInfo()->dmgschool)); |
---|
| 1356 | |
---|
| 1357 | // checked at creature_template loading |
---|
| 1358 | m_defaultMovementType = MovementGeneratorType(data->movementType); |
---|
| 1359 | |
---|
| 1360 | AIM_Initialize(); |
---|
| 1361 | return true; |
---|
| 1362 | } |
---|
| 1363 | |
---|
| 1364 | void Creature::LoadEquipment(uint32 equip_entry, bool force) |
---|
| 1365 | { |
---|
| 1366 | if(equip_entry == 0) |
---|
| 1367 | { |
---|
| 1368 | if (force) |
---|
| 1369 | { |
---|
| 1370 | for (uint8 i=0;i<3;i++) |
---|
| 1371 | { |
---|
| 1372 | SetUInt32Value( UNIT_VIRTUAL_ITEM_SLOT_DISPLAY + i, 0); |
---|
| 1373 | SetUInt32Value( UNIT_VIRTUAL_ITEM_INFO + (i * 2), 0); |
---|
| 1374 | SetUInt32Value( UNIT_VIRTUAL_ITEM_INFO + (i * 2) + 1, 0); |
---|
| 1375 | } |
---|
| 1376 | m_equipmentId = 0; |
---|
| 1377 | } |
---|
| 1378 | return; |
---|
| 1379 | } |
---|
| 1380 | |
---|
| 1381 | EquipmentInfo const *einfo = objmgr.GetEquipmentInfo(equip_entry); |
---|
| 1382 | if (!einfo) |
---|
| 1383 | return; |
---|
| 1384 | |
---|
| 1385 | m_equipmentId = equip_entry; |
---|
| 1386 | for (uint8 i=0;i<3;i++) |
---|
| 1387 | { |
---|
| 1388 | SetUInt32Value( UNIT_VIRTUAL_ITEM_SLOT_DISPLAY + i, einfo->equipmodel[i]); |
---|
| 1389 | SetUInt32Value( UNIT_VIRTUAL_ITEM_INFO + (i * 2), einfo->equipinfo[i]); |
---|
| 1390 | SetUInt32Value( UNIT_VIRTUAL_ITEM_INFO + (i * 2) + 1, einfo->equipslot[i]); |
---|
| 1391 | } |
---|
| 1392 | } |
---|
| 1393 | |
---|
| 1394 | void Creature::LoadGoods() |
---|
| 1395 | { |
---|
| 1396 | // already loaded; |
---|
| 1397 | if(m_itemsLoaded) |
---|
| 1398 | return; |
---|
| 1399 | |
---|
| 1400 | m_vendor_items.clear(); |
---|
| 1401 | |
---|
| 1402 | QueryResult *result = WorldDatabase.PQuery("SELECT item, maxcount, incrtime, ExtendedCost FROM npc_vendor WHERE entry = '%u'", GetEntry()); |
---|
| 1403 | |
---|
| 1404 | if(!result) |
---|
| 1405 | return; |
---|
| 1406 | |
---|
| 1407 | do |
---|
| 1408 | { |
---|
| 1409 | Field *fields = result->Fetch(); |
---|
| 1410 | |
---|
| 1411 | if (GetItemCount() >= MAX_VENDOR_ITEMS) |
---|
| 1412 | { |
---|
| 1413 | sLog.outErrorDb( "Vendor %u has too many items (%u >= %i). Check the DB!", GetEntry(), GetItemCount(), MAX_VENDOR_ITEMS ); |
---|
| 1414 | break; |
---|
| 1415 | } |
---|
| 1416 | |
---|
| 1417 | uint32 item_id = fields[0].GetUInt32(); |
---|
| 1418 | if(!sItemStorage.LookupEntry<ItemPrototype>(item_id)) |
---|
| 1419 | { |
---|
| 1420 | sLog.outErrorDb("Vendor %u have in item list non-existed item %u",GetEntry(),item_id); |
---|
| 1421 | continue; |
---|
| 1422 | } |
---|
| 1423 | |
---|
| 1424 | uint32 ExtendedCost = fields[3].GetUInt32(); |
---|
| 1425 | if(ExtendedCost && !sItemExtendedCostStore.LookupEntry(ExtendedCost)) |
---|
| 1426 | sLog.outErrorDb("Item (Entry: %u) has wrong ExtendedCost (%u) for vendor (%u)",item_id,ExtendedCost,GetEntry()); |
---|
| 1427 | |
---|
| 1428 | AddItem( item_id, fields[1].GetUInt32(), fields[2].GetUInt32(), ExtendedCost); |
---|
| 1429 | } |
---|
| 1430 | while( result->NextRow() ); |
---|
| 1431 | |
---|
| 1432 | delete result; |
---|
| 1433 | |
---|
| 1434 | m_itemsLoaded = true; |
---|
| 1435 | } |
---|
| 1436 | |
---|
| 1437 | bool Creature::hasQuest(uint32 quest_id) const |
---|
| 1438 | { |
---|
| 1439 | QuestRelations const& qr = objmgr.mCreatureQuestRelations; |
---|
| 1440 | for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr) |
---|
| 1441 | { |
---|
| 1442 | if(itr->second==quest_id) |
---|
| 1443 | return true; |
---|
| 1444 | } |
---|
| 1445 | return false; |
---|
| 1446 | } |
---|
| 1447 | |
---|
| 1448 | bool Creature::hasInvolvedQuest(uint32 quest_id) const |
---|
| 1449 | { |
---|
| 1450 | QuestRelations const& qr = objmgr.mCreatureQuestInvolvedRelations; |
---|
| 1451 | for(QuestRelations::const_iterator itr = qr.lower_bound(GetEntry()); itr != qr.upper_bound(GetEntry()); ++itr) |
---|
| 1452 | { |
---|
| 1453 | if(itr->second==quest_id) |
---|
| 1454 | return true; |
---|
| 1455 | } |
---|
| 1456 | return false; |
---|
| 1457 | } |
---|
| 1458 | |
---|
| 1459 | void Creature::DeleteFromDB() |
---|
| 1460 | { |
---|
| 1461 | objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),0); |
---|
| 1462 | objmgr.DeleteCreatureData(m_DBTableGuid); |
---|
| 1463 | |
---|
| 1464 | WorldDatabase.BeginTransaction(); |
---|
| 1465 | WorldDatabase.PExecuteLog("DELETE FROM creature WHERE guid = '%u'", m_DBTableGuid); |
---|
| 1466 | WorldDatabase.PExecuteLog("DELETE FROM creature_addon WHERE guid = '%u'", m_DBTableGuid); |
---|
| 1467 | WorldDatabase.PExecuteLog("DELETE FROM creature_movement WHERE id = '%u'", m_DBTableGuid); |
---|
| 1468 | WorldDatabase.PExecuteLog("DELETE FROM game_event_creature WHERE guid = '%u'", m_DBTableGuid); |
---|
| 1469 | WorldDatabase.PExecuteLog("DELETE FROM game_event_model_equip WHERE guid = '%u'", m_DBTableGuid); |
---|
| 1470 | WorldDatabase.CommitTransaction(); |
---|
| 1471 | } |
---|
| 1472 | |
---|
| 1473 | float Creature::GetAttackDistance(Unit const* pl) const |
---|
| 1474 | { |
---|
| 1475 | float aggroRate = sWorld.getRate(RATE_CREATURE_AGGRO); |
---|
| 1476 | if(aggroRate==0) |
---|
| 1477 | return 0.0f; |
---|
| 1478 | |
---|
| 1479 | int32 playerlevel = pl->getLevelForTarget(this); |
---|
| 1480 | int32 creaturelevel = getLevelForTarget(pl); |
---|
| 1481 | |
---|
| 1482 | int32 leveldif = playerlevel - creaturelevel; |
---|
| 1483 | |
---|
| 1484 | // "The maximum Aggro Radius has a cap of 25 levels under. Example: A level 30 char has the same Aggro Radius of a level 5 char on a level 60 mob." |
---|
| 1485 | if ( leveldif < - 25) |
---|
| 1486 | leveldif = -25; |
---|
| 1487 | |
---|
| 1488 | // "The aggro radius of a mob having the same level as the player is roughly 20 yards" |
---|
| 1489 | float RetDistance = 20; |
---|
| 1490 | |
---|
| 1491 | // "Aggro Radius varries with level difference at a rate of roughly 1 yard/level" |
---|
| 1492 | // radius grow if playlevel < creaturelevel |
---|
| 1493 | RetDistance -= (float)leveldif; |
---|
| 1494 | |
---|
| 1495 | if(creaturelevel+5 <= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL)) |
---|
| 1496 | { |
---|
| 1497 | // detect range auras |
---|
| 1498 | RetDistance += GetTotalAuraModifier(SPELL_AURA_MOD_DETECT_RANGE); |
---|
| 1499 | |
---|
| 1500 | // detected range auras |
---|
| 1501 | RetDistance += pl->GetTotalAuraModifier(SPELL_AURA_MOD_DETECTED_RANGE); |
---|
| 1502 | } |
---|
| 1503 | |
---|
| 1504 | // "Minimum Aggro Radius for a mob seems to be combat range (5 yards)" |
---|
| 1505 | if(RetDistance < 5) |
---|
| 1506 | RetDistance = 5; |
---|
| 1507 | |
---|
| 1508 | return (RetDistance*aggroRate); |
---|
| 1509 | } |
---|
| 1510 | |
---|
| 1511 | void Creature::setDeathState(DeathState s) |
---|
| 1512 | { |
---|
| 1513 | if((s == JUST_DIED && !m_isDeadByDefault)||(s == JUST_ALIVED && m_isDeadByDefault)) |
---|
| 1514 | { |
---|
| 1515 | m_deathTimer = m_corpseDelay*1000; |
---|
| 1516 | |
---|
| 1517 | // always save boss respawn time at death to prevent crash cheating |
---|
| 1518 | if(sWorld.getConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATLY) || isWorldBoss()) |
---|
| 1519 | SaveRespawnTime(); |
---|
| 1520 | |
---|
| 1521 | if(!IsStopped()) |
---|
| 1522 | StopMoving(); |
---|
| 1523 | } |
---|
| 1524 | Unit::setDeathState(s); |
---|
| 1525 | |
---|
| 1526 | if(s == JUST_DIED) |
---|
| 1527 | { |
---|
| 1528 | SetUInt64Value (UNIT_FIELD_TARGET,0); // remove target selection in any cases (can be set at aura remove in Unit::setDeathState) |
---|
| 1529 | SetUInt32Value(UNIT_NPC_FLAGS, 0); |
---|
| 1530 | |
---|
| 1531 | if(!isPet() && GetCreatureInfo()->SkinLootId) |
---|
| 1532 | if ( LootTemplates_Skinning.HaveLootFor(GetCreatureInfo()->SkinLootId) ) |
---|
| 1533 | SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE); |
---|
| 1534 | |
---|
| 1535 | Unit::setDeathState(CORPSE); |
---|
| 1536 | } |
---|
| 1537 | if(s == JUST_ALIVED) |
---|
| 1538 | { |
---|
| 1539 | SetHealth(GetMaxHealth()); |
---|
| 1540 | SetLootRecipient(NULL); |
---|
| 1541 | Unit::setDeathState(ALIVE); |
---|
| 1542 | CreatureInfo const *cinfo = GetCreatureInfo(); |
---|
| 1543 | SetUInt32Value(UNIT_DYNAMIC_FLAGS, 0); |
---|
| 1544 | RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE); |
---|
| 1545 | AddUnitMovementFlag(MOVEMENTFLAG_WALK_MODE); |
---|
| 1546 | SetUInt32Value(UNIT_NPC_FLAGS, cinfo->npcflag); |
---|
| 1547 | clearUnitState(UNIT_STAT_ALL_STATE); |
---|
| 1548 | i_motionMaster.Clear(); |
---|
| 1549 | SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool)); |
---|
| 1550 | LoadCreaturesAddon(true); |
---|
| 1551 | } |
---|
| 1552 | } |
---|
| 1553 | |
---|
| 1554 | void Creature::Respawn() |
---|
| 1555 | { |
---|
| 1556 | RemoveCorpse(); |
---|
| 1557 | |
---|
| 1558 | // forced recreate creature object at clients |
---|
| 1559 | UnitVisibility currentVis = GetVisibility(); |
---|
| 1560 | SetVisibility(VISIBILITY_RESPAWN); |
---|
| 1561 | ObjectAccessor::UpdateObjectVisibility(this); |
---|
| 1562 | SetVisibility(currentVis); // restore visibility state |
---|
| 1563 | ObjectAccessor::UpdateObjectVisibility(this); |
---|
| 1564 | |
---|
| 1565 | if(getDeathState()==DEAD) |
---|
| 1566 | { |
---|
| 1567 | objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),0); |
---|
| 1568 | m_respawnTime = time(NULL); // respawn at next tick |
---|
| 1569 | } |
---|
| 1570 | } |
---|
| 1571 | |
---|
| 1572 | bool Creature::IsImmunedToSpell(SpellEntry const* spellInfo, bool useCharges) |
---|
| 1573 | { |
---|
| 1574 | if (!spellInfo) |
---|
| 1575 | return false; |
---|
| 1576 | |
---|
| 1577 | if (GetCreatureInfo()->MechanicImmuneMask & (1 << (spellInfo->Mechanic - 1))) |
---|
| 1578 | return true; |
---|
| 1579 | |
---|
| 1580 | return Unit::IsImmunedToSpell(spellInfo, useCharges); |
---|
| 1581 | } |
---|
| 1582 | |
---|
| 1583 | bool Creature::IsImmunedToSpellEffect(uint32 effect, uint32 mechanic) const |
---|
| 1584 | { |
---|
| 1585 | if (GetCreatureInfo()->MechanicImmuneMask & (1 << (mechanic-1))) |
---|
| 1586 | return true; |
---|
| 1587 | |
---|
| 1588 | return Unit::IsImmunedToSpellEffect(effect, mechanic); |
---|
| 1589 | } |
---|
| 1590 | |
---|
| 1591 | SpellEntry const *Creature::reachWithSpellAttack(Unit *pVictim) |
---|
| 1592 | { |
---|
| 1593 | if(!pVictim) |
---|
| 1594 | return NULL; |
---|
| 1595 | |
---|
| 1596 | for(uint32 i=0; i < CREATURE_MAX_SPELLS; i++) |
---|
| 1597 | { |
---|
| 1598 | if(!m_spells[i]) |
---|
| 1599 | continue; |
---|
| 1600 | SpellEntry const *spellInfo = sSpellStore.LookupEntry(m_spells[i] ); |
---|
| 1601 | if(!spellInfo) |
---|
| 1602 | { |
---|
| 1603 | sLog.outError("WORLD: unknown spell id %i\n", m_spells[i]); |
---|
| 1604 | continue; |
---|
| 1605 | } |
---|
| 1606 | |
---|
| 1607 | bool bcontinue = true; |
---|
| 1608 | for(uint32 j=0;j<3;j++) |
---|
| 1609 | { |
---|
| 1610 | if( (spellInfo->Effect[j] == SPELL_EFFECT_SCHOOL_DAMAGE ) || |
---|
| 1611 | (spellInfo->Effect[j] == SPELL_EFFECT_INSTAKILL) || |
---|
| 1612 | (spellInfo->Effect[j] == SPELL_EFFECT_ENVIRONMENTAL_DAMAGE) || |
---|
| 1613 | (spellInfo->Effect[j] == SPELL_EFFECT_HEALTH_LEECH ) |
---|
| 1614 | ) |
---|
| 1615 | { |
---|
| 1616 | bcontinue = false; |
---|
| 1617 | break; |
---|
| 1618 | } |
---|
| 1619 | } |
---|
| 1620 | if(bcontinue) continue; |
---|
| 1621 | |
---|
| 1622 | if(spellInfo->manaCost > GetPower(POWER_MANA)) |
---|
| 1623 | continue; |
---|
| 1624 | SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex); |
---|
| 1625 | float range = GetSpellMaxRange(srange); |
---|
| 1626 | float minrange = GetSpellMinRange(srange); |
---|
| 1627 | float dist = GetDistance(pVictim); |
---|
| 1628 | //if(!isInFront( pVictim, range ) && spellInfo->AttributesEx ) |
---|
| 1629 | // continue; |
---|
| 1630 | if( dist > range || dist < minrange ) |
---|
| 1631 | continue; |
---|
| 1632 | if(HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SILENCED)) |
---|
| 1633 | continue; |
---|
| 1634 | return spellInfo; |
---|
| 1635 | } |
---|
| 1636 | return NULL; |
---|
| 1637 | } |
---|
| 1638 | |
---|
| 1639 | SpellEntry const *Creature::reachWithSpellCure(Unit *pVictim) |
---|
| 1640 | { |
---|
| 1641 | if(!pVictim) |
---|
| 1642 | return NULL; |
---|
| 1643 | |
---|
| 1644 | for(uint32 i=0; i < CREATURE_MAX_SPELLS; i++) |
---|
| 1645 | { |
---|
| 1646 | if(!m_spells[i]) |
---|
| 1647 | continue; |
---|
| 1648 | SpellEntry const *spellInfo = sSpellStore.LookupEntry(m_spells[i] ); |
---|
| 1649 | if(!spellInfo) |
---|
| 1650 | { |
---|
| 1651 | sLog.outError("WORLD: unknown spell id %i\n", m_spells[i]); |
---|
| 1652 | continue; |
---|
| 1653 | } |
---|
| 1654 | |
---|
| 1655 | bool bcontinue = true; |
---|
| 1656 | for(uint32 j=0;j<3;j++) |
---|
| 1657 | { |
---|
| 1658 | if( (spellInfo->Effect[j] == SPELL_EFFECT_HEAL ) ) |
---|
| 1659 | { |
---|
| 1660 | bcontinue = false; |
---|
| 1661 | break; |
---|
| 1662 | } |
---|
| 1663 | } |
---|
| 1664 | if(bcontinue) continue; |
---|
| 1665 | |
---|
| 1666 | if(spellInfo->manaCost > GetPower(POWER_MANA)) |
---|
| 1667 | continue; |
---|
| 1668 | SpellRangeEntry const* srange = sSpellRangeStore.LookupEntry(spellInfo->rangeIndex); |
---|
| 1669 | float range = GetSpellMaxRange(srange); |
---|
| 1670 | float minrange = GetSpellMinRange(srange); |
---|
| 1671 | float dist = GetDistance(pVictim); |
---|
| 1672 | //if(!isInFront( pVictim, range ) && spellInfo->AttributesEx ) |
---|
| 1673 | // continue; |
---|
| 1674 | if( dist > range || dist < minrange ) |
---|
| 1675 | continue; |
---|
| 1676 | if(HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SILENCED)) |
---|
| 1677 | continue; |
---|
| 1678 | return spellInfo; |
---|
| 1679 | } |
---|
| 1680 | return NULL; |
---|
| 1681 | } |
---|
| 1682 | |
---|
| 1683 | bool Creature::IsVisibleInGridForPlayer(Player* pl) const |
---|
| 1684 | { |
---|
| 1685 | // gamemaster in GM mode see all, including ghosts |
---|
| 1686 | if(pl->isGameMaster()) |
---|
| 1687 | return true; |
---|
| 1688 | |
---|
| 1689 | // Live player (or with not release body see live creatures or death creatures with corpse disappearing time > 0 |
---|
| 1690 | if(pl->isAlive() || pl->GetDeathTimer() > 0) |
---|
| 1691 | { |
---|
| 1692 | if( GetEntry() == VISUAL_WAYPOINT && !pl->isGameMaster() ) |
---|
| 1693 | return false; |
---|
| 1694 | return isAlive() || m_deathTimer > 0 || m_isDeadByDefault && m_deathState==CORPSE; |
---|
| 1695 | } |
---|
| 1696 | |
---|
| 1697 | // Dead player see live creatures near own corpse |
---|
| 1698 | if(isAlive()) |
---|
| 1699 | { |
---|
| 1700 | Corpse *corpse = pl->GetCorpse(); |
---|
| 1701 | if(corpse) |
---|
| 1702 | { |
---|
| 1703 | // 20 - aggro distance for same level, 25 - max additional distance if player level less that creature level |
---|
| 1704 | if(corpse->IsWithinDistInMap(this,(20+25)*sWorld.getRate(RATE_CREATURE_AGGRO))) |
---|
| 1705 | return true; |
---|
| 1706 | } |
---|
| 1707 | } |
---|
| 1708 | |
---|
| 1709 | // Dead player see Spirit Healer or Spirit Guide |
---|
| 1710 | if(isSpiritService()) |
---|
| 1711 | return true; |
---|
| 1712 | |
---|
| 1713 | // and not see any other |
---|
| 1714 | return false; |
---|
| 1715 | } |
---|
| 1716 | |
---|
| 1717 | void Creature::CallAssistence() |
---|
| 1718 | { |
---|
| 1719 | if( !m_AlreadyCallAssistence && getVictim() && !isPet() && !isCharmed()) |
---|
| 1720 | { |
---|
| 1721 | SetNoCallAssistence(true); |
---|
| 1722 | |
---|
| 1723 | float radius = sWorld.getConfig(CONFIG_CREATURE_FAMILY_ASSISTEMCE_RADIUS); |
---|
| 1724 | if(radius > 0) |
---|
| 1725 | { |
---|
| 1726 | std::list<Creature*> assistList; |
---|
| 1727 | |
---|
| 1728 | { |
---|
| 1729 | CellPair p(MaNGOS::ComputeCellPair(GetPositionX(), GetPositionY())); |
---|
| 1730 | Cell cell(p); |
---|
| 1731 | cell.data.Part.reserved = ALL_DISTRICT; |
---|
| 1732 | cell.SetNoCreate(); |
---|
| 1733 | |
---|
| 1734 | MaNGOS::AnyAssistCreatureInRangeCheck u_check(this, getVictim(), radius); |
---|
| 1735 | MaNGOS::CreatureListSearcher<MaNGOS::AnyAssistCreatureInRangeCheck> searcher(assistList, u_check); |
---|
| 1736 | |
---|
| 1737 | TypeContainerVisitor<MaNGOS::CreatureListSearcher<MaNGOS::AnyAssistCreatureInRangeCheck>, GridTypeMapContainer > grid_creature_searcher(searcher); |
---|
| 1738 | |
---|
| 1739 | CellLock<GridReadGuard> cell_lock(cell, p); |
---|
| 1740 | cell_lock->Visit(cell_lock, grid_creature_searcher, *MapManager::Instance().GetMap(GetMapId(), this)); |
---|
| 1741 | } |
---|
| 1742 | |
---|
| 1743 | for(std::list<Creature*>::iterator iter = assistList.begin(); iter != assistList.end(); ++iter) |
---|
| 1744 | { |
---|
| 1745 | (*iter)->SetNoCallAssistence(true); |
---|
| 1746 | if((*iter)->AI()) |
---|
| 1747 | (*iter)->AI()->AttackStart(getVictim()); |
---|
| 1748 | } |
---|
| 1749 | } |
---|
| 1750 | } |
---|
| 1751 | } |
---|
| 1752 | |
---|
| 1753 | void Creature::SaveRespawnTime() |
---|
| 1754 | { |
---|
| 1755 | if(isPet()) |
---|
| 1756 | return; |
---|
| 1757 | |
---|
| 1758 | if(m_respawnTime > time(NULL)) // dead (no corpse) |
---|
| 1759 | objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),m_respawnTime); |
---|
| 1760 | else if(m_deathTimer > 0) // dead (corpse) |
---|
| 1761 | objmgr.SaveCreatureRespawnTime(m_DBTableGuid,GetInstanceId(),time(NULL)+m_respawnDelay+m_deathTimer/1000); |
---|
| 1762 | } |
---|
| 1763 | |
---|
| 1764 | bool Creature::IsOutOfThreatArea(Unit* pVictim) const |
---|
| 1765 | { |
---|
| 1766 | if(!pVictim) |
---|
| 1767 | return true; |
---|
| 1768 | |
---|
| 1769 | if(!pVictim->IsInMap(this)) |
---|
| 1770 | return true; |
---|
| 1771 | |
---|
| 1772 | if(!pVictim->isTargetableForAttack()) |
---|
| 1773 | return true; |
---|
| 1774 | |
---|
| 1775 | if(!pVictim->isInAccessablePlaceFor(this)) |
---|
| 1776 | return true; |
---|
| 1777 | |
---|
| 1778 | if(sMapStore.LookupEntry(GetMapId())->Instanceable()) |
---|
| 1779 | return false; |
---|
| 1780 | |
---|
| 1781 | float length = pVictim->GetDistance(CombatStartX,CombatStartY,CombatStartZ); |
---|
| 1782 | float AttackDist = GetAttackDistance(pVictim); |
---|
| 1783 | uint32 ThreatRadius = sWorld.getConfig(CONFIG_THREAT_RADIUS); |
---|
| 1784 | |
---|
| 1785 | //Use AttackDistance in distance check if threat radius is lower. This prevents creature bounce in and ouf of combat every update tick. |
---|
| 1786 | return ( length > (ThreatRadius > AttackDist ? ThreatRadius : AttackDist)); |
---|
| 1787 | } |
---|
| 1788 | |
---|
| 1789 | CreatureDataAddon const* Creature::GetCreatureAddon() const |
---|
| 1790 | { |
---|
| 1791 | if(CreatureDataAddon const* addon = ObjectMgr::GetCreatureAddon(m_DBTableGuid)) |
---|
| 1792 | return addon; |
---|
| 1793 | |
---|
| 1794 | // dependent from heroic mode entry |
---|
| 1795 | return ObjectMgr::GetCreatureTemplateAddon(GetCreatureInfo()->Entry); |
---|
| 1796 | } |
---|
| 1797 | |
---|
| 1798 | //creature_addon table |
---|
| 1799 | bool Creature::LoadCreaturesAddon(bool reload) |
---|
| 1800 | { |
---|
| 1801 | CreatureDataAddon const *cainfo = GetCreatureAddon(); |
---|
| 1802 | if(!cainfo) |
---|
| 1803 | return false; |
---|
| 1804 | |
---|
| 1805 | if (cainfo->mount != 0) |
---|
| 1806 | Mount(cainfo->mount); |
---|
| 1807 | |
---|
| 1808 | if (cainfo->bytes0 != 0) |
---|
| 1809 | SetUInt32Value(UNIT_FIELD_BYTES_0, cainfo->bytes0); |
---|
| 1810 | |
---|
| 1811 | if (cainfo->bytes1 != 0) |
---|
| 1812 | SetUInt32Value(UNIT_FIELD_BYTES_1, cainfo->bytes1); |
---|
| 1813 | |
---|
| 1814 | if (cainfo->bytes2 != 0) |
---|
| 1815 | SetUInt32Value(UNIT_FIELD_BYTES_2, cainfo->bytes2); |
---|
| 1816 | |
---|
| 1817 | if (cainfo->emote != 0) |
---|
| 1818 | SetUInt32Value(UNIT_NPC_EMOTESTATE, cainfo->emote); |
---|
| 1819 | |
---|
| 1820 | if (cainfo->move_flags != 0) |
---|
| 1821 | SetUnitMovementFlags(cainfo->move_flags); |
---|
| 1822 | |
---|
| 1823 | if(cainfo->auras) |
---|
| 1824 | { |
---|
| 1825 | for (CreatureDataAddonAura const* cAura = cainfo->auras; cAura->spell_id; ++cAura) |
---|
| 1826 | { |
---|
| 1827 | SpellEntry const *AdditionalSpellInfo = sSpellStore.LookupEntry(cAura->spell_id); |
---|
| 1828 | if (!AdditionalSpellInfo) |
---|
| 1829 | { |
---|
| 1830 | sLog.outErrorDb("Creature (GUIDLow: %u Entry: %u ) has wrong spell %u defined in `auras` field.",GetGUIDLow(),GetEntry(),cAura->spell_id); |
---|
| 1831 | continue; |
---|
| 1832 | } |
---|
| 1833 | |
---|
| 1834 | // skip already applied aura |
---|
| 1835 | if(HasAura(cAura->spell_id,cAura->effect_idx)) |
---|
| 1836 | { |
---|
| 1837 | if(!reload) |
---|
| 1838 | sLog.outErrorDb("Creature (GUIDLow: %u Entry: %u ) has duplicate aura (spell %u effect %u) in `auras` field.",GetGUIDLow(),GetEntry(),cAura->spell_id,cAura->effect_idx); |
---|
| 1839 | |
---|
| 1840 | continue; |
---|
| 1841 | } |
---|
| 1842 | |
---|
| 1843 | Aura* AdditionalAura = CreateAura(AdditionalSpellInfo, cAura->effect_idx, NULL, this, this, 0); |
---|
| 1844 | AddAura(AdditionalAura); |
---|
| 1845 | sLog.outDebug("Spell: %u with Aura %u added to creature (GUIDLow: %u Entry: %u )", cAura->spell_id, AdditionalSpellInfo->EffectApplyAuraName[0],GetGUIDLow(),GetEntry()); |
---|
| 1846 | } |
---|
| 1847 | } |
---|
| 1848 | return true; |
---|
| 1849 | } |
---|
| 1850 | |
---|
| 1851 | /// Send a message to LocalDefense channel for players oposition team in the zone |
---|
| 1852 | void Creature::SendZoneUnderAttackMessage(Player* attacker) |
---|
| 1853 | { |
---|
| 1854 | uint32 enemy_team = attacker->GetTeam(); |
---|
| 1855 | |
---|
| 1856 | WorldPacket data(SMSG_ZONE_UNDER_ATTACK,4); |
---|
| 1857 | data << (uint32)GetZoneId(); |
---|
| 1858 | sWorld.SendGlobalMessage(&data,NULL,(enemy_team==ALLIANCE ? HORDE : ALLIANCE)); |
---|
| 1859 | } |
---|
| 1860 | |
---|
| 1861 | void Creature::_AddCreatureSpellCooldown(uint32 spell_id, time_t end_time) |
---|
| 1862 | { |
---|
| 1863 | m_CreatureSpellCooldowns[spell_id] = end_time; |
---|
| 1864 | } |
---|
| 1865 | |
---|
| 1866 | void Creature::_AddCreatureCategoryCooldown(uint32 category, time_t apply_time) |
---|
| 1867 | { |
---|
| 1868 | m_CreatureCategoryCooldowns[category] = apply_time; |
---|
| 1869 | } |
---|
| 1870 | |
---|
| 1871 | void Creature::AddCreatureSpellCooldown(uint32 spellid) |
---|
| 1872 | { |
---|
| 1873 | SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid); |
---|
| 1874 | if(!spellInfo) |
---|
| 1875 | return; |
---|
| 1876 | |
---|
| 1877 | uint32 cooldown = GetSpellRecoveryTime(spellInfo); |
---|
| 1878 | if(cooldown) |
---|
| 1879 | _AddCreatureSpellCooldown(spellid, time(NULL) + cooldown/1000); |
---|
| 1880 | |
---|
| 1881 | if(spellInfo->Category) |
---|
| 1882 | _AddCreatureCategoryCooldown(spellInfo->Category, time(NULL)); |
---|
| 1883 | |
---|
| 1884 | m_GlobalCooldown = spellInfo->StartRecoveryTime; |
---|
| 1885 | } |
---|
| 1886 | |
---|
| 1887 | bool Creature::HasCategoryCooldown(uint32 spell_id) const |
---|
| 1888 | { |
---|
| 1889 | SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id); |
---|
| 1890 | if(!spellInfo) |
---|
| 1891 | return false; |
---|
| 1892 | |
---|
| 1893 | // check global cooldown if spell affected by it |
---|
| 1894 | if (spellInfo->StartRecoveryCategory > 0 && m_GlobalCooldown > 0) |
---|
| 1895 | return true; |
---|
| 1896 | |
---|
| 1897 | CreatureSpellCooldowns::const_iterator itr = m_CreatureCategoryCooldowns.find(spellInfo->Category); |
---|
| 1898 | return(itr != m_CreatureCategoryCooldowns.end() && time_t(itr->second + (spellInfo->CategoryRecoveryTime / 1000)) > time(NULL)); |
---|
| 1899 | } |
---|
| 1900 | |
---|
| 1901 | bool Creature::HasSpellCooldown(uint32 spell_id) const |
---|
| 1902 | { |
---|
| 1903 | CreatureSpellCooldowns::const_iterator itr = m_CreatureSpellCooldowns.find(spell_id); |
---|
| 1904 | return (itr != m_CreatureSpellCooldowns.end() && itr->second > time(NULL)) || HasCategoryCooldown(spell_id); |
---|
| 1905 | } |
---|
| 1906 | |
---|
| 1907 | bool Creature::IsInEvadeMode() const |
---|
| 1908 | { |
---|
| 1909 | return !i_motionMaster.empty() && i_motionMaster.GetCurrentMovementGeneratorType() == HOME_MOTION_TYPE; |
---|
| 1910 | } |
---|
| 1911 | |
---|
| 1912 | bool Creature::HasSpell(uint32 spellID) const |
---|
| 1913 | { |
---|
| 1914 | uint8 i; |
---|
| 1915 | for(i = 0; i < CREATURE_MAX_SPELLS; ++i) |
---|
| 1916 | if(spellID == m_spells[i]) |
---|
| 1917 | break; |
---|
| 1918 | return i < CREATURE_MAX_SPELLS; //broke before end of iteration of known spells |
---|
| 1919 | } |
---|
| 1920 | |
---|
| 1921 | time_t Creature::GetRespawnTimeEx() const |
---|
| 1922 | { |
---|
| 1923 | time_t now = time(NULL); |
---|
| 1924 | if(m_respawnTime > now) // dead (no corpse) |
---|
| 1925 | return m_respawnTime; |
---|
| 1926 | else if(m_deathTimer > 0) // dead (corpse) |
---|
| 1927 | return now+m_respawnDelay+m_deathTimer/1000; |
---|
| 1928 | else |
---|
| 1929 | return now; |
---|
| 1930 | } |
---|
| 1931 | |
---|
| 1932 | void Creature::GetRespawnCoord( float &x, float &y, float &z, float* ori, float* dist ) const |
---|
| 1933 | { |
---|
| 1934 | if(CreatureData const* data = objmgr.GetCreatureData(GetDBTableGUIDLow())) |
---|
| 1935 | { |
---|
| 1936 | x = data->posX; |
---|
| 1937 | y = data->posY; |
---|
| 1938 | z = data->posZ; |
---|
| 1939 | if(ori) |
---|
| 1940 | *ori = data->orientation; |
---|
| 1941 | if(dist) |
---|
| 1942 | *dist = data->spawndist; |
---|
| 1943 | } |
---|
| 1944 | else |
---|
| 1945 | { |
---|
| 1946 | x = GetPositionX(); |
---|
| 1947 | y = GetPositionY(); |
---|
| 1948 | z = GetPositionZ(); |
---|
| 1949 | if(ori) |
---|
| 1950 | *ori = GetOrientation(); |
---|
| 1951 | if(dist) |
---|
| 1952 | *dist = 0; |
---|
| 1953 | } |
---|
| 1954 | } |
---|
| 1955 | |
---|
| 1956 | void Creature::AllLootRemovedFromCorpse() |
---|
| 1957 | { |
---|
| 1958 | if (!HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE)) |
---|
| 1959 | { |
---|
| 1960 | uint32 nDeathTimer; |
---|
| 1961 | |
---|
| 1962 | CreatureInfo const *cinfo = GetCreatureInfo(); |
---|
| 1963 | |
---|
| 1964 | // corpse was not skinnable -> apply corpse looted timer |
---|
| 1965 | if (!cinfo || !cinfo->SkinLootId) |
---|
| 1966 | nDeathTimer = (uint32)((m_corpseDelay * 1000) * sWorld.getRate(RATE_CORPSE_DECAY_LOOTED)); |
---|
| 1967 | // corpse skinnable, but without skinning flag, and then skinned, corpse will despawn next update |
---|
| 1968 | else |
---|
| 1969 | nDeathTimer = 0; |
---|
| 1970 | |
---|
| 1971 | // update death timer only if looted timer is shorter |
---|
| 1972 | if (m_deathTimer > nDeathTimer) |
---|
| 1973 | m_deathTimer = nDeathTimer; |
---|
| 1974 | } |
---|
| 1975 | } |
---|
| 1976 | |
---|
| 1977 | uint32 Creature::getLevelForTarget( Unit const* target ) const |
---|
| 1978 | { |
---|
| 1979 | if(!isWorldBoss()) |
---|
| 1980 | return Unit::getLevelForTarget(target); |
---|
| 1981 | |
---|
| 1982 | uint32 level = target->getLevel()+sWorld.getConfig(CONFIG_WORLD_BOSS_LEVEL_DIFF); |
---|
| 1983 | if(level < 1) |
---|
| 1984 | return 1; |
---|
| 1985 | if(level > 255) |
---|
| 1986 | return 255; |
---|
| 1987 | return level; |
---|
| 1988 | } |
---|
| 1989 | |
---|
| 1990 | char const* Creature::GetScriptName() const |
---|
| 1991 | { |
---|
| 1992 | return ObjectMgr::GetCreatureTemplate(GetEntry())->ScriptName; |
---|
| 1993 | } |
---|