dnd-item-generator/dnd_item/weapons.py

294 lines
11 KiB
Python
Raw Normal View History

2023-12-29 19:22:51 -08:00
import base64
import hashlib
2023-12-29 19:24:26 -08:00
import random
2023-12-27 22:25:12 -08:00
from functools import cached_property
from random_sets.sets import WeightedSet, equal_weights
2023-12-29 19:24:26 -08:00
from dnd_item import types
2023-12-27 22:25:12 -08:00
def random_from_csv(csv: str) -> str:
2023-12-31 11:55:54 -08:00
"""
Split a comma-separated value string into a list and return a random value.
"""
2023-12-29 19:24:26 -08:00
return random.choice(csv.split(",")).strip()
2023-12-27 22:25:12 -08:00
class Weapon(types.Item):
2023-12-31 11:55:54 -08:00
"""
An Item subclass representing weapons, both magical and mundane.
Much of this subclass is devoted to generating descriptive and entertaining
weapon names. It also implements a number of handy properties for presentation.
"""
2023-12-27 22:25:12 -08:00
def _descriptors(self) -> tuple:
"""
2023-12-31 11:55:54 -08:00
Collect the 'nouns' and 'adjectives' properties from the Item's
'properties' attribute. This is used by _random_descriptors to choose a
set of random nouns and adjectives to apply to a weapon name.
2023-12-27 22:25:12 -08:00
"""
nouns = dict()
adjectives = dict()
2023-12-29 19:24:26 -08:00
if not hasattr(self, "properties"):
2023-12-27 22:25:12 -08:00
return (nouns, adjectives)
for prop_name, prop in self.properties.items():
2023-12-29 19:24:26 -08:00
if hasattr(prop, "nouns"):
nouns[prop_name] = equal_weights(prop.nouns.split(","), blank=False)
if hasattr(prop, "adjectives"):
adjectives[prop_name] = equal_weights(prop.adjectives.split(","), blank=False)
2023-12-29 19:22:51 -08:00
return (nouns, adjectives)
2023-12-27 22:25:12 -08:00
def _name_template(self, with_adjectives: bool, with_nouns: bool) -> str:
2023-12-31 11:55:54 -08:00
"""
Generate a WeightedSet of potential name template strings and pick one.
The possible templates are determined by whether we have selected
nouns, adjectives, or both to describe the item; we can't use a
template that includes {adjectives} if no adjectives are defined in the
Item's properties, for example.
The weighted distribution is tuned so that we mostly get names of the
typical form ("Venomous Shortsowrd", "Dagger of Shocks"). Occasionally
we will select templates resulting in very long names ("Frosty +3 Mace
of Mighty Striking") or repeitive descriptions ("Flaming Spear of
Flames"), but not so often as to make all generated items too silly.
"""
2023-12-27 22:25:12 -08:00
num_properties = len(self.properties)
options = []
2023-12-29 19:22:51 -08:00
if with_nouns and not with_adjectives:
2023-12-29 19:24:26 -08:00
options.append(("{name} of {nouns}", 0.5))
2023-12-29 19:22:51 -08:00
if with_adjectives and not with_nouns:
2023-12-29 19:24:26 -08:00
options.append(("{adjectives} {name}", 0.5))
2023-12-27 22:25:12 -08:00
if with_nouns and with_adjectives:
if num_properties == 1:
2023-12-29 19:24:26 -08:00
options.append(("{adjectives} {name} of {nouns}", 1.0))
2023-12-27 22:25:12 -08:00
elif num_properties > 1:
2023-12-29 19:24:26 -08:00
options.extend(
[
("{adjectives} {name} of {nouns}", 1.0),
("{name} of {adjectives} {nouns}", 0.5),
]
)
2023-12-27 22:25:12 -08:00
return WeightedSet(*options).random()
def _random_descriptors(self):
"""
2023-12-31 11:55:54 -08:00
Select random nouns and adjectives from the available descriptors in
the properties attribute.
This method ensures that if a property adding fire damage to the wepaon
is chosen, 'Flames' or 'Flaming' or 'Fire' (or whatever is defined on
that enchantement) is used when naming the Item.
The randomly-selected set of nouns and/or adjectives will then govern
the naming template selected; see _random_template() above.
2023-12-27 22:25:12 -08:00
"""
random_nouns = []
random_adjectives = []
(nouns, adjectives) = self._descriptors()
if not (nouns or adjectives):
return (random_nouns, random_adjectives)
def add_word(key, obj, source):
obj.append(source[key].random().strip())
2023-12-29 19:22:51 -08:00
seen_nouns = dict()
2023-12-27 22:25:12 -08:00
for prop_name in set(list(nouns.keys()) + list(adjectives.keys())):
2023-12-31 11:55:54 -08:00
2023-12-29 19:22:51 -08:00
if prop_name in nouns and prop_name not in adjectives:
if prop_name not in seen_nouns:
2023-12-31 11:55:54 -08:00
# Ensure we only add one noun for each property so taht we
# don't end up with Items named like "Mace of Venomous
# Venom Poison".
2023-12-29 19:22:51 -08:00
add_word(prop_name, random_nouns, nouns)
seen_nouns[prop_name] = True
elif prop_name in adjectives and prop_name not in nouns:
add_word(prop_name, random_adjectives, adjectives)
2023-12-27 22:25:12 -08:00
if prop_name in nouns and prop_name in adjectives:
2023-12-29 19:22:51 -08:00
# if the property has both nouns and adjectives, select one
# or the other or both, for the weapon name. Both leads to
# spurious names like 'thundering dagger of thunder', so we
# we reduce the likelihood of this eventuality so it is an
# occasionl bit of silliness, not consistently silly.
2023-12-27 22:25:12 -08:00
val = random.random()
2023-12-29 19:22:51 -08:00
if val <= 0.4 and prop_name not in seen_nouns:
2023-12-27 22:25:12 -08:00
add_word(prop_name, random_nouns, nouns)
2023-12-29 19:22:51 -08:00
seen_nouns[prop_name] = True
2023-12-27 22:25:12 -08:00
elif val <= 0.8:
add_word(prop_name, random_adjectives, adjectives)
else:
add_word(prop_name, random_nouns, nouns)
add_word(prop_name, random_adjectives, adjectives)
2023-12-31 11:55:54 -08:00
# Join multiple nouns together, so that instead of "Staff of Strikes
# Cold" we get "Staff of Strikes and Cold." Adjectives we just join
# together ("Staff of Frosty Striking")
2023-12-29 19:24:26 -08:00
random_nouns = " and ".join(random_nouns)
random_adjectives = " ".join(random_adjectives)
2023-12-27 22:25:12 -08:00
return (random_nouns, random_adjectives)
@cached_property
def name(self) -> str:
2023-12-31 11:55:54 -08:00
"""
Generate and cache a random name for the Weapon based on its properties.
This method selects a subset of random nouns and adjectives from the
Weapon's properties, a random name string template compatible with
those descriptors, and returns a formatted title string. The return
value is cached because there are multiple possisble names for a Weapon
with the same set of properties, and we don't want multiple references
to self.name to return different values on the same instance.
"""
2023-12-27 22:25:12 -08:00
base_name = super().name
(nouns, adjectives) = self._random_descriptors()
if not (nouns or adjectives):
return base_name
template = self._name_template(
with_adjectives=True if adjectives else False,
with_nouns=True if nouns else False,
)
return template.format(**self, adjectives=adjectives, nouns=nouns, name=base_name).title()
@property
def to_hit(self):
2023-12-31 11:55:54 -08:00
"""
Return a string summarizing the total bonus to hit from this weapon and its properties. This
could be either a single number (+2), a die roll (1d6), or a combination of both (1d6+2).
"""
2023-12-27 22:25:12 -08:00
bonus_val = 0
2023-12-29 19:24:26 -08:00
bonus_dice = ""
if not hasattr(self, "properties"):
return ""
2023-12-27 22:25:12 -08:00
for prop in self.properties.values():
2023-12-29 19:24:26 -08:00
mod = getattr(prop, "to_hit", None)
2023-12-27 22:25:12 -08:00
if not mod:
continue
if type(mod) is int:
bonus_val += mod
elif type(mod) is str:
bonus_dice += f"+{mod}"
return f"+{bonus_val}{bonus_dice}"
@property
def damage_dice(self):
2023-12-31 11:55:54 -08:00
"""
Return a string summarizing the damage done by a hit with this weapon
by combining all the damage types and dice from the Weapon's
properties. Examples:
- 5 Bludgeoning
- 1d6 Piercing
- 1d8+1 Thunder
- 1d6+1 Slashing + 1d4 Thunder + 3 Poison
"""
2023-12-29 19:24:26 -08:00
if not hasattr(self, "properties"):
return ""
dmg = {self.damage_type: str(self.damage) or ""}
2023-12-27 22:25:12 -08:00
for prop in self.properties.values():
2023-12-29 19:24:26 -08:00
mod = getattr(prop, "damage", None)
2023-12-27 22:25:12 -08:00
if not mod:
continue
key = str(prop.damage_type)
2023-12-29 19:24:26 -08:00
this_damage = dmg.get(key, "")
2023-12-27 22:25:12 -08:00
if this_damage:
dmg[key] = f"{this_damage}+{mod}"
else:
dmg[key] = mod
2023-12-29 19:24:26 -08:00
return " + ".join([f"{v} {k}" for k, v in dmg.items()])
2023-12-27 22:25:12 -08:00
@property
def summary(self):
2023-12-31 11:55:54 -08:00
"""
Return a one-line summary of the Weapon's attack. For example:
+2 to hit, 5 ft., 1 tgts. 1d6+2 Piercing + 1d6 Fire
"""
2023-12-29 19:22:51 -08:00
return f"{self.to_hit} to hit, {self.range} ft., {self.targets} tgts. {self.damage_dice}"
2023-12-27 22:25:12 -08:00
@property
def details(self):
"""
2023-12-31 11:55:54 -08:00
Return details of the Weapon as a multi-line string.
2023-12-27 22:25:12 -08:00
"""
2023-12-29 19:24:26 -08:00
props = ", ".join(self.get("properties", dict()).keys())
return "\n".join(
[
f"{self.name}",
f" * {self.rarity.rarity} {self.category} weapon ({props})",
f" * {self.summary}",
f"\n{self.description}\n",
]
)
2023-12-27 22:25:12 -08:00
2023-12-31 11:55:54 -08:00
@cached_property
2023-12-29 19:22:51 -08:00
def id(self):
2023-12-31 11:55:54 -08:00
"""
Generate a unique ID for this weapon. Unlike self.name, which may have
any of several possisble generated values based on the Weapon's
properties, this property will generate the same ID every for every
instance with the same properties.
This is useful for asserting equality between instances, which may
be helpful when (for example) generating weapon look-up tables,
web pages, item cards, and so on.
"""
2023-12-29 19:24:26 -08:00
sha1bytes = hashlib.sha1(
"".join(
[
self._name,
self.to_hit,
self.damage_dice,
]
).encode()
)
2023-12-31 11:55:54 -08:00
# Only use the first ten characteres of the encoded value. This
# increases the likelihood of hash collisions, but 10 characters is far
# more than is necessary to encode all possible weapons generated by
# this package, and 10 is friendlier for user-facing use casees, such
# as stub URLs.
2023-12-29 19:24:26 -08:00
return base64.urlsafe_b64encode(sha1bytes.digest()).decode("ascii")[:10]
2023-12-29 19:22:51 -08:00
2023-12-27 22:25:12 -08:00
class WeaponGenerator(types.ItemGenerator):
2023-12-31 11:55:54 -08:00
"""
A subclass of ItemGenerator that generates Weapon instances.
"""
2023-12-27 22:25:12 -08:00
item_class = Weapon
def __init__(
self,
bases: WeightedSet = types.WEAPON_TYPES,
rarity: WeightedSet = types.RARITY,
properties_by_rarity: dict = types.PROPERTIES_BY_RARITY,
):
super().__init__(bases=bases, rarity=rarity, properties_by_rarity=properties_by_rarity)
def random_properties(self) -> dict:
2023-12-31 11:55:54 -08:00
# add missing base weapon defaults
# TODO: update the sources then delete this method
2023-12-27 22:25:12 -08:00
item = super().random_properties()
2023-12-29 19:24:26 -08:00
item["targets"] = 1
if item["category"] == "Martial":
if not item["range"]:
item["range"] = ""
2023-12-27 22:25:12 -08:00
return item
def get_enchantment(self, **attrs) -> dict:
2023-12-31 11:55:54 -08:00
"""
PROPERTIES_BY_RARITY includes references to enchamentments, so make
sure we know how to generate a random enchantment when it is referenced
by a template string.
"""
2023-12-27 22:25:12 -08:00
prop = types.ENCHANTMENT.random()
2023-12-29 19:24:26 -08:00
prop["adjectives"] = random_from_csv(prop["adjectives"])
prop["nouns"] = random_from_csv(prop["nouns"])
2023-12-27 22:25:12 -08:00
return prop