-
Notifications
You must be signed in to change notification settings - Fork 6
/
TwineQuest.twee
333 lines (304 loc) · 10.7 KB
/
TwineQuest.twee
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
::TwineQuest.twee About [Twine.private]
LICENSE: Public Domain
PURPOSE:
This is the core TwineQuest file. It contains things that are common to
all TwineQuest plugins. The purpose is to:
1) Bootstrap the TwineQuest system
2) Add TwineQuest support to the wikifier
3) Provide utility functions useful to TwineQuest plugins
3) Register TwineQuest plugins from the PassageStore
4) Send events to TwineQuest plugins: init, start
5) Built in plugins with near universal appeal tq.vars, tq.object
TwineQuest plugins are optional. They should be as optional and rely on
each other as little as practicable.
USAGE:
You will need a recent alpha version of v1.3.5 or v1.3.6 of Twine or
later.
Create a passage with the title "StoryIncludes"
List this file (with relative path) on it's own line inside this passage
(You may list file with an http url too)
List the filenames of any TwineQuest plugins that you wish to use inside
the StoryIncludes passage. One per line.
CONTRIBUTORS:
(Add your name as you contribute)
Henry Soule
Emmanuel King Turner (eturnerx / stormrose / Et) http://eturnerx.com
::TwineQuest Core Macro [script tq.core]
try {
// Bootstrap the TwineQuest System
version.extensions['TwineQuestSystem'] = { major:0, minor:1, revision:0 };
var TwineQuest = {
plugins: [], // TwineQuest plugins
tags: {}, // The passage tags that TwineQuest plugins handle
labels: {}, // The {passagetext} that TwineQuest responds to
};
// Add TwineQuest support to the wikifier
Wikifier.formatters.push({
name: 'TwineQuest',
match: '{',
handler: function(w) {
var s = w.source.substr(w.matchStart);
if(s.length > 2 && s.indexOf('}') > 0) {
s = s.substr(1,s.indexOf('}')-1);
w.nextMatch = w.matchStart + s.length + 2;
if(s.length == 0) {
s = ['_'];
} else {
s = s.split(' ');
}
var label = s.shift();
if(TwineQuest.labels[label] && typeof TwineQuest.labels[label].handler == 'function') {
TwineQuest.labels[label].handler(
w.output, label, s, w,
(TwineQuest.labels[label].data ? TwineQuest.labels[label].data : null),
(TwineQuest.labels[label].plugin ? TwineQuest.labels[label].plugin : null)
);
} else {
var label1 = (label.indexOf('$') == 0 ? label.substr(1) : label);
var branches = label1.split('.');
branches = branches.concat(s);
var node = state.history[0].variables;
var traverse = true;
var newnode;
var i = branches.length;
while(traverse == true && i--) {
newnode = branches.shift();
if(node[newnode] != null) {
node = node[newnode];
} else {
traverse = false;
}
}
if(traverse) {
insertElement(w.output,'span',null,'tq_print_var',node);
} else {
insertElement(w.output,'span',null,'marked',label + (s.length > 0 ? ' ' : '') + s.join(' '));
}
}
} else {
insertElement(w.output,'span',null,'marked','TwineQuest:Opening Curly Brace with no match closing curly brace:');
}
}
});
// Provide utility functions useful to TwineQuest plugins
//isNumber() from: http://stackoverflow.com/a/1830844
TwineQuest.isNumber = function(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
};
TwineQuest.registerPlugin = function(a) {
if(a != null) {
if(a.priority == null) {
a.priority = 1000;
}
this.plugins.push(a);
this.plugins.sort(function(a,b){ return a.priority - b.priority; });
}
};
TwineQuest.registerLabel = function(label, xhandler, xdata, xplugin) {
this.labels[label] = { handler: xhandler, data: xdata, plugin: xplugin };
};
TwineQuest.findPluginById = function(id) {
var r = null;
var i = this.plugins.length - 1;
do {
if(this.plugins[i].id == id) {
r = this.plugins[i];
break;
}
} while(i--);
return r;
};
TwineQuest.acnToObject = function(text) {
var r = {};
var kv = [];
var t = null;
var state = 0; //0 looking for var, 1 adding to value, 2 lastlineblank
var _numvar = function(t) {
var r = t;
if(TwineQuest.isNumber(t)) {
t = parseFloat(r);
if(!isNaN(t)) {
r = t;
} else {
t = parseInt(r);
if(!isNaN(t)) {
r = t;
}
}
}
return r;
}
var lines = text.split(String.fromCharCode(10));
for(var i in lines) {
var line = lines[i];
switch(state) {
case 0:
if(line.indexOf(':')>-1) {
kv = line.split(':');
kv[0] = kv[0].trim();
kv[1] = kv[1].trim();
if(kv[1].length > 0) {
r[kv[0]] = _numvar(kv[1]);
} else {
state = 1;
}
}
break;
case 1:
line = line.trim();
if(line.length == 0) {
state = 2;
kv[1] = kv[1] + String.fromCharCode(10);
} else {
kv[1] = kv[1] + line + String.fromCharCode(10);
}
break;
case 2:
line = line.trim();
var icolon = line.indexOf(':');
var ispace = line.indexOf(' ');
if(icolon > 0 && (ispace < 0 || ispace >= icolon - 1)) {
r[kv[0]] = _numvar(kv[1].trim());
kv = line.split(':');
kv[0] = kv[0].trim();
kv[1] = kv[1].trim();
if(kv[1].length > 0) {
r[kv[0]] = kv[1];
state = 0;
} else {
state = 1;
}
} else {
kv[1] = kv[1] + line + String.fromCharCode(10);
if(line.length > 0) {
state = 1;
}
}
break;
}
}
if(state != 0) {
r[kv[0]] = _numvar(kv[1].trim());
}
return r;
};
TwineQuest.initPlugins = function() {
for(var ip = 0; ip < this.plugins.length; ip++) {
var a = this.plugins[ip];
if(a != null) {
if(typeof a.onInit == 'function') {
a.onInit();
}
if(typeof a.onEvent == 'function') {
a.onEvent('init',null);
}
}
}
};
TwineQuest.tagFindPlugins = function() {
var ip = this.plugins.length - 1;
do {
var a = this.plugins[ip];
if(a && a.tag && (typeof a.onPassage == 'function' || typeof a.onEvent == 'function')) {
var p = tale.lookup('tags', 'tq.' + a.tag);
for(var i = 0; i < p.length; i++) {
if(typeof a.onPassage == 'function') {
a.onPassage(p[i]);
}
if(typeof a.onEvent == 'function') {
a.onEvent('passage', p[i]);
}
}
}
} while(ip--);
};
// Built in plugins with near universal appeal
// Automatically set twine variables from tagged passages at story start
TwineQuest.registerPlugin({
id: 'TwineQuest.AutoVars',
version: '0.1.0',
tag: 'vars',
onPassage: function(passage) {
if(passage && passage.text) {
var t = TwineQuest.acnToObject(passage.text);
for(var i in t) {
this._internalstore[i] = t[i];
}
}
},
onStart: function() {
for(var i in this._internalstore) {
state.history[0].variables[i] = this._internalstore[i];
}
},
_internalstore: {},
});
// Automatically set twine objects from tagged passages at story start
TwineQuest.registerPlugin({
id: 'TwineQuest.AutoObject',
version: '0.1.0',
tag: 'object',
onPassage: function(passage) {
var tid;
if(passage && passage.text) {
var t = TwineQuest.acnToObject(passage.text);
tid = (t.id == null ? passage.title : t.id);
if(t.type == null) {
t.type = 'tq.object';
}
this._internalstore[tid] = t;
}
},
onStart: function() {
for(var i in this._internalstore) {
state.history[0].variables[i] = Object.create(this._internalstore[i]);
}
},
_internalstore: {},
});
// Register TwineQuest plugins from the PassageStore
var p = tale.lookup('tags','tq.system.plugin');
for(var tqi=0;tqi<p.length;tqi++) {
try {
var TwineQuestPlugin;
eval('TwineQuestPlugin = ' + p[tqi].text);
if(TwineQuestPlugin && TwineQuestPlugin.id == null) {
TwineQuestPlugin.id = p[tqi].title;
}
TwineQuest.registerPlugin(TwineQuestPlugin);
} catch(e) {
throwError(place, 'Error registering TwineQuest plugin ' + p[tqi].title + ': ' + e.toString());
}
}
try {
TwineQuest.initPlugins();
} catch(e) {
throwError(place, 'Error initialising TwineQuest plugins: ' + e.toString());
}
try {
TwineQuest.tagFindPlugins();
} catch(e) {
throwError(place, 'Error creating data from passages for TwineQuest plugins: ' + e.toString());
}
// Start the TwineQuest system
// Send start events to TwineQuest plugins
// Uses a macro so that start events are resent if the story restarts
macros['__TwineQuest_core'] = {
handler: function(place, macroName, params, parser) {
// Do nothing - we can't be called
},
init: function() {
//Send "start" event to TwineQuest plugins
for(var i = 0; i < TwineQuest.plugins.length; i++) {
if(typeof TwineQuest.plugins[i].onStart == 'function') {
TwineQuest.plugins[i].onStart();
}
if(typeof TwineQuest.plugins[i].onEvent == 'function') {
TwineQuest.plugins[i].onEvent('start',null);
}
}
},
};
} catch(e) {
throwError(place,"TwineQuest Setup Error: "+e.message);
}