def set_tile_values(self, default, x, y, type=None, reset=True): if reset: self.tile[x][y] = {} if type is not None: if type == 'trap': temp_tile = game.tiles.find_trap() else: temp_tile = game.tiles.get_tile_from_type(default, type) else: temp_tile = game.tiles.get_tile(default) self.tile[x][y].update({'icon': temp_tile.icon, 'name': temp_tile.name, 'type': temp_tile.type, 'article': temp_tile.article}) self.tile[x][y].update({'color': temp_tile.color, 'dark_color': temp_tile.dark_color}) self.tile[x][y].update({'dark_back_color': temp_tile.dark_back_color}) if temp_tile.blocked: self.tile[x][y].update({'blocked': True}) if temp_tile.block_sight: self.tile[x][y].update({'block_sight': True}) if temp_tile.flags: for i in temp_tile.flags: self.tile[x][y].update({i: True}) back_lerp = round(libtcod.random_get_float(game.rnd, 0, 1), 1) back_color = libtcod.color_lerp(temp_tile.back_color_high, temp_tile.back_color_low, back_lerp) if temp_tile.color_low != libtcod.black: fore_lerp = round(libtcod.random_get_float(game.rnd, 0, 1), 1) fore_color = libtcod.color_lerp(temp_tile.color, temp_tile.color_low, fore_lerp) else: fore_color = temp_tile.color if self.tile_is_animated(x, y): self.tile[x][y].update({'back_light_color': temp_tile.back_color_high, 'back_dark_color': temp_tile.back_color_low, 'lerp': back_lerp}) else: self.tile[x][y].update({'color': fore_color, 'back_light_color': back_color, 'back_dark_color': libtcod.color_lerp(libtcod.black, back_color, 0.2), 'lerp': back_lerp})
def renderPlayerOverlay(self, frame, player, cell): visible = libtcod.map_is_in_fov(self.map, cell.x, cell.y) if visible: cell.discovered = True intensity = self.calculateIntensity(player, cell.x, cell.y) if cell.passable: color = libtcod.color_lerp(self.c_lightOpen, self.c_torch, self.a_torch) else: color = libtcod.color_lerp(self.c_lightWall, self.c_torch, self.a_torch) else: intensity = 1 if not cell.discovered: color = libtcod.black else: if cell.passable: color = self.c_darkOpen else: color = self.c_darkWall rgb = 255 - ((cell.y * 255) / self.height) overlayColor = libtcod.Color(rgb, rgb, rgb) color = overlayColor * color frame.setBgColor(cell.x, cell.y - self.yOffset, color, libtcod.BKGND_ALPHA(intensity)) y = player.y - self.yOffset x = player.x frame.putChar(x, y, '@', b'white')
def generate_land(self): for x in range(self.width): for y in range(self.height): f = [self.noise_zoom * x / self.width, self.noise_zoom * y / self.height] value = libtcod.noise_get_fbm(self.world_noise, f, self.noise_octaves, libtcod.NOISE_PERLIN) #Threshold c = ((value + 1.0) / 2.0) col = libtcod.color_lerp(libtcod.dark_green, libtcod.black, c) self.map_list[x][y].land_type = 'grass' c = int((value + 1.0) / 2.0 * 200) c = c - int(self.dist(x,y))/1.5 #This is a beach if c < 28: col = libtcod.lightest_amber self.map_list[x][y].land_type = 'beach' #This is water if c < 20: coef = self.dist(x,y) / 320 col = libtcod.color_lerp(libtcod.azure, libtcod.darkest_azure * (c/2), coef) if c > 5: self.map_list[x][y].land_type = 'shallow_water' else: self.map_list[x][y].land_type = 'deep_water' libtcod.map_set_properties(self.deepsea_map, x, y, False, True) if self.map_list[x][y].land_type == 'grass' or self.map_list[x][y].land_type == 'beach': libtcod.map_set_properties(self.ground_map, x, y, False, True) self.map_list[x][y].color = col libtcod.image_put_pixel(self.world_img, x, y, self.map_list[x][y].color)
def end_struct(self, struct, name): self.temp_tile.dark_color = libtcod.color_lerp(libtcod.black, self.temp_tile.color, 0.2) self.temp_tile.dark_back_color = libtcod.color_lerp(libtcod.black, self.temp_tile.back_color_low, 0.2) if self.temp_tile.type == 'trees1': self.temp_tile.icon = chr(5) if self.temp_tile.type == 'trees2': self.temp_tile.icon = chr(6) if self.temp_tile.name == 'stalagmite': self.temp_tile.icon = chr(24) if self.temp_tile.type == 'chest': self.temp_tile.icon = chr(20) game.tiles.add_to_list(self.temp_tile) return True
def draw(self, in_sight, area): if in_sight: lt.console_set_char_foreground(area.con, self.row, self.col, self.f_color) lt.console_set_char_background(area.con, self.row, self.col, self.b_color) lt.console_set_char(area.con, self.row, self.col, self.char) self.explored = True elif self.explored: f_color = lt.color_lerp(area.fog, self.f_color, area.fog_density) b_color = lt.color_lerp(area.fog, self.b_color, area.fog_density) lt.console_set_char_background(area.con, self.row, self.col, b_color) lt.console_set_default_foreground(area.con, f_color) lt.console_put_char(area.con, self.row, self.col, self.char) else: lt.console_set_char_background(area.con, self.row, self.col, area.fog)
def render_tiles_animations(x, y, icon, light, dark, lerp): if libtcod.random_get_int(game.rnd, 1, 70) == 70: if libtcod.random_get_int(game.rnd, 0, 1) == 0: if lerp >= 0.1: lerp -= 0.1 else: lerp += 0.1 else: if lerp < 1.0: lerp += 0.1 else: lerp -= 0.1 front = libtcod.color_lerp(light, icon, lerp) back = libtcod.color_lerp(dark, light, lerp) return front, back, lerp
def take_behavior_action(self): battle = combat.WorldBattle(date=g.WORLD.time_cycle.get_current_date(), location=(self.figure.wx, self.figure.wy), faction1_named=[self.figure], faction1_populations=[], faction2_named=[self.target], faction2_populations=[]) g.game.add_message(battle.describe(), libtcod.color_lerp(g.PANEL_FRONT, self.figure.color, .3)) self.has_attempted_kill = 1
def update(self): super(Recalling, self).update() if self.duration > 0: self.entity.next_action = 100 tile = self.entity.bg.tiles[(self.entity.x, self.entity.y)] self.entity.color = libtcod.color_lerp(tile.bg_color, self.color, 1 - (self.duration / 10.0))
def create_smoke(pos, color=tcod.gray, age=0, grow=0.1, decay=0.1, direction=-1, speed=0.3, max_opacity=.75, interp_wind=True): _intensity = random.uniform(max_opacity*.25, max_opacity) _color = tcod.color_lerp(color, tcod.white, random.uniform(0, 0.3)) if direction == -1: _velocity = [random.uniform(-speed, speed), random.uniform(-speed, speed), 0] else: _velocity = numbers.velocity(direction, speed) _effect = {'type': 'smoke', 'color': _color, 'intensity': _intensity*age, 'max_intensity': _intensity, 'decay': decay, 'grow': grow, 'disappear': False, 'pos': list(pos), 'float_pos': list(pos), 'interp_wind': interp_wind, 'velocity': [_velocity[0], _velocity[1], _velocity[2]], 'callback': process_smoke, 'draw_callback': draw_smoke, 'unregister_callback': clear_effect} register_effect(_effect)
def lerp_colors(colors): final_color = tcod.white i = 0 for color in colors: i += 1 final_color = tcod.color_lerp(final_color, color, 1.0 / i) return final_color
def PrecipGradMap(World): # ------------------------------------------------------------ Print Map (Surface Temperature Gradient) white -> cold red -> warm -------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): tempv = World[x][y].precip tempcolor = libtcod.color_lerp ( libtcod.white, libtcod.light_blue,tempv) libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT/2 - WORLD_HEIGHT/2, '\333' , tempcolor, libtcod.black) libtcod.console_flush() return
def tint_tile(x,y,color,coef): _view = get_active_view() _o_color = tcod.Color(int(_view['col_buffer'][1][0][y,x]),int(_view['col_buffer'][1][1][y,x]),int(_view['col_buffer'][1][2][y,x])) _n_color = tcod.color_lerp(_o_color,color,coef) _view['col_buffer'][1][0][y,x] = _n_color.r _view['col_buffer'][1][1][y,x] = _n_color.g _view['col_buffer'][1][2][y,x] = _n_color.b
def update(self): super(Linked, self).update() t = self.entity.bg.tiles[(self.entity.x, self.entity.y)] if self.duration > 0: if t not in self.tiles: self.status.clone(self.entity) self.entity.get_attacked(self) self.end() else: t.bg_color = libtcod.color_lerp(libtcod.black, self.owner.original_color, 0.4)
def apply_screen_impact(): """ | Render the whole screen in the color set in gvar.screen_impact | and fade out. """ if gvar.screen_impact is not None: color = libtcod.color_lerp(libtcod.black, gvar.screen_impact[0], float(gvar.screen_impact[1])) libtcod.console_set_default_background(gvar.con, color) libtcod.console_rect(gvar.con, 1, 1, gvar.SCREEN_WIDTH, gvar.SCREEN_HEIGHT, False, libtcod.BKGND_SET) gvar.screen_impact[1] -= 0.25 if gvar.screen_impact[1] < 0: gvar.screen_impact = None
def TempGradMap( World ): # ------------------------------------------------------------ Print Map (Surface Temperature Gradient) white -> cold red -> warm -------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): tempv = World[x][y].temp tempcolor = libtcod.color_lerp(libtcod.white, libtcod.red, tempv) libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT / 2 - WORLD_HEIGHT / 2, '\333', tempcolor, libtcod.black) libtcod.console_flush() return
def draw(self, area): for col_offset in xrange(len(self.graphic)): for row_offset in xrange(len(self.graphic[0])): row = self.row + row_offset col = self.col + col_offset char = self.graphic[col_offset][row_offset] if lt.map_is_in_fov(area.fov_map, col, row): lt.console_set_default_foreground(area.area.con, self.f_color) lt.console_put_char(area.area.con, row, col, char) elif area[col][row].explored: color = lt.color_lerp(area.fog, self.f_color, area.fog_density) lt.console_set_default_foreground(area.con, color) lt.console_put_char(area.con, row, col, char)
def color_lerp(lerp, descending, base=libtcod.black, light=libtcod.light_blue): if descending: lerp -= 0.001 if lerp < 0.4: lerp = 0.4 descending = False else: lerp += 0.001 if lerp > 1.0: lerp = 1.0 descending = True color = libtcod.color_lerp(base, light, lerp) return color, lerp, descending
def PrecipGradMap( World ): # ------------------------------------------------------------ Print Map (Precipitation Gradient) white -> low blue -> high -------------------------------- for x in xrange(WORLD_WIDTH): for y in xrange(WORLD_HEIGHT): tempv = World[x][y].precip tempcolor = libtcod.color_lerp(libtcod.white, libtcod.light_blue, tempv) libtcod.console_put_char_ex( 0, x, y + SCREEN_HEIGHT / 2 - WORLD_HEIGHT / 2, '\333', tempcolor, libtcod.black) libtcod.console_flush() return
def take_behavior_action(self): battle = combat.WorldBattle(date=g.WORLD.time_cycle.get_current_date(), location=(self.figure.wx, self.figure.wy), faction1_named=[self.figure], faction1_populations=[], faction2_named=[self.target], faction2_populations=[]) g.game.add_message( battle.describe(), libtcod.color_lerp(g.PANEL_FRONT, self.figure.color, .3)) self.has_attempted_kill = 1
def process(self, game): visible =self.game.fov.is_visible(self.x, self.y) moved = False if visible: self.seen += 1 self.color = tcod.color_lerp(tcod.dark_gray, self.orig_color, (self.seen % 50) / 100.0) if self.seen % 50 == 0: self.game.duplicate(self) if self.seen == 200: self.character = 'o' self.movement = 0.4 elif self.seen == 400: self.character = 'O' self.movement = 0.6 path = tcod.path_new_using_map(self.game.fov.fov, 1.0) tcod.path_compute(path, self.x, self.y, self.game.player.x, self.game.player.y) if tcod.path_size(path) > 2: self.points += self.movement if self.points >= 1: self.points -= 1 x, y = tcod.path_get(path, 1) self.move(x - self.x, y - self.y) moved = True tcod.path_delete(path) if not moved: self.points += self.movement if self.points >= 1: self.points -= 1 movement = [ (0, 0), (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, -1), (-1, 1), (1, -1) ] self.move(*random.choice(movement)) return True
def create_explosion(pos, force): alife.noise.create(pos, force*100, 'an explosion', 'a low rumble', skip_on_visual=False) create_light(pos, (255, 69, 0), force*6, 1, fade=3) create_smoke_cloud(pos, force*6, age=.8, factor_distance=True) for i in range(random.randint(1, 3)): create_smoke_streamer(pos, 3+random.randint(0, 2), (force*2)+random.randint(3, 6), color=tcod.color_lerp(tcod.gray, tcod.crimson, random.uniform(0.1, 0.3)))
def create_map_legend(self, con, mode=0): for x in range(game.WORLDMAP_WIDTH): for y in range(game.WORLDMAP_HEIGHT): if mode == 0: cellheight = libtcod.heightmap_get_value(game.heightmap, x, y) else: cellheight = self.hm_list[(y * game.WORLDMAP_WIDTH) + x] if cellheight >= game.terrain['Mountain Peak']['elevation']: # mountain peak bcolor = libtcod.color_lerp(libtcod.silver, libtcod.grey, (1.000 - cellheight) / 0.050) elif cellheight >= game.terrain['Mountains']['elevation']: # mountains bcolor = libtcod.color_lerp(libtcod.grey, libtcod.Color(40, 24, 12), (game.terrain['Mountain Peak']['elevation'] - cellheight) / 0.125) elif cellheight >= game.terrain['High Hills']['elevation']: # hills bcolor = libtcod.color_lerp(libtcod.Color(40, 24, 12), libtcod.Color(53, 33, 16), (game.terrain['Mountains']['elevation'] - cellheight) / 0.125) elif cellheight >= game.terrain['Low Hills']['elevation']: # forest bcolor = libtcod.color_lerp(libtcod.Color(53, 33, 16), libtcod.Color(40, 67, 25), (game.terrain['High Hills']['elevation'] - cellheight) / 0.125) elif cellheight >= game.terrain['Forest']['elevation']: # forest bcolor = libtcod.color_lerp(libtcod.Color(40, 67, 25), libtcod.Color(80, 134, 50), (game.terrain['Low Hills']['elevation'] - cellheight) / 0.345) elif cellheight >= game.terrain['Plains']['elevation']: # plains bcolor = libtcod.color_lerp(libtcod.Color(80, 134, 50), libtcod.Color(112, 150, 80), (game.terrain['Forest']['elevation'] - cellheight) / 0.090) elif cellheight >= game.terrain['Coast']['elevation']: # coast bcolor = libtcod.color_lerp(libtcod.Color(112, 150, 80), libtcod.Color(176, 176, 153), (game.terrain['Plains']['elevation'] - cellheight) / 0.020) elif cellheight >= game.terrain['Shore']['elevation']: # shallow water bcolor = libtcod.color_lerp(libtcod.Color(176, 176, 153), libtcod.Color(47, 67, 103), (game.terrain['Coast']['elevation'] - cellheight) / 0.010) elif cellheight >= game.terrain['Sea']['elevation']: # deep water bcolor = libtcod.color_lerp(libtcod.Color(47, 67, 103), libtcod.Color(8, 32, 72), (game.terrain['Shore']['elevation'] - cellheight) / 0.040) else: # ocean bcolor = libtcod.Color(8, 32, 72) libtcod.console_put_char_ex(con, x, y, ' ', bcolor, bcolor) if mode != 3: libtcod.image_put_pixel(self.map_image_small, x, y, bcolor) if mode == 0: self.hm_list[(y * game.WORLDMAP_WIDTH) + x] = float("{0:.4f}".format(cellheight))
def draw_hud(player): libtcod.console_set_foreground_color(0, libtcod.white) X, Y, W, H = 0, 40, 30, 10 libtcod.console_set_background_color(0, libtcod.black) libtcod.console_rect(0, X, Y, W, H, True, libtcod.BKGND_SET) libtcod.console_put_char_ex(0, X + 1, Y + 1, player.symbol, player.color, libtcod.black) libtcod.console_print_left(0, X + 3, Y + 1, libtcod.BKGND_NONE, player.name) libtcod.console_print_left(0, X + 1, Y + 8, libtcod.BKGND_NONE, 'In {:s}'.format(player.map.name)) for i in range(10): if i * player.max_health > player.health * 10: libtcod.console_put_char_ex(0, X + 1 + i, Y + 3, ' ', libtcod.black, libtcod.darker_red) elif (i+1) * player.max_health > player.health * 10: libtcod.console_put_char_ex(0, X + 1 + i, Y + 3, ' ', libtcod.black, libtcod.color_lerp(libtcod.darker_red, libtcod.red, 10.*player.health/player.max_health - i)) else: libtcod.console_put_char_ex(0, X + 1 + i, Y + 3, ' ', libtcod.black, libtcod.red) for i in range(10): if player.energy > i: libtcod.console_put_char_ex(0, X + 1 + i, Y + 4, 'O', libtcod.blue + libtcod.white * 0.2, libtcod.black) else: libtcod.console_put_char_ex(0, X + 1 + i, Y + 4, 'O', libtcod.desaturated_blue * 0.5, libtcod.black) libtcod.console_print_left(0, X + 12, Y + 3, libtcod.BKGND_NONE, 'MH: - {}'.format(player.mainhand and player.mainhand.name or '-')) if player.mainhand: libtcod.console_put_char_ex(0, X + 16, Y + 3, player.mainhand.symbol, player.mainhand.color, libtcod.black) libtcod.console_print_left(0, X + 12, Y + 4, libtcod.BKGND_NONE, 'OH: - {}'.format(player.offhand and player.offhand.name or player.mainhand and player.mainhand.wield_twohands and player.mainhand.name or '-')) if player.offhand: libtcod.console_put_char_ex(0, X + 16, Y + 4, player.offhand.symbol, player.offhand.color, libtcod.black) elif player.mainhand and player.mainhand.wield_twohands: libtcod.console_put_char_ex(0, X + 16, Y + 4, player.mainhand.symbol, player.mainhand.color, libtcod.black) libtcod.console_print_left(0, X + 12, Y + 5, libtcod.BKGND_NONE, 'AR: - {}'.format(player.wearing and player.wearing.name or '-')) if player.wearing: libtcod.console_put_char_ex(0, X + 16, Y + 5, player.wearing.symbol, player.wearing.color, libtcod.black) draw_messages(player)
def _apply_splatters(self, base_color, coeff = 1): # total = 1 # for key, value in self.splatters.items(): # value = min(value, 1) # base_color += MATERIALS[key][0] * (MATERIALS[key][1] * value * coeff) # total += value * coeff # return base_color * (1. / total) amount = self.splatters['blood'] color, alpha = MATERIALS['blood'] return libtcod.color_lerp(base_color, color, amount * coeff * alpha)
def create_explosion(pos, force): alife.noise.create(pos, force * 100, 'an explosion', 'a low rumble', skip_on_visual=False) create_light(pos, (255, 69, 0), force * 6, 1, fade=3) create_smoke_cloud(pos, force * 6, age=.8, factor_distance=True) for i in range(random.randint(1, 3)): create_smoke_streamer(pos, 3 + random.randint(0, 2), (force * 2) + random.randint(3, 6), color=tcod.color_lerp(tcod.gray, tcod.crimson, random.uniform(0.1, 0.3)))
def create_target_list(): _menu_items = [] for target_id in LIFE[SETTINGS['controlling']]['seen']: if LIFE[target_id]['dead']: continue if not _menu_items: SETTINGS['following'] = target_id _color = life.draw_life_icon(LIFE[target_id])[1] _menu_items.append(create_item('single', ' '.join(LIFE[target_id]['name']), None, target=target_id, color=(_color, tcod.color_lerp(_color, tcod.white, 0.5)))) return _menu_items
def update_conway(): global game_map map_width, map_height = map_size new_map = copy.deepcopy(blank_map) for x in range(map_width): for y in range(map_height): adjacent = get_adjacent(x, y) alive = len(adjacent) == 3 or ( len(adjacent) == 2 and game_map[x][y]) if alive: final_color = tcod.white i = 0 for color in adjacent: i += 1 final_color = tcod.color_lerp(final_color, color, 1.0 / i) # color = most_common(adjacent) new_map[x][y] = final_color game_map = new_map
def new_game(): """ | Start a new game | Create a map, FOV Map and print Welcome Mesage """ from charactercreation import create_character from spawn import spawn_player gvar.game = gvar.Game() gvar.game.init_world() spawn_player([gvar.game.world.worldspace, 0]) initialize_fov() color = libtcod.color_lerp(libtcod.white, libtcod.red, 0.9) render.message('Welcome, Child of the Unconquered Sun.', color) render.animate_background('main_menu', 0.5, reverse=True) ch = create_character() if ch == 'exit': main_menu() play_game()
def end_struct(self, struct, name): skill_level = ['apprentice ', 'journeyman ', 'adept ', 'master '] self.temp_monster.dark_color = libtcod.color_lerp(libtcod.black, self.temp_monster.color, 0.3) self.temp_monster.health.nb_dices = self.temp_monster.level self.temp_monster.health.bonus = self.temp_monster.level if self.temp_monster.level == 1: self.temp_monster.flags.append('identified') game.monsters.add_to_list(self.temp_monster) if self.temp_monster.type == 'humanoid': for i in range(0, 4): self.new_monster = copy.deepcopy(self.temp_monster) self.new_monster.name = skill_level[i] + self.new_monster.name self.new_monster.level += (i * 2) + 1 self.new_monster.attack_rating += ((i * 2) + 1) * 8 self.new_monster.defense_rating += ((i * 2) + 1) * 8 self.new_monster.health.nb_dices = self.new_monster.level self.new_monster.health.bonus = self.new_monster.level self.new_monster.damage.multiplier = i + 2 game.monsters.add_to_list(self.new_monster) return True
def render_floating_text_animations(): fps = 8 try: for i, value in enumerate(reversed(game.hp_anim)): if game.MAP_Y + value['y'] - game.cury - (value['turns'] / fps) > 0: libtcod.console_set_default_foreground(0, libtcod.color_lerp(libtcod.black, value['color'], 1 - ((value['turns'] / fps) * 0.2))) if 'player' in value: libtcod.console_print_ex(0, game.MAP_X + game.char.x - game.curx, game.MAP_Y + game.char.y - game.cury - (value['turns'] / fps), libtcod.BKGND_NONE, libtcod.CENTER, value['damage']) else: libtcod.console_print_ex(0, game.MAP_X + value['x'] - game.curx, game.MAP_Y + value['y'] - game.cury - (value['turns'] / fps), libtcod.BKGND_NONE, libtcod.CENTER, value['damage']) if value['turns'] <= fps * 1.5: libtcod.console_set_default_foreground(0, value['icon_color']) if 'player' in value: libtcod.console_print_ex(0, game.MAP_X + game.char.x - game.curx, game.MAP_Y + game.char.y - game.cury, libtcod.BKGND_NONE, libtcod.CENTER, value['icon']) else: libtcod.console_print_ex(0, game.MAP_X + value['x'] - game.curx, game.MAP_Y + value['y'] - game.cury, libtcod.BKGND_NONE, libtcod.CENTER, value['icon']) value.update({'turns': value['turns'] + 1}) if value['turns'] > fps * 4: game.hp_anim.pop(len(game.hp_anim) - i - 1) except IndexError: print "Error in render floating text animations function" print game.hp_anim
def create_smoke(pos, color=tcod.gray, age=0, grow=0.1, decay=0.1, direction=-1, speed=0.3, max_opacity=.75, interp_wind=True): _intensity = random.uniform(max_opacity * .25, max_opacity) _color = tcod.color_lerp(color, tcod.white, random.uniform(0, 0.3)) if direction == -1: _velocity = [ random.uniform(-speed, speed), random.uniform(-speed, speed), 0 ] else: _velocity = bad_numbers.velocity(direction, speed) _effect = { 'type': 'smoke', 'color': _color, 'intensity': _intensity * age, 'max_intensity': _intensity, 'decay': decay, 'grow': grow, 'disappear': False, 'pos': list(pos), 'float_pos': list(pos), 'interp_wind': interp_wind, 'velocity': [_velocity[0], _velocity[1], _velocity[2]], 'callback': process_smoke, 'draw_callback': draw_smoke, 'unregister_callback': clear_effect } register_effect(_effect)
def render_blotter(): global blotter global took_turn reached_top = False y = PANEL_HEIGHT - 2 for turn in range(len(blotter)): messages = blotter[turn] for message in range(len(messages)): (text, color) = messages[message] color = libtcod.color_lerp(color, libtcod.black, min(turn * 0.2, 1)) libtcod.console_set_foreground_color(panel, color) y -= libtcod.console_height_left_rect(panel, STATS_WIDTH + 2, y, SCREEN_WIDTH - STATS_WIDTH - 3, 0, text) libtcod.console_print_left_rect(panel, STATS_WIDTH + 2, y+1, SCREEN_WIDTH - STATS_WIDTH - 3, 0, libtcod.BKGND_NONE, text) if y <= 1: reached_top = True break if reached_top: break if took_turn: blotter.insert(0,[]) blotter = blotter[0:5]
def render_all(): global fov_map, color_dark_wall, color_light_wall global color_dark_ground, color_light_ground global fov_recompute, fov_torchx if fov_recompute: #recompute FOV if needed (the player moved or something) fov_recompute = False libtcod.map_compute_fov(fov_map, player.x, player.y, TORCH_RADIUS, FOV_LIGHT_WALLS, FOV_ALGO) #torch flickers (using noise generator) fov_torchx += 0.2 tdx = [fov_torchx + 20.0] dx = libtcod.noise_get(fov_noise, tdx) * 1.5 tdx[0] += 30.0 dy = libtcod.noise_get(fov_noise, tdx) * 1.5 di = 0.2 * libtcod.noise_get(fov_noise, [fov_torchx]) # Iterate through rendering queue for y in range(MAP_HEIGHT): for x in range(MAP_WIDTH): visible = libtcod.map_is_in_fov(fov_map, x, y) wall = map[x, y].block_sight # check if tile is a wall if not visible: # if it's not visible right now, the player can only # see it if it's explored if map[x, y].explored: # It's out of the player's FOV if wall: libtcod.console_set_char_background(con, x, y, color_dark_wall, libtcod.BKGND_SET) else: libtcod.console_set_char_background(con, x, y, color_dark_ground, libtcod.BKGND_SET) else: # It's visible if wall: base = color_dark_wall light = color_light_wall else: base = color_dark_ground light = color_light_ground #Let the torch actually flicker r = float(x - player.x + dx) * (x - player.x + dx) + \ (y - player.y + dy) * (y - player.y + dy) if r < SQUARED_TORCH_RADIUS: l = (SQUARED_TORCH_RADIUS - r) / SQUARED_TORCH_RADIUS + di if l < 0.0: l = 0.0 elif l > 1.0: l = 1.0 # alter base colors to simulate flickering torch base = libtcod.color_lerp(base, light, l) # actually draw the visible tile libtcod.console_set_char_background(con, x, y, base, libtcod.BKGND_SET) #since it's visible, it's explored map[x, y].explored = True # Draw all objects in the list for object in objects: if object != player: object.draw() player.draw() # Blit the contents of con to the root console libtcod.console_blit(con, 0, 0, MAP_WIDTH, MAP_HEIGHT, 0, 0, 0) # Prepare to render the GUI panel libtcod.console_set_default_background(panel, libtcod.black) libtcod.console_clear(panel) # Print the game messages y = 1 for (line, color) in game_msgs: libtcod.console_set_default_foreground(panel, color) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, MSG_X, y, line) y += 1 # Show the player's stats render_bar(1, 1, BAR_WIDTH, 'HP', player.fighter.hp, player.fighter.max_hp, libtcod.light_red, libtcod.darker_red) libtcod.console_set_alignment(panel, libtcod.LEFT) libtcod.console_print(panel, 1, 3, 'Dungeon level {}'.format(str(dungeon_level))) # Display names of objects under the mouse libtcod.console_set_default_foreground(panel, libtcod.light_grey) libtcod.console_print(panel, 1, 0, get_names_under_mouse()) # Blit the contents of "panel" to the root console libtcod.console_blit(panel, 0, 0, SCREEN_WIDTH, PANEL_HEIGHT, 0, 0, PANEL_Y)
def render(self, map, view_centre, fov_entity): """Render map. Arguments: map - map to render view_centre - world location to centre map on when rendering fov_entity - entity to use for fov calculation """ # Generate field of view map fov_map = map.generate_fov_map(fov_entity) # Calculate bounds for rendering half_xsize = int(self.width / 2) half_ysize = int(self.height / 2) x0 = view_centre[0] - half_xsize y0 = view_centre[1] - half_ysize x1 = view_centre[0] + half_xsize + 1 y1 = view_centre[1] + half_ysize + 1 for y in range(y0, y1): for x in range(x0, x1): tile = map.tiles[x][y] visible = libtcod.map_is_in_fov(fov_map, x, y) # Calculate offset onto screen and render xt = self.x_offset + x - x0 yt = self.y_offset + y - y0 if not visible: if tile.seen: # 70% desaturation greyscale = int(tile.bcolour.r * 0.3 + tile.bcolour.g * 0.59 + tile.bcolour.b * 0.11) nonviscolour = libtcod.color_lerp( tile.bcolour, libtcod.Color(greyscale, greyscale, greyscale), 0.7) libtcod.console_set_char_background( 0, xt, yt, nonviscolour, libtcod.BKGND_SET) libtcod.console_put_char(0, xt, yt, ' ', libtcod.BKGND_NONE) else: libtcod.console_set_char_background( 0, xt, yt, libtcod.black, libtcod.BKGND_SET) libtcod.console_put_char(0, xt, yt, ' ', libtcod.BKGND_NONE) else: tile.seen = True libtcod.console_set_char_background( 0, xt, yt, tile.bcolour, libtcod.BKGND_SET) # Render entities in the world first if tile.entity: libtcod.console_set_default_foreground( 0, tile.entity.colour) libtcod.console_put_char(0, xt, yt, tile.entity.char, libtcod.BKGND_NONE) elif tile.inventory: libtcod.console_set_default_foreground( 0, tile.inventory.colour) libtcod.console_put_char(0, xt, yt, tile.inventory.char, libtcod.BKGND_NONE) elif tile.type == TileType.DOOR: libtcod.console_set_default_foreground( 0, tile.door.colour) libtcod.console_put_char(0, xt, yt, tile.door.char, libtcod.BKGND_NONE) elif tile.type == TileType.WINDOW: libtcod.console_set_default_foreground( 0, tile.window.colour) libtcod.console_put_char(0, xt, yt, tile.window.char, libtcod.BKGND_NONE) elif not tile.blocks_movement: libtcod.console_set_default_foreground( 0, libtcod.black) libtcod.console_put_char(0, xt, yt, '.', libtcod.BKGND_NONE)
def inventory(con, player, game, width=80, height=43): """ TODO: Add in highlighting for Weapons and Equipment consoles Add in keyboard arrow selection support Add in "drop" mode, drop items from inventory Fix Weapons and Equipment keyboard handling Fix duplication bug when equipping items (fixed) Fix take off weapon confirmation when equipping an item (fixed) 2 handed code needs work (?) keyboard input is sluggish. Might have to update libtcod to fix :param con: Destination console (not used atm) :param player: The main player object :param game: The main game object :param width: width of the inventory screen :param height: height of the inventory screen :return: An item that has been used (potion, scroll, etc..) """ equip_height = 14 wield_height = 8 compare_height = height - (equip_height - wield_height)-(wield_height*2) r, g, b = libtcod.white equip_y = wield_height compare_y = equip_height + wield_height inventory_window = game.gEngine.console_new(width/2, height) game.gEngine.console_set_default_foreground(inventory_window, r, g, b) game.gEngine.console_print_frame(inventory_window, 0, 0, width/2, height, True) game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) equipment_window = game.gEngine.console_new(width/2, equip_height) game.gEngine.console_set_default_foreground(equipment_window, r, g, b) game.gEngine.console_print_frame(equipment_window, 0, 0, width/2, equip_height, True) game.gEngine.console_set_default_background(equipment_window, 0, 0, 0) wielded_window = game.gEngine.console_new(width/2, wield_height) game.gEngine.console_set_default_foreground(wielded_window, r, g, b) game.gEngine.console_print_frame(wielded_window, 0, 0, width/2, wield_height, True) game.gEngine.console_set_default_background(wielded_window, 0, 0, 0) compare_window = game.gEngine.console_new(width/2, compare_height) game.gEngine.console_set_default_foreground(compare_window, r, g, b) game.gEngine.console_print_frame(compare_window, 0, 0, width/2, compare_height, True) game.gEngine.console_set_default_background(compare_window, 0, 0, 0) check_boxes = [] slots = ['Torso ', 'Head ', 'Hands ', 'Legs ', 'Feet ', 'Arms ', 'Shoulders', 'Back '] #self.buttons.append(Button(self, self.option_labels[0], self.width//6-5, self.height/2-1, True)) exit_button = Button(label='Exit', game=game, x_pos=(width/2)-9, y_pos=height-6, window=inventory_window, dest_x=width/2, dest_y=0) drop_button = Button(label='Drop', game=game, x_pos=1, y_pos=height-6, window=inventory_window, dest_x=width/2, dest_y=0) if len(player.fighter.inventory) == 0: inventory_items = ['Inventory is empty.'] else: inventory_items = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x+3)) i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color) if player.fighter.inventory[x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[x].item.qty inventory_items.append(i) i_header = 'Inventory' i_header_size = len(i_header) i_header_pos = (width/4)-(i_header_size/2) w_header = 'Weapons' w_header_size = len(w_header) w_header_pos = (width/8) - (w_header_size/2) e_header = 'Equipment' e_header_size = len(e_header) e_header_pos = (width/8) - (e_header_size/2) c_header = 'Compare/Examine' c_header_size = len(c_header) c_header_pos = (width/8) - (c_header_size/2) return_item = None key = libtcod.console_check_for_keypress() current_selection = 0 master_check = CheckBox(1, 30, "Check/Uncheck All") while key.vk != libtcod.KEY_ESCAPE: game.gEngine.console_flush() # get input just after flush key = libtcod.console_check_for_keypress(True) mouse = libtcod.mouse_get_status() exit_input = exit_button.display(mouse) drop_input = drop_button.display(mouse) game.gEngine.console_blit(inventory_window, 0, 0, width/2, height, 0, (width/2), 0, 1.0, 1.0) game.gEngine.console_blit(wielded_window, 0, 0, width/2, height, 0, 0, 0, 1.0, 1.0) game.gEngine.console_blit(equipment_window, 0, 0, width/2, height, 0, 0, equip_y, 1.0, 1.0) game.gEngine.console_blit(compare_window, 0, 0, width/2, height, 0, 0, compare_y, 1.0, 1.0) game.gEngine.console_clear(inventory_window) game.gEngine.console_clear(wielded_window) game.gEngine.console_clear(equipment_window) game.gEngine.console_clear(compare_window) # set up draw screen r, g, b = libtcod.white game.gEngine.console_set_default_foreground(inventory_window, r, g, b) game.gEngine.console_print_frame(inventory_window, 0, 0, width/2, height, True) game.gEngine.console_set_default_foreground(equipment_window, r, g, b) game.gEngine.console_print_frame(equipment_window, 0, 0, width/2, equip_height, True) game.gEngine.console_set_default_foreground(wielded_window, r, g, b) game.gEngine.console_print_frame(wielded_window, 0, 0, width/2, wield_height, True) game.gEngine.console_set_default_foreground(compare_window, r, g, b) game.gEngine.console_print_frame(compare_window, 0, 0, width/2, compare_height, True) # ======================================================================== # print inventory # ======================================================================== game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header) letter_index = ord('a') if len(player.fighter.inventory) > 0: y = 1 for i in range(len(inventory_items)): text = ' (' + chr(letter_index) + ') ' + inventory_items[i] if current_selection == y - 1: r, g, b = libtcod.color_lerp(player.fighter.inventory[i].color, libtcod.blue, 0.5) game.gEngine.console_set_default_background(inventory_window, r, g, b) else: game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) game.gEngine.console_print_ex(inventory_window, 1, y+2, libtcod.BKGND_SET, libtcod.LEFT, text) y += 1 letter_index += 1 game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) game.gEngine.console_print(inventory_window, 1, 31, 'Gold: ' + color_text(str(player.fighter.money), libtcod.gold)) # ======================================================================== # print equipped weapons # ======================================================================== game.gEngine.console_print(wielded_window, w_header_pos, 0, w_header) index = ord('1') if player.fighter.wielded[0] is None: text = '(' + chr(index) + ') ' + 'Left Hand : ' + color_text('Empty', libtcod.darker_gray) else: t = color_text(player.fighter.wielded[0].name.capitalize(), player.fighter.wielded[0].color) text = '(' + chr(index) + ') ' + 'Left Hand : ' + t index += 1 game.gEngine.console_print(wielded_window, 1, 2, text) if player.fighter.wielded[1] is None: text = '(' + chr(index) + ') ' + 'Right Hand: ' + color_text('Empty', libtcod.darker_gray) else: t = color_text(player.fighter.wielded[1].name.capitalize(), player.fighter.wielded[1].color) text = '(' + chr(index) + ') ' + 'Right Hand: ' + t index += 1 game.gEngine.console_print(wielded_window, 1, 3, text) item = player.fighter.wielded[0] if item: damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub) text = 'Damage (total): ' + color_text(damage, libtcod.green) game.gEngine.console_print(wielded_window, 1, 4, text) accuracy = item.item.equipment.accuracy accuracy += game.player.fighter.get_skill(item.item.equipment.damage_type).get_bonus() text = 'Accuracy (total): ' + color_text(str(accuracy), libtcod.green) game.gEngine.console_print(wielded_window, 1, 5, text) # ======================================================================== # print equipped armor # ======================================================================== game.gEngine.console_print(equipment_window, e_header_pos, 0, e_header) i = 0 game.gEngine.console_set_alignment(equipment_window, libtcod.LEFT) armor_bonus = 0 armor_penalty = 0 for item in player.fighter.equipment: text = '(' + chr(index) + ') ' + slots[i] + ': ' if item is None: text += color_text('Empty', libtcod.darker_gray) else: text += color_text(item.name.capitalize(), item.color) armor_bonus += item.item.equipment.bonus armor_penalty += item.item.equipment.penalty game.gEngine.console_print(equipment_window, 1, i+2, text) i += 1 index += 1 c = color_text(str(armor_bonus), libtcod.green) text = 'Total Armor Bonus : ' + c game.gEngine.console_print(equipment_window, 1, i+2, text) c = color_text(str(armor_penalty), libtcod.red) text = 'Total Armor Penalty: -' + c game.gEngine.console_print(equipment_window, 1, i+3, text) game.gEngine.console_print(compare_window, c_header_pos, 0, c_header) # ======================================================================== # handle mouse input # ======================================================================== mc = master_check.update(mouse, width) master_check.render(inventory_window, game) if not mc: for box in check_boxes: box.update(mouse, width) box.render(inventory_window, game) else: for box in check_boxes: box.set_checked(master_check.get_checked()) box.render(inventory_window, game) # Inventory input if mouse.cx >= width/2+3 and mouse.cx < width-2: # inventory screen dims if (mouse.cy-3) < len(player.fighter.inventory) and mouse.cy-3 >= 0: #if mouse.cy-3 <= len(player.fighter.inventory): item = player.fighter.inventory[mouse.cy-3] current_selection = mouse.cy-3 if item.item.equipment: game.gEngine.console_print(compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print(compare_window, 1, 3, 'Type : ' + item.item.equipment.type.capitalize()) if item.item.equipment.type == 'melee': damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub ) game.gEngine.console_print(compare_window, 1, 4, 'Damage : ' + damage) game.gEngine.console_print(compare_window, 1, 5, 'Accuracy: ' + str(item.item.equipment.accuracy)) game.gEngine.console_print(compare_window, 1, 7, 'Skill : ' + item.item.equipment.damage_type) else: game.gEngine.console_print(compare_window, 1, 4, 'Armor : ' + str(item.item.equipment.bonus)) game.gEngine.console_print(compare_window, 1, 5, 'Penalty : ' + str(item.item.equipment.penalty)) game.gEngine.console_print(compare_window, 1, 7, 'Location: ' + item.item.equipment.location.capitalize()) game.gEngine.console_print(compare_window, 1, 6, 'Value : ' + str(item.item.value)) if item.item.spell: game.gEngine.console_print(compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print(compare_window, 1, 3, 'Type : ' + item.item.spell.type.capitalize()) game.gEngine.console_print(compare_window, 1, 4, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max)) game.gEngine.console_print(compare_window, 1, 5, 'Range : ' + str(item.item.spell.range)) game.gEngine.console_print(compare_window, 1, 6, 'Radius: ' + str(item.item.spell.radius)) game.gEngine.console_print(compare_window, 1, 7, 'Value : ' + str(item.item.value)) if mouse.lbutton_pressed and item.item.spell: i_n = color_text(item.name.capitalize(), item.color) message = 'Do you want to use %s?' % i_n w = len(message)+2 d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: # make sure if the player uses a scroll or potion, we exit inventory d_box.destroy_box() # Remember to remove consoles in reverse order of creation to avoid OOB errors return_item = item break else: d_box.destroy_box() if mouse.lbutton_pressed and item.item.equipment: i_n = color_text(item.name.capitalize(), item.color) message = 'Do you want to put %s on?' % i_n w = len(message)+2 d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: item.item.use(game.player.fighter.inventory, game.player, game) d_box.destroy_box() inventory_items = [] check_boxes = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x+3)) i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color) if player.fighter.inventory[x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[x].item.qty inventory_items.append(i) else: current_selection = None # game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) # Wielded elif mouse.cx >= 0 and mouse.cx <= ((width/2)-2): # inventory screen dims if (mouse.cy-2) < len(player.fighter.wielded): item = player.fighter.wielded[mouse.cy-2] if item is not None: if item.item.equipment: game.gEngine.console_print(compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print(compare_window, 1, 3, 'Type : ' + item.item.equipment.type.capitalize()) damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub ) game.gEngine.console_print(compare_window, 1, 4, 'Damage : ' + damage) game.gEngine.console_print(compare_window, 1, 5, 'Accuracy: ' + str(item.item.equipment.accuracy)) game.gEngine.console_print(compare_window, 1, 6, 'Value : ' + str(item.item.value)) # Equipment elif (mouse.cy-2)-equip_y < len(player.fighter.equipment): item = player.fighter.equipment[mouse.cy-2-equip_y] if item is not None: if item.item.equipment: game.gEngine.console_print(compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print(compare_window, 1, 3, 'Type : ' + item.item.equipment.type.capitalize()) game.gEngine.console_print(compare_window, 1, 4, 'Armor : ' + str(item.item.equipment.bonus)) game.gEngine.console_print(compare_window, 1, 5, 'Penalty : ' + str(item.item.equipment.penalty)) game.gEngine.console_print(compare_window, 1, 6, 'Value : ' + str(item.item.value)) if mouse.lbutton_pressed and item is not None: i_n = color_text(item.name.capitalize(), item.color) message = 'Do you want to take %s off?' % i_n w = len(message)+2 d_box = DialogBox(game, w, 10, width/4, height/2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: item.item.equipment.un_equip(game.player, item) d_box.destroy_box() inventory_items = [] check_boxes = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x+3)) i = color_text(player.fighter.inventory[x].name.capitalize(),player.fighter.inventory[x].color) if player.fighter.inventory[x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[x].item.qty inventory_items.append(i) i = 0 for x in player.fighter.wielded: if x == item: player.fighter.wielded[i] = None i += 1 i = 0 for x in player.fighter.equipment: if x == item: player.fighter.equipment[i] = None i += 1 # keyboard input # keeps similar feel to old inventory if using the keys # keyboard input is sluggish as balls for some reason, need to look into it more index = key.c - ord('a') if key: if index >= 0 and index < len(inventory_items): return_item = player.fighter.inventory[index] break index = key.c - ord('1') if index >= 0 and index <= 1: return_item = player.fighter.wielded[index] break elif index >= 2 and index <= 10: return_item = player.fighter.equipment[index-2] break if key.vk == libtcod.KEY_DOWN: if current_selection is None: current_selection = 0 current_selection += 1 if current_selection > len(inventory_items): current_selection = 0 if key.vk == libtcod.KEY_UP: if current_selection is None: current_selection = 0 current_selection -= 1 if current_selection < 0: current_selection = len(inventory_items) # ======================================================================== # handle buttons # ======================================================================== for i in drop_input: if i != -1: message = 'Drop all selected items?' w = len(message)+2 d_box = DialogBox(game, w, 10, width/2-w/2, height/2-5, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: d_box.destroy_box() master_check.set_checked(False) items_to_drop = [] for box in check_boxes: if box.get_checked(): items_to_drop.append(player.fighter.inventory[box.y-3]) for item_to_drop in items_to_drop: if item_to_drop: item_to_drop.objects = game.objects item_to_drop.item.drop(player.fighter.inventory, player, False) item_to_drop.send_to_back() check_boxes = [] inventory_items = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x+3)) inventory_items.append(color_text(player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color)) else: d_box.destroy_box() # ======================================================================== # handle exit button # ======================================================================== for i in exit_input: if i != -1: key.vk = libtcod.KEY_ESCAPE break # Remember to remove consoles in reverse order of creation to avoid OOB errors drop_button.destroy_button() exit_button.destroy_button() game.gEngine.console_remove_console(compare_window) game.gEngine.console_remove_console(wielded_window) game.gEngine.console_remove_console(equipment_window) game.gEngine.console_remove_console(inventory_window) return return_item
def end_struct(self, struct, name): self.temp_suffix.dark_color = libtcod.color_lerp(libtcod.black, self.temp_suffix.color, 0.3) game.suffix.add_to_list(self.temp_suffix) return True
def end_struct(self, struct, name): self.temp_item.dark_color = libtcod.color_lerp(libtcod.black, self.temp_item.color, 0.3) game.baseitems.add_to_list(self.temp_item) return True
#return libtcod.color_gen_map(color_list, color_keys) return Color.color_map(color_list, color_keys) def segment_number(number, times): num_list = [] for x in range(1, times + 1): num_list.append((float(number) / float(times)) * x) print "Number: {0}\n Times: {1}\n Result: {2}\n".format( number, times, num_list) return num_list heat_map_colors = [ libtcod.yellow, libtcod.color_lerp(libtcod.yellow, libtcod.amber, 0.5), libtcod.color_lerp(libtcod.yellow, libtcod.amber, 0.5), libtcod.amber, libtcod.color_lerp(libtcod.amber, libtcod.orange, 0.5), libtcod.color_lerp(libtcod.amber, libtcod.orange, 0.5), libtcod.orange, libtcod.color_lerp(libtcod.orange, libtcod.flame, 0.5), libtcod.color_lerp(libtcod.orange, libtcod.flame, 0.5), libtcod.flame, libtcod.color_lerp(libtcod.flame, libtcod.red, 0.5), libtcod.color_lerp(libtcod.flame, libtcod.red, 0.5), libtcod.red, libtcod.color_lerp(libtcod.red, libtcod.crimson, 0.5), libtcod.color_lerp(libtcod.red, libtcod.crimson, 0.5), libtcod.crimson, libtcod.color_lerp(libtcod.crimson, libtcod.pink, 0.5), libtcod.color_lerp(libtcod.crimson, libtcod.pink, 0.5), libtcod.pink, libtcod.color_lerp(libtcod.pink, libtcod.magenta, 0.5), libtcod.color_lerp(libtcod.pink, libtcod.magenta, 0.5), libtcod.magenta, libtcod.color_lerp(libtcod.magenta, libtcod.fuchsia, 0.5), libtcod.color_lerp(libtcod.magenta, libtcod.fuchsia, 0.5), libtcod.fuchsia,
def shop(con, player, game, container=None, bg=None, header=None, width=80, height=50, splash=False): """ TODO: Make sure you cannot buy items if it were to put you over inventory limit - Done Attach this to a shopkeeper to control splash and inventory. Add a buyback screen to buy back things sold by accident :param con: Destination Console (not used atm) :param player: The Player Object :param game: The main game object :param container: The contents of the store :param bg: The background image to use :param header: The title of the store :param width: width of the shop :param height: height of the shop :param splash: Whether or not to display the splash screen :return: Nothing """ if bg: dark_bg = game.gEngine.image_load(bg) bg = game.gEngine.image_load(bg) w, h = game.gEngine.image_get_size(dark_bg) for y in range(w): for x in range(h): col = game.gEngine.image_get_pixel(dark_bg, y, x) col = libtcod.color_lerp(col, libtcod.black, 0.9) #col = libtcod.color_lerp(col, libtcod.light_azure, 0.2) r, g, b = col game.gEngine.image_put_pixel(dark_bg, y, x, r, g, b) shop_height = 28 compare_height = height - shop_height s_options = [] s_gold = [] s_size = 0 if container: for obj in container: obj_text = color_text(obj.name.capitalize(), obj.color) ob_value = color_text(obj.item.value, libtcod.gold) ob_value = '(%s)' % ob_value s_gold.append(ob_value) opt = '[%s] ' % obj_text s_options.append(opt) if len(obj.name) + 2 > s_size: s_size = len(obj.name) + 2 r, g, b = libtcod.white compare_y = shop_height inventory_window = game.gEngine.console_new(width / 2, height) game.gEngine.console_set_default_foreground(inventory_window, r, g, b) game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2, height, True) shop_window = game.gEngine.console_new(width / 2, shop_height) game.gEngine.console_set_default_foreground(shop_window, r, g, b) game.gEngine.console_print_frame(shop_window, 0, 0, width / 2, shop_height, True) compare_window = game.gEngine.console_new(width / 2, compare_height) game.gEngine.console_set_default_foreground(compare_window, r, g, b) game.gEngine.console_print_frame(compare_window, 0, 0, width / 2, compare_height, True) i_check_boxes = [] s_check_boxes = [] exit_button = Button(label='Exit', game=game, x_pos=(width / 2) - 9, y_pos=height - 6, window=inventory_window, dest_x=width / 2, dest_y=0) sell_button = Button(label='Sell', game=game, x_pos=1, y_pos=height - 6, window=inventory_window, dest_x=width / 2, dest_y=0) buy_button = Button(label='Buy', game=game, x_pos=1, y_pos=compare_height - 6, window=compare_window, dest_x=0, dest_y=compare_y) if len(player.fighter.inventory) == 0: inventory_items = ['Inventory is empty.'] else: inventory_items = [] for x in range(len(player.fighter.inventory)): i_check_boxes.append(CheckBox(x=1, y=x + 1)) i = color_text(player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[x].item.qty inventory_items.append(i) for x in range(len(container)): s_check_boxes.append(CheckBox(x=1, y=x + 1)) i_header = 'Inventory' i_header_size = len(i_header) i_header_pos = (width / 4) - (i_header_size / 2) if header: s_header = header else: s_header = 'Shop' s_header_size = len(s_header) s_header_pos = (width / 4) - (s_header_size / 2) c_header = 'Compare/Examine' c_header_size = len(c_header) c_header_pos = (width / 4) - (c_header_size / 2) key = libtcod.console_check_for_keypress() mouse = libtcod.mouse_get_status() current_selection = 0 s_current_selection = 0 i_master_check = CheckBox(1, 30, "Check/Uncheck All") s_master_check = CheckBox(1, 26, "Check/Uncheck All") fade = 1 if splash: splash_screen = game.gEngine.console_new(width, height) while key.vk != libtcod.KEY_ESCAPE: game.gEngine.console_flush() # get input just after flush key = libtcod.console_check_for_keypress(True) mouse = libtcod.mouse_get_status() exit_input = exit_button.display(mouse) sell_input = sell_button.display(mouse) buy_input = buy_button.display(mouse) game.gEngine.console_blit(inventory_window, 0, 0, width / 2, height, 0, (width / 2), 0, 1.0, 1.0) game.gEngine.console_blit(shop_window, 0, 0, width / 2, shop_height, 0, 0, 0, 1.0, 1.0) game.gEngine.console_blit(compare_window, 0, 0, width / 2, compare_height, 0, 0, compare_y, 1.0, 1.0) if splash: if bg: if fade > 0: game.gEngine.console_blit(splash_screen, 0, 0, width, height, 0, 0, 0, fade, fade) game.gEngine.image_blit_2x(bg, splash_screen, 0, 0, 0, 0) fade -= 0.045 else: fade = 0 game.gEngine.console_clear(inventory_window) game.gEngine.console_clear(shop_window) game.gEngine.console_clear(compare_window) # set up draw screen r, g, b = libtcod.white # ======================================================================== # print inventory # ======================================================================== if bg: game.gEngine.image_blit_2x(dark_bg, inventory_window, 0, 0, width, 0) game.gEngine.console_set_default_foreground(inventory_window, r, g, b) game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2, height, False) game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header) if len(player.fighter.inventory) > 0: letter_index = ord('a') y = 1 for i in range(len(inventory_items)): text = ' (' + chr(letter_index) + ') ' + inventory_items[i] if current_selection == y - 1: r, g, b = libtcod.color_lerp( player.fighter.inventory[i].color, libtcod.blue, 0.5) game.gEngine.console_set_default_background( inventory_window, r, g, b) else: game.gEngine.console_set_default_background( inventory_window, 0, 0, 0) game.gEngine.console_print_ex(inventory_window, 1, y, libtcod.BKGND_SET, libtcod.LEFT, text) y += 1 letter_index += 1 game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) game.gEngine.console_print( inventory_window, 1, 31, 'Gold: ' + color_text(str(player.fighter.money), libtcod.gold)) r, g, b = libtcod.white # ======================================================================== # Print Shop Window # ======================================================================== if bg: game.gEngine.image_blit_2x(dark_bg, shop_window, 0, 0) game.gEngine.console_set_default_foreground(shop_window, r, g, b) game.gEngine.console_print_frame(shop_window, 0, 0, width / 2, shop_height, False) game.gEngine.console_print(shop_window, s_header_pos, 0, s_header) if len(s_options) > 0: letter_index = ord('0') y = 1 for i in range(len(s_options)): text = ' (' + chr(letter_index) + ') ' + s_options[i] if s_current_selection == y - 1: rr, gg, bb = libtcod.color_lerp(container[i].color, libtcod.blue, 0.5) game.gEngine.console_set_default_background( shop_window, rr, gg, bb) else: game.gEngine.console_set_default_background( shop_window, 0, 0, 0) game.gEngine.console_print_ex(shop_window, 1, y, libtcod.BKGND_SET, libtcod.LEFT, text) game.gEngine.console_print_ex(shop_window, s_size + 10, y, libtcod.BKGND_SET, libtcod.RIGHT, s_gold[i]) y += 1 letter_index += 1 game.gEngine.console_set_default_background( inventory_window, 0, 0, 0) r, g, b = libtcod.white # ======================================================================== # Print Compare Window # ======================================================================== if bg: game.gEngine.image_blit_2x(dark_bg, compare_window, 0, 0, sx=0, sy=compare_y * 2) game.gEngine.console_set_default_foreground(compare_window, r, g, b) game.gEngine.console_print_frame(compare_window, 0, 0, width / 2, compare_height, False) game.gEngine.console_print(compare_window, c_header_pos, 0, c_header) # ======================================================================== # handle mouse input # ======================================================================== #Render check boxes #inventory check boxes i_mc = i_master_check.update(mouse, width) i_master_check.render(inventory_window, game) sell_value = 0 if not i_mc: for box in i_check_boxes: box.update(mouse, width) box.render(inventory_window, game) if box.get_checked(): sell_value += player.fighter.inventory[box.y - 1].item.value / 2 else: for box in i_check_boxes: box.set_checked(i_master_check.get_checked()) box.render(inventory_window, game) if box.get_checked(): sell_value += player.fighter.inventory[box.y - 1].item.value / 2 game.gEngine.console_print( inventory_window, 1, 32, 'Sell Value: ' + color_text(str(sell_value), libtcod.gold)) #Shop check boxes s_mc = s_master_check.update(mouse) s_master_check.render(shop_window, game) buy_value = 0 if not s_mc: for box in s_check_boxes: box.update(mouse) box.render(shop_window, game) if box.get_checked(): buy_value += container[box.y - 1].item.value else: for box in s_check_boxes: box.set_checked(s_master_check.get_checked()) box.render(shop_window, game) if box.get_checked(): buy_value += container[box.y - 1].item.value game.gEngine.console_print( shop_window, 1, 25, 'Buy Value: ' + color_text(str(buy_value), libtcod.gold)) # Inventory input if mouse.cx >= width / 2 + 3 and mouse.cx < width - 2: # inventory screen dims if (mouse.cy - 1) < len( player.fighter.inventory) and mouse.cy - 1 >= 0: item = player.fighter.inventory[mouse.cy - 1] current_selection = mouse.cy - 1 game.gEngine.console_set_default_background( compare_window, 0, 0, 0) if item.item.equipment: game.gEngine.console_print_ex( compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print_ex( compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type : ' + item.item.equipment.type.capitalize()) if item.item.equipment.type == 'melee': damage = '%dd%d+%d' % ( item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub) game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Damage : ' + damage) game.gEngine.console_print_ex( compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Accuracy: ' + str(item.item.equipment.accuracy)) game.gEngine.console_print_ex( compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Skill : ' + item.item.equipment.damage_type) else: game.gEngine.console_print_ex( compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Armor : ' + str(item.item.equipment.bonus)) game.gEngine.console_print_ex( compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Penalty : ' + str(item.item.equipment.penalty)) game.gEngine.console_print_ex( compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Location: ' + item.item.equipment.location.capitalize()) game.gEngine.console_print_ex( compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Value : ' + str(item.item.value)) if item.item.spell: game.gEngine.console_print_ex( compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print_ex( compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type : ' + item.item.spell.type.capitalize()) game.gEngine.console_print_ex( compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max)) game.gEngine.console_print_ex( compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Range : ' + str(item.item.spell.range)) game.gEngine.console_print_ex( compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Radius: ' + str(item.item.spell.radius)) game.gEngine.console_print_ex( compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Value : ' + str(item.item.value)) if mouse.lbutton_pressed: i_n = color_text(item.name.capitalize(), item.color) price = color_text(item.item.value / 2, libtcod.gold) message = 'Sell %s for %s?' % (i_n, price) w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 4, height / 2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: player.fighter.money += item.item.value / 2 player.fighter.inventory.remove(item) inventory_items = [] i_check_boxes = [] for x in range(len(player.fighter.inventory)): i_check_boxes.append(CheckBox(x=1, y=x + 1)) i = color_text( player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[ x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[ x].item.qty inventory_items.append(i) # shop input if mouse.cx >= 0 and mouse.cx < width / 2 - 2: # shop screen dims if (mouse.cy - 1) < len(container) and mouse.cy - 1 >= 0: item = container[mouse.cy - 1] s_current_selection = mouse.cy - 1 game.gEngine.console_set_default_background( compare_window, 0, 0, 0) if item.item.equipment: game.gEngine.console_print_ex( compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print_ex( compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type : ' + item.item.equipment.type.capitalize()) if item.item.equipment.type == 'melee': damage = '%dd%d+%d' % ( item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub) game.gEngine.console_print_ex(compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Damage : ' + damage) game.gEngine.console_print_ex( compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Accuracy: ' + str(item.item.equipment.accuracy)) game.gEngine.console_print_ex( compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Skill : ' + item.item.equipment.damage_type) else: game.gEngine.console_print_ex( compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Armor : ' + str(item.item.equipment.bonus)) game.gEngine.console_print_ex( compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Penalty : ' + str(item.item.equipment.penalty)) game.gEngine.console_print_ex( compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Location: ' + item.item.equipment.location.capitalize()) game.gEngine.console_print_ex( compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Value : ' + str(item.item.value)) if item.item.spell: game.gEngine.console_print_ex( compare_window, 1, 2, libtcod.BKGND_SET, libtcod.LEFT, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print_ex( compare_window, 1, 3, libtcod.BKGND_SET, libtcod.LEFT, 'Type : ' + item.item.spell.type.capitalize()) game.gEngine.console_print_ex( compare_window, 1, 4, libtcod.BKGND_SET, libtcod.LEFT, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max)) game.gEngine.console_print_ex( compare_window, 1, 5, libtcod.BKGND_SET, libtcod.LEFT, 'Range : ' + str(item.item.spell.range)) game.gEngine.console_print_ex( compare_window, 1, 6, libtcod.BKGND_SET, libtcod.LEFT, 'Radius: ' + str(item.item.spell.radius)) game.gEngine.console_print_ex( compare_window, 1, 7, libtcod.BKGND_SET, libtcod.LEFT, 'Value : ' + str(item.item.value)) if mouse.lbutton_pressed and mouse.cx >= 3: if len(player.fighter.inventory) >= 26: if not item.item.check_stackable(): message = 'Not enough inventory space!' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='dialog', con=inventory_window) d_box.display_box() else: pass elif item.item.value > player.fighter.money: message = 'Not enough money!' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='dialog', con=inventory_window) d_box.display_box() s_master_check.set_checked(False) for box in s_check_boxes: box.set_checked(s_master_check.get_checked()) else: i_n = color_text(item.name.capitalize(), item.color) price = color_text(item.item.value, libtcod.gold) message = 'Buy %s for %s?' % (i_n, price) w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 4, height / 2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: player.fighter.money -= item.item.value #player.fighter.inventory.append(item) item.item.pick_up(player.fighter.inventory) container.remove(item) inventory_items = [] i_check_boxes = [] for x in range(len(player.fighter.inventory)): i_check_boxes.append(CheckBox(x=1, y=x + 1)) i = color_text( player.fighter.inventory[x].name. capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[ x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[ x].item.qty inventory_items.append(i) s_check_boxes = [] s_options = [] if container: for obj in container: obj_text = color_text( obj.name.capitalize(), obj.color) ob_value = color_text( obj.item.value, libtcod.gold) ob_value = '(%s)' % ob_value s_gold.append(ob_value) opt = '[%s] ' % obj_text s_options.append(opt) if len(obj.name) + 2 > s_size: s_size = len(obj.name) + 2 for x in range(len(container)): s_check_boxes.append(CheckBox(x=1, y=x + 1)) # keyboard input index = key.c - ord('a') if key: if key.vk == libtcod.KEY_DOWN: if current_selection is None: current_selection = 0 current_selection += 1 if current_selection > len(inventory_items): current_selection = 0 if key.vk == libtcod.KEY_UP: if current_selection is None: current_selection = 0 current_selection -= 1 if current_selection < 0: current_selection = len(inventory_items) # ======================================================================== # handle buttons # ======================================================================== # ======================================================================== # Sell Button # ======================================================================== for i in sell_input: if i != -1: message = 'Sell all selected items?' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: d_box.destroy_box() i_master_check.set_checked(False) items_to_sell = [] for box in i_check_boxes: if box.get_checked(): items_to_sell.append( player.fighter.inventory[box.y - 1]) for item_to_sell in items_to_sell: if item_to_sell: player.fighter.money += item_to_sell.item.value / 2 player.fighter.inventory.remove(item_to_sell) i_check_boxes = [] inventory_items = [] for x in range(len(player.fighter.inventory)): i_check_boxes.append(CheckBox(x=1, y=x + 1)) inventory_items.append( color_text( player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color)) else: d_box.destroy_box() # ======================================================================== # buy button # ======================================================================== for i in buy_input: if i != -1: n = 0 for box in s_check_boxes: if box.get_checked(): n += 1 if len(player.fighter.inventory) + n > 26: message = 'Not enough inventory space!' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='dialog', con=inventory_window) d_box.display_box() elif buy_value > player.fighter.money: message = 'Not enough money!' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='dialog', con=inventory_window) d_box.display_box() s_master_check.set_checked(False) for box in s_check_boxes: box.set_checked(s_master_check.get_checked()) else: message = 'Buy all selected items?' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: #d_box.destroy_box() s_master_check.set_checked(False) items_to_buy = [] for box in s_check_boxes: if box.get_checked(): items_to_buy.append(container[box.y - 1]) for item_to_buy in items_to_buy: if item_to_buy: container.remove(item_to_buy) player.fighter.money -= item_to_buy.item.value #player.fighter.inventory.append(item_to_buy) item_to_buy.item.pick_up( player.fighter.inventory) s_check_boxes = [] s_options = [] if container: for obj in container: obj_text = color_text(obj.name.capitalize(), obj.color) ob_value = color_text(obj.item.value, libtcod.gold) ob_value = '(%s)' % ob_value s_gold.append(ob_value) opt = '[%s] ' % obj_text s_options.append(opt) if len(obj.name) + 2 > s_size: s_size = len(obj.name) + 2 for x in range(len(container)): s_check_boxes.append(CheckBox(x=1, y=x + 1)) i_check_boxes = [] inventory_items = [] for x in range(len(player.fighter.inventory)): i_check_boxes.append(CheckBox(x=1, y=x + 1)) i = color_text( player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[ x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[ x].item.qty inventory_items.append(i) #else: d_box.destroy_box() # ======================================================================== # handle exit button # ======================================================================== for i in exit_input: if i != -1: key.vk = libtcod.KEY_ESCAPE break # Remember to remove consoles in reverse order of creation to avoid OOB errors if splash: game.gEngine.console_remove_console(splash_screen) buy_button.destroy_button() sell_button.destroy_button() exit_button.destroy_button() game.gEngine.console_remove_console(compare_window) game.gEngine.console_remove_console(shop_window) game.gEngine.console_remove_console(inventory_window) return
def inventory(con, player, game, width=80, height=43): """ TODO: Add in highlighting for Weapons and Equipment consoles Add in keyboard arrow selection support Add in "drop" mode, drop items from inventory Fix Weapons and Equipment keyboard handling Fix duplication bug when equipping items (fixed) Fix take off weapon confirmation when equipping an item (fixed) 2 handed code needs work (?) keyboard input is sluggish. Might have to update libtcod to fix :param con: Destination console (not used atm) :param player: The main player object :param game: The main game object :param width: width of the inventory screen :param height: height of the inventory screen :return: An item that has been used (potion, scroll, etc..) """ equip_height = 14 wield_height = 8 compare_height = height - (equip_height - wield_height) - (wield_height * 2) r, g, b = libtcod.white equip_y = wield_height compare_y = equip_height + wield_height inventory_window = game.gEngine.console_new(width / 2, height) game.gEngine.console_set_default_foreground(inventory_window, r, g, b) game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2, height, True) game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) equipment_window = game.gEngine.console_new(width / 2, equip_height) game.gEngine.console_set_default_foreground(equipment_window, r, g, b) game.gEngine.console_print_frame(equipment_window, 0, 0, width / 2, equip_height, True) game.gEngine.console_set_default_background(equipment_window, 0, 0, 0) wielded_window = game.gEngine.console_new(width / 2, wield_height) game.gEngine.console_set_default_foreground(wielded_window, r, g, b) game.gEngine.console_print_frame(wielded_window, 0, 0, width / 2, wield_height, True) game.gEngine.console_set_default_background(wielded_window, 0, 0, 0) compare_window = game.gEngine.console_new(width / 2, compare_height) game.gEngine.console_set_default_foreground(compare_window, r, g, b) game.gEngine.console_print_frame(compare_window, 0, 0, width / 2, compare_height, True) game.gEngine.console_set_default_background(compare_window, 0, 0, 0) check_boxes = [] slots = [ 'Torso ', 'Head ', 'Hands ', 'Legs ', 'Feet ', 'Arms ', 'Shoulders', 'Back ' ] #self.buttons.append(Button(self, self.option_labels[0], self.width//6-5, self.height/2-1, True)) exit_button = Button(label='Exit', game=game, x_pos=(width / 2) - 9, y_pos=height - 6, window=inventory_window, dest_x=width / 2, dest_y=0) drop_button = Button(label='Drop', game=game, x_pos=1, y_pos=height - 6, window=inventory_window, dest_x=width / 2, dest_y=0) if len(player.fighter.inventory) == 0: inventory_items = ['Inventory is empty.'] else: inventory_items = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x + 3)) i = color_text(player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[x].item.qty inventory_items.append(i) i_header = 'Inventory' i_header_size = len(i_header) i_header_pos = (width / 4) - (i_header_size / 2) w_header = 'Weapons' w_header_size = len(w_header) w_header_pos = (width / 8) - (w_header_size / 2) e_header = 'Equipment' e_header_size = len(e_header) e_header_pos = (width / 8) - (e_header_size / 2) c_header = 'Compare/Examine' c_header_size = len(c_header) c_header_pos = (width / 8) - (c_header_size / 2) return_item = None key = libtcod.console_check_for_keypress() current_selection = 0 master_check = CheckBox(1, 30, "Check/Uncheck All") while key.vk != libtcod.KEY_ESCAPE: game.gEngine.console_flush() # get input just after flush key = libtcod.console_check_for_keypress(True) mouse = libtcod.mouse_get_status() exit_input = exit_button.display(mouse) drop_input = drop_button.display(mouse) game.gEngine.console_blit(inventory_window, 0, 0, width / 2, height, 0, (width / 2), 0, 1.0, 1.0) game.gEngine.console_blit(wielded_window, 0, 0, width / 2, height, 0, 0, 0, 1.0, 1.0) game.gEngine.console_blit(equipment_window, 0, 0, width / 2, height, 0, 0, equip_y, 1.0, 1.0) game.gEngine.console_blit(compare_window, 0, 0, width / 2, height, 0, 0, compare_y, 1.0, 1.0) game.gEngine.console_clear(inventory_window) game.gEngine.console_clear(wielded_window) game.gEngine.console_clear(equipment_window) game.gEngine.console_clear(compare_window) # set up draw screen r, g, b = libtcod.white game.gEngine.console_set_default_foreground(inventory_window, r, g, b) game.gEngine.console_print_frame(inventory_window, 0, 0, width / 2, height, True) game.gEngine.console_set_default_foreground(equipment_window, r, g, b) game.gEngine.console_print_frame(equipment_window, 0, 0, width / 2, equip_height, True) game.gEngine.console_set_default_foreground(wielded_window, r, g, b) game.gEngine.console_print_frame(wielded_window, 0, 0, width / 2, wield_height, True) game.gEngine.console_set_default_foreground(compare_window, r, g, b) game.gEngine.console_print_frame(compare_window, 0, 0, width / 2, compare_height, True) # ======================================================================== # print inventory # ======================================================================== game.gEngine.console_print(inventory_window, i_header_pos, 0, i_header) letter_index = ord('a') if len(player.fighter.inventory) > 0: y = 1 for i in range(len(inventory_items)): text = ' (' + chr(letter_index) + ') ' + inventory_items[i] if current_selection == y - 1: r, g, b = libtcod.color_lerp( player.fighter.inventory[i].color, libtcod.blue, 0.5) game.gEngine.console_set_default_background( inventory_window, r, g, b) else: game.gEngine.console_set_default_background( inventory_window, 0, 0, 0) game.gEngine.console_print_ex(inventory_window, 1, y + 2, libtcod.BKGND_SET, libtcod.LEFT, text) y += 1 letter_index += 1 game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) game.gEngine.console_print( inventory_window, 1, 31, 'Gold: ' + color_text(str(player.fighter.money), libtcod.gold)) # ======================================================================== # print equipped weapons # ======================================================================== game.gEngine.console_print(wielded_window, w_header_pos, 0, w_header) index = ord('1') if player.fighter.wielded[0] is None: text = '(' + chr(index) + ') ' + 'Left Hand : ' + color_text( 'Empty', libtcod.darker_gray) else: t = color_text(player.fighter.wielded[0].name.capitalize(), player.fighter.wielded[0].color) text = '(' + chr(index) + ') ' + 'Left Hand : ' + t index += 1 game.gEngine.console_print(wielded_window, 1, 2, text) if player.fighter.wielded[1] is None: text = '(' + chr(index) + ') ' + 'Right Hand: ' + color_text( 'Empty', libtcod.darker_gray) else: t = color_text(player.fighter.wielded[1].name.capitalize(), player.fighter.wielded[1].color) text = '(' + chr(index) + ') ' + 'Right Hand: ' + t index += 1 game.gEngine.console_print(wielded_window, 1, 3, text) item = player.fighter.wielded[0] if item: damage = '%dd%d+%d' % (item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub) text = 'Damage (total): ' + color_text(damage, libtcod.green) game.gEngine.console_print(wielded_window, 1, 4, text) accuracy = item.item.equipment.accuracy accuracy += game.player.fighter.get_skill( item.item.equipment.damage_type).get_bonus() text = 'Accuracy (total): ' + color_text(str(accuracy), libtcod.green) game.gEngine.console_print(wielded_window, 1, 5, text) # ======================================================================== # print equipped armor # ======================================================================== game.gEngine.console_print(equipment_window, e_header_pos, 0, e_header) i = 0 game.gEngine.console_set_alignment(equipment_window, libtcod.LEFT) armor_bonus = 0 armor_penalty = 0 for item in player.fighter.equipment: text = '(' + chr(index) + ') ' + slots[i] + ': ' if item is None: text += color_text('Empty', libtcod.darker_gray) else: text += color_text(item.name.capitalize(), item.color) armor_bonus += item.item.equipment.bonus armor_penalty += item.item.equipment.penalty game.gEngine.console_print(equipment_window, 1, i + 2, text) i += 1 index += 1 c = color_text(str(armor_bonus), libtcod.green) text = 'Total Armor Bonus : ' + c game.gEngine.console_print(equipment_window, 1, i + 2, text) c = color_text(str(armor_penalty), libtcod.red) text = 'Total Armor Penalty: -' + c game.gEngine.console_print(equipment_window, 1, i + 3, text) game.gEngine.console_print(compare_window, c_header_pos, 0, c_header) # ======================================================================== # handle mouse input # ======================================================================== mc = master_check.update(mouse, width) master_check.render(inventory_window, game) if not mc: for box in check_boxes: box.update(mouse, width) box.render(inventory_window, game) else: for box in check_boxes: box.set_checked(master_check.get_checked()) box.render(inventory_window, game) # Inventory input if mouse.cx >= width / 2 + 3 and mouse.cx < width - 2: # inventory screen dims if (mouse.cy - 3) < len( player.fighter.inventory) and mouse.cy - 3 >= 0: #if mouse.cy-3 <= len(player.fighter.inventory): item = player.fighter.inventory[mouse.cy - 3] current_selection = mouse.cy - 3 if item.item.equipment: game.gEngine.console_print( compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print( compare_window, 1, 3, 'Type : ' + item.item.equipment.type.capitalize()) if item.item.equipment.type == 'melee': damage = '%dd%d+%d' % ( item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub) game.gEngine.console_print(compare_window, 1, 4, 'Damage : ' + damage) game.gEngine.console_print( compare_window, 1, 5, 'Accuracy: ' + str(item.item.equipment.accuracy)) game.gEngine.console_print( compare_window, 1, 7, 'Skill : ' + item.item.equipment.damage_type) else: game.gEngine.console_print( compare_window, 1, 4, 'Armor : ' + str(item.item.equipment.bonus)) game.gEngine.console_print( compare_window, 1, 5, 'Penalty : ' + str(item.item.equipment.penalty)) game.gEngine.console_print( compare_window, 1, 7, 'Location: ' + item.item.equipment.location.capitalize()) game.gEngine.console_print( compare_window, 1, 6, 'Value : ' + str(item.item.value)) if item.item.spell: game.gEngine.console_print( compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print( compare_window, 1, 3, 'Type : ' + item.item.spell.type.capitalize()) game.gEngine.console_print( compare_window, 1, 4, 'Power : ' + str(item.item.spell.min) + '-' + str(item.item.spell.max)) game.gEngine.console_print( compare_window, 1, 5, 'Range : ' + str(item.item.spell.range)) game.gEngine.console_print( compare_window, 1, 6, 'Radius: ' + str(item.item.spell.radius)) game.gEngine.console_print( compare_window, 1, 7, 'Value : ' + str(item.item.value)) if mouse.lbutton_pressed and item.item.spell: i_n = color_text(item.name.capitalize(), item.color) message = 'Do you want to use %s?' % i_n w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 4, height / 2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: # make sure if the player uses a scroll or potion, we exit inventory d_box.destroy_box() # Remember to remove consoles in reverse order of creation to avoid OOB errors return_item = item break else: d_box.destroy_box() if mouse.lbutton_pressed and item.item.equipment: i_n = color_text(item.name.capitalize(), item.color) message = 'Do you want to put %s on?' % i_n w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 4, height / 2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: item.item.use(game.player.fighter.inventory, game.player, game) d_box.destroy_box() inventory_items = [] check_boxes = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x + 3)) i = color_text( player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[ x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[ x].item.qty inventory_items.append(i) else: current_selection = None # game.gEngine.console_set_default_background(inventory_window, 0, 0, 0) # Wielded elif mouse.cx >= 0 and mouse.cx <= ( (width / 2) - 2): # inventory screen dims if (mouse.cy - 2) < len(player.fighter.wielded): item = player.fighter.wielded[mouse.cy - 2] if item is not None: if item.item.equipment: game.gEngine.console_print( compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print( compare_window, 1, 3, 'Type : ' + item.item.equipment.type.capitalize()) damage = '%dd%d+%d' % ( item.item.equipment.damage.nb_dices, item.item.equipment.damage.nb_faces, item.item.equipment.damage.addsub) game.gEngine.console_print(compare_window, 1, 4, 'Damage : ' + damage) game.gEngine.console_print( compare_window, 1, 5, 'Accuracy: ' + str(item.item.equipment.accuracy)) game.gEngine.console_print( compare_window, 1, 6, 'Value : ' + str(item.item.value)) # Equipment elif (mouse.cy - 2) - equip_y < len(player.fighter.equipment): item = player.fighter.equipment[mouse.cy - 2 - equip_y] if item is not None: if item.item.equipment: game.gEngine.console_print( compare_window, 1, 2, 'Name : ' + color_text(item.name.capitalize(), item.color)) game.gEngine.console_print( compare_window, 1, 3, 'Type : ' + item.item.equipment.type.capitalize()) game.gEngine.console_print( compare_window, 1, 4, 'Armor : ' + str(item.item.equipment.bonus)) game.gEngine.console_print( compare_window, 1, 5, 'Penalty : ' + str(item.item.equipment.penalty)) game.gEngine.console_print( compare_window, 1, 6, 'Value : ' + str(item.item.value)) if mouse.lbutton_pressed and item is not None: i_n = color_text(item.name.capitalize(), item.color) message = 'Do you want to take %s off?' % i_n w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 4, height / 2, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: item.item.equipment.un_equip(game.player, item) d_box.destroy_box() inventory_items = [] check_boxes = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x + 3)) i = color_text( player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color) if player.fighter.inventory[x].item.check_stackable: i += ' (%d)' % player.fighter.inventory[x].item.qty inventory_items.append(i) i = 0 for x in player.fighter.wielded: if x == item: player.fighter.wielded[i] = None i += 1 i = 0 for x in player.fighter.equipment: if x == item: player.fighter.equipment[i] = None i += 1 # keyboard input # keeps similar feel to old inventory if using the keys # keyboard input is sluggish as balls for some reason, need to look into it more index = key.c - ord('a') if key: if index >= 0 and index < len(inventory_items): return_item = player.fighter.inventory[index] break index = key.c - ord('1') if index >= 0 and index <= 1: return_item = player.fighter.wielded[index] break elif index >= 2 and index <= 10: return_item = player.fighter.equipment[index - 2] break if key.vk == libtcod.KEY_DOWN: if current_selection is None: current_selection = 0 current_selection += 1 if current_selection > len(inventory_items): current_selection = 0 if key.vk == libtcod.KEY_UP: if current_selection is None: current_selection = 0 current_selection -= 1 if current_selection < 0: current_selection = len(inventory_items) # ======================================================================== # handle buttons # ======================================================================== for i in drop_input: if i != -1: message = 'Drop all selected items?' w = len(message) + 2 d_box = DialogBox(game, w, 10, width / 2 - w / 2, height / 2 - 5, message, type='option', con=inventory_window) confirm = d_box.display_box() if confirm == 1: d_box.destroy_box() master_check.set_checked(False) items_to_drop = [] for box in check_boxes: if box.get_checked(): items_to_drop.append( player.fighter.inventory[box.y - 3]) for item_to_drop in items_to_drop: if item_to_drop: item_to_drop.objects = game.objects item_to_drop.item.drop(player.fighter.inventory, player, False) item_to_drop.send_to_back() check_boxes = [] inventory_items = [] for x in range(len(player.fighter.inventory)): check_boxes.append(CheckBox(x=1, y=x + 3)) inventory_items.append( color_text( player.fighter.inventory[x].name.capitalize(), player.fighter.inventory[x].color)) else: d_box.destroy_box() # ======================================================================== # handle exit button # ======================================================================== for i in exit_input: if i != -1: key.vk = libtcod.KEY_ESCAPE break # Remember to remove consoles in reverse order of creation to avoid OOB errors drop_button.destroy_button() exit_button.destroy_button() game.gEngine.console_remove_console(compare_window) game.gEngine.console_remove_console(wielded_window) game.gEngine.console_remove_console(equipment_window) game.gEngine.console_remove_console(inventory_window) return return_item