Example #1
0
	def create_map_images(self, mode=0):
		if mode == 0:
			print 'Creating images....'
			t0 = libtcod.sys_elapsed_seconds()
		con = libtcod.console_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
		self.map_image_small = libtcod.image_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
		self.create_map_legend(con, mode)
		libtcod.image_scale(self.map_image_small, (game.SCREEN_WIDTH - 2) * 2, (game.SCREEN_HEIGHT - 2) * 2)

		if mode == 0:
			while self.player_positionx == 0:
				start = self.randomize('int', 0, (game.WORLDMAP_WIDTH * game.WORLDMAP_HEIGHT) - 1, 3)
				if int(self.hm_list[start] * 1000) in range(int(game.terrain['Forest']['elevation'] * 1000), int(game.terrain['Forest']['maxelev'] * 1000)):
					self.player_positionx = start % game.WORLDMAP_WIDTH
					self.player_positiony = start / game.WORLDMAP_WIDTH
					self.originx = self.player_positionx
					self.originy = self.player_positiony

					path = self.set_dijkstra_map()
					for y in range(game.WORLDMAP_HEIGHT):
						for x in range(game.WORLDMAP_WIDTH):
							dist = libtcod.dijkstra_get_distance(path, x, y)
							if dist > self.max_distance:
								self.max_distance = int(round(dist))
					#libtcod.image_put_pixel(self.map_image_small, self.player_positionx, self.player_positiony, libtcod.white)

		if mode == 2:
			self.map_image_big = libtcod.image_from_console(con)
			libtcod.image_save(self.map_image_big, 'maps/worldmap-' + game.player.name + '.png')
			self.map_image_big = None
		libtcod.console_delete(con)
		if mode == 0:
			t1 = libtcod.sys_elapsed_seconds()
			print '    done! (%.3f seconds)' % (t1 - t0)
Example #2
0
	def generate(self):
		print 'Starting world map generation....'
		t0 = libtcod.sys_elapsed_seconds()
		accepted = False
		world = 1
		while not accepted:
			print 'World #' + str(world) + '....'
			self.noise = libtcod.noise_new(2, self.rnd)
			libtcod.noise_set_type(self.noise, libtcod.NOISE_PERLIN)
			game.heightmap = libtcod.heightmap_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
			#game.precipitation = libtcod.heightmap_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
			#game.temperature = libtcod.heightmap_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
			#game.biome = libtcod.heightmap_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)

			self.add_landmass()
			self.smooth_edges()
			self.set_landmass(self.randomize('float', 0.30, 0.45, 3), self.sandheight)
			self.add_rivers()
			self.create_map_images()
			self.place_dungeons()
			accepted = self.analyse_world()
			world += 1
			print '-------------'
		t1 = libtcod.sys_elapsed_seconds()
		print 'World map generation finished.... (%.3f seconds)' % (t1 - t0)
Example #3
0
	def smooth_edges(self):
		print 'Smoothing edges....'
		t0 = libtcod.sys_elapsed_seconds()
		hmcopy = libtcod.heightmap_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
		mask = libtcod.heightmap_new(game.WORLDMAP_WIDTH, game.WORLDMAP_HEIGHT)
		for x in range(game.WORLDMAP_WIDTH):
			for y in range(game.WORLDMAP_HEIGHT):
				ix = x * 0.04
				if x > game.WORLDMAP_WIDTH / 2:
					ix = (game.WORLDMAP_WIDTH - x - 1) * 0.04
				iy = y * 0.04
				if y > game.WORLDMAP_HEIGHT / 2:
					iy = (game.WORLDMAP_HEIGHT - y - 1) * 0.04
				if ix > 1.0:
					ix = 1.0
				if iy > 1.0:
					iy = 1.0
				h = min(ix, iy)
				libtcod.heightmap_set_value(mask, x, y, h)
		libtcod.heightmap_normalize(mask)
		libtcod.heightmap_copy(game.heightmap, hmcopy)
		libtcod.heightmap_multiply_hm(hmcopy, mask, game.heightmap)
		libtcod.heightmap_normalize(game.heightmap)
		t1 = libtcod.sys_elapsed_seconds()
		print '    done! (%.3f seconds)' % (t1 - t0)
Example #4
0
	def set_landmass(self, landmass, waterlevel):
		print 'Reducing landmass....'
		t0 = libtcod.sys_elapsed_seconds()
		heightcount = [0] * 256
		for x in range(game.WORLDMAP_WIDTH):
			for y in range(game.WORLDMAP_HEIGHT):
				h = int(libtcod.heightmap_get_value(game.heightmap, x, y) * 255)
				heightcount[h] += 1

		i, totalcount = 0, 0
		while totalcount < game.WORLDMAP_WIDTH * game.WORLDMAP_HEIGHT * (1.0 - landmass):
			totalcount += heightcount[i]
			i += 1
		newwaterlevel = i / 255.0
		landcoef = (1.0 - waterlevel) / (1.0 - newwaterlevel)
		watercoef = waterlevel / newwaterlevel

		for x in range(game.WORLDMAP_WIDTH):
			for y in range(game.WORLDMAP_HEIGHT):
				h = libtcod.heightmap_get_value(game.heightmap, x, y)
				if h > newwaterlevel:
					h = waterlevel + (h - newwaterlevel) * landcoef
				else:
					h = h * watercoef
				libtcod.heightmap_set_value(game.heightmap, x, y, h)
		t1 = libtcod.sys_elapsed_seconds()
		print '    done! (%.3f seconds)' % (t1 - t0)
Example #5
0
	def place_dungeons(self):
		print 'Placing dungeons....'
		t0 = libtcod.sys_elapsed_seconds()
		path = self.set_dijkstra_map()
		for i in range(game.MAX_THREAT_LEVEL):
			done = False
			attempt = 0
			while not done and attempt <= 1000:
				x = libtcod.random_get_int(self.rnd, 0, game.WORLDMAP_WIDTH - 1)
				y = libtcod.random_get_int(self.rnd, 0, game.WORLDMAP_HEIGHT - 1)
				cellheight = int(libtcod.heightmap_get_value(game.heightmap, x, y) * 1000)
				threat = self.set_threat_level(x, y, path)
				dice = libtcod.random_get_int(self.rnd, 1, 100)
				if dice <= 65:
					dtype = 'Dungeon'
				elif dice <= 95:
					dtype = 'Cave'
				else:
					dtype = 'Maze'
				if cellheight in range(int(game.terrain['Plains']['elevation'] * 1000), int(game.terrain['High Hills']['maxelev'] * 1000)) and threat == i + 1:
					self.dungeons.append((len(self.dungeons) + 1, 'Dungeon', 'Dng', x, y, threat + 1, dtype))
					done = True
				attempt += 1

		starter_dungeon = libtcod.random_get_int(self.rnd, 1, 4)
		if starter_dungeon == 1:
			self.dungeons.append((len(self.dungeons) + 1, 'Starter Dungeon', 'SD', self.player_positionx, self.player_positiony - 1, 1, 'Dungeon'))
		elif starter_dungeon == 2:
			self.dungeons.append((len(self.dungeons) + 1, 'Starter Dungeon', 'SD', self.player_positionx + 1, self.player_positiony, 1, 'Dungeon'))
		elif starter_dungeon == 3:
			self.dungeons.append((len(self.dungeons) + 1, 'Starter Dungeon', 'SD', self.player_positionx, self.player_positiony + 1, 1, 'Dungeon'))
		else:
			self.dungeons.append((len(self.dungeons) + 1, 'Starter Dungeon', 'SD', self.player_positionx - 1, self.player_positiony, 1, 'Dungeon'))
		t1 = libtcod.sys_elapsed_seconds()
		print '    done! (%.3f seconds)' % (t1 - t0)
Example #6
0
	def analyse_world(self):
		print 'Analysing worldmap....'
		t0 = libtcod.sys_elapsed_seconds()
		mountain_peak = 0
		mountains = 0
		high_hills = 0
		low_hills = 0
		forest = 0
		plains = 0
		coast = 0
		shore = 0
		sea = 0
		ocean = 0
		accepted = True

		for x in range(game.WORLDMAP_WIDTH):
			for y in range(game.WORLDMAP_HEIGHT):
				cellheight = libtcod.heightmap_get_value(game.heightmap, x, y)
				if cellheight >= game.terrain['Mountain Peak']['elevation']:
					mountain_peak += 1
				elif cellheight >= game.terrain['Mountains']['elevation']:
					mountains += 1
				elif cellheight >= game.terrain['High Hills']['elevation']:
					high_hills += 1
				elif cellheight >= game.terrain['Low Hills']['elevation']:
					low_hills += 1
				elif cellheight >= game.terrain['Forest']['elevation']:
					forest += 1
				elif cellheight >= game.terrain['Plains']['elevation']:
					plains += 1
				elif cellheight >= game.terrain['Coast']['elevation']:
					coast += 1
				elif cellheight >= game.terrain['Shore']['elevation']:
					shore += 1
				elif cellheight >= game.terrain['Sea']['elevation']:
					sea += 1
				else:
					ocean += 1

		if mountain_peak < 15 or mountains < 150 or high_hills < 600 or low_hills < 1500 or coast < 2500:
			accepted = False
		if forest > 22000 or plains > 10000 or shore > 8000 or sea > 28000 or ocean > 30000:
			accepted = False
		t1 = libtcod.sys_elapsed_seconds()
		if accepted:
			print '    accepted! (%.3f seconds)' % (t1 - t0)
		else:
			self.player_positionx = 0
			self.max_distance = 0
			self.dungeons = []
			print '    rejected! (%.3f seconds)' % (t1 - t0)
		return accepted
Example #7
0
	def add_landmass(self):
		print 'Creating landmass....'
		t0 = libtcod.sys_elapsed_seconds()
		for i in range(int(game.WORLDMAP_WIDTH * 0.55)):
			radius = self.randomize('float', 50 * (1.0 - 0.7), 50 * (1.0 + 0.7), 3)
			x = self.randomize('int', 0, game.WORLDMAP_WIDTH, 3)
			y = self.randomize('int', 0, game.WORLDMAP_HEIGHT, 3)
			libtcod.heightmap_add_hill(game.heightmap, x, y, radius, 0.3)
		libtcod.heightmap_normalize(game.heightmap)
		libtcod.heightmap_add_fbm(game.heightmap, self.noise, 6.5, 6.5, 0, 0, 8.0, 1.0, 4.0)
		libtcod.heightmap_normalize(game.heightmap)
		t1 = libtcod.sys_elapsed_seconds()
		print '    done! (%.3f seconds)' % (t1 - t0)
Example #8
0
    def new_find_path(self, start, end, tiles=[]):
        t0 = libtcod.sys_elapsed_seconds()

        if len(tiles) == 0:
            self.tiles = R.tiles
        else:
            self.tiles = tiles
        if self.check_blocked(start) and self.check_blocked(end):
            return None
        if self.tiles[start[0]][start[1]].continent != self.tiles[end[0]][end[1]].continent:
            print "not on same continent"
            return None

        self.frontier = PriorityQueue()
        self.node_costs = {}
        self.node_status = {}
        self.came_from = {}
        self.largest_cost = 0

        self.end = end_node = PathNode(end, 0)
        self.start = start_node = PathNode(start, 0, endNode=end_node)

        self.heuristic_cost = heuristic_straightline(start,end)

        self.frontier.put(start_node, 0)
        self.node_costs[start_node] = 0

        while not self.frontier.empty():
            current = self.frontier.get()

            if current.is_equal_to_node(end_node) and current == end_node:
                t1 = libtcod.sys_elapsed_seconds()
                path = self.reconstruct_path(self.came_from, start_node, end_node)
                print "path of length: ", len(path), " succeeded in %s" % (t1 - t0), "explored ", len(self.node_costs.keys())

                print "heuristic_cost:", self.heuristic_cost, " Actual cost:", self.node_costs[end_node]
                # return current #TODO: reconstruct path.
                path.reverse()
                return path
            else:
                for neighbour in self.find_neighbours(current, end_node):
                    new_cost = neighbour.cost
                    # if self.node_costs.has_key(neighbour):
                    if new_cost < self.node_costs.get(neighbour, float("inf")):
                        if self.largest_cost < new_cost:
                            self.largest_cost = new_cost
                        self.node_costs[neighbour] = new_cost
                        self.frontier.put(neighbour, new_cost + heuristic_straightline(neighbour.grid, end_node.grid))
                        self.came_from[neighbour] = current
Example #9
0
    def find_path(self, world, start, goal):

        if world.tiles[start[0]][start[1]].continent != world.tiles[goal[0]][goal[1]].continent:
            print "not on same continent"
            return None
        t0 = libtcod.sys_elapsed_seconds()
        graph = SquareGrid(R.MAP_WIDTH, R.MAP_HEIGHT)
        graph.walls = world.blocked
        graph.weights = world.weights
        came_from, costs = self.a_star(graph, start, goal)
        path = self.reconstruct_path(came_from, start, goal)
        path.reverse()
        t1 = libtcod.sys_elapsed_seconds()
        print "path of length: ", len(path), " succeeded in %s" % (t1 - t0), "explored ", len(self.node_costs.keys())
        return path
Example #10
0
def play_game():
    global key, mouse, player_turn

    mouse = libtcod.Mouse()
    key = libtcod.Key()

    start_time = libtcod.sys_elapsed_seconds()
    while not libtcod.console_is_window_closed():

        libtcod.sys_check_for_event(libtcod.EVENT_KEY_PRESS | libtcod.EVENT_MOUSE, key, mouse)

        delta_time = libtcod.sys_get_last_frame_length()
        #render the screen
        if not local:

            ##Clear the characters from screen.
            for object_ in R.world_obj:
                object_.clear(cam_x, cam_y)

            for city in cities:
                for merchant in city.trade_house.caravans_out:
                    merchant.clear(cam_x, cam_y)

            #handles the keys and exit if needed.
            player_action = handle_keys()
            if player_action == "exit":
                save_game()
                break
            if not pause:  #and not player_turn:
                advance_time()
                #player_turn = True

            handle_mouse()
            render_all()

        else:

            #            for object_ in R.locale_obj:
            #                object_.clear(cam_x,cam_y)
            #
            #            you.clear(cam_x, cam_y)

            #handles the keys and exit if needed.
            player_action = handle_keys()
            if player_action == "exit":
                save_game()
                break

            handle_mouse()
            render_local()

        if R.msg_redraw == True:
            update_msg_bar()
Example #11
0
def stats():
	libtcod.console_set_default_foreground(None, libtcod.grey)
	
	libtcod.console_print_ex (
		None, 79, 46, libtcod.BKGND_NONE, libtcod.RIGHT,
		'last frame : %3d ms (%3d fps)' %
		(
			int(libtcod.sys_get_last_frame_length() * 1000.0),
			libtcod.sys_get_fps()
		)
	)
	
	libtcod.console_print_ex (
		None, 79, 47, libtcod.BKGND_NONE, libtcod.RIGHT,
		'elapsed : %8d ms %4.2fs' %
		(
			libtcod.sys_elapsed_milli(),
			libtcod.sys_elapsed_seconds()
		)
	)
Example #12
0
    def find_path(self, start, end, tiles=[]):

        t0 = libtcod.sys_elapsed_seconds()
        if len(tiles) == 0:
            self.tiles = R.tiles
        else:
            self.tiles = tiles

        if len(tiles) <= 0 or start[0] < 0 or start[1] < 0 or end[0] < 0 or end[1] < 0:
            print "FALSE, no tiles, or start/end is out of bounds"
            return None

        if self.check_blocked(start) and self.check_blocked(end):
            print str(start[0]), "/", str(start[1]), "to" + str(end[0]), "/", str(end[1]) + " tile was not traversable."
            return None

        if self.tiles[start[0]][start[1]].continent != self.tiles[end[0]][end[1]].continent:
            print "not on same continent"
            return None

        self.open_list = []
        self.node_costs.clear()
        self.node_status.clear()
        self.largest_cost = 0

        self.end = end_node = PathNode(end, 0)
        self.start = start_node = PathNode(start, 0, endNode=end_node)
        # print "weeee let's go."
        self.add_node(start_node)

        while len(self.open_list) > 0:
            current_node = self.open_list[len(self.open_list) - 1]  #putting this to 0 is cool.
            #check to see if the end has been reached.
            if current_node.is_equal_to_node(end_node):# and current_node == self.open_list[0]: #why does this need to be 0?
                best_path = []
                while current_node != None:
                    best_path.insert(0, current_node.grid)
                    current_node = current_node.parent_node
                #return the path of grid points.
                t1 = libtcod.sys_elapsed_seconds()
                print "path of length: ", len(best_path), " succeeded in %s" % (t1 - t0), "explored ", \
                    len(self.node_costs.keys()), "and open nodes left: " + str(len(self.open_list))
                # print "open_list is " + str(len(self.open_list)) + " long"
                # print "path succeeded in %s" % (t1 - t0)
                t0 = t1
                return best_path
            else:
                if current_node.is_equal_to_node(end_node) and current_node != self.open_list[0]:
                    current_node = self.open_list[len(self.open_list) - 2]
                #do this
                if current_node.grid[0] < 0 or current_node.grid[1] < 0:
                    print "uh-oh somehow it's a minus!"
                    return None

                self.open_list.remove(current_node)
                self.node_status[current_node.grid] = self.CLOSED
                if self.node_costs[current_node.grid] != current_node.cost:
                    print "for some reason, the current grid costs don't match the dictionary.... fixing.."
                    self.node_costs[current_node.grid] = current_node.cost

                #                 if self.node_costs.has_key(current_node.grid):
                #                     #node_costs is used to track the nodes. so this NEEDS to be removed.
                #                     self.node_costs.pop(current_node.grid, 0)
                #                 else:
                #                     print "NODE WAS NOT IN NODE_COSTS"
                #                     continue

                for neighbour in self.find_adjacent_nodes_3(current_node, end_node):
                    if self.node_status.has_key(neighbour.grid):
                        if self.node_status[neighbour.grid] == self.CLOSED:
                            continue
                        if self.node_status[neighbour.grid] == self.OPEN or neighbour.cost < self.node_costs[
                            neighbour.grid]:
                            if neighbour.cost >= self.node_costs[neighbour.grid]:
                                continue
                            else:
                                self.replace_node(neighbour)
                    else:
                        self.add_node(neighbour)
                        if self.largest_cost < neighbour.cost:
                            self.largest_cost = neighbour.cost
                self.node_status[current_node.grid] = self.CLOSED
                # current_node = None

        print "failed", len(self.open_list), start[0], start[1], " --- > ", end[0], end[1]
        t1 = libtcod.sys_elapsed_seconds()
        print "path failed in %s" % (t1 - t0)
        t0 = t1
        return None
Example #13
0
	def add_rivers(self):
		print 'Adding rivers....'
		t0 = libtcod.sys_elapsed_seconds()
		libtcod.heightmap_rain_erosion(game.heightmap, int((game.WORLDMAP_WIDTH * game.WORLDMAP_HEIGHT) * 1.00), 0.06, 0.01, self.rnd)
		t1 = libtcod.sys_elapsed_seconds()
		print '    done! (%.3f seconds)' % (t1 - t0)