-
Notifications
You must be signed in to change notification settings - Fork 0
/
quests.h
executable file
·291 lines (248 loc) · 8.44 KB
/
quests.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef QUESTS_H_
#define QUESTS_H_
// Ksilyan, 2006-01-28
#include <vector>
#include "shared_str.h"
#include "java_iterator.hpp"
extern "C" {
#include <lua.h>
}
// Forward declaration
class QuestManager;
// TODO: add:
// - list of areas
/* Class: Quest
*
* A quest definition has the following components:
*
* - Name: unique string identifying this quest. (for builder identification)
* - Title: one-line name of the quest (for display to players)
* - Author: one-line string for who wrote the quest
* - Summary: one-line summary of the quest. Meant mainly for players.
* Optional.
* - Notes: notes about the quest, typically set by builders. Used to write
* comments about quests; for instance, what mobs are involved in
* the quest. Optional. Multi-line.
* - Steps: a table of steps, keyed by their name, each defining:
* - description: Description of what the step's goals are (for display
* to players). Multi-line.
*
* Additionally, the following properties exist but are not user-editable:
* - date_created: The date the quest was created (sec. since Epoch)
* - date_modified: The date the quest was last modified (sec. since Epoch)
*
* The following step names are reserved:
* - success: the player completed the quest successfully
* - failure: the player failed the quest
* - terminal: the player ended the quest with a "neutral" outcome (e.g.,
* aborted the quest)
*
* Step names "success" and "failure" are reserved. They mean, respectively,
* that the quest was completed succesfully, and that the player failed the
* quest. There is no need for quest builders to create or define these steps.
* It is up to the builder to transition to these states. It is also up to the
* builder to decide if one can restart the quest from either of these states.
*/
class Quest
{
public:
struct Step
{
shared_str name_;
shared_str description_;
};
// Default constructor
Quest() { /* empty */ }
// Constructor
Quest(const shared_str & name, const shared_str & title,
const shared_str & author, const std::vector<Step> & steps,
time_t dateCreated, time_t dateModified);
void setName(const shared_str & name) { name_ = name; }
const shared_str & getName() const { return name_; }
void setTitle(const shared_str & title) { title_ = title; }
const shared_str & getTitle() const { return title_; }
void setAuthor(const shared_str & author) { author_ = author; }
const shared_str & getAuthor() const { return author_; }
void setSummary(const shared_str & summary) { summary_=summary; }
const shared_str & getSummary() const { return summary_; }
void setNotes(const shared_str & notes) { notes_ = notes; }
const shared_str & getNotes() const { return notes_; }
void setSteps(const std::vector<Step> & steps) { steps_ = steps; }
const std::vector<Step> & getSteps() const { return steps_; }
void setDateCreated(time_t t) { dateCreated_ = t; }
time_t getDateCreated() const { return dateCreated_; }
void setDateModified(time_t t) { dateModified_ = t; }
time_t getDateModified() const { return dateModified_; }
/* Method: getStep
*
* Check a quest for a step by name; return that step if found,
* or null if not found.
*
* Parameters:
* name - The name of the step to look up.
*
* Returns:
* A pointer to the step if found, or NULL if not found.
*/
const Step * getStep(const shared_str & name);
friend class QuestManager;
protected:
shared_str name_;
shared_str title_;
shared_str author_;
shared_str summary_;
shared_str notes_;
std::vector<Step> steps_;
time_t dateCreated_;
time_t dateModified_;
};
/* Class: QuestManager
*
* Manages the game's quest definitions.
*
*
* Implementation note: the Lua state is owned by the World. We should not
* leave stuff lying around on the stack, assuming it will be there the
* next time we enter a public method. All entry points should push the
* quest manager table, then pop it when they're done.
*/
class QuestManager
{
public:
/* Constructor: QuestManager
*
* Initializes Lua state.
*/
QuestManager();
/* Destructor: ~QuestManager
*
* Closes Lua state.
*/
~QuestManager();
/* Method: initialize
*
* Initialize quest database from a Lua quest file. If
* successful, pushes the quest table onto the stack.
*
* Parameters:
* questFile - path to the file to read from
*
* Returns:
* True if successful, false on failure.
*/
bool initialize(const std::string & questFile);
/* Method: save
*
* Save the quest table to the specified file.
*
* Parameters:
* questFile - path to the file to save
*
* Returns:
* True if successful, false on failure.
*/
bool save(const std::string & questFile);
/* Method: hasQuest
*
* Returns true if a quest exists.
*
* Parameters:
* name - The name of the quest to check for.
*
* Returns:
* True if the quest exists; false otherwise.
*/
bool hasQuest(const shared_str & name);
/* Method: getQuest
*
* Get a quest's data by name.
*
* Parameters:
* name - The name of the quest to look up.
* quest - The quest object to fill with the information, if
* the quest exists.
*
* Returns:
*
* True if the quest was found, and false if it doesn't exist.
*/
bool getQuest(const shared_str & name, Quest & quest);
/* Method: getQuestIterator
*
* Construct an iterator for the quests.
*
* Returns:
*
* An iterator ranging over defined quests.
*/
Iterator<Quest> getQuestIterator();
/* Method: getQuestNamesIterator
*
* Construct an iterator for the quest names.
*
* Returns:
*
* An iterator ranging over the quest names.
*/
Iterator<shared_str> getQuestNamesIterator();
/* Method: interpret
*
* Interpret an arbitrary Lua string in the quest environment.
*
* Parameters:
* code - The Lua code to interpret.
*
* Returns:
* True if the interpretation succeeded (i.e. no error was
* returned); false otherwise.
*/
bool interpret(const std::string & code);
static const char * QUESTS_TABLE_NAME;
protected:
// Variable: luaState_
//
// The Lua state global to the world.
//
// NOTE:
// We do not own this pointer!!
lua_State * luaState_;
/* Method: findQuest
*
* Look up a quest in the global quest table.
*
* If the quest is found, its table is put onto the stack. If
* the quest isn't found, nothing happens, and the function
* returns false.
*
* Note:
* This function assumes that the quest table is already on the
* top of the stack.
*
* Parameters:
* name - The name of the quest to look up.
*
* Returns:
* True if the quest was found; false otherwise.
*/
bool findQuest(const shared_str & name);
/* Method: fillQuestObject
*
* Fills up a quest object from a quest table.
*
* Assumes that the quest table is on the top of the stack
* already.
*
* Parameters:
* quest - The quest object (by reference) to fill.
*/
void fillQuestObject(Quest & quest);
/* Method: getStepObjects
*
* Take a Lua table (array) of steps, and convert to a vector.
*
* Assumes that the steps table is on the top of the stack
* already.
*/
void getStepObjects(std::vector<Quest::Step> & steps);
};
#endif // include guard