126 lines
6.4 KiB
Python
126 lines
6.4 KiB
Python
"""Build the site's Urban Terror art into public/: levelshots, weapon icons and the bot portraits.
|
|
|
|
python3 scripts/make_art.py ART_DIR public
|
|
|
|
ART_DIR holds files unzipped from Urban Terror 4.3's zUrT43_*.pk3 (freeware, from urbanterror.info):
|
|
levelshots/*, icons/weapons/* and scripts/bots.txt + scripts/*.bot. For example:
|
|
|
|
for f in UrbanTerror43/q3ut4/zUrT43_0*.pk3; do
|
|
unzip -qo "$f" 'levelshots/*' 'icons/weapons/*' 'scripts/bots.txt' 'scripts/*.bot' -d ART_DIR
|
|
done
|
|
|
|
The bots have no portrait art in the game, so each gets a generated 32x32 pixel operator in the
|
|
colors of its in-game skin (swat, urban, desert, ...), the same face every time (seeded by name).
|
|
"""
|
|
import glob, json, os, random, re, sys
|
|
from PIL import Image, ImageDraw
|
|
|
|
ART, OUT = sys.argv[1], sys.argv[2]
|
|
for d in ("levelshots", "weapons", "portraits"):
|
|
os.makedirs(os.path.join(OUT, d), exist_ok=True)
|
|
|
|
# levelshots: one per map, preferring the jpg; 512x384
|
|
shots = {}
|
|
for f in sorted(glob.glob(os.path.join(ART, "levelshots", "*"))):
|
|
name, ext = os.path.splitext(os.path.basename(f))
|
|
name = name.lower()
|
|
if name not in shots or ext.lower() == ".jpg":
|
|
shots[name] = f
|
|
def letterbox(im):
|
|
"""UrT levelshots are widescreen pictures in black bars, with the map's name printed on the
|
|
bottom bar: keep the picture rows only (the longest run of rows that aren't near-black)."""
|
|
g = im.convert("L")
|
|
w, h = g.size
|
|
# judged on the left half only: the map name sits on the right of the bottom bar
|
|
lit = [sum(g.crop((0, y, w // 2, y + 1)).get_flattened_data()) / (w // 2) > 1 for y in range(h)]
|
|
best, run = (0, h), None
|
|
for y, on in enumerate(lit + [False]):
|
|
if on and run is None:
|
|
run = y
|
|
elif not on and run is not None:
|
|
if y - run > best[1] - best[0] or best == (0, h):
|
|
best = (run, y)
|
|
run = None
|
|
top, bottom = best
|
|
return im.crop((0, top, w, bottom)) if bottom - top > h * 0.25 else im
|
|
for name, f in shots.items():
|
|
im = letterbox(Image.open(f).convert("RGB"))
|
|
tw = 512
|
|
im.resize((tw, round(im.height * tw / im.width)), Image.LANCZOS).save(os.path.join(OUT, "levelshots", name + ".jpg"), quality=85)
|
|
|
|
# weapon icons: 512x256 RGBA -> 160x80, named by the kill's weapon key (UT_MOD_X -> x)
|
|
ICON = {"knife": "knife", "knife_thrown": "knife", "beretta": "beretta", "deagle": "deserteagle", "spas": "spas12",
|
|
"ump45": "ump45", "mp5k": "mp5k", "lr300": "lr", "g36": "g36", "psg1": "psg1", "hk69": "hk69", "hk69_hit": "hk69",
|
|
"hegrenade": "grenade_he", "sr8": "sr8", "ak103": "ak103", "negev": "negev", "m4": "m4", "glock": "glock",
|
|
"colt1911": "colt1911", "mac11": "mac11", "frf1": "frf1", "benelli": "benellim9", "p90": "p90",
|
|
"magnum": "44magnum", "tod50": "tod50"}
|
|
src = {os.path.splitext(os.path.basename(f))[0].lower(): f for f in glob.glob(os.path.join(ART, "icons", "weapons", "*"))}
|
|
for key, icon in ICON.items():
|
|
im = Image.open(src[icon]).convert("RGBA")
|
|
im = im.crop(im.getbbox() or (0, 0) + im.size)
|
|
im.thumbnail((160, 80), Image.LANCZOS)
|
|
canvas = Image.new("RGBA", (160, 80))
|
|
canvas.paste(im, ((160 - im.width) // 2, (80 - im.height) // 2), im)
|
|
canvas.save(os.path.join(OUT, "weapons", key + ".png"))
|
|
|
|
# bot portraits: a 32x32 pixel operator in the bot's skin colors, scaled 4x nearest-neighbour
|
|
SKINS = { # background, uniform, accent
|
|
"swat": ((24, 32, 52), (44, 52, 70), (120, 140, 170)), "urban": ((48, 50, 54), (98, 100, 104), (170, 172, 176)),
|
|
"desert": ((92, 72, 44), (176, 146, 98), (226, 200, 150)), "cobra": ((24, 46, 26), (62, 92, 48), (150, 180, 90)),
|
|
"tag": ((70, 20, 60), (200, 70, 160), (255, 170, 230)), "terror": ((18, 14, 14), (40, 34, 32), (150, 30, 30)),
|
|
}
|
|
SKINTONES = [(241, 194, 150), (224, 172, 125), (198, 134, 88), (141, 85, 54), (92, 58, 38)]
|
|
|
|
|
|
def portrait(name, skin, team_tint=None):
|
|
rnd = random.Random(name)
|
|
bg, uni, acc = SKINS[skin]
|
|
face = rnd.choice(SKINTONES)
|
|
im = Image.new("RGB", (32, 32), bg)
|
|
d = ImageDraw.Draw(im)
|
|
for _ in range(14): # camo blotches
|
|
x, y, r = rnd.randrange(32), rnd.randrange(32), rnd.randrange(2, 5)
|
|
d.ellipse((x - r, y - r, x + r, y + r), fill=tuple(min(255, c + 14) for c in bg))
|
|
d.rectangle((5, 24, 26, 31), fill=uni) # shoulders
|
|
d.rectangle((8, 22, 23, 24), fill=uni)
|
|
d.rectangle((13, 20, 18, 23), fill=face) # neck
|
|
d.rectangle((10, 9, 21, 20), fill=face) # head
|
|
gear = rnd.choice(["helmet", "cap", "balaclava", "beret", "bare"])
|
|
if gear == "helmet":
|
|
d.rectangle((9, 6, 22, 11), fill=uni); d.rectangle((8, 10, 23, 11), fill=uni)
|
|
elif gear == "cap":
|
|
d.rectangle((10, 6, 21, 9), fill=acc); d.rectangle((10, 9, 25, 10), fill=acc)
|
|
elif gear == "balaclava":
|
|
d.rectangle((9, 7, 22, 20), fill=(20, 20, 22)); d.rectangle((11, 12, 20, 14), fill=face)
|
|
elif gear == "beret":
|
|
d.rectangle((9, 6, 22, 9), fill=(150, 30, 30) if skin != "terror" else acc); d.rectangle((19, 5, 23, 7), fill=(150, 30, 30))
|
|
else:
|
|
d.rectangle((10, 7, 21, 9), fill=rnd.choice([(30, 24, 20), (90, 60, 30), (200, 180, 120)]))
|
|
if gear != "balaclava" or True:
|
|
eye = (18, 18, 20)
|
|
if rnd.random() < 0.35: # shades
|
|
d.rectangle((11, 12, 20, 13), fill=(10, 10, 12))
|
|
else:
|
|
d.point([(13, 13), (18, 13)], fill=eye)
|
|
if gear != "balaclava":
|
|
d.rectangle((14, 17, 17, 17), fill=tuple(max(0, c - 60) for c in face))
|
|
d.rectangle((14, 26, 17, 27), fill=acc) # a badge
|
|
im = im.resize((128, 128), Image.NEAREST)
|
|
return im
|
|
|
|
|
|
bots = {}
|
|
for f in glob.glob(os.path.join(ART, "scripts", "*.bot")) + [os.path.join(ART, "scripts", "bots.txt")]:
|
|
text = open(f, errors="replace").read()
|
|
for block in re.findall(r"\{(.*?)\}", text, re.S):
|
|
n = re.search(r"\bname\s+(\S+)", block); m = re.search(r"\bmodel\s+(\S+)", block)
|
|
if n and m:
|
|
bots[n.group(1)] = m.group(1).split("/")[1].split("_")[0]
|
|
mapping = {}
|
|
for name, skin in sorted(bots.items()):
|
|
key = name.lower()
|
|
portrait(name, skin if skin in SKINS else "urban").save(os.path.join(OUT, "portraits", key + ".png"))
|
|
mapping[name] = key
|
|
portrait("human", "urban").save(os.path.join(OUT, "portraits", "human.png"))
|
|
json.dump(mapping, open(os.path.join(OUT, "portraits", "bots.json"), "w"), indent=1)
|
|
print(len(shots), "levelshots,", len(ICON), "weapons,", len(mapping) + 1, "portraits")
|