-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleModifier.pde
397 lines (306 loc) · 10.3 KB
/
RuleModifier.pde
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import java.util.*;
class Modifier
{
String field;
ModifierOperation op;
float float_value;
int int_value;
String string_value;
Modifier(String field, ModifierOperation op, String value) { this.field = field; this.op = op; float_value = parse_float(value, 0f); int_value = (int)float_value; string_value = value; }
Modifier() { this("", ModifierOperation.NONE, ""); }
Modifier(String to_parse)
{
this();
String[] chunks = split_respecting_quoted_whitespace(to_parse);
if (chunks.length >= 4 && (chunks[2].toLowerCase().equals("to") || chunks[2].toLowerCase().equals("from")))
{
switch (chunks[0].toLowerCase())
{
case "add": case "sum": case "+": op = ModifierOperation.ADD; break;
case "subtract": case "sub": case "-": op = ModifierOperation.SUBTRACT; break;
case "set": op = ModifierOperation.SET; break;
case "multiply": case "mult": case "*": op = ModifierOperation.MULTIPLY; break;
case "divide": case "div": case "/": op = ModifierOperation.DIVIDE; break;
default: op = ModifierOperation.NONE; println("Unknown rule modifier operator '" + chunks[0] + "'."); break;
}
string_value = chunks[1];
float_value = parse_float(chunks[1],0f);
int_value = (int)float_value;
field = chunks[3];
}
}
float get_float(float current_value)
{
switch (op)
{
case ADD: return current_value + float_value;
case SUBTRACT: return current_value - float_value;
case SET: return float_value;
case MULTIPLY: return current_value * float_value;
case DIVIDE: return current_value / float_value;
default: return current_value;
}
}
int get_int(int current_value)
{
switch (op)
{
case ADD: return current_value + int_value;
case SUBTRACT: return current_value - int_value;
case SET: return int_value;
case MULTIPLY: return current_value * int_value;
case DIVIDE: return current_value / int_value;
default: return current_value;
}
}
String get_string(String current_value)
{
if (current_value.length() == 0 || op == ModifierOperation.SET)
return string_value;
if (string_value.length() == 0)
return current_value;
return current_value + ", " + string_value;
}
float get_float() { return float_value; }
int get_int() { return int_value; }
String get_string() { return string_value; }
}
class Rule
{
String name;
String description;
StringList dependencies;
StringList tags;
ArrayList<Modifier> mods;
Rule(String name, String description, StringList dependencies, StringList tags)
{
this.description = description;
this.name = name;
this.dependencies = new StringList(dependencies);
this.tags = new StringList(tags);
mods = new ArrayList<Modifier>();
}
Rule() { this("", "", new StringList(), new StringList()); }
boolean is_locked(StringList rulenames) { for (String dep : dependencies) { if (!rulenames.hasValue(dep)) return true; } return false; }
void deserialize(JSONObject o)
{
name = o.getString("name", "no name specified");
description = o.getString("description", "no description specified");
tags = getStringList("tags", o);
dependencies = getStringList("dependencies", o);
mods = new ArrayList<Modifier>();
StringList mods_as_strings = getStringList("mods", o);
for (String m : mods_as_strings)
mods.add(new Modifier(m));
}
}
//maintains the active rules for a game session. Also has a separate mapping of all the rules' modifiers keyed by field they modify.
class RuleManager
{
RuleList rules = new RuleList();
HashMap<String, ArrayList<Modifier>> mods = new HashMap<String, ArrayList<Modifier>>();
void put(Rule r)
{
rules.add(r);
for (Modifier m : r.mods)
{
if (!mods.containsKey(m.field))
mods.put(m.field, new ArrayList<Modifier>());
mods.get(m.field).add(m);
}
}
void put(RuleList rl)
{
for (int i = 0; i < rl.size(); ++i)
put(rl.get(i));
}
float get_float(String field, float current_value)
{
float retval = current_value;
ArrayList<Modifier> ms = mods.get(field);
if (ms != null)
{
for (int i = 0; i < ms.size(); ++i)
retval = ms.get(i).get_float(retval);
}
return retval;
}
int get_int(String field, int current_value)
{
int retval = current_value;
ArrayList<Modifier> ms = mods.get(field);
if (ms != null)
{
for (int i = 0; i < ms.size(); ++i)
retval = ms.get(i).get_int(retval);
}
return retval;
}
StringList get_strings(String field)
{
StringList retval = new StringList();
ArrayList<Modifier> ms = mods.get(field);
if (ms != null)
{
for (int i = 0; i < ms.size(); ++i)
retval.appendUnique(ms.get(i).get_string());
}
return retval;
}
float get_float (String field) { return get_float (field,0f); }
int get_int (String field) { return get_int (field,0 ); }
String get_string(String field) { return get_strings(field).join(", "); }
StringList get_rule_names() { return rules.names(); }
RuleList get_rules() { return rules.clone(); }
RuleList get_available_curses()
{
RuleList retval = globals.ruleFactory.get_all().filter_by_tag("curse");
retval.remove_all(rules);
for (int i = 0; i < retval.size(); ++i)
{
if (retval.get(i).is_locked(rules.names()))
{
retval.remove(i);
--i;
}
}
return retval;
}
RuleList get_available_boons()
{
RuleList retval = globals.ruleFactory.get_all().filter_by_tag("boon");
retval.remove_all(rules);
for (int i = 0; i < retval.size(); ++i)
{
if (retval.get(i).is_locked(rules.names()))
{
retval.remove(i);
--i;
}
}
return retval;
}
}
class RuleList
{
private ArrayList<Rule> rules;
RuleList() { rules = new ArrayList<Rule>(); }
RuleList(ArrayList<Rule> rules) { this.rules = rules; }
RuleList(RuleList rhs) { this(); add_all(rhs); }
RuleList(StringList names) { this(); add_all(names); }
StringList names() { StringList retval = new StringList(); for (int i = 0; i < rules.size(); ++i) retval.append(rules.get(i).name); return retval; }
RuleList copy() { return new RuleList(this); }
RuleList clone() { return copy(); }
Rule get(int i) { if (i < 0 || i >= rules.size()) return null; return rules.get(i); }
Rule get_random() { int r = (int)random(0, rules.size()); return rules.get(r); }
RuleList shuffle() { Collections.shuffle(rules); return this; }
RuleList top(int num) { if (num < rules.size()) rules.subList(num, rules.size()).clear(); return this; }
int size() { return rules.size(); }
RuleList add(Rule rule) { rules.add(rule); return this; }
RuleList add(String rulename) { add(globals.ruleFactory.get_rule(rulename)); return this; }
RuleList remove(Rule rule) { rules.remove(rule); return this; }
RuleList remove(int i) { if (i >= 0 && i < rules.size()) rules.remove(i); return this; }
RuleList remove(String rulename) { for (int i = 0; i < rules.size(); ++i) { if (rules.get(i).name.equals(rulename)) rules.remove(i); } return this; }
RuleList filter_by_all_tags(StringList tags)
{
for (int i = 0; i < rules.size(); ++i)
{
StringList this_rules_tags = rules.get(i).tags;
for (String tag : tags)
{
if (!this_rules_tags.hasValue(tag))
{
rules.remove(i);
--i;
break;
}
}
}
return this;
}
RuleList filter_by_all_tags(String... tags) { return filter_by_all_tags(new StringList(tags)); }
RuleList filter_by_any_tag(StringList tags)
{
for (int i = 0; i < rules.size(); ++i)
{
StringList this_rules_tags = rules.get(i).tags;
boolean found_one = false;
for (String tag : tags)
{
if (this_rules_tags.hasValue(tag))
{
found_one = true;
break;
}
}
if (!found_one)
{
rules.remove(i);
--i;
}
}
return this;
}
RuleList filter_by_any_tag(String... tags) { return filter_by_any_tag(new StringList(tags)); }
RuleList filter_by_tag(String tag) { return filter_by_all_tags(tag); }
RuleList filter_out_tags(StringList tags)
{
for (int i = 0; i < rules.size(); ++i)
{
StringList this_rules_tags = rules.get(i).tags;
for (String tag : tags)
{
if (this_rules_tags.hasValue(tag))
{
rules.remove(i);
--i;
break;
}
}
}
return this;
}
RuleList filter_out_tags(String... tags)
{
return filter_out_tags(new StringList(tags));
}
RuleList filter_out_tag(String tag) { return filter_out_tags(tag); }
RuleList clear() { rules.clear(); return this; }
RuleList remove_all(RuleList rhs) { rules.removeAll(rhs.rules); return this; }
RuleList add_all(RuleList rhs) { rules.addAll(rhs.rules); return this; }
RuleList remove_all(StringList rhs_names) { for (int i = 0; i < rules.size(); ++i) { if (rhs_names.hasValue(rules.get(i).name)) { rules.remove(i); --i; } } return this; }
RuleList add_all(StringList rhs_names) { for (String s : rhs_names) rules.add(globals.ruleFactory.get_rule(s)); return this; }
}
class RuleFactory
{
private HashMap<String, Rule> rules = new HashMap<String, Rule>();
private RuleList all_rules;
void load(String filepath)
{
rules = new HashMap<String, Rule>();
all_rules = new RuleList();
JSONArray list = loadJSONArray(filepath);
for (int i = 0; i < list.size(); ++i)
{
JSONObject lojo = list.getJSONObject(i);
if (lojo.getBoolean("disabled",false))
continue;
Rule rr = new Rule();
rr.deserialize(lojo);
rules.put(rr.name, rr);
all_rules.add(rr);
}
}
Rule get_rule(String name) { return rules.get(name); }
RuleList get_all() { return all_rules.copy(); }
StringList get_tags(String name) { return new StringList(get_rule(name).tags); }
}
enum ModifierOperation
{
ADD,
SUBTRACT,
SET,
MULTIPLY,
DIVIDE,
NONE
}