コード例 #1
0
ファイル: galaxy.py プロジェクト: johnrmann/InterstellarAge
def galaxy_from_dict(data, game):
    """
    Creates the `Galaxy` for `game` from the `dict` `data`.

    Args:
        data (list):
            The list of `System` dictionaries.

        game (Game):
            The `Game` that the `Galaxy` is used for.

    Returns:
        The `Galaxy` that was parsed from the given `dict` and `Game`.
    """

    systems = []
    for system_dict in data:
        system = system_lib.system_from_dict(system_dict, game)
        systems.append(system)

    # Create the galaxy and return it.
    galaxy = Galaxy(game)
    galaxy.systems = systems
    return galaxy
コード例 #2
0
ファイル: galaxy.py プロジェクト: johnrmann/InterstellarAge
    def __init__(self, game, generate=False):
        """
        Not only is this the constructor for the `Galaxy` class, this also will
        generate a random `Galaxy` if and only if the correct and precise
        keyword arguments are passed in (hint: to generate a `Galaxy`, set
        `generate` to `True`).

        Args:
            game (Game):
                The `Game` which this `Galaxy` will be used for.

        Keyword Args:
            generate (boolean):
                Set to `True` if this `Galaxy` is to be generated at random.

        Note:
            It is the responsibility of the caller of this constructor to save
            this galaxy to disk.
        """

        # Assign attributes.
        self.game = game

        # Escape gase.
        if not generate:
            return

        # Declare global variables.
        global GALAXY_START_JSON
        global GALAXY_DEFAULT_RANGE

        global GALAXY_LENGTH
        global GALAXY_WIDTH
        global GALAXY_HEIGHT

        global STARS_PER_CUBIC_LY

        # Open the JSON
        import os
        current_directory = os.path.dirname(__file__)
        json_f = open(current_directory+GALAXY_START_JSON)
        json_contents = json_f.read()
        data = json.loads(json_contents)
        json_f.close()

        # Append default systems.
        for system in data:
            # Parse the system dict and assign the system object a unique ID.
            self._system_unique_counter += 1
            system_obj = system_lib.system_from_dict(system, game)
            system_obj.unique = self._system_unique_counter
            system_obj.galaxy = self

            # Assign the planets of this system unique identifiers
            system_planets = system_obj.flat_planets()
            for planet in system_planets:
                self._planet_unique_counter += 1
                planet.unique = self._planet_unique_counter

            self.systems.append(system_obj)

        # The dimensions of the galaxy.
        adj_dim = lambda x: (x - 1) / 2
        abs_sum = lambda x, y, z: abs(x) + abs(y) + abs(z)
        width = irange(-adj_dim(GALAXY_WIDTH), adj_dim(GALAXY_WIDTH))
        length = irange(-adj_dim(GALAXY_LENGTH), adj_dim(GALAXY_LENGTH))
        height = irange(-adj_dim(GALAXY_HEIGHT), adj_dim(GALAXY_HEIGHT))

        positions = [(x, y, z) for x in width for y in length for z in height]
        gdr = GALAXY_DEFAULT_RANGE
        in_default_range = lambda x, y, z: (abs_sum(x, y, z) <= gdr)
        in_discover_range = lambda x, y, z: (abs_sum(x, y, z) <= gdr + 4)

        # Loop through the galatic grid. For the positions outside the range of
        # default systems, randomly create new ones.
        generated = 0
        for (x, y, z) in positions:
            if in_default_range(x, y, z):
                continue
            elif coinflip(STARS_PER_CUBIC_LY):
                generated += 1
                self._system_unique_counter += 1
                new_sys = self._create_system(x, y, z)
                new_sys.unique = self._system_unique_counter

                if in_discover_range(x, y, z):
                    new_sys.discovered_by = set(game.players)

                # Assign the new planets unique identifiers.
                new_sys_planets = new_sys.flat_planets()
                for planet in new_sys_planets:
                    self._planet_unique_counter += 1
                    planet.unique = self._planet_unique_counter

                self.systems.append(new_sys)

        # Save to disk
        print "Generated {0} systems".format(str(generated))