summaryrefslogtreecommitdiff
path: root/src/main/java/tlIItools/Affix.java
blob: 62766b8c7572f414ddaa2065c458d4cac96867d8 (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
package tlIItools;

import java.util.*;

/** Represents a Torchlight II affix.
 *
 * @author Ben Culkin */
public class Affix {
	/** Details which sort of affix this is.
	 *
	 * @author Ben Culkin */
	public enum AffixType {
		/** An affix that applies to a normal item. */
		ITEM,
		/** An affix that applies to a socketable. */
		SOCKETABLE,
		/** An affix that applies to a monster/person. */
		PERSONAL,
		/** An affix that applies as an item enchantment. */
		ENCHANTMENT
	}

	/** Whether or not to record timing info. */
	public static boolean doTiming;

	/** Internal name of the affix. */
	public String intName;

	/* The prefix/suffix attached to the affix.
	 *
	 * In general, only one of these is set for a given affix.
	 *
	 * NOTE: Some affixes have a mis-set suffix/prefix ordering, and may need to be
	 * changed in their files. */

	/** The 'suffix name' attached to this affix.
	 * 
	 * Include the word [ITEM] to indicate where the item name should go.
	 * 
	 * Note that it is not guaranteed that this is actually a suffix; it could be a
	 * prefix. */
	public String affixSuffix;

	/** The 'prefix name' attached to this affix.
	 * 
	 * Include the word [ITEM] to indicate where the item name should go.
	 * 
	 * Note that it is not guaranteed that this is actually a prefix; it could be a
	 * suffix. */
	public String affixPrefix;

	/* The min/max levels the affix can spawn at. */

	/** The minimum level of an item this affix can spawn on. */
	public int minLevel;
	/** The maximum level of an item this affix can spawn on. */
	public int maxLevel;

	/** The spawn weight for the affix. */
	public int weight;

	/** The number of affix slots taken up. */
	public int slots;

	/** The type of thing this affix applies to. */
	public AffixType type = AffixType.ITEM;

	/** The types of equipment this can spawn on. */
	public List<String> equipTypes;
	/** The types of equipment this can't spawn on. */
	public List<String> nonequipTypes;

	/** Places to get this enchantment from; */
	public List<String> enchantSources;

	/** The socketable types this spawns on. */
	public List<String> socketableTypes;

	/** The effects attached to this affix. */
	public List<Effect> effects;

	/** Determine whether this affix is in an 'affix group' with another affix.
	 * 
	 * By 'affix group', what we mean is that it is essentially the same affix, with
	 * generally just differing levels/values.
	 * 
	 * For instance, an affix that granted +2 strength, and one that granted +4
	 * strength would be considered to be in the same affix group (assuming that
	 * both of those strength bonuses had the same effect name).
	 * 
	 * @param afx The affix to check if we are in an affix group with.
	 * 
	 * @return Whether or now we are in an affix group with the specified affix. */
	public boolean isInAffixGroup(Affix afx) {
		if (effects == null) {
			if (afx.effects != null) return false;
		} else if (!effects.equals(afx.effects)) {
			Iterator<Effect> leftIter = effects.iterator();
			Iterator<Effect> rightIter = afx.effects.iterator();
			
			while (leftIter.hasNext() && rightIter.hasNext()) {
			    Effect left = leftIter.next();
			    Effect right = rightIter.next();
			    
			    if (!left.group.equals(right.group)) return false;
			}
		}
		
		if (enchantSources == null) {
			if (afx.enchantSources != null) return false;
		} else if (!enchantSources.equals(afx.enchantSources)) {
			return false;
		} else if (equipTypes == null) {
			if (afx.equipTypes != null) return false;
		} else if (!equipTypes.equals(afx.equipTypes)) {
			return false;
		} else if (type == null) {
			if (afx.type != null) return false;
		} else if (type != afx.type) {
			return false;
		} else if (nonequipTypes == null) {
			if (afx.nonequipTypes != null) return false;
		} else if (!nonequipTypes.equals(afx.nonequipTypes)) {
			return false;
		} else if (socketableTypes == null) {
			if (afx.socketableTypes != null) return false;
		} else if (!socketableTypes.equals(afx.socketableTypes)) {
			return false;
		} else {
			return true;
		}
		return true;
	}

	/** Gets the name of the 'affix group' that this affix is in.
	 * 
	 * By 'affix group', what we mean is that it is essentially the same affix, with
	 * generally just differing levels/values.
	 * 
	 * For instance, an affix that granted +2 strength, and one that granted +4
	 * strength would be considered to be in the same affix group (assuming that
	 * both of those strength bonuses had the same effect name, and applied to the
	 * same sorts of items).
	 * 
	 * @return The name of the affix group */
	public String getAffixGroupName() {
		StringBuilder sb = new StringBuilder();

		for (Effect eft : effects) sb.append(eft.group);
		for (String enchantSource : enchantSources) sb.append(enchantSource);
		for (String equipType : equipTypes) sb.append(equipType);
		for (String nonEquipType : nonequipTypes) sb.append(nonEquipType);
		for (String socketableType : socketableTypes) sb.append(socketableType);

		return sb.toString();
	}

	/* Are invalid equip types being added?
	 * 
	 * NOTE: This is kinda bad practice. It should really be handled via two
	 * separate affix methods, one to add a valid affix, one to add an invalid, w/
	 * the caller keeping tracking/calling the right one. */
	private boolean inNonEquip;

	/** Create a new blank affix. */
	public Affix() {
		equipTypes      = new ArrayList<>();
		nonequipTypes   = new ArrayList<>();
		enchantSources  = new ArrayList<>();
		socketableTypes = new ArrayList<>();
		effects         = new ArrayList<>();
	}

	/** Sets the set of equip types that is added to.
	 *
	 * @param nonequip True if the equip types being added are prohibited, or false
	 *                 if they are allowed. */
	public void setEquipType(boolean nonequip) {
		inNonEquip = nonequip;
	}

	/** Add an equip type to the right set of equip types.
	 *
	 * @param type The equip type to add. */
	public void addEquipType(String equipType) {
		if (inNonEquip) {
			nonequipTypes.add(equipType);
		} else {
			if (equipType.equals("SOCKETABLE") || equipType.contains(" EMBER")) {
				type = AffixType.SOCKETABLE;
				socketableTypes.add(equipType);
			} else if (equipType.startsWith("ENCHANTER")) {
				type = AffixType.ENCHANTMENT;
				enchantSources.add(equipType.substring(10));
			} else if (equipType.equals("MONSTER") || equipType.equals("PLAYER")) {
				type = AffixType.PERSONAL;
			} else {
				equipTypes.add(equipType);
			}
		}
	}

	@Override
	public String toString() {
		return intName;
	}

	/** Print out the full details of this affix.
	 * 
	 * @return The full details of the affix. */
	public String toLongString() {
		StringBuilder sb = new StringBuilder();

		if (type == AffixType.SOCKETABLE) {
			sb.append("Socketable ");
		} else if (type == AffixType.PERSONAL || (intName != null && intName.startsWith("HERO_"))) {
			sb.append("Personal ");
		} else if (intName != null & intName.startsWith("MAP_")) {
			sb.append("Area ");
		}

		if (type == AffixType.ENCHANTMENT) {
			sb.append("Enchantment: ");
		} else {
			sb.append("Affix: ");
		}

		sb.append(intName);
		sb.append("\n");

		if (affixSuffix != null) {
			sb.append("\tSuffix: ");
			sb.append(affixSuffix);
			sb.append("\n");
		}

		if (affixPrefix != null) {
			sb.append("\tPrefix: ");
			sb.append(affixPrefix);
			sb.append("\n");

		}

		sb.append("\t");
		if (minLevel <= 1 && maxLevel == 999) {
			sb.append("No Level Range");
		} else if (minLevel != 1 && maxLevel != 999) {
			sb.append("Level Range: ");
			sb.append(minLevel);
			sb.append("-");
			sb.append(maxLevel);
		} else if (minLevel <= 1) {
			sb.append("Max Level: ");
			sb.append(maxLevel);
		} else if (maxLevel == 999) {
			sb.append("Minimum Level: ");
			sb.append(minLevel);
		}

		sb.append("\n");

		sb.append("\tSpawn Weight: ");
		sb.append(weight);
		sb.append("\n");

		if (slots == 0) {
			sb.append("\tOccupies no slots\n");
		} else {
			sb.append("\tSlots: ");
			sb.append(slots);
			sb.append("\n");
		}

		if (equipTypes.size() != 0) {
			if (type == AffixType.SOCKETABLE) {
				sb.append("\tSocketable Into: ");
			} else if (type == AffixType.ENCHANTMENT) {
				sb.append("\tEnchants Onto: ");
			} else {
				sb.append("\tSpawns On: ");
			}

			sb.append(equipTypes);
			sb.append("\n");
		}

		// Socketables & enchantments use this as a duplicate.
		if (type != AffixType.SOCKETABLE && type != AffixType.ENCHANTMENT && nonequipTypes.size() != 0) {
			sb.append("\tCan't Spawn On: ");
			sb.append(nonequipTypes);
			sb.append("\n");
		}

		if (enchantSources.size() != 0) {
			sb.append("\tEnchantment Sources: ");
			sb.append(enchantSources);
			sb.append("\n");
		}

		if (socketableTypes.size() != 0) {
			sb.append("\tSocketable Types: ");
			sb.append(socketableTypes);
			sb.append("\n");
		}

		if (effects.size() != 0) {
			sb.append("\tEffects: ");
			for (Effect eft : effects) {
				sb.append("\n\t\t");
				sb.append(eft.toString());
			}
			sb.append("\n");
		}
		
		/*
		sb.append("Affix Group: ");
		String afxGroupName = getAffixGroupName();
		sb.append(afxGroupName);
		sb.append(" (ID: ");
		sb.append(afxGroupName.hashCode());
		sb.append(")");
		sb.append("\n");
		*/
		
		return sb.toString();
	}

	/** Load an affix from an input source.
	 *
	 * @param scn The input source to load from.
	 *
	 * @param fName The name of the input source in question.
	 *
	 * @return The loaded affix. */
	public static Affix loadAffix(Scanner scn, String fName) {
		return loadAffix(scn, fName, new ArrayList<>());
	}

	/** Load an affix from an input source.
	 * 
	 * @param scn The input source to read from.
	 * @param scnName The name of the input source.
	 * @param errors A list to stick errors encountered during loading the affix.
	 *
	 * @return The affix, loaded from the file. */
	public static Affix loadAffix(Scanner scn, String scnName, List<String> errors) {
		Affix afx = new Affix();

		long startTime = System.nanoTime();

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

			if (ln.contains("[NOT_UNITTYPES]")) {
				afx.setEquipType(true);
				continue;
			} else if (ln.contains("[UNITTYPES]")) {
				afx.setEquipType(false);
				continue;
			}

			String[] splits = ln.split(":");
			if (ln.contains("<TRANSLATE>")) {
				splits[0] = splits[0].substring(11);

				switch (splits[0]) {
				case "SUFFIX":
					afx.affixSuffix = splits[1];
					break;
				case "PREFIX":
					afx.affixPrefix = splits[1];
					break;
				default:
					String msg = String.format(
							"Misformed affix translation: (%s) (%s) (%s)\n",
							splits[0], splits[1], scnName);
					errors.add(msg);
				}
			} else if (ln.contains("MIN_SPAWN_RANGE")) {
				afx.minLevel = Integer.parseInt(splits[1]);
			} else if (ln.contains("MAX_SPAWN_RANGE")) {
				afx.maxLevel = Integer.parseInt(splits[1]);
			} else if (ln.contains("WEIGHT:")) {
				afx.weight = Integer.parseInt(splits[1]);
			} else if (ln.contains("SLOTS_OCCUPY")) {
				afx.weight = Integer.parseInt(splits[1]);
			} else if (ln.contains("UNITTYPE") && !ln.contains("/")) {
				if (splits.length == 1)
					errors.add(String.format(
							"Malformed equip type: (%s) (%s)\n",
							splits[0], scnName));

				afx.addEquipType(splits[1]);
			} else if (splits[0].equals("<STRING>NAME")) {
				if (splits.length == 1)
					errors.add(String.format(
							"Malformed name: (%s) (%s)\n",
							splits[0], scnName));

				afx.intName = splits[1];
			} else if (ln.contains("[EFFECT]")) {
				List<String> eftErrs = new ArrayList<>();

				Effect eft = Effect.parseEffect(afx, scn, scnName, errors);
				errors.addAll(eftErrs);

				afx.effects.add(eft);
			}
		}

		// Sort effects, so that they are in a stable order, even if specified out of
		// order
		afx.effects.sort(Comparator.comparingInt((val) -> val.hashCode()));

		long endTime = System.nanoTime();
		if (doTiming) {
			String fmt = "\tProcessed affix %s from %s in %d nanoseconds (%.2f seconds)\n\n";
			double seconds = ((double) (endTime - startTime) / 1000000000);

			errors.add(String.format(
					fmt,
					afx.intName, scnName, endTime - startTime, seconds));
		}

		return afx;
	}
}