Skip to content

Commit

Permalink
Convert iuse_actor.cpp actions to new system
Browse files Browse the repository at this point in the history
They now return charges to be used and energy to be used as appropriate.

Updates partially some of the jsons that use batteries as ammo
  • Loading branch information
KheirFerrum committed Aug 31, 2024
1 parent 6ee3e55 commit 13a13e6
Show file tree
Hide file tree
Showing 22 changed files with 412 additions and 536 deletions.
9 changes: 3 additions & 6 deletions data/json/items/armor/power_armor.json
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,7 @@
"warmth": 20,
"material_thickness": 7,
"environmental_protection": 3,
"max_charges": 500,
"ammo": "battery",
"max_power": "500 kJ",
"relic_data": {
"passive_effects": [
{
Expand Down Expand Up @@ -628,8 +627,7 @@
"storage": "7 L",
"warmth": 10,
"material_thickness": 8,
"max_charges": 500,
"ammo": "battery",
"max_power": "500 kJ",
"relic_data": {
"passive_effects": [
{
Expand Down Expand Up @@ -696,8 +694,7 @@
"storage": "15 L",
"warmth": 10,
"material_thickness": 8,
"max_charges": 500,
"ammo": "battery",
"max_charges": "500 kJ",
"relic_data": {
"passive_effects": [
{
Expand Down
2 changes: 1 addition & 1 deletion data/json/items/biosignatures.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"scent_typeid": "sc_fetid",
"moves": 200,
"duration": "1 h",
"charges_to_use": 1,
"cost": 1,
"effects": [ { "id": "fetid_goop", "duration": 3600 } ]
}
}
Expand Down
46 changes: 16 additions & 30 deletions data/json/items/generic/toys_and_sports.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,16 @@
"material": "plastic",
"symbol": "|",
"color": "pink",
"ammo": "battery",
"charges_per_use": 1,
"turns_per_charge": 20,
"power_draw": "1 kJ",
"use_action": "DOLLCHAT",
"magazines": [
[
"battery",
[
"light_minus_disposable_cell",
"light_disposable_cell",
"light_minus_battery_cell",
"light_battery_cell",
"light_plus_battery_cell",
"light_atomic_battery_cell",
"light_minus_atomic_battery_cell"
]
]
"light_minus_disposable_cell",
"light_disposable_cell",
"light_minus_battery_cell",
"light_battery_cell",
"light_plus_battery_cell",
"light_atomic_battery_cell",
"light_minus_atomic_battery_cell"
],
"magazine_well": "250 ml"
},
Expand All @@ -45,23 +38,16 @@
"material": "plastic",
"symbol": "|",
"color": "pink",
"ammo": "battery",
"charges_per_use": 1,
"turns_per_charge": 20,
"power_draw": "1 kJ",
"use_action": "DOLLCHAT",
"magazines": [
[
"battery",
[
"light_minus_disposable_cell",
"light_disposable_cell",
"light_minus_battery_cell",
"light_battery_cell",
"light_plus_battery_cell",
"light_atomic_battery_cell",
"light_minus_atomic_battery_cell"
]
]
"light_minus_disposable_cell",
"light_disposable_cell",
"light_minus_battery_cell",
"light_battery_cell",
"light_plus_battery_cell",
"light_atomic_battery_cell",
"light_minus_atomic_battery_cell"
],
"magazine_well": "250 ml"
}
Expand Down
9 changes: 3 additions & 6 deletions data/json/items/items_holiday.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
"symbol": "0",
"color": "yellow",
"looks_like": "pumpkin",
"initial_charges": 100,
"max_charges": 100,
"ammo": [ "battery" ],
"max_power": "100 kJ",
"use_action": {
"target": "plastic_jack_o_lantern_lit",
"msg": "You light the candle in the jack o'lantern.",
"active": true,
"transform_charges": 1,
"cost": 1,
"menu_text": "Light",
"type": "transform"
}
Expand All @@ -39,8 +37,7 @@
"symbol": "0",
"looks_like": "pumpkin",
"color": "yellow",
"initial_charges": 100,
"max_charges": 100,
"max_power": "100 kJ",
"charges_per_use": 1,
"turns_per_charge": 1350,
"use_action": {
Expand Down
42 changes: 12 additions & 30 deletions data/json/items/melee/bludgeons.json
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@
"material": [ "superalloy", "steel" ],
"symbol": "/",
"color": "light_gray",
"ammo": "battery",
"charges_per_use": 1,
"power_draw": "1 kJ",
"techniques": [ "WBLOCK_1", "RAPID" ],
"use_action": {
"target": "l-stick_on",
Expand All @@ -437,13 +436,8 @@
"type": "transform"
},
"flags": [ "DURABLE_MELEE", "SHEATH_SPEAR" ],
"magazines": [
[
"battery",
[ "medium_plus_battery_cell", "medium_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ]
]
],
"magazine_well": "500 ml"
"magazines": [ "medium_plus_battery_cell", "medium_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ],
"battery_well": "500 ml"
},
{
"id": "l-stick_on",
Expand All @@ -456,7 +450,7 @@
"power_draw": 10000,
"use_action": { "target": "l-stick", "msg": "The l-stick(tm)'s light fades away.", "menu_text": "Turn off", "type": "transform" },
"flags": [ "LIGHT_300", "DURABLE_MELEE", "TRADER_AVOID", "SHEATH_SPEAR" ],
"magazine_well": "500 ml"
"battery_well": "500 ml"
},
{
"id": "lucern_hammer",
Expand Down Expand Up @@ -750,7 +744,6 @@
"volume": "1750 ml",
"price": "80 USD",
"price_postapoc": "45 USD",
"ammo": "battery",
"use_action": {
"type": "transform",
"msg": "You press a button and the staff crackles with electricity.",
Expand All @@ -759,13 +752,8 @@
"need_charges_msg": "The %s's batteries are dead.",
"target": "shock_staff_on"
},
"magazines": [
[
"battery",
[ "medium_battery_cell", "medium_plus_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ]
]
],
"magazine_well": "500 ml"
"magazines": [ "medium_battery_cell", "medium_plus_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ],
"battery_well": "500 ml"
},
{
"id": "shock_staff_on",
Expand All @@ -777,8 +765,8 @@
"revert_msg": "The %s's batteries run dry.",
"use_action": { "type": "transform", "msg": "You press a button and the staff turns off.", "target": "shock_staff" },
"extend": { "flags": [ "TRADER_AVOID", "SHOCKING" ] },
"power_draw": 5000000,
"magazine_well": "500 ml"
"power_draw": "5 kJ",
"battery_well": "500 ml"
},
{
"id": "rock_sock",
Expand Down Expand Up @@ -883,7 +871,6 @@
"price": "170 USD",
"price_postapoc": "25 USD",
"material": [ "plastic", "steel" ],
"ammo": "battery",
"use_action": {
"type": "transform",
"msg": "You press a button and the tonfa crackles with electricity.",
Expand All @@ -893,13 +880,8 @@
"target": "shocktonfa_on"
},
"flags": [ "DURABLE_MELEE", "NONCONDUCTIVE", "BELT_CLIP" ],
"magazines": [
[
"battery",
[ "medium_battery_cell", "medium_plus_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ]
]
],
"magazine_well": "500 ml"
"magazines": [ "medium_battery_cell", "medium_plus_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ],
"battery_well": "500 ml"
},
{
"id": "shocktonfa_on",
Expand All @@ -911,8 +893,8 @@
"revert_msg": "The %s's batteries run dry.",
"use_action": { "type": "transform", "msg": "You press a button and the tonfa turns off.", "target": "shocktonfa_off" },
"extend": { "flags": [ "TRADER_AVOID", "SHOCKING" ] },
"power_draw": 5000000,
"magazine_well": "500 ml"
"power_draw": "5 kJ",
"battery_well": "500 ml"
},
{
"id": "warhammer",
Expand Down
10 changes: 2 additions & 8 deletions data/json/items/melee/misc.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,9 @@
"material": [ "plastic", "aluminum" ],
"symbol": ";",
"color": "dark_gray",
"ammo": "battery",
"charges_per_use": 100,
"power_draw": "100 kJ",
"use_action": "TAZER",
"magazines": [
[
"battery",
[ "medium_battery_cell", "medium_plus_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ]
]
],
"magazines": [ "medium_battery_cell", "medium_plus_battery_cell", "medium_atomic_battery_cell", "medium_disposable_cell" ],
"magazine_well": "500 ml",
"flags": [ "BELT_CLIP" ]
}
Expand Down
7 changes: 4 additions & 3 deletions src/catalua_iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ void lua_iuse_actor::load( const JsonObject & )
// TODO: custom data
}

int lua_iuse_actor::use( player &who, item &itm, bool tick, const tripoint &pos ) const
std::pair<int, units::energy> lua_iuse_actor::use( player &who, item &itm, bool tick,
const tripoint &pos ) const
{
if( !tick ) {
try {
sol::protected_function_result res = luafunc( who.as_character(), itm, pos );
check_func_result( res );
int ret = res;
std::pair<int, units::energy> ret = res;
return ret;
} catch( std::runtime_error &e ) {
debugmsg( "Failed to run iuse_function k='%s': %s", type, e.what() );
}
} else {
// TODO: ticking use
}
return 1;
return std::make_pair( 1, 0_J );
}

ret_val<bool> lua_iuse_actor::can_use( const Character &, const item &, bool,
Expand Down
3 changes: 2 additions & 1 deletion src/catalua_iuse_actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class lua_iuse_actor : public iuse_actor
lua_iuse_actor( const std::string &type, sol::protected_function &&luafunc );
~lua_iuse_actor() override;
void load( const JsonObject &obj ) override;
int use( player &who, item &itm, bool tick, const tripoint &pos ) const override;
std::pair<int, units::energy> use( player &who, item &itm, bool tick,
const tripoint &pos ) const override;
ret_val<bool> can_use( const Character &, const item &, bool, const tripoint & ) const override;
std::unique_ptr<iuse_actor> clone() const override;
};
Expand Down
7 changes: 4 additions & 3 deletions src/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7756,14 +7756,15 @@ bool Character::invoke_item( item *used, const std::string &method, const tripoi
return false;
}

int charges_used = actually_used->type->invoke( *this->as_player(), *actually_used, pt, method );
if( charges_used == 0 ) {
auto[chrg, enrg] = actually_used->type->invoke( *this->as_player(), *actually_used, pt, method );
if( chrg == 0 && enrg == 0_J ) {
return false;
}
// Prevent accessing the item as it may have been deleted by the invoked iuse function.

if( used->is_tool() || used->is_medication() || used->get_contained().is_medication() ) {
return consume_charges( *actually_used, charges_used );
used->energy_consume( enrg, pt ) == enrg;
return( consume_charges( *actually_used, chrg ) );
} else if( used->is_bionic() || used->is_deployable() || method == "place_trap" ) {
used->detach();
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ bool Character::eat( item &food, bool force )
int charges_used = 0;
if( food.type->has_use() ) {
if( !food.type->can_use( "PETFOOD" ) ) {
charges_used = food.type->invoke( *this->as_player(), food, pos() );
charges_used = food.type->invoke( *this->as_player(), food, pos() ).first;
if( charges_used <= 0 ) {
return false;
}
Expand Down Expand Up @@ -1631,7 +1631,7 @@ bool Character::consume_med( item &target )

int amount_used = 1;
if( target.type->has_use() ) {
amount_used = target.type->invoke( *this->as_player(), target, pos() );
amount_used = target.type->invoke( *this->as_player(), target, pos() ).first;
if( amount_used <= 0 ) {
return false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2935,8 +2935,9 @@ void iexamine::fireplace( player &p, const tripoint &examp )
p.add_msg_if_player( _( "You attempt to start a fire with your %s…" ), it->tname() );
const ret_val<bool> can_use = actor->can_use( p, *it, false, examp );
if( can_use.success() ) {
const int charges = actor->use( p, *it, false, examp );
p.use_charges( it->typeId(), charges );
const auto [chrg, enrg] = actor->use( p, *it, false, examp );
it->energy_consume( enrg, examp );
p.use_charges( it->typeId(), chrg );
return;
} else {
p.add_msg_if_player( m_bad, can_use.str() );
Expand Down
18 changes: 3 additions & 15 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7741,7 +7741,7 @@ units::energy item::energy_required() const
return 0_J;
}

bool item::energy_sufficient( units::energy p_needed ) const
bool item::energy_sufficient( const Character &ch, units::energy p_needed ) const
{
return energy_remaining() >= p_needed;
}
Expand Down Expand Up @@ -8456,18 +8456,6 @@ int item::units_remaining( const Character &ch, int limit ) const
}

int res = ammo_remaining();
if( res < limit && is_power_armor() ) {
if( character_funcs::can_interface_armor( ch ) && has_flag( flag_USE_UPS ) ) {
res += std::max( ch.charges_of( itype_UPS, limit - res ), ch.charges_of( itype_bio_armor,
limit - res ) );
} else if( character_funcs::can_interface_armor( ch ) ) {
res += ch.charges_of( itype_bio_armor, limit - res );
} else {
res += ch.charges_of( itype_UPS, limit - res );
}
} else if( res < limit && has_flag( flag_USE_UPS ) ) {
res += ch.charges_of( itype_UPS, limit - res );
}

return std::min( static_cast<int>( res ), limit );
}
Expand Down Expand Up @@ -10151,7 +10139,7 @@ detached_ptr<item> item::process_internal( detached_ptr<item> &&self, player *ca

avatar &you = get_avatar();
if( activate ) {
if( self->type->invoke( carrier != nullptr ? *carrier : you, *self, pos ) > 0 ) {
if( self->type->invoke( carrier != nullptr ? *carrier : you, *self, pos ).first > 0 ) {
return detached_ptr<item>();
}
return std::move( self );
Expand Down Expand Up @@ -10529,7 +10517,7 @@ bool item::on_drop( const tripoint &pos, map &m )
}
avatar &you = get_avatar();
you.flag_encumbrance();
return type->drop_action && type->drop_action.call( you, *this, false, pos );
return type->drop_action && type->drop_action.call( you, *this, false, pos ).first;
}

time_duration item::age() const
Expand Down
2 changes: 1 addition & 1 deletion src/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ class item : public location_visitable<item>, public game_object<item>
units::energy energy_required() const;

/** Check that item has sufficient energy */
bool energy_sufficient( units::energy p_needed ) const;
bool energy_sufficient( const Character &ch, units::energy p_needed = 0_J ) const;

/**
* Consume power(if available) and return the amount of power that was consumed
Expand Down
Loading

0 comments on commit 13a13e6

Please sign in to comment.