Fertility Algorithm / Function Ideas

ririmudev

Member
Dec 15, 2018
304
302
Asking for a friend: any ideas on a decent fertility function? Considerations would be:
  1. Basic: periodic, e.g. alternates between high points and low points
  2. Advanced: subject to influence. For example, 2 NPCs who become roommates could gradually converge to a similar pattern.
My first thought is a sinusoidal function, with appropriate variables for frequency and amplitude. But what about the details? It could have a monthly cadence, so that "that time of the month" would be accurate. Could also be bi-weekly, lunar, or something else.

(I could probably do some research and figure everything out myself, but who knows, maybe this will be a fun short-lived thread)
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,130
14,809
Advanced: subject to influence. For example, 2 NPCs who become roommates could gradually converge to a similar pattern.
This is a total myth and would only complicate something that will already probably be too complicated for the benefit it will add to the game.


If usually devs decide that "this time she'll get pregnant", it's because something more realistic can only works if the game cover a really big period of time and imply a real mess when it come to be consistent through the whole game.

Firstly, depending on the players, it will need one to three (sometimes probably more) in-game months before the girl become pregnant. This even considering that every shot marks. Simply because there's no guaranty that a player will be with this girl when it would matters.
I mean, if the player will necessarily have sex with the girl when she can be pregnant, then all players will impregnate the girl at the exact same moment. This mean that there' absolutely no need for a fertility algorithm, you could hard code the moment she'll be pregnant, it will lead to the exact same result.

Secondly, since you have a realistic fertility algorithm, you'll need a realistic pregnancy depiction. This mean that during the three-four first months the girl will not looks pregnant enough for players who want to satisfy their pregnancy fetish. Therefore, your game need to cover at least seven in-game months.
Doing otherwise would be ridiculous. The player will have to fight against your fertility algorithm to get the girl pregnant, then in two months her pregnancy would be finished ? No, of course no.

Thirdly, since the month where a girl will become pregnant will change from a player to another, and because you need a realistic pregnancy, starting the second month and through the end of the game, you'll have to represent the girls in all the possible stage of their pregnancy.
It's easy the second month, she's pregnant or not. But when you'll reach the eleven month, you'll have to cover the none pregnancy stage, the nine months of pregnancy, the childbirth, and the fact that the girl have had a baby. This don't limit to girl with visual because...

Fourthly, even with a purely textual game, you'll have to adapt the story to each the possible pregnancy stages. There's things that a none pregnant girl can do, but that a seven month pregnant one will not be able to do. A girl that is now a mother do not act like a girl who isn't pregnant. And the said not pregnant girl will not act the same way than a mother to be.
You'll also have to take count of the morning sickness, because it's how realistic pregnancy is, the mood change, and the girl talking about her pregnancy. Once again, all the iteration having to coexist since the girls will not become pregnant at the same moment depending on the player choices.


What lead to the most important question: Did all this worth it ?
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
995
modeling it sounds good, I usually just make math do what 'i want' (ie effects, mechanics) rather than anything accurate

1 a periodic signal: either a sine wave or a sawtooth wave ( 0 to 100%, resets after every 30 days= (x_day%31) / 30 where % = modul or Remainder operator, sawtooth is much simpler to work with than trying to deal with trig)

2 scale the signal through a gate: unless this is for a simulation running at large scale (say over 100 characters) that's fast running (say a graph updates ever second or so = 1 month per second/update), there is no way for people to 'perceive' the nuances of Y%, at best you have <10, 50%, and >90%, which because human brains don't understand true probability, we think (we expect) <10, 50/50 and >90 to be 0%, 1 out of 2 every other time, and 100% (these are the numbers you would actually have to use under the hood otherwise players will think your code is bad, since at 10% you could still get success every role (think getting Nat 20 in dnd over and over again)

So I would use the result of your periodic function, and do math such that you can detect 3 phases, a low, a high, and a mid.

Say we want characters to have 4 days where they think they are 'safe', so 4 days out of 30 = 4/30 = 0.1333 or <= 13.333% (relative to the 0 to 100% equation, you would have to do something a bit more convoluted if you used sine.), say something similar for 'high' probability, 4 days, or the last 13.3% aka anything >= 86.666% of the sawtooth function. so
You don't have permission to view the spoiler content. Log in or register now.

I also give a basic way to calculate how the phase needs to be corrected (thought code would be easier than writing out paragraphs using words), but how data is sent between characters depends on how you want to architect the system (ie programming language matters), and I kept technical terms since i don't know how much you know and I don't want to go through the first semester of signal processing terminology.
 

preglovr12

Salt is a Way of Life
Moderator
May 1, 2018
2,156
5,601
Ultimately, what's the point? The only thing I see coming from a fert cycle is player frustration, and I can provide first hand knowledge of such if you want. As Anne pointed out, unless you're also going to make the most in depth pregnancy mechanic ever seen, why the realism when your preg seekers are going to all knock a girl up asap and your non-preg fans are gonna have to skip that girls content for the week.
 
  • Like
Reactions: Mint-ER#

ririmudev

Member
Dec 15, 2018
304
302
You don't have permission to view the spoiler content. Log in or register now.
It's worth it. Everything goes in the trash bin without it (ok, it's not that serious).
1. Inapplicable; I think you have a VN in mind. The plan is more analogous to Free Cities mechanically (though not slavery-focused).
2. Time passes in 1-week increments. Part of the design up-front.
3. I'm seeking a realistic function as a starting point, and it will be adapted to provide an appropriate experience. And yes, the target will be text-based to manage scope.
4. Nope, there won't be any story to worry about; not a VN. Small amount of lore, but that's it. Already have conception date accounted for, morning sickness (if I feel like implementing it), notifications, due date, birth date, and more. Unfortunately, probably no mood change (out of scope), though some interactions can become disabled (or enabled).

The extra considerations are appreciated, but I've thought of all that already. May I have my function now?

You don't have permission to view the spoiler content. Log in or register now.
Much thanks. I agree about not being beholden to accuracy. I'll be using a date library, so should be able to access a dayOfMonth(inputDate) kind of function. So probably something like X * Y * sine (dayOfMonth(currentDate) + Z), where X is base fertility (depending on human/elf/etc), Y is some constant that feels right, the sine function varies by month, and Z is... I forget my math lol, something to do with tweaking the offset? Probably don't need Z.

Since you mentioned isPregnant, I'd handle that with event lookups. Basically a conception event would be recorded, with appropriate character ids, genetics, and approximate due date. Actually, I might not have isPregnant. knowsSelfPregnant() would be true at say 5% of the interval, can trigger notifying the person responsible, and then knowsNPCPregnant(npcId) would lookup if the notification happened, or could be automatically true later in the interval.

That's cool you took a shot at the phasing code. I'll have to look through it later. TBH I don't think I'll implement any cycle syncing; just wanted to put it out there.

Ultimately, what's the point? The only thing I see coming from a fert cycle is player frustration, and I can provide first hand knowledge of such if you want. As Anne pointed out, unless you're also going to make the most in depth pregnancy mechanic ever seen, why the realism when your preg seekers are going to all knock a girl up asap and your non-preg fans are gonna have to skip that girls content for the week.
Well, the point is I want what I asked, and it'll be easy enough to diminish or disable the effect. This is a tiny piece of the whole; I'd just done enough thinking for today and wanted to outsource a bit. I think you also have a VN in mind, but that's not the case.

As always, expect nothing to come from me. Maybe something before 2077 though.
 
  • Like
Reactions: Saki_Sliz

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,130
14,809
1. Inapplicable; I think you have a VN in mind. The plan is more analogous to Free Cities mechanically (though not slavery-focused).
I had all kind of games in mind, and what I say apply even with game mechanism like Free Cities. But I'll not starts talking about this, because there's a way bigger issue that you having no clue regarding what pregnancy imply for a girl.

And this issue is this:
2. Time passes in 1-week increments. Part of the design up-front.
Wait, you want a realistic fertility algorithm, thinking about a sinusoidal model for it, this for a game that have one week increment ? God... But what have you been taught in school ?

It shouldn't be the 51yo guy I am that have to do a Biology 101 course for you...


The menstrual cycle follow a lunar month, therefore it's strictly a 28 days cycle. Said otherwise, every 28 days the periods starts, and with them a new cycle starts.
Note: Of course, there's variations and some women never achieve to have a regular menstrual cycle, but for the vast majority, girls have a regular menstrual cycle. If not at first, it will become regular when they'll be 15-16 years old.

Ovulation happen between the 13 and 15 day after the starts of the cycle, and the ovum life expectancy is only 12 hours. Therefore, there's only 2 days every 28 days where a girl can possibly be impregnated, yet for only 12 hours during this period of time.
BUT this is without counting on spermatozoon. Inside the vagina, and the womb, its life expectancy is between 75 and 120 hours. Therefore between 3 and 5 days.
When you put everything together, every 28 days, there's only, at most 5 + 2 days (+ 12 hours if ovulation happen at the very last moment) where a girl can be pregnant. What is... well, one week, and therefore in your case one of your in-game time increment.

Side note: Despite this, for many reasons girls are right to consider a two weeks period as being at risk. Better safe than with an unwanted pregnancy.


Now, what it mean for your game ?

Well, it's simple, it mean that whatever the girl, as long as she isn't part of the exception that have a none regular menstrual cycle, there's only two patterns that can apply to her:
Either she have 2 safe in-game period, and 2 in-game period with a moderate risk of pregnancy, or she'll have 3 safe in-game period, and 1 in-game period with a high risk of pregnancy.
And this pattern being applied to 28 days cycles, it will stay exactly the same whatever month you'll look at.

There, you have what you are looking for. Randomly pick one of those two patterns, and you'll have exactly the same behavior than whatever realistic fertility algorithm you can come with.
Would your game be based on one day increment, it would obviously be something different. But your game is based on one week increment...



4. Nope, there won't be any story to worry about; not a VN.
You understand that when I talk about "story", it was in a global way ? The girls will not just fuck, right ? I mean, you don't want to put a realistic algorithm in top of a game where girls do nothing else than waiting for the player to fuck them, right ?
And it happen that pregnancy will affect what they are doing else than fucking. In fact, it will also affect them when they are fucking.
 
  • Thinking Face
Reactions: DuniX

DiviDreamer

Member
Aug 29, 2020
249
215
Why not make simple percent chance?
But if you really need realistic cycle the it's easy to make simple IF logic
something like
IF it's day 1-9 fertility = 1%
IF day 10-24 fertility = 2-5%
IF day 25-30 fertility = 6-35% (search for real curve on google, add age dependency too)
then for each girl set random offset of same curve.

In my games I use this simple pattern and like to use some fun ideas like
if two girl in same location for 7 days then synchronize theirs cycles, and so on.
 

Tompte

Member
Dec 22, 2017
214
152
Ultimately, what's the point? The only thing I see coming from a fert cycle is player frustration, and I can provide first hand knowledge of such if you want. As Anne pointed out, unless you're also going to make the most in depth pregnancy mechanic ever seen, why the realism when your preg seekers are going to all knock a girl up asap and your non-preg fans are gonna have to skip that girls content for the week.
There's plenty of point, but it depends on the game you're making. I'm working on a simulator game where it made a lot of sense to add it.

I added a fertility cycle to my game because pregnancy is not a main feature, rather it's a random side-effect to having unprotected sex. Having a flat fertility rate meant that pregnancy was an omnipresent danger and it didn't feel right. I also wanted pregnancy to be a rare "happy accident".

With a cycle in place, it limits the risk of pregnancy to a few days every month and players who are paying close attention to the girl's cycle can opt to go for impregnation at that time, or conversely, avoid it by not allowing them to have unprotected sex when the risk is too high.

The implementation was very simple (some replies have already touched on this solution).
I just made a float array with 28 probability values for 28 days (recurring) with a constant multiplier, so I can adjust the fertility rate as a whole and balance the number of pregnancies happening over the course of a typical game. If you plotted these numbers, you would see a curve with a distinct peak.
Code:
private static float[] OvulationCycle = new float[28]
{
    0.00f, 0.00f, 0.00f, 0.00f, 0.00f,    // Safe (menstration)
    0.03f, 0.08f, 0.15f, 0.25f, 0.40f,    // Risky
    0.65f, 1.00f, 1.50f, 1.20f, 0.90f,    // Dangerous (ovulation)
    0.65f, 0.35f, 0.15f, 0.10f, 0.05f,    // Risky (post-ovulation)
    0.03f, 0.02f, 0.02f, 0.01f, 0.01f, 0.01f, 0.01f, 0.01f, // Safe (but non-zero)
};

private const float kBaseProbability = ###;

public float GetBaseFertility()
{
    return kBaseProbability * OvulationCycle[cycleDay % OvulationCycle.Length];
}
Each girl has a random offset (cycleDay) so they're not all in sync. I don't tell the player what part of the cycle the girl is in, nor the exact percentage, instead I use loose terms like "safe day", "risky day" and "dangerous day" because not knowing is part of the fun. I also don't inform them about impregnation until a few days after it has occurred, to surprise players and to deter them from reloading a save.

I recommend giving players an out, like making contraceptive items available for those who prefer to be in control. In my case, I also added an option to disable pregnancy altogether.

A few notes regarding my code:
  • The base multiplier (not shown) is balanced to account for the fact that, in my game, girls can have several sex encounters in a single day.
  • Some days have values above 1 because they're being multiplied by a constant that is less than 1. Therefore I have to exaggerate those values to give those days a more significant risk. It doesn't feel right if a girl has unprotected sex multiple times during the dangerous period and still not get pregnant due to bad rolls. If the player is intentionally going for pregnancy, they would then have to wait another month, which feels bad.
  • Males have a flat fertility rate. Some traits can affect fertility as well (including age). I multiply both parties' fertility rates together to get the final probability.
 
Last edited:

ririmudev

Member
Dec 15, 2018
304
302
You don't have permission to view the spoiler content. Log in or register now.
Ah, I see. So what I need is:
JavaScript:
actor.expressions.fert.core * Y * Math.sin((daySinceEpoch % 28) * 2 * Math.PI / 28)
That wasn't so hard. Half the time it would be less than 0; I'll probably tweak that to make it less realistic. Your solution with the 2 patterns is a good fit for the weekly increments. But I'm sticking with the sine. And thanks for the Bio 101; I never took it, but life turned out fine anyhow.

You don't have permission to view the spoiler content. Log in or register now.
That's good, but sine functions are simple enough too.

You don't have permission to view the spoiler content. Log in or register now.
Ah, a fellow man of culture: not knowing which day is indeed part of the fun. For game purposes I'd probably reduce the number of safe days, but that's all. Hmm... was going to say more, but in short I pretty much agree with your whole post.
 

Tompte

Member
Dec 22, 2017
214
152
Ah, a fellow man of culture: not knowing which day is indeed part of the fun. For game purposes I'd probably reduce the number of safe days, but that's all. Hmm... was going to say more, but in short I pretty much agree with your whole post.
Of course. I like this solution because you can tweak the numbers to whatever you want. My numbers are loosely based on real life data and I want pregnancies to be relatively rare occurrences in my game. Also, my days are quite short in real time.
 
Last edited:

HB38

Member
Oct 3, 2017
182
250
So your project sounds very similar to the one I'm currently working on - I have 1 week time increments as well. I "dumbed" down the window to a rolling 4 week window of fertility (Menstruation, Normal, Ovulation, Normal). Menstruation is 0% pregnancy chance, Normal is regular and Ovulation is double. Characters start on a random cycle. There is a bonus fertility (and negative sterility) trait that affects the final calculation that I use for confirming pregnancy that some characters have. The player can force/induce ovulation as a late stage power up, has a way to find out the woman's fertility window, etc. I also scale the impregnation chance with the woman's age.

Ultimately, what's the point? The only thing I see coming from a fert cycle is player frustration, and I can provide first hand knowledge of such if you want. As Anne pointed out, unless you're also going to make the most in depth pregnancy mechanic ever seen, why the realism when your preg seekers are going to all knock a girl up asap and your non-preg fans are gonna have to skip that girls content for the week.
The entire point of my game is impregnating women, so making it a simple "you sex them you win" would have basically meant there is no game there.
 
Last edited:
  • Like
  • Wow
Reactions: DuniX and Saki_Sliz

DuniX

Well-Known Member
Dec 20, 2016
1,086
727
Depends on if pregnancy is a game mechanic, if it's part of unlocking the sexual content, or if it's a simulation.

If it's a game mechanic like for example breeding then even things like Ovum HP and accelerated pregnancy or even instant pregnancy can make sense.

If it's part of the progression of sexual content then usually there is time skip for the pregnant scences.

If it's for pure simulation they tend to be too slow so it's more useful for games where you have tens to hundreds of girls that you manage and that you can parallelize your actions with.
 
Dec 20, 2022
57
66
Haramase has a fertility system, so that's something to look at. The way that game benefits from it is it encourages the player to switch between different partners with some regularity, as every partner has different points at which they are fertile. There are also various ways to influence chance of pregnancy and how many times you can make attempts.
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
995
Of course. I like this solution because you can tweak the numbers to whatever you want. My numbers are loosely based on real life data and I want pregnancies to be relatively rare occurrences in my game. Also, my days are quite short in real time.
I like playing with math, but hindsight being 20/20, a LUT like what you made seem's much more simpler, modifiable, adjustable, faster, etc.