Add dynamic statement

(cherry picked from commit e98680d123)
This commit is contained in:
Gouvernathor 2024-03-27 00:26:23 +01:00 committed by LoafyLemon
parent b489b59545
commit 630ede144c

View File

@ -22,6 +22,38 @@ python early hide:
**{k:getattr(cls, k) for k in __register_params.intersection(vars(cls))})
return cls
@register_decorator
class dynamic:
block = "possible"
@staticmethod
def parse(l):
rv = {}
def parse_simple(ll):
target = ll.require(ll.name, "variable name")
if target in rv:
ll.error(f"Variable {target} already set in the same dynamic block")
ll.require("=", "equals sign")
expression = ll.simple_expression()
ll.expect_eol()
rv[target] = expression
if l.match(":"):
l.expect_block("dynamic block")
l.expect_eol()
ll = l.subblock_lexer()
while ll.advance():
parse_simple(ll)
else:
parse_simple(l)
return rv
def execute(self):
evaled = {n: eval(e) for n, e in self.items()}
renpy.dynamic(**evaled)
python early:
def parse_chibi(l):
who = l.simple_expression()
@ -96,11 +128,11 @@ python early:
return {"blocks": blocks}
def next_random(p):
blocks = [(block, weight, condition) for block, weight, condition in p["blocks"] if eval(condition)]
total_weight = sum(weight for _, weight, _ in blocks)
blocks = [(block, weight) for block, weight, condition in p["blocks"] if eval(condition)]
total_weight = sum(weight for _, weight in blocks)
n = renpy.random.random() * total_weight
for block, weight, _ in blocks:
for block, weight in blocks:
if n <= weight:
break
else: