summaryrefslogtreecommitdiff
path: root/src/main/java/tlIItools/Effect.java
blob: 04383417e53b035c555829ae025ba5885227a7ef (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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package tlIItools;

import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;

/**
 * Represents an effect attached to an affix.
 *
 * @author Ben Culkin
 */
public class Effect {
	/**
	 * Count of all loaded effects.
	 */
	public static int effectCount = 0;
	/**
	 * Do timing analysis when loading effects.
	 */
	public static boolean doTiming;

	/**
	 * The list of detail strings for skills.
	 */
	private static Map<String, String> detals;
	/**
	 * The list of detail strings for timed skills.
	 */
	private static Map<String, String> timeDetals;

	/**
	 * The list of replacements for detail strings.
	 */
	private static List<ReplPair> replList;

	/*
	 * Init. lists from files.
	 */
	static {
		try (FileReader detalReader = new FileReader("data/affix-detals.txt")) {
			detals = readDetails(new Scanner(detalReader));
		} catch (IOException ioex) {
			AffixLister.errOut.println("Error loading affix detail text");
		}

		try (FileReader timedDetalReader = new FileReader("data/timed-affix-detals.txt")) {
			timeDetals = readDetails(new Scanner(timedDetalReader));
		} catch (IOException ioex) {
			AffixLister.errOut.println("Error loading timed affix detail text");
		}

		try (FileReader replListReader = new FileReader("data/replace-list.txt")) {
			replList = ReplPair.readList(new Scanner(replListReader));
		} catch (IOException ioex) {
			AffixLister.errOut.println("Error loading replacement lists");
		}
	}

	/**
	 * Read effect detail strings from an input source.
	 *
	 * @param scn
	 * 	The source to read from.
	 * @return
	 * 	The map of effect details to use.
	 */
	public static Map<String, String> readDetails(Scanner scn) {
		Map<String, String> detals = new HashMap<>();

		return readDetails(detals, scn);
	}

	/**
	 * Read effect detail strings from an input source, adding to an
	 * existing set.
	 *
	 * @param detals
	 * 	The details to add to.
	 * @param scn
	 * 	The source to read from.
	 * @return
	 * 	The map of effect details to use.
	 */
	public static Map<String, String> readDetails(Map<String, String> detals, Scanner scn) {
		while (scn.hasNextLine()) {
			String name = scn.nextLine().trim();
			if (name.equals("")) continue;
			if (name.startsWith("#")) continue;

			String body;
			do {
				body = scn.nextLine().trim();
			} while (body.startsWith("#"));

			detals.put(name, body);
		}

		return detals;
	}

	/**
	 * Sanity check the loaded format strings.
	 */
	public static void sanityCheckFormats() {
		for (Entry<String, String> detal : detals.entrySet()) {
			String fmt = detal.getValue();

			AffixLister.errOut.printf("\tTRACE: Applying replacements for %s\n", detal.getKey());
			for (ReplPair repl : replList) {
				String tmp = fmt;
				fmt = fmt.replaceAll(repl.find, repl.replace);
				if (!fmt.equals(tmp)) {
					String outFmt = "\t\tTRACE: Replaced %s with %s: \n\t\t%s\n\t\t%s\n";

					AffixLister.errOut.printf(outFmt, repl.find, repl.replace, tmp, fmt);
				}
			}

			if (fmt.contains("<") || fmt.contains(">")) {
				String warnFmt = "WARN: Details for effect %s are malformated (contains < or >):\n\t%s\n";

				AffixLister.errOut.printf(warnFmt, detal.getKey(), fmt);
			}
		}

		for (Entry<String, String> detal : timeDetals.entrySet()) {
			String fmt = detal.getValue();

			for (ReplPair repl : replList) {
				fmt = fmt.replaceAll(repl.find, repl.replace);
			}

			if (fmt.contains("<") || fmt.contains(">")) {
				String warnFmt = "WARN: Details for timed effect %s are malformatted (contains < or >):\n\t%s\n";
				AffixLister.errOut.printf(warnFmt, detal.getKey(), fmt);
			}
		}
	}

	/**
	 * The file name this effect came from.
	 */
	public String fName;

	/**
	 * The name of the effect.
	 */
	public String name;

	/**
	 * The specific effect that happens.
	 */
	public String type;

	/**
	 * Damage type for the effect, if applicable.
	 */
	public String damageType = "physical";

	/**
	 * Duration of the effect.
	 */
	public double duration;

	/**
	 * Whether or not we have a duration or not.
	 */
	public boolean hasDuration;

	/**
	 * Minimum value for the effect.
	 */
	public double minValue;
	/**
	 * Maximum value for the effect.
	 */
	public double maxValue;

	/**
	 * The name of the stat that applies to this affect.
	 */
	public String statName;
	/**
	 * The percent of the stat value to apply.
	 */
	public double statPercent;
	/**
	 * Whether or not this stat is a bonus value.
	 */
	public boolean isStatBonus;

	/**
	 * Whether or not this uses the owners level to modify
	 * any applicable graph.
	 */
	public boolean ownerLevel;

	/**
	 * The graph to use instead of the default graph.
	 */
	public String graphOverride;

	/**
	 * Whether or not a graph is used for this effect.
	 */
	public boolean useGraph = true;

	/**
	 * Whether this effect can stack with itself.
	 */
	public boolean exclusive;

	/**
	 * The amount the targets armor is reduced by for this
	 * effect.
	 */
	public double soakScale = 1.0;

	/**
	 * Level of the effect.
	 */
	public int level = -1;

	/**
	 * Whether or not this effect is a 'transfer'
	 * effect (Applied to the enemy on a hit).
	 */
	public boolean isTransfer;

	/**
	 * The amount to resist/do knockback by.
	 */
	public double resist;

	/**
	 * Minimum value per monster.
	 */
	public double minPer;

	/**
	 * Maximum value per monster.
	 */
	public double maxPer;

	/**
	 * Range for effect.
	 */
	public double range;

	/**
	 * Maximum count of monsters.
	 */
	public double maxCount;

	/**
	 * The rate at which the effect fires.
	 */
	public double pulse;

	/**
	 * Create a new blank effect.
	 */
	public Effect() {

	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		if (isTransfer) {
			sb.append("Inflict on Hit: ");
		}

		Map<String, String> detMap = hasDuration ? timeDetals : detals;

		if (detMap.containsKey(type) || (hasDuration && !timeDetals.containsKey(type) && detals.containsKey(type))) {
			String fmt;
			if (hasDuration && !timeDetals.containsKey(type) && detals.containsKey(type)) {
				AffixLister.errOut.printf("Improvised details for timed %s\n", type);
				fmt = detals.get(type) + "for <DUR> seconds";
			} else {
				fmt = detMap.get(type);
			}

			// Expand aliases first.

			for (ReplPair repl : replList) {				
				fmt = fmt.replaceAll(repl.find, repl.replace);
			}
			
			if (minValue <= 0 && maxValue <= 0) {
				fmt = fmt.replaceAll("<C\\|([^|>]+)\\|([^|>]+)>", "$1");
			}

			if (minValue >= 0 && maxValue >= 0) {
				fmt = fmt.replaceAll("<C\\|([^|>]+)\\|([^|>]+)>", "$2");
			}

			if (minPer <= 0 && maxPer <= 0) {
				fmt = fmt.replaceAll("<MC\\|(\\w+)\\|(\\w+)>", "$1");
			}

			if (minPer >= 0 && maxPer >= 0) {
				fmt = fmt.replaceAll("<MC\\|([^|>]+)\\|([^|>]+)>", "$2");
			}
			
			if (fmt.contains("<") || fmt.contains(">")) {
				AffixLister.errOut.printf("WARN: Details for effect %s are malformatted (contains < or >):\n\t%s\n", type, fmt);
			}

			sb.append(String.format(fmt, Math.abs(minValue), Math.abs(maxValue), duration, damageType.toLowerCase(), level, resist, name, Math.abs(minPer), Math.abs(maxPer), range, maxCount, pulse));
		} else {
			sb.append("No effect details for effect ");
			sb.append(type);
			sb.append(String.format(" with parameters (min %.2f, max %.2f, dur %.2f, type %s, level %d)", minValue, maxValue, duration, damageType.toLowerCase(), level, name));

			if (AffixLister.addFileName) {
				sb.append(" from file ");
				sb.append(fName);
			}

			if (hasDuration) AffixLister.errOut.print("TIMED: ");
			AffixLister.errOut.println(sb.toString());
		}

		if (name != null) {
			sb.append(" (named ");
			sb.append(name);
			sb.append(")");
		}

		if (exclusive) sb.append(" (Exclusive)");

		if (graphOverride != null) {
			sb.append(" (Uses ");
			sb.append(graphOverride);
			sb.append(" graph)");
		}

		if (ownerLevel) {
			sb.append(" (Uses owner level for graph)");
		}

		if (soakScale != 1.0) {
			String fmt = " (%.2f%% reduced effectiveness of armor)";

			sb.append(String.format(fmt, (1 - soakScale) * 100));
		}

		if (level != -1) {
			sb.append(" (Level ");
			sb.append(level);
			sb.append(")");
		}

		if (statName != null) {
			sb.append(String.format(" (%.2f of stat %s", statPercent, statName));
			if (isStatBonus) sb.append(" as bonus)");
			else             sb.append(")");
		}

		if (!useGraph) sb.append(" (Ignoring graph)");

		return sb.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((damageType == null) ? 0 : damageType.hashCode());
		result = prime * result + (exclusive ? 1231 : 1237);
		result = prime * result + ((graphOverride == null) ? 0 : graphOverride.hashCode());
		result = prime * result + (hasDuration ? 1231 : 1237);
		result = prime * result + (isStatBonus ? 1231 : 1237);
		result = prime * result + (isTransfer ? 1231 : 1237);
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + (ownerLevel ? 1231 : 1237);
		long temp;
		temp = Double.doubleToLongBits(soakScale);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((statName == null) ? 0 : statName.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		result = prime * result + (useGraph ? 1231 : 1237);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Effect other = (Effect) obj;
		if (damageType == null) {
			if (other.damageType != null)
				return false;
		} else if (!damageType.equals(other.damageType))
			return false;
		if (exclusive != other.exclusive)
			return false;
		if (graphOverride == null) {
			if (other.graphOverride != null)
				return false;
		} else if (!graphOverride.equals(other.graphOverride))
			return false;
		if (hasDuration != other.hasDuration)
			return false;
		if (isStatBonus != other.isStatBonus)
			return false;
		if (isTransfer != other.isTransfer)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (ownerLevel != other.ownerLevel)
			return false;
		if (Double.doubleToLongBits(soakScale) != Double.doubleToLongBits(other.soakScale))
			return false;
		if (statName == null) {
			if (other.statName != null)
				return false;
		} else if (!statName.equals(other.statName))
			return false;
		if (type == null) {
			if (other.type != null)
				return false;
		} else if (!type.equals(other.type))
			return false;
		if (useGraph != other.useGraph)
			return false;
		return true;
	}

	public static Effect parseEffect(Affix afx, Scanner scn, String scnSource) {
		return parseEffect(afx, scn, scnSource, new ArrayList<>());
	}

	public static Effect parseEffect(Affix afx, Scanner scn, String scnSource, List<String> errs) {
		Effect efct = new Effect();

		long startTime = System.nanoTime();

		efct.fName = scnSource;

		while (scn.hasNextLine()) {
			String ln = scn.nextLine();
			ln = ln.replaceAll("\\p{Cntrl}", "");

			if (ln.contains("[/EFFECT]")) break;

			String[] splits = ln.split(":");

			// Empty field
			if (splits.length == 1) continue;

			if (ln.contains("NAME")) {
				efct.name = splits[1];
			} else if (ln.contains("DAMAGE_TYPE")) {
				efct.damageType = splits[1];
			} else if (ln.contains("TYPE")) {
				efct.type = splits[1];
			} else if (ln.contains("ACTIVATION")) {
				switch (splits[1]) {
					case "DYNAMIC":
					case "PASSIVE":
						// Passive is the default, and
						// dynamic doesn't have much
						// actual difference.
						break;
					case "TRANSFER":
						efct.isTransfer = true;
						break;
					default:
						errs.add(String.format("Malformed activation type: (%s) (%s) (%s)\n", splits[1], efct.name, afx.intName));
				}
			} else if (ln.contains("DURATION")) {
				if (splits[1].equals("ALWAYS")) {
					efct.hasDuration = false;

					efct.duration = Double.POSITIVE_INFINITY;
				} else if (splits[1].equals("INSTANT")) {
					efct.hasDuration = false;

					efct.duration = Double.NaN;
				} else if (splits[1].equals("PERCENT")) {
					efct.hasDuration = false;

					efct.duration = Double.NaN;

					errs.add(String.format("WARN: Punting on DURATION:PERCENT for %s\n", scnSource));
				} else if (splits[1].equals("0")) {
					efct.hasDuration = false;
					efct.duration = 0.0;
				} else {
					efct.hasDuration = true;

					if (splits[1].equalsIgnoreCase("instant")) {
						efct.duration = -1;
					} else {
						efct.duration = Double.parseDouble(splits[1]);
					}
				}
			} else if (ln.contains("<FLOAT>MIN:")) {
				efct.minValue = Double.parseDouble(splits[1]);
			} else if (ln.contains("<FLOAT>MAX:")) {
				efct.maxValue = Double.parseDouble(splits[1]);
			} else if (ln.contains("USEOWNERLEVEL")) {
				// We don't care about this, for now
			} else if (ln.contains("LEVEL:")) {
				efct.level = Integer.parseInt(splits[1]);
			} else if (ln.contains("EXCLUSIVE")) {
				efct.exclusive = Boolean.parseBoolean(splits[1]);
			} else if (ln.contains("GRAPHOVERRIDE")) {
				efct.graphOverride = splits[1];
			} else if (ln.contains("USEOWNERLEVEL")) {
				efct.ownerLevel = Boolean.parseBoolean(splits[1]);
			} else if (ln.contains("NOGRAPH")) {
				efct.useGraph = Boolean.parseBoolean(splits[1]);
			} else if (ln.contains("STATNAME")) {
				efct.statName = splits[1];
			} else if (ln.contains("STATPERCENT")) {
				efct.statPercent = Double.parseDouble(splits[1]);
			} else if (ln.contains("STATMODIFIERISBONUS")) {
				efct.isStatBonus = Boolean.parseBoolean(splits[1]);
			} else if (ln.contains("RESISTANCE:")) {
				efct.resist = Double.parseDouble(splits[1]);
			} else if (ln.contains("FORCE:")) {
				efct.resist = Double.parseDouble(splits[1]);
			} else if (ln.contains("MIN_PER")) {
				efct.minPer = Double.parseDouble(splits[1]);
			} else if (ln.contains("MAX_PER")) {
				efct.maxPer = Double.parseDouble(splits[1]);
			} else if (ln.contains("RANGE:") || ln.contains("RADIUS")) {
				efct.range = Double.parseDouble(splits[1]);
			} else if (ln.contains("MAX_COUNT:") || ln.contains("MAX_TARGETS")) {
				efct.maxCount = Double.parseDouble(splits[1]);
			} else if (ln.contains("PULSE_RATE")) {
				efct.pulse = Double.parseDouble(splits[1]);
			} else if (ln.contains("CHANCE:")) {
				// NOTE: Should really use its own field
				efct.resist = Double.parseDouble(splits[1]);
			}
		}

		long endTime = System.nanoTime();
		if (doTiming) {
			String fmt = "\t\tProcessed effect %s from %s in %d nanoseconds (%.2f seconds)\n";

			double seconds = ((double)((endTime - startTime) / 1000000000));
			errs.add(String.format(fmt, efct.name, scnSource, endTime - startTime, seconds));
		}

		effectCount += 1;

		return efct;
	}
	
	
}