Skip to content

Commit

Permalink
NPC activity rework - farming and generic multi-activity loop (Clever…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpwbrown authored and misterprimus committed Sep 21, 2019
1 parent 3bb9b30 commit 6e84674
Show file tree
Hide file tree
Showing 23 changed files with 1,259 additions and 493 deletions.
6 changes: 3 additions & 3 deletions data/json/npcs/TALK_COMMON_ALLY.json
Original file line number Diff line number Diff line change
Expand Up @@ -741,16 +741,16 @@
"effect": "sort_loot"
},
{
"text": "Please work on any unfinished construction task that you know how to finish.",
"text": "Please do any construction work that you can.",
"topic": "TALK_DONE",
"condition": { "not": "npc_has_activity" },
"effect": "do_construction"
},
{
"text": "Please work on any contruction blueprint zones nearby.",
"text": "Please do some farming work.",
"topic": "TALK_DONE",
"condition": { "not": "npc_has_activity" },
"effect": "do_blueprint_construction"
"effect": "do_farming"
}
]
},
Expand Down
31 changes: 28 additions & 3 deletions data/json/player_activities.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"no_resume": true
},
{
"id": "ACT_BLUEPRINT_CONSTRUCTION",
"id": "ACT_TIDY_UP",
"type": "activity_type",
"activity_level": "ACTIVE_EXERCISE",
"verb": "constructing",
"activity_level": "MODERATE_EXERCISE",
"verb": "tidying up",
"suspendable": false,
"based_on": "neither",
"no_resume": true
Expand Down Expand Up @@ -306,6 +306,24 @@
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_FETCH_REQUIRED",
"type": "activity_type",
"activity_level": "MODERATE_EXERCISE",
"verb": "fetching components",
"suspendable": false,
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_MULTIPLE_FARM",
"type": "activity_type",
"activity_level": "ACTIVE_EXERCISE",
"verb": "farming",
"suspendable": false,
"based_on": "neither",
"no_resume": true
},
{
"id": "ACT_PLANT_PLOT",
"type": "activity_type",
Expand Down Expand Up @@ -533,6 +551,13 @@
"verb": "drilling",
"based_on": "speed"
},
{
"id": "ACT_CHURN",
"type": "activity_type",
"activity_level": "EXTRA_EXERCISE",
"verb": "churning earth",
"based_on": "time"
},
{
"id": "ACT_DIG",
"type": "activity_type",
Expand Down
151 changes: 49 additions & 102 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <array>
#include <iterator>
#include <memory>

#include <ostream>
#include <set>
#include <stdexcept>
Expand Down Expand Up @@ -133,7 +134,8 @@ activity_handlers::do_turn_functions = {
{ activity_id( "ACT_PICKUP" ), pickup_do_turn },
{ activity_id( "ACT_WEAR" ), wear_do_turn },
{ activity_id( "ACT_MULTIPLE_CONSTRUCTION" ), multiple_construction_do_turn },
{ activity_id( "ACT_BLUEPRINT_CONSTRUCTION" ), blueprint_construction_do_turn },
{ activity_id( "ACT_MULTIPLE_FARM" ), multiple_farm_do_turn },
{ activity_id( "ACT_FETCH_REQUIRED" ), fetch_do_turn },
{ activity_id( "ACT_BUILD" ), build_do_turn },
{ activity_id( "ACT_EAT_MENU" ), eat_menu_do_turn },
{ activity_id( "ACT_CONSUME_FOOD_MENU" ), consume_food_menu_do_turn },
Expand All @@ -151,6 +153,7 @@ activity_handlers::do_turn_functions = {
{ activity_id( "ACT_BUTCHER_FULL" ), butcher_do_turn },
{ activity_id( "ACT_TRAVELLING" ), travel_do_turn },
{ activity_id( "ACT_AUTODRIVE" ), drive_do_turn },
{ activity_id( "ACT_CHURN" ), churn_do_turn },
{ activity_id( "ACT_FIELD_DRESS" ), butcher_do_turn },
{ activity_id( "ACT_SKIN" ), butcher_do_turn },
{ activity_id( "ACT_QUARTER" ), butcher_do_turn },
Expand All @@ -160,6 +163,7 @@ activity_handlers::do_turn_functions = {
{ activity_id( "ACT_CHOP_TREE" ), chop_tree_do_turn },
{ activity_id( "ACT_CHOP_LOGS" ), chop_tree_do_turn },
{ activity_id( "ACT_CHOP_PLANKS" ), chop_tree_do_turn },
{ activity_id( "ACT_TIDY_UP" ), tidy_up_do_turn },
{ activity_id( "ACT_JACKHAMMER" ), jackhammer_do_turn },
{ activity_id( "ACT_DIG" ), dig_do_turn },
{ activity_id( "ACT_DIG_CHANNEL" ), dig_channel_do_turn },
Expand Down Expand Up @@ -197,6 +201,7 @@ activity_handlers::finish_functions = {
{ activity_id( "ACT_RELOAD" ), reload_finish },
{ activity_id( "ACT_START_FIRE" ), start_fire_finish },
{ activity_id( "ACT_TRAIN" ), train_finish },
{ activity_id( "ACT_CHURN" ), churn_finish },
{ activity_id( "ACT_VEHICLE" ), vehicle_finish },
{ activity_id( "ACT_START_ENGINES" ), start_engines_finish },
{ activity_id( "ACT_OXYTORCH" ), oxytorch_finish },
Expand Down Expand Up @@ -2600,7 +2605,7 @@ void activity_handlers::move_items_do_turn( player_activity *act, player *p )

void activity_handlers::move_loot_do_turn( player_activity *act, player *p )
{
activity_on_turn_move_loot( *act, *p );
generic_multi_activity_handler( *act, *p );
}

void activity_handlers::adv_inventory_do_turn( player_activity *, player *p )
Expand Down Expand Up @@ -3092,6 +3097,24 @@ static bool character_has_skill_for( const player *p, const construction &con )
} );
}

void activity_handlers::churn_do_turn( player_activity *act, player *p )
{
( void )act;
( void )p;
p->set_moves( 0 );
}

void activity_handlers::churn_finish( player_activity *act, player *p )
{
p->add_msg_if_player( _( "You finish churning up the earth here." ) );
g->m.ter_set( g->m.getlocal( act->placement ), t_dirtmound );
// Go back to what we were doing before
// could be player zone activity, or could be NPC multi-farming
act->set_to_null();
p->activity = p->backlog.front();
p->backlog.pop_front();
}

void activity_handlers::build_do_turn( player_activity *act, player *p )
{
const std::vector<construction> &list_constructions = get_constructions();
Expand Down Expand Up @@ -3147,100 +3170,24 @@ void activity_handlers::build_do_turn( player_activity *act, player *p )
}
}

void activity_handlers::multiple_construction_do_turn( player_activity *act, player *p )
void activity_handlers::tidy_up_do_turn( player_activity *act, player *p )
{
( void )act;
const activity_id act_multiple_construction = activity_id( "ACT_MULTIPLE_CONSTRUCTION" );
tripoint src_loc_start = p->pos();
// search in a radius around unsorted zone to find corpse spots
std::vector<tripoint> build_spots;
for( tripoint p : g->m.points_in_radius( src_loc_start, 20 ) ) {
partial_con *pc = g->m.partial_con_at( p );
if( pc ) {
build_spots.push_back( p );
}
}
// Nuke the current activity, leaving the backlog alone.
p->activity = player_activity();

// sort source tiles by distance
for( auto &src_loc : build_spots ) {
if( !g->m.inbounds( src_loc ) ) {
if( !g->m.inbounds( p->pos() ) ) {
// p is implicitly an NPC that has been moved off the map, so reset the activity
// and unload them
p->assign_activity( act_multiple_construction );
p->set_moves( 0 );
g->reload_npcs();
return;
}
std::vector<tripoint> route = route_adjacent( *p, src_loc );
if( route.empty() ) {
// can't get there, can't do anything, skip it
continue;
}
p->set_destination( route, player_activity( act_multiple_construction ) );
return;
}

// skip tiles on fire so as not to try and butcher corpses on fire
// and inaccessible furniture, like filled charcoal kiln
if( g->m.get_field( src_loc, fd_fire ) != nullptr ) {
continue;
}
bool adjacent = false;
for( auto elem : g->m.points_in_radius( src_loc, 1 ) ) {
if( p->pos() == elem ) {
adjacent = true;
break;
}
}
if( !adjacent ) {
std::vector<tripoint> route = route_adjacent( *p, src_loc );

// check if we found path to source / adjacent tile
if( route.empty() ) {
add_msg( m_info, _( "%s can't reach the source tile to construct" ),
p->disp_name() );
return;
}

// set the destination and restart activity after player arrives there
// we don't need to check for safe mode,
// activity will be restarted only if
// player arrives on destination tile
p->set_destination( route, player_activity( act_multiple_construction ) );
return;
}
// maybe the construction dissappeared, double check before starting work
partial_con *nc = g->m.partial_con_at( src_loc );
if( !nc ) {
p->assign_activity( act_multiple_construction );
return;
}
p->backlog.push_front( act_multiple_construction );
p->assign_activity( activity_id( "ACT_BUILD" ) );
p->activity.placement = g->m.getabs( src_loc );
return;
generic_multi_activity_handler( *act, *p );
}

}
if( p->moves <= 0 ) {
// Restart activity and break from cycle.
p->assign_activity( act_multiple_construction );
return;
}
void activity_handlers::multiple_construction_do_turn( player_activity *act, player *p )
{
generic_multi_activity_handler( *act, *p );
}

// If we got here without restarting the activity, it means we're done.
if( p->is_npc() ) {
npc *guy = dynamic_cast<npc *>( p );
guy->current_activity_id = activity_id::NULL_ID();
guy->revert_after_activity();
}
void activity_handlers::multiple_farm_do_turn( player_activity *act, player *p )
{
generic_multi_activity_handler( *act, *p );
}

void activity_handlers::blueprint_construction_do_turn( player_activity *act, player *p )
void activity_handlers::fetch_do_turn( player_activity *act, player *p )
{
activity_on_turn_blueprint_move( *act, *p );
generic_multi_activity_handler( *act, *p );
}

void activity_handlers::craft_do_turn( player_activity *act, player *p )
Expand Down Expand Up @@ -3794,40 +3741,40 @@ static void perform_zone_activity_turn( player *p,
} else { // we are at destination already
/* Perform action */
tile_action( *p, tile_loc );

if( p->moves <= 0 ) {
return;
}
}
}

add_msg( m_info, finished_msg );
p->activity.set_to_null();
}

void activity_handlers::harvest_plot_do_turn( player_activity *, player *p )
{
const auto reject_tile = [p]( const tripoint & tile ) {
return !p->sees( tile ) || !g->m.has_flag_furn( "GROWTH_HARVEST", tile );
const auto reject_tile = []( const tripoint & tile ) {
return !g->m.has_flag_furn( "GROWTH_HARVEST", tile );
};
const auto harvest = [&]( player & p, const tripoint & tile ) {
iexamine::harvest_plant( p, tile, false );
};
perform_zone_activity_turn( p,
zone_type_id( "FARM_PLOT" ),
reject_tile,
iexamine::harvest_plant,
harvest,
_( "You harvested all the plots you could." ) );

}

void activity_handlers::till_plot_do_turn( player_activity *, player *p )
{
const auto reject_tile = [p]( const tripoint & tile ) {
return !p->sees( tile ) || !g->m.has_flag( "PLOWABLE", tile ) || g->m.has_furn( tile );
const auto reject_tile = []( const tripoint & tile ) {
return !g->m.has_flag( "PLOWABLE", tile ) || g->m.has_furn( tile );
};

const auto dig = []( player & p, const tripoint & tile_loc ) {
p.add_msg_if_player( _( "You churn up the earth here." ) );
p.mod_moves( -300 );
g->m.ter_set( tile_loc, t_dirtmound );
p.assign_activity( activity_id( "ACT_CHURN" ), 18000, -1 );
p.activity.placement = tile_loc;
};

perform_zone_activity_turn( p,
Expand Down Expand Up @@ -3863,7 +3810,7 @@ void activity_handlers::fertilize_plot_do_turn( player_activity *act, player *p
const auto reject_tile = [&]( const tripoint & tile ) {
check_fertilizer();
ret_val<bool> can_fert = iexamine::can_fertilize( *p, tile, fertilizer );
return !p->sees( tile ) || !can_fert.success();
return !can_fert.success();
};

const auto fertilize = [&]( player & p, const tripoint & tile ) {
Expand Down Expand Up @@ -3914,7 +3861,7 @@ void activity_handlers::plant_plot_do_turn( player_activity *, player *p )

// cleanup unwanted tiles (local coords)
const auto reject_tiles = [&]( const tripoint & tile ) {
if( !p->sees( tile ) || !g->m.has_flag_ter_or_furn( "PLANTABLE", tile ) ||
if( !g->m.has_flag_ter_or_furn( "PLANTABLE", tile ) ||
g->m.has_items( tile ) ) {
return true;
}
Expand Down
Loading

0 comments on commit 6e84674

Please sign in to comment.