Fichiers
gastinel-tpa/outils/nbt_anvil.py
T
Theo GastineletClaude Sonnet 5.5 53cfd9f02f GastinelTpa 1.1.0 : /spawn, /rtp, /rtp spawn (blocs de commande), notation des beaux spots
- /spawn : spawn du monde (getSharedSpawnPos), délai annulé si mouvement/blessure, recharge.
- /rtp : zone déjà générée uniquement (chunk + 8 voisins complets sur disque, région absente
  jamais lue), atterrissage sûr, évite les claims GastinelClaims, recharge, messages FR.
- /rtp spawn : réservé aux blocs de commande, tirage pondéré parmi le top N de beaux-spots.json.
- /rtp diagnostic (op) : recherche sans téléportation, avec motifs de rejet.
- outils/beaux_spots.py : notation de beauté v0 sur une copie du monde.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-09 20:34:38 +02:00

145 lignes
4.3 KiB
Python

"""Lecture minimale (lecture seule) du format NBT et des régions Anvil (.mca) de Minecraft 1.21.
Aucune dépendance hors numpy. Ne modifie jamais les fichiers lus.
"""
import gzip
import struct
import zlib
import numpy as np
TAG_END, TAG_BYTE, TAG_SHORT, TAG_INT, TAG_LONG, TAG_FLOAT, TAG_DOUBLE = 0, 1, 2, 3, 4, 5, 6
TAG_BYTE_ARRAY, TAG_STRING, TAG_LIST, TAG_COMPOUND, TAG_INT_ARRAY, TAG_LONG_ARRAY = 7, 8, 9, 10, 11, 12
class _Lecteur:
__slots__ = ("b", "p")
def __init__(self, data):
self.b = data
self.p = 0
def u(self, fmt, n):
v = struct.unpack_from(fmt, self.b, self.p)[0]
self.p += n
return v
def chaine(self):
n = self.u(">H", 2)
s = self.b[self.p:self.p + n].decode("utf-8", "replace")
self.p += n
return s
def charge(self, t):
if t == TAG_BYTE:
return self.u(">b", 1)
if t == TAG_SHORT:
return self.u(">h", 2)
if t == TAG_INT:
return self.u(">i", 4)
if t == TAG_LONG:
return self.u(">q", 8)
if t == TAG_FLOAT:
return self.u(">f", 4)
if t == TAG_DOUBLE:
return self.u(">d", 8)
if t == TAG_BYTE_ARRAY:
n = self.u(">i", 4)
v = self.b[self.p:self.p + n]
self.p += n
return v
if t == TAG_STRING:
return self.chaine()
if t == TAG_LIST:
et = self.u(">b", 1)
n = self.u(">i", 4)
return [self.charge(et) for _ in range(n)]
if t == TAG_COMPOUND:
d = {}
while True:
ct = self.u(">b", 1)
if ct == TAG_END:
return d
nom = self.chaine()
d[nom] = self.charge(ct)
if t == TAG_INT_ARRAY:
n = self.u(">i", 4)
v = np.frombuffer(self.b, dtype=">i4", count=n, offset=self.p)
self.p += 4 * n
return v
if t == TAG_LONG_ARRAY:
n = self.u(">i", 4)
v = np.frombuffer(self.b, dtype=">u8", count=n, offset=self.p)
self.p += 8 * n
return v
raise ValueError(f"tag NBT inconnu {t}")
def lire_nbt(data):
r = _Lecteur(data)
t = r.u(">b", 1)
if t != TAG_COMPOUND:
raise ValueError("racine NBT non compound")
r.chaine()
return r.charge(TAG_COMPOUND)
def lire_level_dat(chemin):
with gzip.open(chemin, "rb") as f:
return lire_nbt(f.read())["Data"]
def chunks_de_region(chemin):
"""Itère (cx_local, cz_local, nbt) pour chaque chunk présent dans un fichier .mca."""
with open(chemin, "rb") as f:
data = f.read()
if len(data) < 8192:
return
for i in range(1024):
off = int.from_bytes(data[i * 4:i * 4 + 3], "big")
nsec = data[i * 4 + 3]
if off == 0 or nsec == 0:
continue
p = off * 4096
if p + 5 > len(data):
continue
longueur = int.from_bytes(data[p:p + 4], "big")
comp = data[p + 4]
brut = data[p + 5:p + 4 + longueur]
try:
if comp == 2:
nbt = lire_nbt(zlib.decompress(brut))
elif comp == 1:
nbt = lire_nbt(gzip.decompress(brut))
elif comp == 3:
nbt = lire_nbt(brut)
else:
continue # chunk externe (.mcc) ou compression inconnue : ignoré
except Exception:
continue
yield i % 32, i // 32, nbt
def deplier(longs, n_valeurs, bits):
"""Décode un tableau compacté (format 1.16+ : les valeurs ne chevauchent pas deux longs)."""
par_long = 64 // bits
masque = np.uint64((1 << bits) - 1)
a = np.asarray(longs, dtype=np.uint64)
decal = (np.arange(par_long, dtype=np.uint64) * np.uint64(bits))
vals = ((a[:, None] >> decal[None, :]) & masque).reshape(-1)
return vals[:n_valeurs].astype(np.int64)
def heightmap(longs, min_y):
"""Heightmap 16x16 -> tableau [z, x] de hauteurs absolues (premier bloc libre au-dessus)."""
if longs is None or len(longs) == 0:
return None
par_long = -(-256 // len(longs))
bits = 64 // par_long
return (deplier(longs, 256, bits) + min_y).reshape(16, 16)
def bits_pour(n, minimum):
b = max(1, (n - 1).bit_length())
return max(minimum, b) if n > 1 else 0