Ejemplo n.º 1
0
def main():
    homeA = GalaxySpec("Red", 3, 1, 24, 5)
    homeB = GalaxySpec("Blue", 3, 1, 24, 5)
    hub = GalaxySpec("Alpha", 6, 3, 15, 5)
    goldmineA = GalaxySpec("Lambda", 4, 1, 36, 20)
    goldmineB = GalaxySpec("Mu", 4, 1, 42, 14)
    galaxies = [homeA, homeB, hub, goldmineA, goldmineB]

    bridges = [
            BridgeSpec(2, 2, homeA, hub),
            BridgeSpec(2, 2, homeB, hub),
            BridgeSpec(3, 4, goldmineA, hub),
            BridgeSpec(3, 4, goldmineB, hub),
            ]

    universe_spec = UniverseSpec(galaxies, bridges)

    generator = UniverseGenerator()
    universe = generator.generate_universe(universe_spec)
    with open('universe.dot', 'w') as f:
        f.write(render_universe(universe))

    with open('universe-fog.dot', 'w') as f:
        f.write(render_universe(universe, True))
Ejemplo n.º 2
0
def new_game_map(game, players):
    """Generate and save a fresh new universe"""
    universe_spec = get_default_universe_spec(players)
    generator = UniverseGenerator()

    universe = generator.generate_universe(universe_spec)

    system_to_db_system = {}
    for galaxy in universe.galaxies:
        if galaxy.name:
            db_galaxy = Galaxy(game=game,
                    name=galaxy.name,
                    guid=uuid4().hex)
            db_galaxy.save()
        else:
            db_galaxy = None

        for system in galaxy.systems:
            db_system = System(game=game,
                    galaxy=db_galaxy,
                    name=system.name,
                    population=system.population,
                    owner=None)
            db_system.save()
            system_to_db_system[system] = db_system

    # link systems
    for lane in universe.lanes:
        db_lane = StarLane(game=game,
                passable=False)
        db_lane.save()

        sys_a, sys_b = [system_to_db_system[s] for s in lane.systems]

        a_to_b = LaneAndSystem(game=game,
                               lane=db_lane,
                               system=sys_b)
        a_to_b.save()
        sys_a.neighbors.add(a_to_b)

        b_to_a = LaneAndSystem(game=game,
                               lane=db_lane,
                               system=sys_a)
        b_to_a.save()
        sys_b.neighbors.add(b_to_a)

        db_lane.systems.add(sys_a)
        db_lane.systems.add(sys_b)
        
        for system in lane.systems:
            db_system = system_to_db_system[system]
            link = LaneAndSystem(game=game,
                    lane=db_lane,
                    system=db_system)
            link.save()

            db_system.neighbors.add(link)
            db_lane.systems.add(db_system)

    for player, home in universe.homes.items():
        home_system = system_to_db_system[home]
        home_system.owner = player
        home_system.save()
        home_system.visible_to.add(player)

        player.home = home_system
        player.save()

        Ship(game=game,
                owner=player,
                ship_type=game_constants.SCOUT,
                attack_tech=player.attack_tech,
                range_tech=game_constants.ship_range(player, game_constants.SCOUT),
                moves=game_constants.ship_range(player, game_constants.SCOUT),
                system=home_system).save()

        for lane in StarLane.objects.filter(systems__id=home_system.id):
            lane.visible_to.add(player)