# SPDX-License-Identifier: GPL-3.0
# Copyright (c) 2014-2023 William Edwards <shadowapex@gmail.com>, Benjamin Bean <superman2k5@gmail.com>
from __future__ import annotations
from dataclasses import dataclass
from operator import eq, ge, gt, le, lt
from typing import TYPE_CHECKING, Callable, Mapping, Optional, Union
from tuxemon.item.itemcondition import ItemCondition
if TYPE_CHECKING:
from tuxemon.monster import Monster
cmp_dict: Mapping[Optional[str], Callable[[float, float], bool]] = {
None: ge,
"<": lt,
"<=": le,
">": gt,
">=": ge,
"==": eq,
"=": eq,
}
[docs]@dataclass
class CurrentHitPointsCondition(ItemCondition):
"""
Compares the target Monster's current hitpoints against the given value.
If an integer is passed, it will compare against the number directly, if a
decimal between 0.0 and 1.0 is passed it will compare the current hp
against the total hp.
Example: To make an item only usable if a monster is at less than full
health, you would use the condition "is current_hp target,<,1.0"
"""
name = "current_hp"
comparison: str
value: Union[int, float]
[docs] def test(self, target: Monster) -> bool:
lhs = target.current_hp
op = cmp_dict[self.comparison]
if type(self.value) is float:
rhs = target.hp * self.value
else:
rhs = self.value
return op(lhs, rhs)