/
EncounterManager.java
584 lines (499 loc) · 19.8 KB
/
EncounterManager.java
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
572
573
574
575
576
577
578
579
580
581
582
583
584
package net.sourceforge.kolmafia.session;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.IntStream;
import net.sourceforge.kolmafia.AdventureResult;
import net.sourceforge.kolmafia.KoLAdventure;
import net.sourceforge.kolmafia.KoLCharacter;
import net.sourceforge.kolmafia.KoLConstants;
import net.sourceforge.kolmafia.MonsterData;
import net.sourceforge.kolmafia.RequestLogger;
import net.sourceforge.kolmafia.StaticEntity;
import net.sourceforge.kolmafia.combat.MonsterStatusTracker;
import net.sourceforge.kolmafia.equipment.Slot;
import net.sourceforge.kolmafia.objectpool.EffectPool;
import net.sourceforge.kolmafia.objectpool.ItemPool;
import net.sourceforge.kolmafia.persistence.AdventureDatabase;
import net.sourceforge.kolmafia.persistence.MonsterDatabase;
import net.sourceforge.kolmafia.persistence.QuestDatabase;
import net.sourceforge.kolmafia.persistence.QuestDatabase.Quest;
import net.sourceforge.kolmafia.preferences.Preferences;
import net.sourceforge.kolmafia.utilities.FileUtilities;
import net.sourceforge.kolmafia.utilities.LockableListFactory;
public abstract class EncounterManager {
// Types of special encounters
public enum EncounterType {
NONE,
STOP(true),
LUCKY,
GLYPH(true),
TURTLE,
SEAL,
FIST,
BORIS(true),
BADMOON(true),
BUGBEAR,
WANDERER,
SUPERLIKELY,
ULTRARARE,
FREE_COMBAT,
NOWANDER; // Don't start wandering monster counters
private final boolean autostop;
EncounterType(final boolean autostop) {
this.autostop = autostop;
}
EncounterType() {
this(false);
}
public final boolean isAutostop() {
return this.autostop;
}
}
public static class Encounter {
String location;
EncounterType encounterType;
String encounter;
public Encounter(String[] row) {
location = row[0];
encounterType = EncounterType.valueOf(row[1]);
encounter = row[2];
}
public String getLocation() {
return this.location;
}
public EncounterType getEncounterType() {
return this.encounterType;
}
public String getEncounter() {
return this.encounter;
}
@Override
public String toString() {
return this.encounter;
}
}
private static Encounter[] specialEncounters;
static {
resetEncounters();
}
private static void resetEncounters() {
ArrayList<Encounter> encounters = new ArrayList<>();
try (BufferedReader reader =
FileUtilities.getVersionedReader("encounters.txt", KoLConstants.ENCOUNTERS_VERSION)) {
String[] data;
while ((data = FileUtilities.readData(reader)) != null) {
if (!AdventureDatabase.validateAdventureArea(data[0])) {
RequestLogger.printLine("Invalid adventure area: \"" + data[0] + "\"");
continue;
}
encounters.add(new Encounter(data));
}
} catch (IOException e) {
StaticEntity.printStackTrace(e);
}
specialEncounters = encounters.toArray(new Encounter[encounters.size()]);
}
/** Utility method used to register a given adventure in the running adventure summary. */
public static void registerAdventure(final KoLAdventure adventureLocation) {
if (adventureLocation == null) {
return;
}
EncounterManager.registerAdventure(adventureLocation.getAdventureName());
}
public static void registerAdventure(final String adventureName) {
if (adventureName == null) {
return;
}
RegisteredEncounter previousAdventure =
LockableListFactory.lastElement(KoLConstants.adventureList);
if (previousAdventure != null && previousAdventure.name.equals(adventureName)) {
previousAdventure.increment();
// Even though the RegisteredEncounter is mutated above, this runs
// [LockableListModel.fireContentsChanged]
// which will result in the pane being updated in a Swing context.
KoLConstants.adventureList.set(KoLConstants.adventureList.size() - 1, previousAdventure);
} else {
KoLConstants.adventureList.add(new RegisteredEncounter(null, adventureName));
}
}
public static final Encounter findEncounter(final String encounterName) {
return findEncounter(KoLAdventure.lastVisitedLocation(), encounterName);
}
public static final Encounter findEncounter(
final KoLAdventure adventureLocation, final String encounterName) {
String locationName = adventureLocation == null ? null : adventureLocation.getAdventureName();
return findEncounter(locationName, encounterName);
}
public static final Encounter findEncounter(
final String locationName, final String encounterName) {
return Arrays.stream(specialEncounters)
.filter(e -> locationName == null || e.getLocation().equalsIgnoreCase(locationName))
.filter(e -> e.getEncounter().equalsIgnoreCase(encounterName))
.findAny()
.orElse(null);
}
private static EncounterType encounterType(final String encounterName) {
Encounter encounter = EncounterManager.findEncounter(encounterName);
return EncounterManager.encounterType(encounter, encounterName);
}
private static EncounterType encounterType(
final Encounter encounter, final String encounterName) {
return encounter != null
? encounter.encounterType
: BadMoonManager.specialAdventure(encounterName)
? EncounterType.BADMOON
: EncounterType.NONE;
}
public static final Encounter findEncounterForLocation(
final String locationName, final EncounterType type) {
return Arrays.stream(specialEncounters)
.filter(e -> e.getLocation().equalsIgnoreCase(locationName))
.filter(e -> e.getEncounterType() == type)
.findAny()
.orElse(null);
}
public static final boolean isAutoStop(final String encounterName) {
if (encounterName.equals("Under the Knife")
&& Preferences.getString("choiceAdventure21").equals("2")) {
return false;
}
return encounterType(encounterName).isAutostop();
}
public static boolean isRomanticEncounter(final String responseText, final boolean checkMonster) {
if (responseText.contains("hear a wolf whistle")
|| responseText.contains("you feel the hairs")) {
return true;
}
// This is called from some places before the next monster is set.
// Don't bother checking the monster in those cases.
if (checkMonster
&& TurnCounter.isCounting("Romantic Monster window end", 0, 10)
&& Preferences.getString("nextAdventure").equals("The Deep Machine Tunnels")) {
String name = MonsterStatusTracker.getLastMonsterName();
return name.equalsIgnoreCase(Preferences.getString("romanticTarget"));
}
return false;
}
public static final boolean isEnamorangEncounter(
final String responseText, final boolean checkMonster) {
if (responseText.contains("tangled heartstrings")) {
return true;
}
if (checkMonster
&& TurnCounter.isCounting("Enamorang Monster", 0)
&& Preferences.getString("nextAdventure").equals("The Deep Machine Tunnels")) {
String name = MonsterStatusTracker.getLastMonsterName();
return name.equalsIgnoreCase(Preferences.getString("enamorangMonster"));
}
return false;
}
public static boolean isSpookyVHSTapeMonster(
final String responseText, final boolean checkMonster) {
if (responseText.contains("suddenly roll back and they fall... dead")) {
return true;
}
if (checkMonster
&& TurnCounter.isCounting("Spooky VHS Tape Monster", 0)
&& Preferences.getString("nextAdventure").equals("The Deep Machine Tunnels")) {
String name = MonsterStatusTracker.getLastMonsterName();
return name.equalsIgnoreCase(Preferences.getString("spookyVHSTapeMonster"));
}
return false;
}
public static final boolean isDigitizedEncounter(
final String responseText, final boolean checkMonster) {
if (responseText.contains("must have hit CTRL+V")) {
return true;
}
// This is called from some places before the next monster is set.
// Don't bother checking the monster in those cases.
if (checkMonster
&& TurnCounter.isCounting("Digitize Monster", 0)
&& Preferences.getString("nextAdventure").equals("The Deep Machine Tunnels")) {
String name = MonsterStatusTracker.getLastMonsterName();
return name.equalsIgnoreCase(Preferences.getString("_sourceTerminalDigitizeMonster"));
}
return false;
}
public static final boolean isSaberForceMonster(String monsterName) {
// There's no message to check for, and the monster generally shows up
// immediately, so assume the correct monster is from this skill
if (Preferences.getInteger("_saberForceMonsterCount") < 1) {
return false;
}
return monsterName.equalsIgnoreCase(Preferences.getString("_saberForceMonster"));
}
public static final boolean isSaberForceZone(String monsterName, String zone) {
MonsterData monster = MonsterDatabase.findMonster(monsterName);
return AdventureDatabase.getAreaCombatData(zone).hasMonster(monster);
}
public static final boolean isSaberForceZone(String zone) {
return isSaberForceZone(Preferences.getString("_saberForceMonster"), zone);
}
public static final boolean isSaberForceMonster(MonsterData monster, String zone) {
return isSaberForceMonster(monster.getName(), zone);
}
public static final boolean isSaberForceMonster(String monsterName, String zone) {
if (!isSaberForceZone(monsterName, zone)) {
return false;
}
return isSaberForceMonster(monsterName);
}
public static final boolean isSaberForceMonster() {
String name = MonsterStatusTracker.getLastMonsterName();
return isSaberForceMonster(name);
}
public static final boolean isRelativityMonster() {
// There's no message to check for, and the monster always
// shows up immediately following the fight as a chained
// encounter.
if (Preferences.getBoolean("_relativityMonster")) {
Preferences.setBoolean("_relativityMonster", false);
return true;
}
return false;
}
public static final boolean isGregariousEncounter(final String responseText) {
return responseText.contains("Looks like it's that friend you gregariously made");
}
public static final boolean isWanderingMonster(String encounter) {
MonsterData monster = MonsterDatabase.findMonster(encounter);
return monster != null && monster.getType().contains(EncounterType.WANDERER);
}
public static boolean isLuckyMonster(String encounter) {
MonsterData monster = MonsterDatabase.findMonster(encounter);
return monster != null && monster.getType().contains(EncounterType.LUCKY);
}
public static boolean isSuperlikelyMonster(String encounter) {
MonsterData monster = MonsterDatabase.findMonster(encounter);
return monster != null && monster.getType().contains(EncounterType.SUPERLIKELY);
}
public static boolean isFreeCombatMonster(String encounter) {
MonsterData monster = MonsterDatabase.findMonster(encounter);
return monster != null && monster.getType().contains(EncounterType.FREE_COMBAT);
}
public static boolean isUltrarareMonster(String encounter) {
MonsterData monster = MonsterDatabase.findMonster(encounter);
return monster != null && monster.getType().contains(EncounterType.ULTRARARE);
}
public static boolean isNoWanderMonster(String encounter) {
MonsterData monster = MonsterDatabase.findMonster(encounter);
return monster != null && monster.getType().contains(EncounterType.NOWANDER);
}
// Used to ignore special monsters re-encountered via copying
public static boolean ignoreSpecialMonsters = false;
public static void ignoreSpecialMonsters() {
ignoreSpecialMonsters = true;
}
private static final AdventureResult TELEPORTITIS = EffectPool.get(EffectPool.TELEPORTITIS);
private static final AdventureResult FEELING_LOST = EffectPool.get(EffectPool.FEELING_LOST);
private static void recognizeEncounter(final String encounterName, final String responseText) {
Encounter encounter = EncounterManager.findEncounter(encounterName);
EncounterType encounterType = EncounterManager.encounterType(encounter, encounterName);
if (encounterType == EncounterType.BUGBEAR) {
BugbearManager.registerEncounter(encounter, responseText);
return;
}
// Use of Rain Man skill fires two encounters, first is a non-combat, second a fight, which
// could be a semi-rare
if (KoLCharacter.inRaincore() && responseText.contains("simulacrum of a previous foe")) {
EncounterManager.ignoreSpecialMonsters();
}
// Using the Lecture About Relativity skill fires an immediate encounter, a fight, which could
// be a semi-rare.
if (EncounterManager.isRelativityMonster()) {
EncounterManager.ignoreSpecialMonsters();
}
// You stop for a moment to catch your breath, and possibly a
// cold, and hear a wolf whistle from behind you. You spin
// around and see <monster> that looks suspiciously like the
// ones you shot with a love arrow earlier.
if (encounterType == EncounterType.NONE) {
return;
}
if (encounterType == EncounterType.BADMOON) {
BadMoonManager.registerAdventure(encounterName);
}
if (encounterType.isAutostop()) {
// Don't autostop if you have teleportisis
if (KoLCharacter.hasEquipped(ItemPool.RING_OF_TELEPORTATION, Slot.ACCESSORY1)
|| KoLCharacter.hasEquipped(ItemPool.RING_OF_TELEPORTATION, Slot.ACCESSORY2)
|| KoLCharacter.hasEquipped(ItemPool.RING_OF_TELEPORTATION, Slot.ACCESSORY3)
|| KoLConstants.activeEffects.contains(TELEPORTITIS)
|| KoLConstants.activeEffects.contains(FEELING_LOST)) {
return;
}
GoalManager.checkAutoStop(encounterName);
}
}
/** Utility. The method used to register a given encounter in the running adventure summary. */
public static void registerEncounter(
final String encounterName, final String encounterType, final String responseText) {
final String name = encounterName.trim();
handleSpecialEncounter(name, responseText);
recognizeEncounter(name, responseText);
IntStream.range(0, KoLConstants.encounterList.size())
.mapToObj(i -> Map.entry(i, KoLConstants.encounterList.get(i)))
.filter(e -> e.getValue().name.equalsIgnoreCase(name))
.findFirst()
.ifPresentOrElse(
e -> {
var encounter = e.getValue();
encounter.increment();
// Even though RegisteredEncounter is mutated above, this runs
// [LockableListModel.fireContentsChanged]
// which will result in the pane being updated in a Swing context
KoLConstants.encounterList.set(e.getKey(), encounter);
},
() ->
KoLConstants.encounterList.add(
new RegisteredEncounter(encounterType, encounterName)));
}
public static void handleSpecialEncounter(final String encounterName, final String responseText) {
switch (encounterName.toLowerCase()) {
case "step up to the table, put the ball in play" -> {
if (InventoryManager.hasItem(ItemPool.CARONCH_DENTURES)) {
ResultProcessor.processItem(ItemPool.CARONCH_DENTURES, -1);
QuestDatabase.setQuestIfBetter(Quest.PIRATE, "step4");
}
if (InventoryManager.hasItem(ItemPool.FRATHOUSE_BLUEPRINTS)) {
ResultProcessor.processItem(ItemPool.FRATHOUSE_BLUEPRINTS, -1);
}
return;
}
case "granny, does your dogfish bite?" -> {
if (InventoryManager.hasItem(ItemPool.GRANDMAS_MAP)) {
ResultProcessor.processItem(ItemPool.GRANDMAS_MAP, -1);
}
return;
}
case "meat for nothing and the harem for free" -> {
Preferences.setBoolean("_treasuryEliteMeatCollected", true);
return;
}
case "finally, the payoff" -> {
Preferences.setBoolean("_treasuryHaremMeatCollected", true);
return;
}
case "faction traction = inaction" -> {
Preferences.setInteger("booPeakProgress", 98);
return;
}
case "daily done, john." -> {
// Daily Dungeon Complete
Preferences.setBoolean("dailyDungeonDone", true);
Preferences.setInteger("_lastDailyDungeonRoom", 15);
return;
}
case "a hidden surprise!" -> {
// Since this content is short-lived, create the patterns here every time
// the encounter is found instead of globally
Pattern GIFT_SENDER_PATTERN = Pattern.compile("nounder><b>(.*?)</b></a>");
Pattern NOTE_PATTERN = Pattern.compile("1px solid black;'>(.*?)</td></tr>", Pattern.DOTALL);
Matcher senderMatcher = GIFT_SENDER_PATTERN.matcher(responseText);
if (senderMatcher.find()) {
String sender = senderMatcher.group(1);
RequestLogger.printLine("Gift sender: " + sender);
RequestLogger.updateSessionLog("Gift sender: " + sender);
}
Matcher noteMatcher = NOTE_PATTERN.matcher(responseText);
if (noteMatcher.find()) {
String note = noteMatcher.group(1);
RequestLogger.printLine("Gift note: " + note);
RequestLogger.updateSessionLog("Gift note: " + note);
}
return;
}
case "labrador conspirator" -> {
Preferences.increment("hallowienerCoinspiracy");
return;
}
case "lava dogs" -> {
Preferences.setBoolean("hallowienerVolcoino", true);
return;
}
case "fruuuuuuuit" -> {
Preferences.setBoolean("hallowienerSkeletonStore", true);
return;
}
case "boooooze hound" -> {
Preferences.setBoolean("hallowienerOvergrownLot", true);
return;
}
case "baker's dogzen" -> {
Preferences.setBoolean("hallowienerMadnessBakery", true);
return;
}
case "dog needs food badly" -> {
Preferences.increment("hallowiener8BitRealm");
return;
}
case "ratchet-catcher" -> {
Preferences.setBoolean("hallowienerMiddleChamber", true);
return;
}
case "seeing-eyes dog" -> {
Preferences.setBoolean("hallowienerDefiledNook", true);
return;
}
case "carpenter dog" -> {
Preferences.setBoolean("hallowienerSmutOrcs", true);
return;
}
case "are they made of real dogs?" -> {
Preferences.setBoolean("hallowienerGuanoJunction", true);
return;
}
case "gunbowwowder" -> {
Preferences.setBoolean("hallowienerSonofaBeach", true);
return;
}
case "it isn't a poodle" -> {
Preferences.setBoolean("hallowienerKnollGym", true);
return;
}
}
}
public static class RegisteredEncounter implements Comparable<RegisteredEncounter> {
private final String type;
private final String name;
private final String stringform;
private int encounterCount;
public RegisteredEncounter(final String type, final String name) {
this.type = type;
// The name is likely a substring of a page load, so storing it
// as-is would keep the entire page in memory.
this.name = name;
this.stringform = type == null ? name : type + ": " + name;
this.encounterCount = 1;
}
@Override
public String toString() {
return this.stringform + " (" + this.encounterCount + ")";
}
public void increment() {
this.encounterCount++;
}
public String getName() {
return this.name;
}
public int getCount() {
return this.encounterCount;
}
@Override
public int compareTo(final RegisteredEncounter o) {
if (o == null) {
return -1;
}
if (this.type == null || o.type == null || this.type.equals(o.type)) {
return this.name.compareToIgnoreCase(o.name);
}
return this.type.equals("Combat") ? 1 : -1;
}
}
}