Author Topic: Total math / script failure  (Read 328 times)

Legacy_Fester Pot

  • Hero Member
  • *****
  • Posts: 1698
  • Karma: +0/-0
Total math / script failure
« on: August 01, 2012, 05:47:52 pm »


               I just can't get this to work. I'm attempting to modify a day script, which works great, and modify it to save as a night script for something else.

Problem is, either the placeable never spawns after DUSK, or it is destroyed and recreated in an infinate loop.

For reference, I'm posting the working DAY script first, followed by my attempt at modifying it for a NIGHT script.

DAY SCRIPT [WORKS]

//::///////////////////////////////////////////////
//:: Name: Light Heartbeat Autoupdater  -FIXED!!!
//:: FileName: light_htb
//:: Copyright © 2001 Bioware Corp.
//:: Created By: Grzech_U (gk6)
//:: Created On: 02.04.2007
//:://////////////////////////////////////////////

//<EDIT HERE>
//enter the time of dawn in Your mod
const int DAWN = 6;
//enter the time of dusk in Your mod
const int DUSK = 18;
//enter the number of mintes in one game hour (default is 2)
//seting it to 0 will cause the script to ignore minutes
//may be buggy...
const int MINUTES_PER_HOUR = 2;
//</EDIT HERE>

#include "nw_i0_2q4luskan"
void main()
{
int nCurrentTime = GetTimeHour();
//if its night, destroy the light and create a new light at dawn
if ((nCurrentTime > DUSK) || (nCurrentTime < DAWN))
    {
    location lLocation = GetLocation(OBJECT_SELF);
    string sResRef = GetResRef(OBJECT_SELF);
    DestroyObject(OBJECT_SELF, 0.1);
    //when is the next dawn?
    int nTimeHour;
    if (nCurrentTime>DAWN)
        {
        nCurrentTime=24 -nCurrentTime;
        nTimeHour =nCurrentTime + DAWN;
        }
    else nTimeHour = DAWN -nCurrentTime;
    float fTimeHour = HoursToSeconds(nTimeHour);

    float fTimeMinutes;
    if(MINUTES_PER_HOUR!=0)
        {

        if(GetTimeMinute()>0)
            {
             fTimeMinutes=IntToFloat(MINUTES_PER_HOUR-GetTimeMinute())* HoursToSeconds(1)/60.0;
            }
        }
    AssignCommand( GetModule(),
                DelayCommand(fTimeHour + fTimeMinutes,
                CreateObjectVoid(OBJECT_TYPE_PLACEABLE, sResRef, lLocation)));
    }
}


And this is the attempted modified NIGHT script, which does not work.

//::///////////////////////////////////////////////
//:: Name: Light Heartbeat Autoupdater  -FIXED!!!
//:: FileName: light_htb
//:: Copyright © 2001 Bioware Corp.
//:: Created By: Grzech_U (gk6)
//:: Created On: 02.04.2007
//:://////////////////////////////////////////////

//<EDIT HERE>
//enter the time of dawn in Your mod
const int DAWN = 6;
//enter the time of dusk in Your mod
const int DUSK = 18;
//enter the number of mintes in one game hour (default is 2)
//seting it to 0 will cause the script to ignore minutes
//may be buggy...
const int MINUTES_PER_HOUR = 2;
//</EDIT HERE>

#include "nw_i0_2q4luskan"
void main()
{
int nCurrentTime = GetTimeHour();
//if its day, destroy the light and create a new light at night
if ((nCurrentTime > DAWN) || (nCurrentTime < DUSK))

    {
    location lLocation = GetLocation(OBJECT_SELF);
    string sResRef = GetResRef(OBJECT_SELF);
    string sTime = IntToString(nCurrentTime);
    AssignCommand(GetFirstPC(), SpeakString("DEBUG: DESTROYED AT " + sTime));
    DestroyObject(OBJECT_SELF, 0.1);

    //when is the next dusk?

   // **** I AM THINKING IT IS THIS SECTION THAT IS CAUSING MY PROBLEM

    int nTimeHour;
    if (nCurrentTime>DAWN)
        {
        nCurrentTime=24 -nCurrentTime;
        nTimeHour =nCurrentTime + DAWN;
        }
    else nTimeHour = DAWN -nCurrentTime;

   // *******

    float fTimeHour = HoursToSeconds(nTimeHour);

    float fTimeMinutes;
    if(MINUTES_PER_HOUR!=0)
        {

        if(GetTimeMinute()>0)
            {
             fTimeMinutes=IntToFloat(MINUTES_PER_HOUR-GetTimeMinute())* HoursToSeconds(1)/60.0;
            }
        }
    AssignCommand( GetModule(),
                DelayCommand(fTimeHour + fTimeMinutes,
                CreateObjectVoid(OBJECT_TYPE_PLACEABLE, sResRef, lLocation)));
    }
}


I have marked with *** where I -think- the problem is but I can't figure out how to rewrite it to suit my needs. So, I'm dipping into the community help desk once again!

Thanks,

FP!
               
               

               
            

Legacy_Carcerian

  • Hero Member
  • *****
  • Posts: 1655
  • Karma: +0/-0
Total math / script failure
« Reply #1 on: August 01, 2012, 06:11:56 pm »


               For testing, try a copy of the working script except swap the dawn and dusk values. (ie make it Dusk 'til Dawn)

1. Find/Replace all instances of "DUSK" with "TEMP1"
2. Find/Replace all instances of "DAWN" with "DUSK"
3. Find/Replace all instances of "TEMP1" with "DAWN"

Remember to swap the DUSK and DAWN constants as well...

(change comments as applicable)

See if that works.

(and yes that the ****** region looks like were your problem is, the dawn should be dusk?)
               
               

               


                     Modifié par Carcerian, 01 août 2012 - 05:18 .
                     
                  


            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Total math / script failure
« Reply #2 on: August 01, 2012, 06:29:09 pm »


               Also please post the function code for CreateObjectVoid().  The way it is written you should not have non-positive delays, so I suspect the quirky behavior might lie there.

One real problem I see is that you have done the logical negation wrong.

The dawn script condition for looking at night:

if ((nCurrentTime > DUSK) || (nCurrentTime < DAWN))  

should negate to this night script condition for looking at day

if ((nCurrentTime > DAWN) && (nCurrentTime < DUSK))

(Technically it isn't a true negation since the == circumstance is not accounted for).
               
               

               
            

Legacy_Fester Pot

  • Hero Member
  • *****
  • Posts: 1698
  • Karma: +0/-0
Total math / script failure
« Reply #3 on: August 02, 2012, 02:52:27 am »


               Well, making the modifications to reverse the script works, with WhiZard's suggested change as well.

The placeable is destroyed during the day, as required.

When 18hrs comes along, the placeable is recreated as expected when night begins to fall.

//::///////////////////////////////////////////////
//:: Name: Light Heartbeat Autoupdater  -FIXED!!!
//:: FileName: light_htb
//:: Copyright © 2001 Bioware Corp.
//:: Created By: Grzech_U (gk6)
//:: Created On: 02.04.2007
//:://////////////////////////////////////////////

//<EDIT HERE>
//enter the time of DAWN in Your mod
const int DAWN = 6;
//enter the time of DUSK in Your mod
const int DUSK = 18;
//enter the number of mintes in one game hour (default is 2)
//seting it to 0 will cause the script to ignore minutes
//may be buggy...
const int MINUTES_PER_HOUR = 2;
//</EDIT HERE>

#include "nw_i0_2q4luskan"
void main()
{
int nCurrentTime = GetTimeHour();
//if its night, destroy the light and create a new light at dawn
if ((nCurrentTime > DAWN) && (nCurrentTime < DUSK))
   {
   location lLocation = GetLocation(OBJECT_SELF);
   string sResRef = GetResRef(OBJECT_SELF);
   DestroyObject(OBJECT_SELF, 0.1);   // WORKS OKAY!
   //when is the next DUSK?
   int nTimeHour;
   if (nCurrentTime>DUSK)
       {
       nCurrentTime=24 -nCurrentTime;
       nTimeHour =nCurrentTime + DUSK;
       }
   else nTimeHour = DUSK -nCurrentTime;
   float fTimeHour = HoursToSeconds(nTimeHour);

   float fTimeMinutes;
   if(MINUTES_PER_HOUR!=0)
       {

       if(GetTimeMinute()>0)
           {
            fTimeMinutes=IntToFloat(MINUTES_PER_HOUR-GetTimeMinute())* HoursToSeconds(1)/60.0;
           }
       }
   AssignCommand( GetModule(),
               DelayCommand(fTimeHour + fTimeMinutes,
               CreateObjectVoid(OBJECT_TYPE_PLACEABLE, sResRef, lLocation)));
   }
}


As for the function code for CreateObjectVoid(), it is below for reference.

//Creates a creature with a specific string at a specified location
void CreateObjectVoid(int nObjectType, string sTemplate, location lLoc, int bUseAppearAnimation = FALSE)


Thanks for the help!

FP!
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Total math / script failure
« Reply #4 on: August 02, 2012, 07:25:09 am »


               Oh well, Looks like I am late to the party.  Here are my reworked versions.   Untested at this point.

//::///////////////////////////////////////////////
//:: Name: Light Heartbeat Autoupdater  -FIXED!!!
//:: FileName: light_htb
//:: Copyright © 2001 Bioware Corp.
//:: Created By: Grzech_U (gk6)
//:: Created On: 02.04.2007
//:://////////////////////////////////////////////

//<EDIT HERE>
//enter the time of dawn in Your mod
const int DAWN = 6;
//enter the time of dusk in Your mod
const int DUSK = 18;

//</EDIT HERE>

#include "nw_i0_2q4luskan"
void main()
{
int SECONDS_PER_HOUR = FloatToInt(HoursToSeconds(1));
int SECONDS_PER_DAY  = SECONDS_PER_HOUR * 24;

int nCurrentTime = GetTimeHour()*SECONDS_PER_HOUR + GetTimeMinute() *60 + GetTimeSecond() ;
//if its night, destroy the light and create a new light at dawn
if (GetIsDusk() || GetIsNight())
    {
    location lLocation = GetLocation(OBJECT_SELF);
    string sResRef = GetResRef(OBJECT_SELF);
    DestroyObject(OBJECT_SELF, 0.1);

    //when is the next dawn?
    int nDelay = (DAWN *SECONDS_PER_HOUR - nCurrentTime + SECONDS_PER_DAY )%SECONDS_PER_DAY ;

    AssignCommand( GetModule(),
                DelayCommand( nDelay*1.0,
                CreateObjectVoid(OBJECT_TYPE_PLACEABLE, sResRef, lLocation)));
    }
}
 



//::///////////////////////////////////////////////
//:: Name: Light Heartbeat Autoupdater  -FIXED!!!
//:: FileName: light_htb
//:: Copyright © 2001 Bioware Corp.
//:: Created By: Grzech_U (gk6)
//:: Created On: 02.04.2007
//:://////////////////////////////////////////////
//<EDIT HERE>
//enter the time of dawn in Your mod
const int DAWN = 6;
//enter the time of dusk in Your mod
const int DUSK = 18;
//</EDIT HERE>
#include "nw_i0_2q4luskan"
void main()
{
  int SECONDS_PER_HOUR = FloatToInt(HoursToSeconds(1));
  int SECONDS_PER_DAY  = SECONDS_PER_HOUR * 24;

  int nCurrentTime = GetTimeHour()*SECONDS_PER_HOUR + GetTimeMinute() *60 + GetTimeSecond() ;

  //if its day, destroy the light and create a new light at night
  if (GetIsDawn() || GetIsDay())
    {
    location lLocation = GetLocation(OBJECT_SELF);
    string sResRef = GetResRef(OBJECT_SELF);
    string sTime = IntToString(nCurrentTime);
    AssignCommand(GetFirstPC(), SpeakString("DEBUG: DESTROYED AT " + sTime));
    DestroyObject(OBJECT_SELF, 0.1);

    //when is the next dusk?
    int nDelay = (DUSK *SECONDS_PER_HOUR - nCurrentTime + SECONDS_PER_DAY )%SECONDS_PER_DAY ;

    AssignCommand( GetModule(),
                DelayCommand(nDelay*1.0,
                CreateObjectVoid(OBJECT_TYPE_PLACEABLE, sResRef, lLocation)));
    }
}
 


EDIT: as long as your constants match up with the day and night setting on the module, It should work.
               
               

               


                     Modifié par Lightfoot8, 02 août 2012 - 06:26 .