Some minor adjustments for pep8.

This commit is contained in:
Griatch 2017-04-08 19:28:29 +02:00
parent 005923ee72
commit 59f491eab4

View file

@ -51,6 +51,8 @@ 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.
@ -75,7 +77,8 @@ def roll_init(character):
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):
""" """
@ -101,6 +104,7 @@ def get_attack(attacker, defender):
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
@ -124,6 +128,7 @@ def get_defense(attacker, defender):
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
@ -147,6 +152,7 @@ def get_damage(attacker, defender):
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
@ -161,6 +167,7 @@ def apply_damage(defender, damage):
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.
@ -187,11 +194,12 @@ def resolve_attack(attacker, defender, attack_value=None, defense_value=None):
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.
@ -207,6 +215,7 @@ def combat_cleanup(character):
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.
@ -221,6 +230,7 @@ def is_in_combat(character):
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.
@ -237,6 +247,7 @@ def is_turn(character):
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.
@ -266,6 +277,7 @@ 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
@ -286,6 +298,7 @@ class BattleCharacter(DefaultCharacter):
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
@ -311,11 +324,14 @@ class BattleCharacter(DefaultCharacter):
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.
@ -354,9 +370,11 @@ class CmdFight(Command):
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.
@ -405,6 +423,7 @@ class CmdAttack(Command):
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.
@ -435,6 +454,7 @@ class CmdPass(Command):
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.
@ -470,6 +490,7 @@ class CmdDisengage(Command):
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.
@ -497,6 +518,7 @@ class CmdRest(Command):
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
@ -514,13 +536,14 @@ class CmdCombatHelp(CmdHelp):
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):
""" """
This command set includes all the commmands used in the battle system. This command set includes all the commmands used in the battle system.
@ -538,12 +561,14 @@ class BattleCmdSet(default_cmds.CharacterCmdSet):
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.
@ -585,7 +610,7 @@ class TurnHandler(DefaultScript):
# 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
@ -594,7 +619,7 @@ class TurnHandler(DefaultScript):
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):
@ -614,7 +639,6 @@ class TurnHandler(DefaultScript):
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.
@ -656,7 +680,7 @@ class TurnHandler(DefaultScript):
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
@ -685,7 +709,6 @@ class TurnHandler(DefaultScript):
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.