Skip to content

Commit

Permalink
Create talker item
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramza13 committed Jul 23, 2021
1 parent af75eb4 commit 686ae21
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 4 deletions.
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 @@ -959,3 +960,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
7 changes: 4 additions & 3 deletions src/iuse_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4549,7 +4549,7 @@ void effect_on_conditons_actor::info( const item &, std::vector<iteminfo> &dump
}

cata::optional<int> effect_on_conditons_actor::use( player &p, item &it, bool,
const tripoint & ) const
const tripoint &t ) const
{
dialogue d;
standard_npc default_npc( "Default" );
Expand All @@ -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 686ae21

Please sign in to comment.