I feel like a noob

Post » Mon Sep 14, 2009 10:47 am

Well I have hit another halt in my AI mod, I want to make a function where I use days of the week.

I know I could do something like:

short day_of_the_weekif (Dayspassed == 1)  set day_of_the_week to 1endif;continue to seven raising by one each timeif (dayspassed == 8)  set day_of_the_week to 1endifif (Dayspaseed == 9)set day_of_the_week to 2endif;continue to 14if(dayspassed == 15)set day_of_the_week to 1endif;etc;etc;etc


But this has two issues,

one it ends so if someone has played for more then the codded days this will not work for them.

It is time consuming as every day must be codded.

Is there another better way to do this?
User avatar
Alexis Estrada
 
Posts: 3507
Joined: Tue Aug 29, 2006 6:22 pm

Post » Mon Sep 14, 2009 12:41 am

Since I have some idea where you are headed with this I wonder if the variable day_of_the_week would be more useful as a global short. That way any NPC could reference it in his or her schedule script. This script could run as a global script to continuously update the value for day_of_the_week (it is an efficient script). It is just a thought and not a problem is you make these calculations locally. Here is a script as you have envisioned it:

Begin SetDayOfTheWeekshort currentDayshort storeDaysPassedshort day_of_the_week ; or make global and do not declareif ( currentDay == Day ) ; make do-once (and efficient)    returnendifif ( currentDay == 0 ) ; initial condition    set currentDay to Dayendifset storeCurrentDay to DaysPassed ; never write to DaysPassed!while ( storeCurrentDay >= 8 )    set storeCurrentDay to ( storeCurrentDay - 7 )endwhileif ( storeCurrentDay == 1 )    set day_of_the_week to 1elseif ( storeCurrentDay == 2 )    set day_of_the_week to 2elseif ( storeCurrentDay == 3 )    set day_of_the_week to 3elseif ( storeCurrentDay == 4 )    set day_of_the_week to 4elseif ( storeCurrentDay == 5 )    set day_of_the_week to 5elseif ( storeCurrentDay == 6 )    set day_of_the_week to 6elseif ( storeCurrentDay == 7 )    set day_of_the_week to 7endifBegin SetDayOfTheWeek

This code has not been tested. No acknowledgement necessary.
User avatar
John Moore
 
Posts: 3294
Joined: Sun Jun 10, 2007 8:18 am

Post » Mon Sep 14, 2009 4:06 am

Ah that is what I was missing thanks for clearing this up!

I'll add my own touch to it to make it my own, as I don't believe in copying other's work!

Thanks this will help alot, but if it is a global variable and I used a global script to make this would it effect all scripts made or would I have to set this up for every script I need to use it in?
User avatar
chloe hampson
 
Posts: 3493
Joined: Sun Jun 25, 2006 12:15 pm

Post » Mon Sep 14, 2009 12:10 pm

If you declare the global variable day_of_the_week any local script (or global script for that matter) can read it for use in its own code. The local scripts do not have to assign it a value, your version of the code I offered as a start script will update the value of day_of_the_week each new day. For that matter, if you write this as a global script you need only assign a value to day_of_the_week using my algorithm once, and then each new day increase its value by one. This is possible since no rest or fast travel is longer than 24 hours so you know that the global will run each day at some time. The additional code might look like this:

if ( set day_of_the_week == 7 )    set day_of_the_week to 1else    set day_of_the_week to ( day_of_the_week + 1 )endif

By the way, there was typo in my earlier script. The last line should read: End SetDayOfTheWeek. Cut-n-paste burns me again.

If you really want to make your script unique from mine, look at the modulo algorithm at the http://www.uesp.net/wiki/Tes3Mod:Math_Functions. I thought the Math Mod had some method for modulo mathematics, but I was rushed earlier today and did not have time to confirm. It is much more elegant than my solution. In case you are wondering: it works because assigning a quotient to a short variable truncates it rather than rounds it to an integer.
User avatar
herrade
 
Posts: 3469
Joined: Thu Apr 05, 2007 1:09 pm

Post » Mon Sep 14, 2009 7:42 am

EDIT: This was a bit rewitten as I didn't read the entire thread before writing this.

In cyran0's script this section is used to find the remainder when storeCurrentDay is divided by 7. In other words it is a simple way of preforming a modulo operation.
while ( storeCurrentDay >= 8 )    set storeCurrentDay to ( storeCurrentDay - 7 )endwhile


Unfortunately Morrowind doesn't have a modulo function, however it is possible with some simple maths to implement something similar that avoids the while loop (imagine in days passed was 30,000. The while loop would have to execute 4285 times!)

Taking the first day of the week as being 0, it is obvious that:
DaysPassed = weekNum * 7 + WeekDay        [1]


As we know that WeekDay < 7:
WeekNum = floor(DaysPassed/7)        [2]


Therefore, rearranging equation [1] and substituting in [2] gives
WeekDay = DaysPassed - WeekNum*7               = DaysPassed - floor(DaysPassed/7)*7         [3]


I might be a bit out, so there may need to be the odd correction to that, but something similar should work. cyran0 said that when converting from a real number to an integer, Morrowind trunicates the number. So if we converted 3.95 into an integer it would be 3. So converting from an float to a short (integer) has the same effect as a floor function.

So for example:
float xshort yset x to 3.9set y to x ;convert 3.9 to an integer;y is now 3


Converting the equation [3] into Morrowind code
short weekDayshort weekNumset weekNum to ( DaysPassed / 7 ) ;=> floor(DaysPassed/7), as we are converting the result into a shortset weekDay to ( DaysPassed - ( weekNum * 7 ) )

User avatar
SHAWNNA-KAY
 
Posts: 3444
Joined: Mon Dec 18, 2006 1:22 pm

Post » Mon Sep 14, 2009 3:11 pm

I have added this info to the UESP page
http://www.uesp.net/wiki/Tes3Mod:Math_Functions

It probably messes up on negative numbers (Due to the fact that what I wrote is only true for positive numbers), but that is something I will check at some piont
User avatar
MarilĂș
 
Posts: 3449
Joined: Sat Oct 07, 2006 7:17 am


Return to III - Morrowind