Skip to content

Commit

Permalink
Remove legacy npc_class id handling and extern list
Browse files Browse the repository at this point in the history
  • Loading branch information
Procyonae committed Jun 6, 2024
1 parent e8905f8 commit 88ff63c
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 115 deletions.
6 changes: 4 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@
#include "move_mode.h"
#include "mtype.h"
#include "npc.h"
#include "npc_class.h"
#include "npctrade.h"
#include "omdata.h"
#include "options.h"
Expand Down Expand Up @@ -310,6 +309,9 @@ static const mongroup_id GROUP_BLACK_ROAD( "GROUP_BLACK_ROAD" );

static const mtype_id mon_manhack( "mon_manhack" );

static const npc_class_id NC_DOCTOR( "NC_DOCTOR" );
static const npc_class_id NC_HALLU( "NC_HALLU" );

static const overmap_special_id overmap_special_world( "world" );

static const proficiency_id proficiency_prof_parkour( "prof_parkour" );
Expand Down Expand Up @@ -1284,7 +1286,7 @@ void game::create_starting_npcs()

shared_ptr_fast<npc> tmp = make_shared_fast<npc>();
tmp->normalize();
tmp->randomize( one_in( 2 ) ? NC_DOCTOR : NC_NONE );
tmp->randomize( one_in( 2 ) ? NC_DOCTOR : npc_class_id::NULL_ID() );
// hardcoded, consistent NPC position
// start_loc::place_player relies on this and must be updated if this is changed
tmp->spawn_at_precise( u.get_location() + point_north_west );
Expand Down
3 changes: 1 addition & 2 deletions src/mission.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "map_iterator.h"
#include "monster.h"
#include "npc.h"
#include "npc_class.h"
#include "overmap.h"
#include "overmapbuffer.h"
#include "point.h"
Expand Down Expand Up @@ -878,7 +877,7 @@ mission::mission()
item_id = itype_id::NULL_ID();
item_count = 1;
target_id = string_id<oter_type_t>::NULL_ID();
recruit_class = NC_NONE;
recruit_class = npc_class_id::NULL_ID();
target_npc_id = character_id();
monster_type = mtype_id::NULL_ID();
monster_kill_goal = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/mission.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ struct mission_type {
bool invisible_on_complete = false;
itype_id empty_container = itype_id::NULL_ID();
int item_count = 1;
npc_class_id recruit_class = npc_class_id( "NC_NONE" ); // The type of NPC you are to recruit
npc_class_id recruit_class = npc_class_id::NULL_ID(); // The type of NPC you are to recruit
character_id target_npc_id;
mtype_id monster_type = mtype_id::NULL_ID();
species_id monster_species;
Expand Down
5 changes: 4 additions & 1 deletion src/mission_start.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "mapdata.h"
#include "messages.h"
#include "npc.h"
#include "npc_class.h"
#include "omdata.h"
#include "overmap.h"
#include "overmapbuffer.h"
Expand All @@ -43,6 +42,10 @@ static const itype_id itype_software_useless( "software_useless" );
static const mission_type_id
mission_MISSION_GET_ZOMBIE_BLOOD_ANAL( "MISSION_GET_ZOMBIE_BLOOD_ANAL" );

static const npc_class_id NC_DOCTOR( "NC_DOCTOR" );
static const npc_class_id NC_HACKER( "NC_HACKER" );
static const npc_class_id NC_SCIENTIST( "NC_SCIENTIST" );

static const ter_str_id ter_t_floor( "t_floor" );
static const ter_str_id ter_t_wall_metal( "t_wall_metal" );

Expand Down
11 changes: 7 additions & 4 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ static const mfaction_str_id monfaction_bee( "bee" );
static const mfaction_str_id monfaction_human( "human" );
static const mfaction_str_id monfaction_player( "player" );

static const npc_class_id NC_ARSONIST( "NC_ARSONIST" );
static const npc_class_id NC_BOUNTY_HUNTER( "NC_BOUNTY_HUNTER" );
static const npc_class_id NC_COWBOY( "NC_COWBOY" );
static const npc_class_id NC_EVAC_SHOPKEEP( "NC_EVAC_SHOPKEEP" );
static const npc_class_id NC_TRADER( "NC_TRADER" );

static const overmap_location_str_id overmap_location_source_of_ammo( "source_of_ammo" );
static const overmap_location_str_id overmap_location_source_of_anything( "source_of_anything" );
static const overmap_location_str_id overmap_location_source_of_drink( "source_of_drink" );
Expand Down Expand Up @@ -307,10 +313,7 @@ void npc_template::load( const JsonObject &jsobj )
if( jsobj.has_string( "faction" ) ) {
guy.set_fac_id( jsobj.get_string( "faction" ) );
}

if( jsobj.has_int( "class" ) ) {
guy.myclass = npc_class::from_legacy_int( jsobj.get_int( "class" ) );
} else if( jsobj.has_string( "class" ) ) {
if( jsobj.has_string( "class" ) ) {
guy.myclass = npc_class_id( jsobj.get_string( "class" ) );
}

Expand Down
64 changes: 2 additions & 62 deletions src/npc_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,49 +22,6 @@
#include "skill.h"
#include "trait_group.h"

const npc_class_id NC_ARSONIST( "NC_ARSONIST" );
const npc_class_id NC_BARTENDER( "NC_BARTENDER" );
const npc_class_id NC_BOUNTY_HUNTER( "NC_BOUNTY_HUNTER" );
const npc_class_id NC_COWBOY( "NC_COWBOY" );
const npc_class_id NC_CYBORG( "NC_CYBORG" );
const npc_class_id NC_DOCTOR( "NC_DOCTOR" );
const npc_class_id NC_EVAC_SHOPKEEP( "NC_EVAC_SHOPKEEP" );
const npc_class_id NC_HACKER( "NC_HACKER" );
const npc_class_id NC_HALLU( "NC_HALLU" );
const npc_class_id NC_HUNTER( "NC_HUNTER" );
const npc_class_id NC_JUNK_SHOPKEEP( "NC_JUNK_SHOPKEEP" );
const npc_class_id NC_NINJA( "NC_NINJA" );
const npc_class_id NC_NONE( "NC_NONE" );
const npc_class_id NC_SCAVENGER( "NC_SCAVENGER" );
const npc_class_id NC_SCIENTIST( "NC_SCIENTIST" );
const npc_class_id NC_SHOPKEEP( "NC_SHOPKEEP" );
const npc_class_id NC_SOLDIER( "NC_SOLDIER" );
const npc_class_id NC_THUG( "NC_THUG" );
const npc_class_id NC_TRADER( "NC_TRADER" );

static const std::array<npc_class_id, 19> legacy_ids = {{
NC_NONE,
NC_EVAC_SHOPKEEP, // Found in the Evacuation Center, unique, has more goods than he should be able to carry
NC_SHOPKEEP, // Found in towns. Stays in his shop mostly.
NC_HACKER, // Weak in combat but has hacking skills and equipment
NC_CYBORG, // Broken Cyborg rescued from a lab
NC_DOCTOR, // Found in towns, or roaming. Stays in the clinic.
NC_TRADER, // Roaming trader, journeying between towns.
NC_NINJA, // Specializes in unarmed combat, carries few items
NC_COWBOY, // Gunslinger and survivalist
NC_SCIENTIST, // Uses intelligence-based skills and high-tech items
NC_BOUNTY_HUNTER, // Resourceful and well-armored
NC_THUG, // Moderate melee skills and poor equipment
NC_SCAVENGER, // Good with pistols light weapons
NC_ARSONIST, // Evacuation Center, restocks Molotovs and anarchist type stuff
NC_HUNTER, // Survivor type good with bow or rifle
NC_SOLDIER, // Well equipped and trained combatant, good with rifles and melee
NC_BARTENDER, // Stocks alcohol
NC_JUNK_SHOPKEEP, // Stocks wide range of items...
NC_HALLU // Hallucinatory NPCs
}
};

static generic_factory<npc_class> npc_class_factory( "npc_class" );

/** @relates string_id */
Expand All @@ -81,7 +38,7 @@ bool string_id<npc_class>::is_valid() const
return npc_class_factory.is_valid( *this );
}

npc_class::npc_class() : id( NC_NONE )
npc_class::npc_class() : id( npc_class_id::NULL_ID() )
{
}

Expand Down Expand Up @@ -134,13 +91,6 @@ void npc_class::finalize_all()

void npc_class::check_consistency()
{
for( const npc_class_id &legacy : legacy_ids ) {
if( !npc_class_factory.is_valid( legacy ) ) {
debugmsg( "Missing legacy npc class %s (at index %d)",
legacy.c_str(), &legacy - legacy_ids.data() );
}
}

for( const npc_class &cl : npc_class_factory.get_all() ) {
for( const shopkeeper_item_group &ig : cl.shop_item_groups ) {
if( !item_group::group_is_defined( ig.id ) ) {
Expand Down Expand Up @@ -373,16 +323,6 @@ void npc_class::load( const JsonObject &jo, const std::string_view )
}
}

const npc_class_id &npc_class::from_legacy_int( int i )
{
if( i < 0 || static_cast<size_t>( i ) >= legacy_ids.size() ) {
debugmsg( "Invalid legacy class id: %d", i );
return npc_class_id::NULL_ID();
}

return legacy_ids[ i ];
}

const std::vector<npc_class> &npc_class::get_all()
{
return npc_class_factory.get_all();
Expand All @@ -398,7 +338,7 @@ const npc_class_id &npc_class::random_common()
}

if( common_classes.empty() || one_in( common_classes.size() ) ) {
return NC_NONE;
return npc_class_id::NULL_ID();
}

return *random_entry( common_classes );
Expand Down
23 changes: 0 additions & 23 deletions src/npc_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ class npc_class

void load( const JsonObject &jo, std::string_view src );

static const npc_class_id &from_legacy_int( int i );

static const npc_class_id &random_common();

static void load_npc_class( const JsonObject &jo, const std::string &src );
Expand All @@ -155,25 +153,4 @@ class npc_class
static void check_consistency();
};

// TODO: Get rid of that
extern const npc_class_id NC_NONE;
extern const npc_class_id NC_EVAC_SHOPKEEP;
extern const npc_class_id NC_SHOPKEEP;
extern const npc_class_id NC_HACKER;
extern const npc_class_id NC_CYBORG;
extern const npc_class_id NC_DOCTOR;
extern const npc_class_id NC_TRADER;
extern const npc_class_id NC_NINJA;
extern const npc_class_id NC_COWBOY;
extern const npc_class_id NC_SCIENTIST;
extern const npc_class_id NC_BOUNTY_HUNTER;
extern const npc_class_id NC_THUG;
extern const npc_class_id NC_SCAVENGER;
extern const npc_class_id NC_ARSONIST;
extern const npc_class_id NC_HUNTER;
extern const npc_class_id NC_SOLDIER;
extern const npc_class_id NC_BARTENDER;
extern const npc_class_id NC_JUNK_SHOPKEEP;
extern const npc_class_id NC_HALLU;

#endif // CATA_SRC_NPC_CLASS_H
18 changes: 4 additions & 14 deletions src/savegame_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
#include "mtype.h"
#include "mutation.h"
#include "npc.h"
#include "npc_class.h"
#include "options.h"
#include "overmapbuffer.h"
#include "pimpl.h"
Expand Down Expand Up @@ -2077,12 +2076,10 @@ void npc::load( const JsonObject &data )
}

int misstmp = 0;
int classtmp = 0;
int atttmp = 0;
std::string facID;
std::string comp_miss_role;
tripoint_abs_omt comp_miss_pt;
std::string classid;
std::string companion_mission_role;
time_point companion_mission_t = calendar::turn_zero;
time_point companion_mission_t_r = calendar::turn_zero;
Expand All @@ -2100,12 +2097,8 @@ void npc::load( const JsonObject &data )
data.read( "marked_for_death", marked_for_death );
data.read( "dead", dead );
data.read( "patience", patience );
if( data.has_number( "myclass" ) ) {
data.read( "myclass", classtmp );
myclass = npc_class::from_legacy_int( classtmp );
} else if( data.has_string( "myclass" ) ) {
data.read( "myclass", classid );
myclass = npc_class_id( classid );
if( data.has_string( "myclass" ) ) {
data.read( "myclass", myclass );
}
if( data.has_string( "idz" ) ) {
data.read( "idz", idz );
Expand Down Expand Up @@ -3607,11 +3600,8 @@ void mission::deserialize( const JsonObject &jo )
target_id = string_id<oter_type_t>( omid );
}

if( jo.has_int( "recruit_class" ) ) {
recruit_class = npc_class::from_legacy_int( jo.get_int( "recruit_class" ) );
} else {
recruit_class = npc_class_id( jo.get_string( "recruit_class", "NC_NONE" ) );
}
recruit_class = jo.has_string( "recruit_class" ) ? npc_class_id( jo.get_string( "recruit_class" ) )
: npc_class_id::NULL_ID();

jo.read( "target_npc_id", target_npc_id );
jo.read( "monster_type", monster_type );
Expand Down
2 changes: 1 addition & 1 deletion tests/npc_shopkeeper_item_groups_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TEST_CASE( "npc_shopkeeper_item_groups", "[npc][trade]" )
item_location const scrap_inv = guy.i_add( scrap );
REQUIRE( scrap_inv );
THEN( "sell_belongings is true - item in inventory available for sale" ) {
guy.myclass = NC_NONE;
guy.myclass = npc_class_id::NULL_ID();
REQUIRE( guy.myclass->sells_belongings == true );
REQUIRE( guy.wants_to_sell( scrap_inv ) );
}
Expand Down
5 changes: 2 additions & 3 deletions tests/npc_talk_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ static const itype_id itype_manual_speech( "manual_speech" );

static const mtype_id mon_zombie_bio_op( "mon_zombie_bio_op" );

static const npc_class_id NC_NONE( "NC_NONE" );
static const npc_class_id NC_TEST_CLASS( "NC_TEST_CLASS" );

static const proficiency_id proficiency_prof_test( "prof_test" );
Expand Down Expand Up @@ -378,7 +377,7 @@ TEST_CASE( "npc_talk_class", "[npc_talk]" )
npc &talker_npc = prep_test( d );

d.add_topic( "TALK_TEST_NPC_CLASS" );
talker_npc.myclass = NC_NONE;
talker_npc.myclass = npc_class_id::NULL_ID();
gen_response_lines( d, 1 );
CHECK( d.responses[0].text == "This is a basic test response." );
talker_npc.myclass = NC_TEST_CLASS;
Expand Down Expand Up @@ -1010,7 +1009,7 @@ TEST_CASE( "npc_talk_effects", "[npc_talk]" )
gen_response_lines( d, 19 );
talk_effect_t &effects = d.responses[18].success;
effects.apply( d );
CHECK( talker_npc.myclass == NC_NONE );
CHECK( talker_npc.myclass == npc_class_id::NULL_ID() );
}

TEST_CASE( "npc_change_topic", "[npc_talk]" )
Expand Down
3 changes: 1 addition & 2 deletions tests/npc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "map_helpers.h"
#include "memory_fast.h"
#include "npc.h"
#include "npc_class.h"
#include "npctalk.h"
#include "overmapbuffer.h"
#include "pathfinding.h"
Expand Down Expand Up @@ -75,7 +74,7 @@ static npc create_model()
{
npc model_npc;
model_npc.normalize();
model_npc.randomize( NC_NONE );
model_npc.randomize( npc_class_id::NULL_ID() );
for( const trait_id &tr : model_npc.get_mutations() ) {
model_npc.unset_mutation( tr );
}
Expand Down

0 comments on commit 88ff63c

Please sign in to comment.