def test_intersect_of_non_overlapping_rects(self): r1 = Rect(10, 20, 20, 30) r2 = Rect(10, 30, 20, 40) # abuts the bottom of r1 without intersecting r3 = Rect(20, 20, 30, 40) # abuts the right of r1 without intersecting self.assertIsNone(r1.intersect(r2)) self.assertIsNone(r2.intersect(r1)) self.assertIsNone(r1.intersect(r3)) self.assertIsNone(r3.intersect(r1))
def test_rect_intersect(): R1 = Rect(-1, -1, 2, 2) R2 = Rect(0, 0, 2, 2) assert R1.intersect(R2) == Rect(0, 0, 1, 1) R1 = Rect(-1, -1, 2, 2) R2 = Rect(0, 0, 1, 1) assert R1.intersect(R2) == Rect(0, 0, 1, 1) R1 = Rect(-3, -3, 2, 2) R2 = Rect(0, 0, 2, 2) assert R1.intersect(R2) is None
def aggro_check(self, size): aggro_range = self.map.tile_width * size offset = (self.map.tile_width * (size / 2)) collided = [] for player in self.player_characters: rect = Rect(player.x - offset, player.y - offset, aggro_range, aggro_range) collided += [e for e in self.entities if rect.intersect(Rect(*e.pos + e.size)) and e.data.type == 'enemy'] return collided
def generate_map(self, height, width): """Initialize the map""" self.height = height self.width = width #Set map to all empty tiles self.floor = [[0 for y1 in range(height)] for x1 in range(width)] for x in range(self.width): for y in range(self.height): self.floor[x][y] = Tile.get_new_tile() # Create some rooms rooms = [] num_rooms = 0 for r in range(settings.MAX_ROOMS): # random width and height w = random.randint(settings.ROOM_MIN_SIZE, settings.ROOM_MAX_SIZE) h = random.randint(settings.ROOM_MIN_SIZE, settings.ROOM_MAX_SIZE) # random position without going out of the boundaries of the map x = random.randint(0, self.width - w - 1) y = random.randint(0, self.height - h - 1) new_room = Rect(x, y, w, h) # run through the other rooms and see if they intersect with this one failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: # this means there are no intersections, so this room is valid # "paint" it to the map's tiles self.create_room(new_room) # center coordinates of new room, will be useful later (new_x, new_y) = new_room.center() if not num_rooms == 0: # all rooms after the first: # connect it to the previous room with a tunnel # center coordinates of previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() # draw a coin (random number that is either 0 or 1) if random.randint(0, 1) == 1: # first move horizontally, then vertically self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: # first move vertically, then horizontally self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) # finally, append the new room to the list rooms.append(new_room) num_rooms += 1
def make_map(): global my_map, objects objects = [player] rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): w = randint(ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = randint(ROOM_MIN_SIZE, ROOM_MAX_SIZE) #assign random positions without going out of bounds x = randint(0, MAP_WIDTH-w-1) y = randint(0, MAP_HEIGHT-h-1) new_room = Rect(x, y, w, h) #run through other rooms and see if they intersect with this one failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: create_room(new_room) #center_coords of new room, will be useful later (new_x, new_y) = new_room.center() if num_rooms == 0: #this is the player's starting room player.x = new_x player.y = new_y else: #connect to the previous room with a tunnel #center coords of previous room (prev_x, prev_y) = rooms[num_rooms-1].center() #flip a coin if randint(0, 1): #first move horizontally, then veritcally create_h_tunnel(prev_x, new_x, prev_y) create_v_tunnel(prev_y, new_y, new_x) else: # first move horizontally, then vertically create_v_tunnel(prev_y, new_y, new_x) create_h_tunnel(prev_x, new_x, prev_y) #append the new room to the list place_objects(new_room) rooms.append(new_room) num_rooms += 1
def aggro_check(self, size): aggro_range = self.map.tile_width * size offset = (self.map.tile_width * (size / 2)) collided = [] for player in self.player_characters: rect = Rect(player.x - offset, player.y - offset, aggro_range, aggro_range) collided += [ e for e in self.entities if rect.intersect(Rect(*e.pos + e.size)) and e.data.type == 'enemy' ] return collided
def generate_dungeon(game_map, max_rooms, room_min_size, room_max_size, map_width, map_height, player, entities, min_entities_per_room, max_entities_per_room, max_items_per_room): rooms = [] num_rooms = 0 last_room_center_x = None last_room_center_y = None for r in range(max_rooms): w = randint(room_min_size, room_max_size) h = randint(room_min_size, room_max_size) x = randint(0, map_width - w - 1) y = randint(0, map_height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: # new room does not intersect any other rooms create_room(game_map, new_room) (new_x, new_y) = new_room.center() last_room_center_x = new_x last_room_center_y = new_y if num_rooms == 0: player.x = new_x player.y = new_y else: (prev_x, prev_y) = rooms[num_rooms - 1].center() create_corner_tunnel(game_map, prev_x, prev_y, new_x, new_y) rooms.append(new_room) num_rooms += 1 stairs = Stairs(game_map.dungeon_level + 1) stairs_entity = Entity(last_room_center_x, last_room_center_y, '>', tcod.white, 'Stairs', render_order=RenderOrder.STAIRS, structure=stairs) entities.append(stairs_entity)
def main(): dungeon_map = [['_' for x in xrange(MAP_HEIGHT)] for x in xrange(MAP_WIDTH)] rooms = [] num_rooms = 0 for r in xrange(MAX_ROOMS): width = random.randrange(ROOM_MIN_SIZE, ROOM_MAX_SIZE) height = random.randrange(ROOM_MIN_SIZE, ROOM_MAX_SIZE) xcorner = random.randrange(0, MAP_WIDTH - width - 1) ycorner = random.randrange(0, MAP_HEIGHT - height - 1) new_room = Rect(xcorner, ycorner, width, height) # new_room.print_rect() if num_rooms > 0: for other_room in rooms: if new_room.intersect(other_room): break dungeon_map = create_room(new_room, dungeon_map) (new_x, new_y) = new_room.center() (prev_x, prev_y) = rooms[num_rooms-1].center() if random.randrange(0, 1) == 1: #first move horizontally, then vertically dungeon_map = create_h_tunnel(prev_x, new_x, prev_y, dungeon_map) dungeon_map = create_v_tunnel(prev_y, new_y, new_x, dungeon_map) else: #first move vertically, then horizontally dungeon_map = create_v_tunnel(prev_y, new_y, prev_x, dungeon_map) dungeon_map = create_h_tunnel(prev_x, new_x, new_y, dungeon_map) rooms.append(new_room) num_rooms = num_rooms + 1 else: dungeon_map = create_room(new_room, dungeon_map) rooms.append(new_room) num_rooms = num_rooms + 1 print_dungeon_map(dungeon_map)
def generate_map(self, width, height, room_min_size, room_max_size, max_rooms, player): new_map = self._generate_empty_map(width, height) rooms = [] num_rooms = 0 for r in range(max_rooms): w = libtcod.random_get_int(0, room_min_size, room_max_size) h = libtcod.random_get_int(0, room_min_size, room_max_size) x = libtcod.random_get_int(0, 0, width - w - 1) y = libtcod.random_get_int(0, 0, height - h - 1) new_room = Rect(x, y, w, h) failed = False for old_room in rooms: if new_room.intersect(old_room): failed = True break if not failed: self._create_room(new_room, new_map) (new_x, new_y) = new_room.center() if num_rooms == 0: player.x = new_x player.y = new_y else: # Dig the tunnels (prev_x, prev_y) = rooms[num_rooms - 1].center() # Flip a coin to see if we go horizontally then vertically, # or vice-versa if libtcod.random_get_int(0, 0, 1) == 1: self._create_h_tunnel(prev_x, new_x, prev_y, new_map) self._create_v_tunnel(new_x, prev_y, new_y, new_map) else: self._create_v_tunnel(prev_x, prev_y, new_y, new_map) self._create_h_tunnel(prev_x, new_x, new_y, new_map) rooms.append(new_room) num_rooms += 1 return new_map
def make_map(self): self.map = [[Tile(True) for y in range(self.height)] for x in range(self.width)] rooms = [] num_rooms = 0 self.monster_chances = enemy_decoder.decode_all_spawn_chances() for chance in self.monster_chances: self.monster_chances[chance] = from_dungeon_level( self.monster_chances[chance]) self.item_chances = item_decoder.decode_all_spawn_chances() for chance in self.item_chances: self.item_chances[chance] = from_dungeon_level( self.item_chances[chance]) self.equipment_chances = equipment_decoder.decode_all_spawn_chances() for chance in self.equipment_chances: self.equipment_chances[chance] = from_dungeon_level( self.equipment_chances[chance]) # print self.monster_chances # print self.item_chances # print self.equipment_chances for r in range(Map.MAX_ROOMS): #make a random room w = libtcod.random_get_int(0, Map.ROOM_MIN_SIZE, Map.ROOM_MAX_SIZE) h = libtcod.random_get_int(0, Map.ROOM_MIN_SIZE, Map.ROOM_MAX_SIZE) x = libtcod.random_get_int(0, 0, self.width - w - 1) y = libtcod.random_get_int(0, 0, self.height - h - 1) new_room = Rect(x, y, w, h) #make sure it doesn't intersect other rooms failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: self.create_room(new_room) self.place_objects(new_room) (new_x, new_y) = new_room.center() if num_rooms == 0: self.origin = (new_x, new_y) else: #connect our room to the previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() if libtcod.random_get_int(0, 0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) rooms.append(new_room) num_rooms += 1 self.stairs = Object(new_x, new_y, '<', 'stairs', libtcod.white, always_visible=True) self.objects.append(self.stairs) self.send_to_back(self.stairs)
def setup_map(self): #Create the map, add objects and monsters, and save it all for later use rooms = [] num_rooms = 0 for r in range(self.max_rooms): #Generate a random width and height for each room w = libtcod.random_get_int(0, self.min_room_size, self.max_room_size) h = libtcod.random_get_int(0, self.min_room_size, self.max_room_size) #Generate a random map position, inside the bounds of the map x = libtcod.random_get_int(0, 0, self.width - w - 1) y = libtcod.random_get_int(0, 0, self.height - h - 1) #Create a new room from the random numbers new_room = Rect(x, y, w, h) #Run through all the other rooms and see if they intersect with this one failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: #If we've gotten here, the room is valid, and does not intersect any other rooms #Carve the room into the maps tiles self.create_room(new_room) #Get the center coordinates for the new room (new_x, new_y) = new_room.center() if num_rooms == 0: #This is the first room created, so start the player off in the center player_start_x = new_x player_start_y = new_y else: #This is not the first room, so connect it to the previous room via tunnels #add some contents to this room, such as monsters, objects etc. We never add creatures to the #starting room self.place_objects(new_room) #Get the center coordinates for the previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() #Flip a coin to see if we move horizontally, or vertically first if libtcod.random_get_int(0, 0, 1) == 1: #Tunnel horizontally first, then vertically self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: #Tunnel vertically first, then horizontally self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) #Finally, append the newly added room to the rooms list rooms.append(new_room) num_rooms += 1 #Create the stairs in the last room to be created stairs_down = Object(new_x, new_y, '>', 'stairs down', libtcod.white) self.objects.append(stairs_down) return self.map, self.objects, player_start_x, player_start_y, stairs_down
def test_intersect_by_crossing(self): r1 = Rect(20, 0, 20, 30) r2 = Rect(10, 10, 30, 20) intersection = Rect(20, 10, 20, 20) self.assertEqual(intersection, r1.intersect(r2)) self.assertEqual(intersection, r2.intersect(r1))
def test_intersect_by_partial_intersection(self): r1 = Rect(10, 10, 30, 30) r2 = Rect(20, 20, 40, 40) intersection = Rect(20, 20, 30, 30) self.assertEqual(intersection, r1.intersect(r2)) self.assertEqual(intersection, r2.intersect(r1))
def test_intersect_by_full_containment(self): r1 = Rect(10, 10, 25, 25) r2 = Rect(15, 15, 20, 20) # fully contained within r1 self.assertEqual(r2, r1.intersect(r2)) self.assertEqual(r2, r2.intersect(r1))
def make_map(): global map global player_start_x global player_start_y #Fill map with blocked tiles, this will allow us to 'carve' rooms for the player #to explore map = [[Tile(True) for y in range(MAP_HEIGHT) ] for x in range(MAP_WIDTH) ] rooms = [] num_rooms = 0 for r in range(MAX_ROOMS): #Generate a random width and height for each room w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #Generate a random map position, inside the bounds of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h -1) #Create a new room from the random numbers new_room = Rect(x, y, w, h) #Run through all the other rooms and see if they intersect with this one failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: #If we've gotten here, the room is valid, and does not intersect any other rooms #Carve the room into the maps tiles create_room(new_room) #Get the center coordinates for the new room (new_x, new_y) = new_room.center() if num_rooms == 0: #This is the first room created, so start the player off in the center player_start_x = new_x player_start_y = new_y else: #This is not the first room, so connect it to the previous room via tunnels #add some contents to this room, such as monsters, objects etc. We never add creatures to the #starting room place_objects(new_room) #Get the center coordinates for the previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() #Flip a coin to see if we move horizontally, or vertically first if libtcod.random_get_int(0, 0, 1) == 1: #Tunnel horizontally first, then vertically create_h_tunnel(prev_x, new_x, prev_y) create_v_tunnel(prev_y, new_y, new_x) else: #Tunnel vertically first, then horizontally create_v_tunnel(prev_y, new_y, prev_x) create_h_tunnel(prev_x, new_x, new_y) #Finally, append the newly added room to the rooms list rooms.append(new_room) num_rooms += 1
def setup_map(self): #Create the map, add objects and monsters, and save it all for later use rooms = [] num_rooms = 0 for r in range(self.max_rooms): #Generate a random width and height for each room w = libtcod.random_get_int(0, self.min_room_size, self.max_room_size) h = libtcod.random_get_int(0, self.min_room_size, self.max_room_size) #Generate a random map position, inside the bounds of the map x = libtcod.random_get_int(0, 0, self.width - w - 1) y = libtcod.random_get_int(0, 0, self.height - h -1) #Create a new room from the random numbers new_room = Rect(x, y, w, h) #Run through all the other rooms and see if they intersect with this one failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: #If we've gotten here, the room is valid, and does not intersect any other rooms #Carve the room into the maps tiles self.create_room(new_room) #Get the center coordinates for the new room (new_x, new_y) = new_room.center() if num_rooms == 0: #This is the first room created, so start the player off in the center player_start_x = new_x player_start_y = new_y else: #This is not the first room, so connect it to the previous room via tunnels #add some contents to this room, such as monsters, objects etc. We never add creatures to the #starting room self.place_objects(new_room) #Get the center coordinates for the previous room (prev_x, prev_y) = rooms[num_rooms - 1].center() #Flip a coin to see if we move horizontally, or vertically first if libtcod.random_get_int(0, 0, 1) == 1: #Tunnel horizontally first, then vertically self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: #Tunnel vertically first, then horizontally self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) #Finally, append the newly added room to the rooms list rooms.append(new_room) num_rooms += 1 #Create the stairs in the last room to be created stairs_down = Object(new_x, new_y, '>', 'stairs down', libtcod.white) self.objects.append(stairs_down) return self.map, self.objects, player_start_x, player_start_y, stairs_down
def make_map(self, player, entities): start_time = time.time() rooms = [] num_rooms = 0 center_of_last_room_x = None center_of_last_room_y = None # Essentially, this algorithm generates a random room, and checks to see if it intesects with the generated map (if it does, it breaks) # If not, it generates the room, and using the center of that room, connects that room with a tunnel to the previously generated room # if it's the first room, it plops the player in the middle of it # Then fill it with stuff and keep going for r in range(game_constants.max_rooms): w = randint(game_constants.room_min_size, game_constants.room_max_size) h = randint(game_constants.room_min_size, game_constants.room_max_size) x = randint(0, game_constants.map_width - w - 1) y = randint(0, game_constants.map_height - h - 1) new_room = Rect(x, y, w, h) for other_room in rooms: if new_room.intersect(other_room): break else: self.create_room(new_room) (new_x, new_y) = new_room.center() center_of_last_room_x = new_x center_of_last_room_y = new_y if num_rooms == 0: player.x = new_x player.y = new_y libtcod.mouse_move(player.x, player.y) else: (prev_x, prev_y) = rooms[num_rooms - 1].center() if randint(0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) self.populate_room(new_room, entities) rooms.append(new_room) num_rooms += 1 # Once all of the rooms have been generated, plot the stairs in the middle of the last room created stairs_components = {"Stairs": Stairs(self.dungeon_level + 1)} stairs_entity = Entity(center_of_last_room_x, center_of_last_room_y, ">", libtcod.white, "Stairs Down", blocks=True, render_order=RenderOrder.ACTOR, message_log=self.log, state=AIStates.INANIMATE, components=stairs_components) self.tiles[center_of_last_room_x][center_of_last_room_y].add_entity( stairs_entity) entities.insert_entity(stairs_entity) self.compute_dijkstra_map([stairs_entity], "stairs") print("Map generated in {} seconds".format(time.time() - start_time))
def make_map(self): self.map = [[ Tile(True) for y in range(self.height) ] for x in range(self.width) ] rooms = [] num_rooms = 0 self.monster_chances = enemy_decoder.decode_all_spawn_chances() for chance in self.monster_chances: self.monster_chances[chance] = from_dungeon_level(self.monster_chances[chance]) self.item_chances = item_decoder.decode_all_spawn_chances() for chance in self.item_chances: self.item_chances[chance] = from_dungeon_level(self.item_chances[chance]) self.equipment_chances = equipment_decoder.decode_all_spawn_chances() for chance in self.equipment_chances: self.equipment_chances[chance] = from_dungeon_level(self.equipment_chances[chance]) # print self.monster_chances # print self.item_chances # print self.equipment_chances for r in range(Map.MAX_ROOMS): #make a random room w = libtcod.random_get_int(0, Map.ROOM_MIN_SIZE, Map.ROOM_MAX_SIZE) h = libtcod.random_get_int(0, Map.ROOM_MIN_SIZE, Map.ROOM_MAX_SIZE) x = libtcod.random_get_int(0, 0, self.width - w - 1) y = libtcod.random_get_int(0, 0, self.height - h - 1) new_room = Rect(x, y, w, h) #make sure it doesn't intersect other rooms failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: self.create_room(new_room) self.place_objects(new_room) (new_x, new_y) = new_room.center() if num_rooms == 0: self.origin = (new_x, new_y) else: #connect our room to the previous room (prev_x, prev_y) = rooms[num_rooms-1].center() if libtcod.random_get_int(0, 0, 1) == 1: self.create_h_tunnel(prev_x, new_x, prev_y) self.create_v_tunnel(prev_y, new_y, new_x) else: self.create_v_tunnel(prev_y, new_y, prev_x) self.create_h_tunnel(prev_x, new_x, new_y) rooms.append(new_room) num_rooms+= 1 self.stairs = Object(new_x, new_y, '<', 'stairs', libtcod.white, always_visible=True) self.objects.append(self.stairs) self.send_to_back(self.stairs)
def make_map(): num_rooms = 0 player = {'x' : 0, 'y' : 0} dungeon_map = [[], []] for r in xrange(MAX_ROOMS): #random width and height w = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) h = libtcod.random_get_int(0, ROOM_MIN_SIZE, ROOM_MAX_SIZE) #random position without going out of the boundaries of the map x = libtcod.random_get_int(0, 0, MAP_WIDTH - w - 1) y = libtcod.random_get_int(0, 0, MAP_HEIGHT - h - 1) #"Rect" class makes rectangles easier to work with new_room = Rect(x, y, w, h) #run through the other rooms and see if they intersect with this one failed = False for other_room in rooms: if new_room.intersect(other_room): failed = True break if not failed: #this means there are no intersections, so this room is valid #"paint" it to the map's tiles dungeon_map = create_room(new_room, dungeon_map) #center coordinates of new room, will be useful later (new_x, new_y) = new_room.center() #optional: print "room number" to see how the map drawing worked # we may have more than ten rooms, so print 'A' for the first room, 'B' for the next... room_no = Object(new_x, new_y, chr(65+num_rooms), libtcod.white) objects.insert(0, room_no) #draw early, so monsters are drawn on top` if num_rooms == 0: #this is the first room, where the player starts at player.x = new_x player.y = new_y else: #all rooms after the first: #connect it to the previous room with a tunnel #center coordinates of previous room (prev_x, prev_y) = rooms[num_rooms-1].center() #draw a coin (random number that is either 0 or 1) if libtcod.random_get_int(0, 0, 1) == 1: #first move horizontally, then vertically dungeon_map = create_h_tunnel(prev_x, new_x, prev_y, dungeon_map) dungeon_map = create_v_tunnel(prev_y, new_y, new_x, dungeon_map) else: #first move vertically, then horizontally dungeon_map = create_v_tunnel(prev_y, new_y, prev_x, dungeon_map) dungeon_map = create_h_tunnel(prev_x, new_x, new_y, dungeon_map) #finally, append the new room to the list rooms.append(new_room) num_rooms += 1
def test_intersect_of_rect_with_itself(self): r1 = Rect(10, 20, 20, 30) self.assertEqual(r1, r1, msg="Sanity check") self.assertEqual(r1, r1.intersect(r1))