[Solved!] Of Renpy and Python math... looking for a function.

OhWee

Forum Fanatic
Modder
Game Developer
Jun 17, 2017
5,881
29,890
So, it's been years since I last took an advanced math course, and of course I've forgotten most of the stuff involved there. When your math is primarily focused on just day to day checkbook balancing, things like sine curves just aren't important to you. To your computer programs, maybe...

Anyways, I'm trying to find a math function that will generate some diminishing returns r.e. stuff being added to a stat. I considered square root, but it is a bit too aggressive in what it does (i.e. 1 - 1, 100 - 10 , 1000 - @ 31 (int)). There's probably a way to do a say '1.5, i.e. 3/4 of square root, but doing the math in my head, that still feels a little too aggressive at the larger values for what I want.

I'm wondering about which other Python functions are out there that might be closer to what I need. Say as an example a 50%-60% gradual falloff at around 100 as a baseline, with gradual but continuing 'diminishing returns' past that.. I'm open to suggestions from the 'math inclined'.
 

rayssa0037

New Member
Apr 8, 2017
4
3
Well continuous discounting in finance is done as follows: 100*exp(-0.4*t) will make the series 100, 67, 45, 30, 20, ... for t=0,1,2,3,4,...
But is is probably better is you write a series of numbers you ultimately want to get. I can make you a formula that relates to that series.
 

GoodRecursion

New Member
Mar 12, 2018
4
2
plotly might be a little overkill for this.

This function should give you what you want.
Code:
def nth_diminishing_term(n):
    """ nth term of a diminishing series """
    return 1 / float(n) * sum(map(lambda i: 1/float(1.016**(i - 1)), range(1, n + 1)))


print(nth_diminishing_term(1))
# 1.0
print(nth_diminishing_term(50))
# 0.6957270984137657
print(nth_diminishing_term(100))
# 0.5051616671274516
print(nth_diminishing_term(500))
# 0.12695461109067097

print(sum([nth_diminishing_term(x) for x in range(1,101)]))
# 71.3259132676562
The equation came from wikipedia. Play around with 1.016 to adjust the nth yield. I found this value with trial and error (and a helper function to test).
 
Last edited:
  • Like
Reactions: OhWee

OhWee

Forum Fanatic
Modder
Game Developer
Jun 17, 2017
5,881
29,890
Thanks guys!

I came up with another solution. Essentially, applying a percentage reduction each turn (say 5-15%), before daily bonuses are applied. This way, the loss is tiny at low levels, but at higher levels the loss becomes more significant, offsetting gains more and more as the total increases, and eventually reaching a point where an 'equilibrium' happens, i.e. the points lost equal the 'per turn' increases.

There was an old board/miniatures game that did this with propulsion. At some point, the engine thrust would 'max out' using this concept.

Thanks for y'alls suggestions!
;)
 

xht_002

Member
Sep 25, 2018
342
353
plotly might be a little overkill for this.

This function should give you what you want.
Code:
def nth_diminishing_term(n):
    """ nth term of a diminishing series """
    return 1 / float(n) * sum(map(lambda i: 1/float(1.016**(i - 1)), range(1, n + 1)))


print(nth_diminishing_term(1))
# 1.0
print(nth_diminishing_term(50))
# 0.6957270984137657
print(nth_diminishing_term(100))
# 0.5051616671274516
print(nth_diminishing_term(500))
# 0.12695461109067097

print(sum([nth_diminishing_term(x) for x in range(1,101)]))
# 71.3259132676562
The equation came from wikipedia. Play around with 1.016 to adjust the nth yield. I found this value with trial and error (and a helper function to test).
create your own fully usable functions, and you end up with your own game engine SDK to use as the basis for all future games,

1 function to be called each time it is needed is better then an extra 1000 lines of code
 
Last edited: