Advertisement
Kovitikus

Roundtime Check

Oct 10th, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.88 KB | None | 0 0
  1. """
  2. In world.general_mechanics.py
  3.  
  4. TAKE NOTE OF THE CHECK_ROUNDTIME FUNCTION LINE 13
  5.  
  6. """
  7.  
  8. import time, datetime
  9. from evennia import utils, search_script
  10. from evennia.utils import gametime
  11. from typeclasses.rooms import Room
  12.  
  13. def check_roundtime(owner):
  14.     if owner.db.ko == True:
  15.         owner.msg("You can't do that while unconscious!")
  16.         return False
  17.  
  18.     # Create cooldown attribute if non-existent.
  19.     if not owner.attributes.has('roundtime'):
  20.         owner.db.roundtime = 0
  21.  
  22.     # Calculate current time, total cooldown, and remaining time.
  23.     now = time.time()
  24.     lastcast = owner.attributes.get('roundtime')
  25.     cooldown = lastcast + 2
  26.     time_remaining = cooldown - now
  27.  
  28.     # Inform the owner that they are in cooldown and exit the function.
  29.     if time_remaining > 0 or owner.db.busy == True:
  30.         if time_remaining >= 2:
  31.             message = f"You need to wait {int(time_remaining)} more seconds."
  32.         elif time_remaining >= 1 and time_remaining < 2:
  33.             message = f"You need to wait {int(time_remaining)} more second."
  34.         elif time_remaining < 1:
  35.             message = f"You are in the middle of something."
  36.         owner.msg(message)
  37.         return False
  38.     return True
  39.  
  40. def set_roundtime(owner):
  41.     now = time.time()
  42.     utils.delay(2, unbusy, owner, persistent=True)
  43.     owner.db.busy = True
  44.     owner.db.roundtime = now
  45.  
  46. def unbusy(owner):
  47.     owner.msg('|yYou are no longer busy.|n')
  48.     owner.db.busy = False
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. """
  56. In world.combat_handler.py
  57.  
  58. TAKE NOTE OF THE ATTACK AND HEAL FUNCTIONS LINE 179
  59.  
  60. """
  61.  
  62. from evennia import utils
  63. from world import skillsets
  64. from world import build_skill_str
  65. from world import general_mechanics as gen_mec
  66. import random
  67.  
  68. class CombatHandler:
  69.     def __init__(self, owner):
  70.         self.owner = owner
  71.  
  72.     def approach(self, attacker, target):
  73.         a_app = attacker.attributes.get('approached')
  74.         t_app = target.attributes.get('approached')
  75.         a_name = attacker.key
  76.         t_name = target.key
  77.  
  78.         if target in a_app:
  79.             attacker.msg(f"You are already approached to {t_name}!")
  80.             return
  81.         if len(a_app) >= 1:
  82.             attacker.msg(f"You are already approached to {a_app}!")
  83.             target.msg(f"{a_name} attempts to approach you, but fails.")
  84.             return
  85.         if len(t_app) >= 3:
  86.             attacker.msg(f"{t_app} are already approached to that target!")
  87.             return
  88.         a_app.append(target)
  89.         t_app.append(attacker)
  90.         attacker.msg(f"You approach {t_name}.")
  91.         target.msg(f"{a_name} approaches you.")
  92.         return
  93.  
  94.     def retreat(self, attacker):
  95.         a_app = attacker.attributes.get('approached')
  96.         a_name = attacker.key
  97.  
  98.         if len(a_app) == 0:
  99.             attacker.msg(f"You are not approached to anything.")
  100.             return
  101.         for t in a_app:
  102.             t.db.approached.remove(attacker)
  103.             t.msg(f"{a_name} retreats from you.")
  104.         attacker.msg(f"You retreat.")
  105.        
  106.         a_app.clear()
  107.    
  108.     def success_calc(self, target, skillset, skill, aim):
  109.         """
  110.        Only whole numbers (rounded down) are used to determine the offensive RB.
  111.  
  112.        TODO: Add a round down for the final RB. Integers only!
  113.        """
  114.         owner = self.owner
  115.         a_skillset = owner.attributes.get(skillset)
  116.         a_skill = a_skillset.get(skill)
  117.         a_rb = a_skill.get('rb')
  118.  
  119.         defen_high = target.db.def_rb['high']
  120.         defen_mid = target.db.def_rb['mid']
  121.         defen_low = target.db.def_rb['low']
  122.  
  123.  
  124.         if aim == 'high':
  125.             t_rb = defen_high
  126.         elif aim == 'mid':
  127.             t_rb = defen_mid
  128.         elif aim == 'low':
  129.             t_rb == defen_low
  130.  
  131.         if a_rb > t_rb:
  132.             bonus = a_rb - t_rb
  133.             success = 50 - bonus
  134.         elif t_rb > a_rb:
  135.             loss = t_rb - a_rb
  136.             success = 50 + loss
  137.         else:
  138.             success = 50
  139.  
  140.         return success
  141.  
  142.     def body_part_choice(self, aim):
  143.         high_body = ['head', 'face', 'neck', 'left shoulder', 'right shoulder']
  144.         mid_body = ['chest', 'back', 'left arm', 'left hand', 'right arm', 'right hand', 'waist']
  145.         low_body = ['left thigh', 'left leg', 'left foot', 'right thigh', 'right leg', 'right foot']
  146.        
  147.         if aim == 'high':
  148.             body_part = random.choice(high_body)
  149.         elif aim == 'mid':
  150.             body_part = random.choice(mid_body)
  151.         elif aim == 'low':
  152.             body_part = random.choice(low_body)
  153.  
  154.         return body_part
  155.  
  156.     def damage_tier(self, success, roll):
  157.         if roll > success:
  158.             difference = roll - success
  159.             if 1 <= difference <= 10:
  160.                 damage_tier = 0
  161.                 damage = random.randrange(1, 4)
  162.             elif 11 <= difference <= 30:
  163.                 damage_tier = 1
  164.                 damage = random.randrange(5, 8)
  165.             elif 31 <= difference <= 50:
  166.                 damage_tier = 2
  167.                 damage = random.randrange(9, 12)
  168.             elif 51 <= difference <= 70:
  169.                 damage_tier = 3
  170.                 damage = random.randrange(13, 16)
  171.             elif 71 <= difference <= 100:
  172.                 damage_tier = 4
  173.                 damage = random.randrange(17, 20)
  174.         elif roll <= success:
  175.             damage_tier = 0
  176.             damage = 0
  177.         return damage_tier, damage
  178.  
  179.     def attack(self, target, skillset, skill, weapon, damage_type, aim):
  180.         attacker = self.owner
  181.  
  182.         if not gen_mec.check_roundtime(attacker):
  183.             return
  184.  
  185.         # This is where the fun begins.
  186.  
  187.         roll = random.randint(1, 100)
  188.         success = self.success_calc(target, skillset, skill, aim)
  189.  
  190.         # Make sure that the success is never below 5 or above 95, and always 5 if the target is unconscious.
  191.         if success < 5 or target.db.ko == True:
  192.             success = 5
  193.         elif success > 95:
  194.             success = 95
  195.        
  196.         damage_tier, damage = self.damage_tier(success, roll)
  197.  
  198.         # Randomly choose the body part hit, based on where the attack is aimed.
  199.         body_part = self.body_part_choice(aim)
  200.  
  201.         if roll > success:
  202.             hit = True
  203.             attacker_desc, target_desc, others_desc = build_skill_str.create_attack_desc(attacker, target, skillset, skill, weapon, damage_type, damage_tier, body_part, hit)
  204.  
  205.             self.owner.msg(f"|430[Success: {success} Roll: {roll}] {attacker_desc}|n")
  206.             target.msg(f"|r[Success: {success} Roll: {roll}] {target_desc}|n")
  207.             self.owner.location.msg_contents(f"{others_desc}", exclude=(self.owner, target))
  208.             self.take_damage(target, damage)
  209.  
  210.         else:
  211.             hit = False
  212.             attacker_desc, target_desc, others_desc = build_skill_str.create_attack_desc(attacker, target, skillset, skill, weapon, damage_type, damage_tier, body_part, hit)
  213.  
  214.             self.owner.msg(f"|430[Success: {success} Roll: {roll}] {attacker_desc}|n")
  215.             target.msg(f"|r[Success: {success} Roll: {roll}] {target_desc}|n")
  216.             self.owner.location.msg_contents(f"{others_desc}", exclude=(self.owner, target))
  217.        
  218.         gen_mec.set_roundtime(attacker)
  219.  
  220.     def take_damage(self, target, damage):
  221.         t_name = target.key
  222.         location = target.location
  223.         targ_app = target.attributes.get('approached')
  224.         print('Target\'s approached list is: ', targ_app)
  225.        
  226.         hp = target.attributes.get('hp')
  227.         current_hp = hp['current_hp']
  228.         current_hp -= damage
  229.         target.db.hp['current_hp'] = current_hp
  230.  
  231.         location.msg_contents(f'{t_name} has {current_hp} health remaining!')
  232.         if current_hp >= 1:
  233.             target.db.ko = False
  234.         elif current_hp <= 0 and target.db.ko != True:
  235.             target.db.ko = True
  236.             location.msg_contents(f'{t_name} falls unconscious!')
  237.         if current_hp <= -100:
  238.             # Check for
  239.             for a in targ_app:
  240.                 print('This is a in target\'s approached list: ', a)
  241.                 ap_list = a.attributes.get('approached')
  242.                 print('This is the approached list of the attacker: ', ap_list)
  243.                 if ap_list:
  244.                     ap_list.remove(target)
  245.             if targ_app:
  246.                 targ_app.remove(self.owner)
  247.             if not target.has_account:
  248.                 okay = target.delete()
  249.                 if not okay:
  250.                     location.msg_contents(f'\nERROR: {t_name} not deleted, probably because delete() returned False.')
  251.                 else:
  252.                     location.msg_contents(f'{t_name} breathes a final breath and expires.')
  253.             else:
  254.                 target.db.hp['current_hp'] = target.db.hp['max_hp']
  255.                 location.msg_contents(f"{t_name} dies and is resurrected to max HP.", exclude=target)
  256.                 target.msg("You die and are resurrected to full HP.")
  257.                 target.db.ko = False
  258.  
  259.     def heal(self, target):
  260.         owner = self.owner
  261.  
  262.         if not gen_mec.check_roundtime(owner):
  263.             return
  264.  
  265.         heal_rank = owner.db.holy['heal']['rank']
  266.         max_hp = target.db.hp['max_hp']
  267.         current_hp = target.db.hp['current_hp']
  268.  
  269.         if current_hp == max_hp:
  270.             if target == owner:
  271.                 owner.msg(f"|cYou are already at full health!|n")
  272.             else:
  273.                 owner.msg(f"|c{target.name} is already at full health!|n")
  274.             return
  275.  
  276.         heal_amount = heal_rank * 2
  277.         current_hp += heal_amount
  278.         if current_hp > max_hp:
  279.             current_hp = max_hp
  280.         target.db.hp['current_hp'] = current_hp
  281.         if target == owner:
  282.             owner.msg(f"|cYou heal yourself for {heal_amount} health.|n")
  283.         else:
  284.             owner.msg(f"|cYou heal {target.name} for {heal_amount} health.|n")
  285.         gen_mec.set_roundtime(owner)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement