Skip to content

Commit

Permalink
Refactor and fix ui crash
Browse files Browse the repository at this point in the history
 - Fix crash on closing out of craft selection without selecting a craft to work on
 - Apply copyedits from code review
 - Remove vehicle::use_workbench and merge with iexamine
  • Loading branch information
ifreund committed Apr 13, 2019
1 parent a230842 commit 75fa347
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 105 deletions.
16 changes: 8 additions & 8 deletions src/crafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ static item_location set_item_map_or_vehicle( const player &p, const tripoint &l
item *newit_in_vehicle = &items_at_part[items_at_part.size() - 1];

p.add_msg_player_or_npc(
string_format( pgettext( "item, furniture", "You put the %s on the %s" ),
string_format( pgettext( "item, furniture", "You put the %s on the %s." ),
newit.tname(), vp->part().name() ),
string_format( pgettext( "item, furniture", "<npcname> puts the %s on the %s" ),
string_format( pgettext( "item, furniture", "<npcname> puts the %s on the %s." ),
newit.tname(), vp->part().name() ) );

return item_location( vehicle_cursor( vp->vehicle(), vp->part_index() ), newit_in_vehicle );
Expand All @@ -573,7 +573,7 @@ static item_location set_item_map_or_vehicle( const player &p, const tripoint &l
"Not enough space on the %s. You drop the %s on the ground." ),
vp->part().name(), newit.tname() ),
string_format( pgettext( "furniture, item",
"Not enough space on the %s. <npcname> drops the %s on the ground" ),
"Not enough space on the %s. <npcname> drops the %s on the ground." ),
vp->part().name(), newit.tname() ) );

return item_location( map_cursor( loc ), &g->m.add_item_or_charges( loc, newit ) );
Expand All @@ -582,9 +582,9 @@ static item_location set_item_map_or_vehicle( const player &p, const tripoint &l
if( g->m.has_furn( loc ) ) {
const furn_t &workbench = g->m.furn( loc ).obj();
p.add_msg_player_or_npc(
string_format( pgettext( "item, furniture", "You put the %s on the %s" ),
string_format( pgettext( "item, furniture", "You put the %s on the %s." ),
newit.tname(), workbench.name() ),
string_format( pgettext( "item, furniture", "<npcname> puts the %s on the %s" ),
string_format( pgettext( "item, furniture", "<npcname> puts the %s on the %s." ),
newit.tname(), workbench.name() ) );
}

Expand Down Expand Up @@ -646,7 +646,7 @@ void player::start_craft( const recipe &making, int batch_size, bool is_long, co
craft_in_inventory = set_item_inventory( *this, craft );

if( !has_item( *craft_in_inventory ) ) {
add_msg_if_player( _( "Activate the %s to start crafting" ), craft.tname() );
add_msg_if_player( _( "Activate the %s to start crafting." ), craft.tname() );
return;
}
}
Expand All @@ -662,9 +662,9 @@ void player::start_craft( const recipe &making, int batch_size, bool is_long, co
activity.values.push_back( is_long );

add_msg_player_or_npc(
string_format( pgettext( "in progress craft", "You start working on the %s" ),
string_format( pgettext( "in progress craft", "You start working on the %s." ),
craft.tname() ),
string_format( pgettext( "in progress craft", "<npcname> starts working on the %s" ),
string_format( pgettext( "in progress craft", "<npcname> starts working on the %s." ),
craft.tname() ) );
}

Expand Down
62 changes: 47 additions & 15 deletions src/iexamine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@
#include "ui.h"
#include "uistate.h"
#include "units.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
#include "vpart_reference.h"
#include "weather.h"

const mtype_id mon_dark_wyrm( "mon_dark_wyrm" );
Expand Down Expand Up @@ -4509,7 +4512,38 @@ void iexamine::open_safe( player &, const tripoint &examp )

void iexamine::workbench( player &p, const tripoint &examp )
{
const furn_t &f_workbench = g->m.furn( examp ).obj();
workbench_internal( p, examp, cata::nullopt );
}

void iexamine::workbench_internal( player &p, const tripoint &examp,
const cata::optional<vpart_reference> &part )
{
std::vector<item_location> crafts;
std::string name;

bool items_at_loc = false;

if( part ) {
name = part->part().name();
auto items_at_part = part->vehicle().get_items( part->part_index() );

for( item &it : items_at_part ) {
if( it.is_craft() ) {
crafts.emplace_back( item_location( vehicle_cursor( part->vehicle(), part->part_index() ), &it ) );
}
}
} else {
name = g->m.furn( examp ).obj().name();

auto items_at_furn = g->m.i_at( examp );
items_at_loc = !items_at_furn.empty();

for( item &it : items_at_furn ) {
if( it.is_craft() ) {
crafts.emplace_back( item_location( map_cursor( examp ), &it ) );
}
}
}

uilist amenu;

Expand All @@ -4521,22 +4555,15 @@ void iexamine::workbench( player &p, const tripoint &examp )
get_items
};

auto items_at_furn = g->m.i_at( examp );
std::vector<item_location> crafts;
for( item &it : items_at_furn ) {
if( it.is_craft() ) {
crafts.emplace_back( item_location( map_cursor( examp ), &it ) );
}
amenu.text = string_format( pgettext( "furniture", "What to do at the %s?" ), name );
amenu.addentry( start_craft, true, '1', _( "Craft items" ) );
amenu.addentry( repeat_craft, true, '2', _( "Recraft last recipe" ) );
amenu.addentry( start_long_craft, true, '3', _( "Craft as long as possible" ) );
amenu.addentry( work_on_craft, !crafts.empty(), '4', _( "Work on craft" ) );
if( !part ) {
amenu.addentry( get_items, items_at_loc, '5', _( "Get items" ) );
}

amenu.text = string_format( pgettext( "furniture", "What to do at the %s?" ),
f_workbench.name() );
amenu.addentry( start_craft, true, '1', _( "Craft items" ) );
amenu.addentry( repeat_craft, true, '2', _( "Recraft last recipe" ) );
amenu.addentry( start_long_craft, true, '3', _( "Craft as long as possible" ) );
amenu.addentry( work_on_craft, !crafts.empty(), '4', _( "Work on craft" ) );
amenu.addentry( get_items, !items_at_furn.empty(), '5', _( "Get items" ) );

amenu.query();

option choice = static_cast<option>( amenu.ret );
Expand Down Expand Up @@ -4573,6 +4600,11 @@ void iexamine::workbench( player &p, const tripoint &examp )
}
}
uilist amenu2( _( "Which craft to work on?" ), item_names );

if( amenu2.ret == UILIST_CANCEL ) {
break;
}

const item *selected_craft = crafts[amenu2.ret].get_item();

p.add_msg_player_or_npc(
Expand Down
3 changes: 3 additions & 0 deletions src/iexamine.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class item;
class player;
class npc;
class map;
class vpart_reference;
struct tripoint;
struct itype;
struct mtype;
Expand Down Expand Up @@ -103,6 +104,8 @@ void on_smoke_out( const tripoint &examp,
void smoker_options( player &p, const tripoint &examp );
void open_safe( player &p, const tripoint &examp );
void workbench( player &p, const tripoint &examp );
void workbench_internal( player &p, const tripoint &examp,
const cata::optional<vpart_reference> &part );
hack_result hack_attempt( player &p );

bool pour_into_keg( const tripoint &pos, item &liquid );
Expand Down
1 change: 0 additions & 1 deletion src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1436,7 +1436,6 @@ class vehicle
void use_washing_machine( int p );
void use_monster_capture( int part, const tripoint &pos );
void use_bike_rack( int part );
void use_workbench( const int &part, const tripoint &pos );

veh_interact_results interact_with( const tripoint &pos, int interact_part );

Expand Down
82 changes: 1 addition & 81 deletions src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,86 +1359,6 @@ void vehicle::use_bike_rack( int part )
}
}

void vehicle::use_workbench( const int &part, const tripoint &pos )
{
const vehicle_part &workbench = parts[part];
player &p = g->u;

uilist amenu;

enum option : int {
start_craft = 0,
repeat_craft,
start_long_craft,
work_on_craft
};

auto items_at_part = get_items( part );
std::vector<item_location> crafts;
for( item &it : items_at_part ) {
if( it.is_craft() ) {
crafts.emplace_back( item_location( vehicle_cursor( *this, part ), &it ) );
}
}

amenu.text = string_format( pgettext( "furniture", "What to do at the %s?" ), workbench.name() );
amenu.addentry( start_craft, true, '1', _( "Craft items" ) );
amenu.addentry( repeat_craft, true, '2', _( "Recraft last recipe" ) );
amenu.addentry( start_long_craft, true, '3', _( "Craft as long as possible" ) );
amenu.addentry( work_on_craft, !crafts.empty(), '4', _( "Work on craft" ) );

amenu.query();

option choice = static_cast<option>( amenu.ret );
switch( choice ) {
case start_craft: {
if( p.has_active_mutation( trait_SHELL2 ) ) {
p.add_msg_if_player( m_info, _( "You can't craft while you're in your shell." ) );
} else {
p.craft( pos );
}
break;
}
case repeat_craft: {
if( p.has_active_mutation( trait_SHELL2 ) ) {
p.add_msg_if_player( m_info, _( "You can't craft while you're in your shell." ) );
} else {
p.recraft( pos );
}
break;
}
case start_long_craft: {
if( p.has_active_mutation( trait_SHELL2 ) ) {
p.add_msg_if_player( m_info, _( "You can't craft while you're in your shell." ) );
} else {
p.long_craft( pos );
}
break;
}
case work_on_craft: {
std::vector<std::string> item_names;
for( item_location &it : crafts ) {
if( it ) {
item_names.emplace_back( it.get_item()->tname() );
}
}
uilist amenu2( _( "Which craft to work on?" ), item_names );
const item *selected_craft = crafts[amenu2.ret].get_item();

p.add_msg_player_or_npc(
string_format( pgettext( "in progress craft", "You start working on the %s" ),
selected_craft->tname() ),
string_format( pgettext( "in progress craft", "<npcname> starts working on the %s" ),
selected_craft->tname() ) );
p.assign_activity( activity_id( "ACT_CRAFT" ) );
p.activity.targets.push_back( crafts[amenu2.ret].clone() );
p.activity.values.push_back( 0 ); // Not a long craft
break;
}
}
}


// Handles interactions with a vehicle in the examine menu.
veh_interact_results vehicle::interact_with( const tripoint &pos, int interact_part )
{
Expand Down Expand Up @@ -1678,7 +1598,7 @@ veh_interact_results vehicle::interact_with( const tripoint &pos, int interact_p
return DONE;
}
case WORKBENCH: {
use_workbench( workbench_part, pos );
iexamine::workbench_internal( g->u, pos, vpart_reference( *this, workbench_part ) );
return DONE;
}
}
Expand Down

0 comments on commit 75fa347

Please sign in to comment.