File size: 5,507 Bytes
accac24 e8598c4 accac24 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | """
Save reference JPGs of the game to `reference_images/`, useful for documentation, the
methods section, and checking the stimuli at a glance. No browser needed.
uv run python render_slides.py
Produces:
reference_images/screens/ welcome, overworld, choice, outcome, rating, practice, end
reference_images/stimuli/ a sheet of all chests + one file per chest
reference_images/characters/ the 3 explorer avatars, the partner, and a sheet
"""
import os
import numpy as np
import jax
import jax.tree_util as jtu
from PIL import Image, ImageDraw
import sprites
import experiment_structure as E
OUT = "reference_images"
A_UP, A_DOWN, A_LEFT, A_RIGHT, A_CONT = 0, 1, 2, 3, 4
def _save(arr, path):
os.makedirs(os.path.dirname(path), exist_ok=True)
Image.fromarray(np.asarray(arr, dtype=np.uint8)).convert("RGB").save(path, quality=92)
print(" ", path)
def _nearest(s):
ar, ac = int(s.agent_pos[0]), int(s.agent_pos[1])
hp, act, co = np.asarray(s.house_pos), np.asarray(s.house_active), np.asarray(s.consumed)
best, bd = None, 1e9
for i in range(E.N_PLACES):
if act[i] and not co[i]:
dd = abs(hp[i][0] - ar) + abs(hp[i][1] - ac)
if dd < bd:
best, bd = (int(hp[i][0]), int(hp[i][1])), dd
return best
def capture_screens(params, names, prefix=""):
"""Walk a session and grab render_state the first time each requested mode appears.
names maps mode -> filename (modes: 'overworld','choice','outcome','rating')."""
ts = E.jax_web_env.reset(jax.random.PRNGKey(0), params)
def step(ts, a, sd):
nxt = E.jax_web_env.next_steps(jax.random.PRNGKey(sd), ts, params)
return jtu.tree_map(lambda x: x[a], nxt)
got = {}
cur = ts
for stp in range(4000):
s = cur.state
mode = int(s.mode)
key = {0: "overworld", 1: "choice", 2: "outcome", 3: "rating"}[mode]
if key in names and key not in got:
got[key] = E.render_state(cur)
_save(got[key], f"{OUT}/screens/{prefix}{names[key]}.jpg")
if len(got) == len(names):
break
# drive forward
if mode == 0:
tgt = _nearest(s)
if tgt is None:
break
ar, ac = int(s.agent_pos[0]), int(s.agent_pos[1])
tr, tc = tgt
a = (A_UP if tr < ar else A_DOWN) if ar != tr else (A_LEFT if tc < ac else A_RIGHT)
elif mode == 1:
a = A_LEFT # pick a chest
elif mode == 2:
a = A_CONT # continue past the outcome
else: # rating: confirm to move on
a = A_CONT
cur = step(cur, a, stp + 1)
return got
def chest_sheet():
n = sprites.N_STIM
cols, cw, ch, pad = 6, 150, 120, 14
rows = (n + cols - 1) // cols
W = cols * (cw + pad) + pad
Hh = rows * (ch + pad + 18) + pad
img = Image.new("RGB", (W, Hh), (235, 232, 226))
d = ImageDraw.Draw(img)
for sid in range(n):
r, c = divmod(sid, cols)
x = pad + c * (cw + pad)
y = pad + r * (ch + pad + 18)
sprites.draw_stim_chest(d, x, y, cw, ch, sid, is_open=False)
label = f"{sid}: {sprites.stim_describe(sid)}"
sprites.text(d, x + cw // 2, y + ch + 4, label, size=12, fill=(40, 40, 48), center=True)
# also save the chest on its own
tile = Image.new("RGB", (cw, ch), (235, 232, 226))
sprites.draw_stim_chest(ImageDraw.Draw(tile), 8, 8, cw - 16, ch - 24, sid, is_open=False)
_save(np.asarray(tile), f"{OUT}/stimuli/chest_{sid:02d}.jpg")
_save(np.asarray(img), f"{OUT}/stimuli/all_chests.jpg")
def characters():
# the 3 explorer avatars
for i in range(len(sprites.AVATARS)):
_save(np.asarray(sprites.draw_avatar_preview(i)), f"{OUT}/characters/avatar_{i}.jpg")
# the partner, drawn standalone
part = Image.new("RGB", (150, 170), (224, 226, 232))
sprites.draw_character(ImageDraw.Draw(part), 75, 150, facing=0, **sprites.PARTNER_PAL)
_save(np.asarray(part), f"{OUT}/characters/partner.jpg")
# a combined sheet
sheet = Image.new("RGB", (150 * (len(sprites.AVATARS) + 1), 180), (224, 226, 232))
d = ImageDraw.Draw(sheet)
for i in range(len(sprites.AVATARS)):
sheet.paste(Image.fromarray(np.asarray(sprites.draw_avatar_preview(i)).astype(np.uint8)),
(150 * i + 10, 25))
sprites.text(d, 150 * i + 75, 6, f"explorer {i}", size=13, fill=(30, 30, 36), center=True)
sprites.draw_character(d, 150 * len(sprites.AVATARS) + 75, 160, facing=0, **sprites.PARTNER_PAL)
sprites.text(d, 150 * len(sprites.AVATARS) + 75, 6, "partner", size=13, fill=(30, 30, 36), center=True)
_save(np.asarray(sheet), f"{OUT}/characters/avatars_and_partner.jpg")
if __name__ == "__main__":
print("Rendering reference images ->", OUT)
# splash screens (image-based slides)
_save(np.asarray(sprites.draw_welcome_splash(720, 470)), f"{OUT}/screens/welcome.jpg")
_save(np.asarray(sprites.draw_end_splash(720, 380)), f"{OUT}/screens/end.jpg")
# in-game screens, driven through a real session
capture_screens(E.main_params_for("0003"),
{"overworld": "overworld", "choice": "choice",
"outcome": "outcome", "rating": "rating"})
capture_screens(E.practice_params("0003"),
{"choice": "choice"}, prefix="practice_")
chest_sheet()
characters()
print("Done. See", OUT)
|