def target_tile(self): # return the position of a tile left-clicked # in player's FOV (optionally in a range), # or None if right-clicked. #display.message("Click a tile to target.") while True: #libtcod.console_flush() libtcod.sys_check_for_event(libtcod.EVENT_ANY, g.key_event_structure, g.mouse_event_structure) # render the screen. this erases the inventory # and shows the names of objects under the mouse. display.render_all() (x, y) = (g.mouse_event_structure.cx, g.mouse_event_structure.cy) if g.mouse_event_structure.rbutton_pressed \ or g.key_event_structure.vk == libtcod.KEY_ESCAPE: return None # cancel if the player right-clicked or pressed Escape # accept the target if the player clicked in FOV # and in case a range is specified, if it's in that range if g.mouse_event_structure.lbutton_pressed: if libtcod.map_is_in_fov(display.fov_map, x, y) \ and (self.max_range is None or self.owner.distance(x, y) <= self.max_range): return (x, y) else: display.message("Out of range.")
def trade(buyer, seller, selected_item, msg, screen): if buyer.gold >= selected_item.cost: display.message(screen, 1, 1, msg) buyer.inventory.append(selected_item) buyer.gold -= selected_item.cost seller.gold += selected_item.cost seller.inventory.remove(selected_item)
def buy(screen, merchant, p, n): r_i = merchant.inventory inp = 0 a_row = 2 for i in r_i: i.cost_str = str(i.cost) g_bool = True while inp != 27 and g_bool: screen.clear() a_row = invlib.arrow(a_row, inp, len(r_i)) if invlib.exit_selected(r_i, inp, a_row): break elif inp == 10: selected_item = r_i[a_row - 2] if p.gold >= selected_item.cost: display.message(screen, 1, 1, "You bought " + selected_item.name) p.inventory.append(selected_item) p.gold -= selected_item.cost r_i.remove(selected_item) else: display.message( screen, 1, 1, "If you want " + selected_item.name + ", you better get more gold") display.display_store( cur_row, a_row, "\"Hello, I am %s, these are my wares.\"" % n, )
def death(self): # the game ended! message('You died!', libtcod.red) g.game_state = 'dead' # for added effect, transform the player into a corpse! self.char = '%' self.color = libtcod.dark_red
def drop(self): # add to the map and remove from the player's inventory. # also, place it at the player's coordinates self.owner.inventory.remove(self) g.items.append(self) self.x = self.owner.x self.y = self.owner.y if self.owner == Object.player: display.message('You dropped a ' + self.name + '.', libtcod.yellow) self.owner = None
def attack(self, target): # a simple formula for attack damage damage = self.power - target.combatant.defense if damage > 0: # make the target take some damage message(self.owner.name.capitalize() + ' attacks ' + target.name \ + ' for ' + str(damage) + ' hit points.') target.combatant.change_hp(self.owner, -damage) else: message(self.owner.name.capitalize() + ' attacks ' + target.name \ + ' but it has no effect!')
def use(self): if self.use_classes is None: if self.owner == Object.player: display.message('The ' + self.name + ' cannot be used.') else: for effect in self.use_classes: effect.strength = self.strength result = Usable.use(self) if result: self.quantity -= 1 if self.quantity <= 0: # destroy after use, unless it was cancelled for some reason self.owner.inventory.remove(self) return result
def pick_up(self, character): # add to player's inventory and remove from the map if character == Object.player \ and len(character.inventory.elements) >= 26: display.message('Your inventory is full, cannot pick up ' + self.name + '.', libtcod.red) else: character.inventory.add(self) self.owner = character self.targeter.owner = character g.items.remove(self) if character == Object.player: display.message('You picked up a ' + self.name + '!', libtcod.green)
def target_closest(self): # find closest enemy up to a maximum range and in the character's FOV closest_enemy = None # start with (slightly more than) maximum range closest_dist = self.max_range + 1 for obj in g.actors: if obj.combatant and not obj == self.owner \ and libtcod.map_is_in_fov(display.fov_map, obj.x, obj.y): # calculate distance between this object and the character dist = self.owner.distance_to(obj) if dist < closest_dist: # it's closer, so remember it closest_enemy = obj closest_dist = dist if closest_enemy is None: # and self.owner == player: display.message("No enemy in range.") return closest_enemy
def add_player(name, index_str): if index_str: player = Player(name, int(index_str)) game.add_player(player) msg = f'Player {name} has been added with index {index_str}' else: player = Player(name) game.add_player(player) msg = f'Player {name} has been added' return display.message(msg, ''), 202
def use(self): target = self.targeter.choose_target() if not target: display.message("Usage cancelled.") else: if not self.use_msg and self.verb and self.noun: self.use_msg = self.verb + " " + self.noun + "." if self.use_msg: display.message(self.owner.name + " " + self.use_msg) for i, usage in enumerate(self.use_classes): if self.effect_verb: new_effect_verb = self.effect_verb[i] else: new_effect_verb = None # initialize the use_class as a new Effect new_effect = usage(turns=self.turns, verb=new_effect_verb) if self.strength: # if strength is applicable new_effect.strength = self.strength new_effect.target = target new_effect.owner = self new_effect.init_turn() return target
def level_up(self): display.message("You leveled up!", libtcod.light_blue) Character.level_up(self)
def assign_shreff(shreff_index_str): game.shreff = game.get_player(index=int(shreff_index_str)) msg = 'Shreff has assigned to player ' + shreff_index_str return display.message(msg, ''), 202
def simulate_game(): game.simulate_start() response = display.message('Game simulated', '') return response, 202
def reset_game(): game.reset() if os.path.isfile('data/current.pkl'): os.remove('data/current.pkl') response = display.message('Game reinitialized', '') return response, 202
def hello(): msg = "Laugh all you want, they are not for sale" return display.message("Not for sale", msg)
def save_game(): with open('data/current.pkl', 'wb') as f_: pickle.dump(game, f_) response = display.message('Game state saved', '') return response, 202
def death(self, killer): display.message("The " + self.owner.name + " is dead.", libtcod.orange) display.message("The " + killer.name + " gains " + str(self.death_xp) \ + " XP.", libtcod.light_violet) killer.xp += self.death_xp
def choose_target(self): # generic target function that handles targeting messages if self.target_msg is not None: display.message(self.target_msg) return self.target_fn()
def handle_keys(self): """Gets and interprets a keypress. Possible return values: True: player took turn, proceed as normal False: player didn't take turn 'exit': exit game """ libtcod.console_flush() libtcod.sys_check_for_event( libtcod.EVENT_MOUSE | libtcod.EVENT_KEY_PRESS, g.key_event_structure, g.mouse_event_structure ) if g.key_event_structure.vk == libtcod.KEY_ENTER \ and g.key_event_structure.lalt: # Alt+Enter: toggle fullscreen libtcod.console_set_fullscreen(not libtcod.console_is_fullscreen()) elif g.key_event_structure.vk == libtcod.KEY_ESCAPE: return 'exit' # exit game def switch_key(keys): return g.key_event_structure.vk in keys if g.game_state is not 'dead': # movement keys up = [libtcod.KEY_UP, libtcod.KEY_KP8] upleft = [libtcod.KEY_KP7] left = [libtcod.KEY_LEFT, libtcod.KEY_KP4] downleft = [libtcod.KEY_KP1] down = [libtcod.KEY_DOWN, libtcod.KEY_KP2] downright = [libtcod.KEY_KP3] right = [libtcod.KEY_RIGHT, libtcod.KEY_KP6] upright = [libtcod.KEY_KP9] # other keys pickup_key = ['g', ','] inventory_key = ['I'] spellbook_key = ['B'] skill_key = ['a'] drop_key = ['d'] char_screen_key = ['c'] movement_keys = up + upleft + left + downleft + \ down + downright + right + upright if switch_key(movement_keys): # player tries to move if self.held: print "You're held in place; you can't move." # trying to move shouldn't take a turn if it fails return False if switch_key(up): return self.move_or_attack( 0, -1) elif switch_key(upleft): return self.move_or_attack(-1, -1) elif switch_key(left): return self.move_or_attack(-1, 0) elif switch_key(downleft): return self.move_or_attack(-1, 1) elif switch_key(down): return self.move_or_attack( 0, 1) elif switch_key(downright): return self.move_or_attack( 1, 1) elif switch_key(right): return self.move_or_attack( 1, 0) elif switch_key(upright): return self.move_or_attack( 1, -1) else: # test for other keys key_char = chr(g.key_event_structure.c) if key_char in pickup_key: # pick up an item for item in g.items: # look for an item in the player's tile if item.x == self.x and item.y == self.y: item.pick_up(self) break if key_char in inventory_key: # show the inventory; if an item is selected, use it chosen_item = self.inventory.choice_menu(key_char) if chosen_item is not None: if chosen_item.use(): return True if key_char in spellbook_key: # show the list of spells. If a spell is selected, use it. chosen_spell = self.caster.spellbook.choice_menu() if chosen_spell is not None: if (self.caster.mp - chosen_spell.mp_cost) < 0: display.message( "You don't have enough MP to cast that spell.", libtcod.pink) else: if self.caster.cast_spell(chosen_spell): return True if key_char in skill_key: chosen_skill = self.skills.choice_menu() if chosen_skill is not None: if chosen_skill.use(): return True if key_char in drop_key: # show the inventory; if an item is selected, drop it chosen_item = self.inventory.choice_menu(key_char) if chosen_item is not None: chosen_item.drop() if key_char in char_screen_key: # show the character screen display.char_info_window(self) # wait for keypress libtcod.sys_wait_for_event(libtcod.KEY_PRESSED, g.key_event_structure, g.mouse_event_structure, True) return False
lines.append(" ".join(tmp)) tmp = [i] tmp_len = len(i) + 1 if lines[-1] != tmp: lines.append(" ".join(tmp)) text = "\n".join(lines) return text import sys try: from wikiparser import tell_me_about from display import message except: print "Please Get The BLUG.in's Modules" try: word = sys.argv[1] except: word = raw_input("Please Enter A Word To Look Up") entry = tell_me_about(word) if entry: message("BDict - " + entry.title, format_text(entry.data)) else: message("Sorry","I do not know what %s means" % word)
def run(self): display.welcome() while True: player_json = self._call_api(self.api.get_player_info) self.logger.debug(player_json) self.player = Player.from_json(player_json) display.player_info(self.player) planets = self.potential_planets() if len(planets) == 0: display.message('No more planets to conquer. Exiting...') break display.planets(planets) self.planet = self.best_planet(planets) self.zone = self.planet.best_zone() # Join the best planet if we need to. if self.player.active_planet != self.planet.id: # Leave the current Zone if we've already joined one. if self.player.active_zone_game is not None: self.logger.debug( f'Leaving Zone {self.player.active_zone} ({self.player.active_zone_game}) before leaving Planet {self.player.active_planet}' ) self._call_api(self.api.leave_game, self.player.active_zone_game) # Leave the current Planet if we've already joined one. if self.player.active_planet is not None: self.logger.debug( f'Leaving Planet {self.player.active_planet} before joining Planet {self.planet.id}' ) self._call_api(self.api.leave_game, self.player.active_planet) self.logger.debug(f'Joining planet {self.planet.id}') self._call_api(self.api.join_planet, self.planet.id) # Join the best zone if we aren't already there. if self.player.active_zone_game != self.zone.game_id: # Leave the current Zone if we've already joined one. if self.player.active_zone_game is not None: self.logger.debug( f'Leaving Zone {self.player.active_zone} ({self.player.active_zone_game}) on Planet {self.player.active_planet}' ) self._call_api(self.api.leave_game, self.player.active_zone_game) if self.zone.boss_active: self.logger.debug( f'Joining boss Zone {self.zone.id} on Planet {self.planet.id}' ) self._call_api(self.api.join_boss_zone, self.zone.id) else: self.logger.debug( f'Joining Zone {self.zone.id} on Planet {self.planet.id}' ) self._call_api(self.api.join_zone, self.zone.id) # NOTE: This is a pretty dirty hack, but it needs to happen # to make sure we don't report a zone score too early. The # time in zone is important when we choose the same zone # after restarting the bot, resulting in being out of sync # with the server. self.player.time_in_zone = 0 display.join_zone_status(self.player, self.planet, self.zone) if self.zone.boss_active: self.play_boss_zone() else: self.play_zone()
def kill_player(index_str): player = game.get_player(index=int(index_str)) player.is_dead = True msg = f'Player {index_str} has been killed' return display.message(msg, ''), 202
block_sight_map[y][x] = True if g.level_map[y][x].blocked: block_map[y][x] = True if g.level_map[y][x].block_sight: block_sight_map[y][x] = True libtcod.map_set_properties(display.fov_map, x, y, not block_sight_map[y][x], not block_map[y][x]) g.fov_recompute = True g.game_state = 'playing' player_action = None #display.message('Fight your way through the forest.', libtcod.red) display.message('Use the numpad to move.', libtcod.red) display.message('@ = you, ! = item, o/t = enemy.', libtcod.red) display.message('get-item: g, drop-item: d, inventory: I, ', libtcod.red) display.message('spellbook: B, abilities: a', libtcod.red) ############# # MAIN LOOP # ############# while not libtcod.console_is_window_closed() and g.game_state is not 'exit': # render the screen display.render_all() # step to next round player_action = Ticker.ticker.next_turn() if player_action == 'exit':
def play_boss_zone(self): display.message('Starting boss battle!') use_heal = 0 damage_to_boss = 0 damage_taken = 0 report_damage_wait = 5 healing_cooldown = timedelta(seconds=120) next_heal = datetime.now() while True: self.logger.debug( f'Reporting damage_to_boss {damage_to_boss} - use_heal {use_heal} - damage_taken {damage_taken}' ) resp = self._call_api(self.api.report_boss_damage, use_heal, damage_to_boss, damage_taken) if use_heal == 1: use_heal = 0 next_heal = datetime.now() + healing_cooldown waiting_for_players = resp.get('waiting_for_players') if waiting_for_players: self.logger.debug( f'Waiting for players, sleeping for {report_damage_wait} seconds...' ) time.sleep(report_damage_wait) continue boss_status = resp.get('boss_status', None) if boss_status is None: self.logger.debug( f'Boss status empty, sleeping for {report_damage_wait} seconds...' ) time.sleep(report_damage_wait) continue # Boss is ready to fight. Start doing damage. damage_to_boss = 1 display.boss_progress(resp, self.account_id) should_heal = False team = boss_status.get('boss_players') total_hp_percent = 0.0 for player in team: hp = player.get('hp') max_hp = player.get('max_hp') total_hp_percent += hp / max_hp if player.get('accountid') == self.account_id: if hp <= 0: display.message('!!! Game Over. You are dead! :( !!!') return # Checking if should_heal is false prevents it from being # disabled again if a player before the last in the list needs # healing. if not should_heal: should_heal = max_hp - hp > 10000 avg_hp_percent = total_hp_percent / len(team) remaining_cooldown = next_heal - datetime.now() # Need to check if the remaining cooldown is less than 0 seconds, # otherwise it will show something like 86000 seconds remaining. if remaining_cooldown < timedelta(0): seconds = 0 else: seconds = remaining_cooldown.seconds display.message( f'Average player health: {avg_hp_percent*100:.2f}% - Heal cooldown: {seconds} seconds' ) if should_heal and next_heal <= datetime.now() + timedelta( seconds=report_damage_wait): use_heal = 1 display.message('>>> Using Heal <<<') if resp.get('game_over', False): display.message('Game Over! Leaving boss game...') break time.sleep(report_damage_wait) # Wait a bit before leaving. Some times we check the planets again too # fast and try to join a boss room that is closed and we crash. time.sleep(5)