Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import os, sys
- import random
- import psycopg2, psycopg2.extras
- import Image, ImageDraw, ImageEnhance
- os.chdir(sys.path[0])
- db = psycopg2.connect("host=localhost user=xoom password=1914 dbname=intergalactic")
- cursor = db.cursor(cursor_factory=psycopg2.extras.DictCursor)
- def reduce_opacity(im, opacity):
- assert opacity >= 0 and opacity <= 1
- if im.mode != 'RGBA':
- im = im.convert('RGBA')
- else:
- im = im.copy()
- alpha = im.split()[3]
- alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
- im.putalpha(alpha)
- return im
- class Galaxy(object):
- def __init__(self):
- self.prefix = 'A'
- self.unitSize = 24
- self.cols = 28
- self.rows = 28
- self.sectors = {}
- self.coord = (0, 0)
- self.width = self.unitSize * self.cols
- self.height = self.unitSize * self.rows
- def plot(self):
- for x in range(0, self.cols):
- for y in range(0, self.rows):
- sector = Sector(self, (x, y))
- sector.plot()
- self.sectors[sector.coord] = sector
- def insert(self):
- cursor.execute("DELETE FROM galaxies")
- cursor.execute("INSERT INTO galaxies (prefix, unit_size, cols, rows) VALUES (%s, %s, %s, %s)", (self.prefix, self.unitSize, self.cols, self.rows))
- cursor.execute("SELECT currval('galaxies_galaxy_id_seq')")
- galaxy_id = cursor.fetchone()[0]
- print "Inserted galaxy %d\n" % galaxy_id
- for sector_location, sector in self.sectors.items():
- cursor.execute("INSERT INTO sectors (galaxy_id, x, y) VALUES (%s, %s, %s)", (galaxy_id, sector.coord[0], sector.coord[1]))
- cursor.execute("SELECT currval('sectors_sector_id_seq')")
- sector_id = cursor.fetchone()[0]
- print "\tInserted sector %d at A%02d,%02d:00,00:00,00\n" % (sector_id, sector.coord[0], sector.coord[1])
- for system_location, system in sector.systems.items():
- 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))
- cursor.execute("SELECT currval('systems_system_id_seq')")
- system_id = cursor.fetchone()[0]
- 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])
- print "\n"
- db.commit()
- @staticmethod
- def load():
- cursor.execute("""
- SELECT
- g.galaxy_id, g.prefix, g.unit_size, g.rows, g.cols,
- se.sector_id, se.x sector_x, se.y sector_y,
- sy.system_id, sy.x system_x, sy.y system_y, sy.alpha
- FROM
- galaxies g
- JOIN sectors se ON se.galaxy_id = g.galaxy_id
- JOIN systems sy ON sy.sector_id = se.sector_id
- ORDER BY
- se.x, se.y, sy.x, sy.y
- """)
- last_galaxy = None
- last_sector = None
- last_system = None
- for row in cursor:
- if last_galaxy is None:
- last_galaxy = Galaxy()
- last_galaxy.unitSize = row['unit_size']
- last_galaxy.cols = row['cols']
- last_galaxy.rows = row['rows']
- last_galaxy.prefix = row['prefix']
- last_galaxy.galaxy_id = row['galaxy_id']
- if last_sector is None or last_sector.sector_id != row['sector_id']:
- last_sector = Sector(last_galaxy, (row['sector_x'], row['sector_y']))
- last_sector.sector_id = row['sector_id']
- last_galaxy.sectors[last_sector.coord] = last_sector
- if last_system is None or last_system.system_id != row['system_id']:
- last_system = System(last_sector, (row['system_x'], row['system_y']))
- last_system.system_id = row['system_id']
- last_system.alpha = row['alpha']
- last_sector.systems[last_system.coord] = last_system
- return last_galaxy
- def draw(self):
- background = Image.open("../www/images/map_bg3.png");
- background.resize((self.width, self.height));
- background = reduce_opacity(background, .4);
- box = (0, 0) + background.size
- self.area = Image.new("RGB", (self.width, self.height), 0)
- self.area.paste(background, box, background);
- grid = Image.new("RGBA", self.area.size, (0, 0, 0, 0));
- draw = ImageDraw.Draw(grid);
- #Draw the grid
- """
- for x in range(0, self.width, self.unitSize):
- for y in range(0, self.height, self.unitSize):
- draw.line(
- [
- (x, y),
- (x+self.unitSize, y)
- ]
- )
- draw.line(
- [
- (x, y),
- (x, y+self.unitSize)
- ]
- )
- grid = reduce_opacity(grid, 0.25);
- self.area.paste(grid, (0, 0) + grid.size, grid)
- """
- unit_layer = Image.new("RGBA", self.area.size, (0, 0, 0, 0))
- unit_thumbs = [
- #Image.open("../www/images/clips/star_orange.png"),
- Image.open("../www/images/star_white.png"),
- Image.open("../www/images/star_white_halo.png")
- #Image.open("../www/images/clips/star_blue.png"),
- #Image.open("../www/images/clips/star_red.png")
- ]
- def drawUnit(sector, system, unit_thumb):
- #Get a random size for this unit
- #sq = random.randrange(5, 7);
- width = random.randrange(2, 10)
- height = random.randrange(2, 10)
- thumb = reduce_opacity(unit_thumb, system.alpha * .1)
- #thumb.resize((width, height));
- sector_x = sector.coord[0] * self.unitSize
- sector_y = sector.coord[1] * self.unitSize
- system_x = system.coord[0]
- system_y = system.coord[1]
- x = sector_x + system_x
- y = sector_y + system_y
- x = x - (thumb.size[0]/2)
- y = y - (thumb.size[1]/2)
- x2 = thumb.size[0] + x
- y2 = thumb.size[1] + y
- unit_layer.paste(thumb, (x, y, x2, y2), thumb)
- for sector_coord, sector in self.sectors.items():
- for system_coord, system in sector.systems.items():
- t = unit_thumbs[ random.randrange(0, len(unit_thumbs)) ]
- drawUnit(sector, system, t)
- self.area.paste(unit_layer, (0, 0) + unit_layer.size, unit_layer)
- def save(self, filename):
- self.area.save(filename, "PNG")
- class Sector(object):
- def __init__(self, galaxy, coord):
- self.parent = galaxy
- self.systems = {}
- self.coord = coord
- def plot(self):
- num_units = random.randrange(2, 20)
- for i in range(0, num_units):
- x, y = self.uniqueCoord()
- system = System(self, (x, y))
- system.alpha = random.randrange(1, 10)
- #system.plot()
- self.systems[system.coord] = system
- #create the blanks
- """
- for x in range(0, self.parent.cols):
- for y in range(0, self.parent.rows):
- if (x, y) not in self.systems.keys():
- system = System(self, (x, y))
- self.systems[system.coord] = system
- """
- def uniqueCoord(self):
- unitSize = self.parent.unitSize
- loopProtection = 5000 #Make sure we're not rolling an infinite loop
- i = 0
- while True:
- #Kill the loop if we need to
- i = ++i
- if i >= loopProtection:
- return
- x = random.randrange(0, self.parent.unitSize)
- y = random.randrange(0, self.parent.unitSize)
- #build the box
- x1 = x - (unitSize * .15)
- x2 = x + (unitSize * .15) #Box the unit at -1.5u to +1.5u
- y1 = y - (unitSize * .15)
- y2 = y + (unitSize * .15)
- if (x, y) in self.systems:
- continue
- #Now the hard part, checking the bounding box against all existing
- #Systems
- try:
- for coord, system in self.systems.items():
- sx, sy = coord
- if sx >= x1 and sx <= x2:
- if sy >= y1 and sy <= y2:
- raise Exception("Overlap")
- except:
- continue
- return (x, y)
- class System(object):
- _StarTypes = {
- 'red': 20,
- 'orange': 300,
- 'yellow': 1200,
- 'blue': 2500,
- 'white': 5000
- }
- def __init__(self, sector, coord):
- self.parent = sector
- self.stellars = {}
- self.coord = coord
- self.alpha = 1
- self.type = System._StarTypes['white']
- def plot(self):
- num_units = random.randrange(2, 6)
- for i in range(0, num_units):
- x = random.randrange(0, self.parent.unitSize)
- y = random.randrange(0, self.parent.unitSize)
- stellar = Stellar(self, (x, y))
- stellar.alpha = random.randrange(0, 10)
- self.systems[system.coord] = system
- class Stellar(object):
- _PlanetTypes = {
- }
- def __init__(self, system, coord):
- self.parent = system;
- self.coord = coord;
- self.solar = self.getSolarBonus()
- def getSolarBonus(self):
- pass
- galaxy = Galaxy()
- galaxy.plot()
- galaxy.insert()
- #galaxy = Galaxy.load()
- galaxy.draw()
- galaxy.save("../www/images/galaxy.png")
- import webbrowser
- webbrowser.open("file:///www/intergalactic/www/images/galaxy.png")
Add Comment
Please, Sign In to add comment