Skip to content

Commit

Permalink
Merge pull request #49950 from Ramza13/talker_item
Browse files Browse the repository at this point in the history
Create talker item, paving way for greater item json interactions.
  • Loading branch information
kevingranade authored Jul 31, 2021
2 parents a287fad + 8f36b1a commit a217565
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/JSON_INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -2641,6 +2641,7 @@ CBMs can be defined like this:
"fatigue_mod": 3, // How much fatigue this comestible removes. (Negative values add fatigue)
"radiation": 8, // How much radiation you get from this comestible.
"comestible_type" : "MED", // Comestible type, used for inventory sorting
"consumption_effect_on_conditions" : [ "EOC_1" ], // Effect on conditions to run after consuming. Inline or string id supported
"quench" : 0, // Thirst quenched
"healthy" : -2, // Health effects (used for sickness chances)
"addiction_potential" : 80, // Ability to cause addictions
Expand Down
27 changes: 27 additions & 0 deletions src/consumption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,19 @@ bool Character::consume_effects( item &food )
if( has_effect( effect_tapeworm ) ) {
ingested.nutr /= 2;
}
dialogue d;
standard_npc default_npc( "Default" );
if( avatar *u = as_avatar() ) {
d.alpha = get_talker_for( u );
} else if( npc *n = as_npc() ) {
d.alpha = get_talker_for( n );
}
item_location loc( *( as_character() ), &food );
d.beta = get_talker_for( loc );

for( const effect_on_condition_id &eoc : comest.consumption_eocs ) {
eoc->activate( d );
}

// GET IN MAH BELLY!
stomach.ingest( ingested );
Expand Down Expand Up @@ -1730,6 +1743,20 @@ static bool consume_med( item &target, player &you )
// Take by mouth
you.consume_effects( target );
}
dialogue d;
standard_npc default_npc( "Default" );
if( avatar *u = you.as_avatar() ) {
d.alpha = get_talker_for( u );
} else if( npc *n = you.as_npc() ) {
d.alpha = get_talker_for( n );
}
item_location loc( *( you.as_character() ), &target );
d.beta = get_talker_for( loc );

const auto &comest = *target.get_comestible();
for( const effect_on_condition_id &eoc : comest.consumption_eocs ) {
eoc->activate( d );
}

target.mod_charges( -amount_used );
return true;
Expand Down
5 changes: 5 additions & 0 deletions src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "color.h"
#include "damage.h"
#include "debug.h"
#include "effect_on_condition.h"
#include "enum_conversions.h"
#include "enums.h"
#include "explosion.h"
Expand Down Expand Up @@ -2226,6 +2227,10 @@ void Item_factory::load( islot_comestible &slot, const JsonObject &jo, const std
1000;
}

for( JsonValue jv : jo.get_array( "consumption_effect_on_conditions" ) ) {
slot.consumption_eocs.push_back( effect_on_conditions::load_inline_eoc( jv, "" ) );
}

if( jo.has_member( "nutrition" ) && got_calories ) {
jo.throw_error( "cannot specify both nutrition and calories", "nutrition" );
}
Expand Down
11 changes: 11 additions & 0 deletions src/item_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "ret_val.h"
#include "safe_reference.h"
#include "string_formatter.h"
#include "talker_item.h"
#include "translations.h"
#include "units.h"
#include "vehicle.h"
Expand Down Expand Up @@ -960,3 +961,13 @@ bool item_location::protected_from_liquids() const
// none are closed watertight containers
return false;
}

std::unique_ptr<talker> get_talker_for( item_location &it )
{
return std::make_unique<talker_item>( &it );
}
std::unique_ptr<talker> get_talker_for( item_location *it )
{
return std::make_unique<talker_item>( it );
}

5 changes: 4 additions & 1 deletion src/item_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include "units_fwd.h"

class Character;
class character_id;
class JsonIn;
class JsonOut;
class item;
class map_cursor;
class vehicle_cursor;
class talker;
struct tripoint;

/**
Expand Down Expand Up @@ -137,5 +139,6 @@ class item_location

std::shared_ptr<impl> ptr;
};

std::unique_ptr<talker> get_talker_for( item_location &it );
std::unique_ptr<talker> get_talker_for( item_location *it );
#endif // CATA_SRC_ITEM_LOCATION_H
3 changes: 3 additions & 0 deletions src/itype.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ struct islot_comestible {
/** freezing point in degrees celsius, below this temperature item can freeze */
float freeze_point = 0;

/**effect on conditions to apply on consumption*/
std::vector<effect_on_condition_id> consumption_eocs;

/**List of diseases carried by this comestible and their associated probability*/
std::map<diseasetype_id, int> contamination;

Expand Down
5 changes: 3 additions & 2 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4558,8 +4558,9 @@ cata::optional<int> effect_on_conditons_actor::use( player &p, item &it, bool,
} else if( npc *n = p.as_npc() ) {
d.alpha = get_talker_for( n );
}
///TODO make this talker item
d.beta = get_talker_for( default_npc );

item_location loc( *( p.as_character() ), &it );
d.beta = get_talker_for( loc );

for( const effect_on_condition_id &eoc : eocs ) {
eoc->activate( d );
Expand Down
7 changes: 7 additions & 0 deletions src/talker.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class faction;
class item;
class item_location;
class mission;
class npc;
class player;
Expand Down Expand Up @@ -33,6 +34,12 @@ class talker
virtual npc *get_npc() const {
return nullptr;
}
virtual item_location *get_item() {
return nullptr;
}
virtual item_location *get_item() const {
return nullptr;
}

// identity and location
virtual std::string disp_name() const {
Expand Down
56 changes: 56 additions & 0 deletions src/talker_item.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <memory>

#include "character_id.h"
#include "item.h"
#include "magic.h"
#include "npc.h"
#include "pimpl.h"
#include "player.h"
#include "point.h"
#include "talker_item.h"
#include "vehicle.h"

std::string talker_item::disp_name() const
{
return me_it->get_item()->display_name();
}

int talker_item::posx() const
{
return me_it->position().x;
}

int talker_item::posy() const
{
return me_it->position().y;
}

int talker_item::posz() const
{
return me_it->position().z;
}

tripoint talker_item::pos() const
{
return me_it->position();
}

tripoint_abs_omt talker_item::global_omt_location() const
{
return get_player_character().global_omt_location();
}

std::string talker_item::get_value( const std::string &var_name ) const
{
return me_it->get_item()->get_var( var_name );
}

void talker_item::set_value( const std::string &var_name, const std::string &value )
{
me_it->get_item()->set_var( var_name, value );
}

void talker_item::remove_value( const std::string &var_name )
{
me_it->get_item()->erase_var( var_name );
}
54 changes: 54 additions & 0 deletions src/talker_item.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once
#ifndef CATA_SRC_TALKER_ITEM_H
#define CATA_SRC_TALKER_ITEM_H

#include <functional>
#include <iosfwd>
#include <list>
#include <vector>

#include "coordinates.h"
#include "npc.h"
#include "talker.h"
#include "type_id.h"

class item;

struct tripoint;

/*
* Talker wrapper class for Character. well, ideally, but since Character is such a mess,
* it's the wrapper class for player
* Should never be invoked directly. Only talker_avatar and talker_npc are really valid.
*/
class talker_item: public talker
{
public:
explicit talker_item( item_location *new_me ): me_it( new_me ) {
}
~talker_item() override = default;

// underlying element accessor functions
item_location *get_item() override {
return me_it;
}
item_location *get_item() const override {
return me_it;
}
// identity and location
std::string disp_name() const override;
int posx() const override;
int posy() const override;
int posz() const override;
tripoint pos() const override;
tripoint_abs_omt global_omt_location() const override;

std::string get_value( const std::string &var_name ) const override;
void set_value( const std::string &var_name, const std::string &value ) override;
void remove_value( const std::string & ) override;

protected:
talker_item() = default;
item_location *me_it;
};
#endif // CATA_SRC_TALKER_ITEM_H

0 comments on commit a217565

Please sign in to comment.