From 4770be5f1b3571896020aea5de84bba208cc749c Mon Sep 17 00:00:00 2001 From: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com> Date: Thu, 28 Mar 2024 22:28:53 +0100 Subject: [PATCH] Convert existing cds using the new system --- game/scripts/cds.rpy | 140 ++++++++++++++++++++----------------------- 1 file changed, 66 insertions(+), 74 deletions(-) diff --git a/game/scripts/cds.rpy b/game/scripts/cds.rpy index 49a5835b..74e84582 100644 --- a/game/scripts/cds.rpy +++ b/game/scripts/cds.rpy @@ -54,96 +54,88 @@ python early hide: evaled = {n: eval(e) for n, e in self.items()} renpy.dynamic(**evaled) -python early: - def parse_chibi(l): - who = l.simple_expression() - action = l.simple_expression() + @register_decorator + class chibi: + @staticmethod + def parse(l): + who = l.simple_expression() + action = l.simple_expression() - return (who, action) + return (who, action) - def execute_chibi(p): - print(f"{p}") - who, action = p - func = eval(f"{who}_chibi.{action}") + def execute(self): + print(f"{self}") + who, action = self + func = eval(f"{who}_chibi.{action}") - # print(f"Execution: {who} {action}") + # print(f"Execution: {who} {action}") + + def lint(self): + who, action = self + try: + chibi = eval(f"{who}_chibi") + except Exception: + renpy.error(f"Character chibi not defined: {who}") + + def predict(self): + who, action = self - def lint_chibi(p): - who, action = p - try: chibi = eval(f"{who}_chibi") - except Exception: - renpy.error(f"Character chibi not defined: {who}") + doll = eval(f"{who}") - def predict_chibi(p): - who, action = p + layers = ( + l[0] for pose in chibi.poses.keys() + for k in doll.states.values() if k[0] and k[2] + for l in k[0].get_layers(k[0]._hash, subpath=posixpath.join("chibi", pose)).values() + ) - chibi = eval(f"{who}_chibi") - doll = eval(f"{who}") + return layers - layers = ( - l[0] for pose in chibi.poses.keys() - for k in doll.states.values() if k[0] and k[2] - for l in k[0].get_layers(k[0]._hash, subpath=posixpath.join("chibi", pose)).values() - ) + @register_decorator + class random: + block = True + predict_all = True - return layers + @staticmethod + def parse(l): + l.require(":") + l.expect_eol() - renpy.register_statement( - name="chibi", - parse=parse_chibi, - execute=execute_chibi, - lint=lint_chibi, - predict=predict_chibi, - ) + ll = l.subblock_lexer() + blocks = [] - def parse_random(l): - l.require(":") - l.expect_eol() + while ll.advance(): + with ll.catch_error(): + weight = 1.0 + condition = "True" - ll = l.subblock_lexer() - blocks = [] + if ll.keyword("block"): + ll.expect_block("block") - while ll.advance(): - with ll.catch_error(): - weight = 1.0 - condition = "True" + block = ll.subblock_lexer().renpy_block() - if ll.keyword("block"): - ll.expect_block("block") + if ll.keyword("weight"): + weight = float(ll.require(ll.float)) - block = ll.subblock_lexer().renpy_block() + if ll.keyword("if"): + ll.expect_block("if block") + condition = ll.require(ll.python_expression) + else: + block = ll.renpy_statement() - if ll.keyword("weight"): - weight = float(ll.require(ll.float)) + blocks.append((block, weight, condition)) - if ll.keyword("if"): - ll.expect_block("if block") - condition = ll.require(ll.python_expression) + return {"blocks": blocks} + + def next(self): + blocks = [(block, weight) for block, weight, condition in self["blocks"] if eval(condition)] + total_weight = sum(weight for _, weight in blocks) + n = renpy.random.random() * total_weight + + for block, weight in blocks: + if n <= weight: + break else: - block = ll.renpy_statement() + n -= weight - blocks.append((block, weight, condition)) - - return {"blocks": blocks} - - def next_random(p): - 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: - if n <= weight: - break - else: - n -= weight - - return block - - renpy.register_statement( - name="random", - block=True, - predict_all=True, - parse=parse_random, - next=next_random, - ) + return block