forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
craft_command.h
127 lines (106 loc) · 4.05 KB
/
craft_command.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#ifndef CATA_SRC_CRAFT_COMMAND_H
#define CATA_SRC_CRAFT_COMMAND_H
#include <string>
#include <vector>
#include "point.h"
#include "recipe.h"
#include "requirements.h"
#include "type_id.h"
class JsonIn;
class JsonOut;
class inventory;
class item;
class player;
template<typename T> struct enum_traits;
/**
* enum used by comp_selection to indicate where a component should be consumed from.
*/
enum usage {
use_from_none = 0,
use_from_map = 1,
use_from_player = 2,
use_from_both = 1 | 2,
cancel = 4, // FIXME: hacky.
num_usages
};
template<>
struct enum_traits<usage> {
static constexpr usage last = usage::num_usages;
};
/**
* Struct that represents a selection of a component for crafting.
*/
template<typename CompType>
struct comp_selection {
comp_selection() = default;
comp_selection( usage use_from, const CompType &comp = CompType() )
: use_from( use_from ), comp( comp )
{}
comp_selection( const comp_selection & ) = default;
comp_selection &operator = ( const comp_selection & ) = default;
/** Tells us where the selected component should be used from. */
usage use_from = use_from_none;
CompType comp;
/** provides a translated name for 'comp', suffixed with it's location e.g '(nearby)'. */
std::string nname() const;
void serialize( JsonOut &jsout ) const;
void deserialize( JsonIn &jsin );
};
/**
* Class that describes a crafting job.
*
* The class has functions to execute the crafting job.
*/
class craft_command
{
public:
/** Instantiates an empty craft_command, which can't be executed. */
craft_command() = default;
craft_command( const recipe *to_make, int batch_size, bool is_long, player *crafter,
const tripoint &loc = tripoint_zero ) :
rec( to_make ), batch_size( batch_size ), longcraft( is_long ), crafter( crafter ), loc( loc ) {}
/** Selects components to use for the craft, then assigns the crafting activity to 'crafter'. */
void execute( const tripoint &new_loc = tripoint_zero );
/**
* Consumes the selected components and returns the resulting in progress craft item.
* Must be called after execute().
*/
item create_in_progress_craft();
bool is_long() const {
return longcraft;
}
bool has_cached_selections() const {
return !item_selections.empty() || !tool_selections.empty();
}
bool empty() const {
return rec == nullptr;
}
skill_id get_skill_id();
private:
const recipe *rec = nullptr;
int batch_size = 0;
/**
* Indicates whether the player has initiated a one off craft or wishes to craft as
* long as possible.
*/
bool longcraft = false;
// This is mainly here for maintainability reasons.
player *crafter;
recipe_filter_flags flags = recipe_filter_flags::none;
// Location of the workbench to place the item on
// zero_tripoint indicates crafting without a workbench
tripoint loc = tripoint_zero;
std::vector<comp_selection<item_comp>> item_selections;
std::vector<comp_selection<tool_comp>> tool_selections;
/** Checks if tools we selected in a previous call to execute() are still available. */
std::vector<comp_selection<item_comp>> check_item_components_missing(
const inventory &map_inv ) const;
/** Checks if items we selected in a previous call to execute() are still available. */
std::vector<comp_selection<tool_comp>> check_tool_components_missing(
const inventory &map_inv ) const;
/** Creates a continue pop up asking to continue crafting and listing the missing components */
bool query_continue( const std::vector<comp_selection<item_comp>> &missing_items,
const std::vector<comp_selection<tool_comp>> &missing_tools );
};
#endif // CATA_SRC_CRAFT_COMMAND_H