Advertisement
Guest User

Python Script for Resource Map Generation

a guest
Jan 21st, 2016
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. # Use PIL or Pillow (fork of PIL), either works
  2. from PIL import Image,ImageDraw
  3. import json
  4.  
  5. data = file("resource_dump.json","rb").read()
  6. obj = json.loads(data)
  7.  
  8. scale = 0.5
  9.  
  10. colors = dict()
  11.  
  12. # RGB colors for all possible nodes
  13. # Animals
  14. colors["AITokarServer"]                 = (122,201,255)
  15. colors["AITokarBlueServer"]             = (122,201,255)
  16. colors["AITokarParrotServer"]           = (122,201,255)
  17. colors["AIBorServer"]                   = (122,201,255)
  18. colors["AIBorRadServer"]                = (122,201,255)
  19. colors["AIShigiServer"]                 = (122,201,255)
  20. colors["AIShigiForestServer"]           = (122,201,255)
  21. colors["AIShigiArcticServer"]           = (122,201,255)
  22. colors["AIShigiWolfServer"]             = (122,201,255)
  23. colors["AIYetiForestServer"]            = (122,201,255)
  24. colors["AIYetiServer"]                  = (122,201,255)
  25.  
  26. # Plants
  27. colors["OwrongResourceNode"]            = (158,255,169)
  28. colors["PitcherPlantResourceNode"]      = (158,255,169)
  29. colors["SucculentResourceNode"]         = (158,255,169)
  30.  
  31. # Rocks
  32. colors["FlintRockResourceNodeServer"]   = (0,0,0)
  33. colors["CoalRockResourceNodeServer"]    = (255,128,0)
  34. colors["SandstoneResourceNodeServer"]   = (255,195,160)
  35. colors["IronRockResourceNodeServer"]    = (100,120,140)
  36.  
  37. # Rare ores
  38. colors["Metal2ResourceNodeServer"]      = (255,0,0)
  39. colors["Metal3ResourceNodeServer"]      = (0,255,0)
  40. colors["Metal4ResourceNodeServer"]      = (0,0,255)
  41.  
  42. # Misc.
  43. colors["WorldItemFlint"]                = (0,0,0)
  44. colors["LargeMiningRockServer"]         = (255,00,128)
  45.  
  46. # Wood
  47. colors["WorldItemWood"]                 = (227,198,82)
  48. colors["LogResourceNodeServer"]         = (102,51,0)
  49. colors["Deadtree1ResourceNodeServer"]   = (102,51,0)
  50. colors["Deadtree2ResourceNodeServer"]   = (102,51,0)
  51. colors["Deadtree3ResourceNodeServer"]   = (102,51,0)
  52.  
  53. spawns = obj["nodes"]
  54.  
  55. # Background color to use for the image
  56. bg_color = (40,40,40)
  57.  
  58. # The coordinates in the node array range from roughly -4000.0 - 4000.0 so the full
  59. # scale would be 8000px. I think the map is actually 10000 pixel but there aren't nodes
  60. # outside so it doesn't atter
  61.  
  62. im = Image.new("RGB",(int(8000*scale),int(8000*scale)),bg_color)
  63.  
  64. # Used for single-pixel draw access
  65. px = im.load()
  66.  
  67. # Used for shape drawing
  68. drw = ImageDraw.Draw(im)
  69.  
  70. def draw_box(drw,x,y,col,size=5):
  71.     box = [x-size,y-size,x+size,y+size]
  72.     drw.rectangle(box,fill=col)
  73.  
  74. for obj in spawns:
  75.     # Prints
  76.     print obj["Name"]
  77.     for coord in obj["coords"]:
  78.         # Transform coordinates from -4000 ... 4000 range into scaled 0 ... 8000
  79.         x = coord["x"] * scale
  80.         y = coord["y"] * scale
  81.         x += 8000*scale*0.5
  82.         y += 8000*scale*0.5
  83.        
  84.         if obj["Name"] in colors:
  85.             draw_box(drw,x,y,colors[obj["Name"]],1)
  86.         else:
  87.             print "Warning: Undefined color for node [%s]" % (obj["Name"])
  88.            
  89.             # This draws a white pixel instead
  90.             px[x,y] = (255,255,255)
  91.            
  92. # print "Dimensions: X: %f -> %f Y: %f -> %f" % (low_x,hi_x,low_y,hi_y)
  93.  
  94. # We have to flip the image so it's the right orientation
  95. im = im.transpose(Image.FLIP_TOP_BOTTOM)
  96.  
  97. # And render!
  98. im.save("resource_map.png","PNG")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement