Skip to content

Commit

Permalink
Add optional uncraft time to recipes (CleverRaven#54761)
Browse files Browse the repository at this point in the history
* Add optional `uncraft_time` to recipes

This allows for recipes that might take a long time to create, but a
short time to disassemble.

* Update reversible documentation in JSON_INFO.md
  • Loading branch information
pjf authored Jan 25, 2022
1 parent 9dbadbd commit 648f7c7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
2 changes: 1 addition & 1 deletion data/json/recipes/armor/head.json
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,7 @@
"subcategory": "CSC_ARMOR_HEAD",
"skill_used": "fabrication",
"time": "30 m",
"reversible": true,
"reversible": { "time": "6 s" },
"autolearn": true,
"components": [ [ [ "rag", 4 ] ], [ [ "pot", 1 ] ] ],
"flags": [ "BLIND_HARD" ]
Expand Down
4 changes: 2 additions & 2 deletions data/json/recipes/other/parts.json
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@
"skill_used": "fabrication",
"difficulty": 1,
"time": "2 h",
"reversible": true,
"reversible": { "time": "20 m" },
"autolearn": true,
"using": [ [ "sewing_standard", 50 ], [ "welding_standard", 5 ] ],
"proficiencies": [
Expand All @@ -744,7 +744,7 @@
"skill_used": "fabrication",
"difficulty": 1,
"time": "6 h",
"reversible": true,
"reversible": { "time": "20 m" },
"autolearn": true,
"using": [ [ "sewing_standard", 50 ], [ "welding_standard", 5 ] ],
"proficiencies": [
Expand Down
3 changes: 2 additions & 1 deletion doc/JSON_INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,8 @@ Crafting recipes are defined as a JSON object with the following fields:
"difficulty": 3, // Difficulty of success check
"time": "5 m", // Preferred time to perform recipe, can specify in minutes, hours etc.
"time": 5000, // Legacy time to perform recipe (where 1000 ~= 10 turns ~= 10 seconds game time).
"reversible": false, // Can be disassembled.
"reversible": true, // Can be disassembled. Time taken is as long as to craft the item.
"reversible": { "time": "30 s" }, // Can be disassembled. Time to disassemble as specified.
"autolearn": true, // Automatically learned upon gaining required skills
"autolearn" : [ // Automatically learned upon gaining listed skills
[ "survival", 2 ],
Expand Down
10 changes: 9 additions & 1 deletion src/recipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,15 @@ void recipe::load( const JsonObject &jo, const std::string &src )
mandatory( jo, was_loaded, "category", category );
mandatory( jo, was_loaded, "subcategory", subcategory );
assign( jo, "description", description, strict );
assign( jo, "reversible", reversible, strict );

if( jo.has_bool( "reversible" ) ) {
assign( jo, "reversible", reversible, strict );
} else if( jo.has_object( "reversible" ) ) {
reversible = true;
// Convert duration to time in moves
uncraft_time = to_moves<int>( read_from_json_string<time_duration>
( jo.get_object( "reversible" ).get_member( "time" ), time_duration::units ) );
}

if( jo.has_member( "byproducts" ) ) {
if( this->reversible ) {
Expand Down
3 changes: 3 additions & 0 deletions src/recipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ class recipe
/** Can recipe be used for disassembly of @ref result via @ref disassembly_requirements */
bool reversible = false;

/** Time (in moves) to disassemble if different to assembly. Requires `reversible = true` */
int64_t uncraft_time = 0;

/** What does the item spawn contained in? Unset ("null") means default container. */
itype_id container = itype_id::NULL_ID();

Expand Down
18 changes: 13 additions & 5 deletions src/recipe_dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ void recipe_dictionary::finalize()
finalize_internal( recipe_dict.recipes );
finalize_internal( recipe_dict.uncraft );

for( auto &e : recipe_dict.recipes ) {
auto &r = e.second;
for( const auto &e : recipe_dict.recipes ) {
const recipe &r = e.second;

if( r.obsolete ) {
continue;
Expand All @@ -538,8 +538,16 @@ void recipe_dictionary::finalize()
}

// if reversible and no specific uncraft recipe exists use this recipe
if( r.is_reversible() && !recipe_dict.uncraft.count( recipe_id( r.result().str() ) ) ) {
recipe_dict.uncraft[ recipe_id( r.result().str() ) ] = r;

const string_id<recipe> uncraft_id = recipe_id( r.result().str() );
if( r.is_reversible() && !recipe_dict.uncraft.count( uncraft_id ) ) {
recipe_dict.uncraft[ uncraft_id ] = r;

if( r.uncraft_time > 0 ) {
// If a specified uncraft time has been given, use that in the uncraft
// recipe rather than the original.
recipe_dict.uncraft[ uncraft_id ].time = r.uncraft_time;
}
}
}

Expand All @@ -551,7 +559,7 @@ void recipe_dictionary::finalize()
// books that don't already have an uncrafting recipe
if( e->book && !recipe_dict.uncraft.count( rid ) && e->volume > 0_ml ) {
int pages = e->volume / 12.5_ml;
auto &bk = recipe_dict.uncraft[rid];
recipe &bk = recipe_dict.uncraft[rid];
bk.ident_ = rid;
bk.result_ = id;
bk.reversible = true;
Expand Down

0 comments on commit 648f7c7

Please sign in to comment.