Skip to content

Commit

Permalink
Allow NPCs to read E-books (CleverRaven#65935)
Browse files Browse the repository at this point in the history
* Allow NPCs to read E-books

From now on NPCs are able to read e-books from suitable devices.

Device needs to be not broken, have some e-books in the memory, have at least 1 charge. Furthermore proper lighting conditions are required (NPCs do not turn on flashlight or screen  as player does).

* Update data/json/npcs/common_chat/TALK_COMMON_ALLY.json

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/game_inventory.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/npc.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/npc.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/npc.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/npctalk.h

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Update src/npctalk_funcs.cpp

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Fix Basic Build crash (unused variable) and fix battery charge requirement listed

* Another fix for basic build error

* Leftover commented-out code clean up (review follow-up)

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
Tread4 and github-actions[bot] authored Jun 12, 2023
1 parent 289ab6d commit f424b9c
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 4 deletions.
6 changes: 6 additions & 0 deletions data/json/npcs/common_chat/TALK_COMMON_ALLY.json
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,12 @@
"condition": { "and": [ { "not": "npc_has_activity" }, { "not": { "npc_has_trait": "HALLUCINATION" } } ] },
"effect": "do_read"
},
{
"text": "Please study from an e-book.",
"topic": "TALK_DONE",
"condition": { "and": [ { "not": "npc_has_activity" }, { "not": { "npc_has_trait": "HALLUCINATION" } } ] },
"effect": "do_eread"
},
{
"text": "Please start deconstructing any vehicles in a deconstruction zone.",
"topic": "TALK_DONE",
Expand Down
52 changes: 52 additions & 0 deletions src/game_inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,58 @@ item_location game_menus::inv::gun_to_modify( Character &you, const item &gunmod
_( "You don't have any guns to modify." ) );
}

class ereader_inventory_preset : public pickup_inventory_preset
{
public:
explicit ereader_inventory_preset( const Character &you ) : pickup_inventory_preset( you ),
you( you ) {
_collate_entries = true;
if( get_option<bool>( "INV_USE_ACTION_NAMES" ) ) {
append_cell( [ this ]( const item_location & loc ) {
return string_format( "<color_light_green>%s</color>", get_action_name( *loc ) );
}, _( "ACTION" ) );
}
}

bool is_shown( const item_location &loc ) const override {
return loc->is_ebook_storage();
}

std::string get_denial( const item_location &loc ) const override {
const item &it = *loc;

if( it.is_broken() ) {
return _( "E-reader is broken and won't turn on." );
}

if( !it.ammo_sufficient( &you, "EBOOKREAD" ) ) {
return string_format(
n_gettext( "Needs at least %d charge.",
"Needs at least %d charges.", loc->ammo_required() ),
loc->ammo_required() );
}

if( !it.has_flag( flag_ALLOWS_REMOTE_USE ) ) {
return pickup_inventory_preset::get_denial( loc );
}

return std::string();
}

protected:
std::string get_action_name( const item &it ) const {
return string_format( "Read on %s.", it.tname() );
}
private:
const Character &you;
};

item_location game_menus::inv::ereader_to_use( Character &you )
{
const std::string msg = _( "You don't have any e-readers you can use." );
return inv_internal( you, ereader_inventory_preset( you ), _( "Select e-reader." ), 1, msg );
}

class read_inventory_preset: public pickup_inventory_preset
{
public:
Expand Down
2 changes: 2 additions & 0 deletions src/game_inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ item_location disassemble( Character &you );
item_location gun_to_modify( Character &you, const item &gunmod );
/** Book reading menu. */
item_location read( Character &you );
/** E-Book reading menu. */
item_location ereader_to_use( Character &you );
/** eBook reading menu. */
item_location ebookread( Character &you, item_location &ereader );
/** Menu for stealing stuff. */
Expand Down
18 changes: 15 additions & 3 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,15 +1354,28 @@ time_duration npc::time_to_read( const item &book, const Character &reader ) con
return retval;
}

void npc::do_npc_read()
void npc::do_npc_read( bool ebook )
{
// Can read items from inventory or within one tile (including in vehicles)
Character *npc_player = as_character();
if( !npc_player ) {
return;
}

item_location book = game_menus::inv::read( *npc_player );
item_location book;
item_location ereader;

if( !ebook ) {
book = game_menus::inv::read( *npc_player );
} else {
ereader = game_menus::inv::ereader_to_use( *npc_player );
if( !ereader ) {
add_msg( _( "Never mind." ) );
return;
}
book = game_menus::inv::ebookread( *npc_player, ereader );
}

if( !book ) {
add_msg( _( "Never mind." ) );
return;
Expand All @@ -1380,7 +1393,6 @@ void npc::do_npc_read()

// NPCs can't read to other NPCs yet
const time_duration time_taken = time_to_read( *book, *this );
item_location ereader = {};

// NPCs read until they gain a level
read_activity_actor actor( time_taken, book, ereader, true, getID().get_value() );
Expand Down
2 changes: 1 addition & 1 deletion src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ class npc : public Character
bool wear_if_wanted( const item &it, std::string &reason );
bool can_read( const item &book, std::vector<std::string> &fail_reasons );
time_duration time_to_read( const item &book, const Character &reader ) const;
void do_npc_read();
void do_npc_read( bool ebook = false );
void stow_item( item &it );
bool wield( item &it ) override;
void drop( const drop_locations &what, const tripoint &target,
Expand Down
1 change: 1 addition & 0 deletions src/npctalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,7 @@ void talk_effect_t::parse_string_effect( const std::string &effect_id, const Jso
WRAP( do_mining ),
WRAP( do_mopping ),
WRAP( do_read ),
WRAP( do_eread ),
WRAP( do_butcher ),
WRAP( do_farming ),
WRAP( assign_guard ),
Expand Down
1 change: 1 addition & 0 deletions src/npctalk.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void do_construction( npc & );
void do_mining( npc & );
void do_mopping( npc & );
void do_read( npc & );
void do_eread( npc & );
void do_chop_plank( npc & );
void do_vehicle_deconstruct( npc & );
void do_vehicle_repair( npc & );
Expand Down
5 changes: 5 additions & 0 deletions src/npctalk_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ void talk_function::do_read( npc &p )
p.do_npc_read();
}

void talk_function::do_eread( npc &p )
{
p.do_npc_read( true );
}

void talk_function::dismount( npc &p )
{
p.npc_dismount();
Expand Down

0 comments on commit f424b9c

Please sign in to comment.