From d4435da8bb9d42ee2ee2d0f66a06baee52575d68 Mon Sep 17 00:00:00 2001 From: Mark Langsdorf Date: Mon, 6 May 2019 19:06:36 -0500 Subject: [PATCH] npc: improve NPC omt_destination finding Increase the number of locations that NPCs will search in order to satisfy their needs. Reduce the search radius that NPCs will search for a destination location from 900 overmap tiles to 150. Don't search for destinations if the city size is 0. If an NPC can't find a destination for any reason, just have them pick a random location and go there instead of having them repeat the search on the next turn. --- data/json/npcs/destination_locations.json | 65 ++++++++++++++++++++--- data/json/overmap/special_locations.json | 30 ----------- src/npc.h | 2 - src/npcmove.cpp | 36 ++++++++++--- src/savegame_json.cpp | 11 ---- 5 files changed, 88 insertions(+), 56 deletions(-) diff --git a/data/json/npcs/destination_locations.json b/data/json/npcs/destination_locations.json index 1bee5743db836..f9218d44ec97e 100644 --- a/data/json/npcs/destination_locations.json +++ b/data/json/npcs/destination_locations.json @@ -2,31 +2,84 @@ { "type": "overmap_location", "id": "source_of_anything", - "terrains": [ "house", "s_gas", "s_pharm", "s_hardware", "s_sports", "s_liquor", "s_gun", "s_library" ] + "terrains": [ + "house", + "s_gas", + "s_pharm", + "s_hardware", + "s_sports", + "s_liquor", + "s_gun", + "s_library", + "megastore_entrance", + "fire_station", + "mil_surplus", + "mansion_entry" + ] }, { "type": "overmap_location", "id": "source_of_ammo", - "terrains": [ "house", "s_gun" ] + "terrains": [ "house", "s_gun", "mil_surplus", "mansion_entry" ] }, { "type": "overmap_location", "id": "source_of_gun", - "terrains": [ "s_gun" ] + "terrains": [ "s_gun", "pawn", "hdwr_large_entrance", "mansion_entry" ] }, { "type": "overmap_location", "id": "source_of_weapon", - "terrains": [ "s_gun", "s_sports", "s_hardware" ] + "terrains": [ "s_gun", "s_sports", "s_hardware", "megastore_entrance", "hdwr_large_entrance", "fire_station", "mansion_entry" ] }, { "type": "overmap_location", "id": "source_of_drink", - "terrains": [ "s_gas", "s_pharm", "s_liquor", "s_grocery" ] + "terrains": [ + "s_gas", + "s_pharm", + "s_pharm_1", + "s_restaurant", + "s_restaurant_1", + "s_restaurant_2", + "s_restaurant_3", + "s_restaurant_coffee", + "s_restaurant_coffee_1", + "s_restaurant_coffee_2", + "s_teashop", + "s_teashop_1", + "bar", + "bar_1", + "s_liquor", + "s_grocery", + "megastore_entrance", + "mansion_entry" + ] }, { "type": "overmap_location", "id": "source_of_food", - "terrains": [ "s_grocery" ] + "terrains": [ + "s_grocery", + "farm_1", + "2farm_1", + "dairy_farm_NW", + "sugar_house", + "orchard_stall", + "s_restaurant", + "s_restaurant_1", + "s_restaurant_2", + "s_restaurant_3", + "s_restaurant_fast", + "s_restaurant_fast_1", + "s_butcher", + "s_butcher_1", + "s_butcher_2", + "s_pizza_parlor", + "s_pizza_parlor_1", + "megastore_entrance", + "bakery", + "mansion_entry" + ] } ] diff --git a/data/json/overmap/special_locations.json b/data/json/overmap/special_locations.json index 2b9708b55acbb..f6ee7a30cf110 100644 --- a/data/json/overmap/special_locations.json +++ b/data/json/overmap/special_locations.json @@ -60,36 +60,6 @@ "id": "road", "terrains": [ "hiway_ns", "hiway_ew", "ranch_camp_77", "necropolis_a_11", "road", "road_nesw_manhole" ] }, - { - "type": "overmap_location", - "id": "need_none", - "terrains": [ "house", "s_gas", "s_pharm", "s_hardware", "s_sports", "s_liquor", "s_gun", "s_library" ] - }, - { - "type": "overmap_location", - "id": "need_ammo", - "terrains": [ "house", "s_gun" ] - }, - { - "type": "overmap_location", - "id": "need_gun", - "terrains": [ "s_gun" ] - }, - { - "type": "overmap_location", - "id": "need_weapon", - "terrains": [ "s_gun", "s_sports", "s_hardware" ] - }, - { - "type": "overmap_location", - "id": "need_drink", - "terrains": [ "s_gas", "s_pharm", "s_liquor", "s_grocery" ] - }, - { - "type": "overmap_location", - "id": "need_food", - "terrains": [ "s_grocery" ] - }, { "type": "overmap_location", "id": "forest_trail", diff --git a/src/npc.h b/src/npc.h index 2e18095895274..d63614f81ae4a 100644 --- a/src/npc.h +++ b/src/npc.h @@ -962,8 +962,6 @@ class npc : public player */ tripoint goal; std::vector omt_path; - tripoint wander_pos; // Not actually used (should be: wander there when you hear a sound) - int wander_time; /** * Location and index of the corpse we'd like to pulp (if any). diff --git a/src/npcmove.cpp b/src/npcmove.cpp index e377ae9910819..87993ae9e1828 100644 --- a/src/npcmove.cpp +++ b/src/npcmove.cpp @@ -30,6 +30,7 @@ #include "monster.h" #include "mtype.h" #include "npctalk.h" +#include "options.h" #include "overmap_location.h" #include "overmapbuffer.h" #include "projectile.h" @@ -3221,22 +3222,43 @@ void npc::set_omt_destination() return; } + tripoint surface_omt_loc = global_omt_location(); + // We need that, otherwise find_closest won't work properly + surface_omt_loc.z = 0; + + // also, don't bother looking if the CITY_SIZE is 0, just go somewhere at random + const int city_size = get_option( "CITY_SIZE" ); + if( city_size == 0 ) { + goal = surface_omt_loc + point( rng( -90, 90 ), rng( -90, 90 ) ); + return; + } + + decide_needs(); if( needs.empty() ) { // We don't need anything in particular. needs.push_back( need_none ); } - // We need that, otherwise find_closest won't work properly - // TODO: Allow finding sewers and stuff - tripoint surface_omt_loc = global_omt_location(); - surface_omt_loc.z = 0; + std::string dest_type; + for( const auto &fulfill : needs ) { + dest_type = get_location_for( fulfill )->get_random_terrain().id().str(); + goal = overmap_buffer.find_closest( surface_omt_loc, dest_type, 150, false ); + if( goal != overmap::invalid_tripoint ) { + break; + } + } + + // couldn't find any places to go, so go somewhere. + if( goal == overmap::invalid_tripoint ) { + goal = surface_omt_loc + point( rng( -90, 90 ), rng( -90, 90 ) ); + return; + } - std::string dest_type = get_location_for( needs.front() )->get_random_terrain().id().str(); - goal = overmap_buffer.find_closest( surface_omt_loc, dest_type, 0, false ); DebugLog( D_INFO, DC_ALL ) << "npc::set_omt_destination - new goal for NPC [" << get_name() << "] with [" - << get_need_str_id( needs.front() ) << "] is [" << dest_type << "] in [" + << get_need_str_id( needs.front() ) << "] is [" << dest_type << + "] in [" << goal.x << "," << goal.y << "," << goal.z << "]."; } diff --git a/src/savegame_json.cpp b/src/savegame_json.cpp index 4d4027ae9a165..240b1cc3a6d62 100644 --- a/src/savegame_json.cpp +++ b/src/savegame_json.cpp @@ -1276,13 +1276,6 @@ void npc::load( JsonObject &data ) data.read( "personality", personality ); - data.read( "wandf", wander_time ); - data.read( "wandx", wander_pos.x ); - data.read( "wandy", wander_pos.y ); - if( !data.read( "wandz", wander_pos.z ) ) { - wander_pos.z = posz(); - } - if( !data.read( "submap_coords", submap_coords ) ) { // Old submap coordinates are for the point (0, 0, 0) on local map // New ones are for submap that contains pos @@ -1466,10 +1459,6 @@ void npc::store( JsonOut &json ) const json.member( "myclass", myclass.str() ); json.member( "personality", personality ); - json.member( "wandf", wander_time ); - json.member( "wandx", wander_pos.x ); - json.member( "wandy", wander_pos.y ); - json.member( "wandz", wander_pos.z ); json.member( "submap_coords", submap_coords );