diff options
author | bowser0000 <bowser0000@gmail.com> | 2020-12-04 15:50:07 -0500 |
---|---|---|
committer | bowser0000 <bowser0000@gmail.com> | 2020-12-04 15:50:07 -0500 |
commit | ad70213a1a01b758b850ab37267c21e52d2a5e40 (patch) | |
tree | 6f5081b2278b65804faf2b2e859fd0d49f22bfe1 /src/main/java/me/Danker/DankersSkyblockMod.java | |
parent | 8c16ffbba58226ded7bf84449caca98ecf48147c (diff) | |
download | SkyblockMod-ad70213a1a01b758b850ab37267c21e52d2a5e40.tar.gz SkyblockMod-ad70213a1a01b758b850ab37267c21e52d2a5e40.tar.bz2 SkyblockMod-ad70213a1a01b758b850ab37267c21e52d2a5e40.zip |
Add Tic Tac Toe puzzle solver
Uses minimax to find best move
Diffstat (limited to 'src/main/java/me/Danker/DankersSkyblockMod.java')
-rw-r--r-- | src/main/java/me/Danker/DankersSkyblockMod.java | 97 |
1 files changed, 95 insertions, 2 deletions
diff --git a/src/main/java/me/Danker/DankersSkyblockMod.java b/src/main/java/me/Danker/DankersSkyblockMod.java index 3cfd57c..4a1e9c7 100644 --- a/src/main/java/me/Danker/DankersSkyblockMod.java +++ b/src/main/java/me/Danker/DankersSkyblockMod.java @@ -4,6 +4,7 @@ import com.google.gson.JsonObject; import me.Danker.commands.*; import me.Danker.gui.*; import me.Danker.handlers.*; +import me.Danker.utils.TicTacToeUtils; import me.Danker.utils.Utils; import net.minecraft.block.Block; import net.minecraft.client.Minecraft; @@ -24,10 +25,12 @@ import net.minecraft.inventory.Container; import net.minecraft.inventory.ContainerChest; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.Slot; +import net.minecraft.item.ItemMap; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.*; import net.minecraft.world.World; +import net.minecraft.world.storage.MapData; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.client.GuiIngameForge; import net.minecraftforge.client.event.ClientChatReceivedEvent; @@ -111,6 +114,7 @@ public class DankersSkyblockMod static List<Vec3[]> creeperLines = new ArrayList<>(); static boolean prevInWaterRoom = false; static boolean inWaterRoom = false; + static AxisAlignedBB correctTicTacToeButton = null; static double dungeonStartTime = 0; static double bloodOpenTime = 0; @@ -2298,7 +2302,7 @@ public class DankersSkyblockMod ConfigHandler.writeStringConfig("misc", "display", DisplayCommand.display); } - if (ToggleCommand.creeperToggled && Utils.inDungeons && world != null) { + if (ToggleCommand.creeperToggled && Utils.inDungeons && world != null && player != null) { double x = player.posX; double y = player.posY; double z = player.posZ; @@ -2335,7 +2339,7 @@ public class DankersSkyblockMod } } - if (ToggleCommand.waterToggled && Utils.inDungeons && world != null) { + if (ToggleCommand.waterToggled && Utils.inDungeons && world != null && player != null) { // multi thread block checking new Thread(() -> { prevInWaterRoom = inWaterRoom; @@ -2482,6 +2486,92 @@ public class DankersSkyblockMod } } + if (ToggleCommand.ticTacToeToggled && Utils.inDungeons && world != null && player != null) { + correctTicTacToeButton = null; + AxisAlignedBB aabb = new AxisAlignedBB(player.posX - 6, player.posY - 6, player.posZ - 6, player.posX + 6, player.posY + 6, player.posZ + 6); + List<EntityItemFrame> itemFrames = world.getEntitiesWithinAABB(EntityItemFrame.class, aabb); + List<EntityItemFrame> itemFramesWithMaps = new ArrayList<>(); + // Find how many item frames have maps already placed + for (EntityItemFrame itemFrame : itemFrames) { + ItemStack item = itemFrame.getDisplayedItem(); + if (item == null || !(item.getItem() instanceof ItemMap)) continue; + MapData mapData = ((ItemMap) item.getItem()).getMapData(item, world); + if (mapData == null) continue; + + itemFramesWithMaps.add(itemFrame); + } + + // Only run when it's your turn + if (itemFramesWithMaps.size() != 9 && itemFramesWithMaps.size() % 2 == 1) { + char[][] board = new char[3][3]; + BlockPos leftmostRow = null; + int sign = 1; + char facing = 'X'; + for (EntityItemFrame itemFrame : itemFramesWithMaps) { + ItemStack map = itemFrame.getDisplayedItem(); + MapData mapData = ((ItemMap) map.getItem()).getMapData(map, world); + + // Find position on board + int row = 0; + int column; + sign = 1; + + if (itemFrame.facingDirection == EnumFacing.SOUTH || itemFrame.facingDirection == EnumFacing.WEST) { + sign = -1; + } + + BlockPos itemFramePos = new BlockPos(itemFrame.posX, Math.floor(itemFrame.posY), itemFrame.posZ); + for (int i = 2; i >= 0; i--) { + int realI = i * sign; + BlockPos blockPos = itemFramePos; + if (itemFrame.posX % 0.5 == 0) { + blockPos = itemFramePos.add(realI, 0, 0); + } else if (itemFrame.posZ % 0.5 == 0) { + blockPos = itemFramePos.add(0, 0, realI); + facing = 'Z'; + } + Block block = world.getBlockState(blockPos).getBlock(); + if (block == Blocks.air || block == Blocks.stone_button) { + leftmostRow = blockPos; + row = i; + break; + } + } + + if (itemFrame.posY == 72.5) { + column = 0; + } else if (itemFrame.posY == 71.5) { + column = 1; + } else if (itemFrame.posY == 70.5) { + column = 2; + } else { + continue; + } + + // Get colour + // Middle pixel = 64*128 + 64 = 8256 + int colourInt = mapData.colors[8256] & 255; + if (colourInt == 114) { + board[column][row] = 'X'; + } else if (colourInt == 33) { + board[column][row] = 'O'; + } + } + System.out.println("Board: " + Arrays.deepToString(board)); + + // Draw best move + int bestMove = TicTacToeUtils.getBestMove(board) - 1; + System.out.println("Best move slot: " + bestMove); + if (leftmostRow != null) { + double drawX = facing == 'X' ? leftmostRow.getX() - sign * (bestMove % 3) : leftmostRow.getX(); + double drawY = 72 - Math.floor(bestMove / 3); + double drawZ = facing == 'Z' ? leftmostRow.getZ() - sign * (bestMove % 3) : leftmostRow.getZ(); + + correctTicTacToeButton = new AxisAlignedBB(drawX, drawY, drawZ, drawX + 1, drawY + 1, drawZ + 1); + } + } + } + tickAmount = 0; } @@ -2594,6 +2684,9 @@ public class DankersSkyblockMod Utils.draw3DLine(creeperLines.get(i)[0], creeperLines.get(i)[1], CREEPER_COLOURS[i % 10], event.partialTicks); } } + if (ToggleCommand.ticTacToeToggled && correctTicTacToeButton != null) { + Utils.draw3DBox(correctTicTacToeButton, 0x40FF40, event.partialTicks); + } } @SubscribeEvent |