Author Topic: Math problem with SetTime event [Solved]  (Read 1190 times)

I made a SetTime event for the Environmental Control event pack, but I cant get my head around the math. I'm fairly sure its simple math, but I've tried for hours now and I just cant work it out.
I have 4 variables; Offset, Time, SunTime and Amount. SunTime should be Time+Offset. I want to change Offset so that SunTime = Amount.
Time and Offset have to be between 0 and 1.

Relevant Code, in case i'm derping somewhere else, and its really obvious:
Code: [Select]
//Basically, since you cant even detect where the sun is, i use Lugnut's code below
//to work out the time its on, then calculate a fraction and move the offset by that.

%offset = $EnvGuiServer::dayOffset;
switch(%mode)
{
case 0: //set
%time = (getDayCycleTime()/$EnvGuiServer::dayLength);
%offset -= (%time+%Offset)-%amount;
case 1: //increase
%offset += %amount;
}
if(%offset > 1)
%offset -= 1;
if(%offset < 0)
%offset += 1;
serverCmdEnvGui_SetVar(FakeAdminClient,"DayOffset",%offset);

I've tried working it out, writing it down, I even made a flash app to help me, but nothing's working. If you can, go through it one step at a time, so I can see where i'm getting confused.
« Last Edit: October 07, 2012, 05:28:59 PM by boodals 2 »

are you referring to the buggy piece of horrible stuff that went through 15 versions in a week?
yeaaaaaaaah....

just fyi, you probably already know this, but if you take the day length, and multiply it by the offset and multiply the total by 100, you'll probably get the percentage of completion of the day

Wait, Day Length * Offset * 100 = percentage of day complete? Where does the completion part come from?

%progressNow is a number from 0 to 1:

Code: [Select]
    $dc = DayCycle;
    %daysElap = $sim::time / $dc.dayLength;
    %progressNow = %daysElap - mFloor(%daysElap) + $dc.dayOffset;

%progressNow is a number from 0 to 1:

Code: [Select]
    $dc = DayCycle;
    %daysElap = $sim::time / $dc.dayLength;
    %progressNow = %daysElap - mFloor(%daysElap) + $dc.dayOffset;
Oooh, so thats a new way to get the time, right? Neat. Much smaller than Lugnut's.
Is there any point in using it over Lugnut's?
Code: [Select]
%t = getSimTime();
%l = DayCycle.dayLength;
%t = %t / 1000;
%r = mFloor(%t % %l);
for(%i=0; %r < %l; %i++)
{
%r -= %l;
}
return %r;
}

 - I use $dc to abbreviate "DayCycle" through my whole script
 - I skip a step and get the sim time variable directly
 - I'm sure Lugnut does this later in the script, but I take into account the custom offset

- I'm sure Lugnut does this later in the script, but I take into account the custom offset
Yeah. I think i actually edited that out so I could have more control in the final math. Well, ill be using your code from now on, just because its shorter.
Also, i'll leave in the offset and see if that helps my brain..
Thanks for the help.

The offset is important so that you can obey the host's settings, but since it obviously won't affect day length and if the day is short enough, it doesn't do too much.

kalphiters is shorter and loveier and less riddled with annoying problems than mine

but they use the same premise




use his

%progressNow is a number from 0 to 1:

Code: [Select]
   $dc = DayCycle;
    %daysElap = $sim::time / $dc.dayLength;
    %progressNow = %daysElap - mFloor(%daysElap) + $dc.dayOffset;
%daysElap - mFloor(%daysElap) is actually between 0 and 1 until you add the offset, then it's between the offset and the offset + 1.

%daysElap - mFloor(%daysElap) is actually between 0 and 1 until you add the offset, then it's between the offset and the offset + 1.

You're right, I converted it to a portion of 360 then fixed it so it's in the proper range.

Add this:

Code: [Select]
$dc = DayCycle;
%daysElap = $sim::time / $dc.dayLength;
%pn = %daysElap - mFloor(%daysElap) + $dc.dayOffset;

while(%pn >= 1)
    %pn -= 1;
while(%pn < 0)
    %pn += 1;

%progressNow = %pn;

Code: [Select]
function SetDayCycleTime(%time)
{
dayCycle.setDayOffset(%time-getDayCycleTime());
}
function getDayCycleTime()
{
%daysPassed = $Sim::Time/DayCycle.dayLength;
%dayTime = %daysPassed - mFloor(%daysPassed);
while(%dayTime >= 1)
%dayTime--;
while(%dayTime < 0)
%dayTime++;
return %dayTime;
}
Thanks to Kalphiter.

Code: [Select]
function SetDayCycleTime(%time)
{
dayCycle.setDayOffset(%time-getDayCycleTime());
}
function getDayCycleTime()
{
%daysPassed = $Sim::Time/DayCycle.dayLength;
%dayTime = %daysPassed - mFloor(%daysPassed);
while(%dayTime >= 1)
%dayTime--;
while(%dayTime < 0)
%dayTime++;
return %dayTime;
}
Thanks to Kalphiter.
I cant see where this takes in the current offset, if it does at all. Tested it in game, it did nothing.. Here's current code:
Code: [Select]
function fxDtsBrick::env_time(%this,%mode,%amount) //By Boodals :D
{
if(isPackage("VCE_Main"))
{
%check = filterVariableString(%amount,$inputTarget_Self);
if(%check !$= %amount)
{
%amount = %check;
}
}
//Basically, since you cant even detect where the sun is, i use Kalphiter's code below
//to work out the time its on, then move the offset based off the time.
//Credits to Plexious <3

%offset = $EnvGuiServer::dayOffset;
switch(%mode)
{
case 0: //set
%offset = %amount-getDayCycleTime();
case 1: //increase
%offset += %amount;
}
if(%offset > 1)
%offset -= 1;
if(%offset < 0)
%offset += 1;
serverCmdEnvGui_SetVar(FakeAdminClient,"DayOffset",%offset);
}

function getDayCycleTime() //Credits to Kalphiter and Lugnut <3
{
%daysPassed = $Sim::Time/DayCycle.dayLength;
%dayTime = %daysPassed - mFloor(%daysPassed);
while(%dayTime >= 1)
%dayTime--;
while(%dayTime < 0)
%dayTime++;
return %dayTime;
}
What am I doing wrong?

Jasa decided to erase my offset piece.

Jasa decided to erase my offset piece.
I'm not sure how to explain it, but setDayCycleTime() works fine even if getDayCycleTime() doesn't take in to account the offset.

setDayCycleTime(0); sets the daycycle to when the sun first rises.
setDayCycleTime(0.29); sets the daycycle to mid-day.
setDayCycleTime(0.58); sets it to the start of night.
I cant see where this takes in the current offset, if it does at all. Tested it in game, it did nothing.. Here's current code:
What am I doing wrong?
Just use setDayCycleTime();
The offset can be less than 0 and greater than 1 without causing any problems.