From a7dda4887636a1c7ebc0dabd9fa8d3c0a971bc97 Mon Sep 17 00:00:00 2001 From: Fris0uman <41293484+Fris0uman@users.noreply.github.com> Date: Wed, 26 Aug 2020 07:48:43 +0200 Subject: [PATCH] Apply effects from passive enchantments (#43101) --- data/mods/TEST_DATA/bionics.json | 16 +++++ data/mods/TEST_DATA/enchantments.json | 18 ++++++ data/mods/TEST_DATA/items.json | 18 ++++++ data/mods/TEST_DATA/mutations.json | 13 ++++ src/item.cpp | 10 ---- src/suffer.cpp | 2 + tests/enchantments_test.cpp | 86 +++++++++++++++++++++++++++ 7 files changed, 153 insertions(+), 10 deletions(-) create mode 100644 data/mods/TEST_DATA/bionics.json create mode 100644 data/mods/TEST_DATA/enchantments.json create mode 100644 data/mods/TEST_DATA/mutations.json create mode 100644 tests/enchantments_test.cpp diff --git a/data/mods/TEST_DATA/bionics.json b/data/mods/TEST_DATA/bionics.json new file mode 100644 index 0000000000000..0e1f2b250f28e --- /dev/null +++ b/data/mods/TEST_DATA/bionics.json @@ -0,0 +1,16 @@ +[ + { + "id": "test_bio_night", + "type": "bionic", + "name": { "str": "Artificial Night Generator" }, + "description": "When active, this bionic eliminates all light within a 2 tile radius through destructive interference.", + "occupied_bodyparts": [ [ "torso", 16 ] ], + "flags": [ "BIONIC_TOGGLED" ], + "enchantments": [ "TEST_ENCH" ], + "act_cost": "9 kJ", + "react_cost": "9 kJ", + "included": true, + "//": "not actually included, this is a trick to not have to add an item version.", + "time": 1 + } +] diff --git a/data/mods/TEST_DATA/enchantments.json b/data/mods/TEST_DATA/enchantments.json new file mode 100644 index 0000000000000..150617c6df896 --- /dev/null +++ b/data/mods/TEST_DATA/enchantments.json @@ -0,0 +1,18 @@ +[ + { + "type": "enchantment", + "id": "TEST_ENCH", + "condition": "ALWAYS", + "emitter": "emit_shadow_field", + "values": [ { "value": "STRENGTH", "multiply": 2, "add": 25 } ], + "hit_me_effect": [ + { + "id": "generic_blinding_spray_1", + "hit_self": false, + "message": "Your ink glands spray some ink into %2$s's eyes.", + "npc_message": "%1$s's ink glands spay some ink into %2$s's eyes." + } + ], + "ench_effects": [ { "effect": "invisibility", "intensity": 1 } ] + } +] diff --git a/data/mods/TEST_DATA/items.json b/data/mods/TEST_DATA/items.json index 08d52f2314de5..951acd4875aaf 100644 --- a/data/mods/TEST_DATA/items.json +++ b/data/mods/TEST_DATA/items.json @@ -21,6 +21,24 @@ "effects": [ "NEVER_MISFIRES", "NON-FOULING", "RECOVER_80" ], "qualities": [ [ "HAMMER", 1 ] ] }, + { + "type": "TOOL_ARMOR", + "id": "test_ring_strength_1", + "weight": "4 g", + "volume": "1 ml", + "price": 5000, + "material": [ "copper" ], + "symbol": "[", + "color": "light_red", + "covers": [ "hand_l", "hand_r" ], + "sided": true, + "coverage": 0, + "warmth": 0, + "flags": [ "WATER_FRIENDLY", "ALLOWS_NATURAL_ATTACKS", "FANCY", "ONLY_ONE", "SKINTIGHT" ], + "name": { "str": "ring of strength +1", "str_pl": "rings of strength +1" }, + "description": "A copper ring that makes you a little stronger when you wear it.", + "relic_data": { "passive_effects": [ { "has": "WORN", "condition": "ALWAYS", "values": [ { "value": "STRENGTH", "add": 1 } ] } ] } + }, { "id": "test_rag", "type": "TOOL", diff --git a/data/mods/TEST_DATA/mutations.json b/data/mods/TEST_DATA/mutations.json new file mode 100644 index 0000000000000..61ed54258f8b3 --- /dev/null +++ b/data/mods/TEST_DATA/mutations.json @@ -0,0 +1,13 @@ +[ + { + "type": "mutation", + "id": "TEST_INK_GLANDS", + "name": "Ink glands", + "points": 1, + "visibility": 1, + "ugliness": 1, + "description": "Several ink glands have grown onto your torso. They can be used to spray defensive ink and blind an attacker in an emergency, as long as the torso isn't covered.", + "enchantments": [ "TEST_ENCH" ], + "category": [ "CEPHALOPOD" ] + } +] diff --git a/src/item.cpp b/src/item.cpp index 2a1b67a4c5ec5..977b2c4583d7b 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -9414,16 +9414,6 @@ void item::process_relic( Character *carrier, const tripoint &pos ) active_enchantments.emplace_back( ench ); } } - - for( const enchantment &ench : active_enchantments ) { - ench.activate_passive( *carrier ); - } - - // Recalculate, as it might have changed (by mod_*_bonus above) - carrier->str_cur = carrier->get_str(); - carrier->int_cur = carrier->get_int(); - carrier->dex_cur = carrier->get_dex(); - carrier->per_cur = carrier->get_per(); } bool item::process_corpse( player *carrier, const tripoint &pos ) diff --git a/src/suffer.cpp b/src/suffer.cpp index a14d77a520622..dfe7167dd2513 100644 --- a/src/suffer.cpp +++ b/src/suffer.cpp @@ -1501,6 +1501,8 @@ void Character::suffer() suffer_without_sleep( sleep_deprivation ); suffer_from_tourniquet(); suffer_from_pain(); + //Suffer from enchantments + enchantment_cache->activate_passive( *this ); } bool Character::irradiate( float rads, bool bypass ) diff --git a/tests/enchantments_test.cpp b/tests/enchantments_test.cpp new file mode 100644 index 0000000000000..b167f1461c30f --- /dev/null +++ b/tests/enchantments_test.cpp @@ -0,0 +1,86 @@ +#include "catch/catch.hpp" + +#include "avatar.h" +#include "bionics.h" +#include "character.h" +#include "field.h" +#include "map.h" +#include "map_helpers.h" +#include "monster.h" +#include "mutation.h" +#include "player_helpers.h" +#include "item.h" + + + +static void test_generic_ench( avatar &p, int str_before ) +{ + // wait a turn for the effect to kick in + p.process_turn(); + + CHECK( p.get_str() == str_before + p.get_str_base() * 2 + 25 ); + + CHECK( p.has_effect( efftype_id( "invisibility" ) ) ); + + const field &fields_here = get_map().field_at( p.pos() ); + CHECK( fields_here.find_field( field_type_id( "fd_shadow" ) ) != nullptr ); + + // place a zombie next to the avatar + const tripoint spot( 61, 60, 0 ); + clear_map(); + monster &zombie = spawn_test_monster( "mon_zombie", spot ); + + p.on_hit( &zombie, bodypart_id( "torso" ), 0.0, nullptr ); + + CHECK( zombie.has_effect( efftype_id( "blind" ) ) ); +} + +TEST_CASE( "worn enchantments", "[enchantments][worn][items]" ) +{ + avatar p; + clear_character( p ); + + int str_before = p.get_str(); + + // put on the ring + item &equiped_ring_strplus_one = p.i_add( item( "test_ring_strength_1" ) ); + p.wear( equiped_ring_strplus_one, false ); + + // wait a turn for the effect to kick in + p.recalculate_enchantment_cache(); + p.process_turn(); + + CHECK( p.get_str() == str_before + 1 ); + +} + +TEST_CASE( "bionic enchantments", "[enchantments][bionics]" ) +{ + avatar p; + clear_character( p ); + + int str_before = p.get_str(); + + p.set_max_power_level( 100_kJ ); + p.set_power_level( 100_kJ ); + + give_and_activate_bionic( p, bionic_id( "test_bio_night" ) ); + + test_generic_ench( p, str_before ); +} + +TEST_CASE( "mutation enchantments", "[enchantments][mutations]" ) +{ + avatar p; + clear_character( p ); + + const trait_id test_ink( "TEST_INK_GLANDS" ); + int str_before = p.get_str(); + + p.toggle_trait( test_ink ); + REQUIRE( p.has_trait( test_ink ) ); + + p.recalculate_enchantment_cache(); + + test_generic_ench( p, str_before ); +}