Advertisement
Guest User

Untitled

a guest
Jul 25th, 2023
27
0
64 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. import sys
  2. import math
  3. import random
  4. from omg import WAD, MapEditor # omgifol
  5. from PIL import Image, ImageDraw
  6.  
  7. SIDE_MARGIN = 40
  8. ICON_SIZE = 16
  9. TILE_SIZE = ICON_SIZE + 2
  10. TILE_ROW = 50
  11.  
  12. g_index = 0
  13. g_type2def = dict()
  14. g_name2def = dict()
  15.  
  16.  
  17. class S:
  18.     pass
  19.  
  20.  
  21. class ThingDef:
  22.     def __init__(self, name, type_id, icon):
  23.         self.name = name
  24.         self.type_id = type_id
  25.         self.icon = icon
  26.  
  27.         global g_index
  28.         g_index += 1
  29.         self.index = g_index
  30.  
  31.         global g_type2def, g_name2def
  32.         g_type2def[type_id] = self
  33.         g_name2def[name] = self
  34.  
  35.  
  36. g_things = [
  37.     ThingDef("zombie",              3004,   (1,0)),
  38.     ThingDef("sergeant",            9,      (2,0)),
  39.     ThingDef("commando",            65,     (3,0)),
  40.     ThingDef("imp",                 3001,   (4,0)),
  41.     ThingDef("demon",               3002,   (5,0)),
  42.     ThingDef("spectre",             58,     (6,0)),
  43.     ThingDef("lost soul",           3006,   (7,0)),
  44.     ThingDef("cacodemon",           3005,   (8,0)),
  45.     ThingDef("hell knight",         69,     (9,0)),
  46.     ThingDef("baron of hell",       3003,   (10,0)),
  47.     ThingDef("revenant",            66,     (11,0)),
  48.     ThingDef("mancubus",            67,     (12,0)),
  49.     ThingDef("arachnotron",         68,     (13,0)),
  50.     ThingDef("pain elemental",      71,     (14,0)),
  51.     ThingDef("archvile",            64,     (15,0)),
  52.     ThingDef("cyberdemon",          16,     (16,0)),
  53.     ThingDef("spider mastermind",   7,      (17,0)),
  54.  
  55.     ThingDef("ss guy",              84,     (18,0)),
  56.     ThingDef("commander keen",      72,     (19,0)),
  57.  
  58.     # ThingDef("spawn target", 87, (0,0)),
  59.     # ThingDef("spawn shooter", 89, (0,0)),
  60.     # ThingDef("romero head", 88, (0,0)),
  61.  
  62.     # ThingDef("sunder hive mother", 72, (0,0)),
  63.     # ThingDef("ancient aliens stealthy", 23, (0,0)),
  64.     # ThingDef("ancient aliens guardian", 72, (0,0)),
  65.     # ThingDef("ancient aliens guardian (2)", 12, (0,0)),
  66. ]
  67.  
  68.  
  69. # Construct icons dict
  70. g_icon_image = Image.open("icons.png")
  71. g_icons = dict()
  72. for y in range(g_icon_image.height // ICON_SIZE):
  73.     for x in range(g_icon_image.width // ICON_SIZE):
  74.         x0, y0 = x * ICON_SIZE, y * ICON_SIZE
  75.         x1, y1 = x0 + ICON_SIZE, y0 + ICON_SIZE
  76.         crop = g_icon_image.crop([x0,y0,x1,y1])
  77.         g_icons[(x,y)] = crop
  78.  
  79.  
  80. # Get list of all things in a specific map in g_type2def
  81. def mapGetThings(wad, map_id, skill='hard'):
  82.     edit = MapEditor(wad.maps[map_id])
  83.  
  84.     result = []
  85.  
  86.     for thing in edit.things:
  87.         if thing.multiplayer: continue
  88.         # I'm sure there's a better way to do this
  89.         if skill == 'easy' and not thing.easy: continue
  90.         if skill == 'medium' and not thing.medium: continue
  91.         if skill == 'hard' and not thing.hard: continue
  92.         type_id = thing.type
  93.         if type_id not in g_type2def: continue
  94.         thing = g_type2def[type_id]
  95.         result.append(thing)
  96.  
  97.     result = sorted(result, key=lambda t: t.index)
  98.     return result
  99.  
  100.  
  101.  
  102. # Draw the thing icons from a particular map_info
  103. def processMap(wad, im, map_info, y_off):
  104.  
  105.     draw = ImageDraw.Draw(im)
  106.     y_text = y_off
  107.     for line in map_info.text:
  108.         draw.text((4, y_text), line, fill='black')
  109.         y_text += 12
  110.  
  111.     for i, thing in enumerate(map_info.things):
  112.         x = SIDE_MARGIN + (i // map_info.rows) * TILE_SIZE
  113.         y = y_off + (i % map_info.rows) * TILE_SIZE
  114.         x0, y0 = x+1, y+1
  115.         x1, y1 = x0 + ICON_SIZE, y0 + ICON_SIZE
  116.         im.paste(g_icons[thing.icon], [x0,y0,x1,y1])
  117.  
  118.  
  119.  
  120. def main():
  121.     # Args
  122.     wad = WAD(sys.argv[1])
  123.     skill_arg = 'hard' # 'easy', 'medium' or 'hard'
  124.     if len(sys.argv) >= 3: skill_arg = sys.argv[2]
  125.     if skill_arg == 'all':
  126.         skills = ['easy', 'medium', 'hard']
  127.     else:
  128.         skills = [skill_arg]
  129.  
  130.     # Get list of sub-plots to draw (typically one per map)
  131.     map_ids = []
  132.     for map_id in wad.maps:
  133.         map_ids.append(map_id)
  134.     map_ids = sorted(map_ids)
  135.     subs = []
  136.     for map_id in map_ids:
  137.         for skill in skills:
  138.             subs.append((map_id, skill))
  139.  
  140.     # Get the necessarily map_infos
  141.     map_infos = []
  142.     im_height = 0
  143.     for map_id, skill in subs:
  144.         s = S()
  145.         s.text = [map_id]
  146.         if skill_arg == 'all':
  147.             s.text.append(skill)
  148.         print(f"scanning {s.text}")
  149.         s.things = mapGetThings(wad, map_id, skill)
  150.         s.rows = math.ceil(len(s.things) / TILE_ROW)
  151.         if len(s.things) == 0: s.rows += 1
  152.         map_infos.append(s)
  153.         im_height += TILE_SIZE * (s.rows + 1)
  154.  
  155.     # Create image
  156.     y_off = TILE_SIZE // 2 # Start half a tile lower
  157.     im_width = SIDE_MARGIN + TILE_SIZE * TILE_ROW
  158.     im = Image.new('RGB', (im_width, im_height), 'grey')
  159.     for map_info in map_infos:
  160.         print(f"drawing {map_info.text} ({len(map_info.things)} things)")
  161.         processMap(wad, im, map_info, y_off)
  162.         y_off += (map_info.rows + 1) * TILE_SIZE
  163.     im.save("out.png")
  164.  
  165.  
  166. if __name__ == "__main__":
  167.     main()
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement