summaryrefslogtreecommitdiff
path: root/src/include/tome/squelch/rule.hpp
blob: 63f1b6c0ce19c62fd973a373de799f7881f2902a (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma once

#include <jansson.h>
#include <memory>

#include "tome/squelch/condition_fwd.hpp"
#include "tome/squelch/cursor_fwd.hpp"
#include "tome/squelch/tree_printer_fwd.hpp"
#include "tome/enum_string_map.hpp"
#include "../object_type_fwd.hpp"

namespace squelch {

/**
 * Types of automatizer actions: destroy, pick up, and inscribe.
 */
enum class action_type { AUTO_DESTROY, AUTO_PICKUP, AUTO_INSCRIBE };

/**
 * Bidirectional map between rule actions and strings.
 */
EnumStringMap<action_type> &action_mapping();

/**
 * Rules are the representation of "when condition X is true, do Y".
 */
class Rule : public boost::noncopyable
{
public:
	Rule(std::string name,
	     action_type action,
	     int module_idx,
	     std::shared_ptr<Condition> condition)
		: m_name(name)
		, m_action(action)
		, m_module_idx(module_idx)
		, m_condition(condition) {
	}

	/**
	 * Set the name of the rule
	 */
	void set_name(const char *new_name);

	/**
	 * Get the name of the rule
	 */
	const char *get_name() const;

	/**
	 * Get condition
	 */
	std::shared_ptr<Condition> get_condition() const;

	/**
	 * Add new condition using a factory to instantiate the
	 * condition only if the rule permits adding a condition.
	 */
	void add_new_condition(Cursor *cursor,
			       ConditionFactory const &factory);

	/**
	 * Remove currently selected condition
	 */
	void delete_selected_condition(Cursor *cursor);

	/**
	 * Write out tree representing rule
	 */
	void write_tree(TreePrinter *p, Cursor *cursor) const;

	/**
	 * Apply rule to object
	 */
	bool apply_rule(object_type *o_ptr, int item_idx) const;

	/**
	 * Convert rule to JSON
	 */
	virtual json_t *to_json() const;

	/**
	 * Parse rule from JSON
	 */
	static std::shared_ptr<Rule> parse_rule(json_t *);

protected:
	virtual bool do_apply_rule(object_type *, int) const = 0;
	virtual void do_write_tree(TreePrinter *p) const = 0;

protected:
	// Rule name
	std::string m_name;
	// What does the rule do?
	action_type m_action;
	// Which module does this rule apply to?
	int m_module_idx;
	// Condition to check
	std::shared_ptr<Condition> m_condition;
};

/**
 * Rule for destroying matching objects
 */
class DestroyRule : public Rule
{
public:
	DestroyRule(std::string name,
		    int module_idx,
		    std::shared_ptr<Condition> condition)
		: Rule(name, action_type::AUTO_DESTROY, module_idx, condition) {
	}

protected:
	virtual bool do_apply_rule(object_type *o_ptr, int item_idx) const override;
	virtual void do_write_tree(TreePrinter *p) const override;
};

/**
 * Rule for picking up matching objects
 */
class PickUpRule : public Rule
{
public:

	PickUpRule(std::string name,
		   int module_idx,
		   std::shared_ptr<Condition> condition)
		: Rule(name, action_type::AUTO_PICKUP, module_idx, condition) {
	}

protected:
	virtual void do_write_tree(TreePrinter *p) const override;
	virtual bool do_apply_rule(object_type *o_ptr, int item_idx) const override;
};

/**
 * Rule for inscribing matching objects
 */
class InscribeRule : public Rule
{
public:
	InscribeRule(std::string name,
		     int module_idx,
		     std::shared_ptr<Condition> condition,
		     std::string inscription)
		: Rule(name, action_type::AUTO_INSCRIBE, module_idx, condition)
		, m_inscription(inscription) {
	}

	json_t *to_json() const override;

protected:
        virtual void do_write_tree(TreePrinter *p) const override;
        virtual bool do_apply_rule(object_type *o_ptr, int) const override;

private:
	// Inscription to use for inscription rules.
	std::string m_inscription;
};

} // namespace