Author Sticky: Post Working Scripts Here
Kihon Posted: Monday, 08 July 09:47PM
1) Script Name: Resting Script

2) What it does: It allows resting only if a specified ammount of time has passed and only if the players are in possesion of a "Bedroll" item or use a placeable that allows them to rest (a bed, a bedroll...).

3) Notes: A "Bedroll" item is any custom item tagged "Bedroll". A placeable item requires another script that sets the LocalInt "iRestAllowed" to 1 onUsed.

4) The Main Script:
Placed in the onResting event in the Module properties.

NWScript:
//::///////////////////////////////////////////////

//:: Resting Script

//:: boz_o0_resting

//:: Copyright (c) 2002 Brotherhood of Zock

//:://////////////////////////////////////////////

/*

   This script allows the players to rest after a specified amount of hours

   has passed and only if they are in possession of a "Bedroll" or use a local

   "placeable" item that allows them to rest (and sets the LocalInt "iRestAllowed" to 1).

*/

//:://////////////////////////////////////////////

//:: Created By: Kihon

//:: Created On: July 8, 2002

//:://////////////////////////////////////////////

// Counting the actual date from Year0 Month0 Day0 Hour0 in hours

int GetHourTimeZero()

{

   int iHourTimeZero;

   iHourTimeZero = (GetCalendarYear()-1)*12*28*24 + (GetCalendarMonth()-1)*28*24 + (GetCalendarDay()-1)*24 + GetTimeHour();

   return iHourTimeZero;

}



// The main function placed in the onRest event

void main()

{

  object oPlayer = GetLastPCRested();

  object oBedroll;

  int iRestPossible;

  int iRestDelay = 8; //The ammount of hours a player must wait between Rests

  // Allow Resting if the player carries a "Bedroll"

  if (GetIsObjectValid(oBedroll=GetItemPossessedBy(oPlayer,"Bedroll")))

  {

    SetLocalInt (oPlayer, "iRestAllowed", 1);

  }

  // Prevent the player from resting before the RestDelay is over

  if (GetHourTimeZero() < GetLocalInt (oPlayer, "iNextRestHourTimeZero"))

  {

    SetLocalInt (oPlayer, "iRestAllowed", 0);

    SendMessageToPC (oPlayer, "You must wait " + IntToString (GetLocalInt (oPlayer, "iNextRestHourTimeZero")-GetHourTimeZero()) + " hours before resting again.");

  }

  if (GetLastRestEventType() == REST_EVENTTYPE_REST_STARTED)

    {

      // Check if Player is allowed to Rest (Bedroll used or in Inventory)

      if (GetLocalInt (oPlayer, "iRestAllowed") != 1)

        {

          AssignCommand (oPlayer, ClearAllActions()); //Prevent Resting

          SendMessageToPC (oPlayer, "You may not rest now.");

        }

      else

        {

          SetLocalInt (oPlayer, "iRestAllowed", 0);

          SetLocalInt (oPlayer, "iNextRestHourTimeZero", GetHourTimeZero()+iRestDelay);

        }

    }

}





The script placed in the onUsed event of the "Rest-allowing-placable":

NWScript:
//Function placed on the onUsed Event of a placeable item that allows resting (a bed, or a bedroll...)

void main()

{

  object oLastUser = GetLastUsedBy();

  if (GetIsPC (oLastUser) && GetIsObjectValid (oLastUser))

  {

    SetLocalInt (oLastUser, "iRestAllowed", 1);

    AssignCommand (oLastUser, ActionRest());

  }

}