//:://////////////////////////////////////////////////
//:: shaz_lover_onheart
//::  based on nw_c2_default1
/*
  Default OnHeartbeat script for NPCs.

  This script causes NPCs to perform default animations
  while not otherwise engaged.

  This script duplicates the behavior of the default
  script and just cleans up the code and removes
  redundant conditional checks.

 */
//:://////////////////////////////////////////////////
//:: Copyright (c) 2002 Floodgate Entertainment
//:: Created By: Naomi Novik
//:: Created On: 12/22/2002
//:://////////////////////////////////////////////////
// ChazM 6/20/05 if is encounter crature no longer checked - ambient anims flag set on spawn instead.
// ChazM 1/6/06 modified call to WalkWayPoints()
// ChazM 3/6/06 fixed AI level exit condition bug
// ChazM 3/23/06 added DoDebug(); no functionality changes

#include "nw_i0_generic"
#include "ginc_debug"
#include "shaz_inc_love"

void DoDebug(string sMessage)
{
	
	//if ((GetTag(OBJECT_SELF) != "3030_dwarf")  && (GetTag(OBJECT_SELF) != "c_cow"))
		return;
	PrettyDebug("NW_C2_DEFAULT1 " + GetName(OBJECT_SELF) + ": " + sMessage);
}

void main()
{
	object oPC = GetFirstPC(FALSE);
	//SendMessageToPC(oPC, "shaz_lover_onheart!");

    // * if not runnning normal or better Ai then exit for performance reasons
    if (GetAILevel() == AI_LEVEL_VERY_LOW)
	{
		DoDebug("goodness gracious - i have very low AI!");
		return;
	}

    // Buff ourselves up right away if we should
    if(GetSpawnInCondition(NW_FLAG_FAST_BUFF_ENEMY))
    {
        // This will return TRUE if an enemy was within 40.0 m
        // and we buffed ourselves up instantly to respond --
        // simulates a spellcaster with protections enabled
        // already.
        if(TalentAdvancedBuff(40.0))
        {
            // This is a one-shot deal
            SetSpawnInCondition(NW_FLAG_FAST_BUFF_ENEMY, FALSE);

            // This return means we skip sending the user-defined
            // heartbeat signal in this one case.
            return;
        }
    }
	
	// SHAZ: Here we'll do special "in love" behavior - most notably, follow the loved around
	object oLove = GetLocalObject(OBJECT_SELF, "ShazLoved");
	
	if(GetIsDead(oLove) || !GetIsObjectValid(oLove)) {
		// if our love is dead (or invalid), we'll mourn their loss and then return to normal (after a little bit)
		ClearAllActions(TRUE);
		SetFacingPoint(GetPosition(oLove));
		DelayCommand(10.0f, SendMessageToPC(oLove, "Restoring " + GetName(OBJECT_SELF) + " scripts"));
		DelayCommand(10.0f, SafeRestoreEventHandlers(OBJECT_SELF));
		if(GetIsOwnedByPlayer(OBJECT_SELF) || GetIsRosterMember(OBJECT_SELF)) {
			// restore faction settings... attacking people while under the influence of love might have changed them
			DelayCommand(10.0f, SetStandardFactionReputation(STANDARD_FACTION_COMMONER, 100));
			DelayCommand(10.0f, SetStandardFactionReputation(STANDARD_FACTION_MERCHANT, 100));
			DelayCommand(10.0f, SetStandardFactionReputation(STANDARD_FACTION_DEFENDER, 100));
			DelayCommand(10.0f, SetStandardFactionReputation(STANDARD_FACTION_HOSTILE, 0));
		}
		if(CanISpeakIntelligently()) {
			int iRandom = Random(2);
			if(iRandom == 0) {
				if(GetIsObjectValid(oLove)) {
					SpeakString("Noooo! My love! Oh, " + GetFirstName(oLove) + ", how could I possibly go on without you...?", TALKVOLUME_SHOUT);
				}
			} else if(iRandom == 1) {
				if(GetGender(oLove) == GENDER_FEMALE) {
					SpeakString("No! Oh cruel gods, why have you taken her from me!?", TALKVOLUME_SHOUT);
				} else {
					SpeakString("No! Oh cruel gods, why have you taken him from me!?", TALKVOLUME_SHOUT);
				}
			}
		} else if(CanISpeakAtAll()) {
			int iRandom = Random(2);
			if(iRandom == 0) {
				SpeakString("Nooo! Don't leave friend!", TALKVOLUME_SHOUT);
			} else if(iRandom == 1) {
				SpeakString("I miss you, friend...");
			}
		}
	} else if(GetDistanceBetween(OBJECT_SELF, oLove) > 7.0f) {
		// if our love is too far away, we'll complain and then run to them
		if(d100(1) <= 75) {
			if(CanISpeakIntelligently()) {
				int iRandom = Random(3);
				if(iRandom == 0) {
					SpeakString("My love, where are you going?!", TALKVOLUME_SHOUT);
				} else if(iRandom == 1) {
					SpeakString("Don't leave me, my love!", TALKVOLUME_SHOUT);
				} else if(iRandom == 2) {
					SpeakString("I will follow you to the ends of the earth!", TALKVOLUME_SHOUT);
				}
			} else if(CanISpeakAtAll()) {
				int iRandom = Random(2);
				if(iRandom == 0) {
					SpeakString("No leave me!", TALKVOLUME_SHOUT);
				} else {
					SpeakString("Me follow!", TALKVOLUME_SHOUT);
				}
			}
		}
		AssignCommand(OBJECT_SELF, ActionMoveToObject(oLove, TRUE, 3.0f));
	} else if((GetIsInCombat() == FALSE) && (GetIsInCombat(oLove) == FALSE) && (d100(1) <= 75)) {
		// if neither us or our love is in combat, we might tell them how much we love them
		SetFacingPoint(GetPosition(oLove));
		ClearAllActions(TRUE);
		if(CanISpeakIntelligently()) {
			int iRandom = Random(5);
			switch(iRandom) {
				case 0:
					if(GetGender(OBJECT_SELF) != GetGender(oLove)) {
						SpeakString("Marry me, " + GetName(oLove) + "!");
					} else {
						SpeakString("Lets be friends forever, " + GetFirstName(oLove) + "!");
					}
					break;
				case 1:
					SpeakString("I love you, " + GetFirstName(oLove) + "!", TALKVOLUME_SHOUT);
					break;
				case 2:
					SpeakString("We belong together, " + GetFirstName(oLove) + ", don't deny it!");
					break;
				case 3:
					SpeakString(GetFirstName(oLove) + ", you are the most beautiful thing I have ever seen!");
					break;
				case 4:
					SpeakString(GetFirstName(oLove) + ", you are more beautiful than the setting sun");
				default:
					break;
			}
		} else if(CanISpeakAtAll()) {
			int iRandom = Random(4);
			switch(iRandom) {
				case 0:
					SpeakString("Me Love you!", TALKVOLUME_SHOUT);
					break;
				case 1:
					SpeakString("Me want to be friend!", TALKVOLUME_SHOUT);
					break;
				case 2:
					SpeakString("You pretty!");
					break;
				case 3:
					if(GetGender(OBJECT_SELF) != GetGender(oLove)) {
						SpeakString("You be mate?");
					}
				default:
					break;
			}
		}
	} else if(GetIsInCombat(oLove)) {
		// our love is fighting something, lets join in!
		//SendMessageToPC(oPC, "Loved is in combat");
		if(GetIsObjectValid(GetLastHostileActor(oLove))) {
			//SendMessageToPC(oPC, "Loved's last hostile actor was " + GetName(GetLastHostileActor(oLove)));
			if(GetLastHostileActor(oLove) == OBJECT_SELF) {
				// shaz: I'm not entirely sure how, but it seems the creature will
				// switch to attack you if are standing next to it when its current target dies
				// this will get them to stop attacking you (although, there could be up to six second of it attacking you, and that sucks...)
				ClearAllActions(TRUE);
			} else {
				//if(GetIsInCombat() == FALSE) {
				if(GetAttackTarget() != GetLastHostileActor(oLove)) {
					if(CanISpeakIntelligently()) {
						SpeakString("I will save you, my love!", TALKVOLUME_SHOUT);
					} else if(CanISpeakAtAll()) {
						SpeakString("Me smash for you!", TALKVOLUME_SHOUT);
					}
				}
				// shaz: without adjusting rep, if you fall in love with a hostile creature and a guard attacks it,
				//  you'll be completely unable to cast any spell at the guard. I don't like that, so...
				AdjustReputation(OBJECT_SELF, GetLastHostileActor(oLove), -100);
				AdjustReputation(OBJECT_SELF, oLove, 50);
				// hopefully make this person attack them...
				DetermineCombatRound(GetLastHostileActor(oLove));
			}
		} else {
			//SendMessageToPC(oPC, "Loved's last hostile actor INVALID!");
		}
	}

    // Check to see if we should be playing default animations
    // - make sure we don't have any current targets
    if ( !GetIsObjectValid(GetAttemptedAttackTarget())
          && !GetIsObjectValid(GetAttemptedSpellTarget())
          && !GetIsObjectValid(GetNearestSeenEnemy()))
    {
		DoDebug(" I'm gonna look at playing some animations.");
        if (GetBehaviorState(NW_FLAG_BEHAVIOR_SPECIAL) || GetBehaviorState(NW_FLAG_BEHAVIOR_OMNIVORE) ||
            GetBehaviorState(NW_FLAG_BEHAVIOR_HERBIVORE))
        {
            // This handles special attacking/fleeing behavior
            // for omnivores & herbivores.
			DoDebug("I'm gonna do something special!");
            DetermineSpecialBehavior();
        }
        else if (!IsInConversation(OBJECT_SELF))
        {
            if (GetSpawnInCondition(NW_FLAG_AMBIENT_ANIMATIONS)
                || GetSpawnInCondition(NW_FLAG_AMBIENT_ANIMATIONS_AVIAN))
                //|| GetIsEncounterCreature())
            {
				DoDebug("I'm gonna play some ambient animations!");
				//SpawnScriptDebugger();
                PlayMobileAmbientAnimations();
            }
            else if (GetSpawnInCondition(NW_FLAG_IMMOBILE_AMBIENT_ANIMATIONS))
            {
				DoDebug("I'm gonna play some immobile ambient animations!");
                PlayImmobileAmbientAnimations();
            }
			else
			{
				DoDebug("I don't have any flags telling me to play animations.");
			}
        }
		else
		{
			DoDebug("I must be in a conversation!");
		}
    }

    // Send the user-defined event signal if specified
    if(GetSpawnInCondition(NW_FLAG_HEARTBEAT_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(EVENT_HEARTBEAT));
    }
}