mirror of
https://github.com/evennia/evennia.git
synced 2026-03-28 02:36:32 +01:00
Merge branch 'turnbattle_refactor' into develop
This commit is contained in:
commit
b4888a5b17
6 changed files with 1831 additions and 3545 deletions
|
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
Simple turn-based combat system
|
||||
|
||||
Contrib - Tim Ashley Jenkins 2017
|
||||
Contrib - Tim Ashley Jenkins 2017, Refactor by Griatch 2022
|
||||
|
||||
This is a framework for a simple turn-based combat system, similar
|
||||
to those used in D&D-style tabletop role playing games. It allows
|
||||
|
|
@ -62,237 +62,235 @@ COMBAT FUNCTIONS START HERE
|
|||
"""
|
||||
|
||||
|
||||
def roll_init(character):
|
||||
class BasicCombatRules:
|
||||
"""
|
||||
Rolls a number between 1-1000 to determine initiative.
|
||||
Stores all combat rules and helper methods.
|
||||
|
||||
Args:
|
||||
character (obj): The character to determine initiative for
|
||||
|
||||
Returns:
|
||||
initiative (int): The character's place in initiative - higher
|
||||
numbers go first.
|
||||
|
||||
Notes:
|
||||
By default, does not reference the character and simply returns
|
||||
a random integer from 1 to 1000.
|
||||
|
||||
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
|
||||
character has a 'dexterity' attribute, you can use it to give that character
|
||||
an advantage in turn order, like so:
|
||||
|
||||
return (randint(1,20)) + character.db.dexterity
|
||||
|
||||
This way, characters with a higher dexterity will go first more often.
|
||||
"""
|
||||
return randint(1, 1000)
|
||||
|
||||
def roll_init(self, character):
|
||||
"""
|
||||
Rolls a number between 1-1000 to determine initiative.
|
||||
|
||||
def get_attack(attacker, defender):
|
||||
"""
|
||||
Returns a value for an attack roll.
|
||||
Args:
|
||||
character (obj): The character to determine initiative for
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
Returns:
|
||||
initiative (int): The character's place in initiative - higher
|
||||
numbers go first.
|
||||
|
||||
Returns:
|
||||
attack_value (int): Attack roll value, compared against a defense value
|
||||
to determine whether an attack hits or misses.
|
||||
Notes:
|
||||
By default, does not reference the character and simply returns
|
||||
a random integer from 1 to 1000.
|
||||
|
||||
Notes:
|
||||
By default, returns a random integer from 1 to 100 without using any
|
||||
properties from either the attacker or defender.
|
||||
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
|
||||
character has a 'dexterity' attribute, you can use it to give that character
|
||||
an advantage in turn order, like so:
|
||||
|
||||
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
|
||||
to this function, even though nothing from either one are used in this example.
|
||||
"""
|
||||
# For this example, just return a random integer up to 100.
|
||||
attack_value = randint(1, 100)
|
||||
return attack_value
|
||||
return (randint(1,20)) + character.db.dexterity
|
||||
|
||||
This way, characters with a higher dexterity will go first more often.
|
||||
"""
|
||||
return randint(1, 1000)
|
||||
|
||||
def get_defense(attacker, defender):
|
||||
"""
|
||||
Returns a value for defense, which an attack roll must equal or exceed in order
|
||||
for an attack to hit.
|
||||
def get_attack(self, attacker, defender):
|
||||
"""
|
||||
Returns a value for an attack roll.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Returns:
|
||||
defense_value (int): Defense value, compared against an attack roll
|
||||
to determine whether an attack hits or misses.
|
||||
Returns:
|
||||
attack_value (int): Attack roll value, compared against a defense value
|
||||
to determine whether an attack hits or misses.
|
||||
|
||||
Notes:
|
||||
By default, returns 50, not taking any properties of the defender or
|
||||
attacker into account.
|
||||
Notes:
|
||||
By default, returns a random integer from 1 to 100 without using any
|
||||
properties from either the attacker or defender.
|
||||
|
||||
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.
|
||||
defense_value = 50
|
||||
return defense_value
|
||||
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
|
||||
to this function, even though nothing from either one are used in this example.
|
||||
"""
|
||||
# For this example, just return a random integer up to 100.
|
||||
attack_value = randint(1, 100)
|
||||
return attack_value
|
||||
|
||||
def get_defense(self, attacker, defender):
|
||||
"""
|
||||
Returns a value for defense, which an attack roll must equal or exceed in order
|
||||
for an attack to hit.
|
||||
|
||||
def get_damage(attacker, defender):
|
||||
"""
|
||||
Returns a value for damage to be deducted from the defender's HP after abilities
|
||||
successful hit.
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being damaged
|
||||
Returns:
|
||||
defense_value (int): Defense value, compared against an attack roll
|
||||
to determine whether an attack hits or misses.
|
||||
|
||||
Returns:
|
||||
damage_value (int): Damage value, which is to be deducted from the defending
|
||||
character's HP.
|
||||
Notes:
|
||||
By default, returns 50, not taking any properties of the defender or
|
||||
attacker into account.
|
||||
|
||||
Notes:
|
||||
By default, returns a random integer from 15 to 25 without using any
|
||||
properties from either the attacker or defender.
|
||||
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.
|
||||
defense_value = 50
|
||||
return defense_value
|
||||
|
||||
Again, this can be expanded upon.
|
||||
"""
|
||||
# For this example, just generate a number between 15 and 25.
|
||||
damage_value = randint(15, 25)
|
||||
return damage_value
|
||||
def get_damage(self, attacker, defender):
|
||||
"""
|
||||
Returns a value for damage to be deducted from the defender's HP after abilities
|
||||
successful hit.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being damaged
|
||||
|
||||
def apply_damage(defender, damage):
|
||||
"""
|
||||
Applies damage to a target, reducing their HP by the damage amount to a
|
||||
minimum of 0.
|
||||
Returns:
|
||||
damage_value (int): Damage value, which is to be deducted from the defending
|
||||
character's HP.
|
||||
|
||||
Args:
|
||||
defender (obj): Character taking damage
|
||||
damage (int): Amount of damage being taken
|
||||
"""
|
||||
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 defender.db.hp <= 0:
|
||||
defender.db.hp = 0
|
||||
Notes:
|
||||
By default, returns a random integer from 15 to 25 without using any
|
||||
properties from either the attacker or defender.
|
||||
|
||||
Again, this can be expanded upon.
|
||||
"""
|
||||
# For this example, just generate a number between 15 and 25.
|
||||
damage_value = randint(15, 25)
|
||||
return damage_value
|
||||
|
||||
def at_defeat(defeated):
|
||||
"""
|
||||
Announces the defeat of a fighter in combat.
|
||||
def apply_damage(self, defender, damage):
|
||||
"""
|
||||
Applies damage to a target, reducing their HP by the damage amount to a
|
||||
minimum of 0.
|
||||
|
||||
Args:
|
||||
defeated (obj): Fighter that's been defeated.
|
||||
|
||||
Notes:
|
||||
All this does is announce a defeat message by default, but if you
|
||||
want anything else to happen to defeated fighters (like putting them
|
||||
into a dying state or something similar) then this is the place to
|
||||
do it.
|
||||
"""
|
||||
defeated.location.msg_contents("%s has been defeated!" % defeated)
|
||||
|
||||
|
||||
def resolve_attack(attacker, defender, attack_value=None, defense_value=None):
|
||||
"""
|
||||
Resolves an attack and outputs the result.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Notes:
|
||||
Even though the attack and defense values are calculated
|
||||
extremely simply, they are separated out into their own functions
|
||||
so that they are easier to expand upon.
|
||||
"""
|
||||
# Get an attack roll from the attacker.
|
||||
if not attack_value:
|
||||
attack_value = get_attack(attacker, defender)
|
||||
# Get a defense value from the defender.
|
||||
if not defense_value:
|
||||
defense_value = get_defense(attacker, defender)
|
||||
# If the attack value is lower than the defense value, miss. Otherwise, hit.
|
||||
if attack_value < defense_value:
|
||||
attacker.location.msg_contents("%s's attack misses %s!" % (attacker, defender))
|
||||
else:
|
||||
damage_value = get_damage(attacker, defender) # Calculate damage value.
|
||||
# Announce damage dealt and apply damage.
|
||||
attacker.location.msg_contents(
|
||||
"%s hits %s for %i damage!" % (attacker, defender, damage_value)
|
||||
)
|
||||
apply_damage(defender, damage_value)
|
||||
# If defender HP is reduced to 0 or less, call at_defeat.
|
||||
Args:
|
||||
defender (obj): Character taking damage
|
||||
damage (int): Amount of damage being taken
|
||||
"""
|
||||
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 defender.db.hp <= 0:
|
||||
at_defeat(defender)
|
||||
defender.db.hp = 0
|
||||
|
||||
def at_defeat(self, defeated):
|
||||
"""
|
||||
Announces the defeat of a fighter in combat.
|
||||
|
||||
Args:
|
||||
defeated (obj): Fighter that's been defeated.
|
||||
|
||||
Notes:
|
||||
All this does is announce a defeat message by default, but if you
|
||||
want anything else to happen to defeated fighters (like putting them
|
||||
into a dying state or something similar) then this is the place to
|
||||
do it.
|
||||
"""
|
||||
defeated.location.msg_contents("%s has been defeated!" % defeated)
|
||||
|
||||
def resolve_attack(self, attacker, defender, attack_value=None, defense_value=None):
|
||||
"""
|
||||
Resolves an attack and outputs the result.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Notes:
|
||||
Even though the attack and defense values are calculated
|
||||
extremely simply, they are separated out into their own functions
|
||||
so that they are easier to expand upon.
|
||||
"""
|
||||
# Get an attack roll from the attacker.
|
||||
if not attack_value:
|
||||
attack_value = self.get_attack(attacker, defender)
|
||||
# Get a defense value from the defender.
|
||||
if not defense_value:
|
||||
defense_value = self.get_defense(attacker, defender)
|
||||
# If the attack value is lower than the defense value, miss. Otherwise, hit.
|
||||
if attack_value < defense_value:
|
||||
attacker.location.msg_contents("%s's attack misses %s!" % (attacker, defender))
|
||||
else:
|
||||
damage_value = self.get_damage(attacker, defender) # Calculate damage value.
|
||||
# Announce damage dealt and apply damage.
|
||||
attacker.location.msg_contents(
|
||||
"%s hits %s for %i damage!" % (attacker, defender, damage_value)
|
||||
)
|
||||
self.apply_damage(defender, damage_value)
|
||||
# If defender HP is reduced to 0 or less, call at_defeat.
|
||||
if defender.db.hp <= 0:
|
||||
self.at_defeat(defender)
|
||||
|
||||
def combat_cleanup(self, character):
|
||||
"""
|
||||
Cleans up all the temporary combat-related attributes on a character.
|
||||
|
||||
Args:
|
||||
character (obj): Character to have their combat attributes removed
|
||||
|
||||
Notes:
|
||||
Any attribute whose key begins with 'combat_' is temporary and no
|
||||
longer needed once a fight ends.
|
||||
"""
|
||||
for attr in character.attributes.all():
|
||||
if attr.key[:7] == "combat_": # If the attribute name starts with 'combat_'...
|
||||
character.attributes.remove(key=attr.key) # ...then delete it!
|
||||
|
||||
def is_in_combat(self, character):
|
||||
"""
|
||||
Returns true if the given character is in combat.
|
||||
|
||||
Args:
|
||||
character (obj): Character to determine if is in combat or not
|
||||
|
||||
Returns:
|
||||
(bool): True if in combat or False if not in combat
|
||||
"""
|
||||
return bool(character.db.combat_turnhandler)
|
||||
|
||||
def is_turn(self, character):
|
||||
"""
|
||||
Returns true if it's currently the given character's turn in combat.
|
||||
|
||||
Args:
|
||||
character (obj): Character to determine if it is their turn or not
|
||||
|
||||
Returns:
|
||||
(bool): True if it is their turn or False otherwise
|
||||
"""
|
||||
turnhandler = character.db.combat_turnhandler
|
||||
currentchar = turnhandler.db.fighters[turnhandler.db.turn]
|
||||
return bool(character == currentchar)
|
||||
|
||||
def spend_action(self, character, actions, action_name=None):
|
||||
"""
|
||||
Spends a character's available combat actions and checks for end of turn.
|
||||
|
||||
Args:
|
||||
character (obj): Character spending the action
|
||||
actions (int) or 'all': Number of actions to spend, or 'all' to spend all actions
|
||||
|
||||
Keyword Args:
|
||||
action_name (str or None): If a string is given, sets character's last action in
|
||||
combat to provided string
|
||||
"""
|
||||
if action_name:
|
||||
character.db.combat_lastaction = action_name
|
||||
if actions == "all": # If spending all actions
|
||||
character.db.combat_actionsleft = 0 # Set actions to 0
|
||||
else:
|
||||
character.db.combat_actionsleft -= actions # Use up actions.
|
||||
if character.db.combat_actionsleft < 0:
|
||||
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.
|
||||
|
||||
|
||||
def combat_cleanup(character):
|
||||
"""
|
||||
Cleans up all the temporary combat-related attributes on a character.
|
||||
|
||||
Args:
|
||||
character (obj): Character to have their combat attributes removed
|
||||
|
||||
Notes:
|
||||
Any attribute whose key begins with 'combat_' is temporary and no
|
||||
longer needed once a fight ends.
|
||||
"""
|
||||
for attr in character.attributes.all():
|
||||
if attr.key[:7] == "combat_": # If the attribute name starts with 'combat_'...
|
||||
character.attributes.remove(key=attr.key) # ...then delete it!
|
||||
|
||||
|
||||
def is_in_combat(character):
|
||||
"""
|
||||
Returns true if the given character is in combat.
|
||||
|
||||
Args:
|
||||
character (obj): Character to determine if is in combat or not
|
||||
|
||||
Returns:
|
||||
(bool): True if in combat or False if not in combat
|
||||
"""
|
||||
return bool(character.db.combat_turnhandler)
|
||||
|
||||
|
||||
def is_turn(character):
|
||||
"""
|
||||
Returns true if it's currently the given character's turn in combat.
|
||||
|
||||
Args:
|
||||
character (obj): Character to determine if it is their turn or not
|
||||
|
||||
Returns:
|
||||
(bool): True if it is their turn or False otherwise
|
||||
"""
|
||||
turnhandler = character.db.combat_turnhandler
|
||||
currentchar = turnhandler.db.fighters[turnhandler.db.turn]
|
||||
return bool(character == currentchar)
|
||||
|
||||
|
||||
def spend_action(character, actions, action_name=None):
|
||||
"""
|
||||
Spends a character's available combat actions and checks for end of turn.
|
||||
|
||||
Args:
|
||||
character (obj): Character spending the action
|
||||
actions (int) or 'all': Number of actions to spend, or 'all' to spend all actions
|
||||
|
||||
Keyword Args:
|
||||
action_name (str or None): If a string is given, sets character's last action in
|
||||
combat to provided string
|
||||
"""
|
||||
if action_name:
|
||||
character.db.combat_lastaction = action_name
|
||||
if actions == "all": # If spending all actions
|
||||
character.db.combat_actionsleft = 0 # Set actions to 0
|
||||
else:
|
||||
character.db.combat_actionsleft -= actions # Use up actions.
|
||||
if character.db.combat_actionsleft < 0:
|
||||
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.
|
||||
|
||||
COMBAT_RULES = BasicCombatRules()
|
||||
|
||||
"""
|
||||
----------------------------------------------------------------------------
|
||||
|
|
@ -306,6 +304,7 @@ class TBBasicCharacter(DefaultCharacter):
|
|||
A character able to participate in turn-based combat. Has attributes for current
|
||||
and maximum HP, and access to combat commands.
|
||||
"""
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def at_object_creation(self):
|
||||
"""
|
||||
|
|
@ -339,7 +338,7 @@ class TBBasicCharacter(DefaultCharacter):
|
|||
|
||||
"""
|
||||
# Keep the character from moving if at 0 HP or in combat.
|
||||
if is_in_combat(self):
|
||||
if self.rules.is_in_combat(self):
|
||||
self.msg("You can't exit a room while in combat!")
|
||||
return False # Returning false keeps the character from moving.
|
||||
if self.db.HP <= 0:
|
||||
|
|
@ -367,6 +366,8 @@ class TBBasicTurnHandler(DefaultScript):
|
|||
remaining participants choose to end the combat with the 'disengage' command.
|
||||
"""
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def at_script_creation(self):
|
||||
"""
|
||||
Called once, when the script is created.
|
||||
|
|
@ -388,9 +389,10 @@ class TBBasicTurnHandler(DefaultScript):
|
|||
# Add a reference to this script to the room
|
||||
self.obj.db.combat_turnhandler = self
|
||||
|
||||
# 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.
|
||||
ordered_by_roll = sorted(self.db.fighters, key=roll_init, reverse=True)
|
||||
# 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 method and can be
|
||||
# customized easily.
|
||||
ordered_by_roll = sorted(self.db.fighters, key=self.rules.roll_init, reverse=True)
|
||||
self.db.fighters = ordered_by_roll
|
||||
|
||||
# Announce the turn order.
|
||||
|
|
@ -408,7 +410,7 @@ class TBBasicTurnHandler(DefaultScript):
|
|||
Called at script termination.
|
||||
"""
|
||||
for fighter in self.db.fighters:
|
||||
combat_cleanup(fighter) # Clean up the combat attributes for every fighter.
|
||||
self.rules.combat_cleanup(fighter) # Clean up the combat attributes for every fighter.
|
||||
self.obj.db.combat_turnhandler = None # Remove reference to turn handler in location
|
||||
|
||||
def at_repeat(self):
|
||||
|
|
@ -423,7 +425,7 @@ class TBBasicTurnHandler(DefaultScript):
|
|||
if self.db.timer <= 0:
|
||||
# Force current character to disengage if timer runs out.
|
||||
self.obj.msg_contents("%s's turn timed out!" % currentchar)
|
||||
spend_action(
|
||||
self.rules.spend_action(
|
||||
currentchar, "all", action_name="disengage"
|
||||
) # Spend all remaining actions.
|
||||
return
|
||||
|
|
@ -439,7 +441,8 @@ class TBBasicTurnHandler(DefaultScript):
|
|||
Args:
|
||||
character (obj): Character to initialize for combat.
|
||||
"""
|
||||
combat_cleanup(character) # Clean up leftover combat attributes beforehand, just in case.
|
||||
# Clean up leftover combat attributes beforehand, just in case.
|
||||
self.rules.combat_cleanup(character)
|
||||
character.db.combat_actionsleft = (
|
||||
0 # Actions remaining - start of turn adds to this, turn ends when it reaches 0
|
||||
)
|
||||
|
|
@ -560,6 +563,9 @@ class CmdFight(Command):
|
|||
key = "fight"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
combat_handler_class = TBBasicTurnHandler
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
|
|
@ -570,7 +576,7 @@ class CmdFight(Command):
|
|||
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!")
|
||||
return
|
||||
if is_in_combat(self.caller): # Already in a fight
|
||||
if self.rules.is_in_combat(self.caller): # Already in a fight
|
||||
self.caller.msg("You're already in a fight!")
|
||||
return
|
||||
for thing in here.contents: # Test everything in the room to add it to the fight.
|
||||
|
|
@ -585,8 +591,7 @@ class CmdFight(Command):
|
|||
return
|
||||
here.msg_contents("%s starts a fight!" % self.caller)
|
||||
# Add a turn handler script to the room, which starts combat.
|
||||
here.scripts.add("contrib.game_systems.turnbattle.tb_basic.TBBasicTurnHandler")
|
||||
# Remember you'll have to change the path to the script if you copy this code to your own modules!
|
||||
here.scripts.add(self.command_handler_class)
|
||||
|
||||
|
||||
class CmdAttack(Command):
|
||||
|
|
@ -603,15 +608,17 @@ class CmdAttack(Command):
|
|||
key = "attack"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"This performs the actual command."
|
||||
"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 self.rules.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)")
|
||||
return
|
||||
|
||||
if not is_turn(self.caller): # If it's not your turn, can't attack.
|
||||
if not self.rules.is_turn(self.caller): # If it's not your turn, can't attack.
|
||||
self.caller.msg("You can only do that on your turn.")
|
||||
return
|
||||
|
||||
|
|
@ -634,8 +641,8 @@ class CmdAttack(Command):
|
|||
return
|
||||
|
||||
"If everything checks out, call the attack resolving function."
|
||||
resolve_attack(attacker, defender)
|
||||
spend_action(self.caller, 1, action_name="attack") # Use up one action.
|
||||
self.rules.resolve_attack(attacker, defender)
|
||||
self.rules.spend_action(self.caller, 1, action_name="attack") # Use up one action.
|
||||
|
||||
|
||||
class CmdPass(Command):
|
||||
|
|
@ -653,22 +660,25 @@ class CmdPass(Command):
|
|||
aliases = ["wait", "hold"]
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
if not is_in_combat(self.caller): # Can only pass a turn in combat.
|
||||
if not self.rules.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)")
|
||||
return
|
||||
|
||||
if not is_turn(self.caller): # Can only pass if it's your turn.
|
||||
if not self.rules.is_turn(self.caller): # Can only pass if it's your turn.
|
||||
self.caller.msg("You can only do that on your turn.")
|
||||
return
|
||||
|
||||
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 all remaining actions.
|
||||
self.rules.spend_action(self.caller, "all", action_name="pass")
|
||||
|
||||
|
||||
class CmdDisengage(Command):
|
||||
|
|
@ -687,20 +697,23 @@ class CmdDisengage(Command):
|
|||
aliases = ["spare"]
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
if not is_in_combat(self.caller): # If you're not in combat
|
||||
if not self.rules.is_in_combat(self.caller): # If you're not in combat
|
||||
self.caller.msg("You can only do that in combat. (see: help fight)")
|
||||
return
|
||||
|
||||
if not is_turn(self.caller): # If it's not your turn
|
||||
if not self.rules.is_turn(self.caller): # If it's not your turn
|
||||
self.caller.msg("You can only do that on your turn.")
|
||||
return
|
||||
|
||||
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 all remaining actions.
|
||||
self.rules.spend_action(self.caller, "all", action_name="disengage")
|
||||
"""
|
||||
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.
|
||||
|
|
@ -721,10 +734,12 @@ class CmdRest(Command):
|
|||
key = "rest"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"This performs the actual command."
|
||||
|
||||
if is_in_combat(self.caller): # If you're in combat
|
||||
if self.rules.is_in_combat(self.caller): # If you're in combat
|
||||
self.caller.msg("You can't rest while you're in combat.")
|
||||
return
|
||||
|
||||
|
|
@ -748,17 +763,21 @@ class CmdCombatHelp(CmdHelp):
|
|||
topics related to the game.
|
||||
"""
|
||||
|
||||
rules = COMBAT_RULES
|
||||
combat_help_text = (
|
||||
"Available combat commands:|/"
|
||||
"|wAttack:|n Attack a target, attempting to deal damage.|/"
|
||||
"|wPass:|n Pass your turn without further action.|/"
|
||||
"|wDisengage:|n End your turn and attempt to end combat.|/"
|
||||
)
|
||||
|
||||
# Just like the default help command, but will give quick
|
||||
# tips on combat when used in a fight with no arguments.
|
||||
|
||||
def func(self):
|
||||
if is_in_combat(self.caller) and not self.args: # In combat and entered 'help' alone
|
||||
self.caller.msg(
|
||||
"Available combat commands:|/"
|
||||
+ "|wAttack:|n Attack a target, attempting to deal damage.|/"
|
||||
+ "|wPass:|n Pass your turn without further action.|/"
|
||||
+ "|wDisengage:|n End your turn and attempt to end combat.|/"
|
||||
)
|
||||
# In combat and entered 'help' alone
|
||||
if self.rules.is_in_combat(self.caller) and not self.args:
|
||||
self.caller.msg(self.combat_help_text)
|
||||
else:
|
||||
super().func() # Call the default help command
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"""
|
||||
Simple turn-based combat system with equipment
|
||||
|
||||
Contrib - Tim Ashley Jenkins 2017
|
||||
Contrib - Tim Ashley Jenkins 2017, Refactor by Griatch 2022
|
||||
|
||||
This is a version of the 'turnbattle' contrib with a basic system for
|
||||
weapons and armor implemented. Weapons can have unique damage ranges
|
||||
|
|
@ -55,8 +55,8 @@ in your game and using it as-is.
|
|||
"""
|
||||
|
||||
from random import randint
|
||||
from evennia import DefaultCharacter, Command, default_cmds, DefaultScript, DefaultObject
|
||||
from evennia.commands.default.help import CmdHelp
|
||||
from evennia import Command, default_cmds, DefaultObject
|
||||
from . import tb_basic
|
||||
|
||||
"""
|
||||
----------------------------------------------------------------------------
|
||||
|
|
@ -74,276 +74,156 @@ COMBAT FUNCTIONS START HERE
|
|||
"""
|
||||
|
||||
|
||||
def roll_init(character):
|
||||
class EquipmentCombatRules(tb_basic.BasicCombatRules):
|
||||
"""
|
||||
Rolls a number between 1-1000 to determine initiative.
|
||||
Has all the methods of the basic combat, with the addition of equipment.
|
||||
|
||||
Args:
|
||||
character (obj): The character to determine initiative for
|
||||
|
||||
Returns:
|
||||
initiative (int): The character's place in initiative - higher
|
||||
numbers go first.
|
||||
|
||||
Notes:
|
||||
By default, does not reference the character and simply returns
|
||||
a random integer from 1 to 1000.
|
||||
|
||||
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
|
||||
character has a 'dexterity' attribute, you can use it to give that character
|
||||
an advantage in turn order, like so:
|
||||
|
||||
return (randint(1,20)) + character.db.dexterity
|
||||
|
||||
This way, characters with a higher dexterity will go first more often.
|
||||
"""
|
||||
return randint(1, 1000)
|
||||
|
||||
def get_attack(self, attacker, defender):
|
||||
"""
|
||||
Returns a value for an attack roll.
|
||||
|
||||
def get_attack(attacker, defender):
|
||||
"""
|
||||
Returns a value for an attack roll.
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
Returns:
|
||||
attack_value (int): Attack roll value, compared against a defense value
|
||||
to determine whether an attack hits or misses.
|
||||
|
||||
Returns:
|
||||
attack_value (int): Attack roll value, compared against a defense value
|
||||
to determine whether an attack hits or misses.
|
||||
Notes:
|
||||
In this example, a weapon's accuracy bonus is factored into the attack
|
||||
roll. Lighter weapons are more accurate but less damaging, and heavier
|
||||
weapons are less accurate but deal more damage. Of course, you can
|
||||
change this paradigm completely in your own game.
|
||||
"""
|
||||
# Start with a roll from 1 to 100.
|
||||
attack_value = randint(1, 100)
|
||||
accuracy_bonus = 0
|
||||
# If armed, add weapon's accuracy bonus.
|
||||
if attacker.db.wielded_weapon:
|
||||
weapon = attacker.db.wielded_weapon
|
||||
accuracy_bonus += weapon.db.accuracy_bonus
|
||||
# If unarmed, use character's unarmed accuracy bonus.
|
||||
else:
|
||||
accuracy_bonus += attacker.db.unarmed_accuracy
|
||||
# Add the accuracy bonus to the attack roll.
|
||||
attack_value += accuracy_bonus
|
||||
return attack_value
|
||||
|
||||
Notes:
|
||||
In this example, a weapon's accuracy bonus is factored into the attack
|
||||
roll. Lighter weapons are more accurate but less damaging, and heavier
|
||||
weapons are less accurate but deal more damage. Of course, you can
|
||||
change this paradigm completely in your own game.
|
||||
"""
|
||||
# Start with a roll from 1 to 100.
|
||||
attack_value = randint(1, 100)
|
||||
accuracy_bonus = 0
|
||||
# If armed, add weapon's accuracy bonus.
|
||||
if attacker.db.wielded_weapon:
|
||||
weapon = attacker.db.wielded_weapon
|
||||
accuracy_bonus += weapon.db.accuracy_bonus
|
||||
# If unarmed, use character's unarmed accuracy bonus.
|
||||
else:
|
||||
accuracy_bonus += attacker.db.unarmed_accuracy
|
||||
# Add the accuracy bonus to the attack roll.
|
||||
attack_value += accuracy_bonus
|
||||
return attack_value
|
||||
def get_defense(self, attacker, defender):
|
||||
"""
|
||||
Returns a value for defense, which an attack roll must equal or exceed in order
|
||||
for an attack to hit.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
def get_defense(attacker, defender):
|
||||
"""
|
||||
Returns a value for defense, which an attack roll must equal or exceed in order
|
||||
for an attack to hit.
|
||||
Returns:
|
||||
defense_value (int): Defense value, compared against an attack roll
|
||||
to determine whether an attack hits or misses.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
Notes:
|
||||
Characters are given a default defense value of 50 which can be
|
||||
modified up or down by armor. In this example, wearing armor actually
|
||||
makes you a little easier to hit, but reduces incoming damage.
|
||||
"""
|
||||
# Start with a defense value of 50 for a 50/50 chance to hit.
|
||||
defense_value = 50
|
||||
# Modify this value based on defender's armor.
|
||||
if defender.db.worn_armor:
|
||||
armor = defender.db.worn_armor
|
||||
defense_value += armor.db.defense_modifier
|
||||
return defense_value
|
||||
|
||||
Returns:
|
||||
defense_value (int): Defense value, compared against an attack roll
|
||||
to determine whether an attack hits or misses.
|
||||
def get_damage(self, attacker, defender):
|
||||
"""
|
||||
Returns a value for damage to be deducted from the defender's HP after abilities
|
||||
successful hit.
|
||||
|
||||
Notes:
|
||||
Characters are given a default defense value of 50 which can be
|
||||
modified up or down by armor. In this example, wearing armor actually
|
||||
makes you a little easier to hit, but reduces incoming damage.
|
||||
"""
|
||||
# Start with a defense value of 50 for a 50/50 chance to hit.
|
||||
defense_value = 50
|
||||
# Modify this value based on defender's armor.
|
||||
if defender.db.worn_armor:
|
||||
armor = defender.db.worn_armor
|
||||
defense_value += armor.db.defense_modifier
|
||||
return defense_value
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being damaged
|
||||
|
||||
Returns:
|
||||
damage_value (int): Damage value, which is to be deducted from the defending
|
||||
character's HP.
|
||||
|
||||
def get_damage(attacker, defender):
|
||||
"""
|
||||
Returns a value for damage to be deducted from the defender's HP after abilities
|
||||
successful hit.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being damaged
|
||||
|
||||
Returns:
|
||||
damage_value (int): Damage value, which is to be deducted from the defending
|
||||
character's HP.
|
||||
|
||||
Notes:
|
||||
Damage is determined by the attacker's wielded weapon, or the attacker's
|
||||
unarmed damage range if no weapon is wielded. Incoming damage is reduced
|
||||
by the defender's armor.
|
||||
"""
|
||||
damage_value = 0
|
||||
# Generate a damage value from wielded weapon if armed
|
||||
if attacker.db.wielded_weapon:
|
||||
weapon = attacker.db.wielded_weapon
|
||||
# Roll between minimum and maximum damage
|
||||
damage_value = randint(weapon.db.damage_range[0], weapon.db.damage_range[1])
|
||||
# Use attacker's unarmed damage otherwise
|
||||
else:
|
||||
damage_value = randint(
|
||||
attacker.db.unarmed_damage_range[0], attacker.db.unarmed_damage_range[1]
|
||||
)
|
||||
# If defender is armored, reduce incoming damage
|
||||
if defender.db.worn_armor:
|
||||
armor = defender.db.worn_armor
|
||||
damage_value -= armor.db.damage_reduction
|
||||
# Make sure minimum damage is 0
|
||||
if damage_value < 0:
|
||||
Notes:
|
||||
Damage is determined by the attacker's wielded weapon, or the attacker's
|
||||
unarmed damage range if no weapon is wielded. Incoming damage is reduced
|
||||
by the defender's armor.
|
||||
"""
|
||||
damage_value = 0
|
||||
return damage_value
|
||||
# Generate a damage value from wielded weapon if armed
|
||||
if attacker.db.wielded_weapon:
|
||||
weapon = attacker.db.wielded_weapon
|
||||
# Roll between minimum and maximum damage
|
||||
damage_value = randint(weapon.db.damage_range[0], weapon.db.damage_range[1])
|
||||
# Use attacker's unarmed damage otherwise
|
||||
else:
|
||||
damage_value = randint(
|
||||
attacker.db.unarmed_damage_range[0], attacker.db.unarmed_damage_range[1]
|
||||
)
|
||||
# If defender is armored, reduce incoming damage
|
||||
if defender.db.worn_armor:
|
||||
armor = defender.db.worn_armor
|
||||
damage_value -= armor.db.damage_reduction
|
||||
# Make sure minimum damage is 0
|
||||
if damage_value < 0:
|
||||
damage_value = 0
|
||||
return damage_value
|
||||
|
||||
def resolve_attack(self, attacker, defender, attack_value=None, defense_value=None):
|
||||
"""
|
||||
Resolves an attack and outputs the result.
|
||||
|
||||
def apply_damage(defender, damage):
|
||||
"""
|
||||
Applies damage to a target, reducing their HP by the damage amount to a
|
||||
minimum of 0.
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Args:
|
||||
defender (obj): Character taking damage
|
||||
damage (int): Amount of damage being taken
|
||||
"""
|
||||
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 defender.db.hp <= 0:
|
||||
defender.db.hp = 0
|
||||
|
||||
|
||||
def at_defeat(defeated):
|
||||
"""
|
||||
Announces the defeat of a fighter in combat.
|
||||
|
||||
Args:
|
||||
defeated (obj): Fighter that's been defeated.
|
||||
|
||||
Notes:
|
||||
All this does is announce a defeat message by default, but if you
|
||||
want anything else to happen to defeated fighters (like putting them
|
||||
into a dying state or something similar) then this is the place to
|
||||
do it.
|
||||
"""
|
||||
defeated.location.msg_contents("%s has been defeated!" % defeated)
|
||||
|
||||
|
||||
def resolve_attack(attacker, defender, attack_value=None, defense_value=None):
|
||||
"""
|
||||
Resolves an attack and outputs the result.
|
||||
|
||||
Args:
|
||||
attacker (obj): Character doing the attacking
|
||||
defender (obj): Character being attacked
|
||||
|
||||
Notes:
|
||||
Even though the attack and defense values are calculated
|
||||
extremely simply, they are separated out into their own functions
|
||||
so that they are easier to expand upon.
|
||||
"""
|
||||
# Get the attacker's weapon type to reference in combat messages.
|
||||
attackers_weapon = "attack"
|
||||
if attacker.db.wielded_weapon:
|
||||
weapon = attacker.db.wielded_weapon
|
||||
attackers_weapon = weapon.db.weapon_type_name
|
||||
# Get an attack roll from the attacker.
|
||||
if not attack_value:
|
||||
attack_value = get_attack(attacker, defender)
|
||||
# Get a defense value from the defender.
|
||||
if not defense_value:
|
||||
defense_value = get_defense(attacker, defender)
|
||||
# If the attack value is lower than the defense value, miss. Otherwise, hit.
|
||||
if attack_value < defense_value:
|
||||
attacker.location.msg_contents(
|
||||
"%s's %s misses %s!" % (attacker, attackers_weapon, defender)
|
||||
)
|
||||
else:
|
||||
damage_value = get_damage(attacker, defender) # Calculate damage value.
|
||||
# Announce damage dealt and apply damage.
|
||||
if damage_value > 0:
|
||||
Notes:
|
||||
Even though the attack and defense values are calculated
|
||||
extremely simply, they are separated out into their own functions
|
||||
so that they are easier to expand upon.
|
||||
"""
|
||||
# Get the attacker's weapon type to reference in combat messages.
|
||||
attackers_weapon = "attack"
|
||||
if attacker.db.wielded_weapon:
|
||||
weapon = attacker.db.wielded_weapon
|
||||
attackers_weapon = weapon.db.weapon_type_name
|
||||
# Get an attack roll from the attacker.
|
||||
if not attack_value:
|
||||
attack_value = self.get_attack(attacker, defender)
|
||||
# Get a defense value from the defender.
|
||||
if not defense_value:
|
||||
defense_value = self.get_defense(attacker, defender)
|
||||
# If the attack value is lower than the defense value, miss. Otherwise, hit.
|
||||
if attack_value < defense_value:
|
||||
attacker.location.msg_contents(
|
||||
"%s's %s strikes %s for %i damage!"
|
||||
% (attacker, attackers_weapon, defender, damage_value)
|
||||
"%s's %s misses %s!" % (attacker, attackers_weapon, defender)
|
||||
)
|
||||
else:
|
||||
attacker.location.msg_contents(
|
||||
"%s's %s bounces harmlessly off %s!" % (attacker, attackers_weapon, defender)
|
||||
)
|
||||
apply_damage(defender, damage_value)
|
||||
# If defender HP is reduced to 0 or less, call at_defeat.
|
||||
if defender.db.hp <= 0:
|
||||
at_defeat(defender)
|
||||
damage_value = self.get_damage(attacker, defender) # Calculate damage value.
|
||||
# Announce damage dealt and apply damage.
|
||||
if damage_value > 0:
|
||||
attacker.location.msg_contents(
|
||||
"%s's %s strikes %s for %i damage!"
|
||||
% (attacker, attackers_weapon, defender, damage_value)
|
||||
)
|
||||
else:
|
||||
attacker.location.msg_contents(
|
||||
"%s's %s bounces harmlessly off %s!" % (attacker, attackers_weapon, defender)
|
||||
)
|
||||
self.apply_damage(defender, damage_value)
|
||||
# If defender HP is reduced to 0 or less, call at_defeat.
|
||||
if defender.db.hp <= 0:
|
||||
self.at_defeat(defender)
|
||||
|
||||
|
||||
def combat_cleanup(character):
|
||||
"""
|
||||
Cleans up all the temporary combat-related attributes on a character.
|
||||
|
||||
Args:
|
||||
character (obj): Character to have their combat attributes removed
|
||||
|
||||
Notes:
|
||||
Any attribute whose key begins with 'combat_' is temporary and no
|
||||
longer needed once a fight ends.
|
||||
"""
|
||||
for attr in character.attributes.all():
|
||||
if attr.key[:7] == "combat_": # If the attribute name starts with 'combat_'...
|
||||
character.attributes.remove(key=attr.key) # ...then delete it!
|
||||
|
||||
|
||||
def is_in_combat(character):
|
||||
"""
|
||||
Returns true if the given character is in combat.
|
||||
|
||||
Args:
|
||||
character (obj): Character to determine if is in combat or not
|
||||
|
||||
Returns:
|
||||
(bool): True if in combat or False if not in combat
|
||||
"""
|
||||
return bool(character.db.combat_turnhandler)
|
||||
|
||||
|
||||
def is_turn(character):
|
||||
"""
|
||||
Returns true if it's currently the given character's turn in combat.
|
||||
|
||||
Args:
|
||||
character (obj): Character to determine if it is their turn or not
|
||||
|
||||
Returns:
|
||||
(bool): True if it is their turn or False otherwise
|
||||
"""
|
||||
turnhandler = character.db.combat_turnhandler
|
||||
currentchar = turnhandler.db.fighters[turnhandler.db.turn]
|
||||
return bool(character == currentchar)
|
||||
|
||||
|
||||
def spend_action(character, actions, action_name=None):
|
||||
"""
|
||||
Spends a character's available combat actions and checks for end of turn.
|
||||
|
||||
Args:
|
||||
character (obj): Character spending the action
|
||||
actions (int) or 'all': Number of actions to spend, or 'all' to spend all actions
|
||||
|
||||
Keyword Args:
|
||||
action_name (str or None): If a string is given, sets character's last action in
|
||||
combat to provided string
|
||||
"""
|
||||
if action_name:
|
||||
character.db.combat_lastaction = action_name
|
||||
if actions == "all": # If spending all actions
|
||||
character.db.combat_actionsleft = 0 # Set actions to 0
|
||||
else:
|
||||
character.db.combat_actionsleft -= actions # Use up actions.
|
||||
if character.db.combat_actionsleft < 0:
|
||||
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.
|
||||
|
||||
COMBAT_RULES = EquipmentCombatRules()
|
||||
|
||||
"""
|
||||
----------------------------------------------------------------------------
|
||||
|
|
@ -352,7 +232,7 @@ SCRIPTS START HERE
|
|||
"""
|
||||
|
||||
|
||||
class TBEquipTurnHandler(DefaultScript):
|
||||
class TBEquipTurnHandler(tb_basic.TBBasicTurnHandler):
|
||||
"""
|
||||
This is the script that handles the progression of combat through turns.
|
||||
On creation (when a fight is started) it adds all combat-ready characters
|
||||
|
|
@ -363,174 +243,7 @@ class TBEquipTurnHandler(DefaultScript):
|
|||
Fights persist until only one participant is left with any HP or all
|
||||
remaining participants choose to end the combat with the 'disengage' command.
|
||||
"""
|
||||
|
||||
def at_script_creation(self):
|
||||
"""
|
||||
Called once, when the script is created.
|
||||
"""
|
||||
self.key = "Combat Turn Handler"
|
||||
self.interval = 5 # Once every 5 seconds
|
||||
self.persistent = True
|
||||
self.db.fighters = []
|
||||
|
||||
# Add all fighters in the room with at least 1 HP to the combat."
|
||||
for thing in self.obj.contents:
|
||||
if thing.db.hp:
|
||||
self.db.fighters.append(thing)
|
||||
|
||||
# Initialize each fighter for combat
|
||||
for fighter in self.db.fighters:
|
||||
self.initialize_for_combat(fighter)
|
||||
|
||||
# Add a reference to this script to the room
|
||||
self.obj.db.combat_turnhandler = self
|
||||
|
||||
# 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.
|
||||
ordered_by_roll = sorted(self.db.fighters, key=roll_init, reverse=True)
|
||||
self.db.fighters = ordered_by_roll
|
||||
|
||||
# Announce the turn order.
|
||||
self.obj.msg_contents("Turn order is: %s " % ", ".join(obj.key for obj in self.db.fighters))
|
||||
|
||||
# Start first fighter's turn.
|
||||
self.start_turn(self.db.fighters[0])
|
||||
|
||||
# Set up the current turn and turn timeout delay.
|
||||
self.db.turn = 0
|
||||
self.db.timer = TURN_TIMEOUT # Set timer to turn timeout specified in options
|
||||
|
||||
def at_stop(self):
|
||||
"""
|
||||
Called at script termination.
|
||||
"""
|
||||
for fighter in self.db.fighters:
|
||||
combat_cleanup(fighter) # Clean up the combat attributes for every fighter.
|
||||
self.obj.db.combat_turnhandler = None # Remove reference to turn handler in location
|
||||
|
||||
def at_repeat(self):
|
||||
"""
|
||||
Called once every self.interval seconds.
|
||||
"""
|
||||
currentchar = self.db.fighters[
|
||||
self.db.turn
|
||||
] # Note the current character in the turn order.
|
||||
self.db.timer -= self.interval # Count down the timer.
|
||||
|
||||
if self.db.timer <= 0:
|
||||
# Force current character to disengage if timer runs out.
|
||||
self.obj.msg_contents("%s's turn timed out!" % currentchar)
|
||||
spend_action(
|
||||
currentchar, "all", action_name="disengage"
|
||||
) # Spend all remaining actions.
|
||||
return
|
||||
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.
|
||||
currentchar.msg("WARNING: About to time out!")
|
||||
self.db.timeout_warning_given = True
|
||||
|
||||
def initialize_for_combat(self, character):
|
||||
"""
|
||||
Prepares a character for combat when starting or entering a fight.
|
||||
|
||||
Args:
|
||||
character (obj): Character to initialize for combat.
|
||||
"""
|
||||
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_turnhandler = (
|
||||
self # Add a reference to this turn handler script to the character
|
||||
)
|
||||
character.db.combat_lastaction = "null" # Track last action taken in combat
|
||||
|
||||
def start_turn(self, character):
|
||||
"""
|
||||
Readies a character for the start of their turn by replenishing their
|
||||
available actions and notifying them that their turn has come up.
|
||||
|
||||
Args:
|
||||
character (obj): Character to be readied.
|
||||
|
||||
Notes:
|
||||
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
|
||||
attributes. You can even add multiple different kinds of actions, I.E. actions
|
||||
separated for movement, by adding "character.db.combat_movesleft = 3" or
|
||||
something similar.
|
||||
"""
|
||||
character.db.combat_actionsleft = ACTIONS_PER_TURN # Replenish actions
|
||||
# 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)
|
||||
|
||||
def next_turn(self):
|
||||
"""
|
||||
Advances to the next character in the turn order.
|
||||
"""
|
||||
|
||||
# Check to see if every character disengaged as their last action. If so, end combat.
|
||||
disengage_check = True
|
||||
for fighter in self.db.fighters:
|
||||
if (
|
||||
fighter.db.combat_lastaction != "disengage"
|
||||
): # If a character has done anything but disengage
|
||||
disengage_check = False
|
||||
if disengage_check: # All characters have disengaged
|
||||
self.obj.msg_contents("All fighters have disengaged! Combat is over!")
|
||||
self.stop() # Stop this script and end combat.
|
||||
return
|
||||
|
||||
# Check to see if only one character is left standing. If so, end combat.
|
||||
defeated_characters = 0
|
||||
for fighter in self.db.fighters:
|
||||
if fighter.db.HP == 0:
|
||||
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
|
||||
for fighter in self.db.fighters:
|
||||
if fighter.db.HP != 0:
|
||||
LastStanding = fighter # Pick the one fighter left with HP remaining
|
||||
self.obj.msg_contents("Only %s remains! Combat is over!" % LastStanding)
|
||||
self.stop() # Stop this script and end combat.
|
||||
return
|
||||
|
||||
# Cycle to the next turn.
|
||||
currentchar = self.db.fighters[self.db.turn]
|
||||
self.db.turn += 1 # Go to the next in the turn order.
|
||||
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.
|
||||
newchar = self.db.fighters[self.db.turn] # Note the new character
|
||||
self.db.timer = TURN_TIMEOUT + self.time_until_next_repeat() # Reset the timer.
|
||||
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.start_turn(newchar) # Start the new character's turn.
|
||||
|
||||
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.
|
||||
|
||||
Args:
|
||||
character (obj): Character to test for end of turn
|
||||
"""
|
||||
if not character.db.combat_actionsleft: # Character has no actions remaining
|
||||
self.next_turn()
|
||||
return
|
||||
|
||||
def join_fight(self, character):
|
||||
"""
|
||||
Adds a new character to a fight already in progress.
|
||||
|
||||
Args:
|
||||
character (obj): Character to be added to the fight.
|
||||
"""
|
||||
# Inserts the fighter to the turn order, right behind whoever's turn it currently is.
|
||||
self.db.fighters.insert(self.db.turn, character)
|
||||
# Tick the turn counter forward one to compensate.
|
||||
self.db.turn += 1
|
||||
# Initialize the character like you do at the start.
|
||||
self.initialize_for_combat(character)
|
||||
rules = COMBAT_RULES
|
||||
|
||||
|
||||
"""
|
||||
|
|
@ -543,7 +256,9 @@ TYPECLASSES START HERE
|
|||
class TBEWeapon(DefaultObject):
|
||||
"""
|
||||
A weapon which can be wielded in combat with the 'wield' command.
|
||||
|
||||
"""
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def at_object_creation(self):
|
||||
"""
|
||||
|
|
@ -592,7 +307,7 @@ class TBEArmor(DefaultObject):
|
|||
"""
|
||||
Can't drop in combat.
|
||||
"""
|
||||
if is_in_combat(dropper):
|
||||
if self.rules.is_in_combat(dropper):
|
||||
dropper.msg("You can't doff armor in a fight!")
|
||||
return False
|
||||
return True
|
||||
|
|
@ -609,7 +324,7 @@ class TBEArmor(DefaultObject):
|
|||
"""
|
||||
Can't give away in combat.
|
||||
"""
|
||||
if is_in_combat(giver):
|
||||
if self.rules.is_in_combat(giver):
|
||||
dropper.msg("You can't doff armor in a fight!")
|
||||
return False
|
||||
return True
|
||||
|
|
@ -623,7 +338,7 @@ class TBEArmor(DefaultObject):
|
|||
giver.location.msg_contents("%s removes %s." % (giver, self))
|
||||
|
||||
|
||||
class TBEquipCharacter(DefaultCharacter):
|
||||
class TBEquipCharacter(tb_basic.TBBasicCharacter):
|
||||
"""
|
||||
A character able to participate in turn-based combat. Has attributes for current
|
||||
and maximum HP, and access to combat commands.
|
||||
|
|
@ -649,31 +364,6 @@ class TBEquipCharacter(DefaultCharacter):
|
|||
can be changed at creation and factor into combat calculations.
|
||||
"""
|
||||
|
||||
def at_pre_move(self, destination):
|
||||
"""
|
||||
Called just before starting to move this object to
|
||||
destination.
|
||||
|
||||
Args:
|
||||
destination (Object): The object we are moving to
|
||||
|
||||
Returns:
|
||||
shouldmove (bool): If we should move or not.
|
||||
|
||||
Notes:
|
||||
If this method returns False/None, the move is cancelled
|
||||
before it is even started.
|
||||
|
||||
"""
|
||||
# Keep the character from moving if at 0 HP or in combat.
|
||||
if is_in_combat(self):
|
||||
self.msg("You can't exit a room while in combat!")
|
||||
return False # Returning false keeps the character from moving.
|
||||
if self.db.HP <= 0:
|
||||
self.msg("You can't move, you've been defeated!")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
"""
|
||||
----------------------------------------------------------------------------
|
||||
|
|
@ -682,7 +372,7 @@ COMMANDS START HERE
|
|||
"""
|
||||
|
||||
|
||||
class CmdFight(Command):
|
||||
class CmdFight(tb_basic.CmdFight):
|
||||
"""
|
||||
Starts a fight with everyone in the same room as you.
|
||||
|
||||
|
|
@ -697,36 +387,11 @@ class CmdFight(Command):
|
|||
key = "fight"
|
||||
help_category = "combat"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
here = self.caller.location
|
||||
fighters = []
|
||||
|
||||
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!")
|
||||
return
|
||||
if is_in_combat(self.caller): # Already in a fight
|
||||
self.caller.msg("You're already in a fight!")
|
||||
return
|
||||
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...
|
||||
fighters.append(thing) # ...then add it to the fight.
|
||||
if len(fighters) <= 1: # If you're the only able fighter in the room
|
||||
self.caller.msg("There's nobody here to fight!")
|
||||
return
|
||||
if here.db.combat_turnhandler: # If there's already a fight going on...
|
||||
here.msg_contents("%s joins the fight!" % self.caller)
|
||||
here.db.combat_turnhandler.join_fight(self.caller) # Join the fight!
|
||||
return
|
||||
here.msg_contents("%s starts a fight!" % self.caller)
|
||||
# Add a turn handler script to the room, which starts combat.
|
||||
here.scripts.add("contrib.game_systems.turnbattle.tb_equip.TBEquipTurnHandler")
|
||||
# Remember you'll have to change the path to the script if you copy this code to your own modules!
|
||||
rules = COMBAT_RULES
|
||||
command_handler_class = TBEquipTurnHandler
|
||||
|
||||
|
||||
class CmdAttack(Command):
|
||||
class CmdAttack(tb_basic.CmdAttack):
|
||||
"""
|
||||
Attacks another character.
|
||||
|
||||
|
|
@ -740,42 +405,10 @@ class CmdAttack(Command):
|
|||
key = "attack"
|
||||
help_category = "combat"
|
||||
|
||||
def func(self):
|
||||
"This performs the actual command."
|
||||
"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.
|
||||
self.caller.msg("You can only do that in combat. (see: help fight)")
|
||||
return
|
||||
|
||||
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.")
|
||||
return
|
||||
|
||||
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.")
|
||||
return
|
||||
|
||||
attacker = self.caller
|
||||
defender = self.caller.search(self.args)
|
||||
|
||||
if not defender: # No valid target given.
|
||||
return
|
||||
|
||||
if not defender.db.hp: # Target object has no HP left or to begin with
|
||||
self.caller.msg("You can't fight that!")
|
||||
return
|
||||
|
||||
if attacker == defender: # Target and attacker are the same
|
||||
self.caller.msg("You can't attack yourself!")
|
||||
return
|
||||
|
||||
"If everything checks out, call the attack resolving function."
|
||||
resolve_attack(attacker, defender)
|
||||
spend_action(self.caller, 1, action_name="attack") # Use up one action.
|
||||
rules = COMBAT_RULES
|
||||
|
||||
|
||||
class CmdPass(Command):
|
||||
class CmdPass(tb_basic.CmdPass):
|
||||
"""
|
||||
Passes on your turn.
|
||||
|
||||
|
|
@ -790,25 +423,10 @@ class CmdPass(Command):
|
|||
aliases = ["wait", "hold"]
|
||||
help_category = "combat"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
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)")
|
||||
return
|
||||
|
||||
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.")
|
||||
return
|
||||
|
||||
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.
|
||||
rules = COMBAT_RULES
|
||||
|
||||
|
||||
class CmdDisengage(Command):
|
||||
class CmdDisengage(tb_basic.CmdDisengage):
|
||||
"""
|
||||
Passes your turn and attempts to end combat.
|
||||
|
||||
|
|
@ -824,27 +442,10 @@ class CmdDisengage(Command):
|
|||
aliases = ["spare"]
|
||||
help_category = "combat"
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
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)")
|
||||
return
|
||||
|
||||
if not is_turn(self.caller): # If it's not your turn
|
||||
self.caller.msg("You can only do that on your turn.")
|
||||
return
|
||||
|
||||
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.
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
rules = COMBAT_RULES
|
||||
|
||||
|
||||
class CmdRest(Command):
|
||||
class CmdRest(tb_basic.CmdRest):
|
||||
"""
|
||||
Recovers damage.
|
||||
|
||||
|
|
@ -858,21 +459,10 @@ class CmdRest(Command):
|
|||
key = "rest"
|
||||
help_category = "combat"
|
||||
|
||||
def func(self):
|
||||
"This performs the actual command."
|
||||
|
||||
if is_in_combat(self.caller): # If you're in combat
|
||||
self.caller.msg("You can't rest while you're in combat.")
|
||||
return
|
||||
|
||||
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)
|
||||
"""
|
||||
You'll probably want to replace this with your own system for recovering HP.
|
||||
"""
|
||||
rules = COMBAT_RULES
|
||||
|
||||
|
||||
class CmdCombatHelp(CmdHelp):
|
||||
class CmdCombatHelp(tb_basic.CmdCombatHelp):
|
||||
"""
|
||||
View help or a list of topics
|
||||
|
||||
|
|
@ -885,19 +475,7 @@ class CmdCombatHelp(CmdHelp):
|
|||
topics related to the game.
|
||||
"""
|
||||
|
||||
# Just like the default help command, but will give quick
|
||||
# tips on combat when used in a fight with no arguments.
|
||||
|
||||
def func(self):
|
||||
if is_in_combat(self.caller) and not self.args: # In combat and entered 'help' alone
|
||||
self.caller.msg(
|
||||
"Available combat commands:|/"
|
||||
+ "|wAttack:|n Attack a target, attempting to deal damage.|/"
|
||||
+ "|wPass:|n Pass your turn without further action.|/"
|
||||
+ "|wDisengage:|n End your turn and attempt to end combat.|/"
|
||||
)
|
||||
else:
|
||||
super().func() # Call the default help command
|
||||
rules = COMBAT_RULES
|
||||
|
||||
|
||||
class CmdWield(Command):
|
||||
|
|
@ -918,13 +496,15 @@ class CmdWield(Command):
|
|||
key = "wield"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
# If in combat, check to see if it's your turn.
|
||||
if is_in_combat(self.caller):
|
||||
if not is_turn(self.caller):
|
||||
if self.rules.is_in_combat(self.caller):
|
||||
if not self.rules.is_turn(self.caller):
|
||||
self.caller.msg("You can only do that on your turn.")
|
||||
return
|
||||
if not self.args:
|
||||
|
|
@ -933,7 +513,8 @@ class CmdWield(Command):
|
|||
weapon = self.caller.search(self.args, candidates=self.caller.contents)
|
||||
if not weapon:
|
||||
return
|
||||
if not weapon.is_typeclass("evennia.contrib.game_systems.turnbattle.tb_equip.TBEWeapon", exact=True):
|
||||
if not weapon.is_typeclass("evennia.contrib.game_systems.turnbattle.tb_equip.TBEWeapon",
|
||||
exact=True):
|
||||
self.caller.msg("That's not a weapon!")
|
||||
# Remember to update the path to the weapon typeclass if you move this module!
|
||||
return
|
||||
|
|
@ -948,8 +529,8 @@ class CmdWield(Command):
|
|||
"%s lowers %s and wields %s." % (self.caller, old_weapon, weapon)
|
||||
)
|
||||
# Spend an action if in combat.
|
||||
if is_in_combat(self.caller):
|
||||
spend_action(self.caller, 1, action_name="wield") # Use up one action.
|
||||
if self.rules.is_in_combat(self.caller):
|
||||
self.rules.spend_action(self.caller, 1, action_name="wield") # Use up one action.
|
||||
|
||||
|
||||
class CmdUnwield(Command):
|
||||
|
|
@ -966,13 +547,15 @@ class CmdUnwield(Command):
|
|||
key = "unwield"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
# If in combat, check to see if it's your turn.
|
||||
if is_in_combat(self.caller):
|
||||
if not is_turn(self.caller):
|
||||
if self.rules.is_in_combat(self.caller):
|
||||
if not self.rules.is_turn(self.caller):
|
||||
self.caller.msg("You can only do that on your turn.")
|
||||
return
|
||||
if not self.caller.db.wielded_weapon:
|
||||
|
|
@ -998,12 +581,14 @@ class CmdDon(Command):
|
|||
key = "don"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
# Can't do this in combat
|
||||
if is_in_combat(self.caller):
|
||||
if self.rules.is_in_combat(self.caller):
|
||||
self.caller.msg("You can't don armor in a fight!")
|
||||
return
|
||||
if not self.args:
|
||||
|
|
@ -1012,7 +597,8 @@ class CmdDon(Command):
|
|||
armor = self.caller.search(self.args, candidates=self.caller.contents)
|
||||
if not armor:
|
||||
return
|
||||
if not armor.is_typeclass("evennia.contrib.game_systems.turnbattle.tb_equip.TBEArmor", exact=True):
|
||||
if not armor.is_typeclass("evennia.contrib.game_systems.turnbattle.tb_equip.TBEArmor",
|
||||
exact=True):
|
||||
self.caller.msg("That's not armor!")
|
||||
# Remember to update the path to the armor typeclass if you move this module!
|
||||
return
|
||||
|
|
@ -1043,12 +629,14 @@ class CmdDoff(Command):
|
|||
key = "doff"
|
||||
help_category = "combat"
|
||||
|
||||
rules = COMBAT_RULES
|
||||
|
||||
def func(self):
|
||||
"""
|
||||
This performs the actual command.
|
||||
"""
|
||||
# Can't do this in combat
|
||||
if is_in_combat(self.caller):
|
||||
if self.rules.is_in_combat(self.caller):
|
||||
self.caller.msg("You can't doff armor in a fight!")
|
||||
return
|
||||
if not self.caller.db.worn_armor:
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -108,53 +108,54 @@ class TestTurnBattleBasicFunc(BaseEvenniaTest):
|
|||
def tearDown(self):
|
||||
super().tearDown()
|
||||
self.turnhandler.stop()
|
||||
self.testroom.delete()
|
||||
self.attacker.delete()
|
||||
self.defender.delete()
|
||||
self.joiner.delete()
|
||||
self.testroom.delete()
|
||||
|
||||
# Test combat functions
|
||||
def test_tbbasicfunc(self):
|
||||
# Initiative roll
|
||||
initiative = tb_basic.roll_init(self.attacker)
|
||||
initiative = tb_basic.COMBAT_RULES.roll_init(self.attacker)
|
||||
self.assertTrue(initiative >= 0 and initiative <= 1000)
|
||||
# Attack roll
|
||||
attack_roll = tb_basic.get_attack(self.attacker, self.defender)
|
||||
attack_roll = tb_basic.COMBAT_RULES.get_attack(self.attacker, self.defender)
|
||||
self.assertTrue(attack_roll >= 0 and attack_roll <= 100)
|
||||
# Defense roll
|
||||
defense_roll = tb_basic.get_defense(self.attacker, self.defender)
|
||||
defense_roll = tb_basic.COMBAT_RULES.get_defense(self.attacker, self.defender)
|
||||
self.assertTrue(defense_roll == 50)
|
||||
# Damage roll
|
||||
damage_roll = tb_basic.get_damage(self.attacker, self.defender)
|
||||
damage_roll = tb_basic.COMBAT_RULES.get_damage(self.attacker, self.defender)
|
||||
self.assertTrue(damage_roll >= 15 and damage_roll <= 25)
|
||||
# Apply damage
|
||||
self.defender.db.hp = 10
|
||||
tb_basic.apply_damage(self.defender, 3)
|
||||
tb_basic.COMBAT_RULES.apply_damage(self.defender, 3)
|
||||
self.assertTrue(self.defender.db.hp == 7)
|
||||
# Resolve attack
|
||||
self.defender.db.hp = 40
|
||||
tb_basic.resolve_attack(self.attacker, self.defender, attack_value=20, defense_value=10)
|
||||
tb_basic.COMBAT_RULES.resolve_attack(self.attacker, self.defender,
|
||||
attack_value=20, defense_value=10)
|
||||
self.assertTrue(self.defender.db.hp < 40)
|
||||
# Combat cleanup
|
||||
self.attacker.db.Combat_attribute = True
|
||||
tb_basic.combat_cleanup(self.attacker)
|
||||
tb_basic.COMBAT_RULES.combat_cleanup(self.attacker)
|
||||
self.assertFalse(self.attacker.db.combat_attribute)
|
||||
# Is in combat
|
||||
self.assertFalse(tb_basic.is_in_combat(self.attacker))
|
||||
self.assertFalse(tb_basic.COMBAT_RULES.is_in_combat(self.attacker))
|
||||
# Set up turn handler script for further tests
|
||||
self.attacker.location.scripts.add(tb_basic.TBBasicTurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_TurnHandler
|
||||
self.assertTrue(self.attacker.db.combat_TurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_turnHandler
|
||||
self.assertTrue(self.attacker.db.combat_turnHandler)
|
||||
# Set the turn handler's interval very high to keep it from repeating during tests.
|
||||
self.turnhandler.interval = 10000
|
||||
# Force turn order
|
||||
self.turnhandler.db.fighters = [self.attacker, self.defender]
|
||||
self.turnhandler.db.turn = 0
|
||||
# Test is turn
|
||||
self.assertTrue(tb_basic.is_turn(self.attacker))
|
||||
self.assertTrue(tb_basic.COMBAT_RULES.is_turn(self.attacker))
|
||||
# Spend actions
|
||||
self.attacker.db.Combat_ActionsLeft = 1
|
||||
tb_basic.spend_action(self.attacker, 1, action_name="Test")
|
||||
tb_basic.COMBAT_RULES.spend_action(self.attacker, 1, action_name="Test")
|
||||
self.assertTrue(self.attacker.db.Combat_ActionsLeft == 0)
|
||||
self.assertTrue(self.attacker.db.Combat_LastAction == "Test")
|
||||
# Initialize for combat
|
||||
|
|
@ -201,53 +202,53 @@ class TestTurnBattleEquipFunc(BaseEvenniaTest):
|
|||
def tearDown(self):
|
||||
super().tearDown()
|
||||
self.turnhandler.stop()
|
||||
self.testroom.delete()
|
||||
self.attacker.delete()
|
||||
self.defender.delete()
|
||||
self.joiner.delete()
|
||||
self.testroom.delete()
|
||||
|
||||
# Test the combat functions in tb_equip too. They work mostly the same.
|
||||
def test_tbequipfunc(self):
|
||||
# Initiative roll
|
||||
initiative = tb_equip.roll_init(self.attacker)
|
||||
initiative = tb_equip.COMBAT_RULES.roll_init(self.attacker)
|
||||
self.assertTrue(initiative >= 0 and initiative <= 1000)
|
||||
# Attack roll
|
||||
attack_roll = tb_equip.get_attack(self.attacker, self.defender)
|
||||
attack_roll = tb_equip.COMBAT_RULES.get_attack(self.attacker, self.defender)
|
||||
self.assertTrue(attack_roll >= -50 and attack_roll <= 150)
|
||||
# Defense roll
|
||||
defense_roll = tb_equip.get_defense(self.attacker, self.defender)
|
||||
defense_roll = tb_equip.COMBAT_RULES.get_defense(self.attacker, self.defender)
|
||||
self.assertTrue(defense_roll == 50)
|
||||
# Damage roll
|
||||
damage_roll = tb_equip.get_damage(self.attacker, self.defender)
|
||||
damage_roll = tb_equip.COMBAT_RULES.get_damage(self.attacker, self.defender)
|
||||
self.assertTrue(damage_roll >= 0 and damage_roll <= 50)
|
||||
# Apply damage
|
||||
self.defender.db.hp = 10
|
||||
tb_equip.apply_damage(self.defender, 3)
|
||||
tb_equip.COMBAT_RULES.apply_damage(self.defender, 3)
|
||||
self.assertTrue(self.defender.db.hp == 7)
|
||||
# Resolve attack
|
||||
self.defender.db.hp = 40
|
||||
tb_equip.resolve_attack(self.attacker, self.defender, attack_value=20, defense_value=10)
|
||||
tb_equip.COMBAT_RULES.resolve_attack(self.attacker, self.defender, attack_value=20, defense_value=10)
|
||||
self.assertTrue(self.defender.db.hp < 40)
|
||||
# Combat cleanup
|
||||
self.attacker.db.Combat_attribute = True
|
||||
tb_equip.combat_cleanup(self.attacker)
|
||||
tb_equip.COMBAT_RULES.combat_cleanup(self.attacker)
|
||||
self.assertFalse(self.attacker.db.combat_attribute)
|
||||
# Is in combat
|
||||
self.assertFalse(tb_equip.is_in_combat(self.attacker))
|
||||
self.assertFalse(tb_equip.COMBAT_RULES.is_in_combat(self.attacker))
|
||||
# Set up turn handler script for further tests
|
||||
self.attacker.location.scripts.add(tb_equip.TBEquipTurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_TurnHandler
|
||||
self.assertTrue(self.attacker.db.combat_TurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_turnHandler
|
||||
self.assertTrue(self.attacker.db.combat_turnHandler)
|
||||
# Set the turn handler's interval very high to keep it from repeating during tests.
|
||||
self.turnhandler.interval = 10000
|
||||
# Force turn order
|
||||
self.turnhandler.db.fighters = [self.attacker, self.defender]
|
||||
self.turnhandler.db.turn = 0
|
||||
# Test is turn
|
||||
self.assertTrue(tb_equip.is_turn(self.attacker))
|
||||
self.assertTrue(tb_equip.COMBAT_RULES.is_turn(self.attacker))
|
||||
# Spend actions
|
||||
self.attacker.db.Combat_ActionsLeft = 1
|
||||
tb_equip.spend_action(self.attacker, 1, action_name="Test")
|
||||
tb_equip.COMBAT_RULES.spend_action(self.attacker, 1, action_name="Test")
|
||||
self.assertTrue(self.attacker.db.Combat_ActionsLeft == 0)
|
||||
self.assertTrue(self.attacker.db.Combat_LastAction == "Test")
|
||||
# Initialize for combat
|
||||
|
|
@ -293,55 +294,57 @@ class TestTurnBattleRangeFunc(BaseEvenniaTest):
|
|||
def tearDown(self):
|
||||
super().tearDown()
|
||||
self.turnhandler.stop()
|
||||
self.testroom.delete()
|
||||
self.attacker.delete()
|
||||
self.defender.delete()
|
||||
self.joiner.delete()
|
||||
self.testroom.delete()
|
||||
|
||||
# Test combat functions in tb_range too.
|
||||
def test_tbrangefunc(self):
|
||||
# Initiative roll
|
||||
initiative = tb_range.roll_init(self.attacker)
|
||||
initiative = tb_range.COMBAT_RULES.roll_init(self.attacker)
|
||||
self.assertTrue(initiative >= 0 and initiative <= 1000)
|
||||
# Attack roll
|
||||
attack_roll = tb_range.get_attack(self.attacker, self.defender, "test")
|
||||
attack_roll = tb_range.COMBAT_RULES.get_attack(self.attacker, self.defender,
|
||||
attack_type="test")
|
||||
self.assertTrue(attack_roll >= 0 and attack_roll <= 100)
|
||||
# Defense roll
|
||||
defense_roll = tb_range.get_defense(self.attacker, self.defender, "test")
|
||||
defense_roll = tb_range.COMBAT_RULES.get_defense(self.attacker, self.defender,
|
||||
attack_type="test")
|
||||
self.assertTrue(defense_roll == 50)
|
||||
# Damage roll
|
||||
damage_roll = tb_range.get_damage(self.attacker, self.defender)
|
||||
damage_roll = tb_range.COMBAT_RULES.get_damage(self.attacker, self.defender)
|
||||
self.assertTrue(damage_roll >= 15 and damage_roll <= 25)
|
||||
# Apply damage
|
||||
self.defender.db.hp = 10
|
||||
tb_range.apply_damage(self.defender, 3)
|
||||
tb_range.COMBAT_RULES.apply_damage(self.defender, 3)
|
||||
self.assertTrue(self.defender.db.hp == 7)
|
||||
# Resolve attack
|
||||
self.defender.db.hp = 40
|
||||
tb_range.resolve_attack(
|
||||
self.attacker, self.defender, "test", attack_value=20, defense_value=10
|
||||
tb_range.COMBAT_RULES.resolve_attack(
|
||||
self.attacker, self.defender, attack_type="test", attack_value=20, defense_value=10
|
||||
)
|
||||
self.assertTrue(self.defender.db.hp < 40)
|
||||
# Combat cleanup
|
||||
self.attacker.db.Combat_attribute = True
|
||||
tb_range.combat_cleanup(self.attacker)
|
||||
tb_range.COMBAT_RULES.combat_cleanup(self.attacker)
|
||||
self.assertFalse(self.attacker.db.combat_attribute)
|
||||
# Is in combat
|
||||
self.assertFalse(tb_range.is_in_combat(self.attacker))
|
||||
self.assertFalse(tb_range.COMBAT_RULES.is_in_combat(self.attacker))
|
||||
# Set up turn handler script for further tests
|
||||
self.attacker.location.scripts.add(tb_range.TBRangeTurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_TurnHandler
|
||||
self.assertTrue(self.attacker.db.combat_TurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_turnHandler
|
||||
self.assertTrue(self.attacker.db.combat_turnHandler)
|
||||
# Set the turn handler's interval very high to keep it from repeating during tests.
|
||||
self.turnhandler.interval = 10000
|
||||
# Force turn order
|
||||
self.turnhandler.db.fighters = [self.attacker, self.defender]
|
||||
self.turnhandler.db.turn = 0
|
||||
# Test is turn
|
||||
self.assertTrue(tb_range.is_turn(self.attacker))
|
||||
self.assertTrue(tb_range.COMBAT_RULES.is_turn(self.attacker))
|
||||
# Spend actions
|
||||
self.attacker.db.Combat_ActionsLeft = 1
|
||||
tb_range.spend_action(self.attacker, 1, action_name="Test")
|
||||
tb_range.COMBAT_RULES.spend_action(self.attacker, 1, action_name="Test")
|
||||
self.assertTrue(self.attacker.db.Combat_ActionsLeft == 0)
|
||||
self.assertTrue(self.attacker.db.Combat_LastAction == "Test")
|
||||
# Initialize for combat
|
||||
|
|
@ -359,7 +362,7 @@ class TestTurnBattleRangeFunc(BaseEvenniaTest):
|
|||
# Start turn
|
||||
self.defender.db.Combat_ActionsLeft = 0
|
||||
self.turnhandler.start_turn(self.defender)
|
||||
self.assertTrue(self.defender.db.Combat_ActionsLeft == 2)
|
||||
self.assertEqual(self.defender.db.Combat_ActionsLeft, 2)
|
||||
# Next turn
|
||||
self.turnhandler.db.fighters = [self.attacker, self.defender]
|
||||
self.turnhandler.db.turn = 0
|
||||
|
|
@ -378,13 +381,13 @@ class TestTurnBattleRangeFunc(BaseEvenniaTest):
|
|||
self.assertTrue(self.turnhandler.db.turn == 1)
|
||||
self.assertTrue(self.turnhandler.db.fighters == [self.joiner, self.attacker, self.defender])
|
||||
# Now, test for approach/withdraw functions
|
||||
self.assertTrue(tb_range.get_range(self.attacker, self.defender) == 1)
|
||||
self.assertTrue(tb_range.COMBAT_RULES.get_range(self.attacker, self.defender) == 1)
|
||||
# Approach
|
||||
tb_range.approach(self.attacker, self.defender)
|
||||
self.assertTrue(tb_range.get_range(self.attacker, self.defender) == 0)
|
||||
tb_range.COMBAT_RULES.approach(self.attacker, self.defender)
|
||||
self.assertTrue(tb_range.COMBAT_RULES.get_range(self.attacker, self.defender) == 0)
|
||||
# Withdraw
|
||||
tb_range.withdraw(self.attacker, self.defender)
|
||||
self.assertTrue(tb_range.get_range(self.attacker, self.defender) == 1)
|
||||
tb_range.COMBAT_RULES.withdraw(self.attacker, self.defender)
|
||||
self.assertTrue(tb_range.COMBAT_RULES.get_range(self.attacker, self.defender) == 1)
|
||||
|
||||
|
||||
class TestTurnBattleItemsFunc(BaseEvenniaTest):
|
||||
|
|
@ -407,54 +410,55 @@ class TestTurnBattleItemsFunc(BaseEvenniaTest):
|
|||
def tearDown(self):
|
||||
super().tearDown()
|
||||
self.turnhandler.stop()
|
||||
self.testroom.delete()
|
||||
self.attacker.delete()
|
||||
self.defender.delete()
|
||||
self.joiner.delete()
|
||||
self.user.delete()
|
||||
self.testroom.delete()
|
||||
|
||||
# Test functions in tb_items.
|
||||
def test_tbitemsfunc(self):
|
||||
# Initiative roll
|
||||
initiative = tb_items.roll_init(self.attacker)
|
||||
initiative = tb_items.COMBAT_RULES.roll_init(self.attacker)
|
||||
self.assertTrue(initiative >= 0 and initiative <= 1000)
|
||||
# Attack roll
|
||||
attack_roll = tb_items.get_attack(self.attacker, self.defender)
|
||||
attack_roll = tb_items.COMBAT_RULES.get_attack(self.attacker, self.defender)
|
||||
self.assertTrue(attack_roll >= 0 and attack_roll <= 100)
|
||||
# Defense roll
|
||||
defense_roll = tb_items.get_defense(self.attacker, self.defender)
|
||||
defense_roll = tb_items.COMBAT_RULES.get_defense(self.attacker, self.defender)
|
||||
self.assertTrue(defense_roll == 50)
|
||||
# Damage roll
|
||||
damage_roll = tb_items.get_damage(self.attacker, self.defender)
|
||||
damage_roll = tb_items.COMBAT_RULES.get_damage(self.attacker, self.defender)
|
||||
self.assertTrue(damage_roll >= 15 and damage_roll <= 25)
|
||||
# Apply damage
|
||||
self.defender.db.hp = 10
|
||||
tb_items.apply_damage(self.defender, 3)
|
||||
tb_items.COMBAT_RULES.apply_damage(self.defender, 3)
|
||||
self.assertTrue(self.defender.db.hp == 7)
|
||||
# Resolve attack
|
||||
self.defender.db.hp = 40
|
||||
tb_items.resolve_attack(self.attacker, self.defender, attack_value=20, defense_value=10)
|
||||
tb_items.COMBAT_RULES.resolve_attack(self.attacker, self.defender, attack_value=20,
|
||||
defense_value=10)
|
||||
self.assertTrue(self.defender.db.hp < 40)
|
||||
# Combat cleanup
|
||||
self.attacker.db.Combat_attribute = True
|
||||
tb_items.combat_cleanup(self.attacker)
|
||||
tb_items.COMBAT_RULES.combat_cleanup(self.attacker)
|
||||
self.assertFalse(self.attacker.db.combat_attribute)
|
||||
# Is in combat
|
||||
self.assertFalse(tb_items.is_in_combat(self.attacker))
|
||||
self.assertFalse(tb_items.COMBAT_RULES.is_in_combat(self.attacker))
|
||||
# Set up turn handler script for further tests
|
||||
self.attacker.location.scripts.add(tb_items.TBItemsTurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_TurnHandler
|
||||
self.assertTrue(self.attacker.db.combat_TurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_turnHandler
|
||||
self.assertTrue(self.attacker.db.combat_turnHandler)
|
||||
# Set the turn handler's interval very high to keep it from repeating during tests.
|
||||
self.turnhandler.interval = 10000
|
||||
# Force turn order
|
||||
self.turnhandler.db.fighters = [self.attacker, self.defender]
|
||||
self.turnhandler.db.turn = 0
|
||||
# Test is turn
|
||||
self.assertTrue(tb_items.is_turn(self.attacker))
|
||||
self.assertTrue(tb_items.COMBAT_RULES.is_turn(self.attacker))
|
||||
# Spend actions
|
||||
self.attacker.db.Combat_ActionsLeft = 1
|
||||
tb_items.spend_action(self.attacker, 1, action_name="Test")
|
||||
tb_items.COMBAT_RULES.spend_action(self.attacker, 1, action_name="Test")
|
||||
self.assertTrue(self.attacker.db.Combat_ActionsLeft == 0)
|
||||
self.assertTrue(self.attacker.db.Combat_LastAction == "Test")
|
||||
# Initialize for combat
|
||||
|
|
@ -485,29 +489,29 @@ class TestTurnBattleItemsFunc(BaseEvenniaTest):
|
|||
self.assertTrue(self.turnhandler.db.fighters == [self.joiner, self.attacker, self.defender])
|
||||
# Now time to test item stuff.
|
||||
# Spend item use
|
||||
tb_items.spend_item_use(self.test_healpotion, self.user)
|
||||
tb_items.COMBAT_RULES.spend_item_use(self.test_healpotion, self.user)
|
||||
self.assertTrue(self.test_healpotion.db.item_uses == 2)
|
||||
# Use item
|
||||
self.user.db.hp = 2
|
||||
tb_items.use_item(self.user, self.test_healpotion, self.user)
|
||||
tb_items.COMBAT_RULES.use_item(self.user, self.test_healpotion, self.user)
|
||||
self.assertTrue(self.user.db.hp > 2)
|
||||
# Add contition
|
||||
tb_items.add_condition(self.user, self.user, "Test", 5)
|
||||
tb_items.COMBAT_RULES.add_condition(self.user, self.user, "Test", 5)
|
||||
self.assertTrue(self.user.db.conditions == {"Test": [5, self.user]})
|
||||
# Condition tickdown
|
||||
tb_items.condition_tickdown(self.user, self.user)
|
||||
self.assertTrue(self.user.db.conditions == {"Test": [4, self.user]})
|
||||
tb_items.COMBAT_RULES.condition_tickdown(self.user, self.user)
|
||||
self.assertEqual(self.user.db.conditions, {"Test": [4, self.user]})
|
||||
# Test item functions now!
|
||||
# Item heal
|
||||
self.user.db.hp = 2
|
||||
tb_items.itemfunc_heal(self.test_healpotion, self.user, self.user)
|
||||
tb_items.COMBAT_RULES.itemfunc_heal(self.test_healpotion, self.user, self.user)
|
||||
# Item add condition
|
||||
self.user.db.conditions = {}
|
||||
tb_items.itemfunc_add_condition(self.test_healpotion, self.user, self.user)
|
||||
tb_items.COMBAT_RULES.itemfunc_add_condition(self.test_healpotion, self.user, self.user)
|
||||
self.assertTrue(self.user.db.conditions == {"Regeneration": [5, self.user]})
|
||||
# Item cure condition
|
||||
self.user.db.conditions = {"Poisoned": [5, self.user]}
|
||||
tb_items.itemfunc_cure_condition(self.test_healpotion, self.user, self.user)
|
||||
tb_items.COMBAT_RULES.itemfunc_cure_condition(self.test_healpotion, self.user, self.user)
|
||||
self.assertTrue(self.user.db.conditions == {})
|
||||
|
||||
|
||||
|
|
@ -526,53 +530,54 @@ class TestTurnBattleMagicFunc(BaseEvenniaTest):
|
|||
def tearDown(self):
|
||||
super().tearDown()
|
||||
self.turnhandler.stop()
|
||||
self.testroom.delete()
|
||||
self.attacker.delete()
|
||||
self.defender.delete()
|
||||
self.joiner.delete()
|
||||
self.testroom.delete()
|
||||
|
||||
# Test combat functions in tb_magic.
|
||||
def test_tbbasicfunc(self):
|
||||
# Initiative roll
|
||||
initiative = tb_magic.roll_init(self.attacker)
|
||||
initiative = tb_magic.COMBAT_RULES.roll_init(self.attacker)
|
||||
self.assertTrue(initiative >= 0 and initiative <= 1000)
|
||||
# Attack roll
|
||||
attack_roll = tb_magic.get_attack(self.attacker, self.defender)
|
||||
attack_roll = tb_magic.COMBAT_RULES.get_attack(self.attacker, self.defender)
|
||||
self.assertTrue(attack_roll >= 0 and attack_roll <= 100)
|
||||
# Defense roll
|
||||
defense_roll = tb_magic.get_defense(self.attacker, self.defender)
|
||||
defense_roll = tb_magic.COMBAT_RULES.get_defense(self.attacker, self.defender)
|
||||
self.assertTrue(defense_roll == 50)
|
||||
# Damage roll
|
||||
damage_roll = tb_magic.get_damage(self.attacker, self.defender)
|
||||
damage_roll = tb_magic.COMBAT_RULES.get_damage(self.attacker, self.defender)
|
||||
self.assertTrue(damage_roll >= 15 and damage_roll <= 25)
|
||||
# Apply damage
|
||||
self.defender.db.hp = 10
|
||||
tb_magic.apply_damage(self.defender, 3)
|
||||
tb_magic.COMBAT_RULES.apply_damage(self.defender, 3)
|
||||
self.assertTrue(self.defender.db.hp == 7)
|
||||
# Resolve attack
|
||||
self.defender.db.hp = 40
|
||||
tb_magic.resolve_attack(self.attacker, self.defender, attack_value=20, defense_value=10)
|
||||
tb_magic.COMBAT_RULES.resolve_attack(self.attacker, self.defender, attack_value=20,
|
||||
defense_value=10)
|
||||
self.assertTrue(self.defender.db.hp < 40)
|
||||
# Combat cleanup
|
||||
self.attacker.db.Combat_attribute = True
|
||||
tb_magic.combat_cleanup(self.attacker)
|
||||
tb_magic.COMBAT_RULES.combat_cleanup(self.attacker)
|
||||
self.assertFalse(self.attacker.db.combat_attribute)
|
||||
# Is in combat
|
||||
self.assertFalse(tb_magic.is_in_combat(self.attacker))
|
||||
self.assertFalse(tb_magic.COMBAT_RULES.is_in_combat(self.attacker))
|
||||
# Set up turn handler script for further tests
|
||||
self.attacker.location.scripts.add(tb_magic.TBMagicTurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_TurnHandler
|
||||
self.assertTrue(self.attacker.db.combat_TurnHandler)
|
||||
self.turnhandler = self.attacker.db.combat_turnHandler
|
||||
self.assertTrue(self.attacker.db.combat_turnHandler)
|
||||
# Set the turn handler's interval very high to keep it from repeating during tests.
|
||||
self.turnhandler.interval = 10000
|
||||
# Force turn order
|
||||
self.turnhandler.db.fighters = [self.attacker, self.defender]
|
||||
self.turnhandler.db.turn = 0
|
||||
# Test is turn
|
||||
self.assertTrue(tb_magic.is_turn(self.attacker))
|
||||
self.assertTrue(tb_magic.COMBAT_RULES.is_turn(self.attacker))
|
||||
# Spend actions
|
||||
self.attacker.db.Combat_ActionsLeft = 1
|
||||
tb_magic.spend_action(self.attacker, 1, action_name="Test")
|
||||
tb_magic.COMBAT_RULES.spend_action(self.attacker, 1, action_name="Test")
|
||||
self.assertTrue(self.attacker.db.Combat_ActionsLeft == 0)
|
||||
self.assertTrue(self.attacker.db.Combat_LastAction == "Test")
|
||||
# Initialize for combat
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue