def territorial_monster(monster, player, metadata): """ Move randomly but near home until approached or hurt """ _spotting(monster, metadata) if metadata.active_turns > 0: metadata.active_turns -= 1 # In the grander scheme of things this should reset to territorial # after killing its prey, but that doesn't matter so long as we only # go hostile on the player. if monster.fighter.last_attacker: monster.ai = AI(hostile_monster, hostile_monster_metadata(monster.fighter.last_attacker)) monster.ai.set_owner(monster) return monster.ai.take_turn(player) if monster.distance_to_obj(player) < metadata.radius: log.message(monster.name.capitalize() + ' decides ' + player.name + ' is too close!', libtcod.red) monster.ai = AI(hostile_monster, hostile_monster_metadata(player)) monster.ai.set_owner(monster) return monster.ai.take_turn(player) while True: trial_dir = actions.random_direction() candidate = monster.pos + trial_dir cand_dist = candidate.distance(metadata.home) if ((cand_dist < metadata.radius or cand_dist < monster.pos.distance(metadata.home)) and not monster.current_map.is_blocked_at(candidate)): actions.move(monster, trial_dir) return
def territorial_monster(monster, player, metadata): """ Move randomly but near home until approached or hurt """ _spotting(monster, metadata) if metadata.active_turns > 0: metadata.active_turns -= 1 # In the grander scheme of things this should reset to territorial # after killing its prey, but that doesn't matter so long as we only # go hostile on the player. if monster.fighter.last_attacker: monster.ai = AI( hostile_monster, hostile_monster_metadata(monster.fighter.last_attacker)) monster.ai.set_owner(monster) return monster.ai.take_turn(player) if monster.distance_to_obj(player) < metadata.radius: log.message( monster.name.capitalize() + ' decides ' + player.name + ' is too close!', libtcod.red) monster.ai = AI(hostile_monster, hostile_monster_metadata(player)) monster.ai.set_owner(monster) return monster.ai.take_turn(player) while True: trial_dir = actions.random_direction() candidate = monster.pos + trial_dir cand_dist = candidate.distance(metadata.home) if ((cand_dist < metadata.radius or cand_dist < monster.pos.distance(metadata.home)) and not monster.current_map.is_blocked_at(candidate)): actions.move(monster, trial_dir) return
def ignoring_monster(monster, player, metadata): """ A creature that moves randomly (q.v. confused_monster) and ignores the player unless hurt, in which case it becomes hostile or scared. """ _spotting(monster, metadata) if metadata.active_turns > 0: metadata.active_turns -= 1 if monster.fighter.last_attacker: if monster.fighter.unarmed_damage > 2: monster.ai = AI( hostile_monster, hostile_monster_metadata(monster.fighter.last_attacker)) else: monster.old_ai = monster.ai monster.ai = AI( fleeing_monster, hostile_monster_metadata(monster.fighter.last_attacker)) monster.ai.set_owner(monster) return monster.ai.take_turn(player) # TODO: this movement may fail, so the monster will appear to # move less when in constricted quarters. if metadata.on_idle and not metadata.on_idle(player): actions.move(monster, actions.random_direction())
def confused_monster(monster, player, metadata): if metadata.num_turns > 0: actions.move(monster, random_direction()) metadata.num_turns -= 1 else: # Restore the previous AI (this one will be deleted # because it's not referenced anymore) monster.ai = metadata.old_ai log.message(monster.name.capitalize() + ' is no longer confused!', libtcod.red)
def player_move_or_attack(player, direction, try_running): """ Returns true if the player makes an attack or moves successfully; false if the attempt to move fails. """ goal = player.pos + direction if (goal.x < 0 or goal.y < 0 or goal.x >= player.current_map.width or goal.y >= player.current_map.height): log.message(player.current_map.out_of_bounds(goal)) return False # Is there an attackable object? target_obj = None for obj in player.current_map.objects: if obj.fighter and obj.pos == goal: target_obj = obj break if target_obj is not None: # Make sure we're not going up or down a cliff if not player.current_map.is_blocked_from( player.pos, target_obj.pos, ignore=target_obj): actions.attack(player.fighter, target_obj) return True return False # Is there an interactable object? target_obj = None for obj in player.current_map.objects: if obj.interactable and obj.pos == goal: target_obj = obj break if target_obj is not None: target_obj.interactable.use_function(player, target_obj) return True elevation_before_moving = player.current_map.elevation( player.pos.x, player.pos.y) if actions.move(player, direction): player.current_map.fov_needs_recompute = True if player.current_map.xp_visit: player.current_map.xp_visit(player.current_map, player) if player.current_map.is_outdoors: new_region = player.current_map.region[player.pos.x][player.pos.y] new_elevation = player.current_map.region_elevations[new_region] if new_elevation != elevation_before_moving: player.current_map.fov_elevation_changed = True player.fighter.exhaustion += actions.CLIMB_EXHAUSTION if try_running: player.game_state = 'running' player.run_direction = direction # Automatically sweep up ammunition after a shooting spree ammo_eq = actions.get_equipped_in_slot(player, 'quiver') if ammo_eq: for obj in player.current_map.objects: if (obj.pos == player.pos and obj.item and obj.item.can_combine(ammo_eq.owner)): actions.pick_up(player, obj) return True return False
def player_move_or_attack(player, direction, try_running): """ Returns true if the player makes an attack or moves successfully; false if the attempt to move fails. """ goal = player.pos + direction if (goal.x < 0 or goal.y < 0 or goal.x >= player.current_map.width or goal.y >= player.current_map.height): log.message(player.current_map.out_of_bounds(goal)) return False # Is there an attackable object? target = None for object in player.current_map.objects: if object.fighter and object.pos == goal: target = object break if target is not None: actions.attack(player.fighter, target) return True else: if actions.move(player, direction): player.current_map.fov_needs_recompute = True if try_running: player.game_state = 'running' player.run_direction = direction return True return False
def move_into() -> Parser: """ :return: a parser that parses going to a location and then through the door, e.g. 'go into the second room'. """ return non_consuming(move()) \ .then_ignore(through_door()) \ .map_parsed(lambda move: Composite([move, ThroughDoor(ObjectRelativeDirection.VICINITY)]))
def player_move_or_attack(player, direction, try_running): """ Returns true if the player makes an attack or moves successfully; false if the attempt to move fails. """ goal = player.pos + direction if (goal.x < 0 or goal.y < 0 or goal.x >= player.current_map.width or goal.y >= player.current_map.height): log.message(player.current_map.out_of_bounds(goal)) return False # Is there an attackable object? target_obj = None for obj in player.current_map.objects: if obj.fighter and obj.pos == goal: target_obj = obj break if target_obj is not None: # Make sure we're not going up or down a cliff if not player.current_map.is_blocked_from(player.pos, target_obj.pos, ignore=target_obj): actions.attack(player.fighter, target_obj) return True return False # Is there an interactable object? target_obj = None for obj in player.current_map.objects: if obj.interactable and obj.pos == goal: target_obj = obj break if target_obj is not None: target_obj.interactable.use_function(player, target_obj) return True elevation_before_moving = player.current_map.elevation(player.pos.x, player.pos.y) if actions.move(player, direction): player.current_map.fov_needs_recompute = True if player.current_map.xp_visit: player.current_map.xp_visit(player.current_map, player) if player.current_map.is_outdoors: new_region = player.current_map.region[player.pos.x][player.pos.y] new_elevation = player.current_map.region_elevations[new_region] if new_elevation != elevation_before_moving: player.current_map.fov_elevation_changed = True player.fighter.exhaustion += actions.CLIMB_EXHAUSTION if try_running: player.game_state = 'running' player.run_direction = direction # Automatically sweep up ammunition after a shooting spree ammo_eq = actions.get_equipped_in_slot(player, 'quiver') if ammo_eq: for obj in player.current_map.objects: if (obj.pos == player.pos and obj.item and obj.item.can_combine(ammo_eq.owner)): actions.pick_up(player, obj) return True return False
def userinput(player, room): do = input("What do I want to do?") if do == "move": actions.move(player, room) elif do == "break": actions.destroy(player, room) elif do == "status": actions.status(player) elif do == "quit": actions.quit(player) else: print("I don't know how to do that.")
def ignoring_monster(monster, player, metadata): """ A creature that moves randomly (q.v. confused_monster) and ignores the player unless hurt, in which case it becomes hostile or scared. """ _spotting(monster, metadata) if metadata.active_turns > 0: metadata.active_turns -= 1 if monster.fighter.last_attacker: if monster.fighter.unarmed_damage > 2: monster.ai = AI(hostile_monster, hostile_monster_metadata(monster.fighter.last_attacker)) else: monster.old_ai = monster.ai monster.ai = AI(fleeing_monster, hostile_monster_metadata(monster.fighter.last_attacker)) monster.ai.set_owner(monster) return monster.ai.take_turn(player) # TODO: this movement may fail, so the monster will appear to # move less when in constricted quarters. if metadata.on_idle and not metadata.on_idle(player): actions.move(monster, actions.random_direction())
def player_move_or_attack(player, dx, dy): x = player.x_pos + dx y = player.y_pos + dy target = None for entity in player.current_map.map_entities: if entity.fighter and entity.x_pos == x and entity.y_pos == y: target = entity break if target is not None: actions.attack(player.fighter, target) else: if actions.move(player, dx, dy): player.current_map.fov_needs_recompute = True
def action_detail(request): if request.method == 'POST': serializer = ActionSerializer(data=request.data) if serializer.is_valid(): tile_acting = serializer.validated_data['tile_acting'] tile_effected = serializer.validated_data['tile_effected'] map = serializer.validated_data['map'] action_type = serializer.validated_data['action_type'] # Validation rules for suspicious calls if tile_acting.owner != request.user: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if tile_acting.map != map or tile_effected.map != map: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) if action_type == "ATT": tile_acting, tile_effected = attack(map, tile_acting, tile_effected) # if tile_effected.tiles == 0, prompt for movement action details on UI if action_type == "MOVE": units_to_move = serializer.validated_data['units'] tile_acting, tile_effected = move(map, tile_acting, tile_effected, units_to_move) serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def parse_args(user_input, player): """ Args: user_input (string): raw string of user input player (Player object): object of active player """ # Accounting for null input if user_input.strip() == "": print(random.choice(CONFUSED_RESPONSES)) return # Split string into list of strings (separated by spaces) list_of_input = user_input.split() # Splitting command into a verb and its following argument command = list_of_input[0].lower() if len(list_of_input) > 1: arguments = [x.lower() for x in list_of_input[1:]] # Formatting each argument if command == "pick" and arguments[0] == "up": if len(arguments) > 1: actions.pick_up(player, "".join(arguments[1:]), " ".join(arguments[1:])) else: print("What do you want to pick up?") return elif command == "use" and ("on" in arguments): # Deals with "use ___ on ___ " input actions.use_on(arguments, player) return elif command == "interact" and (arguments[0] == "with"): # Deals with "interact with _____ " input if len(arguments) < 2: # If user just says "interact with" print("Specify what you'd like to interact with.") else: actions.interact_with(arguments[1:], player) return raw_argument = " ".join(list_of_input[1:]) # Here's the raw argument argument = (" ".join(arguments)) # Turning into name format. else: argument = "" raw_argument = "" if command == "go" or command == "move": actions.move(argument, player) elif command in ["east", "west", "north", "south"]: actions.move(command, player) elif command == "observe" and argument == "" or command == "look" and (argument == "around" or argument == ""): actions.examine_surroundings(player) elif command == "inventory" or (command == "view" and argument == "inventory") or (command == "i" and argument==""): actions.print_inventory(player) elif command == "where" and argument == "am i" or (command == "location" and argument == ""): actions.where(player) elif command == "drop": actions.drop(player, argument, raw_argument) elif command == "grab" or command == "take" or command == "get": actions.pick_up(player, argument, raw_argument) elif command == "examine": actions.examine(player, arguments, raw_argument) elif command == "enter": actions.enter(player, argument, raw_argument) elif (command == "kill" and argument == "myself") or (command == "kms"): actions.kill_player(player) elif command == "drink": actions.drink(player, argument, raw_argument) elif command == "eat": actions.eat(player, argument, raw_argument) elif command == "uuddlrlrbastart": # Konami code print("Nice try, bud.") else: print(random.choice(CONFUSED_RESPONSES)) return
def on_press(key): # this is the input function of the program # we give them the "pointer" and concat what they are typing onto it pointer = ("%s>%s" % (fg('white'), attr('reset'))) world = utils.Variables.world global currentlyTyping try: key = "{0}".format(key.char) if (key == "T" or key == "t" or currentlyTyping): if (currentlyTyping): # after pressing chat key it starts adding characters utils.Variables.currentlyTyping += key currentlyTyping = True print("\033[A \033[A") print(pointer + " {0}".format(utils.Variables.currentlyTyping)) elif (key == "Q" or key == "q"): action = actions.destroy() return False elif (key == "E" or key == "e"): action = actions.place() return False elif (key == "W" or key == "w"): command = ["move", "forward"] actions.move(command, world) return False elif (key == "S" or key == "s"): command = ["move", "down"] actions.move(command, world) return False elif (key == "A" or key == "a"): command = ["move", "left"] actions.move(command, world) return False elif (key == "D" or key == "d"): command = ["move", "right"] actions.move(command, world) return False except AttributeError: # key pressed was nonalphanumeric, check for movement. key = "{0}".format(key) if (key == "Key.ctrl_l" or key == "key.ctrl_r"): # control key, exit program sys.exit() if (key == "Key.enter"): # execute whatever command is in terminal currently currentlyTyping = False command = utils.Variables.currentlyTyping.split(" ") utils.Variables.currentlyTyping = "" if (command[0] == "move"): # inputted command is move # it default updates the player in the variables so no reassign needed. worked = actions.move(command, world) if (not worked): print("\nEror executing command %s." % command[0]) time.sleep(3) print("\033[A \033[A") print("\033[A \033[A") return True else: return False elif (command[0] == "chop"): worked = actions.chop(world) if (not worked): print("\nEror executing command %s." % command[0]) time.sleep(3) print("\033[A \033[A") print("\033[A \033[A") return True else: return False elif (command[0] == "save"): worked = actions.save(world) if (not worked): print("\nEror executing command %s." % command[0]) time.sleep(3) print("\033[A \033[A") print("\033[A \033[A") return True else: return False elif (command[0] == "exit" or command[0] == "quit"): actions.save(world) os._exit(1) elif (key == "Key.space"): utils.Variables.currentlyTyping += ' ' elif (key == "Key.backspace"): utils.Variables.currentlyTyping = utils.Variables.currentlyTyping[: -1] print("\033[A \033[A") print(pointer + " {0}".format(utils.Variables.currentlyTyping)) elif (key == "Key.up"): if (utils.Variables.currentlyUsing == 0): return utils.Variables.currentlyUsing -= 1 return False elif (key == "Key.down"): if (utils.Variables.currentlyUsing == 5): return utils.Variables.currentlyUsing += 1 return False
def move(self, name_obj, to_location): actions.move(self, name_obj, to_location)
def main(): player_number = input("How many players(2-6)? ") if (player_number == "quit"): print("Thankyou for playing, Goodbye!") exit(0) PLAYER_NUMBER = int(player_number) while (PLAYER_NUMBER < 2 or PLAYER_NUMBER > 6): PLAYER_NUMBER = int(input("Invalid input: ")) #Enter how much money each player will have player_money = input("How much money will each player start out with? ") if (player_money == "quit"): print("Thankyou for playing, Goodbye!") exit(0) player_money = int(player_money) while (player_money < 1 or player_money > 1000000): player_number = int( input( "Invalid input, please choose positive number from 1 to 1000000: " )) print("Then let's get started!") #DONE #Player vector player_vector = {} #Declare number of players(loop through if you want after finished) if (PLAYER_NUMBER == 2): Player1 = player.Player(player_money, "Player1", 0) player_vector[0] = Player1 Player2 = player.Player(player_money, "Player2", 0) player_vector[1] = Player2 elif (PLAYER_NUMBER == 3): Player1 = player.Player(player_money, "Player1", 0) player_vector[0] = Player1 Player2 = player.Player(player_money, "Player2", 0) player_vector[1] = Player2 Player3 = player.Player(player_money, "Player3", 0) player_vector[2] = Player3 elif (PLAYER_NUMBER == 4): Player1 = player.Player(player_money, "Player1", 0) player_vector[0] = Player1 Player2 = player.Player(player_money, "Player2", 0) player_vector[1] = Player2 Player3 = player.Player(player_money, "Player3", 0) player_vector[2] = Player3 Player4 = player.Player(player_money, "Player4", 0) player_vector[3] = Player4 elif (PLAYER_NUMBER == 5): Player1 = player.Player(player_money, "Player1", 0) player_vector[0] = Player1 Player2 = player.Player(player_money, "Player2", 0) player_vector[1] = Player2 Player3 = player.Player(player_money, "Player3", 0) player_vector[2] = Player3 Player4 = player.Player(player_money, "Player4", 0) player_vector[3] = Player4 Player5 = player.Player(player_money, "Player5", 0) player_vector[4] = Player5 elif (PLAYER_NUMBER == 6): Player1 = player.Player(player_money, "Player1", 0) player_vector[0] = Player1 Player2 = player.Player(player_money, "Player2", 0) player_vector[1] = Player2 Player3 = player.Player(player_money, "Player3", 0) player_vector[2] = Player3 Player4 = player.Player(player_money, "Player4", 0) player_vector[3] = Player4 Player5 = player.Player(player_money, "Player5", 0) player_vector[4] = Player5 Player6 = player.Player(player_money, "Player6", 0) player_vector[5] = Player6 user_input = "" #Set game counter game_counter = 0 while (user_input != "quit"): pot = 0 player_vector_index = 0 if (game_counter == 0): #Draws the hands of Players, puts them into a (player_number * 2) matrix #Later try to consolidate the 3 loops into 2 hands = [["", ""], ["", ""], ["", ""], ["", ""], ["", ""], ["", ""]] card_index = 0 i = 0 while (i < 2): card_index = 0 while (card_index < PLAYER_NUMBER): hands[card_index][i] = cards.draw(deck, cards_dealt) card_index = card_index + 1 i = i + 1 #Sets the hands of all players using hands matrix #Sets the initial status to 1 hand_index = 0 while (hand_index < PLAYER_NUMBER): player_vector[player_vector_index].setHand( hands[hand_index][0], hands[hand_index][1]) player_vector[player_vector_index].setStatus(1) hand_index = hand_index + 1 player_vector_index = player_vector_index + 1 #Starts game play #Initial round of betting before the flop players_moved = 0 previous = -2 player_vector_index = 0 player_round_number = PLAYER_NUMBER while (players_moved != player_round_number): print(player_vector[player_vector_index].getName()) round_bet = actions.move(player_vector[player_vector_index], previous) if (round_bet > 0): pot = pot + round_bet previous = round_bet players_moved = 1 elif (round_bet == 0): previous = round_bet players_moved = players_moved + 1 elif (round_bet == -1): previous = round_bet player_round_number = player_round_number - 1 if (player_round_number == 1): actions.endRound(player_vector, False) continue