0 votes
by (950 points)

Hey everyone :D

It got to my mind that it would be cool if I could somewhat "access" the progression of a pregnancy. This, I better try to explain as best as possible. So this is a the line from my pregnancy code that inflicts a pregnancy... it does set the length of a pregnancy and randomly adds or takes away some days from the amount of time the pregnancy lasts in total:

<<run $birth.setDate($birth.getDate() + 266 + random(-21, 14))>>\

I wonder if I can find a code that I can include into the PassageFooter that runs along and somewhat shows progression of how far along the pregnancy is. Like week by week till the end. I didn't decide yet whether to just put that into every passage the character is walking in or just in a Mirror Passage to display only when you look into the mirror... If I was to put that into a mirror I don't need that in the PassageFooter but if that was to go on every passage then maybe it's better of in the footer passage...

I probably will have to take the $birth variable for it since that is also the variable that is counting in the background how long till the birth happens.

I was thinking about something like this:

<<if $birth.getDate() lt 7>>
You are in the 1st week of your pregnancy
<</if>
//this then for each week... 14(days) for 2nd week, 21(days) for the 3rd week etc

Like that it would be a lot of code but I fail to think of something better... Maybe I am completely off on it...

Thank you in advance for helping me :)

*waves*

Mr. Peppermint

1 Answer

+1 vote
by (159k points)
edited by
 
Best answer

Your question can be broken down into two main parts:

1. Determine the state of the pregnancy progression.

The generally formula for this is.

(Estimated) Date of Conception + Average Gestation Period (in days) = Estimated Due Date.

... you will notices that the Due Date is an estimate because in real-life we don't actually know exactly when the birth will occur, unlike your game where you have the $birth variable with the exact date-time the baby is due.

The pregnancy progression can be also be expressed in two forms:
a. Period between the (Estimated) Date of Conception and Now. (eg. 10 weeks along)
b. Period between Now and the (Estimated) Due Date. (eg. 10 weeks to go.)

So you need to decide two things:

1. Do you want the "pregnancy progression" to be an estimate like it is in real life or not?
2. Do you want it to be calculated based on the (Estimated) Date of Conception or the (Estimated) Due Date?

How to calculate the number of weeks between two Date objects.

Detailed breakdown.

<<set $gameDate to new Date()>>\
<<set $birth to clone($gameDate)>>\
<<run $birth.setDate($birth.getDate() + 266 + random(-21, 14))>>\

current game date: <<= $gameDate>>
birth: <<= $birth>>

<<set _difference to $birth.getTime() - $gameDate.getTime()>>\
the milliseconds between the two dates: _difference

the milliseconds as seconds: <<= _difference / 1000>>
the milliseconds as minutes: <<= _difference / (1000 * 60)>>
the milliseconds as hours: <<= _difference / (1000 * 60 * 60)>>
the milliseconds as days: <<= _difference / (1000 * 60 * 60 * 24)>>
the milliseconds as weeks: <<= _difference / (1000 * 60 * 60 * 24 * 7)>>

Condensed version.

<<set $gameDate to new Date()>>\
<<set $birth to clone($gameDate)>>\
<<run $birth.setDate($birth.getDate() + 266 + random(-21, 14))>>\

current game date: <<= $gameDate>>
birth: <<= $birth>>

<<set _weeks to ($birth.getTime() - $gameDate.getTime()) / 604800000>>\
the weeks between the two dates: _weeks

... both of the above use the <Date>.getTime() function to convert a Date object into a integer representing the number of milliseconds that have pasted since a special predetermined point in time.

EDIT: The following is based on your decisions.

Because you have chosen to based things on the Estimated Date of Conception then you will need to track when that occurred, the following is a replacement for the existing  code you are using to generate the $birth variable. It creates a $conception variable which is currently being set to be exactly the same as the current $gameDate, you may want to randomly change the $conception value by a couple of days depending on the logic of your "Impregnated" event(s) and if the player knows exactly when they occurred.

<<set $gameDate to new Date()>>\
<<set $conception to clone($gameDate)>>\
<<set $birth to clone($gameDate)>>\
<<run $birth.setDate($birth.getDate() + 266 + random(-21, 14))>>\

You can now use custom Javascript functions to determine if the NPC is pregnant, the estimated due date, and then number of weeks since conception. The following should be placed within your Story Javascript area, it relies on the existence of the new $conception variable.

/*
* Math.trunc Polyfill from MDN.
*/
if (!Math.trunc) {
	Math.trunc = function(v) {
		v = +v;
		return (v - v % 1) ||
				(! isFinite(v) || v === 0 ? v : v < 0 ? -0 : 0);
	};
}

/*
* Returns true if NPC is pregnant, otherwise returns false.
*/
setup.isPregnant = function () {
	return State.variables.hasOwnProperty("birth");
};

/*
* Returns the Estimated Due Date (based on date of conception)
* if the NPC is pregnant, otherwise returns null.
*/
setup.estimatedDueDate = function () {
	var due = null;
	if (setup.isPregnant()) {
		var due = clone(State.variables["conception"]);
		due.setDate(due.getDate() + 266);
	}
	return due;
};

/* Returns the number of weeks since the date of conception,
* otherwise returns -1.
*/
setup.weeksPregnant = function () {
	if (! setup.isPregnant()) {
		return -1;	// Not Pregnant!
	}
	
	var conception = State.variables["conception"];
	var now = State.variables["gameDate"];
	var weeks = (now.getTime() - conception.getTime()) / 604800000;

	/* The above weeks calculation could result in a decimal number
	*	(like 4.35) which doesn't make sense in R/L, for this reason
	* the following removes the fractional digit part before returning
	* the value.
	*/
	return Math.trunc(weeks);
};

... and you can used the new custom functions within a Passage like so.

<<silently>>
	<<set $gameDate to new Date()>>\
	<<set $conception to clone($gameDate)>>\
	<<set $birth to clone($gameDate)>>\
	<<run $birth.setDate($birth.getDate() + 266 + random(-21, 14))>>\

	/% Move game forward 3 weeks, %/
	<<run $gameDate.setDate($gameDate.getDate() + 21)>>

	<<set _pregnant to setup.isPregnant()>>
	<<set _due to setup.estimatedDueDate()>>
	<<set _weeks to setup.weeksPregnant()>>
<</silently>>
\conception: <<= $conception>>
birth: <<= $birth>>
now: <<= $gameDate>>

The NPC is <<if _pregnant>>pregnant!<<else>>not pregnant!<</if>>
The NPC's estimated due date is: <<= _due>>
The NPC has been pregnant for <<= _weeks>> weeks.

note: you don't have to store the values returned by the custom function within variables, I just did that to make the above easier to understand.

2. Displaying status messages and the like based on the pregnancy progression.

How you do this greatly depends on exactly what you want to inform the Player about!

If you want to indicate that the NPC is having morning sickness then you could do something like the following.

<<if setup.weeksPregnant() is 6>>The NPC quickly rushed to the bathroom sink to throw up, you wonder they are pregnant.<</if>>

... or a range of outputs like the following.

<<set _weeks to setup.weeksPregnant()>>
<<if _weeks is 6>>The NPC has stated showing the signs of having morning sickness.
\<<elseif _weeks gt 6 and _weeks lte 10>>The NPC has stated showing the next signs being pregnant....
\<</if>>

 

by (950 points)
edited by

1. Do you want the "pregnancy progression" to be an estimate like it is in real life or not?

Reply: I would like it to be like in real life hence I set up 266 + random like it was suggested I think you wrote it in an earlier post... the access to the weeks progression is just meant to have something so I can display the progression in anyway like throwing up in the earlier stage of pregnancy and the weight gain, breasts getting bigger and harder, lactation etc... Like that I could trigger eventually an event that the player runs to the bathroom to throw up... or just display the changes openly.

2. Do you want it to be calculated based on the (Estimated) Date of Conception or the (Estimated) Due Date? 

Reply: I believe, calculating it on the estimated date of conception is better in that case as it is to track about the beginning changes like morning sickness etc... What do you think?

By the way, I didn't think it would be this complicated to narrow that down... jesus so many miliseconds for one week ... So that sets the variable _weeks up ...but how to lastly use it to show the text I want it to show like lets say in the first 4 weeks you feel sick in the morning...

I probably have to set up _week1 and week2 etc by calculating the miliseconds... or _month1 and month2 etc so like that I can trigger each month or each week seperatedly...right?

Thank you for looking at it ;)

*waves*

Mr. Peppermint

 

by (159k points)
I have modified my original answer based on your decisions.
by (950 points)
Thank you very much, greyelf :D

That was what I was looking for ... Have a happy new year :)

*waves*

Mr. Peppermint
This site has been placed in read-only mode. Please use the Interactive Fiction Community Forum instead.
...