edited docstrings to reflect multiplier addition. added mult to validation and string methods
This commit is contained in:
parent
15e966a9c5
commit
b355008295
1 changed files with 56 additions and 50 deletions
|
|
@ -45,7 +45,7 @@ class Character(DefaultCharacter):
|
||||||
|
|
||||||
def at_object_creation(self):
|
def at_object_creation(self):
|
||||||
# (or wherever you want)
|
# (or wherever you want)
|
||||||
self.traits.add("str", "Strength", trait_type="static", base=10, mod=2)
|
self.traits.add("str", "Strength", trait_type="static", base=10, mod=2, mult=2.0)
|
||||||
self.traits.add("hp", "Health", trait_type="gauge", min=0, max=100)
|
self.traits.add("hp", "Health", trait_type="gauge", min=0, max=100)
|
||||||
self.traits.add("hunting", "Hunting Skill", trait_type="counter",
|
self.traits.add("hunting", "Hunting Skill", trait_type="counter",
|
||||||
base=10, mod=1, min=0, max=100)
|
base=10, mod=1, min=0, max=100)
|
||||||
|
|
@ -74,9 +74,9 @@ from evennia.contrib.rpg.traits import TraitProperty
|
||||||
|
|
||||||
class Object(DefaultObject):
|
class Object(DefaultObject):
|
||||||
...
|
...
|
||||||
strength = TraitProperty("Strength", trait_type="static", base=10, mod=2)
|
strength = TraitProperty("Strength", trait_type="static", base=10, mod=2, mult=1.5)
|
||||||
health = TraitProperty("Health", trait_type="gauge", min=0, base=100, mod=2)
|
health = TraitProperty("Health", trait_type="gauge", min=0, base=100, mod=2)
|
||||||
hunting = TraitProperty("Hunting Skill", trait_type="counter", base=10, mod=1, min=0, max=100)
|
hunting = TraitProperty("Hunting Skill", trait_type="counter", base=10, mod=1, mult=2.0, min=0, max=100)
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -102,14 +102,14 @@ each other depends on the trait type.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
> obj.traits.strength.value
|
> obj.traits.strength.value
|
||||||
12 # base + mod
|
18 # (base + mod) * mult
|
||||||
|
|
||||||
> obj.traits.strength.base += 5
|
> obj.traits.strength.base += 6
|
||||||
obj.traits.strength.value
|
obj.traits.strength.value
|
||||||
17
|
27
|
||||||
|
|
||||||
> obj.traits.hp.value
|
> obj.traits.hp.value
|
||||||
102 # base + mod
|
102 # (base + mod) * mult
|
||||||
|
|
||||||
> obj.traits.hp.base -= 200
|
> obj.traits.hp.base -= 200
|
||||||
> obj.traits.hp.value
|
> obj.traits.hp.value
|
||||||
|
|
@ -131,11 +131,11 @@ obj.traits.strength.value
|
||||||
# with TraitProperties:
|
# with TraitProperties:
|
||||||
|
|
||||||
> obj.hunting.value
|
> obj.hunting.value
|
||||||
12
|
22
|
||||||
|
|
||||||
> obj.strength.value += 5
|
> obj.strength.value += 5
|
||||||
> obj.strength.value
|
> obj.strength.value
|
||||||
17
|
32
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -171,25 +171,26 @@ if trait1 > trait2:
|
||||||
```
|
```
|
||||||
## Static trait
|
## Static trait
|
||||||
|
|
||||||
`value = base + mod`
|
`value = (base + mod) * mult`
|
||||||
|
|
||||||
The static trait has a `base` value and an optional `mod`-ifier. A typical use
|
The static trait has a `base` value and an optional `mod`-ifier and 'mult'-iplier. A typical use
|
||||||
of a static trait would be a Strength stat or Skill value. That is, something
|
of a static trait would be a Strength stat or Skill value. That is, something
|
||||||
that varies slowly or not at all, and which may be modified in-place.
|
that varies slowly or not at all, and which may be modified in-place.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
> obj.traits.add("str", "Strength", trait_type="static", base=10, mod=2)
|
> obj.traits.add("str", "Strength", trait_type="static", base=10, mod=2)
|
||||||
> obj.traits.mytrait.value
|
> obj.traits.mytrait.value
|
||||||
|
|
||||||
12 # base + mod
|
12 # base + mod
|
||||||
|
|
||||||
> obj.traits.mytrait.base += 2
|
> obj.traits.mytrait.base += 2
|
||||||
> obj.traits.mytrait.mod += 1
|
> obj.traits.mytrait.mod += 1
|
||||||
> obj.traits.mytrait.value
|
> obj.traits.mytrait.value
|
||||||
15
|
15
|
||||||
|
|
||||||
> obj.traits.mytrait.mod = 0
|
> obj.traits.mytrait.mod = 0
|
||||||
|
> obj.traits.mytrait.mult = 2.0
|
||||||
> obj.traits.mytrait.value
|
> obj.traits.mytrait.value
|
||||||
12
|
20
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -201,16 +202,17 @@ that varies slowly or not at all, and which may be modified in-place.
|
||||||
current value
|
current value
|
||||||
= current
|
= current
|
||||||
+ mod
|
+ mod
|
||||||
|
* mult
|
||||||
|
|
||||||
A counter describes a value that can move from a base. The `.current` property
|
A counter describes a value that can move from a base. The `.current` property
|
||||||
is the thing usually modified. It starts at the `.base`. One can also add a
|
is the thing usually modified. It starts at the `.base`. One can also add a
|
||||||
modifier, which will both be added to the base and to current (forming
|
modifier, which is added to both the base and to current. '.value' is then formed
|
||||||
`.value`). The min/max of the range are optional, a boundary set to None will
|
by multiplying by the multiplier. The min/max of the range are optional, a boundary
|
||||||
remove it. A suggested use for a Counter Trait would be to track skill values.
|
set to None will remove it. A suggested use for a Counter Trait would be to track skill values.
|
||||||
|
|
||||||
```python
|
```python
|
||||||
> obj.traits.add("hunting", "Hunting Skill", trait_type="counter",
|
> obj.traits.add("hunting", "Hunting Skill", trait_type="counter",
|
||||||
base=10, mod=1, min=0, max=100)
|
base=10, mod=1, mult=1.0, min=0, max=100)
|
||||||
> obj.traits.hunting.value
|
> obj.traits.hunting.value
|
||||||
11 # current starts at base + mod
|
11 # current starts at base + mod
|
||||||
|
|
||||||
|
|
@ -222,7 +224,10 @@ remove it. A suggested use for a Counter Trait would be to track skill values.
|
||||||
> del obj.traits.hunting.current
|
> del obj.traits.hunting.current
|
||||||
> obj.traits.hunting.value
|
> obj.traits.hunting.value
|
||||||
11
|
11
|
||||||
|
|
||||||
> obj.traits.hunting.max = None # removing upper bound
|
> obj.traits.hunting.max = None # removing upper bound
|
||||||
|
> obj.traits.hunting.mult = 100.0
|
||||||
|
1100
|
||||||
|
|
||||||
# for TraitProperties, pass the args/kwargs of traits.add() to the
|
# for TraitProperties, pass the args/kwargs of traits.add() to the
|
||||||
# TraitProperty constructor instead.
|
# TraitProperty constructor instead.
|
||||||
|
|
@ -1142,10 +1147,10 @@ class Trait:
|
||||||
|
|
||||||
class StaticTrait(Trait):
|
class StaticTrait(Trait):
|
||||||
"""
|
"""
|
||||||
Static Trait. This is a single value with a modifier,
|
Static Trait. This is a single value with a modifier,
|
||||||
with no concept of a 'current' value or min/max etc.
|
multiplier, and no concept of a 'current' value or min/max etc.
|
||||||
|
|
||||||
value = base + mod
|
value = (base + mod) * mult
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
@ -1155,7 +1160,7 @@ class StaticTrait(Trait):
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
status = "{value:11}".format(value=self.value)
|
status = "{value:11}".format(value=self.value)
|
||||||
return "{name:12} {status} ({mod:+3})".format(name=self.name, status=status, mod=self.mod)
|
return "{name:12} {status} ({mod:+3}) (* {mult:.2f})".format(name=self.name, status=status, mod=self.mod, mult=self.mult)
|
||||||
|
|
||||||
# Helpers
|
# Helpers
|
||||||
@property
|
@property
|
||||||
|
|
@ -1207,8 +1212,9 @@ class CounterTrait(Trait):
|
||||||
current value
|
current value
|
||||||
= current
|
= current
|
||||||
+ mod
|
+ mod
|
||||||
|
* mult
|
||||||
|
|
||||||
- value = current + mod, starts at base + mod
|
- value = (current + mod) * mult, starts at base + mod
|
||||||
- if min or max is None, there is no upper/lower bound (default)
|
- if min or max is None, there is no upper/lower bound (default)
|
||||||
- if max is set to "base", max will be equal ot base+mod
|
- if max is set to "base", max will be equal ot base+mod
|
||||||
- descs are used to optionally describe each value interval.
|
- descs are used to optionally describe each value interval.
|
||||||
|
|
@ -1311,16 +1317,16 @@ class CounterTrait(Trait):
|
||||||
now = time()
|
now = time()
|
||||||
tdiff = now - self._data["last_update"]
|
tdiff = now - self._data["last_update"]
|
||||||
current += rate * tdiff
|
current += rate * tdiff
|
||||||
value = current + self.mod
|
value = (current + self.mod) * self.mult
|
||||||
|
|
||||||
# we must make sure so we don't overstep our bounds
|
# we must make sure so we don't overstep our bounds
|
||||||
# even if .mod is included
|
# even if .mod is included
|
||||||
|
|
||||||
if self._passed_ratetarget(value):
|
if self._passed_ratetarget(value):
|
||||||
current = self._data["ratetarget"] - self.mod
|
current = (self._data["ratetarget"] - self.mod) / self.mult
|
||||||
self._stop_timer()
|
self._stop_timer()
|
||||||
elif not self._within_boundaries(value):
|
elif not self._within_boundaries(value):
|
||||||
current = self._enforce_boundaries(value) - self.mod
|
current = (self._enforce_boundaries(value) - self.mod) / self.mult
|
||||||
self._stop_timer()
|
self._stop_timer()
|
||||||
else:
|
else:
|
||||||
self._data["last_update"] = now
|
self._data["last_update"] = now
|
||||||
|
|
@ -1342,10 +1348,10 @@ class CounterTrait(Trait):
|
||||||
if value is None:
|
if value is None:
|
||||||
self._data["base"] = self.default_keys["base"]
|
self._data["base"] = self.default_keys["base"]
|
||||||
if type(value) in (int, float):
|
if type(value) in (int, float):
|
||||||
if self.min is not None and value + self.mod < self.min:
|
if self.min is not None and (value + self.mod) * self.mult < self.min:
|
||||||
value = self.min - self.mod
|
value = (self.min - self.mod) / self.mult
|
||||||
if self.max is not None and value + self.mod > self.max:
|
if self.max is not None and (value + self.mod) * self.mult > self.max:
|
||||||
value = self.max - self.mod
|
value = (self.max - self.mod) / self.mult
|
||||||
self._data["base"] = value
|
self._data["base"] = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -1358,10 +1364,10 @@ class CounterTrait(Trait):
|
||||||
# unsetting the boundary to default
|
# unsetting the boundary to default
|
||||||
self._data["mod"] = self.default_keys["mod"]
|
self._data["mod"] = self.default_keys["mod"]
|
||||||
elif type(value) in (int, float):
|
elif type(value) in (int, float):
|
||||||
if self.min is not None and value + self.base < self.min:
|
if self.min is not None and (value + self.base) * self.mult < self.min:
|
||||||
value = self.min - self.base
|
value = (self.min - self.base) / self.mult
|
||||||
if self.max is not None and value + self.base > self.max:
|
if self.max is not None and (value + self.base) * self.mult > self.max:
|
||||||
value = self.max - self.base
|
value = (self.max - self.base) / self.mult
|
||||||
self._data["mod"] = value
|
self._data["mod"] = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -1385,7 +1391,7 @@ class CounterTrait(Trait):
|
||||||
elif type(value) in (int, float):
|
elif type(value) in (int, float):
|
||||||
if self.max is not None:
|
if self.max is not None:
|
||||||
value = min(self.max, value)
|
value = min(self.max, value)
|
||||||
self._data["min"] = min(value, self.base + self.mod)
|
self._data["min"] = min(value, (self.base + self.mod) * self.mult)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max(self):
|
def max(self):
|
||||||
|
|
@ -1399,7 +1405,7 @@ class CounterTrait(Trait):
|
||||||
elif type(value) in (int, float):
|
elif type(value) in (int, float):
|
||||||
if self.min is not None:
|
if self.min is not None:
|
||||||
value = max(self.min, value)
|
value = max(self.min, value)
|
||||||
self._data["max"] = max(value, self.base + self.mod)
|
self._data["max"] = max(value, (self.base + self.mod) * self.mult)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current(self):
|
def current(self):
|
||||||
|
|
@ -1482,15 +1488,15 @@ class GaugeTrait(CounterTrait):
|
||||||
"""
|
"""
|
||||||
Gauge Trait.
|
Gauge Trait.
|
||||||
|
|
||||||
This emulates a gauge-meter that empties from a base+mod value.
|
This emulates a gauge-meter that empties from a (base+mod) * mult value.
|
||||||
|
|
||||||
min/0 max=base+mod
|
min/0 max=(base+mod)*mult
|
||||||
|-----------------------X---------------------------|
|
|-----------------------X---------------------------|
|
||||||
value
|
value
|
||||||
= current
|
= current
|
||||||
|
|
||||||
- min defaults to 0
|
- min defaults to 0
|
||||||
- max value is always base + mad
|
- max value is always (base + mod) * mult
|
||||||
- .max is an alias of .base
|
- .max is an alias of .base
|
||||||
- value = current and varies from min to max.
|
- value = current and varies from min to max.
|
||||||
- descs is a mapping {upper_bound_inclusive: desc}. These
|
- descs is a mapping {upper_bound_inclusive: desc}. These
|
||||||
|
|
@ -1552,11 +1558,11 @@ class GaugeTrait(CounterTrait):
|
||||||
"""Ensures that incoming value falls within trait's range."""
|
"""Ensures that incoming value falls within trait's range."""
|
||||||
if self.min is not None and value <= self.min:
|
if self.min is not None and value <= self.min:
|
||||||
return self.min
|
return self.min
|
||||||
return min(self.mod + self.base, value)
|
return min((self.mod + self.base) * self.mult, value)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
status = "{value:4} / {base:4}".format(value=self.value, base=self.base)
|
status = "{value:4} / {base:4}".format(value=self.value, base=self.base)
|
||||||
return "{name:12} {status} ({mod:+3})".format(name=self.name, status=status, mod=self.mod)
|
return "{name:12} {status} ({mod:+3}) (* {mult:.2f})".format(name=self.name, status=status, mod=self.mod, mult=self.mult)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def base(self):
|
def base(self):
|
||||||
|
|
@ -1564,10 +1570,10 @@ class GaugeTrait(CounterTrait):
|
||||||
|
|
||||||
@base.setter
|
@base.setter
|
||||||
def base(self, value):
|
def base(self, value):
|
||||||
"""Limit so base+mod can never go below min."""
|
"""Limit so (base+mod)*mult can never go below min."""
|
||||||
if type(value) in (int, float):
|
if type(value) in (int, float):
|
||||||
if value + self.mod < self.min:
|
if (value + self.mod) * self.mult < self.min:
|
||||||
value = self.min - self.mod
|
value = (self.min - self.mod) / self.mult
|
||||||
self._data["base"] = value
|
self._data["base"] = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -1576,10 +1582,10 @@ class GaugeTrait(CounterTrait):
|
||||||
|
|
||||||
@mod.setter
|
@mod.setter
|
||||||
def mod(self, value):
|
def mod(self, value):
|
||||||
"""Limit so base+mod can never go below min."""
|
"""Limit so (base+mod)*mult can never go below min."""
|
||||||
if type(value) in (int, float):
|
if type(value) in (int, float):
|
||||||
if value + self.base < self.min:
|
if (value + self.base) * self.mult < self.min:
|
||||||
value = self.min - self.base
|
value = (self.min - self.base) / self.mult
|
||||||
self._data["mod"] = value
|
self._data["mod"] = value
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
@ -1598,11 +1604,11 @@ class GaugeTrait(CounterTrait):
|
||||||
|
|
||||||
@min.setter
|
@min.setter
|
||||||
def min(self, value):
|
def min(self, value):
|
||||||
"""Limit so min can never be greater than base+mod."""
|
"""Limit so min can never be greater than (base+mod)*mult."""
|
||||||
if value is None:
|
if value is None:
|
||||||
self._data["min"] = self.default_keys["min"]
|
self._data["min"] = self.default_keys["min"]
|
||||||
elif type(value) in (int, float):
|
elif type(value) in (int, float):
|
||||||
self._data["min"] = min(value, self.base + self.mod)
|
self._data["min"] = min(value, (self.base + self.mod) * self.mult)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max(self):
|
def max(self):
|
||||||
|
|
@ -1625,7 +1631,7 @@ class GaugeTrait(CounterTrait):
|
||||||
def current(self):
|
def current(self):
|
||||||
"""The `current` value of the gauge."""
|
"""The `current` value of the gauge."""
|
||||||
return self._update_current(
|
return self._update_current(
|
||||||
self._enforce_boundaries(self._data.get("current", self.base + self.mod))
|
self._enforce_boundaries(self._data.get("current", (self.base + self.mod) * self.mult))
|
||||||
)
|
)
|
||||||
|
|
||||||
@current.setter
|
@current.setter
|
||||||
|
|
@ -1636,7 +1642,7 @@ class GaugeTrait(CounterTrait):
|
||||||
@current.deleter
|
@current.deleter
|
||||||
def current(self):
|
def current(self):
|
||||||
"Resets current back to 'full'"
|
"Resets current back to 'full'"
|
||||||
self._data["current"] = self.base + self.mod
|
self._data["current"] = (self.base + self.mod) * self.mult
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def value(self):
|
def value(self):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue