106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from engine.character import Character
|
|
from engine.defs import DefRegistry
|
|
from engine.entity import EntityPosition
|
|
from engine.map import Map
|
|
from engine.tile import Tile, TileLayerInstance
|
|
from engine.world import World
|
|
from resources.logic.actions import activate_implant
|
|
|
|
DEFS_DIR = Path(__file__).resolve().parent.parent / "resources" / "defs"
|
|
|
|
|
|
@pytest.fixture()
|
|
def registry():
|
|
return DefRegistry.load(DEFS_DIR)
|
|
|
|
|
|
def make_human(registry, **kwargs) -> Character:
|
|
return Character(species_id="human", default_body_parts=registry.new_default_body_parts("human"), **kwargs)
|
|
|
|
|
|
def make_world(registry):
|
|
field = Map("field", 15, 15, 1)
|
|
for x in range(15):
|
|
for y in range(15):
|
|
field.set_tile(x, y, 0, Tile(flooring=TileLayerInstance("grass")))
|
|
world = World(registry=registry)
|
|
world.add_map(field)
|
|
return world
|
|
|
|
|
|
# --- install/uninstall -----------------------------------------------------------------------
|
|
|
|
|
|
def test_install_implant_adds_to_character(registry):
|
|
character = make_human(registry)
|
|
assert character.install_implant("chronoimplant", registry) is True
|
|
assert "chronoimplant" in character.implants
|
|
|
|
|
|
def test_install_implant_rejects_non_implant_item(registry):
|
|
character = make_human(registry)
|
|
assert character.install_implant("chronowand", registry) is False
|
|
assert character.implants == []
|
|
|
|
|
|
def test_install_implant_rejects_duplicate(registry):
|
|
character = make_human(registry)
|
|
character.install_implant("chronoimplant", registry)
|
|
assert character.install_implant("chronoimplant", registry) is False
|
|
assert character.implants == ["chronoimplant"]
|
|
|
|
|
|
def test_uninstall_implant_removes_it(registry):
|
|
character = make_human(registry)
|
|
character.install_implant("chronoimplant", registry)
|
|
character.uninstall_implant("chronoimplant")
|
|
assert character.implants == []
|
|
|
|
|
|
def test_uninstall_implant_no_op_if_not_installed(registry):
|
|
character = make_human(registry)
|
|
character.uninstall_implant("chronoimplant") # should not raise
|
|
assert character.implants == []
|
|
|
|
|
|
# --- activate_implant -------------------------------------------------------------------------
|
|
|
|
|
|
def test_activate_implant_casts_time_warp_sphere(registry):
|
|
world = make_world(registry)
|
|
caster = make_human(registry, position=EntityPosition("field", 5.0, 5.0, 0.0))
|
|
caster.install_implant("chronoimplant", registry)
|
|
world.add_entity(caster)
|
|
|
|
assert world.time_warp_zones == []
|
|
result = activate_implant(caster, world, registry, "chronoimplant", now=3.0)
|
|
assert len(world.time_warp_zones) == 1
|
|
assert result is world.time_warp_zones[0]
|
|
assert result.expires_at > 3.0
|
|
|
|
|
|
def test_activate_implant_returns_none_if_not_installed(registry):
|
|
world = make_world(registry)
|
|
caster = make_human(registry, position=EntityPosition("field", 5.0, 5.0, 0.0))
|
|
world.add_entity(caster)
|
|
|
|
result = activate_implant(caster, world, registry, "chronoimplant", now=3.0)
|
|
assert result is None
|
|
assert world.time_warp_zones == []
|
|
|
|
|
|
def test_activate_implant_returns_none_for_non_ability_implant(registry):
|
|
world = make_world(registry)
|
|
caster = make_human(registry, position=EntityPosition("field", 5.0, 5.0, 0.0))
|
|
caster.install_implant("chronoimplant", registry)
|
|
world.add_entity(caster)
|
|
# a hypothetical implant with no grants_ability should be a safe no-op, not a crash
|
|
object.__setattr__(registry.get_item_def("chronoimplant"), "grants_ability", None)
|
|
|
|
result = activate_implant(caster, world, registry, "chronoimplant", now=3.0)
|
|
assert result is None
|