Guest User

Untitled

a guest
Sep 3rd, 2018
181
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import os, sys
  3. import random
  4. import psycopg2, psycopg2.extras
  5. import Image, ImageDraw, ImageEnhance
  6.  
  7. os.chdir(sys.path[0])
  8.  
  9. db = psycopg2.connect("host=localhost user=xoom password=1914 dbname=intergalactic")
  10. cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
  11.  
  12. def reduce_opacity(im, opacity):
  13. assert opacity >= 0 and opacity <= 1
  14. if im.mode != 'RGBA':
  15. im = im.convert('RGBA')
  16. else:
  17. im = im.copy()
  18. alpha = im.split()[3]
  19. alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
  20. im.putalpha(alpha)
  21. return im
  22.  
  23. class Galaxy(object):
  24.  
  25. def __init__(self):
  26. self.prefix = 'A'
  27. self.unitSize = 24
  28. self.cols = 28
  29. self.rows = 28
  30. self.sectors = {}
  31. self.coord = (0, 0)
  32.  
  33. self.width = self.unitSize * self.cols
  34. self.height = self.unitSize * self.rows
  35.  
  36. def plot(self):
  37. for x in range(0, self.cols):
  38. for y in range(0, self.rows):
  39. sector = Sector(self, (x, y))
  40. sector.plot()
  41. self.sectors[sector.coord] = sector
  42.  
  43. def insert(self):
  44. cursor.execute("DELETE FROM galaxies")
  45.  
  46. cursor.execute("INSERT INTO galaxies (prefix, unit_size, cols, rows) VALUES (%s, %s, %s, %s)", (self.prefix, self.unitSize, self.cols, self.rows))
  47. cursor.execute("SELECT currval('galaxies_galaxy_id_seq')")
  48. galaxy_id = cursor.fetchone()[0]
  49. print "Inserted galaxy %d\n" % galaxy_id
  50.  
  51. for sector_location, sector in self.sectors.items():
  52. cursor.execute("INSERT INTO sectors (galaxy_id, x, y) VALUES (%s, %s, %s)", (galaxy_id, sector.coord[0], sector.coord[1]))
  53. cursor.execute("SELECT currval('sectors_sector_id_seq')")
  54. sector_id = cursor.fetchone()[0]
  55.  
  56. print "\tInserted sector %d at A%02d,%02d:00,00:00,00\n" % (sector_id, sector.coord[0], sector.coord[1])
  57.  
  58. for system_location, system in sector.systems.items():
  59. cursor.execute("INSERT INTO systems (sector_id, x, y, alpha) VALUES (%s, %s, %s, %s)", (sector_id, system.coord[0], system.coord[1], system.alpha))
  60. cursor.execute("SELECT currval('systems_system_id_seq')")
  61. system_id = cursor.fetchone()[0]
  62.  
  63. print "\t\tInserted system %s at A%02d,%02d:%02d,%02d:00,00\n" % (system_id, sector.coord[0], sector.coord[1], system.coord[0], system.coord[1])
  64.  
  65. print "\n"
  66.  
  67. db.commit()
  68.  
  69. @staticmethod
  70. def load():
  71. cursor.execute("""
  72. SELECT
  73. g.galaxy_id, g.prefix, g.unit_size, g.rows, g.cols,
  74. se.sector_id, se.x sector_x, se.y sector_y,
  75. sy.system_id, sy.x system_x, sy.y system_y, sy.alpha
  76. FROM
  77. galaxies g
  78. JOIN sectors se ON se.galaxy_id = g.galaxy_id
  79. JOIN systems sy ON sy.sector_id = se.sector_id
  80. ORDER BY
  81. se.x, se.y, sy.x, sy.y
  82. """)
  83.  
  84. last_galaxy = None
  85. last_sector = None
  86. last_system = None
  87.  
  88. for row in cursor:
  89. if last_galaxy is None:
  90. last_galaxy = Galaxy()
  91. last_galaxy.unitSize = row['unit_size']
  92. last_galaxy.cols = row['cols']
  93. last_galaxy.rows = row['rows']
  94. last_galaxy.prefix = row['prefix']
  95. last_galaxy.galaxy_id = row['galaxy_id']
  96.  
  97. if last_sector is None or last_sector.sector_id != row['sector_id']:
  98. last_sector = Sector(last_galaxy, (row['sector_x'], row['sector_y']))
  99. last_sector.sector_id = row['sector_id']
  100.  
  101. last_galaxy.sectors[last_sector.coord] = last_sector
  102.  
  103. if last_system is None or last_system.system_id != row['system_id']:
  104. last_system = System(last_sector, (row['system_x'], row['system_y']))
  105. last_system.system_id = row['system_id']
  106. last_system.alpha = row['alpha']
  107.  
  108. last_sector.systems[last_system.coord] = last_system
  109.  
  110. return last_galaxy
  111.  
  112. def draw(self):
  113. background = Image.open("../www/images/map_bg3.png");
  114. background.resize((self.width, self.height));
  115. background = reduce_opacity(background, .4);
  116. box = (0, 0) + background.size
  117.  
  118. self.area = Image.new("RGB", (self.width, self.height), 0)
  119. self.area.paste(background, box, background);
  120.  
  121. grid = Image.new("RGBA", self.area.size, (0, 0, 0, 0));
  122. draw = ImageDraw.Draw(grid);
  123.  
  124. #Draw the grid
  125. """
  126. for x in range(0, self.width, self.unitSize):
  127. for y in range(0, self.height, self.unitSize):
  128. draw.line(
  129. [
  130. (x, y),
  131. (x+self.unitSize, y)
  132. ]
  133. )
  134. draw.line(
  135. [
  136. (x, y),
  137. (x, y+self.unitSize)
  138. ]
  139. )
  140. grid = reduce_opacity(grid, 0.25);
  141. self.area.paste(grid, (0, 0) + grid.size, grid)
  142. """
  143.  
  144. unit_layer = Image.new("RGBA", self.area.size, (0, 0, 0, 0))
  145. unit_thumbs = [
  146. #Image.open("../www/images/clips/star_orange.png"),
  147. Image.open("../www/images/star_white.png"),
  148. Image.open("../www/images/star_white_halo.png")
  149. #Image.open("../www/images/clips/star_blue.png"),
  150. #Image.open("../www/images/clips/star_red.png")
  151. ]
  152.  
  153. def drawUnit(sector, system, unit_thumb):
  154.  
  155. #Get a random size for this unit
  156. #sq = random.randrange(5, 7);
  157. width = random.randrange(2, 10)
  158. height = random.randrange(2, 10)
  159.  
  160. thumb = reduce_opacity(unit_thumb, system.alpha * .1)
  161. #thumb.resize((width, height));
  162.  
  163. sector_x = sector.coord[0] * self.unitSize
  164. sector_y = sector.coord[1] * self.unitSize
  165.  
  166. system_x = system.coord[0]
  167. system_y = system.coord[1]
  168.  
  169. x = sector_x + system_x
  170. y = sector_y + system_y
  171.  
  172. x = x - (thumb.size[0]/2)
  173. y = y - (thumb.size[1]/2)
  174.  
  175. x2 = thumb.size[0] + x
  176. y2 = thumb.size[1] + y
  177.  
  178. unit_layer.paste(thumb, (x, y, x2, y2), thumb)
  179.  
  180. for sector_coord, sector in self.sectors.items():
  181. for system_coord, system in sector.systems.items():
  182. t = unit_thumbs[ random.randrange(0, len(unit_thumbs)) ]
  183. drawUnit(sector, system, t)
  184.  
  185. self.area.paste(unit_layer, (0, 0) + unit_layer.size, unit_layer)
  186.  
  187. def save(self, filename):
  188. self.area.save(filename, "PNG")
  189.  
  190. class Sector(object):
  191.  
  192. def __init__(self, galaxy, coord):
  193. self.parent = galaxy
  194. self.systems = {}
  195. self.coord = coord
  196.  
  197. def plot(self):
  198. num_units = random.randrange(2, 20)
  199. for i in range(0, num_units):
  200. x, y = self.uniqueCoord()
  201. system = System(self, (x, y))
  202. system.alpha = random.randrange(1, 10)
  203. #system.plot()
  204. self.systems[system.coord] = system
  205.  
  206. #create the blanks
  207. """
  208. for x in range(0, self.parent.cols):
  209. for y in range(0, self.parent.rows):
  210. if (x, y) not in self.systems.keys():
  211. system = System(self, (x, y))
  212. self.systems[system.coord] = system
  213. """
  214.  
  215. def uniqueCoord(self):
  216. unitSize = self.parent.unitSize
  217. loopProtection = 5000 #Make sure we're not rolling an infinite loop
  218. i = 0
  219. while True:
  220. #Kill the loop if we need to
  221. i = ++i
  222. if i >= loopProtection:
  223. return
  224.  
  225. x = random.randrange(0, self.parent.unitSize)
  226. y = random.randrange(0, self.parent.unitSize)
  227. #build the box
  228. x1 = x - (unitSize * .15)
  229. x2 = x + (unitSize * .15) #Box the unit at -1.5u to +1.5u
  230. y1 = y - (unitSize * .15)
  231. y2 = y + (unitSize * .15)
  232.  
  233. if (x, y) in self.systems:
  234. continue
  235.  
  236. #Now the hard part, checking the bounding box against all existing
  237. #Systems
  238. try:
  239. for coord, system in self.systems.items():
  240. sx, sy = coord
  241. if sx >= x1 and sx <= x2:
  242. if sy >= y1 and sy <= y2:
  243. raise Exception("Overlap")
  244. except:
  245. continue
  246.  
  247. return (x, y)
  248.  
  249. class System(object):
  250.  
  251. _StarTypes = {
  252. 'red': 20,
  253. 'orange': 300,
  254. 'yellow': 1200,
  255. 'blue': 2500,
  256. 'white': 5000
  257. }
  258.  
  259. def __init__(self, sector, coord):
  260. self.parent = sector
  261. self.stellars = {}
  262. self.coord = coord
  263. self.alpha = 1
  264. self.type = System._StarTypes['white']
  265.  
  266. def plot(self):
  267. num_units = random.randrange(2, 6)
  268. for i in range(0, num_units):
  269. x = random.randrange(0, self.parent.unitSize)
  270. y = random.randrange(0, self.parent.unitSize)
  271. stellar = Stellar(self, (x, y))
  272. stellar.alpha = random.randrange(0, 10)
  273.  
  274. self.systems[system.coord] = system
  275.  
  276.  
  277. class Stellar(object):
  278.  
  279. _PlanetTypes = {
  280. }
  281. def __init__(self, system, coord):
  282. self.parent = system;
  283. self.coord = coord;
  284. self.solar = self.getSolarBonus()
  285.  
  286. def getSolarBonus(self):
  287. pass
  288.  
  289. galaxy = Galaxy()
  290. galaxy.plot()
  291. galaxy.insert()
  292.  
  293. #galaxy = Galaxy.load()
  294.  
  295. galaxy.draw()
  296. galaxy.save("../www/images/galaxy.png")
  297.  
  298. import webbrowser
  299. webbrowser.open("file:///www/intergalactic/www/images/galaxy.png")
RAW Paste Data