vssg/starmap_gen.py
2007-01-24 17:06:27 +00:00

171 lines
5 KiB
Python

from starmap import *
import copy
import random
class Starmap_Params(object):
def __init__(self):
self.density = 2
self.minerals = Minerals_Params(0.1, 0.2, 0.4, 0.2, 0.1)
self.habitat = Habitat_Params(0.05, 0.03, 0.10, 0.07, 0.06, 0.09, 0.06, 0.09, 0.08, 0.08, 0.12, 0.08, 0.06, 0.08)
self.size = (80, 60)
class PercentArray(object):
def __init__(self):
self.order = []
self.array = {}
def __setitem__(self, key, value):
if not self.array.has_key(key):
self.order += [key]
self.array[key] = value
def __getitem__(self, key):
return self.array[key]
def normalize(self):
tot = sum(self.array.values())
for k in self.array.keys():
self[k] = self[k] / tot
def __mul__(self, other):
if type(other) != type(self):
raise TypeError
retval = copy.deepcopy(self)
for k in retval.array.keys():
retval[k] *= other[k]
retval.normalize()
return retval
def random(self):
loopcount = 0
while True:
loopcount += 1
rv = random.random()
tot = 0.0
for k in self.order:
tot += self[k]
if rv < tot:
return k
if loopcount >= 5:
raise IndexError
class Minerals_Params(PercentArray):
def __init__(self, vpoor, poor, normal, rich, vrich):
PercentArray.__init__(self)
self['vpoor'] = vpoor
self['poor'] = poor
self['normal'] = normal
self['rich'] = rich
self['vrich'] = vrich
class Habitat_Params(PercentArray):
def __init__(self, noplanet, paradise, terran, ocean, swamp, arid, desert, tundra, minimal, barren, dead, inferno, toxic, radiated):
PercentArray.__init__(self)
self['noplanet'] = noplanet
self['paradise'] = paradise
self['terran'] = terran
self['ocean'] = ocean
self['swamp'] = swamp
self['arid'] = arid
self['desert'] = desert
self['tundra'] = tundra
self['minimal'] = minimal
self['barren'] = barren
self['dead'] = dead
self['inferno'] = inferno
self['toxic'] = toxic
self['radiated'] = radiated
startypes = []
whitedwarf_min = Minerals_Params(0.0, 0.2, 0.5, 2.0, 3.5)
whitedwarf_hab = Habitat_Params(3.0, 0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.8, 0.7, 1.5, 4.5, 1.0, 2.0, 2.0)
bluestar_min = Minerals_Params(0.5, 0.5, 1.0, 2.0, 2.5)
bluestar_hab = Habitat_Params(1.0, 0.4, 0.6, 0.6, 0.6, 0.9, 1.0, 0.6, 1.1, 1.2, 1.5, 3.0, 2.0, 3.0)
redstar_min = Minerals_Params(2.0, 2.0, 1.0, 0.4, 0.3)
redstar_hab = Habitat_Params(1.0, 1.5, 1.5, 1.5, 1.5, 1.5, 1.5, 1.2, 1.0, 0.9, 0.8, 0.5, 0.5, 0.5)
yellowstar_min = Minerals_Params(1.0, 1.0, 1.0, 1.0, 1.0)
yellowstar_hab = Habitat_Params(1.0, 1.5, 1.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
greenstar_min = Minerals_Params(1.0, 1.0, 1.0, 1.5, 1.5)
greenstar_hab = Habitat_Params(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)
startypes.append((0.15, StarClass(name="Blue Giant", color="Blue", size="Giant", minerals_mod=bluestar_min, habitat_mod=bluestar_hab)))
startypes.append((0.15, StarClass(name="Green Star", color="Green", size="Star", minerals_mod=greenstar_min, habitat_mod=greenstar_hab)))
startypes.append((0.25, StarClass(name="Yellow Star", color="Yellow", size="Star", minerals_mod=yellowstar_min, habitat_mod=yellowstar_hab)))
startypes.append((0.30, StarClass(name="Red Dwarf", color="Red", size="Dwarf", minerals_mod=redstar_min, habitat_mod=redstar_hab)))
startypes.append((0.10, StarClass(name="Red Giant", color="Red", size="Giant", minerals_mod=redstar_min, habitat_mod=redstar_hab)))
startypes.append((0.05, StarClass(name="White Dwarf", color="White", size="Dwarf", minerals_mod=whitedwarf_min, habitat_mod=whitedwarf_hab)))
def starmap_generate_star(map):
pass
def starmap_generate():
params = Starmap_Params()
numstars = int(round((params.size[0] * params.size[1] / 100.0) * params.density))
mindist = None
mapx = params.size[0] - 2
mapy = params.size[1] - 2
map = Galaxy(params.size[0], params.size[1])
i = 0
print numstars
while True:
if i >= numstars:
break
if i != 0:
mindist = max(175.0, (mapx + mapy) / 2.0 * 100.0 / ((i + 1)**0.75*0.75))
x = (random.random() * mapx) + 1
y = (random.random() * mapy) + 1
x *= 100
y *= 100
if mindist != None and False:
star = map.get_nearest_star(x, y)
dist = map.get_distance(x, y, star.x, star.y)
if dist < mindist:
print "Dist (%s, %s): %s, Mindist (%s, %s): %s" % (star.x, star.y, dist, int(x), int(y), mindist)
continue
loopcount = 0
startype = None
while True:
loopcount += 1
rv = random.random()
tot = 0.0
for pct, startype_test in startypes:
tot += pct
if rv < tot:
startype = startype_test
break
if startype != None:
break
if loopcount >= 5:
raise IndexError
hab = params.habitat * startype.habitat_mod
min = params.minerals * startype.minerals_mod
hab = hab.random()
min = min.random()
print "Generated star %s %s %s at (%s, %s)" % (startype.name, hab, min, int(x), int(y))
star = StarSys(int(x), int(y), startype, hab, min)
star.set_frame_timers(0.1 + (random.random() * 2), 0.1 + (random.random() * 2))
map.add_star(star)
i += 1
return map