-
Notifications
You must be signed in to change notification settings - Fork 0
/
mechanics.py
174 lines (138 loc) · 5.75 KB
/
mechanics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/python
# -*- coding: utf-8 -*-
import dice
import math
import re
import messages
class Movement:
def __init__(self, x=0, y=0):
pass
class DnDRuleset:
@staticmethod
def roll_hit(source, target, attack, always_hit=False):
""" Genera hit roller
:param source source of attack (creature object)
:param target target of attack (creature object)
:param attack ability or weapon (weapon or ability object) """
messages.IO.reset()
bonus = attack.to_hit
attack_name = attack.name
""" Check advantage conditions """
if target.gives_advantage_to_attacker:
source.set_advantage('hit', 1)
""" Rollening's """
advantage = source.advantage['hit']
hitroll = dice.roll(1, 20, 0, advantage)
""" Override hitroll if always_hit is true"""
if always_hit:
hitroll = target.ac + target.ac_bonus + 10
advantage = 0
""" Apply critical multiplier """
multiplier = int(max(hitroll / 10, 1))
""" Auto-crit if target is paralyzed """
if target.is_paralyzed:
hitroll = 20
multiplier = 2
critical_failure_effect = False
""" Check if attack hits """
if hitroll == 1:
source.misses += 1
hit = False
# TODO: CRITICAL FAILURES
roll = dice.roll(times=1, sides=3, bonus=0)
if roll == 1:
msg = "falls prone due to critical FAILURE attacking"
critical_failure_effect = True
else:
msg = "FAILS critically attacking"
elif hitroll == 20:
hit = True
msg = "lands a CRITICAL hit on"
source.hits += 1
elif hitroll + bonus > target.ac + target.ac_bonus:
hit = True
msg = "attacks"
source.hits += 1
else:
hit = False
msg = "misses"
source.misses += 1
if advantage == 1:
adv = ' (adv.)'
elif advantage == -1:
adv = ' (disadv.)'
else:
adv = ''
messages.IO.log += "{source} {hit} {target}"\
" with {attackname}{adv}.".format(source=source.name,
target=target.name,
attackname=attack_name.title(),
hit=msg,
adv=adv)
if not hit:
messages.IO.printlog()
messages.IO.reset()
""" Set critical failure effects """
if critical_failure_effect:
source.set_prone(True)
source.take_damage(source, {'bludgeoning': dice.roll(1, 6, 0)}, 1)
return hit, multiplier, hitroll + bonus
@staticmethod
def roll_save(target, ability, dc):
""" Roll a save tied on ability against DC
:type target CreatureBaseClass
:type ability str
:type dc int
A boolean is returned and the value is also written
to the creature for more complex situations """
bonus = target.saves[ability]
advantage = target.advantage[ability]
result = dice.roll(1, 20, bonus, advantage) >= int(dc)
""" Auto-fails """
if target.is_paralyzed and ability in ("str", "dex"):
result = False
target.save_success = result
return result
@staticmethod
def iterate_damage_(source, target, weapon, crit_multiplier=1,
success=None, save=None, dc=None):
""" Iterate all damage types in weapon and roll damage"""
total = []
for i in range(len(weapon.damage)):
dmg = weapon.damage[i]
dmg_type = weapon.damage_type[i]
total.append(DnDRuleset.roll_damage(source, target, weapon, dmg,
dmg_type, crit_multiplier, success, save, dc))
return sum(total)
@staticmethod
def roll_damage_(source, target, weapon, dmg, dmg_type, crit_multiplier=1,
success=None, save=None, dc=None):
""" Parse damage as times, sides and bonus """
times, sides, bonus = dmg
damage = dice.roll(times*crit_multiplier, sides, bonus)
""" If attack allows save, multiply damage with success multiplier
in case target did not fail its save """
if success is not None:
if target.save_success:
damage = int(damage * success)
target.reset_save()
""" Subtract damage from target's HP pool """
return target.take_damage(source, damage, dmg_type, crit_multiplier)
@staticmethod
def roll_damage(source, target, weapon, crit_multiplier=1,
success=None, save=None, dc=None):
damage_types = {}
for i in range(len(weapon.damage)):
t, s, b = weapon.damage[i]
damage = dice.roll(t * crit_multiplier, s, b)
dmg_type = weapon.damage_type[i]
""" If attack allows save, multiply damage with success multiplier
in case target did not fail its save """
if success is not None:
if target.save_success:
damage = int(damage * success)
target.reset_save()
damage_types[dmg_type] = damage
""" Subtract damage from target's HP pool """
#return target.take_damage(source, damage, dmg_type, crit_multiplier)
return target.take_damage(source, damage_types, crit_multiplier)