Skip to content

Commit

Permalink
Merge pull request #37436 from Qrox/construction-id
Browse files Browse the repository at this point in the history
Migrate construction to use int_id and string_id
  • Loading branch information
ZhilkinSerg authored Jan 31, 2020
2 parents cf81aeb + fff89e7 commit 78e6e40
Show file tree
Hide file tree
Showing 19 changed files with 505 additions and 64 deletions.
267 changes: 267 additions & 0 deletions data/json/construction.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions data/mods/Aftershock/recipes/construction.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"type": "construction",
"id": "constr_afs_fridge_vehicle",
"description": "Convert Fridge Power Supply",
"category": "FURN",
"required_skills": [ [ "fabrication", 3 ], [ "electronics", 4 ], [ "mechanics", 3 ] ],
Expand All @@ -13,6 +14,7 @@
},
{
"type": "construction",
"id": "constr_afs_freezer_vehicle",
"description": "Convert Vehicle Fridge to Freezer",
"category": "FURN",
"required_skills": [ [ "electronics", 3 ], [ "mechanics", 3 ] ],
Expand Down
10 changes: 10 additions & 0 deletions data/mods/CRT_EXPANSION/constructions/crt_constructions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"type": "construction",
"id": "constr_crt_cut_grass_long",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -12,6 +13,7 @@
},
{
"type": "construction",
"id": "constr_crt_cut_grass_tall",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -23,6 +25,7 @@
},
{
"type": "construction",
"id": "constr_crt_remove_grass",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -34,6 +37,7 @@
},
{
"type": "construction",
"id": "constr_crt_remove_grass_long",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -45,6 +49,7 @@
},
{
"type": "construction",
"id": "constr_crt_remove_grass_tall",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -56,6 +61,7 @@
},
{
"type": "construction",
"id": "constr_crt_remove_grass_dead",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -67,6 +73,7 @@
},
{
"type": "construction",
"id": "constr_crt_remove_grass_golf",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -78,6 +85,7 @@
},
{
"type": "construction",
"id": "constr_crt_remove_grass_white",
"description": "Cut Grass",
"category": "OTHER",
"required_skills": [ [ "survival", 0 ] ],
Expand All @@ -89,6 +97,7 @@
},
{
"type": "construction",
"id": "constr_crt_chop_trunk",
"description": "Chop Tree Trunk Into Logs",
"category": "FARM_WOOD",
"required_skills": [ [ "survival", 2 ] ],
Expand All @@ -100,6 +109,7 @@
},
{
"type": "construction",
"id": "constr_crt_wall_stick",
"description": "Makeshift Wall",
"category": "OTHER",
"required_skills": [ [ "survival", 2 ] ],
Expand Down
2 changes: 2 additions & 0 deletions data/mods/Hydroponics/construction.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"type": "construction",
"id": "constr_hydroponics",
"description": "Build Hydroponics",
"category": "CONSTRUCT",
"required_skills": [ [ "fabrication", 4 ], [ "survival", 2 ], [ "electronics", 2 ] ],
Expand All @@ -24,6 +25,7 @@
},
{
"type": "construction",
"id": "constr_hydro_heater",
"description": "Build Hydroponics Heater",
"category": "CONSTRUCT",
"required_skills": [ [ "fabrication", 4 ], [ "electronics", 3 ] ],
Expand Down
1 change: 1 addition & 0 deletions data/mods/Magiclysm/recipes/construction.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"type": "construction",
"id": "constr_magiclysm_translocator_gate",
"description": "Build Translocator Gate",
"category": "FURN",
"required_skills": [ [ "fabrication", 6 ], [ "spellcraft", 6 ] ],
Expand Down
2 changes: 2 additions & 0 deletions data/mods/blazemod/blaze_blob_construct.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[
{
"type": "construction",
"id": "constr_blaze_blob_pit",
"category": "OTHER",
"description": "Harvest Blob Feed from Corpse Pit: Smash to Harvest",
"difficulty": 0,
Expand All @@ -13,6 +14,7 @@
},
{
"type": "construction",
"id": "constr_blaze_blob_pit_slime",
"category": "OTHER",
"description": "Harvest Blob Feed from Slime: Smash to Harvest",
"difficulty": 0,
Expand Down
5 changes: 2 additions & 3 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3320,7 +3320,6 @@ void activity_handlers::churn_finish( player_activity *act, player *p )

void activity_handlers::build_do_turn( player_activity *act, player *p )
{
const std::vector<construction> &list_constructions = get_constructions();
partial_con *pc = g->m.partial_con_at( g->m.getlocal( act->placement ) );
// Maybe the player and the NPC are working on the same construction at the same time
if( !pc ) {
Expand All @@ -3336,8 +3335,8 @@ void activity_handlers::build_do_turn( player_activity *act, player *p )
return;
}
// if you ( or NPC ) are finishing someone elses started construction...
const construction &built = list_constructions[pc->id];
if( !p->meets_skill_requirements( built ) ) {
const construction &built = pc->id.obj();
if( !p->has_trait( trait_DEBUG_HS ) && !p->meets_skill_requirements( built ) ) {
add_msg( m_info, _( "%s can't work on this construction anymore." ), p->disp_name() );
p->cancel_activity();
if( p->is_npc() ) {
Expand Down
6 changes: 3 additions & 3 deletions src/activity_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ struct activity_reason_info {
//is it possible to do this
bool can_do;
//construction index
cata::optional<size_t> con_idx;
cata::optional<construction_id> con_idx;

activity_reason_info( do_activity_reason reason_, bool can_do_,
cata::optional<size_t> con_idx_ = cata::optional<size_t>() ) :
const cata::optional<construction_id> &con_idx_ = cata::nullopt ) :
reason( reason_ ),
can_do( can_do_ ),
con_idx( con_idx_ )
Expand All @@ -75,7 +75,7 @@ struct activity_reason_info {
}

static activity_reason_info build( const do_activity_reason &reason_, bool can_do_,
size_t con_idx_ ) {
const construction_id &con_idx_ ) {
return activity_reason_info( reason_, can_do_, con_idx_ );
}

Expand Down
30 changes: 14 additions & 16 deletions src/activity_item_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,11 +1024,11 @@ static activity_reason_info find_base_construction(
player &p,
const inventory &inv,
const tripoint &loc,
const cata::optional<size_t> &part_con_idx,
const size_t idx,
std::set<size_t> &used )
const cata::optional<construction_id> &part_con_idx,
const construction_id idx,
std::set<construction_id> &used )
{
const construction &build = list_constructions[idx];
const construction &build = idx.obj();
//already done?
const furn_id furn = g->m.furn( loc );
const ter_id ter = g->m.ter( loc );
Expand Down Expand Up @@ -1088,7 +1088,7 @@ static activity_reason_info find_base_construction(
//we can't immediately build it, looking for pre-req
used.insert( idx );
cata::optional<do_activity_reason> reason;
size_t pre_req_idx = 0;
construction_id pre_req_idx( -1 );
//first step: try only constructions with the same description
//second step: try all constructions
for( int try_num = 0; try_num < 2; ++try_num ) {
Expand Down Expand Up @@ -1487,7 +1487,7 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe
zones = mgr.get_zones( zone_type_CONSTRUCTION_BLUEPRINT,
g->m.getabs( src_loc ) );
const partial_con *part_con = g->m.partial_con_at( src_loc );
cata::optional<size_t> part_con_idx;
cata::optional<construction_id> part_con_idx;
if( part_con ) {
part_con_idx = part_con->id;
}
Expand All @@ -1497,13 +1497,13 @@ static activity_reason_info can_do_activity_there( const activity_id &act, playe
const inventory pre_inv = p.crafting_inventory( src_loc, PICKUP_RANGE - 1 );
for( const zone_data &zone : zones ) {
const blueprint_options options = dynamic_cast<const blueprint_options &>( zone.get_options() );
const int index = options.get_index();
const construction_id index = options.get_index();
if( !stuff_there.empty() ) {
return activity_reason_info::build( BLOCKING_TILE, false, static_cast<size_t>( index ) );
return activity_reason_info::build( BLOCKING_TILE, false, index );
}
std::set<size_t> used_idx;
std::set<construction_id> used_idx;
const activity_reason_info act_info = find_base_construction( list_constructions, p, pre_inv,
src_loc, part_con_idx, static_cast<size_t>( index ), used_idx );
src_loc, part_con_idx, index, used_idx );
return act_info;
}
} else if( act == ACT_MULTIPLE_FARM ) {
Expand Down Expand Up @@ -1846,7 +1846,7 @@ static std::vector<std::tuple<tripoint, itype_id, int>> requirements_map( player
}

static void construction_activity( player &p, const zone_data *zone, const tripoint &src_loc,
const activity_reason_info &act_info, const std::vector<construction> &list_constructions,
const activity_reason_info &act_info,
activity_id activity_to_restore )
{
const blueprint_options options = dynamic_cast<const blueprint_options &>( zone->get_options() );
Expand All @@ -1855,7 +1855,7 @@ static void construction_activity( player &p, const zone_data *zone, const tripo
debugmsg( "no construction selected" );
return;
}
const construction &built_chosen = list_constructions[*act_info.con_idx];
const construction &built_chosen = act_info.con_idx->obj();
std::list<item> used;
// create the partial construction struct
partial_con pc;
Expand Down Expand Up @@ -2512,7 +2512,6 @@ static bool generic_multi_activity_check_requirement( player &p, const activity_
{
const tripoint abspos = g->m.getabs( p.pos() );
zone_manager &mgr = zone_manager::get_manager();
const std::vector<construction> &list_constructions = get_constructions();

bool &can_do_it = act_info.can_do;
const do_activity_reason &reason = act_info.reason;
Expand Down Expand Up @@ -2573,7 +2572,7 @@ static bool generic_multi_activity_check_requirement( player &p, const activity_
return true;
}
// its a construction and we need the components.
const construction &built_chosen = list_constructions[ *act_info.con_idx ];
const construction &built_chosen = act_info.con_idx->obj();
what_we_need = built_chosen.requirements;
} else if( reason == NEEDS_VEH_DECONST || reason == NEEDS_VEH_REPAIR ) {
const vehicle *veh = veh_pointer_or_null( g->m.veh_at( src_loc ) );
Expand Down Expand Up @@ -2681,7 +2680,6 @@ static bool generic_multi_activity_do( player &p, const activity_id &act_id,
const tripoint &src, const tripoint &src_loc )
{
zone_manager &mgr = zone_manager::get_manager();
const std::vector<construction> &list_constructions = get_constructions();

const do_activity_reason &reason = act_info.reason;
const zone_data *zone = mgr.get_zone_at( src, get_zone_for_act( src_loc, mgr, act_id ) );
Expand Down Expand Up @@ -2729,7 +2727,7 @@ static bool generic_multi_activity_do( player &p, const activity_id &act_id,
p.activity.placement = src;
return false;
}
construction_activity( p, zone, src_loc, act_info, list_constructions, act_id );
construction_activity( p, zone, src_loc, act_info, act_id );
return false;
} else if( reason == CAN_DO_FETCH && act_id == ACT_TIDY_UP ) {
if( !tidy_activity( p, src_loc, act_id, ACTIVITY_SEARCH_DISTANCE ) ) {
Expand Down
29 changes: 17 additions & 12 deletions src/clzones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,26 +163,26 @@ bool zone_options::is_valid( const zone_type_id &type, const zone_options &optio
return !options.has_options();
}

int blueprint_options::get_final_construction(
construction_id blueprint_options::get_final_construction(
const std::vector<construction> &list_constructions,
int idx,
std::set<int> &skip_index
const construction_id &idx,
std::set<construction_id> &skip_index
)
{
const construction &con = list_constructions[idx];
const construction &con = idx.obj();
if( con.post_terrain.empty() ) {
return idx;
}

for( int i = 0; i < static_cast<int>( list_constructions.size() ); ++i ) {
if( i == idx || skip_index.find( i ) != skip_index.end() ) {
if( construction_id( i ) == idx || skip_index.find( construction_id( i ) ) != skip_index.end() ) {
continue;
}
const construction &con_next = list_constructions[i];
if( con.description == con_next.description &&
con.post_terrain == con_next.pre_terrain ) {
skip_index.insert( idx );
return get_final_construction( list_constructions, i, skip_index );
return get_final_construction( list_constructions, construction_id( i ), skip_index );
}
}

Expand All @@ -191,13 +191,13 @@ int blueprint_options::get_final_construction(

blueprint_options::query_con_result blueprint_options::query_con()
{
int con_index = construction_menu( true );
if( con_index > -1 ) {
construction_id con_index = construction_menu( true );
if( con_index.is_valid() ) {
const std::vector<construction> &list_constructions = get_constructions();
std::set<int> skip_index;
std::set<construction_id> skip_index;
con_index = get_final_construction( list_constructions, con_index, skip_index );

const construction &chosen = list_constructions[con_index];
const construction &chosen = con_index.obj();

const std::string &chosen_desc = chosen.description;
const std::string &chosen_mark = chosen.post_terrain;
Expand Down Expand Up @@ -384,14 +384,19 @@ void blueprint_options::serialize( JsonOut &json ) const
{
json.member( "mark", mark );
json.member( "con", con );
json.member( "index", index );
json.member( "index", index.id() );
}

void blueprint_options::deserialize( const JsonObject &jo_zone )
{
jo_zone.read( "mark", mark );
jo_zone.read( "con", con );
jo_zone.read( "index", index );
if( jo_zone.has_int( "index" ) ) {
// Oops, saved incorrectly as an int id by legacy code. Just load it and hope for the best
index = construction_id( jo_zone.get_int( "index" ) );
} else {
index = construction_str_id( jo_zone.get_string( "index" ) ).id();
}
}

void plot_options::serialize( JsonOut &json ) const
Expand Down
10 changes: 5 additions & 5 deletions src/clzones.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class blueprint_options : public zone_options, public mark_option
// furn/ter id as string.
std::string mark;
std::string con;
int index;
construction_id index;

enum query_con_result {
canceled,
Expand All @@ -168,18 +168,18 @@ class blueprint_options : public zone_options, public mark_option
std::string get_con() const {
return con;
}
int get_index() const {
construction_id get_index() const {
return index;
}

bool has_options() const override {
return true;
}

int get_final_construction(
construction_id get_final_construction(
const std::vector<construction> &list_constructions,
int idx,
std::set<int> &skip_index );
const construction_id &idx,
std::set<construction_id> &skip_index );

bool query_at_creation() override;
bool query() override;
Expand Down
Loading

0 comments on commit 78e6e40

Please sign in to comment.