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::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
}
ARCEMU_INLINE bool SetFlagAtWar(uint8 & flag, bool set)
{
	if(set && !AtWar(flag))
		flag |= FACTION_FLAG_AT_WAR;
	else if(!set && AtWar(flag))
		flag &= ~FACTION_FLAG_AT_WAR;
	else
		return false;

	return true;
}
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(!Visible(rep->flag))
    {
        SetFlagVisible(rep->flag);
        if(IsInWorld())
            m_session->OutPacket(SMSG_SET_FACTION_VISIBLE, 4, &dbc->RepListId);
    }

    if(GetReputationRankFromStanding(rep->standing) <= STANDING_HOSTILE && !AtWar(rep->flag))
        SetFlagAtWar(rep->flag);

    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::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
}