-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestGenerator.js
258 lines (205 loc) · 7.88 KB
/
QuestGenerator.js
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
/**
* Created by Dean on 6/21/2015.
*/
var QUESTIFY = (function (QUESTIFY) {
function createQuestGenerator(tagSplitter) {
var that = {},
motivations = {},
strategies = {},
atomicActions = {},
compoundActions = {},
conditionFunctions = {},
onParseActions = {};
tagSplitter = tagSplitter || ':';
function selectMotivation(subjectNPC) {
var index = Math.floor(Math.random() * subjectNPC.motivations.length);
return subjectNPC.motivations[index];
}
function defineVariable(arg, selectedObjects, entities) {
var pieces = arg.split(tagSplitter),
piece,
arrString,
id,
currentArr,
argObject = entities;
for (var p = 0, pl = pieces.length; p < pl; p++) {
piece = pieces[p];
//We have an array selection to make
if (piece.charAt(0) === "[") {
arrString = piece.slice(1, -1);
//increment p so we can get the id value
p += 1;
id = pieces[p];
//Remove single quotes on the id
if (id.charAt(0) === "'") {
id = id.slice(1, -1);
} else {
//We want to force users to use the quotes to make sure we
//can clearly understand their intent here
throw new Error(id + " is not a valid id identifier and follows an array.");
}
if (selectedObjects.hasOwnProperty(id)) {
argObject = selectedObjects[id];
} else {
if (!argObject.hasOwnProperty(arrString)) {
throw new Error(argObject + " does not have a property of " + arrString);
}
currentArr = argObject[arrString];
//Get a random value in the array. Add to selected values to track it.
argObject = currentArr[Math.floor(Math.random() * currentArr.length)];
selectedObjects[id] = argObject;
}
}
//We have a property access
else {
id = piece;
if (!argObject.hasOwnProperty(id)) {
throw new Error(argObject + " does not have a property of " + id);
}
argObject = argObject[id];
}
}
return argObject;
}
function parseArgument(arg, variablesObj) {
var tag, prop, argPieces, argObject;
argPieces = arg.split(tagSplitter);
tag = argPieces[0];
if (!variablesObj.hasOwnProperty(tag)) {
throw new Error(tag + " was not defined for this strategy");
}
argObject = variablesObj[tag];
//Loop through the property chain
for (var p = 1, pl = argPieces.length; p < pl; p++) {
prop = argPieces[p];
if (!argObject.hasOwnProperty(prop)) {
throw new Error(argObject + " does not have the property " + prop);
}
argObject = argObject[prop];
}
return argObject;
}
function parseActionArgs(actionArgs, variablesObj) {
var actionArgsAsObjects = [],
conditionArgsAsObjects = [],
currentConditionArgsArr = [],
currentArgString = "";
//Loops through the conditions for the action
for (var c = 0, cl = actionArgs.length; c < cl; c++) {
currentConditionArgsArr = actionArgs[c];
conditionArgsAsObjects = [];
//Loop through the arguments to each condition
for (var a = 0, al = currentConditionArgsArr.length; a < al; a++) {
currentArgString = currentConditionArgsArr[a];
conditionArgsAsObjects.push(parseArgument(currentArgString, variablesObj));
}
actionArgsAsObjects.push(conditionArgsAsObjects);
}
return actionArgsAsObjects;
}
function parseAndExecuteParseAction(parseActionStringArr, variablesObj) {
if (!(parseActionStringArr[0] in onParseActions)) {
throw new Error("The generation action '" + parseActionStringArr[0] + "' does not exist!");
}
var parseAction = onParseActions[parseActionStringArr[0]],
argString,
args = [];
for (var g = 1, gl = parseActionStringArr.length; g < gl; g++) {
argString = parseActionStringArr[g];
args.push(parseArgument(argString, variablesObj));
}
parseAction.apply(this, args);
}
function parseDescription(descriptionStr, variablesObj) {
var openBracketIndex = 0, closedBracketIndex = 0;
for (var i = 0, il = descriptionStr.length; i < il; i++) {
if (descriptionStr.charAt(i) === '[') {
openBracketIndex = i;
} else if (descriptionStr.charAt(i) === ']') {
closedBracketIndex = i;
if (closedBracketIndex < openBracketIndex) {
throw new Error("'" + descriptionStr + "' is not a valid description string." +
" A closed bracket was found before a matching opened bracket.");
}
var argumentString = descriptionStr.substring(openBracketIndex + 1,
closedBracketIndex);
//Regex matches all text between brackets
descriptionStr = descriptionStr.replace(/\[([^]+)\]/,
parseArgument(argumentString, variablesObj));
}
}
return descriptionStr;
}
function parseTagMapping(tagMapping, variablesObj) {
var pieces = tagMapping.split(":"),
parentTag = pieces[0],
childTag = pieces[1];
if (pieces.length !== 2) {
console.log("Expected to see two parts in the tag mapping " + tagMapping);
}
if (!variablesObj.hasOwnProperty(parentTag)) {
console.log("The parent tag of '" + parentTag + "' was not defined for this strategy");
}
variablesObj[childTag] = variablesObj[parentTag];
}
function processStrategy(strategy, variablesObj, actions, argsArr, descriptions) {
var currentActionObject, actionString, actionArgs, s, sl;
//For every action in the strategy, get it's action function and the arguments to that action
for (s = 0, sl = strategy.actionsObjects.length; s < sl; s++) {
currentActionObject = strategy.actionsObjects[s];
if (currentActionObject.hasOwnProperty('atomicAction')) {
//Add the current action for this quest
actionString = currentActionObject.atomicAction;
actions.push(atomicActions[actionString]);
//Process actionArgs
if (!currentActionObject.hasOwnProperty('actionArgs')) {
throw new Error(actionString + " wasn't given any arguments to pass to it's condition functions!");
} else {
//Now add it's arguments
actionArgs = currentActionObject['actionArgs'];
argsArr.push(parseActionArgs(actionArgs, variablesObj));
}
//Process onParseAction's
if (currentActionObject.hasOwnProperty('onParseAction')) {
parseAndExecuteParseAction(currentActionObject['onParseAction'], variablesObj);
}
//Process descriptions
if (currentActionObject.hasOwnProperty('description')) {
descriptions.push(parseDescription(currentActionObject['description'], variablesObj));
}
} else if (currentActionObject.hasOwnProperty("strategy")) {
if (!currentActionObject.hasOwnProperty("tagMapping")) {
throw new Error(currentActionObject + " does not have a tag mapping for a sub-strategy");
}
parseTagMapping(currentActionObject["tagMapping"], variablesObj);
processStrategy(strategies[currentActionObject["strategy"]], variablesObj, actions, argsArr, descriptions);
}
}
}
that.motivations = motivations;
that.strategies = strategies;
that.atomicActions = atomicActions;
that.compoundActions = compoundActions;
that.conditionFunctions = conditionFunctions;
that.onParseActions = onParseActions;
that.generateQuest = function(player, subjectNPC, entities, forcedStrategy) {
var motivation = selectMotivation(subjectNPC),
strategy = forcedStrategy || motivation.selectStrategy(),
variableDefinitions = strategy.variableDefinitions,
actions = [],
argsArr = [],
descriptions = [],
//Initialize variablesObj with Questify's predefined quest variables
variablesObj = {pc: player, giver: subjectNPC, start: subjectNPC.location};
//Parse variable definitions
for (var v = 0, vl = variableDefinitions.length; v < vl; v++) {
defineVariable(variableDefinitions[v], variablesObj, entities);
}
processStrategy(strategy, variablesObj, actions, argsArr, descriptions);
return QUESTIFY.createQuest(strategy, actions, argsArr, descriptions);
};
return that;
}
QUESTIFY.createQuestGenerator = createQuestGenerator;
return QUESTIFY;
}(QUESTIFY || {}));