Kovitikus

combat handler

Aug 18th, 2019
137
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from evennia import utils
  2. from world import skillsets
  3. from world import build_skill_str
  4. import time
  5. import random
  6.  
  7. class CombatHandler:
  8.     def __init__(self, owner):
  9.         self.owner = owner
  10.  
  11.     def approach(self, attacker, target):
  12.         a_app = attacker.attributes.get('approached')
  13.         t_app = target.attributes.get('approached')
  14.         a_name = attacker.key
  15.         t_name = target.key
  16.  
  17.         if target in a_app:
  18.             attacker.msg(f"You are already approached to {t_name}!")
  19.             return
  20.         if len(a_app) >= 1:
  21.             attacker.msg(f"You are already approached to {a_app}!")
  22.             target.msg(f"{a_name} attempts to approach you, but fails.")
  23.             return
  24.         if len(t_app) >= 3:
  25.             attacker.msg(f"{t_app} are already approached to that target!")
  26.             return
  27.         a_app.append(target)
  28.         t_app.append(attacker)
  29.         attacker.msg(f"You approach {t_name}.")
  30.         target.msg(f"{a_name} approaches you.")
  31.         return
  32.  
  33.     def retreat(self, attacker):
  34.         a_app = attacker.attributes.get('approached')
  35.         a_name = attacker.key
  36.  
  37.         if len(a_app) == 0:
  38.             attacker.msg(f"You are not approached to anything.")
  39.             return
  40.         for t in a_app:
  41.             t.db.approached.remove(attacker)
  42.             t.msg(f"{a_name} retreats from you.")
  43.         attacker.msg(f"You retreat.")
  44.        
  45.         a_app.clear()
  46.    
  47.     def success_calc(self, target, skillset, skill):
  48.         """
  49.        Only whole numbers rounded down are used to determine the offensive RB.
  50.        TO DO: ADD A ROUND DOWN!!
  51.        """
  52.         a_skillset = self.owner.attributes.get(skillset)
  53.         a_skill = a_skillset.get(skill)
  54.         a_rb = a_skill.get('rb')
  55.  
  56.         t_rb = 0
  57.  
  58.         if a_rb > t_rb:
  59.             bonus = a_rb - t_rb
  60.             success = 50 - bonus
  61.         elif t_rb > a_rb:
  62.             loss = t_rb - a_rb
  63.             success = 50 + loss
  64.         else:
  65.             success = 50
  66.  
  67.         return success
  68.  
  69.     def attack(self, target, damage_type, skillset, skill):
  70.         attacker = self.owner
  71.  
  72.         if self.owner.db.ko == True:
  73.             self.owner.msg("You can't do that while unconscious!")
  74.             return
  75.  
  76.         damage = 20
  77.  
  78.         # Create cooldown attribute if non-existent.
  79.         if not self.owner.attributes.has('attack_cd'):
  80.             self.owner.db.attack_cd = 0
  81.  
  82.         # Calculate current time, total cooldown, and remaining time.
  83.         now = time.time()
  84.         lastcast = self.owner.attributes.get('attack_cd')
  85.         cooldown = lastcast + 3
  86.         time_remaining = cooldown - now
  87.  
  88.         # Inform the attacker that they are in cooldown and exit the function.
  89.         if time_remaining > 0 or self.owner.db.busy == True:
  90.             if time_remaining >= 2:
  91.                 message = f"You need to wait {int(time_remaining)} more seconds."
  92.             elif time_remaining >= 1 and time_remaining < 2:
  93.                 message = f"You need to wait {int(time_remaining)} more second."
  94.             elif time_remaining < 1:
  95.                 message = f"You are in the middle of something."
  96.             self.owner.msg(message)
  97.             return
  98.  
  99.         roll = random.randint(1, 100)
  100.         success = self.success_calc(target, skillset, skill)
  101.  
  102.         # Make sure that the success is never below 5 or above 95 and always 5 if the target is unconscious.
  103.         if success < 5 or target.db.ko == True:
  104.             success = 5
  105.         elif success > 95:
  106.             success = 95
  107.        
  108.         #temp values
  109.         damage_tier = 0
  110.         body_part = 'head'
  111.  
  112.         # a_desc, t_desc = build_skill_str.create_attack_desc(self.owner, target, damage_type, damage_tier, body_part)
  113.         # outcome = a_desc
  114.         # weapon = 'quarterstave'
  115.  
  116.         if roll > success:
  117.             hit = True
  118.             attacker_desc, target_desc, others_desc = build_skill_str.create_attack_desc(attacker, target, skillset, skill, damage_type, damage_tier, body_part, hit)
  119.  
  120.             self.owner.msg(f"|430[Success: {success} Roll: {roll}] {attacker_desc}|n")
  121.             target.msg(f"|r[Success: {success} Roll: {roll}] {target_desc}|n")
  122.             self.owner.location.msg_contents(f"{others_desc}", exclude=(self.owner, target))
  123.             self.take_damage(target, damage)
  124.  
  125.         else:
  126.             hit = False
  127.             attacker_desc, target_desc, others_desc = build_skill_str.create_attack_desc(attacker, target, skillset, skill, damage_type, damage_tier, body_part, hit)
  128.  
  129.             self.owner.msg(f"|430[Success: {success} Roll: {roll}] {attacker_desc}|n")
  130.             target.msg(f"|r[Success: {success} Roll: {roll}] {target_desc}|n")
  131.             self.owner.location.msg_contents(f"{others_desc}", exclude=(self.owner, target))
  132.  
  133.         utils.delay(3, self.unbusy)
  134.         self.owner.db.busy = True
  135.         self.owner.db.attack_cd = now
  136.  
  137.     def take_damage(self, target, damage):
  138.         t_name = target.key
  139.         location = target.location
  140.         targ_app = target.attributes.get('approached')
  141.         print('Target\'s approached list is: ', targ_app)
  142.        
  143.         hp = target.attributes.get('hp')
  144.         current_hp = hp['current_hp']
  145.         current_hp -= damage
  146.         target.db.hp['current_hp'] = current_hp
  147.  
  148.         location.msg_contents(f'{t_name} has {current_hp} health remaining!')
  149.         if current_hp >= 1:
  150.             target.db.ko = False
  151.         elif current_hp <= 0 and target.db.ko != True:
  152.             target.db.ko = True
  153.             location.msg_contents(f'{t_name} falls unconscious!')
  154.         if current_hp <= -100:
  155.             # Check for
  156.             for a in targ_app:
  157.                 print('This is a in target\'s approached list: ', a)
  158.                 ap_list = a.attributes.get('approached')
  159.                 print('This is the approached list of the attacker: ', ap_list)
  160.                 if ap_list:
  161.                     ap_list.remove(target)
  162.             if targ_app:
  163.                 targ_app.remove(self.owner)
  164.             if not target.has_account:
  165.                 okay = target.delete()
  166.                 if not okay:
  167.                     location.msg_contents(f'\nERROR: {t_name} not deleted, probably because delete() returned False.')
  168.                 else:
  169.                     location.msg_contents(f'{t_name} breathes a final breath and expires.')
  170.             else:
  171.                 target.db.hp['current_hp'] = target.db.hp['max_hp']
  172.                 location.msg_contents(f"{t_name} dies and is resurrected to max HP.", exclude=target)
  173.                 target.msg("You die and are resurrected to full HP.")
  174.                 target.db.ko = False
  175.    
  176.     def unbusy(self):
  177.             self.owner.msg('|yYou are no longer busy.|n')
  178.             self.owner.db.busy = False
RAW Paste Data