Exemplo n.º 1
0
 def create_npc(self, game, action):
     """Creates an NPC object and adds it to the game's current list of NPC's.
 
     :param game: The main game object that contains all the game's variables.
     :param action: The action (tuple) retrieved from the database that contains the action's
         parameters
 
     :type game: core.tools.Control
     :type action: Tuple
 
     :rtype: None
     :returns: None
 
     Valid Parameters: name,tile_pos_x,tile_pos_y,animations,behavior
         
     **Examples:**
 
     >>> action
     ('create_npc', 'Oak,1,5,oak,wander', '1', 6)
 
     """
 
     # Get a copy of the world state.
     world = game.state_dict["WORLD"]
 
     # Get the npc's parameters from the action
     parameters = action[1].split(",")
     name = str(parameters[0])
     tile_pos_x = int(parameters[1])
     tile_pos_y = int(parameters[2])
     animations = str(parameters[3])
     behavior = str(parameters[4])
 
     # Create a new NPC object
     npc = player.Npc(sprite_name=animations, name=name)
         
     # Set the NPC object's variables
     npc.tile_pos = [tile_pos_x, tile_pos_y]
     npc.behavior = behavior
     npc.ai = ai.AI()
     npc.scale_sprites(world.scale)
 
     # Set the NPC's pixel position based on its tile position, tile size, and
     # current global_x/global_y variables
     npc.position = [(tile_pos_x * world.tile_size[0]) + world.global_x,
                     (tile_pos_y * world.tile_size[1]) + world.global_y]
 
         
     # Add the NPC to the game's NPC list
     world.npcs.append(npc)
Exemplo n.º 2
0
    def start(self):
        # Get a copy of the world state.
        world = self.game.get_state_name("WorldState")
        if not world:
            return

        # Get the npc's parameters from the action
        slug = self.parameters.npc_slug
        tile_pos_x = self.parameters.tile_pos_x
        tile_pos_y = self.parameters.tile_pos_y
        animations = self.parameters.animations
        behavior = self.parameters.behavior

        # Ensure that the NPC doesn't already exist on the map.
        if slug in world.npcs:
            return

        # Create a new NPC object
        npc = player.Npc(sprite_name=animations, slug=slug)

        # Set the NPC object's variables
        npc.tile_pos = [tile_pos_x, tile_pos_y]
        npc.behavior = behavior
        npc.ai = ai.AI()
        npc.scale_sprites(prepare.SCALE)
        npc.walkrate *= prepare.SCALE
        npc.runrate *= prepare.SCALE
        npc.moverate = npc.walkrate

        # Set the NPC's pixel position based on its tile position, tile size, and
        # current global_x/global_y variables
        npc.position = [(tile_pos_x * world.tile_size[0]) + world.global_x,
                        (tile_pos_y * world.tile_size[1]) +
                        (world.global_y - world.tile_size[1])]

        # Add the NPC to the game's NPC list
        world.npcs[slug] = npc
Exemplo n.º 3
0
    def create_npc(self, game, action):
        """Creates an NPC object and adds it to the game's current list of NPC's.

        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters

        :type game: core.control.Control
        :type action: Tuple

        :rtype: None
        :returns: None

        Valid Parameters: name,tile_pos_x,tile_pos_y,animations,behavior

        **Examples:**

        >>> action.__dict__
        {
            "type": "create_npc",
            "parameters": [
                "Oak",
                "1",
                "5",
                "oak",
                "wander"
            ]
        }

        """
        # Get a copy of the world state.
        world = game.get_state_name("WorldState")
        if not world:
            return

        # Get the npc's parameters from the action
        name = str(action.parameters[0])
        tile_pos_x = int(action.parameters[1])
        tile_pos_y = int(action.parameters[2])
        animations = str(action.parameters[3])
        behavior = str(action.parameters[4])

        # Ensure that the NPC doesn't already exist on the map.
        event_engine = game.event_engine
        npc_exists = Condition("npc_exists", [name], 1, 1, "is", 0, 0)
        if event_engine.conditions["npc_exists"]["method"](game, npc_exists):
            return

        # Create a new NPC object
        npc = player.Npc(sprite_name=animations, name=name)

        # Set the NPC object's variables
        npc.tile_pos = [tile_pos_x, tile_pos_y]
        npc.behavior = behavior
        npc.ai = ai.AI()
        npc.scale_sprites(world.scale)
        npc.walkrate *= world.scale
        npc.runrate *= world.scale
        npc.moverate = npc.walkrate

        # Set the NPC's pixel position based on its tile position, tile size, and
        # current global_x/global_y variables
        npc.position = [(tile_pos_x * world.tile_size[0]) + world.global_x,
                        (tile_pos_y * world.tile_size[1]) +
                        (world.global_y - world.tile_size[1])]

        # Add the NPC to the game's NPC list
        world.npcs.append(npc)
        return npc
Exemplo n.º 4
0
    def start_battle(self, game, action, contexts):
        """Start a battle and switch to the combat module. The parameters must contain an NPC slug
        in the NPC database.

        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters

        :type game: core.control.Control
        :type action: Tuple

        :rtype: None
        :returns: None

        Valid Parameters: npc_slug

        **Examples:**

        >>> action.__dict__
        {
            "type": "start_battle",
            "parameters": [
                "npc_hiker1"
            ]
        }

        """
        # Don't start a battle if we don't even have monsters in our party yet.
        if not self.check_battle_legal(game.player1):
            return False

        # Stop movement and keypress on the server.
        if game.isclient or game.ishost:
            game.client.update_player(game.player1.facing,
                                      event_type="CLIENT_START_BATTLE")

        # Start combat
        npc_slug = action.parameters[0]

        # TODO: This should *really* be handled in the Npc initializer
        # Create an NPC object that will be used as our opponent
        npc = player.Npc(slug=npc_slug)

        # Look up the NPC's details from our NPC database
        npcs = db.JSONDatabase()
        npcs.load("npc")
        npc_details = npcs.database['npc'][npc_slug]

        # Set the NPC object with the details fetched from the database.
        npc.name = npc_details['name']

        # Set the NPC object's AI model.
        npc.ai = ai.AI()

        # Look up the NPC's monster party
        npc_party = npc_details['monsters']

        # Look up the monster's details
        monsters = db.JSONDatabase()
        monsters.load("monster")

        # Look up each monster in the NPC's party
        for npc_monster_details in npc_party:
            results = monsters.database['monster'][
                npc_monster_details['monster']]

            # Create a monster object for each monster the NPC has in their party.
            # TODO: unify save/load of monsters
            current_monster = monster.Monster()
            current_monster.load_from_db(npc_monster_details['monster'])
            current_monster.name = npc_monster_details['name']
            current_monster.level = npc_monster_details['level']
            current_monster.hp = npc_monster_details['hp']
            current_monster.current_hp = npc_monster_details['hp']
            current_monster.attack = npc_monster_details['attack']
            current_monster.defense = npc_monster_details['defense']
            current_monster.speed = npc_monster_details['speed']
            current_monster.special_attack = npc_monster_details[
                'special_attack']
            current_monster.special_defense = npc_monster_details[
                'special_defense']
            current_monster.experience_give_modifier = npc_monster_details[
                'exp_give_mod']
            current_monster.experience_required_modifier = npc_monster_details[
                'exp_req_mod']

            current_monster.type1 = results['types'][0]

            current_monster.set_level(current_monster.level)

            if len(results['types']) > 1:
                current_monster.type2 = results['types'][1]

            current_monster.load_sprite_from_db()

            pound = technique.Technique('technique_pound')

            current_monster.learn(pound)

            # Add our monster to the NPC's party
            npc.monsters.append(current_monster)

        # Add our players and setup combat
        game.push_state("CombatState",
                        players=(game.player1, npc),
                        combat_type="trainer")

        # Start some music!
        logger.info("Playing battle music!")
        filename = "147066_pokemon.ogg"

        mixer.music.load(prepare.BASEDIR + "resources/music/" + filename)
        mixer.music.play(-1)
Exemplo n.º 5
0
    def random_encounter(self, game, action, contexts):
        """Randomly starts a battle with a monster defined in the "encounter" table in the
        "monster.db" database. The chance that this will start a battle depends on the
        "encounter_rate" specified in the database. The "encounter_rate" number is the chance
        walking in to this tile will trigger a battle out of 100.

        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters

        :type game: core.control.Control
        :type action: Tuple

        :rtype: None
        :returns: None

        Valid Parameters: encounter_slug

        """
        player1 = game.player1

        # Don't start a battle if we don't even have monsters in our party yet.
        if not self.check_battle_legal(player1):
            return False

        # Get the parameters to determine what encounter group we'll look up in the database.
        encounter_slug = action.parameters[0]

        # Look up the encounter details
        monsters = db.JSONDatabase()
        monsters.load("encounter")
        monsters.load("monster")

        # Keep an encounter variable that will let us know if we're going to start a battle.
        encounter = None

        # Get all the monsters associated with this encounter.
        encounters = monsters.database['encounter'][encounter_slug]['monsters']

        for item in encounters:
            # Perform a roll to see if this monster is going to start a battle.
            roll = random.randrange(1, 1000)
            if roll <= int(item['encounter_rate']):
                # Set our encounter details
                encounter = item

        # If a random encounter was successfully rolled, look up the monster and start the
        # battle.
        if encounter:

            logger.info("Start battle!")

            # Stop movement and keypress on the server.
            if game.isclient or game.ishost:
                game.client.update_player(game.player1.facing,
                                          event_type="CLIENT_START_BATTLE")

            # Create a monster object
            current_monster = monster.Monster()
            current_monster.load_from_db(encounter['monster'])

            # Set the monster's level based on the specified level range
            if len(encounter['level_range']) > 1:
                level = random.randrange(encounter['level_range'][0],
                                         encounter['level_range'][1])
            else:
                level = encounter['level_range'][0]

            # Set the monster's level
            current_monster.level = 1
            current_monster.set_level(level)

            # Create an NPC object which will be this monster's "trainer"
            npc = player.Npc()
            npc.monsters.append(current_monster)

            # Set the NPC object's AI model.
            npc.ai = ai.AI()

            # Add our players and setup combat
            # "queueing" it will mean it starts after the top of the stack is popped (or replaced)
            game.queue_state("CombatState",
                             players=(player1, npc),
                             combat_type="monster")

            # flash the screen
            game.push_state("FlashTransition")

            # Start some music!
            filename = "147066_pokemon.ogg"
            mixer.music.load(prepare.BASEDIR + "resources/music/" + filename)
            mixer.music.play(-1)
            game.current_music["status"] = "playing"
            game.current_music["song"] = filename
Exemplo n.º 6
0
    def start(self):
        player1 = self.game.player1

        # Don't start a battle if we don't even have monsters in our party yet.
        if not check_battle_legal(player1):
            return False

        # Get the parameters to determine what encounter group we'll look up in the database.
        encounter_slug = self.parameters.encounter_slug

        # Look up the encounter details
        monsters = db.JSONDatabase()
        monsters.load("encounter")
        monsters.load("monster")

        # Keep an encounter variable that will let us know if we're going to start a battle.
        encounter = None

        # Get all the monsters associated with this encounter.
        encounters = monsters.database['encounter'][encounter_slug]['monsters']

        for item in encounters:
            # Perform a roll to see if this monster is going to start a battle.
            roll = random.randrange(1, 1000)
            if roll <= int(item['encounter_rate']):
                # Set our encounter details
                encounter = item

        # If a random encounter was successfully rolled, look up the monster and start the
        # battle.
        if encounter:
            logger.info("Starting random encounter!")

            # Stop movement and keypress on the server.
            if self.game.isclient or self.game.ishost:
                self.game.client.update_player(
                    self.game.player1.facing, event_type="CLIENT_START_BATTLE")

            # Create a monster object
            current_monster = monster.Monster()
            current_monster.load_from_db(encounter['monster'])

            # Set the monster's level based on the specified level range
            if len(encounter['level_range']) > 1:
                level = random.randrange(encounter['level_range'][0],
                                         encounter['level_range'][1])
            else:
                level = encounter['level_range'][0]

            # Set the monster's level
            current_monster.level = 1
            current_monster.set_level(level)

            # Create an NPC object which will be this monster's "trainer"
            npc = player.Npc()
            npc.monsters.append(current_monster)
            npc.party_limit = 0

            # Set the NPC object's AI model.
            npc.ai = ai.AI()

            # Add our players and setup combat
            # "queueing" it will mean it starts after the top of the stack is popped (or replaced)
            self.game.queue_state("CombatState",
                                  players=(player1, npc),
                                  combat_type="monster")

            # flash the screen
            self.game.push_state("FlashTransition")

            # Start some music!
            filename = "147066_pokemon.ogg"
            mixer.music.load(prepare.BASEDIR + "resources/music/" + filename)
            mixer.music.play(-1)
            if self.game.current_music["song"]:
                self.game.current_music[
                    "previoussong"] = self.game.current_music["song"]
            self.game.current_music["status"] = "playing"
            self.game.current_music["song"] = filename
Exemplo n.º 7
0
    def start(self):
        # Don't start a battle if we don't even have monsters in our party yet.
        if not check_battle_legal(self.game.player1):
            logger.debug("battle is not legal, won't start")
            return False

        # Stop movement and keypress on the server.
        if self.game.isclient or self.game.ishost:
            self.game.client.update_player(self.game.player1.facing,
                                           event_type="CLIENT_START_BATTLE")

        # Start combat
        npc_slug = self.parameters.npc_slug

        # TODO: This should *really* be handled in the Npc initializer
        # Create an NPC object that will be used as our opponent
        npc = player.Npc(slug=npc_slug)

        # Look up the NPC's details from our NPC database
        npcs = db.JSONDatabase()
        npcs.load("npc")
        npc_details = npcs.database['npc'][npc_slug]

        # Set the NPC object's AI model.
        npc.ai = ai.AI()

        # Look up the NPC's monster party
        npc_party = npc_details['monsters']

        # Look up the monster's details
        monsters = db.JSONDatabase()
        monsters.load("monster")

        # Look up each monster in the NPC's party
        for npc_monster_details in npc_party:
            results = monsters.database['monster'][
                npc_monster_details['monster']]

            # Create a monster object for each monster the NPC has in their party.
            # TODO: unify save/load of monsters
            current_monster = monster.Monster()
            current_monster.load_from_db(npc_monster_details['monster'])
            current_monster.name = npc_monster_details['name']
            current_monster.level = npc_monster_details['level']
            current_monster.hp = npc_monster_details['hp']
            current_monster.current_hp = npc_monster_details['hp']
            current_monster.attack = npc_monster_details['attack']
            current_monster.defense = npc_monster_details['defense']
            current_monster.speed = npc_monster_details['speed']
            current_monster.special_attack = npc_monster_details[
                'special_attack']
            current_monster.special_defense = npc_monster_details[
                'special_defense']
            current_monster.experience_give_modifier = npc_monster_details[
                'exp_give_mod']
            current_monster.experience_required_modifier = npc_monster_details[
                'exp_req_mod']

            current_monster.type1 = results['types'][0]

            current_monster.set_level(current_monster.level)

            if len(results['types']) > 1:
                current_monster.type2 = results['types'][1]

            current_monster.load_sprite_from_db()

            pound = technique.Technique('technique_pound')

            current_monster.learn(pound)

            # Add our monster to the NPC's party
            npc.monsters.append(current_monster)

        # Add our players and setup combat
        logger.debug("Starting battle!")
        self.game.push_state("CombatState",
                             players=(self.game.player1, npc),
                             combat_type="trainer")

        # Start some music!
        logger.info("Playing battle music!")
        filename = "147066_pokemon.ogg"

        mixer.music.load(prepare.BASEDIR + "resources/music/" + filename)
        mixer.music.play(-1)

        if self.game.current_music["song"]:
            self.game.current_music["previoussong"] = self.game.current_music[
                "song"]
        self.game.current_music["status"] = "playing"
        self.game.current_music["song"] = filename
Exemplo n.º 8
0
    def start_battle(self, game, action):
        """Start a battle and switch to the combat module. The parameters must contain an NPC id
        in the NPC database.
            
        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters
            
        :type game: core.tools.Control
        :type action: Tuple
    
        :rtype: None
        :returns: None
    
        Valid Parameters: npc_id
    
        **Examples:**
            
        >>> action
        ... (u'start_battle', u'1', 1, 9)
        
        """
    
        # Don't start a battle if we don't even have monsters in our party yet.
        if len(game.player1.monsters) < 1:
            logger.warning("Cannot start battle, player has no monsters!")
            return False
    
        # Start combat
        npc_id = int(action[1])
            
        # Create an NPC object that will be used as our opponent
        npc = player.Npc()
            
        # Look up the NPC's details from our NPC database
        npcs = db.JSONDatabase()
        npcs.load("npc")
        npc_details = npcs.database['npc'][npc_id]
    
        # Set the NPC object with the details fetched from the database.
        npc.name = npc_details['name']
    
        # Set the NPC object's AI model.
        npc.ai = ai.AI()
            
        # Look up the NPC's monster party
        npc_party = npc_details['monsters']
    
        # Look up the monster's details
        monsters = db.JSONDatabase()
        monsters.load("monster")
    
        # Look up each monster in the NPC's party
        for npc_monster_details in npc_party:
            results = monsters.database['monster'][npc_monster_details['monster_id']]
    
            # Create a monster object for each monster the NPC has in their party.
            current_monster = monster.Monster()
            current_monster.name = npc_monster_details['name']
            current_monster.monster_id = npc_monster_details['monster_id']
            current_monster.level = npc_monster_details['level']
            current_monster.hp = npc_monster_details['hp']
            current_monster.current_hp = npc_monster_details['hp']
            current_monster.attack = npc_monster_details['attack']
            current_monster.defense = npc_monster_details['defense']
            current_monster.speed = npc_monster_details['speed']
            current_monster.special_attack = npc_monster_details['special_attack']
            current_monster.special_defense = npc_monster_details['special_defense']
            current_monster.experience_give_modifier = npc_monster_details['exp_give_mod']
            current_monster.experience_required_modifier = npc_monster_details['exp_req_mod']
    
            current_monster.type1 = results['types'][0]
    
            if len(results['types']) > 1:
                current_monster.type2 = results['types'][1]
    
            current_monster.load_sprite_from_db()
    
            pound = monster.Technique('Pound')
                
            current_monster.learn(pound)
            
            # Add our monster to the NPC's party
            npc.monsters.append(current_monster)
    
        # Add our players and start combat
        game.state_dict["COMBAT"].players.append(game.player1)
        game.state_dict["COMBAT"].players.append(npc)
        game.state_dict["COMBAT"].combat_type = "trainer"
        #game.state_dict["WORLD"].combat_started = True
        game.state_dict["WORLD"].start_battle_transition = True

        # Start some music!
        logger.info("Playing battle music!")
        filename = "147066_pokemon.ogg"
        mixer.music.load("resources/music/" + filename)
        mixer.music.play(-1)
Exemplo n.º 9
0
    def random_encounter(self, game, action):
        """Randomly starts a battle with a monster defined in the "encounter" table in the
        "monster.db" database. The chance that this will start a battle depends on the
        "encounter_rate" specified in the database. The "encounter_rate" number is the chance
        walking in to this tile will trigger a battle out of 100.
    
        :param game: The main game object that contains all the game's variables.
        :param action: The action (tuple) retrieved from the database that contains the action's
            parameters
    
        :type game: core.tools.Control
        :type action: Tuple
    
        :rtype: None
        :returns: None
    
        Valid Parameters: encounter_id
    
        """
    
        player1 = game.player1
    
        # Don't start a battle if we don't even have monsters in our party yet.
        if len(player1.monsters) < 1:
            logger.warning("Cannot start battle, player has no monsters!")
            return False
        else:
            if player1.monsters[0].current_hp <= 0:
                logger.warning("Cannot start battle, player's monsters are all DEAD")
                return False
    
        # Get the parameters to determine what encounter group we'll look up in the database.
        encounter_id = int(action[1])
    
        # Look up the encounter details
        monsters = db.JSONDatabase()
        monsters.load("encounter")
        monsters.load("monster")
    
        # Keep an encounter variable that will let us know if we're going to start a battle.
        encounter = None
    
        # Get all the monsters associated with this encounter.
        encounters = monsters.database['encounter'][encounter_id]['monsters']
    
        for item in encounters:
            # Perform a roll to see if this monster is going to start a battle.
            roll = random.randrange(1,100)
            rate = range(1, int(item['encounter_rate']) + 1)
            if roll in rate:
                # Set our encounter details
                encounter = item
    
        # If a random encounter was successfully rolled, look up the monster and start the
        # battle.
        if encounter:
            logger.info("Start battle!")
                
            # Create a monster object
            current_monster = monster.Monster()
            current_monster.load_from_db(encounter['monster_id'])
    
            # Set the monster's level based on the specified level range
            if len(encounter['level_range']) > 1:
                level = random.randrange(encounter['level_range'][0], encounter['level_range'][1])
            else:
                level = encounter['level_range'][0]
    
            # Set the monster's level
            current_monster.level = level
    
            # Create an NPC object which will be this monster's "trainer"
            npc = player.Npc()
            npc.monsters.append(current_monster)
    
            # Set the NPC object's AI model.
            npc.ai = ai.AI()
    
            # Add our players and start combat
            game.state_dict["COMBAT"].players.append(player1)
            game.state_dict["COMBAT"].players.append(npc)
            game.state_dict["COMBAT"].combat_type = "monster"
            game.state_dict["WORLD"].start_battle_transition = True

            # Start some music!
            filename = "147066_pokemon.ogg"
            mixer.music.load("resources/music/" + filename)
            mixer.music.play(-1)
            game.current_music["status"] = "playing"
            game.current_music["song"] = filename

            # Stop the player's movement
            game.state_dict["WORLD"].menu_blocking = True
            player1.moving = False
            player1.direction = {'down': False, 'left': False, 'right': False, 'up': False}