Advertisement
MrCheeze

pokered_collision.py

Mar 19th, 2025
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. import glob
  2. from PIL import Image, ImageFont, ImageDraw, ImageEnhance
  3.  
  4. map_dimensions = {}
  5.  
  6. for line in open('pokered/constants/map_constants.asm'):
  7.     if 'map_const ' in line:
  8.         _, enum, x, y, _ = line.strip().split(None,4)
  9.         map_dimensions[enum[:-1]] = (int(x[:-1]),int(y))
  10.  
  11. tileset_map = {
  12. 'OVERWORLD': ('overworld', {0x00, 0x10, 0x1b, 0x20, 0x21, 0x23, 0x2c, 0x2d, 0x2e, 0x30, 0x31, 0x33, 0x39, 0x3c, 0x3e, 0x52, 0x54, 0x58, 0x5b}, {}),
  13. 'REDS_HOUSE_1': ('reds_house', {0x01, 0x02, 0x03, 0x11, 0x12, 0x13, 0x14, 0x1c, 0x1a}, {}),
  14. 'MART': ('pokecenter', {0x11, 0x1a, 0x1c, 0x3c, 0x5e}, {}),
  15. 'FOREST': ('forest', {0x1e, 0x20, 0x2e, 0x30, 0x34, 0x37, 0x39, 0x3a, 0x40, 0x51, 0x52, 0x5a, 0x5c, 0x5e, 0x5f}, {(0x30, 0x2E), (0x52, 0x2E), (0x55, 0x2E), (0x56, 0x2E), (0x20, 0x2E), (0x5E, 0x2E), (0x5F, 0x2E), (0x14, 0x2E), (0x48, 0x2E)}),
  16. #'FOREST': ('forest', {0x1e, 0x20, 0x2e, 0x30, 0x34, 0x37, 0x39, 0x3a, 0x40, 0x51, 0x52, 0x5a, 0x5c, 0x5e, 0x5f}, {(0x30, 0x2E), (0x52, 0x2E), (0x55, 0x2E), (0x56, 0x2E), (0x20, 0x2E), (0x5E, 0x2E), (0x5F, 0x2E)}),
  17. 'REDS_HOUSE_2': ('reds_house', {0x01, 0x02, 0x03, 0x11, 0x12, 0x13, 0x14, 0x1c, 0x1a}, {}),
  18. 'DOJO': ('gym', {0x11, 0x16, 0x19, 0x2b, 0x3c, 0x3d, 0x3f, 0x4a, 0x4c, 0x4d, 0x03}, {}),
  19. 'POKECENTER': ('pokecenter', {0x11, 0x1a, 0x1c, 0x3c, 0x5e}, {}),
  20. 'GYM': ('gym', {0x11, 0x16, 0x19, 0x2b, 0x3c, 0x3d, 0x3f, 0x4a, 0x4c, 0x4d, 0x03}, {}),
  21. 'HOUSE': ('house', {0x01, 0x12, 0x14, 0x28, 0x32, 0x37, 0x44, 0x54, 0x5c}, {}),
  22. 'FOREST_GATE': ('gate', {0x01, 0x12, 0x14, 0x1a, 0x1c, 0x37, 0x38, 0x3b, 0x3c, 0x5e}, {}),
  23. 'MUSEUM': ('gate', {0x01, 0x12, 0x14, 0x1a, 0x1c, 0x37, 0x38, 0x3b, 0x3c, 0x5e}, {}),
  24. 'UNDERGROUND': ('underground', {0x0b, 0x0c, 0x13, 0x15, 0x18}, {}),
  25. 'GATE': ('gate', {0x01, 0x12, 0x14, 0x1a, 0x1c, 0x37, 0x38, 0x3b, 0x3c, 0x5e}, {}),
  26. 'SHIP': ('ship', {0x04, 0x0d, 0x17, 0x1d, 0x1e, 0x23, 0x34, 0x37, 0x39, 0x4a}, {}),
  27. 'SHIP_PORT': ('ship_port', {0x0a, 0x1a, 0x32, 0x3b}, {}),
  28. 'CEMETERY': ('cemetery', {0x01, 0x10, 0x13, 0x1b, 0x22, 0x42, 0x52}, {}),
  29. 'INTERIOR': ('interior', {0x04, 0x0f, 0x15, 0x1f, 0x3b, 0x45, 0x47, 0x55, 0x56}, {}),
  30. 'CAVERN': ('cavern', {0x05, 0x15, 0x18, 0x1a, 0x20, 0x21, 0x22, 0x2a, 0x2d, 0x30}, {(0x20, 0x05), (0x41, 0x05), (0x2A, 0x05), (0x05, 0x21), (0x14, 0x05)}),
  31. #'CAVERN': ('cavern', {0x05, 0x15, 0x18, 0x1a, 0x20, 0x21, 0x22, 0x2a, 0x2d, 0x30}, {(0x20, 0x05), (0x41, 0x05), (0x2A, 0x05), (0x05, 0x21)}),
  32. 'LOBBY': ('lobby', {0x14, 0x17, 0x1a, 0x1c, 0x20, 0x38, 0x45}, {}),
  33. 'MANSION': ('mansion', {0x01, 0x05, 0x11, 0x12, 0x14, 0x1a, 0x1c, 0x2c, 0x53}, {}),
  34. 'LAB': ('lab', {0x0c, 0x26, 0x16, 0x1e, 0x34, 0x37}, {}),
  35. 'CLUB': ('club', {0x0f, 0x1a, 0x1f, 0x26, 0x28, 0x29, 0x2c, 0x2d, 0x2e, 0x2f, 0x41}, {}),
  36. 'FACILITY': ('facility', {0x01, 0x10, 0x11, 0x13, 0x1b, 0x20, 0x21, 0x22, 0x30, 0x31, 0x32, 0x42, 0x43, 0x48, 0x52, 0x55, 0x58, 0x5e}, {}),
  37. 'PLATEAU': ('plateau', {0x1b, 0x23, 0x2c, 0x2d, 0x3b, 0x45}, {}),
  38. }
  39.  
  40. for fname in glob.glob("pokered/data/maps/headers/*"):
  41.     name, enum, tileset, _ = next(open(fname)).strip()[11:].split(', ')
  42.     print(name)
  43.     width, height = map_dimensions[enum]
  44.     if name == 'UndergroundPathRoute7Copy':
  45.         name2 = 'UndergroundPathRoute7'
  46.     #elif name.startswith('CeruleanCave'):
  47.     #    name2 = 'green/'+name
  48.     else:
  49.         name2 = name
  50.  
  51.     blocks = open(f'pokered/maps/{name2}.blk','rb').read()
  52.  
  53.     tileset2, coll_tiles, coll_pairs = tileset_map[tileset]
  54.    
  55.     tileset_gfx = Image.open(f'pokered/gfx/tilesets/{tileset2}.png')
  56.     tiles = open(f'pokered/gfx/blocksets/{tileset2}.bst','rb').read()
  57.  
  58.     graphics = []
  59.     for y in range(0,tileset_gfx.height,8):
  60.         for x in range(0,tileset_gfx.width,8):
  61.             graphics.append(tileset_gfx.crop((x,y,x+8,y+8)))
  62.  
  63.     im = Image.new(mode='RGB',size=(width*32,height*32))
  64.     draw = ImageDraw.Draw(im, 'RGBA')
  65.  
  66.     subtiles = []
  67.     for y in range(height*4):
  68.         subtiles.append([None] * (width*4))
  69.  
  70.     for y in range(len(blocks)//width):
  71.         for x in range(width):
  72.             block = blocks[y*width+x]
  73.             tile = tiles[block*0x10:(block+1)*0x10]
  74.             for i in range(0x10):
  75.                 y_coord = y*4 + (i // 4)
  76.                 x_coord = x*4 + (i % 4)
  77.                 subtiles[y_coord][x_coord] = tile[i]
  78.  
  79.     for y in range(len(subtiles)):
  80.         for x,subtile in enumerate(subtiles[y]):
  81.             if subtile is not None:
  82.                 im.paste(graphics[subtile], (x*8, y*8))
  83.  
  84.     for y in range(len(subtiles)):
  85.         for x,subtile in enumerate(subtiles[y]):
  86.             if y%2==1 and x%2==0:
  87.                 if subtile not in coll_tiles:
  88.                     if subtile in {0x14,0x32,0x48} and tileset in {'OVERWORLD','FOREST','DOJO','GYM','SHIP','CAVERN','FACILITY','PLATEAU'}:
  89.                         draw.rectangle((x*8, y*8-8, x*8+16, y*8+8), fill=(0,0,255,128))
  90.                     elif subtile == 0x14 and tileset == 'SHIP_PORT':
  91.                         draw.rectangle((x*8, y*8-8, x*8+16, y*8+8), fill=(0,0,255,128))
  92.                     else:
  93.                         draw.rectangle((x*8, y*8-8, x*8+16, y*8+8), fill=(255,0,0,128))
  94.  
  95.                 if (y-2) >= 0 and (subtile, subtiles[y-2][x]) in coll_pairs:
  96.                     draw.rectangle((x*8, y*8-12, x*8+16, y*8-4), fill=(255,0,0,128))
  97.                 if (x-2) >= 0 and (subtile, subtiles[y][x-2]) in coll_pairs:
  98.                     draw.rectangle((x*8-4, y*8-8, x*8+4, y*8+8), fill=(255,0,0,128))
  99.                 if (y+2) < len(subtiles) and (subtile, subtiles[y+2][x]) in coll_pairs:
  100.                     draw.rectangle((x*8, y*8+4, x*8+16, y*8+12), fill=(255,0,0,128))
  101.                 if (x+2) < len(subtiles[y]) and (subtile, subtiles[y][x+2]) in coll_pairs:
  102.                     draw.rectangle((x*8+12, y*8-8, x*8+20, y*8+8), fill=(255,0,0,128))
  103.    
  104.     im.save(f'output/{name2}.png')
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement