#include "NW_I0_GENERIC"
#include "prc_inc_spells"

////
void jw_woken();
void jw_sleep();
void jw_wake();
void jw_general();
void jw_cook();
void jw_endcook();
void Homelife();

void Homelife()
{
int nTime=GetLocalInt(OBJECT_SELF,"jw_time");



 if ((nTime>7)&&(nTime<23))
         {

        SetLocalInt(OBJECT_SELF,"njw_current",3);
        }

     if (((nTime>=23)||(nTime<=6))&&(GetLocalInt(OBJECT_SELF,"njw_completed")!=1))
        {

        SetLocalInt(OBJECT_SELF,"njw_current",1);
        }

        if (((nTime>=7)&&(nTime<=12))&&(GetLocalInt(OBJECT_SELF,"njw_completed")==1))
        {
        SetLocalInt(OBJECT_SELF,"njw_current",2);
        }




 if (((nTime>=13)&&(nTime<14))&&(GetLocalInt(OBJECT_SELF,"njw_completed")!=4))
        {
        SetLocalInt(OBJECT_SELF,"njw_current",4);
        }

 if (((nTime>=14)&&(nTime<15))&&(GetLocalInt(OBJECT_SELF,"njw_completed")!=5))
        {
        SetLocalInt(OBJECT_SELF,"njw_current",5);
        }




    if (GetLocalInt(OBJECT_SELF,"njw_current")==1)
     {jw_general();}


       if ((GetLocalInt(OBJECT_SELF,"njw_completed")==1)&&(GetLocalInt(OBJECT_SELF,"njw_current")==0))
     {jw_general();}

      if (GetLocalInt(OBJECT_SELF,"njw_current")==2)
     { jw_general();}

  if (GetLocalInt(OBJECT_SELF,"njw_current")==3)
     { jw_general();}

 if (GetLocalInt(OBJECT_SELF,"njw_current")==4)
     { jw_cook();}

      if (GetLocalInt(OBJECT_SELF,"njw_current")==5)
     { jw_endcook();}



}


void jw_sleep()


{
int nCounter;
object oObject;

if ((!IsInConversation(OBJECT_SELF))&&(!GetIsInCombat(OBJECT_SELF)))
   {


        if (GetLocalInt(OBJECT_SELF,"njw_steps")==0)
        {
         ClearAllActions();
        ActionSpeakString("Time for bed.");
        ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",1));
        }

          if (GetLocalInt(OBJECT_SELF,"njw_steps")==1)
          {
          nCounter=0;
          oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle1",OBJECT_SELF,nCounter);

         while ((GetIsObjectValid(oObject))&&(GetLocalInt(oObject,"NW_L_AMION")!=2))

         {

        ActionMoveToObject(oObject);
        ActionInteractObject(oObject);
        ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,0.5);
        nCounter++;
         oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle1",OBJECT_SELF,nCounter);
        }

        ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",2));
       }



        if (GetLocalInt(OBJECT_SELF,"njw_steps")==2)
         {


   ActionMoveToObject(GetNearestObjectByTag("WP_jw_"+GetTag(OBJECT_SELF)+"up_at"),0,0.1);
     ActionMoveToObject(GetNearestObjectByTag("WP_jw_"+GetTag(OBJECT_SELF)+"down_at"),0,0.1);
   ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",3));
         }
    //  else
// if (GetLocalInt(OBJECT_SELF,"njw_steps")==2)
 //{
  //  ActionJumpToObject(GetNearestObjectByTag("WP_jw_"+GetTag(OBJECT_SELF)+"down_at"));
 //   ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",3));
 // }

     if (GetLocalInt(OBJECT_SELF,"njw_steps")==3)
    {

    oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle2");

         if ((GetIsObjectValid(oObject))&&(GetLocalInt(oObject,"NW_L_AMION")!=2))
         {
         ActionMoveToObject(oObject);
        ActionInteractObject(oObject);
          ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,0.5);

         }
         ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",4));
         }

     if (GetLocalInt(OBJECT_SELF,"njw_steps")==4)
     {

ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_door"),0,0.1);
ActionOpenDoor(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_door"));
ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",5));
}

     if (GetLocalInt(OBJECT_SELF,"njw_steps")==5)

     {

ActionMoveToLocation(GetLocation(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"sleep2_wp")));

ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",6));

}



  if (GetLocalInt(OBJECT_SELF,"njw_steps")==6)

  {
      for (nCounter=0;nCounter<=1;nCounter++)

         {
         oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle3",OBJECT_SELF,nCounter);

         if (GetIsObjectValid(oObject)&&(GetLocalInt(oObject,"NW_L_AMION")!=2))
         {
         ActionMoveToObject(oObject);
        ActionInteractObject(oObject);
          ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,0.5);

         }

          }
    ActionMoveToLocation(GetLocation(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"sleep2_wp")));
    ActionDoCommand(SetFacing(DIRECTION_NORTH));
    ActionRest();
    ActionPlayAnimation(ANIMATION_LOOPING_MEDITATE,1.0,3.0);
   effect eLieDown = EffectSleep();
effect eSnore = EffectVisualEffect (VFX_IMP_SLEEP);
effect eSleep = EffectLinkEffects (eLieDown, eSnore);

  ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",0));
        ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_completed",1));
        ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_current",0));
  DelayCommand(1.0,ActionDoCommand(ApplyEffectToObject (DURATION_TYPE_PERMANENT, eSleep, OBJECT_SELF)));
DelayCommand(2.0,ActionDoCommand(ClearAllActions()));

   }
   }
}

void jw_woken()
{  int nWoken=0;
object oTarget;
//
if ((!IsInConversation(OBJECT_SELF))&&(!GetIsInCombat(OBJECT_SELF)))
    {
    // if I am asleep
    if (PRCGetHasEffect(EFFECT_TYPE_SLEEP))
    {
        // get the nearest enemy creature to me
         oTarget = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR,PLAYER_CHAR_IS_PC);
        // and if there is one and it is less than 5 meters away
        if ((GetDistanceToObject(oTarget) < 3.0) && (GetIsObjectValid(oTarget)))
        {
            effect eSleep = GetFirstEffect(OBJECT_SELF);
            // scroll through my current effects
            while (GetIsEffectValid(eSleep))
            {
                // and if one of them if the effect sleep but but didn't come from a sleep spell
                if ((GetEffectType(eSleep) == EFFECT_TYPE_SLEEP) && (GetEffectSpellId(eSleep) != SPELL_SLEEP))
                {
                    // remove it
                    RemoveEffect(OBJECT_SELF, eSleep);
                    nWoken=1;
                }
                eSleep = GetNextEffect(OBJECT_SELF);
            }
      }

    }

if (nWoken==1)
{


SpeakString("You woke me up - please leave");

DelayCommand(2.0,ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",4)));
DelayCommand(2.1,ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_completed",0)));

}
}
}

void jw_wake()

{
object oTarget;
int nCounter;
object oObject;

 if ((!IsInConversation(OBJECT_SELF))&&(!GetIsInCombat(OBJECT_SELF)))

{
 if (GetSittingCreature(GetLocalObject(OBJECT_SELF,"currentchair"))==OBJECT_SELF)
   {ClearAllActions();}


 if (PRCGetHasEffect(EFFECT_TYPE_SLEEP))
    {
        effect eSleep = GetFirstEffect(OBJECT_SELF);
            // scroll through my current effects
            while (GetIsEffectValid(eSleep))
            {
                // and if one of them if the effect sleep but but didn't come from a sleep spell
                if ((GetEffectType(eSleep) == EFFECT_TYPE_SLEEP) && (GetEffectSpellId(eSleep) != SPELL_SLEEP))
                {
                    // remove it
                    RemoveEffect(OBJECT_SELF, eSleep);

                }
                eSleep = GetNextEffect(OBJECT_SELF);
            }


    } // is now awake
  /// do candle3s

 for (nCounter=0;nCounter<=1;nCounter++)

         {
         oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle3",OBJECT_SELF,nCounter);

         if (GetIsObjectValid(oObject)&&(GetLocalInt(oObject,"NW_L_AMION")!=1))
         {
         ActionMoveToObject(oObject);
        ActionInteractObject(oObject);
          ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,0.5);
         }

          }

// open door

ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_door"),0,0.1);
ActionOpenDoor(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_door"));

// turn off candle2

 oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle2");

         if ((GetIsObjectValid(oObject))&&(GetLocalInt(oObject,"NW_L_AMION")!=1))
         {
         ActionMoveToObject(oObject);
        ActionInteractObject(oObject);
          ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,0.5);

         }

         // go to upstairs waypoint
         ActionMoveToObject(GetNearestObjectByTag("WP_jw_"+GetTag(OBJECT_SELF)+"down_at"),0,0.1);

       // move to downstairs waypoint
      ActionMoveToObject(GetNearestObjectByTag("WP_jw_"+GetTag(OBJECT_SELF)+"up_at"));
       // move to living room centre

       ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"living_wp"),0,0.1);
       ActionSpeakString("What a beautiful morning");

       // turn off all the candles

        for (nCounter=0;nCounter<=4;nCounter++)

         {
         oObject=GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_candle1",OBJECT_SELF,nCounter);

         if ((GetIsObjectValid(oObject))&&(GetLocalInt(oObject,"NW_L_AMION")!=1))
         {
         ActionMoveToObject(oObject);
        ActionInteractObject(oObject);
        ActionPlayAnimation(ANIMATION_LOOPING_GET_MID,1.0,0.5);

         }

          }
    // say it is all done




       ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"living_wp"),0,1.0);
          ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_completed",2));
          ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_current",3));
           ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",0));
   ActionDoCommand(ClearAllActions());
 }
}


void jw_general()

{


////one in 10 chance of changing behaviour
int nRandom=Random(10);
object oChair;
int nCounter=1;
int nFoundchair=0;
 if ((!IsInConversation(OBJECT_SELF))&&(!GetIsInCombat(OBJECT_SELF)))
 {
if (nRandom==1)

 {

 nRandom=Random(5);


 ClearAllActions();
switch (nRandom)

{

case 0:
 ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_bookshelf"),0,0.1);
 ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,1.0);
 oChair =GetNearestObjectByTag ("jw_"+GetTag(OBJECT_SELF)+"_couch", OBJECT_SELF);
 if (GetIsObjectValid(GetSittingCreature(oChair)))
  { break;}
 else
 {
  ActionMoveToObject(oChair,0);

 ActionSit(oChair);


 break;
 }
 case 1:
 ActionRandomWalk();
 break;

 case 2:
 ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_bookshelf"),0,0.1);
 ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,1.0);
 oChair =GetNearestObjectByTag ("jw_"+GetTag(OBJECT_SELF)+"_chair", OBJECT_SELF,nCounter);


  while (GetIsObjectValid(oChair)&&(nFoundchair==0))
  {
   if (!GetIsObjectValid(GetSittingCreature(oChair)))
  {
  nFoundchair=1;
   ActionMoveToObject(oChair,0);

   ActionSit(oChair);
   break;
  }
  else
  {
  nCounter++;
  oChair=GetNearestObjectByTag ("jw_"+GetTag(OBJECT_SELF)+"_chair", OBJECT_SELF,nCounter);
  }
  }
   if (nFoundchair==0)
   {
    ActionSpeakString("Hmm . . . it's busy in here today");
   break;
   }
   case 3:
   ActionPlayAnimation(ANIMATION_FIREFORGET_HEAD_TURN_LEFT);
    ActionPlayAnimation(ANIMATION_FIREFORGET_HEAD_TURN_RIGHT);
     ActionPlayAnimation(ANIMATION_FIREFORGET_PAUSE_SCRATCH_HEAD);
   ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_cabinet"),0,0.1);
    ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,1.0);
    ActionWait(1.0);
     ActionPlayAnimation(ANIMATION_FIREFORGET_DRINK);
     ActionWait(1.0);
      ActionRandomWalk();

case 4:
 ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_bookshelf"),0,0.1);
 ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,1.0);
 oChair =GetNearestObjectByTag ("jw_"+GetTag(OBJECT_SELF)+"_couch", OBJECT_SELF);
 if (GetIsObjectValid(GetSittingCreature(oChair)))
  { break;}
 else
 {
  ActionMoveToObject(oChair,0);

 ActionSit(oChair);
 break;
 }
SetLocalInt(OBJECT_SELF,"njw_steps",0);
   }
    ///ended switch
}
}
}

void jw_cook()
{
if ((!IsInConversation(OBJECT_SELF))&&(!GetIsInCombat(OBJECT_SELF)))
   {
   if (GetLocalInt(OBJECT_SELF,"njw_steps")==0)
        {
 ClearAllActions();
    ActionSpeakString("Time to make some food");
    ActionDoCommand( SetLocalInt(OBJECT_SELF,"njw_steps",1));
    }

 if (GetLocalInt(OBJECT_SELF,"njw_steps")==1)
        {
   ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_oven"),0,0.1);
    ActionDoCommand(SetFacing(DIRECTION_SOUTH));
      ActionDoCommand( SetLocalInt(OBJECT_SELF,"njw_steps",2));
      }

    if (GetLocalInt(OBJECT_SELF,"njw_steps")==2)
        {
 if (GetLocalInt(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_oven"),"NW_L_AMION") != 1)
 {
   ActionInteractObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_oven"));

   }
    ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",3));

    }

   if (GetLocalInt(OBJECT_SELF,"njw_steps")==3)
        {
       ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,2.0);

   ActionPlayAnimation(ANIMATION_FIREFORGET_PAUSE_BORED);
   ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"living_wp"));
             ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_completed",4));
    ActionDoCommand( SetLocalInt(OBJECT_SELF,"njw_current",3));
       ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",0));
       ActionDoCommand(ClearAllActions());
   }
   }
}


void jw_endcook()
{

object oChair;
int nCounter;
int nFoundchair;

if ((!IsInConversation(OBJECT_SELF))&&(!GetIsInCombat(OBJECT_SELF)))
   {
    if (GetLocalInt(OBJECT_SELF,"njw_steps")==0)
        {
 ClearAllActions();
    ActionSpeakString("Time to get the food out of the oven");
    ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",1));
    }
     if (GetLocalInt(OBJECT_SELF,"njw_steps")==1)
        {
   ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_oven"),0,0.1);
    ActionDoCommand(SetFacing(DIRECTION_SOUTH));
    ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",2));
    }
   if (GetLocalInt(OBJECT_SELF,"njw_steps")==2)
        {

 if (GetLocalInt(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_oven"),"NW_L_AMION") != 2)
   {
   ActionInteractObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"_oven"));

   }
   ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",3));
}
  if (GetLocalInt(OBJECT_SELF,"njw_steps")==3)
        {
        ActionPlayAnimation(ANIMATION_LOOPING_GET_LOW,1.0,2.0);
   ActionPlayAnimation(ANIMATION_FIREFORGET_HEAD_TURN_RIGHT);

   ActionMoveToObject(GetNearestObjectByTag("jw_"+GetTag(OBJECT_SELF)+"living_wp"));

oChair =GetNearestObjectByTag ("jw_"+GetTag(OBJECT_SELF)+"_chair", OBJECT_SELF,nCounter);


  while (GetIsObjectValid(oChair)&&(nFoundchair==0))
  {
   if (!GetIsObjectValid(GetSittingCreature(oChair)))
  {
  nFoundchair=1;
   ActionMoveToObject(oChair,0);
   ActionDoCommand(DelayCommand(15.0,SetLocalInt(OBJECT_SELF,"njw_completed",5)));
   ActionDoCommand(DelayCommand(15.0,SetLocalInt(OBJECT_SELF,"njw_current",3)));
     ActionDoCommand(DelayCommand(15.0,SetLocalInt(OBJECT_SELF,"njw_steps",0)));
   ActionSit(oChair);

  }
  else
  {
  nCounter++;
  oChair=GetNearestObjectByTag ("jw_"+GetTag(OBJECT_SELF)+"_chair", OBJECT_SELF,nCounter);
  }
  }
   if (nFoundchair==0)
   {
    ActionSpeakString("Well, there's nowhere to sit");

   }

           ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_completed",5));
    ActionDoCommand( SetLocalInt(OBJECT_SELF,"njw_current",3));
    ActionDoCommand(SetLocalInt(OBJECT_SELF,"njw_steps",0));
      ActionDoCommand(ClearAllActions());
    }

   }
}