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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
import re
import json
from dataclasses import dataclass, asdict
from items import Armor, Weapon
from game import LevelingPlan, Features, Modifier, Conditions, RollInfo
class FeaturesPlan(LevelingPlan, dict):
@classmethod
def load(cls, name):
with open(f"leveling/{name}.json", "r", encoding="utf8") as js_file:
return cls({int(k):v for k,v in json.load(js_file).items()})
def build(self, level: int):
res = dict()
# should be 0, but just in case of negative levels
for lvl in range(-5, level+1):
res.update(self.get(lvl, dict()))
return res
def merge(self, other):
for lvl, other_val in other.items():
if lvl in self:
val = self[lvl]
for key in (val.keys() & other_val.keys()):
assert other_val[key] == val[key]
val.update(other_val)
else:
self[lvl] = other_val
class Abilities(dict):
def __init__(self, data=None, **kwargs):
super().__init__({field: 10 for field in Features.abilities})
if data:
self.update(data)
if kwargs:
self.update(kwargs)
@staticmethod
def ability_boost_factor(value):
if value < 18:
return 2
return 1
@staticmethod
def ability_modifier(value):
return (value - 10) // 2
def apply_change(self, ability_name, step_direction=1):
ability = self[ability_name]
ability += step_direction*Abilities.ability_boost_factor(ability)
self[ability_name] = ability
def apply_changes(self, changes, step_direction=1):
for change in changes:
self.apply_change(change.lower(), step_direction)
def modifier(self, ability_name):
return Abilities.ability_modifier(self[ability_name])
class AbilitiesPlan(LevelingPlan):
def __init__(self, data) -> None:
super().__init__()
boosts = data.pop("mapLevelledBoosts")
self.levelBoosts = { int(level): values for level, values in boosts.items() }
self.initialFlaws = dict()
for flaw in ["ancestryFlaws"]:
self.initialFlaws[flaw] = data.pop(flaw)
self.initialBoosts = data
def build(self, level: int):
res = Abilities()
for flaw in self.initialFlaws.values():
res.apply_changes(flaw, -1)
if level <= 0:
return res
for boost in self.initialBoosts.values():
res.apply_changes(boost)
for lvl, boost in self.levelBoosts.items():
if lvl <= level:
res.apply_changes(boost)
return res
@dataclass
class Character:
data: dict
hp_bonus: int
hp_per_level: int
speed: int
level: int
abilities: Abilities
features: dict
armor: Armor
conditions: Conditions
def __init__(self, data):
self.data = data
self.hp_bonus = 10
self.hp_per_level = 8
self.speed = 25
self.level = 1
self.abilities = Abilities()
self.features = dict()
self.armor = Armor("Unarmored", "light", 0)
self.weapons = [Weapon("Fist", "unarmed", "brawling", 4)]
self.conditions = Conditions()
def max_hp(self):
per_level = self.hp_per_level + self.abilities.modifier("con")
return self.hp_bonus + per_level * self.level
def skill_ability(self, skill):
if skill == "classDC":
return self.data["keyability"]
return Features.skill_ability.get(skill)
def ability_modifier(self, ability):
return self.conditions.modifier([ability]) + self.abilities.modifier(ability)
def proficiency_bonus(self, skill):
bonus = self.features.get(skill, 0)
if bonus == 0:
return 0
return bonus + self.level
def proficiency(self, skill):
ability_name = self.skill_ability(skill)
res = Modifier()
if ability_name:
res += self.ability_modifier(ability_name)
res += self.proficiency_bonus(skill) + self.conditions.modifier([skill])
return res.total()
def ac(self):
dex_mod = self.conditions.modifier(["dex"], exclude=["ac"])
dex_mod += self.abilities.modifier("dex")
dex_bonus = min(self.armor.dex_cap, dex_mod.total())
prof = self.proficiency(self.armor.category)
armor = Modifier(item=self.armor.ac+self.armor.potency)
armor += self.conditions.modifier(["ac"])
return (10 + dex_bonus + prof + armor).total()
def classDC(self):
return 10 + self.proficiency("classDC")
def update_fundamental_runes(self):
self.armor.potency = self.features.get("armor potency", 0)
self.armor.resilient = self.features.get("resilient", 0)
for weapon in self.weapons:
weapon.potency = self.features.get("weapon potency", 0)
weapon.striking = self.features.get("striking", 0)
def weapon_proficiency_bonus(self, weapon):
return max(self.features.get(weapon.category, 0),
self.features.get(weapon.name, 0))
def weapon_specialization_bonus(self, weapon):
factor = self.features.get("weapon specialization", 0)
return self.weapon_proficiency_bonus(weapon) * factor // 2
def attack_bonus(self, weapon: Weapon, strike_type="melee"):
dex = self.ability_modifier("dex").total()
mod = self.ability_modifier("str").total()
if strike_type == "melee":
if weapon.finesse:
mod = max(mod, dex)
elif strike_type == "ranged":
mod = dex
prof = self.weapon_proficiency_bonus(weapon)
return mod + prof + self.level + weapon.potency
def attack_damage_bonus(self, weapon: Weapon, strike_type="melee"):
str_mod = self.ability_modifier("str")
dmg_mod = self.conditions.modifier("damage")
dmg_mod += self.weapon_specialization_bonus(weapon)
if strike_type == "melee":
if "thief" in self.features and weapon.finesse:
return (self.ability_modifier("dex")+ dmg_mod).total()
return (str_mod + dmg_mod).total()
elif strike_type == "ranged":
if weapon.thrown > 0:
return (str_mod + dmg_mod).total()
if weapon.propulsive:
return (str_mod.total()//2 + dmg_mod).total()
return dmg_mod.total()
def sneak_attack(self):
sneak = "sneak attack"
if sneak not in self.features:
return 0
return RollInfo.parse(self.features[sneak]).distribution()
def toJSON(self):
methods = ["classDC", "max_hp", "ac"]
obj = asdict(self)
for method in methods:
obj[method] = getattr(self, method)()
return json.dumps(obj, indent=2)
class CharacterPlan(LevelingPlan):
def __init__(self, data) -> None:
self.data = data
(self._class, self.subclass) = self.parse_subclass(data["class"])
data["class"] = self._class
data["subclass"] = self.subclass
abilities = data["abilities"]["breakdown"]
self.attributes = data["attributes"]
self.cleanup_build_data(data)
self.abilities = AbilitiesPlan(abilities)
self.features = FeaturesPlan()
self.features.merge(FeaturesPlan.load(self._class))
self.features.merge(FeaturesPlan.load("fundamental_runes"))
# TODO add skill increases to calculate proficiencies
@staticmethod
def cleanup_build_data(data):
duplicates = ["acTotal", "level", "xp", "abilities", "attributes",
"proficiencies"]
spells = ["rituals", "spellCasters", "focus", "focusPoints"]
equipment = ["money", "armor", "weapons", "equipment", "equipmentContainers"]
blacklist = duplicates
blacklist.extend(spells)
blacklist.extend(equipment)
blacklist.append("feats")
for key in blacklist:
del data[key]
@staticmethod
def parse_subclass(s: str):
match = re.fullmatch(r'\s*([^(]+)\s*\(\s*([^)]+)\s*\)\s*', s)
if match:
return match.group(1).strip().lower(), match.group(2).strip().lower()
return s, None
def build(self, level: int):
res = Character(self.data)
res.level = level
res.speed = self.attributes["speed"] + self.attributes["speedBonus"]
res.hp_bonus = self.attributes["bonushp"] + self.attributes["ancestryhp"]
res.hp_per_level = \
self.attributes["classhp"] + self.attributes["bonushpPerLevel"]
res.abilities = self.abilities.build(level)
res.features = self.features.build(level)
return res
|