Add the ConstantRandom utility class

This commit is contained in:
Asriel Senna 2024-01-07 13:49:58 +01:00
parent d119e71116
commit 5980329125

View File

@ -226,3 +226,33 @@ init python early:
Returns the value returned by the screen, if any. Returns the value returned by the screen, if any.
""" """
return renpy.call_in_new_context("call_screen", sc_name, *args, **kwargs) return renpy.call_in_new_context("call_screen", sc_name, *args, **kwargs)
class ConstantRandom(random.Random):
"""
The state of the object never changes when generative methods are called,
meaning that successive calls of the same method with the same arguments
produce the same results.
Populated in the block below.
"""
def seed(self, a=None, *args, **kwargs):
if a is None:
a = renpy.random.random()
super().seed(a, *args, **kwargs)
self.state = self.getstate()
python early hide:
service_randomobj = random.Random()
def random_wrap(func_name):
def wrapped(self, *args, **kwargs):
service_randomobj.setstate(self.getstate())
rv = getattr(service_randomobj, func_name)(*args, **kwargs)
return rv
return wrapped
for fn in random.__all__:
if fn in ("Random", "SystemRandom", "getstate", "setstate", "seed"):
continue
setattr(ConstantRandom, fn, random_wrap(fn))