KAZIK/tools/generators/pack_card_tables.py
Artvasil09 3ad5bf4c9d Develop assembly-only casino scenes and native games
Add native Durak, poker, blackjack, roulette, Buckshot and billiards scenes with animation assets and regression tests. Improve arrival, city, hall, profiles, shops, audio and exit handling; remove the portable C game core and refresh shipping executables.
2026-10-08 22:39:19 +03:00

49 lines
2 KiB
Python

"""Compile artwork and Treys/Cactus Kev lookup data. Not part of the runtime."""
import itertools
import json
import random
import struct
import sys
from pathlib import Path
from PIL import Image
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tools/vendor"))
from treys import Card, Evaluator
from treys.lookup import LookupTable
def native_card(index):
return Card.new("23456789TJQKA"[index % 13] + "cdhs"[index // 13])
def main():
image = Image.open(ROOT / "assets/sprites/tables/card-table-v1.png").convert("RGBA")
image = image.resize((640, 480), Image.Resampling.LANCZOS)
pixels = image.tobytes("raw", "BGRA")
out = ROOT / "assets/textures/card-table.bin"
out.write_bytes(struct.pack("<4sIII", b"CTA1", 640, 480, len(pixels)) + pixels)
table = LookupTable()
# Sorted products allow a compact binary search in native assembly.
flush = sorted(table.flush_lookup.items())
plain = sorted(table.unsuited_lookup.items())
assert len(flush) == 1287 and len(plain) == 6175
data = b"".join(struct.pack("<II", product, score) for product, score in flush + plain)
path = ROOT / "assets/data/poker-ranks.bin"
path.write_bytes(struct.pack("<4sIII", b"PKR1", len(flush), len(plain), len(data)) + data)
evaluator = Evaluator()
random_source = random.Random(77291)
hands = [list(hand) for hand in itertools.combinations([8, 9, 10, 11, 12, 13, 25], 5)]
hands += [random_source.sample(range(52), count) for count in (5, 6, 7) for _ in range(3000)]
vectors = b"".join(struct.pack("<9I", len(hand), *hand, *([0] * (7 - len(hand))),
evaluator.evaluate([], [native_card(card) for card in hand]))
for hand in hands)
tests = ROOT / "build/tables/poker-vectors.bin"
tests.write_bytes(struct.pack("<I", len(hands)) + vectors)
print(json.dumps({"rank_entries": len(flush) + len(plain), "rank_bytes": len(data),
"test_hands": len(hands), "table": str(out)}))
if __name__ == "__main__":
main()