Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Venera3 committed Jun 7, 2023
1 parent 8fd7e59 commit 0038d56
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 6 deletions.
4 changes: 2 additions & 2 deletions data/json/monsters/zed-classic.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
"species": [ "ZOMBIE", "HUMAN" ],
"volume": "62500 ml",
"weight": "81500 g",
"hp": 9500,
"hp": 95,
"speed": 55,
"material": [ "flesh" ],
"symbol": "Z",
Expand All @@ -209,7 +209,7 @@
"vision_night": 3,
"harvest": "zombie",
"grab_strength": 20,
"special_attacks": [ { "id": "grab", "cooldown": 5 }, { "id": "bite_humanoid", "cooldown": 5 }, { "id": "scratch_humanoid" } ],
"special_attacks": [ { "id": "grab" }, { "id": "bite_humanoid", "cooldown": 5 }, { "id": "scratch_humanoid" } ],
"death_drops": "default_zombie_death_drops",
"burn_into": "mon_zombie_scorched",
"fungalize_into": "mon_zombie_fungus",
Expand Down
86 changes: 85 additions & 1 deletion data/mods/TEST_DATA/martialarts.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,90 @@
"weapon_categories_allowed": [ "TEST_CAT1", "BLADES" ],
"stun_dur": 2
},
{
"type": "technique",
"id": "test_tech_condition_sweep",
"name": "Test Sweep",
"unarmed_allowed": true,
"condition": {
"and": [
{ "math": [ "u_val('size') + 1", ">=", "n_val('size')" ] },
{ "math": [ "n_val('size')", "!=", "1" ] },
{ "not": { "npc_has_effect": "downed" } },
{ "or": [ { "npc_bodytype": "human" }, { "npc_bodytype": "angel" } ] },
{ "or": [ { "not": { "npc_has_flag": "FLIES" } }, { "npc_has_flag": "DISABLE_FLIGHT" } ] }
]
},
"down_dur": 2,
"messages": [ "You see if you can sweep %s", "<npcname> sees if they can sweep %s" ],
"attack_vectors": [ "FOOT" ]
},
{
"type": "technique",
"id": "test_tech_condition_stun",
"name": "Test Stun",
"messages": [ "You see if you can stun %s", "<npcname> tries stunning %s" ],
"melee_allowed": true,
"condition": {
"and": [
{ "math": [ "u_val('size') + 1", ">=", "n_val('size')" ] },
{ "not": { "npc_has_effect": "stunned" } },
{
"and": [
{ "not": { "npc_has_species": "ZOMBIE" } },
{ "not": { "npc_has_species": "NETHER" } },
{ "not": { "npc_has_species": "NETHER_EMENATION" } },
{ "not": { "npc_has_species": "LEECH_PLANT" } },
{ "not": { "npc_has_species": "MIGO" } },
{ "not": { "npc_has_species": "SLIME" } },
{ "not": { "npc_has_species": "FUNGUS" } },
{ "not": { "npc_has_species": "PLANT" } },
{ "not": { "npc_has_species": "ROBOT" } },
{ "not": { "npc_has_species": "CYBORG" } },
{ "not": { "npc_has_species": "HALLUCINATION" } },
{ "not": { "npc_has_species": "HORROR" } },
{ "not": { "npc_has_species": "ABERRATION" } },
{ "not": { "npc_has_species": "KRAKEN" } }
]
}
]
},
"stun_dur": 1,
"weapon_categories_allowed": [ "MEDIUM_SWORDS" ],
"attack_vectors": [ "WEAPON" ]
},
{
"type": "technique",
"id": "test_tech_condition_knockback",
"name": "Test Knockback",
"messages": [ "You try to toss %s aside", "<npcname> tries to toss %s aside" ],
"melee_allowed": true,
"condition": {
"and": [
{ "math": [ "u_val('size') + 1", ">=", "n_val('size')" ] },
{ "not": { "npc_has_effect": "downed" } },
{
"or": [
{
"and": [
{ "npc_has_flag": "GRAB_FILTER" },
{ "u_has_flag": "GRAB" },
{
"roll_contested": { "math": [ "u_val('strength')" ] },
"die_size": 20,
"difficulty": { "math": [ "n_val('grab_strength')" ] }
}
]
},
{ "not": { "and": [ { "npc_has_flag": "GRAB_FILTER" }, { "u_has_flag": "GRAB" } ] } }
]
}
]
},
"knockback_dist": 2,
"weapon_categories_allowed": [ "MACES" ],
"attack_vectors": [ "WEAPON" ]
},
{
"type": "martial_art",
"id": "test_style_ma1",
Expand Down Expand Up @@ -37,7 +121,7 @@
"weapon_categories_allowed": "KNIVES"
}
],
"techniques": [ "test_technique" ],
"techniques": [ "test_technique", "test_tech_condition_sweep", "test_tech_condition_stun", "test_tech_condition_knockback" ],
"weapon_category": [ "TEST_CAT1" ]
}
]
18 changes: 18 additions & 0 deletions data/mods/TEST_DATA/monsters.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@
],
"upgrades": false
},
{
"type": "MONSTER",
"id": "mon_test_tech_grabber",
"name": { "str": "MA tech dialog condition test monster" },
"description": "It exists only to somehow grab you despite not having any grab strength",
"default_faction": "zombie",
"material": [ "flesh" ],
"species": [ "HUMAN", "ZOMBIE" ],
"symbol": "D",
"volume": "81500 ml",
"speed": 100,
"melee_skill": 1000,
"melee_dice": 0,
"morale": 100,
"aggression": 100,
"grab_strength": 0,
"special_attacks": [ { "id": "grab" } ]
},
{
"type": "MONSTER",
"id": "mon_test_mattack_dialog",
Expand Down
3 changes: 3 additions & 0 deletions src/character.h
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,9 @@ class Character : public Creature, public visitable
/** Returns a random valid technique */
matec_id pick_technique( Creature &t, const item_location &weap,
bool crit, bool dodge_counter, bool block_counter );
// Houses the actual picking logic and returns the vector of eligable techniques
std::vector<matec_id> evaluate_techniques( Creature &t, const item_location &weap,
bool crit = false, bool dodge_counter = false, bool block_counter = false );
void perform_technique( const ma_technique &technique, Creature &t, damage_instance &di,
int &move_cost, item_location &cur_weapon );

Expand Down
11 changes: 9 additions & 2 deletions src/melee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,10 +1458,17 @@ void Character::roll_damage( const damage_type_id &dt, bool crit, damage_instanc
di.add_damage( dt, other_dam, arpen, armor_mult, other_mul );
}
}

matec_id Character::pick_technique( Creature &t, const item_location &weap, bool crit,
bool dodge_counter, bool block_counter )
{
std::vector<matec_id> possible = evaluate_techniques( t, weap, crit,
dodge_counter, block_counter );
return random_entry( possible, tec_none );
}
std::vector<matec_id> Character::evaluate_techniques( Creature &t, const item_location &weap,
bool crit,
bool dodge_counter, bool block_counter )
{

const std::vector<matec_id> all = martial_arts_data->get_all_techniques( weap, *this );

Expand Down Expand Up @@ -1592,7 +1599,7 @@ matec_id Character::pick_technique( Creature &t, const item_location &weap, bool
}
}

return random_entry( possible, tec_none );
return possible;
}

bool Character::valid_aoe_technique( Creature &t, const ma_technique &technique )
Expand Down
104 changes: 103 additions & 1 deletion tests/martial_art_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@
#include "avatar.h"
#include "character_martial_arts.h"
#include "martialarts.h"
#include "map_helpers.h"
#include "mtype.h"
#include "npc.h"

static const efftype_id effect_downed( "downed" );
static const efftype_id effect_grabbed( "grabbed" );
static const efftype_id effect_grabbing( "grabbing" );
static const efftype_id effect_stunned( "stunned" );


static const itype_id itype_2_by_sword( "sword_wood" );
static const itype_id itype_club( "club_wooden" );
static const itype_id itype_test_weapon1( "test_weapon1" );
static const itype_id itype_test_weapon2( "test_weapon2" );

static const matec_id test_stun( "test_tech_condition_stun" );
static const matec_id test_sweep( "test_tech_condition_sweep" );
static const matec_id test_knockback( "test_tech_condition_knockback" );
static const matec_id test_tech( "test_technique" );
static const matype_id test_style_ma1( "test_style_ma1" );

static const species_id species_HUMAN( "HUMAN" );
static const species_id species_SLIME( "SLIME" );
static const species_id species_ZOMBIE( "ZOMBIE" );

static constexpr tripoint dude_pos( HALF_MAPSIZE_X, HALF_MAPSIZE_Y, 0 );

TEST_CASE( "martial arts", "[martial_arts]" )
Expand Down Expand Up @@ -48,7 +66,7 @@ TEST_CASE( "Martial art required weapon categories", "[martial_arts]" )

SECTION( "Weapon categories required for technique" ) {
REQUIRE( !test_style_ma1->techniques.empty() );
const matec_id &tec = *test_style_ma1->techniques.begin();
const matec_id &tec = *test_style_ma1->techniques.find( test_tech );

REQUIRE( tec->reqs.weapon_categories_allowed[0] == *test_style_ma1->weapon_category.begin() );
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
Expand All @@ -61,3 +79,87 @@ TEST_CASE( "Martial art required weapon categories", "[martial_arts]" )
CHECK( tec->is_valid_character( dude ) );
}
}

TEST_CASE( "Martial art technique conditionals", "[martial_arts]" )
{
standard_npc dude( "TestCharacter", dude_pos, {}, 0, 8, 8, 8, 8 );
const tripoint target_1_pos = dude_pos + tripoint_east;
const tripoint target_2_pos = dude_pos + tripoint_north;
const tripoint target_3_pos = dude_pos + tripoint_west;
clear_character( dude, true );
clear_creatures();
dude.martial_arts_data->add_martialart( test_style_ma1 );
dude.martial_arts_data->set_style( test_style_ma1, false );
REQUIRE( dude.get_size() == 3 );
SECTION( "Test sweep" ) {
const matec_id &tec = *test_style_ma1->techniques.find( test_sweep );
monster &target_1 = spawn_test_monster( "mon_zombie_fat", target_1_pos );
monster &target_2 = spawn_test_monster( "mon_zombie_hulk", target_2_pos );
monster &target_3 = spawn_test_monster( "mon_blob", target_3_pos );
std::vector<matec_id> tech_1 = dude.evaluate_techniques( target_1, dude.used_weapon() );

// test sweeping a zombie (succeed)
REQUIRE( target_1.get_size() == 3 );
CHECK( tech_1.size() == 1 );
CHECK( std::find( tech_1.begin(), tech_1.end(), tec ) != tech_1.end() );
// Being downed disables the attack
target_1.add_effect( effect_downed, 1_days );
CHECK( dude.evaluate_techniques( target_1, dude.used_weapon() ).size() == 0 );
// test sweeping a big zomb (fail)
REQUIRE( target_2.get_size() == 5 );
CHECK( dude.evaluate_techniques( target_2, dude.used_weapon() ).size() == 0 );
// test sweeping a slime (fail)
REQUIRE( target_3.get_size() == 3 );
REQUIRE( target_3.type->bodytype == "blob" );
CHECK( dude.evaluate_techniques( target_3, dude.used_weapon() ).size() == 0 );
}

SECTION( "Test stun" ) {
const matec_id &tec = *test_style_ma1->techniques.find( test_stun );
monster &target_1 = spawn_test_monster( "mon_feral_human_pipe", target_1_pos );
monster &target_2 = spawn_test_monster( "mon_zombie_fat", target_2_pos );
monster &target_3 = spawn_test_monster( "mon_blob", target_3_pos );
item weap( itype_2_by_sword );
dude.wield( weap );
// test stunning a feral (succeed)
std::vector<matec_id> tech_1 = dude.evaluate_techniques( target_1, dude.used_weapon() );
REQUIRE( target_1.get_size() == 3 );
REQUIRE( !target_1.type->in_species( species_ZOMBIE ) );
CHECK( std::find( tech_1.begin(), tech_1.end(), tec ) != tech_1.end() );
// Being downed disables the attack
target_1.add_effect( effect_stunned, 1_days );
CHECK( dude.evaluate_techniques( target_1, dude.used_weapon() ).size() == 0 );
// test stunning a zombie (fail)
REQUIRE( target_2.get_size() == 3 );
REQUIRE( target_2.type->in_species( species_ZOMBIE ) );
CHECK( dude.evaluate_techniques( target_2, dude.used_weapon() ).size() == 0 );
// test stunning a slime (fail)
REQUIRE( target_3.get_size() == 3 );
REQUIRE( target_3.type->in_species( species_SLIME ) );
CHECK( dude.evaluate_techniques( target_3, dude.used_weapon() ).size() == 0 );
}
SECTION( "Test knockback" ) {
const matec_id &tec = *test_style_ma1->techniques.find( test_knockback );
monster &target_1 = spawn_test_monster( "mon_feral_human_pipe", target_1_pos );
monster &target_2 = spawn_test_monster( "mon_zombie_hulk", target_2_pos );
monster &target_3 = spawn_test_monster( "mon_test_tech_grabber", target_3_pos );
item weap( itype_club );
dude.wield( weap );
// test throwing a feral (succeed)
std::vector<matec_id> tech_1 = dude.evaluate_techniques( target_1, dude.used_weapon() );
REQUIRE( target_1.get_size() == 3 );
CHECK( std::find( tech_1.begin(), tech_1.end(), tec ) != tech_1.end() );
// Being downed disables the attack
target_1.add_effect( effect_downed, 1_days );
CHECK( dude.evaluate_techniques( target_1, dude.used_weapon() ).size() == 0 );
// test throwing a large target (fail)
REQUIRE( target_2.get_size() == 5 );
CHECK( dude.evaluate_techniques( target_2, dude.used_weapon() ).size() == 0 );
// test throwing a monster grabbing you (succeed)
dude.add_effect( effect_grabbed, 1_days );
target_3.add_effect( effect_grabbing, 1_days );
REQUIRE( target_3.get_size() == 4 );
REQUIRE( target_3.get_grab_strength() == 0 );
CHECK( dude.evaluate_techniques( target_3, dude.used_weapon() ).size() == 1 );
}
}

0 comments on commit 0038d56

Please sign in to comment.