aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/miscfeatures/CrystalWishingCompassSolver.java
blob: 25f39c3a7f4eba0dca33dc2371596da83239f8cf (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
package io.github.moulberry.notenoughupdates.miscfeatures;

import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.core.util.Line;
import io.github.moulberry.notenoughupdates.options.customtypes.NEUDebugFlag;
import io.github.moulberry.notenoughupdates.util.NEUDebugLogger;
import io.github.moulberry.notenoughupdates.util.SBInfo;
import net.minecraft.client.Minecraft;
import net.minecraft.event.ClickEvent;
import net.minecraft.event.HoverEvent;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatStyle;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.Vec3;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class CrystalWishingCompassSolver {
	private static final CrystalWishingCompassSolver INSTANCE = new CrystalWishingCompassSolver();
	public static CrystalWishingCompassSolver getInstance() {
		return INSTANCE;
	}

	private static final Minecraft mc = Minecraft.getMinecraft();
	private static boolean isSkytilsPresent = false;

	// Crystal Nucleus unbreakable blocks, area coordinates reported by Hypixel server are slightly different
	private static final AxisAlignedBB NUCLEUS_BB = new AxisAlignedBB(463, 63, 460, 563, 181, 564);
	private static final double MAX_COMPASS_PARTICLE_SPREAD = 16;

	private static BlockPos prevPlayerPos;
	private long compassUsedMillis = 0;
	private Vec3 firstParticle = null;
	private Vec3 lastParticle = null;
	private double lastParticleDistanceFromFirst = 0;
	private Line firstCompassLine = null;
	private Line secondCompassLine = null;
	private Vec3 solution = null;
	private Line solutionIntersectionLine = null;

	private void resetForNewCompass() {
		compassUsedMillis = 0;
		firstParticle = null;
		lastParticle = null;
		lastParticleDistanceFromFirst = 0;
	}

	private void resetForNewTarget() {
		NEUDebugLogger.log(NEUDebugFlag.WISHING,"Resetting for new target");
		resetForNewCompass();
		firstCompassLine = null;
		secondCompassLine = null;
		solutionIntersectionLine = null;
		prevPlayerPos = null;
		solution = null;
	}

	@SubscribeEvent
	public void onWorldLoad(WorldEvent.Unload event) {
		resetForNewTarget();
		isSkytilsPresent = Loader.isModLoaded("skytils");
	}

	@SubscribeEvent
	public void onPlayerInteract(PlayerInteractEvent event) {
		if (!NotEnoughUpdates.INSTANCE.config.mining.wishingCompassSolver ||
			SBInfo.getInstance().getLocation() == null ||
			!SBInfo.getInstance().getLocation().equals("crystal_hollows") ||
			event.entityPlayer != mc.thePlayer ||
			(event.action != PlayerInteractEvent.Action.RIGHT_CLICK_AIR &&
				event.action != PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK)
			) {
			return;
		}

		ItemStack heldItem = event.entityPlayer.getHeldItem();
		if (heldItem == null || heldItem.getItem() != Items.skull) {
			return;
		}

		String heldInternalName = NotEnoughUpdates.INSTANCE.manager.getInternalNameForItem(heldItem);
		if (heldInternalName == null || !heldInternalName.equals("WISHING_COMPASS")) {
			return;
		}

		try {
			if (isSolved()) {
				resetForNewTarget();
			}

			// 64.0 is an arbitrary value but seems to work well
			if (prevPlayerPos != null && prevPlayerPos.distanceSq(mc.thePlayer.getPosition()) < 64.0) {
				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW +
					"[NEU] Move a little further before using the wishing compass again."));
				event.setCanceled(true);
				return;
			}

			prevPlayerPos = mc.thePlayer.getPosition().getImmutable();
			compassUsedMillis = System.currentTimeMillis();
		} catch (Exception e) {
			mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED +
				"[NEU] Error processing wishing compass action - see log for details"));
			e.printStackTrace();
		}
	}

	/*
	 * Processes particles if the wishing compass was used within the last 5 seconds.
	 *
	 * The first and the last particles are used to create a line for each wishing compass
	 * use that is then used to calculate the target.
	 *
	 * Once two lines have been calculated, the shortest line between the two is calculated
	 * with the midpoint on that line being the wishing compass target. The accuracy of this
	 * seems to be very high.
	 *
	 * The target location varies based on various criteria, including, but not limited to:
	 *  Topaz Crystal (Khazad-dûm)                Magma Fields
	 *  Odawa (Jungle Village)                    Jungle w/no Jungle Key in inventory
	 *  Amethyst Crystal (Jungle Temple)          Jungle w/Jungle Key in inventory
	 *  Sapphire Crystal (Lost Precursor City)    Precursor Remnants
	 *  Jade Crystal (Mines of Divan)             Mithril Deposits
	 *  King Yolkar                               Goblin Holdout without "King's Scent I" effect
	 *  Goblin Queen                              Goblin Holdout with "King's Scent I" effect
	 *  Crystal Nucleus                           All Crystals found and none placed
	 *                                            per-area structure missing, or because Hypixel.
	 *                                            Always within 1 block of X=513 Y=106 Z=551.
	 */
	public void onSpawnParticle(
		EnumParticleTypes particleType,
		double x,
		double y,
		double z
	) {
		if (!NotEnoughUpdates.INSTANCE.config.mining.wishingCompassSolver ||
			particleType != EnumParticleTypes.VILLAGER_HAPPY ||
			!SBInfo.getInstance().getLocation().equals("crystal_hollows") ||
			isSolved() ||
			System.currentTimeMillis() - compassUsedMillis > 5000) {
			return;
		}

		try {
			Vec3 particleVec = new Vec3(x, y, z);
			if (firstParticle == null) {
				firstParticle = particleVec;
				return;
			}

			double distanceFromFirst = particleVec.distanceTo(firstParticle);
			if (distanceFromFirst > MAX_COMPASS_PARTICLE_SPREAD) {
				return;
			}

			if (distanceFromFirst >= lastParticleDistanceFromFirst) {
				lastParticleDistanceFromFirst = distanceFromFirst;
				lastParticle = particleVec;
				return;
			}

			// We get here when the second repetition of particles begins.
			// Since the second repetition overlaps with the last few particles
			// of the first repetition, the last particle we capture isn't truly the last.
			// But that's OK since subsequent particles will be on the same line.
			Line line = new Line(firstParticle, lastParticle);
			if (firstCompassLine == null) {
				firstCompassLine = line;
				resetForNewCompass();
				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW +
					"[NEU] Need another position to determine wishing compass target."));
				return;
			}

			secondCompassLine = line;
			solutionIntersectionLine = firstCompassLine.getIntersectionLineSegment(secondCompassLine);
			if (solutionIntersectionLine == null) {
				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED +
					"[NEU] Unable to determine wishing compass target."));
				logDiagnosticData(false);
				return;
			}

			solution = solutionIntersectionLine.getMidpoint();

			if (solution.distanceTo(firstParticle) < 8) {
				mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.YELLOW +
					"[NEU] WARNING: Solution is likely incorrect."));
				logDiagnosticData(false);
				return;
			}

			showSolution();
		} catch (Exception e) {
			mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED +
				"[NEU] Exception while calculating wishing compass solution - see log for details"));
			e.printStackTrace();
		}
	}

	private boolean isSolved() {
		return solution != null;
	}

	private void showSolution() {
		if (solution == null) return;
		String description = "[NEU] Wishing compass target: ";
		String coordsText = String.format("%.0f %.0f %.0f",
			solution.xCoord,
			solution.yCoord,
			solution.zCoord);

		if (NUCLEUS_BB.isVecInside(solution)) {
			description += "Crystal Nucleus (" + coordsText + ")";
		} else {
			description += coordsText;
		}

		ChatComponentText message = new ChatComponentText(EnumChatFormatting.YELLOW + description);

		if (isSkytilsPresent) {
			ChatStyle clickEvent = new ChatStyle().setChatClickEvent(
				new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/sthw add WishingTarget " + coordsText));
			clickEvent.setChatHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(
				EnumChatFormatting.YELLOW +
					"Add Skytils hollows waypoint")));
			message.setChatStyle(clickEvent);
		}

		Minecraft.getMinecraft().thePlayer.addChatMessage(message);
	}

	public void logDiagnosticData(boolean outputAlways) {
		if (SBInfo.getInstance().getLocation() == null) {
			mc.thePlayer.addChatMessage(new ChatComponentText(EnumChatFormatting.RED +
				"[NEU] This command is not available outside SkyBlock"));
			return;
		}

		if (!NotEnoughUpdates.INSTANCE.