All pastes #491091 Raw Edit

Untitled

public text v1 · immutable
·published 2007-05-16 13:01 UTC
rendered paste body
bool Pet::LoadPetFromDB( Unit* owner, uint32 petentry, uint32 petnumber, bool current )
{
    uint32 ownerid = owner->GetGUIDLow();

    QueryResult *result;

    if(current)
        // current pet (slot 0)           0    1       2       3         4       5     6            7            8              9               10        11           12     13     14        15          16        17             18        19              20		 21					 22
        result = sDatabase.PQuery("SELECT `id`,`entry`,`owner`,`modelid`,`level`,`exp`,`nextlvlexp`,`Reactstate`,`Commandstate`,`loyaltypoints`,`loyalty`,`trainpoint`,`slot`,`name`,`renamed`,`curhealth`,`curmana`,`curhappiness`,`ABData`,`TeachSpelldata`,`savetime`,`resettalents_cost`,`resettalents_time` FROM `character_pet` WHERE `owner` = '%u' AND `slot` = '0'",ownerid );
    else if(petnumber)
        // known petnumber entry           0    1       2       3         4       5     6            7            8              9               10        11           12     13     14        15          16        17             18        19              20		 21					 22
        result = sDatabase.PQuery("SELECT `id`,`entry`,`owner`,`modelid`,`level`,`exp`,`nextlvlexp`,`Reactstate`,`Commandstate`,`loyaltypoints`,`loyalty`,`trainpoint`,`slot`,`name`,`renamed`,`curhealth`,`curmana`,`curhappiness`,`ABData`,`TeachSpelldata`,`savetime`,`resettalents_cost`,`resettalents_time` FROM `character_pet` WHERE `owner` = '%u' AND `id` = '%u'",ownerid, petnumber);
    else if(petentry)
        // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
        //                                   0    1       2       3         4       5     6            7            8              9               10        11           12     13     14        15          16        17             18        19              20		 21					 22
        result = sDatabase.PQuery("SELECT `id`,`entry`,`owner`,`modelid`,`level`,`exp`,`nextlvlexp`,`Reactstate`,`Commandstate`,`loyaltypoints`,`loyalty`,`trainpoint`,`slot`,`name`,`renamed`,`curhealth`,`curmana`,`curhappiness`,`ABData`,`TeachSpelldata`,`savetime`,`resettalents_cost`,`resettalents_time` FROM `character_pet` WHERE `owner` = '%u' AND `entry` = '%u' AND (`slot` = '0' OR `slot` = '3') ",ownerid, petentry );
    else
        // any current or other non-stabled pet (for hunter "call pet")
        //                                   0    1       2       3         4       5     6            7            8              9               10        11           12     13     14        15          16        17             18        19              20		 21					 22
        result = sDatabase.PQuery("SELECT `id`,`entry`,`owner`,`modelid`,`level`,`exp`,`nextlvlexp`,`Reactstate`,`Commandstate`,`loyaltypoints`,`loyalty`,`trainpoint`,`slot`,`name`,`renamed`,`curhealth`,`curmana`,`curhappiness`,`ABData`,`TeachSpelldata`,`savetime`,`resettalents_cost`,`resettalents_time` FROM `character_pet` WHERE `owner` = '%u' AND (`slot` = '0' OR `slot` = '3') ",ownerid);

    if(!result)
        return false;

    Field *fields = result->Fetch();

    // update for case of current pet "slot = 0"
    petentry = fields[1].GetUInt32();
    if(!petentry)
        return false;

    float px, py, pz;
    owner->GetClosePoint(NULL, px, py, pz,PET_FOLLOW_DIST,PET_FOLLOW_ANGLE);
    uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_UNIT);
    if(!Create(guid, owner->GetMapId(), px, py, pz, owner->GetOrientation(), petentry))
    {
        delete result;
        return false;
    }

    SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE,owner->getFaction());

    CreatureInfo const *cinfo = GetCreatureInfo();
    if(cinfo->type == CREATURE_TYPE_CRITTER)
    {
        AIM_Initialize();
        AddToWorld();
        MapManager::Instance().GetMap(owner->GetMapId(), owner)->Add((Creature*)this);
        return true;
    }
    SetUInt32Value(UNIT_FIELD_PETNUMBER, fields[0].GetUInt32() );
    SetUInt64Value(UNIT_FIELD_SUMMONEDBY, owner->GetGUID());
    SetUInt64Value(UNIT_FIELD_DISPLAYID,       fields[3].GetUInt32());
    SetUInt64Value(UNIT_FIELD_NATIVEDISPLAYID, fields[3].GetUInt32());
    uint32 petlevel=fields[4].GetUInt32();
    SetUInt32Value(UNIT_NPC_FLAGS , 0);
    SetName(fields[13].GetString());

    switch(getPetType())
    {

        case SUMMON_PET:
            petlevel=owner->getLevel();

            SetUInt32Value(UNIT_FIELD_BYTES_0,2048);
            SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_UNKNOWN1);
                                                            // this enables popup window (pet dismiss, cancel)
            break;
        case HUNTER_PET:
            SetUInt32Value(UNIT_FIELD_BYTES_0, 0x2020100); //??
            SetUInt32Value(UNIT_FIELD_BYTES_1,(fields[10].GetUInt32()<<8));
            SetUInt32Value(UNIT_FIELD_BYTES_2, 0x00022801); // can't be renamed (byte (0x02))
            SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_UNKNOWN1);// + UNIT_FLAG_RESTING);
                                                            // this enables popup window (pet abandon, cancel)

            // pet not renamed yet, let rename if wont
            if(!fields[14].GetBool())
            {
                //SetUInt32Value(UNIT_FIELD_BYTES_2, uint32(0x03 << 16)); // check it...
                SetUInt32Value(UNIT_FIELD_BYTES_2, 0x00032801); // 0x03
                //SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_RENAME); // old, not working...
            }

            SetTP(fields[11].GetInt32());
            SetMaxPower(POWER_HAPPINESS,1000000);
            SetPower(   POWER_HAPPINESS,fields[17].GetUInt32());
            setPowerType(POWER_FOCUS);
            break;
        default:
            sLog.outError("Pet have incorrect type (%u) for pet loading.",getPetType());
    }
    InitStatsForLevel( petlevel);
    SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
    SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, fields[6].GetUInt32());
    SetUInt64Value(UNIT_FIELD_CREATEDBY, owner->GetGUID());

	m_ReactSate = fields[7].GetUInt32();
    m_CommandState = fields[8].GetUInt32();
	m_loyaltyPoints = fields[9].GetInt32();
	
	uint32 savedhealth = fields[15].GetUInt32();
	uint32 savedmana = fields[16].GetUInt32();

    // set current pet as current
    if(fields[12].GetUInt32() != 0)
    {
        sDatabase.BeginTransaction();
        sDatabase.PExecute("UPDATE `character_pet` SET `slot` = '3' WHERE `owner` = '%u' AND `slot` = '0' AND `id` <=> '%u'",ownerid, GetPetNumber());
        sDatabase.PExecute("UPDATE `character_pet` SET `slot` = '0' WHERE `owner` = '%u' AND `id` = '%u'",ownerid, GetPetNumber());
        sDatabase.CommitTransaction();
    }
    
	//init AB
    vector<string> tokens = StrSplit(fields[18].GetString(), " ");

    if(tokens.size() != 20)
        return false;

    vector<string>::iterator iter;
    int index;

    for (iter = tokens.begin(), index = 0; index < 10; ++iter, ++index)
    {
        PetActionBar[index].Type = atol((*iter).c_str());
        ++iter;
        PetActionBar[index].SpellOrAction = atol((*iter).c_str());
    }

    //init teach spells
    tokens = StrSplit(fields[19].GetString(), " ");
    for (iter = tokens.begin(), index = 0; index < 4; ++iter, ++index)
    {
		uint32 tmp = atol((*iter).c_str());
		++iter;
		if(tmp)
			AddTeachSpell(tmp, atol((*iter).c_str()));
		else break;
    }

    delete result;
    
	//load spells/cooldowns/auras
    // since last save (in seconds)
    uint32 timediff = (time(NULL) - fields[20].GetUInt32());
    //InitStatsForLevel(getLevel());
    _ApplyAllOwnerMods();
    _LoadAuras(timediff);
    _LoadSpells(timediff);
    _LoadSpellCooldowns();

    if(getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
	{
		SetHealth(GetMaxHealth());
		SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
	}
	else
	{
	    SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
	    SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
	}

    ObjectAccessor::Instance().AddPet(this);

    AIM_Initialize();
    AddToWorld();
    MapManager::Instance().GetMap(owner->GetMapId(), owner)->Add((Creature*)this);
    owner->SetPet(this);                                    // in DB stored only full controlled creature
    sLog.outDebug("New Pet has guid %u", GetGUIDLow());

    if(owner->GetTypeId() == TYPEID_PLAYER)
        ((Player*)owner)->PetSpellInitialize();

    return true;
}