2022-05-17 21:05:27 +00:00
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
default event_callbacks = []
|
|
|
|
|
2023-06-24 22:27:13 +00:00
|
|
|
default _events_completed_any = None
|
|
|
|
default _events_completed_all = None
|
|
|
|
default _events_filtered_completed_all = None
|
|
|
|
default _events_filtered_completed_any = None
|
|
|
|
default _event_completed = None
|
|
|
|
default _event_completed_failed = None
|
2023-06-26 00:44:25 +00:00
|
|
|
default _event_queue = None
|
|
|
|
default _event = None
|
2023-06-24 22:27:13 +00:00
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
init -1 python:
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
class EventQueue(object):
|
2023-06-26 00:44:25 +00:00
|
|
|
ignore_globals = ("mainloop",)
|
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
def __init__(self, id=None):
|
|
|
|
self.id = id
|
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
self.queue = []
|
|
|
|
self.freeze = False
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if config.developer:
|
|
|
|
rollback = f"{stdcol.UNDERLINE}(Rollback){stdcol.END} " if renpy.in_rollback() else ""
|
|
|
|
print(f"{rollback}Creating EventQueue object '{stdcol.BLUE}{id}{stdcol.END}' ...")
|
2023-06-24 22:27:13 +00:00
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
def freeze(self):
|
|
|
|
self.freeze = True
|
|
|
|
|
|
|
|
def unfreeze(self):
|
|
|
|
self.freeze = False
|
|
|
|
|
|
|
|
def delay(self, n):
|
|
|
|
for i in self.queue:
|
|
|
|
i.wait += n
|
|
|
|
|
|
|
|
def tick(self):
|
|
|
|
"""Causes time to pass."""
|
|
|
|
if self.freeze:
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in self.queue:
|
|
|
|
i.wait -= 1
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def list_filtered(self, func=lambda ev: (ev.wait <= 0) and not ev.disabled and ev.requirements_met()):
|
|
|
|
# queue = [ev for ev in self.queue if (ev.wait <= 0) and not ev.disabled and ev.requirements_met() ]
|
|
|
|
# queue.sort(key=lambda x: x.priority)
|
|
|
|
|
|
|
|
filtered = sorted(filter(func, self.queue), key=lambda x: x.priority)
|
|
|
|
|
|
|
|
return filtered
|
2022-05-17 21:05:27 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def initialize_globals(self):
|
|
|
|
global _event_queue, _events_completed_all, _events_completed_any, _events_filtered_completed_all, _events_filtered_completed_any
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
_event_queue = self
|
2023-06-24 22:27:13 +00:00
|
|
|
|
|
|
|
queue = self.queue
|
|
|
|
|
|
|
|
_events_completed_all = all(ev.completed for ev in queue)
|
|
|
|
_events_completed_any = any(ev.completed for ev in queue)
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
queue = self.list_filtered()
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-24 22:27:13 +00:00
|
|
|
_events_filtered_completed_all = all(ev.completed for ev in queue)
|
|
|
|
_events_filtered_completed_any = any(ev.completed for ev in queue)
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def next(self):
|
|
|
|
queue = self.list_filtered()
|
|
|
|
repeatable = []
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
for ev in queue:
|
2023-06-26 00:44:25 +00:00
|
|
|
if ev.completed:
|
|
|
|
if ev.repeat:
|
|
|
|
repeatable.append(ev)
|
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
continue
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
return ev
|
2022-05-17 21:05:27 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if repeatable:
|
|
|
|
return renpy.random.choice(repeatable)
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def start(self):
|
|
|
|
if not self.id in self.ignore_globals:
|
|
|
|
self.initialize_globals()
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
ev = self.next()
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if ev:
|
|
|
|
return ev.start(self)
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def _debug(self, sort=lambda x: x.priority):
|
2023-06-23 18:07:37 +00:00
|
|
|
queue = self.queue
|
2023-06-26 00:44:25 +00:00
|
|
|
queue.sort(key=sort)
|
2023-06-23 18:07:37 +00:00
|
|
|
|
|
|
|
for ev in queue:
|
|
|
|
s = f"ID:{ev.id} QUE:{ev.queued} STA:{ev.started} COM:{ev.completed} COMF:{ev.completed_failed} DIS:{ev.disabled}"
|
|
|
|
|
|
|
|
print(s)
|
|
|
|
|
|
|
|
def _debug_menu(self):
|
|
|
|
queue = self.queue
|
|
|
|
queue.sort(key=lambda x: x.priority)
|
|
|
|
|
|
|
|
menu = []
|
|
|
|
|
|
|
|
for ev in queue:
|
|
|
|
req = f"{{color=#00ff00}}{ev.req}{{/color}}" if ev.requirements_met() else f"{{color=#ff0000}}{ev.req}{{/color}}"
|
|
|
|
|
|
|
|
s = f"{{size=12}}id:{ev.id} label:{ev.label}{{/size}}{{size=8}}\npriority:{ev.priority} Daytime:{ev.daytime} Req:{req} AutoENQ:{ev.autoenqueue} AutoDEQ:{ev.autodequeue}{{/size}}"
|
|
|
|
l = ev.label
|
|
|
|
|
|
|
|
menu.append( (s, l) )
|
|
|
|
|
|
|
|
renpy.display_menu(menu)
|
|
|
|
|
2022-05-17 21:05:27 +00:00
|
|
|
class Event(object):
|
2023-06-23 18:07:37 +00:00
|
|
|
_queue = None
|
2023-06-27 23:54:14 +00:00
|
|
|
_parent = None
|
2022-05-17 21:05:27 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def __init__(self, id, wait=0, priority=5, daytime=None, req=None, label=None, func=None, queue="eventqueue", autoenqueue=False, autodequeue=True,
|
|
|
|
repeat=True, fail_suffixes=("_fail", "too_much", "too_much_public"), ignore_labels=[], subevents=[]):
|
2022-05-17 21:05:27 +00:00
|
|
|
self.id = id
|
|
|
|
self.wait = wait
|
|
|
|
self.priority = priority
|
|
|
|
self.daytime = daytime
|
|
|
|
self.req = req
|
|
|
|
self.label = label
|
|
|
|
self.func = func
|
2023-06-23 18:07:37 +00:00
|
|
|
self.queue = queue
|
|
|
|
self.autoenqueue = autoenqueue
|
|
|
|
self.autodequeue = autodequeue
|
|
|
|
self.repeat = repeat
|
|
|
|
self.fail_suffixes = tuple(fail_suffixes)
|
|
|
|
self.ignore_labels = ignore_labels
|
2023-06-26 00:44:25 +00:00
|
|
|
self.subevents = subevents
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-27 23:54:14 +00:00
|
|
|
for ev in subevents:
|
|
|
|
getattr(store, ev)._parent = self
|
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
self.queued = False
|
|
|
|
self.started = False
|
|
|
|
self.completed = False
|
|
|
|
self.completed_failed = False
|
|
|
|
self.disabled = False
|
2022-05-17 21:05:27 +00:00
|
|
|
|
|
|
|
if not renpy.has_label(self.label):
|
|
|
|
raise Exception("Supplied label does not exist.")
|
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
if autoenqueue:
|
|
|
|
self.enqueue()
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if config.developer:
|
|
|
|
def __setattr__(self, attr, value):
|
|
|
|
if hasattr(self, attr) and getattr(self, attr) != value:
|
|
|
|
id = getattr(self, "id")
|
|
|
|
rollback = f"{stdcol.UNDERLINE}(Rollback){stdcol.END} " if renpy.in_rollback() else ""
|
|
|
|
caller_id = renpy.get_filename_line()
|
|
|
|
|
|
|
|
if not "/00start.rpy" in caller_id[0]:
|
|
|
|
# Ignore init
|
2023-06-27 18:02:35 +00:00
|
|
|
print(f"{rollback}Setting '{stdcol.GREEN}{id}{stdcol.END}.{stdcol.RED}{attr}{stdcol.END}={stdcol.YELLOW}{value}{stdcol.END}' caller '{stdcol.BLUE}{caller_id}{stdcol.END}'...")
|
2023-06-26 00:44:25 +00:00
|
|
|
|
|
|
|
super().__setattr__(attr, value)
|
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
def disable(self):
|
|
|
|
self.disabled = True
|
|
|
|
|
|
|
|
def enable(self):
|
|
|
|
self.disabled = False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def queue(self):
|
|
|
|
return getattr(store, self._queue).queue if self._queue else None
|
|
|
|
|
|
|
|
@queue.setter
|
|
|
|
def queue(self, name):
|
|
|
|
self._queue = name
|
|
|
|
|
|
|
|
if name is not None and not hasattr(store, name):
|
2023-06-26 00:44:25 +00:00
|
|
|
setattr(store, name, EventQueue(name))
|
|
|
|
|
|
|
|
def enqueue(self, queue=None):
|
|
|
|
if queue:
|
|
|
|
queue = getattr(store, queue).queue
|
|
|
|
else:
|
|
|
|
queue = self.queue
|
|
|
|
|
|
|
|
if not self in queue:
|
2023-06-23 18:07:37 +00:00
|
|
|
if config.developer:
|
2023-06-26 00:44:25 +00:00
|
|
|
rollback = f"{stdcol.UNDERLINE}(Rollback){stdcol.END} " if renpy.in_rollback() else ""
|
|
|
|
name = getattr(store, self._queue).id
|
|
|
|
caller_id = renpy.get_filename_line()
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if not "/00start.rpy" in caller_id[0]:
|
|
|
|
print(f"{rollback}Enqueueing '{stdcol.GREEN}{self.id}{stdcol.END}' into '{stdcol.BLUE}{name}{stdcol.END}' ...")
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
queue.append(self)
|
2022-05-17 21:05:27 +00:00
|
|
|
self.queued = True
|
|
|
|
|
|
|
|
def requirements_met(self):
|
|
|
|
if self.req:
|
|
|
|
return eval(self.req)
|
|
|
|
return True
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def start(self, _caller=None):
|
|
|
|
global _event, _event_completed, _event_completed_failed
|
|
|
|
|
|
|
|
if config.developer:
|
|
|
|
rollback = f"{stdcol.UNDERLINE}(Rollback){stdcol.END} " if renpy.in_rollback() else ""
|
|
|
|
caller_id = _caller.id if _caller else renpy.get_filename_line()
|
|
|
|
print(f"{rollback}Starting '{stdcol.GREEN}{self.id}{stdcol.END}' caller '{stdcol.BLUE}{caller_id}{stdcol.END}' ... ")
|
2023-06-23 18:07:37 +00:00
|
|
|
|
|
|
|
self.started = True
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
_event = self
|
2023-06-24 22:27:13 +00:00
|
|
|
_event_completed = self.completed
|
|
|
|
_event_completed_failed = self.completed_failed
|
2023-06-23 18:07:37 +00:00
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if self.autodequeue:
|
|
|
|
queue = _caller.queue or self.queue
|
|
|
|
queue.remove(self)
|
2023-06-23 18:07:37 +00:00
|
|
|
self.queued = False
|
|
|
|
|
|
|
|
## additional requirements check
|
|
|
|
|
|
|
|
if not self._track_completion in event_callbacks:
|
|
|
|
event_callbacks.append(self._track_completion)
|
2022-05-17 21:05:27 +00:00
|
|
|
|
|
|
|
if self.func:
|
|
|
|
self.func()
|
|
|
|
|
|
|
|
if self.label:
|
2023-06-23 18:07:37 +00:00
|
|
|
return renpy.jump(self.label)
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def cancel(self, _caller=None):
|
|
|
|
if config.developer:
|
|
|
|
rollback = f"{stdcol.UNDERLINE}(Rollback){stdcol.END} " if renpy.in_rollback() else ""
|
|
|
|
caller_id = _caller.id if _caller else renpy.get_filename_line()
|
|
|
|
print(f"{rollback}Cancelling '{stdcol.GREEN}{self.id}{stdcol.END}' caller '{stdcol.BLUE}{caller_id}{stdcol.END}'... ")
|
|
|
|
self.started = False
|
|
|
|
|
|
|
|
if self._track_completion in event_callbacks:
|
|
|
|
event_callbacks.remove(self._track_completion)
|
|
|
|
|
2023-06-27 23:54:14 +00:00
|
|
|
if self._parent:
|
|
|
|
self._parent.cancel()
|
|
|
|
|
|
|
|
if renpy.get_return_stack():
|
|
|
|
renpy.pop_call()
|
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
def _track_completion(self, label, abnormal):
|
|
|
|
if renpy.is_init_phase():
|
|
|
|
return
|
|
|
|
|
|
|
|
if label == self.label:
|
|
|
|
# Ignore jumps to self.
|
|
|
|
return
|
|
|
|
|
|
|
|
if label in self.ignore_labels:
|
2023-06-27 18:02:35 +00:00
|
|
|
# Postpone completing the event until end label returns.
|
|
|
|
return
|
|
|
|
|
|
|
|
if "." in label:
|
|
|
|
# Ignore local labels
|
2023-06-23 18:07:37 +00:00
|
|
|
return
|
|
|
|
|
2023-06-27 23:54:14 +00:00
|
|
|
# if abnormal: # Irrelevant
|
2023-06-23 18:07:37 +00:00
|
|
|
# return
|
|
|
|
|
|
|
|
if renpy.game.context().return_stack:
|
|
|
|
# If return stack exists, ignore, because we're probably in a call label.
|
2023-06-27 23:54:14 +00:00
|
|
|
# We only allow room_menu, to pass.
|
2023-06-23 18:07:37 +00:00
|
|
|
return
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
if self.started: # Event cancelled abnormally?
|
|
|
|
|
|
|
|
if label.endswith(self.fail_suffixes):
|
|
|
|
self.completed_failed = True
|
|
|
|
else:
|
|
|
|
self.completed = True
|
|
|
|
|
|
|
|
self.started = False
|
|
|
|
|
|
|
|
for ev in self.subevents:
|
|
|
|
if isinstance(ev, str):
|
|
|
|
ev = getattr(store, ev)
|
|
|
|
ev.enqueue()
|
2023-06-23 18:07:37 +00:00
|
|
|
|
|
|
|
if self._track_completion in event_callbacks:
|
|
|
|
event_callbacks.remove(self._track_completion)
|
|
|
|
|
|
|
|
def execute_event_callbacks(label, abnormal):
|
|
|
|
if renpy.is_init_phase() or not hasattr(store, "event_callbacks"):
|
|
|
|
return
|
|
|
|
|
|
|
|
for callback in event_callbacks: callback(label, abnormal)
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def initialize_event_callbacks():
|
2023-06-23 18:07:37 +00:00
|
|
|
if renpy.in_rollback():
|
|
|
|
return
|
|
|
|
|
|
|
|
# We need to add these after defaults are finished.
|
|
|
|
renpy.config.label_callbacks.append(execute_event_callbacks)
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
def show_events_menu(queues, filter=False, **kwargs):
|
|
|
|
def menu_hints(queue, filter):
|
|
|
|
filtered_queue = queue.list_filtered() if filter is False else queue.list_filtered(filter)
|
|
|
|
total_applicable = len(filtered_queue)
|
|
|
|
total_events = len(queue.queue)
|
|
|
|
completed = 0
|
|
|
|
hints = []
|
|
|
|
|
|
|
|
for ev in filtered_queue:
|
|
|
|
if ev.completed:
|
|
|
|
hints.append("v")
|
|
|
|
completed += 1
|
|
|
|
elif ev.completed_failed:
|
|
|
|
hints.append("x")
|
|
|
|
completed += 1
|
|
|
|
else:
|
|
|
|
hints.append("o")
|
|
|
|
|
|
|
|
s = "(" + "".join(hints) + f") {completed}/{total_applicable}"
|
|
|
|
|
|
|
|
if config.developer:
|
|
|
|
s += f"/{total_events}"
|
|
|
|
|
|
|
|
return s
|
|
|
|
|
2023-06-23 18:07:37 +00:00
|
|
|
l = []
|
|
|
|
|
|
|
|
for queue in queues:
|
|
|
|
if isinstance(queue, str):
|
|
|
|
queue = getattr(store, queue)
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
title = f"-{queue.id}- {menu_hints(queue, filter)}"
|
2023-06-23 18:07:37 +00:00
|
|
|
|
|
|
|
l.append( (title, queue) )
|
|
|
|
|
|
|
|
return renpy.display_menu(l, **kwargs)
|
|
|
|
|
2023-06-26 00:44:25 +00:00
|
|
|
config.after_default_callbacks.append(initialize_event_callbacks)
|
2022-05-17 21:05:27 +00:00
|
|
|
|
2022-05-19 15:44:36 +00:00
|
|
|
init offset = -5
|
|
|
|
|
2023-06-27 23:54:14 +00:00
|
|
|
default eventqueue = EventQueue("mainloop")
|