def structure(self): if dry_run: self.U = Input('U', input_dim) self.X = Layer('X', 3, self.U, rect, 1.0) else: self.U = Input('U', input_dim) self.X1 = Layer('X1', 100, self.U, lambda x: rect(x), min_tau=0., stiffx=1.0)
def structure(self): if dry_run: self.U = Input('U', input_dim) self.Y = Input('Y', label_dim) RegressionLayer('Z', 3, (self.U, self.Y), rect, min_tau=0.0, stiffx=1.0, merge_op=lambda u, y: rect(u + y)), else: self.U = Input('U', input_dim) self.Y = Input('Y', label_dim) RegressionLayer('Z', 100, (self.U, self.Y), rect, min_tau=0.0, stiffx=1.0, merge_op=lambda u, y: u)
def get_location_near_stairs(self): # Find stair location stair_location = [e for e in self.children if e.isinstance('Stairs') and e.name == 'Up'][0].position # Find open areas around stairs rect = utils.rect(stair_location[0] - 3, stair_location[1] - 3, 7, 7) filled_location = [e.position for e in self.children if hasattr(e, 'position')] possible_locations = [] for point in rect: if point not in self.level.data: continue ch, fg, bg = self.level.data.get_char(*point) if ch == ord('.'): possible_locations.append(point) possible_locations = list(set(possible_locations).difference(set(filled_location))) if not possible_locations: raise RuntimeError('Unable to find empty space around stairs up') pos = possible_locations[random.randint(0, len(possible_locations) - 1)] return pos
def generate_level(width, height, player_count): new_entities = [] new_level = level.Level(0, 0, width, height) new_level.data.draw_str(0, 0, 'X' * width * height) TOP = 0b0001 RIGHT = 0b0010 BOTTOM = 0b0100 LEFT = 0b1000 MASK = 0b1111 STAIRDOWN = 0b010000 STAIRUP = 0b100000 floor = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0] cursor = random.randint(0, 2) floor[cursor] |= STAIRUP def up(): nonlocal floor nonlocal cursor if cursor >= 3: floor[cursor] |= TOP cursor -= 3 floor[cursor] |= BOTTOM return cursor return None def down(): nonlocal floor nonlocal cursor if cursor <= 5: floor[cursor] |= BOTTOM cursor += 3 floor[cursor] |= TOP return cursor return None def right(): nonlocal floor nonlocal cursor if cursor not in [2, 5, 8]: floor[cursor] |= RIGHT cursor += 1 floor[cursor] |= LEFT return cursor return None def left(): nonlocal floor nonlocal cursor if cursor not in [0, 3, 6]: floor[cursor] |= LEFT cursor -= 1 floor[cursor] |= RIGHT return cursor return None possible_moves = [up, down, left, right] moves = random.randint(8, 20) while moves > 0: current_cursor = random.choice(possible_moves)() if current_cursor: moves -= 1 floor[cursor] |= STAIRDOWN # Build out floor for i, m in enumerate(floor): x = i % 3 * 9 + 1 y = i // 3 * 7 current_room = tdl.Console(9, 7) templates = room_templates[m & MASK] room = templates[random.randint(0, len(templates) - 1)] current_room.draw_str(0, 0, room) new_level.data.blit(current_room, x, y) if m & STAIRUP: rect = utils.rect(x, y, 9, 7) potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.Stairs(position=coord) new_entities.append(ent) if m & STAIRDOWN: rect = utils.rect(x, y, 9, 7) potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.StairsDown(position=coord) new_entities.append(ent) color_table = { '#': palette.WHITE, 'X': palette.get_nearest((171, 82, 54)), '.': palette.BRIGHT_BLACK } #temp_level = new_level.data.__copy__() # Colorize tiles for x, y in new_level.data: char, fg, bg = new_level.data.get_char(x, y) if chr(char) == '+': char = ord('.') coords = helpers.DirectionHelper.directions coords = [utils.math.add(c, (x, y)) for c in coords] chars = [ new_level.data.get_char(*c)[0] for c in coords if c in new_level.data ] if ord('+') not in chars: d = door.Door(position=(x, y)) new_entities.append(d) fg = color_table.get(chr(char)) if not fg: fg = palette.WHITE new_level.data.draw_char(x, y, char, fg, bg) # Placing Entities for (x, y) in new_level.data: ch, fg, bg = new_level.data.get_char(x, y) if ch == ord('.'): if random.random() < 1 / 25 * max(1.0, player_count / 3): MonsterClass = registry.Registry.get('monster') mon = MonsterClass(position=(x, y)) new_entities.append(mon) elif random.random() < 1 / 80: WeaponClass = registry.Registry.get('weapon') weapon = WeaponClass(position=(x, y)) new_entities.append(weapon) elif random.random() < 1 / 80: ItemClass = registry.Registry.get('item') item = ItemClass(position=(x, y)) new_entities.append(item) return new_level, new_entities
def generate_level(width, height, player_count, scene_info): new_entities = [] new_level = level.Level(0, 0, width, height) new_level.data.draw_str(0, 0, 'X' * width * height) TOP = 0b0001 RIGHT = 0b0010 BOTTOM = 0b0100 LEFT = 0b1000 MASK = 0b1111 STAIRDOWN = 0b010000 STAIRUP = 0b100000 floor = [0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0] cursor = random.randint(0, 2) start_cursor = cursor floor[cursor] |= STAIRUP def up(): nonlocal floor nonlocal cursor if cursor >= 3: floor[cursor] |= TOP cursor -= 3 floor[cursor] |= BOTTOM return cursor return None def down(): nonlocal floor nonlocal cursor if cursor <= 5: floor[cursor] |= BOTTOM cursor += 3 floor[cursor] |= TOP return cursor return None def right(): nonlocal floor nonlocal cursor if cursor not in [2, 5, 8]: floor[cursor] |= RIGHT cursor += 1 floor[cursor] |= LEFT return cursor return None def left(): nonlocal floor nonlocal cursor if cursor not in [0, 3, 6]: floor[cursor] |= LEFT cursor -= 1 floor[cursor] |= RIGHT return cursor return None possible_moves = [up, down, left, right] moves = random.randint(8, 20) while moves > 0: current_cursor = random.choice(possible_moves)() if current_cursor and current_cursor != start_cursor: moves -= 1 floor[cursor] |= STAIRDOWN dont_place_monsters_here = [] # Build out floor for i, m in enumerate(floor): x = i % 3 * 9 + 1 y = i // 3 * 7 current_room = tdl.Console(9, 7) templates = room_templates[m & MASK] room = templates[random.randint(0, len(templates) - 1)] current_room.draw_str(0, 0, room) new_level.data.blit(current_room, x, y) if m & STAIRUP: rect = utils.rect(x, y, 9, 7) dont_place_monsters_here = rect potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.Stairs(position=coord) new_entities.append(ent) if m & STAIRDOWN: rect = utils.rect(x, y, 9, 7) potential_coords = [] for point in rect: ch, fg, bg = new_level.data.get_char(*point) if ch == ord('.'): potential_coords.append(point) coord = potential_coords[random.randint(0, len(potential_coords) - 1)] ent = stairs.StairsDown(position=coord) new_entities.append(ent) color_table = { '#': palette.WHITE, 'X': palette.get_nearest((171, 82, 54)), '.': palette.BRIGHT_BLACK } # Colorize tiles for x, y in new_level.data: char, fg, bg = new_level.data.get_char(x, y) if chr(char) == '+': char = ord('.') coords = helpers.DirectionHelper.directions coords = [utils.math.add(c, (x, y)) for c in coords] chars = [ new_level.data.get_char(*c)[0] for c in coords if c in new_level.data ] if ord('+') not in chars: d = door.Door(position=(x, y)) new_entities.append(d) fg = color_table.get(chr(char)) if not fg: fg = palette.WHITE new_level.data.draw_char(x, y, char, fg, bg) level_scaling_factor = (scene_info['level'] / 10) + 1 monster_spawn_rate = 1 / 16 * max( 1.0, player_count / 3) #* level_scaling_factor uncommon_monster_class = registry.Registry.get('uncommon_monster') common_monster_class = registry.Registry.get('common_monster') uncommon_monster_statuses = [] common_monster_statuses = [] if random.random() < 1 / 2: uncommon_monster_statuses = list( set([ registry.Registry.get('statuses_drop_table') for _ in range(random.randint(0, 2)) ])) uncommon_monster_statuses = [_ for _ in uncommon_monster_statuses if _] if random.random() < 1 / 2: common_monster_statuses = list( set([ registry.Registry.get('statuses_drop_table') for _ in range(random.randint(0, 2)) ])) common_monster_statuses = [_ for _ in common_monster_statuses if _] registry.Registry.clear('monster_drop_table') registry.Registry.register(uncommon_monster_class, 'monster_drop_table', 3) registry.Registry.register(common_monster_class, 'monster_drop_table', 4) scene_info['enemies'] = [] scene_info['enemies'].append( (uncommon_monster_class, uncommon_monster_statuses)) scene_info['enemies'].append( (common_monster_class, common_monster_statuses)) # Placing Entities for (x, y) in new_level.data: ch, fg, bg = new_level.data.get_char(x, y) if ch == ord('.'): if random.random() < monster_spawn_rate and ( x, y) not in dont_place_monsters_here: MonsterClass = registry.Registry.get('monster_drop_table') mon = MonsterClass(position=(x, y)) if MonsterClass is uncommon_monster_class: for status in uncommon_monster_statuses: s = status(mon) mon.add_status(s) else: for status in common_monster_statuses: s = status(mon) mon.add_status(s) new_entities.append(mon) elif random.random() < 1 / 80: WeaponClass = registry.Registry.get('weapon') weapon = WeaponClass(position=(x, y)) new_entities.append(weapon) elif random.random() < 1 / 80: ItemClass = registry.Registry.get('item') item = ItemClass(position=(x, y)) new_entities.append(item) return new_level, new_entities, dont_place_monsters_here
def op(self, value): self.ox, self.oy = rect(value)
def yp(self, value): self.yx, self.yy = rect(value)
def xp(self, value): self.xx, self.xy = rect(value)