Hop, donc.
A mettre dans un daytime_inc à inclure dans les scripts concernés.
Pour l'utiliser, il y a deux types de fonctions.
- onDawn et onDusk peuvent se mettre dans un onModuleLoad, dans le onSpawn d'une créature, etc. Ils feront exécuter à la cible le script précisé, à chaque aube (dawn) ou crépuscule (dusk).
Exemple : onDusk("backhome", oPNJ) fera exécuter "backhome.nss" à oPNJ chaque soir.
- InitializeDawnEvent et InitializeDuskEvent peuvent se placer aussi dans un onModuleLoad, dans le onSpawn d'une créature, etc. Ils activeront l'UserDefined Event correspondant, à chaque aube ou crépuscule. Le "event number" qui correspond est défini au début de la lib, par EVENT_DAWN et EVENT_DUSK, et il peut être récupéré dans le onUserDefined par un GetUserDefinedEventNumber() classique.
Je vais le tester dans les jours qui viennent, s'il y a des corrections à apporter je préviendrai.
//::///////////////////////////////////////////////
//:: Dawn and Dusk events and script scheduler
//:: daytime_inc
//:: (Feel free to use or modify)
//:://////////////////////////////////////////////
/*
Calls a script or signals an event
when dawn or dusk
*/
//:://////////////////////////////////////////////
//:: Created By: Gad, for Faltazia
//:: Created On: Sep 9, 2003
//:://////////////////////////////////////////////
// DECLARATIONS
int EVENT_DAWN = 100;
int EVENT_DUSK = 101;
// Make oTarget run sScript on each dawn
void onDawn(string sScript, object oTarget = OBJECT_SELF);
// Make oTarget run sScript on each dusk
void onDusk(string sScript, object oTarget = OBJECT_SELF);
// Signal UserDefined Event EVENT_DAWN on each dawn for oTarget
void InitializeDawnEvent(object oTarget = OBJECT_SELF);
// Signal UserDefined Event EVENT_DUSK on each dusk for oTarget
void InitializeDuskEvent(object oTarget = OBJECT_SELF);
// FUNCTIONS
int DAYTIME_DAWN = 0;
int DAYTIME_DUSK = 1;
void RunDailyScript(string sScript, object oTarget)
{
ExecuteScript(sScript, oTarget);
DelayCommand(HoursToSeconds(24) - HoursToSeconds(GetTimeMinute()) / 60, RunDailyScript(sScript, oTarget));
}
void SignalDailyEvent(object oTarget, int iEvent)
{
SignalEvent(oTarget, EventUserDefined(iEvent));
DelayCommand(HoursToSeconds(24) - HoursToSeconds(GetTimeMinute()) / 60, SignalDailyEvent(oTarget, iEvent));
}
void WaitForDawnOrDusk(object oTarget, int DayTime, string sScript)
{
if (GetIsDawn())
SetLocalInt(GetModule(), "DawnHour", GetTimeHour());
if (GetIsDusk())
SetLocalInt(GetModule(), "DuskHour", GetTimeHour());
if (((DayTime == DAYTIME_DAWN) && (GetIsDawn())) || ((DayTime == DAYTIME_DUSK) && (GetIsDusk())))
if (sScript != "")
RunDailyScript(sScript, oTarget);
else
SignalDailyEvent(oTarget, EVENT_DAWN * (DayTime == DAYTIME_DAWN) + EVENT_DUSK * (DayTime == DAYTIME_DUSK));
else
DelayCommand(HoursToSeconds(1), WaitForDawnOrDusk(oTarget, DayTime, sScript));
}
void onDawnOrDusk(object oTarget, int DayTime, string sScript = "")
{
if (!GetLocalInt(GetModule(), "DawnAndDusk initialized"))
{
SetLocalInt(GetModule(), "DawnHour", -1);
SetLocalInt(GetModule(), "DuskHour", -1);
SetLocalInt(GetModule(), "DawnAndDusk initialized", TRUE);
}
int iEventHour;
if (DayTime == DAYTIME_DAWN)
iEventHour = GetLocalInt(GetModule(), "DawnHour");
else
iEventHour = GetLocalInt(GetModule(), "DuskHour");
if (iEventHour != -1)
{
int CurrentHour = GetTimeHour();
if (iEventHour < CurrentHour)
iEventHour += 24;
if (sScript != "")
DelayCommand(HoursToSeconds(iEventHour - CurrentHour), RunDailyScript(sScript, oTarget));
else
DelayCommand(HoursToSeconds(iEventHour - CurrentHour), SignalDailyEvent(oTarget, DayTime));
}
else
DelayCommand(HoursToSeconds(60 - GetTimeMinute()) / 60, WaitForDawnOrDusk(oTarget, DayTime, sScript));
}
void onDawn(string sScript, object oTarget = OBJECT_SELF)
{
onDawnOrDusk(oTarget, DAYTIME_DAWN, sScript);
}
void onDusk(string sScript, object oTarget = OBJECT_SELF)
{
onDawnOrDusk(oTarget, DAYTIME_DUSK, sScript);
}
void InitializeDawnEvent(object oTarget = OBJECT_SELF)
{
onDawnOrDusk(oTarget, DAYTIME_DAWN);
}
void InitializeDuskEvent(object oTarget = OBJECT_SELF)
{
onDawnOrDusk(oTarget, DAYTIME_DUSK);
}