Last active
May 15, 2024 18:26
-
-
Save numberoverzero/a009a14f8d04aabfd8caf44d68cdb93a to your computer and use it in GitHub Desktop.
muddy-2024-05-15
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import random | |
import dataclasses | |
DEBUG = bool("DEBUG" in os.environ) | |
def rand(x, y, label="") -> int: | |
value = int(random.randrange(x, y)) | |
if label and DEBUG: | |
print(f"generated random {value} from [{x}, {y}) for {label}") | |
return value | |
@dataclasses.dataclass | |
class Environment: | |
bonus_school: str = "fire" | |
bonus_scale: int = 2 | |
temperature: int = int((212 + 32) / 2) | |
def scale_for_env(self, value: int, value_school: str) -> int: | |
if value_school == self.bonus_school: | |
value *= self.bonus_scale | |
return value | |
def set_temp(self, new_temp: int) -> None: | |
self.temperature = new_temp | |
if self.bonus_school == "water" and self.temperature <= 32: | |
self.bonus_school = "ice" | |
elif self.bonus_school == "ice" and 32 <= self.temperature < 212: | |
self.bonus_school = "water" | |
@dataclasses.dataclass | |
class Weapon: | |
name: str | |
scale: int | |
min: int | |
max: int | |
magic_school: str = "physical" | |
def roll(self, environment: Environment) -> int: | |
scale = environment.scale_for_env(self.scale, self.magic_school) | |
value = scale * rand(self.min, self.max, label=f"weapon:{self.name}") | |
return value | |
def can_cause_lethal(self, target: int, environment: Environment) -> bool: | |
scale = environment.scale_for_env(self.scale, self.magic_school) | |
return scale * self.max > target | |
def roll(name: str, scale: int, min: int, max: int, magic_school: str, environment: Environment) -> int: | |
if magic_school == environment.bonus_school: | |
scale *= 2 | |
value = scale * rand(min, max, label=f"weapon:{name}") | |
return value | |
def can_cause_lethal( | |
weapon_name: str, | |
weapon_scale: int, | |
weapon_min: int, | |
weapon_max: int, | |
weapon_magic_school: str, | |
target: int, | |
environment: Environment, | |
) -> bool: | |
if weapon_magic_school == environment.bonus_school: | |
weapon_scale *= 2 | |
return weapon_scale * weapon_max > target | |
my_json = {"name": "greatsword", "scale": 4, "min": 2, "max": 5} | |
env = Environment(bonus_scale=2, bonus_school="water") | |
# temperature dropped, water is now ice | |
env.bonus_school = "ice" | |
weapon = Weapon(**my_json) | |
value = weapon.roll(env) | |
print(value) | |
value = roll( | |
my_json["name"], my_json["scale"], my_json["min"], my_json["max"], my_json.get("magic_school", "physical"), env | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment