예제 #1
0
파일: test_edge.py 프로젝트: sl4ker/hexgen
    def setUp(self):
        params = default_params
        self.size = 50
        params['size'] = self.size
        self.heightmap = Heightmap(params)

        self.grid = Grid(self.heightmap, params)

        self.h1 = self.grid.find_hex(0, 0)
        self.h4 = self.grid.find_hex(1, 0)
        self.h9 = self.grid.find_hex(2, 2)
        self.e1 = self.h1.edge_south_east
        self.e2 = self.h4.edge_north_west
        self.e3 = self.h9.edge_north_west
예제 #2
0
    def __init__(self, params, debug=False):
        """ initialize """
        self.params = default_params
        self.params.update(params)

        if debug:
            print("Making world with params:")
            for key, value in params.items():
                print("\t{}:\t{}".format(key, value))

        self.debug = debug

        if type(params.get('random_seed')) is int:
            random.seed(params.get('random_seed'))

        with Timer("Building Heightmap", self.debug):
            self.heightmap = Heightmap(self.params, self.debug)

        self.hex_grid = Grid(self.heightmap, self.params)
        if self.debug is True:
            print("\tAverage Height: {}".format(self.hex_grid.average_height))
            print("\tHighest Height: {}".format(self.hex_grid.highest_height))
            print("\tLowest Height: {}".format(self.hex_grid.lowest_height))

        print("Making calendar")
        self.calendar = Calendar(self.params.get('year_length'),
                                 self.params.get('day_length'))

        self.rivers = []
        self.rivers_sources = []

        with Timer("Computing hex distances", self.debug):
            self._get_distances()

        self._generate_pressure()

        if self.params.get('hydrosphere'):
            self._generate_rivers()

            # give coastal land hexes moisture based on how close to the coast they are
            # TODO: replace with more realistic model
            print("Making coastal moisture") if self.debug else False
            for y, row in enumerate(self.hex_grid.grid):
                for x, col in enumerate(row):
                    hex = self.hex_grid.grid[x][y]
                    if hex.is_land:
                        if hex.distance <= 5:
                            hex.moisture += 1
                        if hex.distance <= 3:
                            hex.moisture += random.randint(1, 3)
                        if hex.distance <= 1:
                            hex.moisture += random.randint(1, 6)

        # generate aquifers
        num_aquifers = random.randint(5, 25)

        if self.params.get('hydrosphere') is False or self.params.get(
                'sea_percent') == 100:
            num_aquifers = 0

        print(
            "Making {} aquifers".format(num_aquifers)) if self.debug else False
        aquifers = []
        while len(aquifers) < num_aquifers:
            rx = random.randint(0, len(self.hex_grid.grid) - 1)
            ry = random.randint(0, len(self.hex_grid.grid) - 1)
            hex = self.hex_grid.grid[rx][ry]
            if hex.is_land and hex.moisture < 5:
                aquifers.append(hex)

        for hex in aquifers:
            # print("Aquifer at ", hex)
            r1 = hex.bubble(distance=3)
            for hex in r1:
                if hex.is_land:
                    hex.moisture += random.randint(0, 2)
            r2 = hex.bubble(distance=2)
            for hex in r2:
                if hex.is_land:
                    hex.moisture += 1
            r3 = hex.surrounding
            for hex in r3:
                if hex.is_land:
                    hex.moisture += 1

        # decide terrain features
        print("Making terrain features") if self.debug else False
        # craters only form in barren planets with a normal or lower atmosphere
        if self.params.get('craters') is True:

            # decide number of craters
            num_craters = random.randint(0, 15)
            print("Making {} craters".format(num_craters))
            craters = []

            while len(craters) < num_craters:
                size = random.randint(1, 3)
                craters.append(
                    dict(hex=random.choice(self.hex_grid.hexes),
                         size=size,
                         depth=10 * size))

            for crater in craters:

                center_hex = crater.get('hex')
                size = crater.get('size')
                depth = crater.get('depth')
                hexes = []

                if size >= 1:
                    hexes = center_hex.surrounding
                    for h in hexes:
                        h.add_feature(HexFeature.crater)
                        h.altitude = center_hex.altitude - 5
                        h.altitude = max(h.altitude, 0)
                if size >= 2:
                    hexes = center_hex.bubble(distance=2)
                    for h in hexes:
                        h.add_feature(HexFeature.crater)
                        h.altitude = center_hex.altitude - 10
                        h.altitude = max(h.altitude, 0)

                if size >= 3:
                    hexes = center_hex.bubble(distance=3)
                    for h in hexes:
                        h.add_feature(HexFeature.crater)
                        h.altitude = center_hex.altitude - 15
                        h.altitude = max(h.altitude, 0)
                for h in hexes[:round(len(hexes) / 3)]:
                    for i in h.surrounding:
                        if i.has_feature(HexFeature.crater) is False:
                            i.add_feature(HexFeature.crater)
                            i.altitude = center_hex.altitude - 20
                            i.altitude = max(i.altitude, 0)

        # volcanoes
        if self.params.get('volcanoes'):

            num_volcanoes = random.randint(0, 10)
            print("Making {} volcanoes".format(num_volcanoes))
            volcanoes = []
            while len(volcanoes) < num_volcanoes:
                center_hex = random.choice(self.hex_grid.hexes)
                if center_hex.altitude > 50:
                    size = random.randint(1, 5)
                    height = random.randint(30, 70)
                    volcanoes.append(
                        dict(hex=center_hex, size=size, height=height))

            for volcano in volcanoes:
                height = volcano.get('height')
                size = volcano.get('size')
                center_hex = volcano.get('hex')
                print("\tVolcano: Size: {}, Height: {}".format(size, height))
                size_list = list(range(size))
                size_list.reverse()
                hexes = []
                for i in size_list:
                    i += 1
                    this_height = round(height / i)
                    if i == 1:
                        l = center_hex.surrounding + [center_hex]
                    else:
                        l = center_hex.bubble(distance=i)
                    for h in l:
                        hexes.append(h)
                        h.altitude = center_hex.altitude + this_height
                        h.add_feature(HexFeature.volcano)

                last_altitude = 0
                for h in hexes[:round(len(hexes) / 2)]:
                    for i in h.surrounding:
                        if i.has_feature(HexFeature.volcano) is False:
                            i.add_feature(HexFeature.volcano)
                            i.altitude += 5
                            i.altitude = min(i.altitude, 255)
                            last_altitude = i.altitude
                center_hex.altitude += last_altitude + 5

                # lava flow
                def step(active_hex):
                    if active_hex.altitude < 50:
                        return
                    else:
                        active_hex.add_feature(HexFeature.lava_flow)
                        found = []
                        for i in active_hex.surrounding:
                            if i.altitude <= active_hex.altitude and i.has_feature(
                                    HexFeature.lava_flow) is False:
                                found.append(i)
                        found.sort(key=lambda x: x.altitude)
                        if len(found) > 1:
                            step(found[0])
                        if len(found) > 2:
                            step(found[1])

                step(center_hex)

        self.territories = []
        self.generate_territories()
        self.generate_resources()
        self.geoforms = []
        self._determine_landforms()

        print("Done") if self.debug else False