void EntertainingSessionImplementation::doPerformanceAction() {
	ManagedReference<CreatureObject*> entertainer = this->entertainer.get();

	if (entertainer == NULL)
		return;

	Locker locker(entertainer);

	Performance* performance = NULL;

	PerformanceManager* performanceManager = SkillManager::instance()->getPerformanceManager();
	ManagedReference<Instrument*> instrument = getInstrument(entertainer);

	if (isDancing())
		performance = performanceManager->getDance(performanceName);
	else if (isPlayingMusic() && instrument)
		performance = performanceManager->getSong(performanceName, instrument->getInstrumentType());
	else {
		cancelSession();
		return;
	}

	if (performance == NULL) { // shouldn't happen
		StringBuffer msg;
		msg << "Performance was null.  Please report to www.swgemu.com/bugs ! Name: " << performanceName << " and Type: " << dec << instrument->getInstrumentType();

		entertainer->sendSystemMessage(msg.toString());
		return;
	}

	int actionDrain = performance->getActionPointsPerLoop();

	//Adjust cost based upon player's quickness
	actionDrain = entertainer->calculateCostAdjustment(CreatureAttribute::QUICKNESS, actionDrain);

	if (entertainer->getHAM(CreatureAttribute::ACTION) <= actionDrain) {
		if (isDancing()) {
			stopDancing();
			entertainer->sendSystemMessage("@performance:dance_too_tired");
		}

		if (isPlayingMusic()) {
			stopPlayingMusic();
			entertainer->sendSystemMessage("@performance:music_too_tired");
		}
	} else {
		entertainer->inflictDamage(entertainer, CreatureAttribute::ACTION, actionDrain, false, true);
	}
}
void EntertainingSessionImplementation::increaseEntertainerBuff(CreatureObject* patron){
	ManagedReference<CreatureObject*> entertainer = this->entertainer.get();

	PerformanceManager* performanceManager = SkillManager::instance()->getPerformanceManager();
	Performance* performance = NULL;

	ManagedReference<Instrument*> instrument = getInstrument(entertainer);

	if (performanceName == "")
		return;

	if (dancing) {
		performance = performanceManager->getDance(performanceName);
	} else if (playingMusic && instrument != NULL) {
		performance = performanceManager->getSong(performanceName, instrument->getInstrumentType());
	} else {
		cancelSession();
		return;
	}

	if(!canGiveEntertainBuff())
		return;

	if (performance == NULL) { // shouldn't happen
		return;
	}

	ManagedReference<PlayerObject*> entPlayer = entertainer->getPlayerObject();
	//Check if the patron is a valid buff target
	//Whether it be passive(in the same group) or active (/setPerform target)
	if ((!entertainer->isGrouped() || entertainer->getGroupID() != patron->getGroupID())
			&& entPlayer->getPerformanceBuffTarget() != patron->getObjectID()) {
		return;
	}

	if(isInDenyServiceList(patron))
		return;

	float buffAcceleration = 1 + ((float)entertainer->getSkillMod("accelerate_entertainer_buff") / 100.f);

	addEntertainerBuffDuration(patron, performance->getType(), 2.0f * buffAcceleration);
	addEntertainerBuffStrength(patron, performance->getType(), performance->getHealShockWound());

}
void EntertainingSessionImplementation::doFlourish(int flourishNumber) {
	ManagedReference<CreatureObject*> entertainer = this->entertainer.get();

	int fid = flourishNumber;

	if (!dancing && !playingMusic) {
		entertainer->sendSystemMessage("@performance:flourish_not_performing");
		return;
	}

	PerformanceManager* performanceManager = SkillManager::instance()->getPerformanceManager();
	Performance* performance = NULL;
	ManagedReference<Instrument*> instrument = getInstrument(entertainer);

	if (dancing)
		performance = performanceManager->getDance(performanceName);
	else if (playingMusic && instrument)
		performance = performanceManager->getSong(performanceName, instrument->getInstrumentType());
	else {
		cancelSession();
		return;
	}

	if (!performance) { // shouldn't happen
		StringBuffer msg;
		msg << "Performance was null.  Please report to www.swgemu.com/bugs ! Name: " << performanceName << " and Type: " << dec << instrument->getInstrumentType();

		entertainer->sendSystemMessage(msg.toString());
		return;
	}

	float baseActionDrain = performance->getActionPointsPerLoop() - (int)(entertainer->getHAM(CreatureAttribute::QUICKNESS)/35.f);

	//float baseActionDrain = -40 + (getQuickness() / 37.5);
	float flourishActionDrain = baseActionDrain / 2.0;

	int actionDrain = (int)round((flourishActionDrain * 10 + 0.5) / 10.0); // Round to nearest dec for actual int cost

	if (entertainer->getHAM(CreatureAttribute::ACTION) <= actionDrain) {
		entertainer->sendSystemMessage("@performance:flourish_too_tired");
	} else {
		entertainer->inflictDamage(entertainer, CreatureAttribute::ACTION, actionDrain, false, true);

		if (dancing) {
			StringBuffer msg;
			msg << "skill_action_" << fid;
			entertainer->doAnimation(msg.toString());
		} else if (playingMusic) {
			Flourish* flourish = new Flourish(entertainer, fid);
			entertainer->broadcastMessage(flourish, true);
		}

		//check to see how many flourishes have occurred this tick
		if(flourishCount < 5) {
			// Add buff
			addEntertainerFlourishBuff();

			// Grant Experience
			if(flourishCount < 2)
				flourishXp += performance->getBaseXp() + performance->getFlourishXpMod();

			flourishCount++;
		}
		entertainer->notifyObservers(ObserverEventType::FLOURISH, entertainer, fid);

		entertainer->sendSystemMessage("@performance:flourish_perform");
	}
}
void EntertainingSessionImplementation::doEntertainerPatronEffects() {
	ManagedReference<CreatureObject*> creo = entertainer.get();

	if (creo == NULL)
		return;

	if (performanceName == "")
		return;

	Locker locker(creo);

	//**DECLARATIONS**
	VectorMap<ManagedReference<CreatureObject*>, EntertainingData>* patrons = NULL;

	SkillManager* skillManager = creo->getZoneServer()->getSkillManager();

	PerformanceManager* performanceManager = skillManager->getPerformanceManager();
	Performance* performance = NULL;

	ManagedReference<Instrument*> instrument = getInstrument(creo);

	float woundHealingSkill = 0.0f;
	float playerShockHealingSkill = 0.0f;
	float buildingShockHealingSkill = creo->getSkillMod("private_med_battle_fatigue");
	float factionPerkSkill = creo->getSkillMod("private_faction_mind_heal");

	//**LOAD PATRONS, GET THE PERFORMANCE AND ENT'S HEALING SKILL.**
	if (dancing) {
		patrons = &watchers;
		performance = performanceManager->getDance(performanceName);
		woundHealingSkill = (float) creo->getSkillMod("healing_dance_wound");
		playerShockHealingSkill = (float) creo->getSkillMod("healing_dance_shock");
	} else if (playingMusic && instrument != NULL) {
		patrons = &listeners;
		performance = performanceManager->getSong(performanceName, instrument->getInstrumentType());
		woundHealingSkill = (float) creo->getSkillMod("healing_music_wound");
		playerShockHealingSkill = (float) creo->getSkillMod("healing_music_shock");

	} else {
		cancelSession();
		return;
	}

	if (performance == NULL) {
		return;
	}

	ManagedReference<BuildingObject*> building = creo->getRootParent().get().castTo<BuildingObject*>();

	if (building != NULL && factionPerkSkill > 0 && building->isPlayerRegisteredWithin(creo->getObjectID())) {
		unsigned int buildingFaction = building->getFaction();
		unsigned int healerFaction = creo->getFaction();
		PlayerObject* ghost = creo->getPlayerObject();

		if (ghost != NULL && healerFaction != 0 && healerFaction == buildingFaction && ghost->getFactionStatus() == FactionStatus::OVERT) {
			woundHealingSkill += factionPerkSkill;
			playerShockHealingSkill += factionPerkSkill;
		}
	}

	//**DETERMINE WOUND HEAL AMOUNTS.**
	int woundHeal = ceil(performance->getHealMindWound() * (woundHealingSkill / 100.0f));
	int shockHeal = ceil(performance->getHealShockWound() * ((playerShockHealingSkill + buildingShockHealingSkill) / 100.0f));

	//**ENTERTAINER HEALS THEIR OWN MIND.**
	healWounds(creo, woundHeal*(flourishCount+1), shockHeal*(flourishCount+1));

	//**APPLY EFFECTS TO PATRONS.**
	if (patrons != NULL && patrons->size() > 0) {

		for (int i = 0; i < patrons->size(); ++i) {
			ManagedReference<CreatureObject*> patron = patrons->elementAt(i).getKey();

			try {
				//**VERIFY THE PATRON IS NOT ON THE DENY SERVICE LIST

				if (creo->isInRange(patron, 10.0f)) {
					healWounds(patron, woundHeal*(flourishCount+1), shockHeal*(flourishCount+1));
					increaseEntertainerBuff(patron);

				} else { //patron is not in range, force to stop listening
					ManagedReference<PlayerManager*> playerManager = patron->getZoneServer()->getPlayerManager();

					Locker locker(patron, entertainer.get());

					if (dancing) {
						if (playerManager != NULL)
							playerManager->stopWatch(patron, creo->getObjectID(), true, false, false, true);

						if (!patron->isListening())
							sendEntertainmentUpdate(patron, 0, "", true);

					} else if (playingMusic) {
						if (playerManager != NULL)
							playerManager->stopListen(patron, creo->getObjectID(), true, false, false, true);

						if (!patron->isWatching())
							sendEntertainmentUpdate(patron, 0, "", true);
					}
				}

			} catch (Exception& e) {
				error("Unreported exception caught in EntertainingSessionImplementation::doEntertainerPatronEffects()");
			}
		}
	} //else
	//System::out << "There are no patrons.\n";


	info("EntertainingSessionImplementation::doEntertainerPatronEffects() end");
}