2024-03-26 23:26:14 +00:00
|
|
|
python early hide:
|
|
|
|
import inspect
|
|
|
|
__register_params = frozenset(inspect.signature(renpy.register_statement).parameters).difference({"name", "parse"})
|
|
|
|
def register_decorator(cls):
|
|
|
|
"""
|
|
|
|
A class decorator which registers a new statement.
|
|
|
|
|
|
|
|
The name of the statement will be the class name unless a `name` class attribute is present, which should be a string.
|
|
|
|
|
|
|
|
The `parse` method should be a static method that returns an object which will be passed to the other methods as `self`.
|
|
|
|
Returning an instance of the class is disabled for now.
|
|
|
|
"""
|
|
|
|
# security
|
|
|
|
def raiser(*args, **kwargs):
|
|
|
|
raise NotImplementedError("Returning an instance of the class is disabled")
|
|
|
|
cls.__init__ = raiser
|
|
|
|
|
|
|
|
name = getattr(cls, "name", cls.__name__)
|
|
|
|
parse = getattr(cls, "parse", cls) # left in
|
|
|
|
renpy.register_statement(name,
|
|
|
|
parse=parse,
|
|
|
|
**{k:getattr(cls, k) for k in __register_params.intersection(vars(cls))})
|
|
|
|
return cls
|
|
|
|
|
2023-02-21 02:53:16 +00:00
|
|
|
python early:
|
2023-03-13 20:29:05 +00:00
|
|
|
def parse_chibi(l):
|
|
|
|
who = l.simple_expression()
|
|
|
|
action = l.simple_expression()
|
2023-02-21 02:53:16 +00:00
|
|
|
|
|
|
|
return (who, action)
|
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
def execute_chibi(p):
|
|
|
|
print(f"{p}")
|
|
|
|
who, action = p
|
2023-03-20 21:44:55 +00:00
|
|
|
func = eval(f"{who}_chibi.{action}")
|
2023-02-21 02:53:16 +00:00
|
|
|
|
2023-06-18 22:18:50 +00:00
|
|
|
# print(f"Execution: {who} {action}")
|
2023-02-21 02:53:16 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
def lint_chibi(p):
|
|
|
|
who, action = p
|
2023-02-21 02:53:16 +00:00
|
|
|
try:
|
2023-03-20 21:44:55 +00:00
|
|
|
chibi = eval(f"{who}_chibi")
|
2023-02-21 02:53:16 +00:00
|
|
|
except Exception:
|
|
|
|
renpy.error(f"Character chibi not defined: {who}")
|
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
def predict_chibi(p):
|
|
|
|
who, action = p
|
2023-02-21 02:53:16 +00:00
|
|
|
|
2023-03-20 21:44:55 +00:00
|
|
|
chibi = eval(f"{who}_chibi")
|
2023-02-21 02:53:16 +00:00
|
|
|
doll = eval(f"{who}")
|
|
|
|
|
|
|
|
layers = (
|
2023-02-21 20:33:04 +00:00
|
|
|
l[0] for pose in chibi.poses.keys()
|
2023-02-21 02:53:16 +00:00
|
|
|
for k in doll.states.values() if k[0] and k[2]
|
2023-02-21 20:33:04 +00:00
|
|
|
for l in k[0].get_layers(k[0]._hash, subpath=posixpath.join("chibi", pose)).values()
|
2023-02-21 02:53:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return layers
|
|
|
|
|
|
|
|
renpy.register_statement(
|
|
|
|
name="chibi",
|
|
|
|
parse=parse_chibi,
|
|
|
|
execute=execute_chibi,
|
|
|
|
lint=lint_chibi,
|
|
|
|
predict=predict_chibi,
|
|
|
|
)
|
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
def parse_random(l):
|
|
|
|
l.require(":")
|
|
|
|
l.expect_eol()
|
2023-03-10 23:09:32 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
ll = l.subblock_lexer()
|
|
|
|
blocks = []
|
2023-03-10 23:09:32 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
while ll.advance():
|
|
|
|
with ll.catch_error():
|
|
|
|
weight = 1.0
|
|
|
|
condition = "True"
|
2023-03-10 23:09:32 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
if ll.keyword("block"):
|
|
|
|
ll.expect_block("block")
|
2023-03-10 23:09:32 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
block = ll.subblock_lexer().renpy_block()
|
|
|
|
|
|
|
|
if ll.keyword("weight"):
|
|
|
|
weight = float(ll.require(ll.float))
|
|
|
|
|
|
|
|
if ll.keyword("if"):
|
|
|
|
ll.expect_block("if block")
|
|
|
|
condition = ll.require(ll.python_expression)
|
|
|
|
else:
|
|
|
|
block = ll.renpy_statement()
|
|
|
|
|
|
|
|
blocks.append((block, weight, condition))
|
2023-03-10 23:09:32 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
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)
|
|
|
|
n = renpy.random.random() * total_weight
|
|
|
|
|
|
|
|
for block, weight, _ in blocks:
|
|
|
|
if n <= weight:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
n -= weight
|
2023-03-10 23:09:32 +00:00
|
|
|
|
2023-03-13 20:29:05 +00:00
|
|
|
return block
|
2023-03-10 23:09:32 +00:00
|
|
|
|
|
|
|
renpy.register_statement(
|
|
|
|
name="random",
|
|
|
|
block=True,
|
2023-03-13 20:29:05 +00:00
|
|
|
predict_all=True,
|
2023-03-10 23:09:32 +00:00
|
|
|
parse=parse_random,
|
|
|
|
next=next_random,
|
|
|
|
)
|