Some minor adjustments for pep8.
This commit is contained in:
parent
005923ee72
commit
59f491eab4
1 changed files with 192 additions and 169 deletions
|
|
@ -25,7 +25,7 @@ And change your game's character typeclass to inherit from BattleCharacter
|
||||||
instead of the default:
|
instead of the default:
|
||||||
|
|
||||||
class Character(BattleCharacter):
|
class Character(BattleCharacter):
|
||||||
|
|
||||||
Next, import this module into your default_cmdsets.py module:
|
Next, import this module into your default_cmdsets.py module:
|
||||||
|
|
||||||
from evennia.contrib import turnbattle
|
from evennia.contrib import turnbattle
|
||||||
|
|
@ -51,48 +51,51 @@ from evennia.commands.default.help import CmdHelp
|
||||||
COMBAT FUNCTIONS START HERE
|
COMBAT FUNCTIONS START HERE
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
def roll_init(character):
|
def roll_init(character):
|
||||||
"""
|
"""
|
||||||
Rolls a number between 1-1000 to determine initiative.
|
Rolls a number between 1-1000 to determine initiative.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): The character to determine initiative for
|
character (obj): The character to determine initiative for
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
initiative (int): The character's place in initiative - higher
|
initiative (int): The character's place in initiative - higher
|
||||||
numbers go first.
|
numbers go first.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
By default, does not reference the character and simply returns
|
By default, does not reference the character and simply returns
|
||||||
a random integer from 1 to 1000.
|
a random integer from 1 to 1000.
|
||||||
|
|
||||||
Since the character is passed to this function, you can easily reference
|
Since the character is passed to this function, you can easily reference
|
||||||
a character's stats to determine an initiative roll - for example, if your
|
a character's stats to determine an initiative roll - for example, if your
|
||||||
character has a 'dexterity' attribute, you can use it to give that character
|
character has a 'dexterity' attribute, you can use it to give that character
|
||||||
an advantage in turn order, like so:
|
an advantage in turn order, like so:
|
||||||
|
|
||||||
return (randint(1,20)) + character.db.dexterity
|
return (randint(1,20)) + character.db.dexterity
|
||||||
|
|
||||||
This way, characters with a higher dexterity will go first more often.
|
This way, characters with a higher dexterity will go first more often.
|
||||||
"""
|
"""
|
||||||
return randint(1,1000)
|
return randint(1, 1000)
|
||||||
|
|
||||||
|
|
||||||
def get_attack(attacker, defender):
|
def get_attack(attacker, defender):
|
||||||
"""
|
"""
|
||||||
Returns a value for an attack roll.
|
Returns a value for an attack roll.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
attacker (obj): Character doing the attacking
|
attacker (obj): Character doing the attacking
|
||||||
defender (obj): Character being attacked
|
defender (obj): Character being attacked
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
attack_value (int): Attack roll value, compared against a defense value
|
attack_value (int): Attack roll value, compared against a defense value
|
||||||
to determine whether an attack hits or misses.
|
to determine whether an attack hits or misses.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
By default, returns a random integer from 1 to 100 without using any
|
By default, returns a random integer from 1 to 100 without using any
|
||||||
properties from either the attacker or defender.
|
properties from either the attacker or defender.
|
||||||
|
|
||||||
This can easily be expanded to return a value based on characters stats,
|
This can easily be expanded to return a value based on characters stats,
|
||||||
equipment, and abilities. This is why the attacker and defender are passed
|
equipment, and abilities. This is why the attacker and defender are passed
|
||||||
to this function, even though nothing from either one are used in this example.
|
to this function, even though nothing from either one are used in this example.
|
||||||
|
|
@ -100,75 +103,79 @@ def get_attack(attacker, defender):
|
||||||
# For this example, just return a random integer up to 100.
|
# For this example, just return a random integer up to 100.
|
||||||
attack_value = randint(1, 100)
|
attack_value = randint(1, 100)
|
||||||
return attack_value
|
return attack_value
|
||||||
|
|
||||||
|
|
||||||
def get_defense(attacker, defender):
|
def get_defense(attacker, defender):
|
||||||
"""
|
"""
|
||||||
Returns a value for defense, which an attack roll must equal or exceed in order
|
Returns a value for defense, which an attack roll must equal or exceed in order
|
||||||
for an attack to hit.
|
for an attack to hit.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
attacker (obj): Character doing the attacking
|
attacker (obj): Character doing the attacking
|
||||||
defender (obj): Character being attacked
|
defender (obj): Character being attacked
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
defense_value (int): Defense value, compared against an attack roll
|
defense_value (int): Defense value, compared against an attack roll
|
||||||
to determine whether an attack hits or misses.
|
to determine whether an attack hits or misses.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
By default, returns 50, not taking any properties of the defender or
|
By default, returns 50, not taking any properties of the defender or
|
||||||
attacker into account.
|
attacker into account.
|
||||||
|
|
||||||
As above, this can be expanded upon based on character stats and equipment.
|
As above, this can be expanded upon based on character stats and equipment.
|
||||||
"""
|
"""
|
||||||
# For this example, just return 50, for about a 50/50 chance of hit.
|
# For this example, just return 50, for about a 50/50 chance of hit.
|
||||||
defense_value = 50
|
defense_value = 50
|
||||||
return defense_value
|
return defense_value
|
||||||
|
|
||||||
|
|
||||||
def get_damage(attacker, defender):
|
def get_damage(attacker, defender):
|
||||||
"""
|
"""
|
||||||
Returns a value for damage to be deducted from the defender's HP after abilities
|
Returns a value for damage to be deducted from the defender's HP after abilities
|
||||||
successful hit.
|
successful hit.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
attacker (obj): Character doing the attacking
|
attacker (obj): Character doing the attacking
|
||||||
defender (obj): Character being damaged
|
defender (obj): Character being damaged
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
damage_value (int): Damage value, which is to be deducted from the defending
|
damage_value (int): Damage value, which is to be deducted from the defending
|
||||||
character's HP.
|
character's HP.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
By default, returns a random integer from 15 to 25 without using any
|
By default, returns a random integer from 15 to 25 without using any
|
||||||
properties from either the attacker or defender.
|
properties from either the attacker or defender.
|
||||||
|
|
||||||
Again, this can be expanded upon.
|
Again, this can be expanded upon.
|
||||||
"""
|
"""
|
||||||
# For this example, just generate a number between 15 and 25.
|
# For this example, just generate a number between 15 and 25.
|
||||||
damage_value = randint(15, 25)
|
damage_value = randint(15, 25)
|
||||||
return damage_value
|
return damage_value
|
||||||
|
|
||||||
|
|
||||||
def apply_damage(defender, damage):
|
def apply_damage(defender, damage):
|
||||||
"""
|
"""
|
||||||
Applies damage to a target, reducing their HP by the damage amount to a
|
Applies damage to a target, reducing their HP by the damage amount to a
|
||||||
minimum of 0.
|
minimum of 0.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
defender (obj): Character taking damage
|
defender (obj): Character taking damage
|
||||||
damage (int): Amount of damage being taken
|
damage (int): Amount of damage being taken
|
||||||
"""
|
"""
|
||||||
defender.db.hp -= damage # Reduce defender's HP by the damage dealt.
|
defender.db.hp -= damage # Reduce defender's HP by the damage dealt.
|
||||||
# If this reduces it to 0 or less, set HP to 0.
|
# If this reduces it to 0 or less, set HP to 0.
|
||||||
if defender.db.hp <= 0:
|
if defender.db.hp <= 0:
|
||||||
defender.db.hp = 0
|
defender.db.hp = 0
|
||||||
|
|
||||||
|
|
||||||
def resolve_attack(attacker, defender, attack_value=None, defense_value=None):
|
def resolve_attack(attacker, defender, attack_value=None, defense_value=None):
|
||||||
"""
|
"""
|
||||||
Resolves an attack and outputs the result.
|
Resolves an attack and outputs the result.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
attacker (obj): Character doing the attacking
|
attacker (obj): Character doing the attacking
|
||||||
defender (obj): Character being attacked
|
defender (obj): Character being attacked
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
Even though the attack and defense values are calculated
|
Even though the attack and defense values are calculated
|
||||||
extremely simply, they are separated out into their own functions
|
extremely simply, they are separated out into their own functions
|
||||||
|
|
@ -184,18 +191,19 @@ def resolve_attack(attacker, defender, attack_value=None, defense_value=None):
|
||||||
if attack_value < defense_value:
|
if attack_value < defense_value:
|
||||||
attacker.location.msg_contents("%s's attack misses %s!" % (attacker, defender))
|
attacker.location.msg_contents("%s's attack misses %s!" % (attacker, defender))
|
||||||
else:
|
else:
|
||||||
damage_value = get_damage(attacker, defender) # Calculate damage value.
|
damage_value = get_damage(attacker, defender) # Calculate damage value.
|
||||||
# Announce damage dealt and apply damage.
|
# Announce damage dealt and apply damage.
|
||||||
attacker.location.msg_contents("%s hits %s for %i damage!" % (attacker, defender, damage_value))
|
attacker.location.msg_contents("%s hits %s for %i damage!" % (attacker, defender, damage_value))
|
||||||
apply_damage (defender, damage_value)
|
apply_damage(defender, damage_value)
|
||||||
# If defender HP is reduced to 0 or less, announce defeat.
|
# If defender HP is reduced to 0 or less, announce defeat.
|
||||||
if defender.db.hp <= 0:
|
if defender.db.hp <= 0:
|
||||||
attacker.location.msg_contents("%s has been defeated!" % defender)
|
attacker.location.msg_contents("%s has been defeated!" % defender)
|
||||||
|
|
||||||
|
|
||||||
def combat_cleanup(character):
|
def combat_cleanup(character):
|
||||||
"""
|
"""
|
||||||
Cleans up all the temporary combat-related attributes on a character.
|
Cleans up all the temporary combat-related attributes on a character.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to have their combat attributes removed
|
character (obj): Character to have their combat attributes removed
|
||||||
|
|
||||||
|
|
@ -204,13 +212,14 @@ def combat_cleanup(character):
|
||||||
longer needed once a fight ends.
|
longer needed once a fight ends.
|
||||||
"""
|
"""
|
||||||
for attr in character.attributes.all():
|
for attr in character.attributes.all():
|
||||||
if attr.key[:7] == "combat_": # If the attribute name starts with 'combat_'...
|
if attr.key[:7] == "combat_": # If the attribute name starts with 'combat_'...
|
||||||
character.attributes.remove(key=attr.key) # ...then delete it!
|
character.attributes.remove(key=attr.key) # ...then delete it!
|
||||||
|
|
||||||
|
|
||||||
def is_in_combat(character):
|
def is_in_combat(character):
|
||||||
"""
|
"""
|
||||||
Returns true if the given character is in combat.
|
Returns true if the given character is in combat.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to determine if is in combat or not
|
character (obj): Character to determine if is in combat or not
|
||||||
|
|
||||||
|
|
@ -220,11 +229,12 @@ def is_in_combat(character):
|
||||||
if character.db.Combat_TurnHandler:
|
if character.db.Combat_TurnHandler:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def is_turn(character):
|
def is_turn(character):
|
||||||
"""
|
"""
|
||||||
Returns true if it's currently the given character's turn in combat.
|
Returns true if it's currently the given character's turn in combat.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to determine if it is their turn or not
|
character (obj): Character to determine if it is their turn or not
|
||||||
|
|
||||||
|
|
@ -236,29 +246,30 @@ def is_turn(character):
|
||||||
if character == currentchar:
|
if character == currentchar:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def spend_action(character, actions, action_name=None):
|
def spend_action(character, actions, action_name=None):
|
||||||
"""
|
"""
|
||||||
Spends a character's available combat actions and checks for end of turn.
|
Spends a character's available combat actions and checks for end of turn.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character spending the action
|
character (obj): Character spending the action
|
||||||
actions (int) or 'all': Number of actions to spend, or 'all' to spend all actions
|
actions (int) or 'all': Number of actions to spend, or 'all' to spend all actions
|
||||||
|
|
||||||
Kwargs:
|
Kwargs:
|
||||||
action_name (str or None): If a string is given, sets character's last action in
|
action_name (str or None): If a string is given, sets character's last action in
|
||||||
combat to provided string
|
combat to provided string
|
||||||
"""
|
"""
|
||||||
if action_name:
|
if action_name:
|
||||||
character.db.Combat_LastAction = action_name
|
character.db.Combat_LastAction = action_name
|
||||||
if actions == 'all': # If spending all actions
|
if actions == 'all': # If spending all actions
|
||||||
character.db.Combat_ActionsLeft = 0 # Set actions to 0
|
character.db.Combat_ActionsLeft = 0 # Set actions to 0
|
||||||
else:
|
else:
|
||||||
character.db.Combat_ActionsLeft -= actions # Use up actions.
|
character.db.Combat_ActionsLeft -= actions # Use up actions.
|
||||||
if character.db.Combat_ActionsLeft < 0:
|
if character.db.Combat_ActionsLeft < 0:
|
||||||
character.db.Combat_ActionsLeft = 0 # Can't have fewer than 0 actions
|
character.db.Combat_ActionsLeft = 0 # Can't have fewer than 0 actions
|
||||||
character.db.Combat_TurnHandler.turn_end_check(character) # Signal potential end of turn.
|
character.db.Combat_TurnHandler.turn_end_check(character) # Signal potential end of turn.
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
|
|
@ -266,26 +277,28 @@ CHARACTER TYPECLASS
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class BattleCharacter(DefaultCharacter):
|
class BattleCharacter(DefaultCharacter):
|
||||||
"""
|
"""
|
||||||
A character able to participate in turn-based combat. Has attributes for current
|
A character able to participate in turn-based combat. Has attributes for current
|
||||||
and maximum HP, and access to combat commands.
|
and maximum HP, and access to combat commands.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def at_object_creation(self):
|
def at_object_creation(self):
|
||||||
"""
|
"""
|
||||||
Called once, when this object is first created. This is the
|
Called once, when this object is first created. This is the
|
||||||
normal hook to overload for most object types.
|
normal hook to overload for most object types.
|
||||||
"""
|
"""
|
||||||
self.db.max_hp = 100 # Set maximum HP to 100
|
self.db.max_hp = 100 # Set maximum HP to 100
|
||||||
self.db.hp = self.db.max_hp # Set current HP to maximum
|
self.db.hp = self.db.max_hp # Set current HP to maximum
|
||||||
"""
|
"""
|
||||||
Adds attributes for a character's current and maximum HP.
|
Adds attributes for a character's current and maximum HP.
|
||||||
We're just going to set this value at '100' by default.
|
We're just going to set this value at '100' by default.
|
||||||
|
|
||||||
You may want to expand this to include various 'stats' that
|
You may want to expand this to include various 'stats' that
|
||||||
can be changed at creation and factor into combat calculations.
|
can be changed at creation and factor into combat calculations.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def at_before_move(self, destination):
|
def at_before_move(self, destination):
|
||||||
"""
|
"""
|
||||||
Called just before starting to move this object to
|
Called just before starting to move this object to
|
||||||
|
|
@ -305,24 +318,27 @@ class BattleCharacter(DefaultCharacter):
|
||||||
# Keep the character from moving if at 0 HP or in combat.
|
# Keep the character from moving if at 0 HP or in combat.
|
||||||
if is_in_combat(self):
|
if is_in_combat(self):
|
||||||
self.msg("You can't exit a room while in combat!")
|
self.msg("You can't exit a room while in combat!")
|
||||||
return False # Returning false keeps the character from moving.
|
return False # Returning false keeps the character from moving.
|
||||||
if self.db.HP <= 0:
|
if self.db.HP <= 0:
|
||||||
self.msg("You can't move, you've been defeated!")
|
self.msg("You can't move, you've been defeated!")
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
COMMANDS START HERE
|
COMMANDS START HERE
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class CmdFight(Command):
|
class CmdFight(Command):
|
||||||
"""
|
"""
|
||||||
Starts a fight with everyone in the same room as you.
|
Starts a fight with everyone in the same room as you.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
fight
|
fight
|
||||||
|
|
||||||
When you start a fight, everyone in the room who is able to
|
When you start a fight, everyone in the room who is able to
|
||||||
fight is added to combat, and a turn order is randomly rolled.
|
fight is added to combat, and a turn order is randomly rolled.
|
||||||
When it's your turn, you can attack other characters.
|
When it's your turn, you can attack other characters.
|
||||||
|
|
@ -336,82 +352,85 @@ class CmdFight(Command):
|
||||||
"""
|
"""
|
||||||
here = self.caller.location
|
here = self.caller.location
|
||||||
fighters = []
|
fighters = []
|
||||||
|
|
||||||
if not self.caller.db.hp: # If you don't have any hp
|
if not self.caller.db.hp: # If you don't have any hp
|
||||||
self.caller.msg("You can't start a fight if you've been defeated!")
|
self.caller.msg("You can't start a fight if you've been defeated!")
|
||||||
return
|
return
|
||||||
if is_in_combat(self.caller): # Already in a fight
|
if is_in_combat(self.caller): # Already in a fight
|
||||||
self.caller.msg("You're already in a fight!")
|
self.caller.msg("You're already in a fight!")
|
||||||
return
|
return
|
||||||
for thing in here.contents: # Test everything in the room to add it to the fight.
|
for thing in here.contents: # Test everything in the room to add it to the fight.
|
||||||
if thing.db.HP: # If the object has HP...
|
if thing.db.HP: # If the object has HP...
|
||||||
fighters.append(thing) # ...then add it to the fight.
|
fighters.append(thing) # ...then add it to the fight.
|
||||||
if len(fighters) <= 1: # If you're the only able fighter in the room
|
if len(fighters) <= 1: # If you're the only able fighter in the room
|
||||||
self.caller.msg("There's nobody here to fight!")
|
self.caller.msg("There's nobody here to fight!")
|
||||||
return
|
return
|
||||||
if here.db.Combat_TurnHandler: # If there's already a fight going on...
|
if here.db.Combat_TurnHandler: # If there's already a fight going on...
|
||||||
here.msg_contents("%s joins the fight!" % self.caller)
|
here.msg_contents("%s joins the fight!" % self.caller)
|
||||||
here.db.Combat_TurnHandler.join_fight(self.caller) # Join the fight!
|
here.db.Combat_TurnHandler.join_fight(self.caller) # Join the fight!
|
||||||
return
|
return
|
||||||
here.msg_contents("%s starts a fight!" % self.caller)
|
here.msg_contents("%s starts a fight!" % self.caller)
|
||||||
here.scripts.add("contrib.turnbattle.TurnHandler") # Add a turn handler script to the room, which starts combat.
|
# Add a turn handler script to the room, which starts combat.
|
||||||
|
here.scripts.add("contrib.turnbattle.TurnHandler")
|
||||||
# Remember you'll have to change the path to the script if you copy this code to your own modules!
|
# Remember you'll have to change the path to the script if you copy this code to your own modules!
|
||||||
|
|
||||||
|
|
||||||
class CmdAttack(Command):
|
class CmdAttack(Command):
|
||||||
"""
|
"""
|
||||||
Attacks another character.
|
Attacks another character.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
attack <target>
|
attack <target>
|
||||||
|
|
||||||
When in a fight, you may attack another character. The attack has
|
When in a fight, you may attack another character. The attack has
|
||||||
a chance to hit, and if successful, will deal damage.
|
a chance to hit, and if successful, will deal damage.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
key = "attack"
|
key = "attack"
|
||||||
help_category = "combat"
|
help_category = "combat"
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"This performs the actual command."
|
"This performs the actual command."
|
||||||
"Set the attacker to the caller and the defender to the target."
|
"Set the attacker to the caller and the defender to the target."
|
||||||
|
|
||||||
if not is_in_combat(self.caller): # If not in combat, can't attack.
|
if not is_in_combat(self.caller): # If not in combat, can't attack.
|
||||||
self.caller.msg("You can only do that in combat. (see: help fight)")
|
self.caller.msg("You can only do that in combat. (see: help fight)")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not is_turn(self.caller): # If it's not your turn, can't attack.
|
if not is_turn(self.caller): # If it's not your turn, can't attack.
|
||||||
self.caller.msg("You can only do that on your turn.")
|
self.caller.msg("You can only do that on your turn.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not self.caller.db.hp: # Can't attack if you have no HP.
|
if not self.caller.db.hp: # Can't attack if you have no HP.
|
||||||
self.caller.msg("You can't attack, you've been defeated.")
|
self.caller.msg("You can't attack, you've been defeated.")
|
||||||
return
|
return
|
||||||
|
|
||||||
attacker = self.caller
|
attacker = self.caller
|
||||||
defender = self.caller.search(self.args)
|
defender = self.caller.search(self.args)
|
||||||
|
|
||||||
if not defender: # No valid target given.
|
if not defender: # No valid target given.
|
||||||
return
|
return
|
||||||
|
|
||||||
if not defender.db.hp: # Target object has no HP left or to begin with
|
if not defender.db.hp: # Target object has no HP left or to begin with
|
||||||
self.caller.msg("You can't fight that!")
|
self.caller.msg("You can't fight that!")
|
||||||
return
|
return
|
||||||
|
|
||||||
if attacker == defender: # Target and attacker are the same
|
if attacker == defender: # Target and attacker are the same
|
||||||
self.caller.msg("You can't attack yourself!")
|
self.caller.msg("You can't attack yourself!")
|
||||||
return
|
return
|
||||||
|
|
||||||
"If everything checks out, call the attack resolving function."
|
"If everything checks out, call the attack resolving function."
|
||||||
resolve_attack(attacker, defender)
|
resolve_attack(attacker, defender)
|
||||||
spend_action(self.caller, 1, action_name="attack") # Use up one action.
|
spend_action(self.caller, 1, action_name="attack") # Use up one action.
|
||||||
|
|
||||||
|
|
||||||
class CmdPass(Command):
|
class CmdPass(Command):
|
||||||
"""
|
"""
|
||||||
Passes on your turn.
|
Passes on your turn.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
When in a fight, you can use this command to end your turn early, even
|
When in a fight, you can use this command to end your turn early, even
|
||||||
if there are still any actions you can take.
|
if there are still any actions you can take.
|
||||||
"""
|
"""
|
||||||
|
|
@ -424,24 +443,25 @@ class CmdPass(Command):
|
||||||
"""
|
"""
|
||||||
This performs the actual command.
|
This performs the actual command.
|
||||||
"""
|
"""
|
||||||
if not is_in_combat(self.caller): # Can only pass a turn in combat.
|
if not is_in_combat(self.caller): # Can only pass a turn in combat.
|
||||||
self.caller.msg("You can only do that in combat. (see: help fight)")
|
self.caller.msg("You can only do that in combat. (see: help fight)")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not is_turn(self.caller): # Can only pass if it's your turn.
|
if not is_turn(self.caller): # Can only pass if it's your turn.
|
||||||
self.caller.msg("You can only do that on your turn.")
|
self.caller.msg("You can only do that on your turn.")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.caller.location.msg_contents("%s takes no further action, passing the turn." % self.caller)
|
self.caller.location.msg_contents("%s takes no further action, passing the turn." % self.caller)
|
||||||
spend_action(self.caller, 'all', action_name="pass") # Spend all remaining actions.
|
spend_action(self.caller, 'all', action_name="pass") # Spend all remaining actions.
|
||||||
|
|
||||||
|
|
||||||
class CmdDisengage(Command):
|
class CmdDisengage(Command):
|
||||||
"""
|
"""
|
||||||
Passes your turn and attempts to end combat.
|
Passes your turn and attempts to end combat.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
disengage
|
disengage
|
||||||
|
|
||||||
Ends your turn early and signals that you're trying to end
|
Ends your turn early and signals that you're trying to end
|
||||||
the fight. If all participants in a fight disengage, the
|
the fight. If all participants in a fight disengage, the
|
||||||
fight ends.
|
fight ends.
|
||||||
|
|
@ -455,48 +475,50 @@ class CmdDisengage(Command):
|
||||||
"""
|
"""
|
||||||
This performs the actual command.
|
This performs the actual command.
|
||||||
"""
|
"""
|
||||||
if not is_in_combat(self.caller): # If you're not in combat
|
if not is_in_combat(self.caller): # If you're not in combat
|
||||||
self.caller.msg("You can only do that in combat. (see: help fight)")
|
self.caller.msg("You can only do that in combat. (see: help fight)")
|
||||||
return
|
return
|
||||||
|
|
||||||
if not is_turn(self.caller): # If it's not your turn
|
if not is_turn(self.caller): # If it's not your turn
|
||||||
self.caller.msg("You can only do that on your turn.")
|
self.caller.msg("You can only do that on your turn.")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.caller.location.msg_contents("%s disengages, ready to stop fighting." % self.caller)
|
self.caller.location.msg_contents("%s disengages, ready to stop fighting." % self.caller)
|
||||||
spend_action(self.caller, 'all', action_name="disengage") # Spend all remaining actions.
|
spend_action(self.caller, 'all', action_name="disengage") # Spend all remaining actions.
|
||||||
"""
|
"""
|
||||||
The action_name kwarg sets the character's last action to "disengage", which is checked by
|
The action_name kwarg sets the character's last action to "disengage", which is checked by
|
||||||
the turn handler script to see if all fighters have disengaged.
|
the turn handler script to see if all fighters have disengaged.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class CmdRest(Command):
|
class CmdRest(Command):
|
||||||
"""
|
"""
|
||||||
Recovers damage.
|
Recovers damage.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
rest
|
rest
|
||||||
|
|
||||||
Resting recovers your HP to its maximum, but you can only
|
Resting recovers your HP to its maximum, but you can only
|
||||||
rest if you're not in a fight.
|
rest if you're not in a fight.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
key = "rest"
|
key = "rest"
|
||||||
help_category = "combat"
|
help_category = "combat"
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
"This performs the actual command."
|
"This performs the actual command."
|
||||||
|
|
||||||
if is_in_combat(self.caller): # If you're in combat
|
if is_in_combat(self.caller): # If you're in combat
|
||||||
self.caller.msg("You can't rest while you're in combat.")
|
self.caller.msg("You can't rest while you're in combat.")
|
||||||
return
|
return
|
||||||
|
|
||||||
self.caller.db.hp = self.caller.db.max_hp # Set current HP to maximum
|
self.caller.db.hp = self.caller.db.max_hp # Set current HP to maximum
|
||||||
self.caller.location.msg_contents("%s rests to recover HP." % self.caller)
|
self.caller.location.msg_contents("%s rests to recover HP." % self.caller)
|
||||||
"""
|
"""
|
||||||
You'll probably want to replace this with your own system for recovering HP.
|
You'll probably want to replace this with your own system for recovering HP.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class CmdCombatHelp(CmdHelp):
|
class CmdCombatHelp(CmdHelp):
|
||||||
"""
|
"""
|
||||||
View help or a list of topics
|
View help or a list of topics
|
||||||
|
|
@ -511,15 +533,16 @@ class CmdCombatHelp(CmdHelp):
|
||||||
"""
|
"""
|
||||||
# Just like the default help command, but will give quick
|
# Just like the default help command, but will give quick
|
||||||
# tips on combat when used in a fight with no arguments.
|
# tips on combat when used in a fight with no arguments.
|
||||||
|
|
||||||
def func(self):
|
def func(self):
|
||||||
if is_in_combat(self.caller) and not self.args: # In combat and entered 'help' alone
|
if is_in_combat(self.caller) and not self.args: # In combat and entered 'help' alone
|
||||||
self.caller.msg("Available combat commands:|/"+
|
self.caller.msg("Available combat commands:|/" +
|
||||||
"|wAttack:|n Attack a target, attempting to deal damage.|/"+
|
"|wAttack:|n Attack a target, attempting to deal damage.|/" +
|
||||||
"|wPass:|n Pass your turn without further action.|/"+
|
"|wPass:|n Pass your turn without further action.|/" +
|
||||||
"|wDisengage:|n End your turn and attempt to end combat.|/")
|
"|wDisengage:|n End your turn and attempt to end combat.|/")
|
||||||
else:
|
else:
|
||||||
super(CmdCombatHelp, self).func() # Call the default help command
|
super(CmdCombatHelp, self).func() # Call the default help command
|
||||||
|
|
||||||
|
|
||||||
class BattleCmdSet(default_cmds.CharacterCmdSet):
|
class BattleCmdSet(default_cmds.CharacterCmdSet):
|
||||||
"""
|
"""
|
||||||
|
|
@ -537,13 +560,15 @@ class BattleCmdSet(default_cmds.CharacterCmdSet):
|
||||||
self.add(CmdPass())
|
self.add(CmdPass())
|
||||||
self.add(CmdDisengage())
|
self.add(CmdDisengage())
|
||||||
self.add(CmdCombatHelp())
|
self.add(CmdCombatHelp())
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
SCRIPTS START HERE
|
SCRIPTS START HERE
|
||||||
----------------------------------------------------------------------------
|
----------------------------------------------------------------------------
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class TurnHandler(DefaultScript):
|
class TurnHandler(DefaultScript):
|
||||||
"""
|
"""
|
||||||
This is the script that handles the progression of combat through turns.
|
This is the script that handles the progression of combat through turns.
|
||||||
|
|
@ -551,90 +576,89 @@ class TurnHandler(DefaultScript):
|
||||||
to its roster and then sorts them into a turn order. There can only be one
|
to its roster and then sorts them into a turn order. There can only be one
|
||||||
fight going on in a single room at a time, so the script is assigned to a
|
fight going on in a single room at a time, so the script is assigned to a
|
||||||
room as its object.
|
room as its object.
|
||||||
|
|
||||||
Fights persist until only one participant is left with any HP or all
|
Fights persist until only one participant is left with any HP or all
|
||||||
remaining participants choose to end the combat with the 'disengage' command.
|
remaining participants choose to end the combat with the 'disengage' command.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def at_script_creation(self):
|
def at_script_creation(self):
|
||||||
"""
|
"""
|
||||||
Called once, when the script is created.
|
Called once, when the script is created.
|
||||||
"""
|
"""
|
||||||
self.key = "Combat Turn Handler"
|
self.key = "Combat Turn Handler"
|
||||||
self.interval = 5 # Once every 5 seconds
|
self.interval = 5 # Once every 5 seconds
|
||||||
self.persistent = True
|
self.persistent = True
|
||||||
self.db.fighters = []
|
self.db.fighters = []
|
||||||
|
|
||||||
# Add all fighters in the room with at least 1 HP to the combat."
|
# Add all fighters in the room with at least 1 HP to the combat."
|
||||||
for object in self.obj.contents:
|
for object in self.obj.contents:
|
||||||
if object.db.hp:
|
if object.db.hp:
|
||||||
self.db.fighters.append(object)
|
self.db.fighters.append(object)
|
||||||
|
|
||||||
# Initialize each fighter for combat
|
# Initialize each fighter for combat
|
||||||
for fighter in self.db.fighters:
|
for fighter in self.db.fighters:
|
||||||
self.initialize_for_combat(fighter)
|
self.initialize_for_combat(fighter)
|
||||||
|
|
||||||
# Add a reference to this script to the room
|
# Add a reference to this script to the room
|
||||||
self.obj.db.Combat_TurnHandler = self
|
self.obj.db.Combat_TurnHandler = self
|
||||||
|
|
||||||
# Roll initiative and sort the list of fighters depending on who rolls highest to determine turn order.
|
# Roll initiative and sort the list of fighters depending on who rolls highest to determine turn order.
|
||||||
# The initiative roll is determined by the roll_init function and can be customized easily.
|
# The initiative roll is determined by the roll_init function and can be customized easily.
|
||||||
ordered_by_roll = sorted(self.db.fighters, key=roll_init, reverse=True)
|
ordered_by_roll = sorted(self.db.fighters, key=roll_init, reverse=True)
|
||||||
self.db.fighters = ordered_by_roll
|
self.db.fighters = ordered_by_roll
|
||||||
|
|
||||||
# Announce the turn order.
|
# Announce the turn order.
|
||||||
self.obj.msg_contents("Turn order is: %s " % ", ".join(obj.key for obj in self.db.fighters))
|
self.obj.msg_contents("Turn order is: %s " % ", ".join(obj.key for obj in self.db.fighters))
|
||||||
|
|
||||||
#Set up the current turn and turn timeout delay.
|
# Set up the current turn and turn timeout delay.
|
||||||
self.db.turn = 0
|
self.db.turn = 0
|
||||||
self.db.timer = 30 # 30 seconds
|
self.db.timer = 30 # 30 seconds
|
||||||
|
|
||||||
def at_stop(self):
|
def at_stop(self):
|
||||||
"""
|
"""
|
||||||
Called at script termination.
|
Called at script termination.
|
||||||
"""
|
"""
|
||||||
for fighter in self.db.fighters:
|
for fighter in self.db.fighters:
|
||||||
combat_cleanup(fighter) #Clean up the combat attributes for every fighter.
|
combat_cleanup(fighter) # Clean up the combat attributes for every fighter.
|
||||||
self.obj.db.Combat_TurnHandler = None # Remove reference to turn handler in location
|
self.obj.db.Combat_TurnHandler = None # Remove reference to turn handler in location
|
||||||
|
|
||||||
def at_repeat(self):
|
def at_repeat(self):
|
||||||
"""
|
"""
|
||||||
Called once every self.interval seconds.
|
Called once every self.interval seconds.
|
||||||
"""
|
"""
|
||||||
currentchar = self.db.fighters[self.db.turn] # Note the current character in the turn order.
|
currentchar = self.db.fighters[self.db.turn] # Note the current character in the turn order.
|
||||||
self.db.timer -= self.interval # Count down the timer.
|
self.db.timer -= self.interval # Count down the timer.
|
||||||
|
|
||||||
if self.db.timer <= 0:
|
if self.db.timer <= 0:
|
||||||
# Force current character to disengage if timer runs out.
|
# Force current character to disengage if timer runs out.
|
||||||
self.obj.msg_contents("%s's turn timed out!" % currentchar)
|
self.obj.msg_contents("%s's turn timed out!" % currentchar)
|
||||||
spend_action(currentchar, 'all', action_name="disengage") # Spend all remaining actions.
|
spend_action(currentchar, 'all', action_name="disengage") # Spend all remaining actions.
|
||||||
return
|
return
|
||||||
elif self.db.timer <= 10 and not self.db.timeout_warning_given: # 10 seconds left
|
elif self.db.timer <= 10 and not self.db.timeout_warning_given: # 10 seconds left
|
||||||
# Warn the current character if they're about to time out.
|
# Warn the current character if they're about to time out.
|
||||||
currentchar.msg("WARNING: About to time out!")
|
currentchar.msg("WARNING: About to time out!")
|
||||||
self.db.timeout_warning_given = True
|
self.db.timeout_warning_given = True
|
||||||
|
|
||||||
|
|
||||||
def initialize_for_combat(self, character):
|
def initialize_for_combat(self, character):
|
||||||
"""
|
"""
|
||||||
Prepares a character for combat when starting or entering a fight.
|
Prepares a character for combat when starting or entering a fight.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to initialize for combat.
|
character (obj): Character to initialize for combat.
|
||||||
"""
|
"""
|
||||||
combat_cleanup(character) # Clean up leftover combat attributes beforehand, just in case.
|
combat_cleanup(character) # Clean up leftover combat attributes beforehand, just in case.
|
||||||
character.db.Combat_ActionsLeft = 0 # Actions remaining - start of turn adds to this, turn ends when it reaches 0
|
character.db.Combat_ActionsLeft = 0 # Actions remaining - start of turn adds to this, turn ends when it reaches 0
|
||||||
character.db.Combat_TurnHandler = self # Add a reference to this turn handler script to the character
|
character.db.Combat_TurnHandler = self # Add a reference to this turn handler script to the character
|
||||||
character.db.Combat_LastAction = "null" # Track last action taken in combat
|
character.db.Combat_LastAction = "null" # Track last action taken in combat
|
||||||
|
|
||||||
def start_turn(self, character):
|
def start_turn(self, character):
|
||||||
"""
|
"""
|
||||||
Readies a character for the start of their turn by replenishing their
|
Readies a character for the start of their turn by replenishing their
|
||||||
available actions and notifying them that their turn has come up.
|
available actions and notifying them that their turn has come up.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to be readied.
|
character (obj): Character to be readied.
|
||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
Here, you only get one action per turn, but you might want to allow more than
|
Here, you only get one action per turn, but you might want to allow more than
|
||||||
one per turn, or even grant a number of actions based on a character's
|
one per turn, or even grant a number of actions based on a character's
|
||||||
|
|
@ -642,65 +666,64 @@ class TurnHandler(DefaultScript):
|
||||||
separated for movement, by adding "character.db.Combat_MovesLeft = 3" or
|
separated for movement, by adding "character.db.Combat_MovesLeft = 3" or
|
||||||
something similar.
|
something similar.
|
||||||
"""
|
"""
|
||||||
character.db.Combat_ActionsLeft = 1 # 1 action per turn.
|
character.db.Combat_ActionsLeft = 1 # 1 action per turn.
|
||||||
# Prompt the character for their turn and give some information.
|
# Prompt the character for their turn and give some information.
|
||||||
character.msg("|wIt's your turn! You have %i HP remaining.|n" % character.db.hp)
|
character.msg("|wIt's your turn! You have %i HP remaining.|n" % character.db.hp)
|
||||||
|
|
||||||
def next_turn(self):
|
def next_turn(self):
|
||||||
"""
|
"""
|
||||||
Advances to the next character in the turn order.
|
Advances to the next character in the turn order.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Check to see if every character disengaged as their last action. If so, end combat.
|
# Check to see if every character disengaged as their last action. If so, end combat.
|
||||||
disengage_check = True
|
disengage_check = True
|
||||||
for fighter in self.db.fighters:
|
for fighter in self.db.fighters:
|
||||||
if fighter.db.Combat_LastAction != "disengage": # If a character has done anything but disengage
|
if fighter.db.Combat_LastAction != "disengage": # If a character has done anything but disengage
|
||||||
disengage_check = False
|
disengage_check = False
|
||||||
if disengage_check == True: # All characters have disengaged
|
if disengage_check: # All characters have disengaged
|
||||||
self.obj.msg_contents("All fighters have disengaged! Combat is over!")
|
self.obj.msg_contents("All fighters have disengaged! Combat is over!")
|
||||||
self.stop() # Stop this script and end combat.
|
self.stop() # Stop this script and end combat.
|
||||||
return
|
return
|
||||||
|
|
||||||
# Check to see if only one character is left standing. If so, end combat.
|
# Check to see if only one character is left standing. If so, end combat.
|
||||||
defeated_characters = 0
|
defeated_characters = 0
|
||||||
for fighter in self.db.fighters:
|
for fighter in self.db.fighters:
|
||||||
if fighter.db.HP == 0:
|
if fighter.db.HP == 0:
|
||||||
defeated_characters += 1 # Add 1 for every fighter with 0 HP left (defeated)
|
defeated_characters += 1 # Add 1 for every fighter with 0 HP left (defeated)
|
||||||
if defeated_characters == (len(self.db.fighters) - 1): # If only one character isn't defeated
|
if defeated_characters == (len(self.db.fighters) - 1): # If only one character isn't defeated
|
||||||
for fighter in self.db.fighters:
|
for fighter in self.db.fighters:
|
||||||
if fighter.db.HP != 0:
|
if fighter.db.HP != 0:
|
||||||
LastStanding = fighter # Pick the one fighter left with HP remaining
|
LastStanding = fighter # Pick the one fighter left with HP remaining
|
||||||
self.obj.msg_contents("Only %s remains! Combat is over!" % LastStanding)
|
self.obj.msg_contents("Only %s remains! Combat is over!" % LastStanding)
|
||||||
self.stop() # Stop this script and end combat.
|
self.stop() # Stop this script and end combat.
|
||||||
return
|
return
|
||||||
|
|
||||||
# Cycle to the next turn.
|
# Cycle to the next turn.
|
||||||
currentchar = self.db.fighters[self.db.turn]
|
currentchar = self.db.fighters[self.db.turn]
|
||||||
self.db.turn += 1 # Go to the next in the turn order.
|
self.db.turn += 1 # Go to the next in the turn order.
|
||||||
if self.db.turn > len(self.db.fighters) - 1:
|
if self.db.turn > len(self.db.fighters) - 1:
|
||||||
self.db.turn = 0 # Go back to the first in the turn order once you reach the end.
|
self.db.turn = 0 # Go back to the first in the turn order once you reach the end.
|
||||||
newchar = self.db.fighters[self.db.turn] # Note the new character
|
newchar = self.db.fighters[self.db.turn] # Note the new character
|
||||||
self.db.timer = 30 + self.time_until_next_repeat() # Reset the timer.
|
self.db.timer = 30 + self.time_until_next_repeat() # Reset the timer.
|
||||||
self.db.timeout_warning_given = False # Reset the timeout warning.
|
self.db.timeout_warning_given = False # Reset the timeout warning.
|
||||||
self.obj.msg_contents("%s's turn ends - %s's turn begins!" % (currentchar, newchar))
|
self.obj.msg_contents("%s's turn ends - %s's turn begins!" % (currentchar, newchar))
|
||||||
self.start_turn(newchar) # Start the new character's turn.
|
self.start_turn(newchar) # Start the new character's turn.
|
||||||
|
|
||||||
|
|
||||||
def turn_end_check(self, character):
|
def turn_end_check(self, character):
|
||||||
"""
|
"""
|
||||||
Tests to see if a character's turn is over, and cycles to the next turn if it is.
|
Tests to see if a character's turn is over, and cycles to the next turn if it is.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to test for end of turn
|
character (obj): Character to test for end of turn
|
||||||
"""
|
"""
|
||||||
if not character.db.Combat_ActionsLeft: # Character has no actions remaining
|
if not character.db.Combat_ActionsLeft: # Character has no actions remaining
|
||||||
self.next_turn()
|
self.next_turn()
|
||||||
return
|
return
|
||||||
|
|
||||||
def join_fight(self, character):
|
def join_fight(self, character):
|
||||||
"""
|
"""
|
||||||
Adds a new character to a fight already in progress.
|
Adds a new character to a fight already in progress.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
character (obj): Character to be added to the fight.
|
character (obj): Character to be added to the fight.
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue