Skip to content

Commit

Permalink
Merge pull request #34134 from ZhilkinSerg/iuse-need-wielding
Browse files Browse the repository at this point in the history
 Make possible to define `item_transform` and `cast_spell` item actions which are only allowed when wielding or wearing item
  • Loading branch information
KorGgenT committed Sep 21, 2019
2 parents d729c0d + 0ad13da commit 5f6978f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
36 changes: 18 additions & 18 deletions data/mods/Magiclysm/enchanted/enchanted_wands.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"plural": "minor wands of magic missile",
"copy-from": "spell_wand",
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts magic missile.",
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 5 }
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 5, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -33,7 +33,7 @@
"copy-from": "spell_wand",
"price": 20000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts magic missile.",
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 10 }
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 10, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -43,7 +43,7 @@
"copy-from": "spell_wand",
"price": 30000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts magic missile.",
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 15 }
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 15, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -52,7 +52,7 @@
"plural": "minor wands of fireball",
"copy-from": "spell_wand",
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts fireball.",
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 5 }
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 5, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -62,7 +62,7 @@
"copy-from": "spell_wand",
"price": 20000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts fireball.",
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 10 }
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 10, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -72,7 +72,7 @@
"copy-from": "spell_wand",
"price": 30000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts fireball.",
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 15 }
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 15, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -81,7 +81,7 @@
"plural": "minor wands of mana beam",
"copy-from": "spell_wand",
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts mana beam.",
"use_action": { "type": "cast_spell", "spell_id": "magus_mana_beam", "no_fail": true, "level": 5 }
"use_action": { "type": "cast_spell", "spell_id": "magus_mana_beam", "no_fail": true, "level": 5, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -91,7 +91,7 @@
"copy-from": "spell_wand",
"price": 20000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts mana beam.",
"use_action": { "type": "cast_spell", "spell_id": "magus_mana_beam", "no_fail": true, "level": 10 }
"use_action": { "type": "cast_spell", "spell_id": "magus_mana_beam", "no_fail": true, "level": 10, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -101,7 +101,7 @@
"copy-from": "spell_wand",
"price": 30000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts mana beam.",
"use_action": { "type": "cast_spell", "spell_id": "magus_mana_beam", "no_fail": true, "level": 15 }
"use_action": { "type": "cast_spell", "spell_id": "magus_mana_beam", "no_fail": true, "level": 15, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -110,7 +110,7 @@
"plural": "minor wands of point flare",
"copy-from": "spell_wand",
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts point flare.",
"use_action": { "type": "cast_spell", "spell_id": "point_flare", "no_fail": true, "level": 5 }
"use_action": { "type": "cast_spell", "spell_id": "point_flare", "no_fail": true, "level": 5, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -120,7 +120,7 @@
"copy-from": "spell_wand",
"price": 20000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts point flare.",
"use_action": { "type": "cast_spell", "spell_id": "point_flare", "no_fail": true, "level": 10 }
"use_action": { "type": "cast_spell", "spell_id": "point_flare", "no_fail": true, "level": 10, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -130,7 +130,7 @@
"copy-from": "spell_wand",
"price": 30000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts point flare.",
"use_action": { "type": "cast_spell", "spell_id": "point_flare", "no_fail": true, "level": 15 }
"use_action": { "type": "cast_spell", "spell_id": "point_flare", "no_fail": true, "level": 15, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -139,7 +139,7 @@
"plural": "minor wands of iceball",
"copy-from": "spell_wand",
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts iceball.",
"use_action": { "type": "cast_spell", "spell_id": "iceball", "no_fail": true, "level": 5 }
"use_action": { "type": "cast_spell", "spell_id": "iceball", "no_fail": true, "level": 5, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -149,7 +149,7 @@
"copy-from": "spell_wand",
"price": 20000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts iceball.",
"use_action": { "type": "cast_spell", "spell_id": "iceball", "no_fail": true, "level": 10 }
"use_action": { "type": "cast_spell", "spell_id": "iceball", "no_fail": true, "level": 10, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -159,7 +159,7 @@
"copy-from": "spell_wand",
"price": 30000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts iceball.",
"use_action": { "type": "cast_spell", "spell_id": "iceball", "no_fail": true, "level": 15 }
"use_action": { "type": "cast_spell", "spell_id": "iceball", "no_fail": true, "level": 15, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -168,7 +168,7 @@
"plural": "minor wands of cone of cold",
"copy-from": "spell_wand",
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts cone of cold.",
"use_action": { "type": "cast_spell", "spell_id": "cone_cold", "no_fail": true, "level": 5 }
"use_action": { "type": "cast_spell", "spell_id": "cone_cold", "no_fail": true, "level": 5, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -178,7 +178,7 @@
"copy-from": "spell_wand",
"price": 20000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts cone of cold.",
"use_action": { "type": "cast_spell", "spell_id": "cone_cold", "no_fail": true, "level": 10 }
"use_action": { "type": "cast_spell", "spell_id": "cone_cold", "no_fail": true, "level": 10, "need_wielding": true }
},
{
"type": "TOOL",
Expand All @@ -188,6 +188,6 @@
"copy-from": "spell_wand",
"price": 30000,
"description": "A slender wooden wand with a mana crystal socket at the base that casts a spell when activated. This wand casts cone of cold.",
"use_action": { "type": "cast_spell", "spell_id": "cone_cold", "no_fail": true, "level": 15 }
"use_action": { "type": "cast_spell", "spell_id": "cone_cold", "no_fail": true, "level": 15, "need_wielding": true }
}
]
4 changes: 2 additions & 2 deletions data/mods/Magiclysm/items/obsolete.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"color": "brown",
"charges_per_use": 1,
"ammo": "crystallized_mana",
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 5 },
"use_action": { "type": "cast_spell", "spell_id": "fireball", "no_fail": true, "level": 5, "need_wielding": true },
"magazines": [ [ "crystallized_mana", [ "small_mana_crystal" ] ] ],
"magazine_well": 1
},
Expand All @@ -35,7 +35,7 @@
"color": "brown",
"charges_per_use": 1,
"ammo": "crystallized_mana",
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 10 },
"use_action": { "type": "cast_spell", "spell_id": "magic_missile", "no_fail": true, "level": 10, "need_wielding": true },
"magazines": [ [ "crystallized_mana", [ "small_mana_crystal" ] ] ],
"magazine_well": 1
}
Expand Down
26 changes: 21 additions & 5 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ void iuse_transform::load( JsonObject &obj )
}

obj.read( "need_worn", need_worn );
obj.read( "need_wielding", need_wielding );

obj.read( "qualities_needed", qualities_needed );

Expand All @@ -190,6 +191,10 @@ int iuse_transform::use( player &p, item &it, bool t, const tripoint &pos ) cons
p.add_msg_if_player( m_info, _( "You need to wear the %1$s before activating it." ), it.tname() );
return 0;
}
if( possess && need_wielding && !p.is_wielding( it ) ) {
p.add_msg_if_player( m_info, _( "You need to wield the %1$s before activating it." ), it.tname() );
return 0;
}
if( need_charges && it.units_remaining( p ) < need_charges ) {
if( possess ) {
p.add_msg_if_player( m_info, need_charges_msg, it.tname() );
Expand Down Expand Up @@ -513,7 +518,7 @@ void unfold_vehicle_iuse::load( JsonObject &obj )
obj.read( "tools_needed", tools_needed );
}

int unfold_vehicle_iuse::use( player &p, item &it, bool /*t*/, const tripoint &/*pos*/ ) const
int unfold_vehicle_iuse::use( player &p, item &it, bool, const tripoint & ) const
{
if( p.is_underwater() ) {
p.add_msg_if_player( m_info, _( "You can't do that while underwater." ) );
Expand Down Expand Up @@ -771,7 +776,7 @@ void place_monster_iuse::load( JsonObject &obj )
skill2 = skill_id( obj.get_string( "skill2", skill2.str() ) );
}

int place_monster_iuse::use( player &p, item &it, bool, const tripoint &/*pos*/ ) const
int place_monster_iuse::use( player &p, item &it, bool, const tripoint & ) const
{
monster newmon( mtypeid );
tripoint target;
Expand Down Expand Up @@ -2394,6 +2399,8 @@ void cast_spell_actor::load( JsonObject &obj )
no_fail = obj.get_bool( "no_fail" );
item_spell = spell_id( obj.get_string( "spell_id" ) );
spell_level = obj.get_int( "level" );
need_worn = obj.get_bool( "need_worn", false );
need_wielding = obj.get_bool( "need_wielding", false );
}

void cast_spell_actor::info( const item &, std::vector<iteminfo> &dump ) const
Expand All @@ -2406,10 +2413,19 @@ void cast_spell_actor::info( const item &, std::vector<iteminfo> &dump ) const
}
}

int cast_spell_actor::use( player &p, item &itm, bool, const tripoint & ) const
int cast_spell_actor::use( player &p, item &it, bool, const tripoint & ) const
{
if( need_worn && !p.is_worn( it ) ) {
p.add_msg_if_player( m_info, _( "You need to wear the %1$s before activating it." ), it.tname() );
return 0;
}
if( need_wielding && !p.is_wielding( it ) ) {
p.add_msg_if_player( m_info, _( "You need to wield the %1$s before activating it." ), it.tname() );
return 0;
}

spell casting = spell( spell_id( item_spell ) );
int charges = itm.type->charges_to_use();
int charges = it.type->charges_to_use();

player_activity cast_spell( activity_id( "ACT_SPELLCASTING" ), casting.casting_time( p ) );
// [0] this is used as a spell level override for items casting spells
Expand All @@ -2422,7 +2438,7 @@ int cast_spell_actor::use( player &p, item &itm, bool, const tripoint & ) const
cast_spell.values.emplace_back( 0 );
}
cast_spell.name = casting.id().c_str();
if( itm.has_flag( "USE_PLAYER_ENERGY" ) ) {
if( it.has_flag( "USE_PLAYER_ENERGY" ) ) {
// [2] this value overrides the mana cost if set to 0
cast_spell.values.emplace_back( 1 );
charges = 0;
Expand Down
11 changes: 9 additions & 2 deletions src/iuse_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ class iuse_transform : public iuse_actor
/**does the item requires to be worn to be activable*/
bool need_worn = false;

/**does the item requires to be wielded to be activable*/
bool need_wielding = false;

/** subtracted from @ref Creature::moves when transformation is successful */
int moves = 0;

Expand Down Expand Up @@ -694,12 +697,16 @@ class cast_spell_actor : public iuse_actor
// the spell this item casts when used.
spell_id item_spell;
int spell_level;
/**does the item requires to be worn to be activable*/
bool need_worn = false;
/**does the item requires to be wielded to be activable*/
bool need_wielding = false;

cast_spell_actor( const std::string &type = "cast_spell" ) : iuse_actor( type ) {}

~cast_spell_actor() override = default;
void load( JsonObject &obj ) override;
int use( player &p, item &itm, bool, const tripoint & ) const override;
int use( player &p, item &it, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
void info( const item &, std::vector<iteminfo> & ) const override;
};
Expand Down Expand Up @@ -1096,7 +1103,7 @@ class weigh_self_actor : public iuse_actor

~weigh_self_actor() override = default;
void load( JsonObject &jo ) override;
int use( player &p, item &itm, bool, const tripoint & ) const override;
int use( player &p, item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
void info( const item &, std::vector<iteminfo> & ) const override;
};
Expand Down

0 comments on commit 5f6978f

Please sign in to comment.