bool Player::IsHostileBasedOnReputation(FactionDBC * dbc)
{
	if(dbc->RepListId < 0 || dbc->RepListId >= 128)
		return false;

	FactionReputation * rep = reputationByListId[dbc->RepListId];
	if(!rep) return false;

	// forced reactions take precedence
	if( m_forcedReactions.size() )
	{
		map<uint32,uint32>::iterator itr = m_forcedReactions.find( dbc->ID );
		if( itr != m_forcedReactions.end() )
			return ( itr->second <= STANDING_HOSTILE ) ? true : false;
	}

	if(rep->flag & FACTION_FLAG_AT_WAR || GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE)
		return true;
	else
		return false;

	/*map<uint32, FactionReputation>::iterator itr = m_reputation.find(Faction);
	if(itr == m_reputation.end()) return false;
	if(itr->second.flag & FACTION_FLAG_AT_WAR || GetReputationRankFromStanding(itr->second.standing) <= STANDING_HOSTILE)
		return true;
	else
		return false;*/
}
void Player::SetAtWar(uint32 Faction, bool Set)
{
	if( Faction >= 128 )
		return;

	FactionReputation * rep = reputationByListId[Faction];
	if(!rep) return;
	
	if(GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE && !Set) // At this point we have to be at war.
		return;

	FactionDBC * f = dbcFaction.LookupEntry(Faction);
	if(rep->flag & 0x4)
		return;

	if(Set)
	{
		if(!AtWar(rep->flag))
			SetFlagAtWar(rep->flag);

		UpdateInrangeSetsBasedOnReputation();
	}
	else
	{
		if(AtWar(rep->flag))
			UnsetFlagAtWar(rep->flag);

		UpdateInrangeSetsBasedOnReputation();
	}

#ifdef OPTIMIZED_PLAYER_SAVING
	save_Reputation();
#endif
}
void Player::SetAtWar(uint32 Faction, bool Set)
{
    if( Faction >= 128 )
        return;

    FactionReputation * rep = reputationByListId[Faction];
    if(!rep)
        return;

    if(GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE && !Set) // At this point we have to be at war.
        return;

    if(rep->flag & 0x4 || rep->flag & 16 )
        return;

    if(Set)
    {
        if(!AtWar(rep->flag))
            SetFlagAtWar(rep->flag);

        UpdateInrangeSetsBasedOnReputation();
    }
    else
    {
        if(AtWar(rep->flag))
            UnsetFlagAtWar(rep->flag);

        UpdateInrangeSetsBasedOnReputation();
    }
}
void Player::_InitialReputation()
{
	// Generate Initial Reputation Values
	FactionReputation * rep;
	FactionDBC * f;
	int32 BaseRep = 0;
	uint32 j;
	uint32 RaceMask = getRaceMask();
	for(uint32 i = 0; i < dbcFaction.GetNumRows(); ++i)
	{
		f = dbcFaction.LookupRow(i);
		if(f == 0) continue;

		// Get Base Reputation
		for(j = 0; j < 4; ++j)
		{
			if(f->baseRepMask[j] & RaceMask)
			{
				BaseRep = f->baseRepValue[j];
				break;
			}
		}

		if(j != 4 && f->RepListId >= 0)
		{
			// This is one we can add.
			rep = new FactionReputation;
			rep->standing = BaseRep;
			rep->flag = 0;
			rep->baseStanding = BaseRep;
			if(GetReputationRankFromStanding(BaseRep) <= STANDING_HOSTILE)
				SetFlagAtWar(rep->flag);

			// Visible-ness shit.
			if((f->parentFaction == 469 && GetTeam() == 0) ||		// Alliance own faction.
				(f->parentFaction == 67 && GetTeam() == 1) )		 // Horde own faction.
			{
				SetFlagVisible(rep->flag);
				SetFlagPeaceForced(rep->flag);
			}

			if((f->parentFaction == 469 && GetTeam() == 1) ||		// Alliance own faction.
				(f->parentFaction == 67 && GetTeam() == 0) )		 // Horde own faction.
			{
				SetForcedInvisible(rep->flag);
			}
				
			reputationByListId[f->RepListId] = rep;
		}
		else
		{
			// No default standing.
			continue;
		}

		m_reputation[f->ID] = rep;
	}
}
void Player::SetStanding(uint32 Faction, int32 Value)
{
	ReputationMap::iterator itr = m_reputation.find(Faction);
	FactionDBC * dbc = dbcFaction.LookupEntry(Faction);
	if(dbc == 0) return;

	if(itr == m_reputation.end())
	{
		// New faction!
		FactionReputation * rep = new FactionReputation;
		rep->flag = 0;
		rep->standing = Value;
		rep->baseStanding = 0;
		m_reputation[dbc->ID] = rep;
		if(dbc->RepListId >= 0)
			reputationByListId[dbc->RepListId] = rep;
	}
	else
	{
		// Increment it.
		if(RankChangedFlat(itr->second->standing, Value))
		{
			itr->second->standing = Value;
			UpdateInrangeSetsBasedOnReputation();
		}
		else
		{
			itr->second->standing = Value;
		}

		// Set visible if invisible.
		if(!Visible(itr->second->flag))
		{
			SetFlagVisible(itr->second->flag);
			if(IsInWorld())
				m_session->OutPacket(SMSG_SET_FACTION_VISIBLE, 4, &dbc->RepListId);
		}

		// Set at war if we're beyond hostile.
		Standing rank = GetReputationRankFromStanding(itr->second->standing);
		if(rank <= STANDING_HOSTILE && !AtWar(itr->second->flag))
			SetFlagAtWar(itr->second->flag);
		else if(rank >= STANDING_NEUTRAL && AtWar(itr->second->flag))
			UnsetFlagAtWar(itr->second->flag);

		if(IsInWorld())
		{
			WorldPacket data(SMSG_SET_FACTION_STANDING, 12);
			data << uint32(0) << uint8(0) << uint32(itr->second->flag) << dbc->RepListId << itr->second->CalcStanding();
			m_session->SendPacket(&data);
		}
	}

#ifdef OPTIMIZED_PLAYER_SAVING
	save_Reputation();
#endif
}
void Player::OnModStanding( FactionDBC * dbc, FactionReputation * rep )
{
	if ( SetFlagVisible( rep->flag, true ) && IsInWorld() )
	{
#ifdef USING_BIG_ENDIAN
		uint32 swapped = swap32( dbc->RepListId );
		m_session->OutPacket( SMSG_SET_FACTION_VISIBLE, 4, &swapped );
#else
		m_session->OutPacket( SMSG_SET_FACTION_VISIBLE, 4, &dbc->RepListId );
#endif
	}

	SetFlagAtWar( rep->flag, ( GetReputationRankFromStanding( rep->standing ) <= STANDING_HOSTILE ) );

	if ( Visible( rep->flag ) && IsInWorld() )
	{
		WorldPacket data( SMSG_SET_FACTION_STANDING, 12 );
		data << uint32( 0 ) << uint32( rep->flag ) << dbc->RepListId << rep->CalcStanding();
		m_session->SendPacket( &data );
	}

	// PVP title as a reward for exalted reputations
	switch( dbc->RepListId )
	{
		case FACTION_STORMPIKE_GUARDS:
		case FACTION_SILVERWING_SENTINELS:
		case FACTION_THE_LEAGUE_OF_ARATHOR:
		{
			if( GetTeam() == 0 && 
				GetStandingRank( 730 ) == STANDING_EXALTED &&
				GetStandingRank( 890 ) == STANDING_EXALTED &&
				GetStandingRank( 509 ) == STANDING_EXALTED )
			{
				SetKnownTitle( PVPTITLE_JUSTICAR , true );
			}
			else
				SetKnownTitle( PVPTITLE_JUSTICAR , false );
		} break;
		case FACTION_THE_DEFILERS:
		case FACTION_FROSTWOLF_CLAN:
		case FACTION_WARSONG_OUTRIDERS:
		{
			if( GetTeam() == 1 && 
				GetStandingRank( 510 ) == STANDING_EXALTED &&
				GetStandingRank( 729 ) == STANDING_EXALTED &&
				GetStandingRank( 889 ) == STANDING_EXALTED )
			{
				SetKnownTitle( PVPTITLE_CONQUEROR , true );
			}
			else
				SetKnownTitle( PVPTITLE_CONQUEROR , false );
		} break;
	}
}
bool Player::IsHostileBasedOnReputation(FactionDBC* dbc)
{
	if(dbc->RepListId < 0 || dbc->RepListId >= 128)
		return false;

	FactionReputation* rep = reputationByListId[dbc->RepListId];
	if(rep == NULL)
		return false;

	// forced reactions take precedence
	map<uint32, uint32>::iterator itr = m_forcedReactions.find(dbc->ID);
	if(itr != m_forcedReactions.end())
		return (itr->second <= STANDING_HOSTILE);

	return (AtWar(rep->flag) || GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE);
}
void Player::OnModStanding(FactionDBC* dbc, FactionReputation* rep)
{
	if(SetFlagVisible(rep->flag, true) && IsInWorld())
	{

		m_session->OutPacket(SMSG_SET_FACTION_VISIBLE, 4, &dbc->RepListId);
	}

	SetFlagAtWar(rep->flag, (GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE));

	if(Visible(rep->flag) && IsInWorld())
	{
		WorldPacket data(SMSG_SET_FACTION_STANDING, 17);
		data << uint32(0) ;
		data << uint8(1) ;   //count
		data << uint32(rep->flag) << dbc->RepListId << rep->CalcStanding();
		m_session->SendPacket(&data);
	}
}
void Player::SetAtWar(uint32 Faction, bool Set)
{
	if(Faction >= 128)
		return;

	FactionReputation* rep = reputationByListId[Faction];
	if(rep == NULL)
		return;

	if(GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE && !Set)     // At this point we have to be at war.
		return;

	if(!CanToggleAtWar(rep->flag))
		return;

	if(SetFlagAtWar(rep->flag, Set))
	{
		UpdateInrangeSetsBasedOnReputation();
	}
}
Standing Player::GetStandingRank(uint32 Faction)
{
	return Standing(GetReputationRankFromStanding(GetStanding(Faction)));
}
void Player::ModStanding(uint32 Faction, int32 Value)
{
	ReputationMap::iterator itr = m_reputation.find(Faction);
	FactionDBC * dbc = dbcFaction.LookupEntry(Faction);
	if(dbc == 0) return;

	if(itr == m_reputation.end())
	{
		// New faction!
		FactionReputation * rep = new FactionReputation;
		rep->flag = 0;
		rep->standing = Value;
		rep->baseStanding = 0;
		m_reputation[dbc->ID] = rep;
		if(dbc->RepListId >= 0)
			reputationByListId[dbc->RepListId] = rep;
	}
	else
	{
		// Bonus
		if(pctReputationMod > 0)
		{
			float d = float(float(pctReputationMod) / 100.0f);
			Value += FL2UINT( float( float(Value) * d ) );
		}

		// Increment it.
		if(RankChanged(itr->second->standing, Value))
		{
			itr->second->standing += Value;
			UpdateInrangeSetsBasedOnReputation();
		}
		else
		{
			itr->second->standing += Value;
		}

		// Set visible if invisible.
		if(!Visible(itr->second->flag))
		{
			SetFlagVisible(itr->second->flag);
			if(IsInWorld())
			{
#ifdef USING_BIG_ENDIAN
				uint32 swapped = swap32(dbc->RepListId);
				m_session->OutPacket(SMSG_SET_FACTION_VISIBLE, 4, &swapped);
#else
				m_session->OutPacket(SMSG_SET_FACTION_VISIBLE, 4, &dbc->RepListId);
#endif
			}
		}

		// Set at war if we're beyond hostile.
		if(GetReputationRankFromStanding(itr->second->standing) <= STANDING_HOSTILE && !AtWar(itr->second->flag))
			SetFlagAtWar(itr->second->flag);

		if(IsInWorld() && Visible(itr->second->flag))
		{
			WorldPacket data(SMSG_SET_FACTION_STANDING, 12);
			data << uint32(0) << uint32(1) << dbc->RepListId << itr->second->CalcStanding();
			m_session->SendPacket(&data);
		}
   }

#ifdef OPTIMIZED_PLAYER_SAVING
	save_Reputation();
#endif
}