From d1fe7eee92dd4113b061739f4d70b52c9c8ec867 Mon Sep 17 00:00:00 2001 From: mezz Date: Wed, 4 Nov 2015 05:01:54 -0800 Subject: [PATCH] Clean up Escritoire code and behavior --- .../genetics/alleles/AlleleTreeSpecies.java | 2 +- .../core/gui/ContainerEscritoire.java | 31 +- .../java/forestry/core/gui/GuiEscritoire.java | 265 ++--------- .../core/gui/widgets/GameTokenWidget.java | 99 ++++ .../core/gui/widgets/ProbeButton.java | 65 +++ .../core/network/DataInputStreamForestry.java | 13 +- .../network/DataOutputStreamForestry.java | 4 + .../forestry/core/render/TextureManager.java | 2 +- .../forestry/core/tiles/EscritoireGame.java | 436 +++--------------- .../core/tiles/EscritoireGameBoard.java | 227 +++++++++ .../core/tiles/EscritoireGameToken.java | 165 +++++++ .../core/tiles/EscritoireTextSource.java | 87 ++++ .../forestry/core/tiles/TileEscritoire.java | 49 +- .../factory/recipes/RecipeMemory.java | 7 +- 14 files changed, 807 insertions(+), 645 deletions(-) create mode 100644 src/main/java/forestry/core/gui/widgets/GameTokenWidget.java create mode 100644 src/main/java/forestry/core/gui/widgets/ProbeButton.java create mode 100644 src/main/java/forestry/core/tiles/EscritoireGameBoard.java create mode 100644 src/main/java/forestry/core/tiles/EscritoireGameToken.java create mode 100644 src/main/java/forestry/core/tiles/EscritoireTextSource.java diff --git a/src/main/java/forestry/arboriculture/genetics/alleles/AlleleTreeSpecies.java b/src/main/java/forestry/arboriculture/genetics/alleles/AlleleTreeSpecies.java index 5635ab400..03c8d8c62 100644 --- a/src/main/java/forestry/arboriculture/genetics/alleles/AlleleTreeSpecies.java +++ b/src/main/java/forestry/arboriculture/genetics/alleles/AlleleTreeSpecies.java @@ -104,7 +104,7 @@ public class AlleleTreeSpecies extends AlleleSpecies implements IAlleleTreeSpeci @Override public int getIconColour(int renderPass) { - return 0xffffff; + return leafIconProvider.getColor(false); } @Override diff --git a/src/main/java/forestry/core/gui/ContainerEscritoire.java b/src/main/java/forestry/core/gui/ContainerEscritoire.java index ec3d542f6..922b62bca 100755 --- a/src/main/java/forestry/core/gui/ContainerEscritoire.java +++ b/src/main/java/forestry/core/gui/ContainerEscritoire.java @@ -16,6 +16,9 @@ import net.minecraft.entity.player.EntityPlayerMP; import forestry.core.gui.slots.SlotFiltered; import forestry.core.gui.slots.SlotOutput; import forestry.core.network.PacketGuiSelectRequest; +import forestry.core.network.PacketGuiUpdate; +import forestry.core.proxy.Proxies; +import forestry.core.tiles.EscritoireGame; import forestry.core.tiles.TileEscritoire; public class ContainerEscritoire extends ContainerTile implements IGuiSelectable { @@ -46,24 +49,26 @@ public class ContainerEscritoire extends ContainerTile implement @Override public void detectAndSendChanges() { super.detectAndSendChanges(); - if (lastUpdate == tile.getGame().getLastUpdate()) { - return; - } - lastUpdate = tile.getGame().getLastUpdate(); - tile.sendBoard(player); + long gameLastUpdate = tile.getGame().getLastUpdate(); + if (lastUpdate != gameLastUpdate) { + lastUpdate = gameLastUpdate; + Proxies.net.sendToPlayer(new PacketGuiUpdate(tile), player); + } } @Override public void handleSelectionRequest(EntityPlayerMP player, PacketGuiSelectRequest packet) { - if (!tile.getGame().isEnded()) { - int index = packet.getPrimaryIndex(); - if (index == -1) { - tile.probe(); - } else { - tile.getGame().choose(index); - tile.processTurnResult(player.getGameProfile()); - } + EscritoireGame.Status status = tile.getGame().getStatus(); + if (status != EscritoireGame.Status.PLAYING) { + return; + } + + int index = packet.getPrimaryIndex(); + if (index == -1) { + tile.probe(); + } else { + tile.choose(player.getGameProfile(), index); } } } diff --git a/src/main/java/forestry/core/gui/GuiEscritoire.java b/src/main/java/forestry/core/gui/GuiEscritoire.java index 6b7641d97..232edc100 100755 --- a/src/main/java/forestry/core/gui/GuiEscritoire.java +++ b/src/main/java/forestry/core/gui/GuiEscritoire.java @@ -10,11 +10,6 @@ ******************************************************************************/ package forestry.core.gui; -import com.google.common.collect.ArrayListMultimap; -import com.google.common.collect.ListMultimap; - -import java.util.List; - import net.minecraft.client.renderer.RenderHelper; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Items; @@ -24,195 +19,62 @@ import net.minecraft.util.EnumChatFormatting; import org.lwjgl.opengl.GL11; import forestry.core.config.Constants; +import forestry.core.gui.widgets.GameTokenWidget; +import forestry.core.gui.widgets.ProbeButton; import forestry.core.gui.widgets.Widget; -import forestry.core.gui.widgets.WidgetManager; -import forestry.core.network.PacketGuiSelectRequest; -import forestry.core.proxy.Proxies; -import forestry.core.render.SpriteSheet; -import forestry.core.render.TextureManager; import forestry.core.tiles.EscritoireGame; -import forestry.core.tiles.EscritoireGame.GameToken; +import forestry.core.tiles.EscritoireTextSource; import forestry.core.tiles.TileEscritoire; -import forestry.core.utils.StringUtil; public class GuiEscritoire extends GuiForestry { - - private enum Notes { - level1, level2, level3, level4, success, failure - } - - private static final ListMultimap researchNotes; - - static { - researchNotes = ArrayListMultimap.create(); - for (Notes notesLevel : Notes.values()) { - int levelCount = Integer.valueOf(StringUtil.localize("gui.escritoire.notes." + notesLevel + ".count")); - for (int i = 1; i <= levelCount; i++) { - String note = StringUtil.localize("gui.escritoire.notes." + notesLevel + '.' + i); - researchNotes.put(notesLevel, note); - } - } - } - - private class TokenSlot extends Widget { - - private final ItemStack HIDDEN_TOKEN; - private final int index; - - public TokenSlot(WidgetManager manager, int xPos, int yPos, int index) { - super(manager, xPos, yPos); - this.index = index; - - HIDDEN_TOKEN = new ItemStack(Items.book); - } - - private boolean hasToken() { - return inventory.getGame().getToken(index) != null; - } - - private GameToken getToken() { - return inventory.getGame().getToken(index); - } - - @Override - public void draw(int startX, int startY) { - - if (!hasToken()) { - return; - } - - int tokenColour = getToken().getTokenColour(); - - float colorR = (tokenColour >> 16 & 255) / 255.0F; - float colorG = (tokenColour >> 8 & 255) / 255.0F; - float colorB = (tokenColour & 255) / 255.0F; - - manager.gui.bindTexture(manager.gui.textureFile); - GL11.glColor4f(colorR, colorG, colorB, 1.0F); - manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, 228, 0, 22, 22); - GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F); - - ItemStack tokenStack = HIDDEN_TOKEN; - if (getToken().isVisible()) { - tokenStack = getToken().tokenStack; - } - - - GL11.glPushAttrib(GL11.GL_ENABLE_BIT); - GL11.glEnable(GL11.GL_DEPTH_TEST); - RenderHelper.enableGUIStandardItemLighting(); - manager.gui.drawItemStack(tokenStack, startX + xPos + 3, startY + yPos + 3); - RenderHelper.disableStandardItemLighting(); - GL11.glPopAttrib(); - - manager.gui.setZLevel(150f); - for (String ident : getToken().getOverlayIcons()) { - RenderHelper.enableGUIStandardItemLighting(); - Proxies.render.bindTexture(SpriteSheet.ITEMS); - manager.gui.drawTexturedModelRectFromIcon(startX + xPos + 3, startY + yPos + 3, TextureManager.getInstance().getDefault(ident), 16, 16); - RenderHelper.disableStandardItemLighting(); - } - manager.gui.setZLevel(0f); - } - - @Override - protected String getLegacyTooltip(EntityPlayer player) { - return hasToken() && getToken().isVisible() ? getToken().getTooltip() : null; - } - - @Override - public void handleMouseClick(int mouseX, int mouseY, int mouseButton) { - Proxies.net.sendToServer(new PacketGuiSelectRequest(index, 0)); - } - } - - private class ProbeButton extends Widget { - - private boolean pressed; - - public ProbeButton(WidgetManager manager, int xPos, int yPos) { - super(manager, xPos, yPos); - width = 22; - height = 25; - } - - @Override - public void draw(int startX, int startY) { - manager.gui.bindTexture(manager.gui.textureFile); - GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F); - manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, 228, pressed ? 47 : 22, width, height); - } - - @Override - protected String getLegacyTooltip(EntityPlayer player) { - return StringUtil.localize("gui.escritoire.probe"); - } - - @Override - public void handleMouseClick(int mouseX, int mouseY, int mouseButton) { - pressed = true; - Proxies.net.sendToServer(new PacketGuiSelectRequest(-1, 0)); - } - - @Override - public void handleMouseRelease(int mouseX, int mouseY, int eventType) { - if (pressed) { - pressed = false; - } - } - - @Override - public void handleMouseMove(int mouseX, int mouseY, int mouseButton, long time) { - if (widgetManager.getAtPosition(mouseX - getGuiLeft(), mouseY - getGuiTop()) != this) { - pressed = false; - } - } - } - - private final ItemStack LEVEL_ITEM; - private String researchNote = ""; - private long lastUpdate; + private final ItemStack LEVEL_ITEM = new ItemStack(Items.paper); + private final EscritoireTextSource textSource = new EscritoireTextSource(); public GuiEscritoire(EntityPlayer player, TileEscritoire tile) { super(Constants.TEXTURE_PATH_GUI + "/escritoire.png", new ContainerEscritoire(player, tile), tile); - LEVEL_ITEM = new ItemStack(Items.paper); - xSize = 228; ySize = 235; - widgetManager.add(new ProbeButton(widgetManager, 14, 16)); + widgetManager.add(new ProbeButton(this, widgetManager, 14, 16)); + + EscritoireGame game = tile.getGame(); // Inner ring - widgetManager.add(new TokenSlot(widgetManager, 115, 51, 0)); - widgetManager.add(new TokenSlot(widgetManager, 115, 77, 1)); - widgetManager.add(new TokenSlot(widgetManager, 94, 90, 2)); - widgetManager.add(new TokenSlot(widgetManager, 73, 77, 3)); - widgetManager.add(new TokenSlot(widgetManager, 73, 51, 4)); - widgetManager.add(new TokenSlot(widgetManager, 94, 38, 5)); + addTokenWidget(game, 115, 51, 0); + addTokenWidget(game, 115, 77, 1); + addTokenWidget(game, 94, 90, 2); + addTokenWidget(game, 73, 77, 3); + addTokenWidget(game, 73, 51, 4); + addTokenWidget(game, 94, 38, 5); // Outer ring - widgetManager.add(new TokenSlot(widgetManager, 115, 25, 6)); - widgetManager.add(new TokenSlot(widgetManager, 136, 38, 7)); - widgetManager.add(new TokenSlot(widgetManager, 136, 64, 8)); + addTokenWidget(game, 115, 25, 6); + addTokenWidget(game, 136, 38, 7); + addTokenWidget(game, 136, 64, 8); - widgetManager.add(new TokenSlot(widgetManager, 136, 90, 9)); - widgetManager.add(new TokenSlot(widgetManager, 115, 103, 10)); - widgetManager.add(new TokenSlot(widgetManager, 94, 116, 11)); + addTokenWidget(game, 136, 90, 9); + addTokenWidget(game, 115, 103, 10); + addTokenWidget(game, 94, 116, 11); - widgetManager.add(new TokenSlot(widgetManager, 73, 103, 12)); - widgetManager.add(new TokenSlot(widgetManager, 52, 90, 13)); - widgetManager.add(new TokenSlot(widgetManager, 52, 64, 14)); + addTokenWidget(game, 73, 103, 12); + addTokenWidget(game, 52, 90, 13); + addTokenWidget(game, 52, 64, 14); - widgetManager.add(new TokenSlot(widgetManager, 52, 38, 15)); - widgetManager.add(new TokenSlot(widgetManager, 73, 25, 16)); - widgetManager.add(new TokenSlot(widgetManager, 94, 12, 17)); + addTokenWidget(game, 52, 38, 15); + addTokenWidget(game, 73, 25, 16); + addTokenWidget(game, 94, 12, 17); // Corners - widgetManager.add(new TokenSlot(widgetManager, 52, 12, 18)); - widgetManager.add(new TokenSlot(widgetManager, 136, 12, 19)); - widgetManager.add(new TokenSlot(widgetManager, 52, 116, 20)); - widgetManager.add(new TokenSlot(widgetManager, 136, 116, 21)); + addTokenWidget(game, 52, 12, 18); + addTokenWidget(game, 136, 12, 19); + addTokenWidget(game, 52, 116, 20); + addTokenWidget(game, 136, 116, 21); + } + + private void addTokenWidget(EscritoireGame game, int x, int y, int index) { + Widget gameTokenWidget = new GameTokenWidget(game, widgetManager, x, y, index); + widgetManager.add(gameTokenWidget); } @Override @@ -227,53 +89,20 @@ public class GuiEscritoire extends GuiForestry= EscritoireGame.BOUNTY_MAX) { - researchNote = getRandomNote(researchNotes.get(Notes.level1)); - } else if (bounty > EscritoireGame.BOUNTY_MAX / 2) { - researchNote = getRandomNote(researchNotes.get(Notes.level2)); - } else if (bounty > EscritoireGame.BOUNTY_MAX / 4) { - researchNote = getRandomNote(researchNotes.get(Notes.level3)); - } else { - researchNote = getRandomNote(researchNotes.get(Notes.level4)); - } - } - } - - lastUpdate = inventory.getGame().getLastUpdate(); - return researchNote; - } - - private String getRandomNote(List candidates) { - int index = mc.theWorld.rand.nextInt(candidates.size()); - return candidates.get(index); - } } diff --git a/src/main/java/forestry/core/gui/widgets/GameTokenWidget.java b/src/main/java/forestry/core/gui/widgets/GameTokenWidget.java new file mode 100644 index 000000000..dd9d67dad --- /dev/null +++ b/src/main/java/forestry/core/gui/widgets/GameTokenWidget.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.gui.widgets; + +import net.minecraft.client.renderer.RenderHelper; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Items; +import net.minecraft.item.ItemStack; + +import org.lwjgl.opengl.GL11; + +import forestry.core.network.PacketGuiSelectRequest; +import forestry.core.proxy.Proxies; +import forestry.core.render.SpriteSheet; +import forestry.core.render.TextureManager; +import forestry.core.tiles.EscritoireGame; +import forestry.core.tiles.EscritoireGameToken; + +public class GameTokenWidget extends Widget { + private final ItemStack HIDDEN_TOKEN = new ItemStack(Items.book); + + private final EscritoireGame game; + private final int index; + + public GameTokenWidget(EscritoireGame game, WidgetManager manager, int xPos, int yPos, int index) { + super(manager, xPos, yPos); + this.game = game; + this.index = index; + } + + private EscritoireGameToken getToken() { + return game.getToken(index); + } + + @Override + public void draw(int startX, int startY) { + + EscritoireGameToken token = getToken(); + if (token == null) { + return; + } + + int tokenColour = token.getTokenColour(); + + float colorR = (tokenColour >> 16 & 255) / 255.0F; + float colorG = (tokenColour >> 8 & 255) / 255.0F; + float colorB = (tokenColour & 255) / 255.0F; + + Proxies.render.bindTexture(manager.gui.textureFile); + + GL11.glColor4f(colorR, colorG, colorB, 1.0F); + manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, 228, 0, 22, 22); + GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F); + + ItemStack tokenStack = HIDDEN_TOKEN; + if (token.isVisible()) { + tokenStack = token.getTokenStack(); + } + + GL11.glPushAttrib(GL11.GL_ENABLE_BIT); + GL11.glEnable(GL11.GL_DEPTH_TEST); + RenderHelper.enableGUIStandardItemLighting(); + manager.gui.drawItemStack(tokenStack, startX + xPos + 3, startY + yPos + 3); + RenderHelper.disableStandardItemLighting(); + GL11.glPopAttrib(); + + manager.gui.setZLevel(150f); + for (String ident : getToken().getOverlayIcons()) { + RenderHelper.enableGUIStandardItemLighting(); + Proxies.render.bindTexture(SpriteSheet.ITEMS); + manager.gui.drawTexturedModelRectFromIcon(startX + xPos + 3, startY + yPos + 3, TextureManager.getInstance().getDefault(ident), 16, 16); + RenderHelper.disableStandardItemLighting(); + } + manager.gui.setZLevel(0f); + } + + @Override + protected String getLegacyTooltip(EntityPlayer player) { + EscritoireGameToken token = getToken(); + if (token == null || !token.isVisible()) { + return null; + } + + return token.getTooltip(); + } + + @Override + public void handleMouseClick(int mouseX, int mouseY, int mouseButton) { + Proxies.net.sendToServer(new PacketGuiSelectRequest(index, 0)); + } +} diff --git a/src/main/java/forestry/core/gui/widgets/ProbeButton.java b/src/main/java/forestry/core/gui/widgets/ProbeButton.java new file mode 100644 index 000000000..cd758bd25 --- /dev/null +++ b/src/main/java/forestry/core/gui/widgets/ProbeButton.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.gui.widgets; + +import net.minecraft.entity.player.EntityPlayer; + +import org.lwjgl.opengl.GL11; + +import forestry.core.gui.GuiEscritoire; +import forestry.core.network.PacketGuiSelectRequest; +import forestry.core.proxy.Proxies; +import forestry.core.utils.StringUtil; + +public class ProbeButton extends Widget { + + private final GuiEscritoire guiEscritoire; + private boolean pressed; + + public ProbeButton(GuiEscritoire guiEscritoire, WidgetManager manager, int xPos, int yPos) { + super(manager, xPos, yPos); + this.guiEscritoire = guiEscritoire; + width = 22; + height = 25; + } + + @Override + public void draw(int startX, int startY) { + GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0F); + Proxies.render.bindTexture(manager.gui.textureFile); + manager.gui.drawTexturedModalRect(startX + xPos, startY + yPos, 228, pressed ? 47 : 22, width, height); + } + + @Override + protected String getLegacyTooltip(EntityPlayer player) { + return StringUtil.localize("gui.escritoire.probe"); + } + + @Override + public void handleMouseClick(int mouseX, int mouseY, int mouseButton) { + pressed = true; + Proxies.net.sendToServer(new PacketGuiSelectRequest(-1, 0)); + } + + @Override + public void handleMouseRelease(int mouseX, int mouseY, int eventType) { + if (pressed) { + pressed = false; + } + } + + @Override + public void handleMouseMove(int mouseX, int mouseY, int mouseButton, long time) { + if (manager.getAtPosition(mouseX - guiEscritoire.getGuiLeft(), mouseY - guiEscritoire.getGuiTop()) != this) { + pressed = false; + } + } +} diff --git a/src/main/java/forestry/core/network/DataInputStreamForestry.java b/src/main/java/forestry/core/network/DataInputStreamForestry.java index da13d938d..406d815cd 100644 --- a/src/main/java/forestry/core/network/DataInputStreamForestry.java +++ b/src/main/java/forestry/core/network/DataInputStreamForestry.java @@ -4,7 +4,6 @@ import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.List; import net.minecraft.inventory.IInventory; @@ -78,16 +77,15 @@ public class DataInputStreamForestry extends DataInputStream { return null; } - public List readStreamables(Class streamableClass) throws IOException { + public void readStreamables(List outputList, Class streamableClass) throws IOException { + outputList.clear(); int length = readVarInt(); - List streamables = new ArrayList<>(length); if (length > 0) { for (int i = 0; i < length; i++) { T streamable = readStreamable(streamableClass); - streamables.add(streamable); + outputList.add(streamable); } } - return streamables; } /** @@ -111,6 +109,11 @@ public class DataInputStreamForestry extends DataInputStream { return varInt; } + public > T readEnum(Class enumClass) throws IOException { + int ordinal = readVarInt(); + return enumClass.getEnumConstants()[ordinal]; + } + public NBTTagCompound readNBTTagCompound() throws IOException { int length = readVarInt(); diff --git a/src/main/java/forestry/core/network/DataOutputStreamForestry.java b/src/main/java/forestry/core/network/DataOutputStreamForestry.java index 60e38cae8..3e7922329 100644 --- a/src/main/java/forestry/core/network/DataOutputStreamForestry.java +++ b/src/main/java/forestry/core/network/DataOutputStreamForestry.java @@ -86,6 +86,10 @@ public class DataOutputStreamForestry extends DataOutputStream { writeByte(varInt); } + public void writeEnum(Enum enumValue) throws IOException { + writeVarInt(enumValue.ordinal()); + } + public void writeNBTTagCompound(NBTTagCompound nbttagcompound) throws IOException { if (nbttagcompound == null) { writeVarInt(-1); diff --git a/src/main/java/forestry/core/render/TextureManager.java b/src/main/java/forestry/core/render/TextureManager.java index 29b3d9486..d407485a1 100755 --- a/src/main/java/forestry/core/render/TextureManager.java +++ b/src/main/java/forestry/core/render/TextureManager.java @@ -53,7 +53,7 @@ public class TextureManager implements ITextureManager { "analyzer/anything", "analyzer/bee", "analyzer/cave", "analyzer/closed", "analyzer/drone", "analyzer/flyer", "analyzer/item", "analyzer/nocturnal", "analyzer/princess", "analyzer/pure_breed", "analyzer/pure_cave", "analyzer/pure_flyer", "analyzer/pure_nocturnal", "analyzer/queen", - "particles/swarm_bee", "errors/errored", + "particles/swarm_bee", "errors/errored", "errors/unknown", "slots/blocked", "slots/blocked_2", "slots/liquid", "slots/container", "slots/locked", "mail/carrier.player", "mail/carrier.trader"}; for (String str : defaultIconNames) { diff --git a/src/main/java/forestry/core/tiles/EscritoireGame.java b/src/main/java/forestry/core/tiles/EscritoireGame.java index d5b85a363..24f49f70c 100644 --- a/src/main/java/forestry/core/tiles/EscritoireGame.java +++ b/src/main/java/forestry/core/tiles/EscritoireGame.java @@ -11,130 +11,40 @@ package forestry.core.tiles; import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; import java.util.Random; import net.minecraft.inventory.IInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import forestry.api.core.INBTTagable; import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAlleleSpecies; import forestry.api.genetics.IIndividual; -import forestry.api.genetics.ISpeciesRoot; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.IStreamable; -import forestry.core.utils.ColourUtil; -import forestry.core.utils.StringUtil; public class EscritoireGame implements INBTTagable, IStreamable { - + private static final Random rand = new Random(); public static final int BOUNTY_MAX = 16; - public static class GameToken implements INBTTagable, IStreamable { - - private static final String[] OVERLAY_NONE = new String[0]; - private static final String[] OVERLAY_FAILED = new String[]{"errors/errored"}; - - public ItemStack tokenStack; - - protected boolean isFailed = false; - protected boolean isProbed = false; - protected boolean isRevealed = false; - - public GameToken() { - - } - - public GameToken(ItemStack tokenStack) { - this.tokenStack = tokenStack; - } - - public GameToken(NBTTagCompound nbttagcompound) { - readFromNBT(nbttagcompound); - } - - @Override - public void readFromNBT(NBTTagCompound nbttagcompound) { - isFailed = nbttagcompound.getBoolean("isFailed"); - isProbed = nbttagcompound.getBoolean("isProbed"); - isRevealed = nbttagcompound.getBoolean("isRevealed"); - - if (nbttagcompound.hasKey("tokenStack")) { - tokenStack = ItemStack.loadItemStackFromNBT(nbttagcompound.getCompoundTag("tokenStack")); - } - } - - @Override - public void writeToNBT(NBTTagCompound nbttagcompound) { - nbttagcompound.setBoolean("isFailed", isFailed); - nbttagcompound.setBoolean("isProbed", isProbed); - nbttagcompound.setBoolean("isRevealed", isRevealed); - - if (tokenStack != null) { - NBTTagCompound stackcompound = new NBTTagCompound(); - tokenStack.writeToNBT(stackcompound); - nbttagcompound.setTag("tokenStack", stackcompound); - } - } - - /* NETWORK */ - @Override - public void writeData(DataOutputStreamForestry data) throws IOException { - data.writeBoolean(isFailed); - data.writeBoolean(isProbed); - data.writeBoolean(isRevealed); - data.writeItemStack(tokenStack); - } - - @Override - public void readData(DataInputStreamForestry data) throws IOException { - isFailed = data.readBoolean(); - isProbed = data.readBoolean(); - isRevealed = data.readBoolean(); - tokenStack = data.readItemStack(); - } - - public boolean isVisible() { - return isRevealed; - } - - public int getTokenColour() { - if (tokenStack == null || !isVisible()) { - return 0xffffff; - } - - if (isProbed) { - return ColourUtil.multiplyRGBComponents(AlleleManager.alleleRegistry.getIndividual(tokenStack).getGenome().getPrimary().getIconColour(0), 0.7f); - } else { - return AlleleManager.alleleRegistry.getIndividual(tokenStack).getGenome().getPrimary().getIconColour(0); - } - } - - public String getTooltip() { - return tokenStack != null ? tokenStack.getDisplayName() : StringUtil.localize("gui.unknown"); - } - - public String[] getOverlayIcons() { - return isFailed ? OVERLAY_FAILED : OVERLAY_NONE; - } - - public boolean matches(GameToken other) { - return tokenStack.isItemEqual(other.tokenStack) && ItemStack.areItemStackTagsEqual(tokenStack, other.tokenStack); - } + public enum Status { + EMPTY, PLAYING, FAILURE, SUCCESS } - private final Random rand = new Random(); - private GameToken[] gameTokens; + private final EscritoireGameBoard gameBoard = new EscritoireGameBoard(); private long lastUpdate; - - private boolean isEnded; private int bountyLevel; + private Status status = Status.EMPTY; + + public EscritoireGameToken getToken(int index) { + return gameBoard.getToken(index); + } + + public Status getStatus() { + return status; + } public long getLastUpdate() { return lastUpdate; @@ -143,48 +53,22 @@ public class EscritoireGame implements INBTTagable, IStreamable { /* SAVING & LOADING */ @Override public void writeToNBT(NBTTagCompound nbttagcompound) { - nbttagcompound.setBoolean("isEnded", isEnded); nbttagcompound.setInteger("bountyLevel", bountyLevel); nbttagcompound.setLong("lastUpdate", lastUpdate); + gameBoard.writeToNBT(nbttagcompound); - if (gameTokens != null) { - nbttagcompound.setInteger("TokenCount", gameTokens.length); - NBTTagList nbttaglist = new NBTTagList(); - - for (int i = 0; i < gameTokens.length; i++) { - if (gameTokens[i] == null) { - continue; - } - - NBTTagCompound nbttagcompound2 = new NBTTagCompound(); - nbttagcompound2.setByte("Slot", (byte) i); - gameTokens[i].writeToNBT(nbttagcompound2); - nbttaglist.appendTag(nbttagcompound2); - } - - nbttagcompound.setTag("GameTokens", nbttaglist); - } else { - nbttagcompound.setInteger("TokenCount", 0); - } - + nbttagcompound.setInteger("Status", status.ordinal()); } @Override public void readFromNBT(NBTTagCompound nbttagcompound) { - isEnded = nbttagcompound.getBoolean("isEnded"); bountyLevel = nbttagcompound.getInteger("bountyLevel"); lastUpdate = nbttagcompound.getLong("lastUpdate"); + gameBoard.readFromNBT(nbttagcompound); - int tokenCount = nbttagcompound.getInteger("TokenCount"); - if (tokenCount > 0) { - gameTokens = new GameToken[tokenCount]; - NBTTagList nbttaglist = nbttagcompound.getTagList("GameTokens", 10); - - for (int j = 0; j < nbttaglist.tagCount(); ++j) { - NBTTagCompound nbttagcompound2 = nbttaglist.getCompoundTagAt(j); - int index = nbttagcompound2.getByte("Slot"); - gameTokens[index] = new GameToken(nbttagcompound2); - } + if (nbttagcompound.hasKey("Status")) { + int statusOrdinal = nbttagcompound.getInteger("Status"); + status = Status.values()[statusOrdinal]; } lastUpdate = System.currentTimeMillis(); @@ -193,301 +77,91 @@ public class EscritoireGame implements INBTTagable, IStreamable { /* NETWORK */ @Override public void writeData(DataOutputStreamForestry data) throws IOException { - data.writeBoolean(isEnded); data.writeInt(bountyLevel); - data.writeLong(lastUpdate); - - List gameTokensList = gameTokens == null ? null : Arrays.asList(gameTokens); - data.writeStreamables(gameTokensList); + gameBoard.writeData(data); + data.writeEnum(status); } @Override public void readData(DataInputStreamForestry data) throws IOException { - isEnded = data.readBoolean(); bountyLevel = data.readInt(); - lastUpdate = data.readLong(); - - List gameTokensList = data.readStreamables(GameToken.class); - if (gameTokensList != null) { - this.gameTokens = gameTokensList.toArray(new GameToken[gameTokensList.size()]); - } + gameBoard.readData(data); + status = data.readEnum(Status.class); } /* INTERACTION */ public void initialize(ItemStack specimen) { - IIndividual individual = AlleleManager.alleleRegistry.getIndividual(specimen); - if (individual == null) { - return; + if (gameBoard.initialize(specimen)) { + status = Status.PLAYING; + bountyLevel = BOUNTY_MAX; + lastUpdate = System.currentTimeMillis(); } - - int boardSize = individual.getGenome().getPrimary().getComplexity() + individual.getGenome().getSecondary().getComplexity(); - if (boardSize % 2 != 0) { - boardSize = Math.round((float) boardSize / 2) * 2; - } - if (boardSize > 22) { - boardSize = 22; - } else if (boardSize < 6) { - boardSize = 6; - } - - isEnded = false; - bountyLevel = BOUNTY_MAX; - gameTokens = new GameToken[boardSize]; - - ISpeciesRoot root = individual.getGenome().getPrimary().getRoot(); - ArrayList pairs = new ArrayList<>(); - for (int i = 0; i < boardSize / 2; i++) { - IIndividual token = root.templateAsIndividual(root.getRandomTemplate(rand)); - pairs.add(root.getMemberStack(token, 0)); - } - - boolean first = true; - for (ItemStack pair : pairs) { - if (first) { - gameTokens[0] = new GameToken(pair.copy()); - first = false; - } else { - gameTokens[getFreeTokenIndex()] = new GameToken(pair.copy()); - } - gameTokens[getFreeTokenIndex()] = new GameToken(pair.copy()); - } - - lastUpdate = System.currentTimeMillis(); - } - - private int getFreeTokenIndex() { - while (true) { - int probe = rand.nextInt(gameTokens.length); - if (gameTokens[probe] == null) { - return probe; - } - } - } - - private int countUnrevealedTokens() { - int count = 0; - for (GameToken token : gameTokens) { - if (!token.isRevealed) { - count++; - } - } - - return count; - } - - private int[] getUnrevealedTokens(int count) { - ArrayList existing = new ArrayList<>(); - int remaining = countUnrevealedTokens(); - int[] unrevealed = new int[count < remaining ? count : remaining]; - - for (int i = 0; i < unrevealed.length; i++) { - - int found = -1; - while (found < 0) { - int probe = rand.nextInt(gameTokens.length); - if (gameTokens[probe].isRevealed) { - continue; - } - - boolean taken = false; - for (int exist : existing) { - if (exist == probe) { - taken = true; - break; - } - } - if (!taken) { - existing.add(probe); - found = probe; - } - } - unrevealed[i] = found; - } - - return unrevealed; } public void probe(ItemStack specimen, IInventory inventory, int startSlot, int slotCount) { - if (gameTokens == null) { - return; - } - if (isEnded) { + if (status != Status.PLAYING) { return; } + IIndividual individual = AlleleManager.alleleRegistry.getIndividual(specimen); if (individual == null) { return; } - hideProbedTokens(); if (bountyLevel > 1) { bountyLevel--; } - int[] tokenIndices = getUnrevealedTokens(getSampleSize() <= slotCount ? getSampleSize() : slotCount); - int processedTokens = 0; + IAlleleSpecies species = individual.getGenome().getPrimary(); + gameBoard.hideProbedTokens(); - for (int i = 0; i < slotCount; i++) { - ItemStack sample = inventory.getStackInSlot(startSlot + i); + int revealCount = getSampleSize(slotCount); + for (int i = 0; i < revealCount; i++) { + ItemStack sample = inventory.decrStackSize(startSlot + i, 1); if (sample == null || sample.stackSize <= 0) { continue; } - sample = inventory.decrStackSize(startSlot + i, 1); - if (rand.nextFloat() >= individual.getGenome().getPrimary().getResearchSuitability(sample)) { - continue; - } - - GameToken token = gameTokens[tokenIndices[processedTokens]]; - token.isProbed = true; - token.isRevealed = true; - - processedTokens++; - if (processedTokens >= tokenIndices.length) { - break; + if (rand.nextFloat() < species.getResearchSuitability(sample)) { + gameBoard.probe(); } } lastUpdate = System.currentTimeMillis(); } - public void choose(final int tokenIndex) { - if (isEnded) { - return; - } - if (gameTokens == null || tokenIndex >= gameTokens.length) { - return; - } - - GameToken token = gameTokens[tokenIndex]; - if (token.isRevealed && !token.isProbed) { - return; - } - hideProbedTokens(); - - Collection singles = getRevealedSingles(token); - if (singles.size() > 0) { - boolean matched = false; - for (GameToken single : singles) { - if (single.matches(token)) { - matched = true; - break; - } - } - if (!matched) { - token.isFailed = true; - } - } - token.isRevealed = true; - checkGameEnd(); - lastUpdate = System.currentTimeMillis(); - } - public void reset() { - gameTokens = null; + bountyLevel = BOUNTY_MAX; + gameBoard.reset(); + status = Status.EMPTY; + lastUpdate = System.currentTimeMillis(); } - public boolean isInited() { - return gameTokens != null; - } + public void choose(int tokenIndex) { + if (getStatus() != Status.PLAYING) { + return; + } - public boolean isEnded() { - return isEnded; + EscritoireGameToken token = gameBoard.getToken(tokenIndex); + if (token != null) { + status = gameBoard.choose(token); + lastUpdate = System.currentTimeMillis(); + } } public int getBountyLevel() { return bountyLevel; } - public boolean isWon() { - if (!isEnded) { - return false; - } - - return !isLost(); - } - - private void checkGameEnd() { - if (isLost()) { - isEnded = true; - } - if (isRevealed()) { - isEnded = true; - } - } - - private boolean isRevealed() { - for (GameToken token : gameTokens) { - if (!token.isRevealed) { - return false; - } - } - - return true; - } - - private boolean isLost() { - for (GameToken token : gameTokens) { - if (token.isFailed) { - return true; - } - } - - return false; - } - - private void hideProbedTokens() { - for (GameToken token : gameTokens) { - if (token.isProbed) { - token.isRevealed = false; - token.isProbed = false; - } - } - } - - private Collection getRevealedSingles(GameToken exclude) { - ArrayList singles = new ArrayList<>(); - - for (GameToken token : gameTokens) { - if (!token.isRevealed || token == exclude) { - continue; - } - - GameToken matching = null; - for (GameToken single : singles) { - if (!single.matches(token)) { - continue; - } - matching = single; - break; - } - - if (matching == null) { - singles.add(token); - } else { - singles.remove(matching); - } - } - - return singles; - } - /* RETRIEVAL */ - public int getSampleSize() { - if (gameTokens == null) { + public int getSampleSize(int slotCount) { + if (status == Status.EMPTY) { return 0; } - int samples = gameTokens.length / 4; - return Math.max(samples, 2); - } - - public GameToken getToken(int index) { - if (gameTokens != null && index < gameTokens.length) { - return gameTokens[index]; - } - - return null; + int samples = gameBoard.getTokenCount() / 4; + samples = Math.max(samples, 2); + return Math.min(samples, slotCount); } } diff --git a/src/main/java/forestry/core/tiles/EscritoireGameBoard.java b/src/main/java/forestry/core/tiles/EscritoireGameBoard.java new file mode 100644 index 000000000..178ae9280 --- /dev/null +++ b/src/main/java/forestry/core/tiles/EscritoireGameBoard.java @@ -0,0 +1,227 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.tiles; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.nbt.NBTTagList; + +import forestry.api.core.INBTTagable; +import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAlleleSpecies; +import forestry.api.genetics.IGenome; +import forestry.api.genetics.IIndividual; +import forestry.api.genetics.ISpeciesRoot; +import forestry.core.network.DataInputStreamForestry; +import forestry.core.network.DataOutputStreamForestry; +import forestry.core.network.IStreamable; + +public class EscritoireGameBoard implements INBTTagable, IStreamable { + private static final Random rand = new Random(); + private static final int TOKEN_COUNT_MAX = 22; + private static final int TOKEN_COUNT_MIN = 6; + + private final List gameTokens = new ArrayList<>(TOKEN_COUNT_MAX); + private int tokenCount; + + + public boolean initialize(ItemStack specimen) { + IIndividual individual = AlleleManager.alleleRegistry.getIndividual(specimen); + if (individual == null) { + return false; + } + + IGenome genome = individual.getGenome(); + ISpeciesRoot root = genome.getPrimary().getRoot(); + + tokenCount = getTokenCount(genome); + + for (int i = 0; i < tokenCount / 2; i++) { + IIndividual token = root.templateAsIndividual(root.getRandomTemplate(rand)); + ItemStack pair = root.getMemberStack(token, 0); + gameTokens.add(new EscritoireGameToken(pair)); + gameTokens.add(new EscritoireGameToken(pair)); + } + Collections.shuffle(gameTokens); + return true; + } + + public EscritoireGameToken getToken(int index) { + if (index >= tokenCount) { + return null; + } + return gameTokens.get(index); + } + + public int getTokenCount() { + return tokenCount; + } + + public void hideProbedTokens() { + for (EscritoireGameToken token : gameTokens) { + if (token.isProbed()) { + token.setProbed(false); + } + } + } + + private List getUnrevealedTokens() { + List unrevealed = new ArrayList<>(); + for (EscritoireGameToken token : gameTokens) { + if (!token.isVisible()) { + unrevealed.add(token); + } + } + + return unrevealed; + } + + private EscritoireGameToken getSelected() { + for (EscritoireGameToken token : gameTokens) { + if (token.isSelected()) { + return token; + } + } + + return null; + } + + private boolean isBoardCleared() { + for (EscritoireGameToken token : gameTokens) { + if (!token.isMatched()) { + return false; + } + } + + return true; + } + + public void probe() { + List tokens = getUnrevealedTokens(); + int index = rand.nextInt(tokens.size()); + + EscritoireGameToken token = tokens.get(index); + token.setProbed(true); + } + + public EscritoireGame.Status choose(EscritoireGameToken token) { + EscritoireGame.Status status = EscritoireGame.Status.PLAYING; + if (token.isMatched() || token.isSelected()) { + return status; + } + + EscritoireGameToken selected = getSelected(); + if (selected == null) { + token.setSelected(); + hideProbedTokens(); + } else if (token.matches(selected)) { + selected.setMatched(); + token.setMatched(); + if (isBoardCleared()) { + status = EscritoireGame.Status.SUCCESS; + } + hideProbedTokens(); + } else { + token.setFailed(); + selected.setFailed(); + status = EscritoireGame.Status.FAILURE; + } + + return status; + } + + public void reset() { + gameTokens.clear(); + tokenCount = 0; + } + + private static int getTokenCount(IGenome genome) { + IAlleleSpecies species1 = genome.getPrimary(); + IAlleleSpecies species2 = genome.getSecondary(); + + int tokenCount = species1.getComplexity() + species2.getComplexity(); + + if (tokenCount % 2 != 0) { + tokenCount = Math.round((float) tokenCount / 2) * 2; + } + + if (tokenCount > TOKEN_COUNT_MAX) { + tokenCount = TOKEN_COUNT_MAX; + } else if (tokenCount < TOKEN_COUNT_MIN) { + tokenCount = TOKEN_COUNT_MIN; + } + + return tokenCount; + } + + /* INBTTagable */ + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + tokenCount = nbttagcompound.getInteger("TokenCount"); + gameTokens.clear(); + + if (tokenCount > 0) { + EscritoireGameToken[] tokens = new EscritoireGameToken[tokenCount]; + NBTTagList nbttaglist = nbttagcompound.getTagList("GameTokens", 10); + + for (int j = 0; j < nbttaglist.tagCount(); ++j) { + NBTTagCompound nbttagcompound2 = nbttaglist.getCompoundTagAt(j); + int index = nbttagcompound2.getByte("Slot"); + tokens[index] = new EscritoireGameToken(nbttagcompound2); + } + + Collections.addAll(gameTokens, tokens); + } + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + if (tokenCount > 0) { + nbttagcompound.setInteger("TokenCount", tokenCount); + NBTTagList nbttaglist = new NBTTagList(); + + for (int i = 0; i < tokenCount; i++) { + EscritoireGameToken token = gameTokens.get(i); + if (token == null) { + continue; + } + + NBTTagCompound nbttagcompound2 = new NBTTagCompound(); + nbttagcompound2.setByte("Slot", (byte) i); + token.writeToNBT(nbttagcompound2); + nbttaglist.appendTag(nbttagcompound2); + } + + nbttagcompound.setTag("GameTokens", nbttaglist); + } else { + nbttagcompound.setInteger("TokenCount", 0); + } + } + + /* IStreamable */ + @Override + public void writeData(DataOutputStreamForestry data) throws IOException { + data.writeVarInt(tokenCount); + data.writeStreamables(gameTokens); + } + + @Override + public void readData(DataInputStreamForestry data) throws IOException { + tokenCount = data.readVarInt(); + data.readStreamables(gameTokens, EscritoireGameToken.class); + } +} diff --git a/src/main/java/forestry/core/tiles/EscritoireGameToken.java b/src/main/java/forestry/core/tiles/EscritoireGameToken.java new file mode 100644 index 000000000..f7207eef3 --- /dev/null +++ b/src/main/java/forestry/core/tiles/EscritoireGameToken.java @@ -0,0 +1,165 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.tiles; + +import java.io.IOException; + +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; + +import forestry.api.core.INBTTagable; +import forestry.api.genetics.AlleleManager; +import forestry.core.network.DataInputStreamForestry; +import forestry.core.network.DataOutputStreamForestry; +import forestry.core.network.IStreamable; +import forestry.core.utils.ColourUtil; +import forestry.core.utils.StringUtil; + +public class EscritoireGameToken implements INBTTagable, IStreamable { + + private enum State { + UNREVEALED,// face down + PROBED, // shown by escritoire probe action + SELECTED, // selected by the user as the first half of a match + MATCHED, // successfully matched + FAILED // failed to match + } + + private static final String[] OVERLAY_NONE = new String[0]; + private static final String[] OVERLAY_FAILED = new String[]{"errors/errored"}; + private static final String[] OVERLAY_SELECTED = new String[]{"errors/unknown"}; + + private ItemStack tokenStack; + private State state = State.UNREVEALED; + + @SuppressWarnings("unused") + public EscritoireGameToken() { + // required for IStreamable serialization + } + + public EscritoireGameToken(ItemStack tokenStack) { + this.tokenStack = tokenStack; + } + + public EscritoireGameToken(NBTTagCompound nbttagcompound) { + readFromNBT(nbttagcompound); + } + + public ItemStack getTokenStack() { + return tokenStack; + } + + public boolean isVisible() { + return state != State.UNREVEALED; + } + + public boolean isProbed() { + return state == State.PROBED; + } + + public boolean isMatched() { + return state == State.MATCHED; + } + + public boolean isSelected() { + return state == State.SELECTED; + } + + public void setFailed() { + state = State.FAILED; + } + + public void setProbed(boolean probed) { + if (probed) { + state = State.PROBED; + } else { + state = State.UNREVEALED; + } + } + + public void setSelected() { + state = State.SELECTED; + } + + public void setMatched() { + state = State.MATCHED; + } + + public int getTokenColour() { + if (tokenStack == null || !isVisible()) { + return 0xffffff; + } + + int iconColor = AlleleManager.alleleRegistry.getIndividual(tokenStack).getGenome().getPrimary().getIconColour(0); + + if (state == State.MATCHED) { + return ColourUtil.multiplyRGBComponents(iconColor, 0.7f); + } else { + return iconColor; + } + } + + public String getTooltip() { + return tokenStack != null ? tokenStack.getDisplayName() : StringUtil.localize("gui.unknown"); + } + + public String[] getOverlayIcons() { + switch (state) { + case FAILED: + return OVERLAY_FAILED; + case SELECTED: + return OVERLAY_SELECTED; + default: + return OVERLAY_NONE; + } + } + + public boolean matches(EscritoireGameToken other) { + return ItemStack.areItemStacksEqual(tokenStack, other.getTokenStack()); + } + + /* INBTTagable */ + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + if (nbttagcompound.hasKey("state")) { + int stateOrdinal = nbttagcompound.getInteger("state"); + state = State.values()[stateOrdinal]; + } + + if (nbttagcompound.hasKey("tokenStack")) { + tokenStack = ItemStack.loadItemStackFromNBT(nbttagcompound.getCompoundTag("tokenStack")); + } + } + + @Override + public void writeToNBT(NBTTagCompound nbttagcompound) { + nbttagcompound.setInteger("state", state.ordinal()); + + if (tokenStack != null) { + NBTTagCompound stackcompound = new NBTTagCompound(); + tokenStack.writeToNBT(stackcompound); + nbttagcompound.setTag("tokenStack", stackcompound); + } + } + + /* IStreamable */ + @Override + public void writeData(DataOutputStreamForestry data) throws IOException { + data.writeEnum(state); + data.writeItemStack(tokenStack); + } + + @Override + public void readData(DataInputStreamForestry data) throws IOException { + state = data.readEnum(State.class); + tokenStack = data.readItemStack(); + } +} diff --git a/src/main/java/forestry/core/tiles/EscritoireTextSource.java b/src/main/java/forestry/core/tiles/EscritoireTextSource.java new file mode 100644 index 000000000..a34aede82 --- /dev/null +++ b/src/main/java/forestry/core/tiles/EscritoireTextSource.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2011-2014 SirSengir. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Lesser Public License v3 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/lgpl-3.0.txt + * + * Various Contributors including, but not limited to: + * SirSengir (original work), CovertJaguar, Player, Binnie, MysteriousAges + ******************************************************************************/ +package forestry.core.tiles; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.ListMultimap; + +import java.util.EnumSet; +import java.util.List; +import java.util.Random; + +import forestry.core.utils.StringUtil; + +public class EscritoireTextSource { + + private enum Notes { + level1, level2, level3, level4, success, failure, empty + } + + private static final ListMultimap researchNotes; + private static final Random random = new Random(); + + static { + researchNotes = ArrayListMultimap.create(); + EnumSet multipleTranslationNoteLevels = EnumSet.of(Notes.level1, Notes.level2, Notes.level3, Notes.level4, Notes.success, Notes.failure); + for (Notes notesLevel : multipleTranslationNoteLevels) { + int levelCount = Integer.valueOf(StringUtil.localize("gui.escritoire.notes." + notesLevel + ".count")); + for (int i = 1; i <= levelCount; i++) { + String note = StringUtil.localize("gui.escritoire.notes." + notesLevel + '.' + i); + researchNotes.put(notesLevel, note); + } + } + researchNotes.put(Notes.empty, StringUtil.localize("gui.escritoire.instructions")); + } + + private String researchNote; + private Notes lastNoteLevel; + + public String getText(EscritoireGame escritoireGame) { + Notes noteLevel = getNoteLevel(escritoireGame); + if (lastNoteLevel != noteLevel) { + researchNote = getRandomNote(noteLevel); + lastNoteLevel = noteLevel; + } + + return researchNote; + } + + private static String getRandomNote(Notes level) { + List candidates = researchNotes.get(level); + int index = random.nextInt(candidates.size()); + return candidates.get(index); + } + + public static Notes getNoteLevel(EscritoireGame game) { + EscritoireGame.Status status = game.getStatus(); + switch (status) { + case PLAYING: { + int bounty = game.getBountyLevel(); + if (bounty >= EscritoireGame.BOUNTY_MAX) { + return Notes.level1; + } else if (bounty > EscritoireGame.BOUNTY_MAX / 2) { + return Notes.level2; + } else if (bounty > EscritoireGame.BOUNTY_MAX / 4) { + return Notes.level3; + } else { + return Notes.level4; + } + } + case FAILURE: + return Notes.failure; + case SUCCESS: + return Notes.success; + case EMPTY: + return Notes.empty; + } + return null; + } +} diff --git a/src/main/java/forestry/core/tiles/TileEscritoire.java b/src/main/java/forestry/core/tiles/TileEscritoire.java index 4d6b5ad74..8d2314f22 100644 --- a/src/main/java/forestry/core/tiles/TileEscritoire.java +++ b/src/main/java/forestry/core/tiles/TileEscritoire.java @@ -21,19 +21,19 @@ import com.mojang.authlib.GameProfile; import forestry.api.core.ForestryAPI; import forestry.api.genetics.AlleleManager; +import forestry.api.genetics.IAlleleSpecies; import forestry.api.genetics.IIndividual; import forestry.core.inventory.TileInventoryAdapter; import forestry.core.network.DataInputStreamForestry; import forestry.core.network.DataOutputStreamForestry; import forestry.core.network.GuiId; -import forestry.core.network.PacketTileStream; -import forestry.core.proxy.Proxies; +import forestry.core.network.IStreamableGui; import forestry.core.render.TankRenderInfo; import forestry.core.utils.GeneticsUtil; import forestry.core.utils.InventoryUtil; import forestry.core.utils.SlotUtil; -public class TileEscritoire extends TileBase implements ISidedInventory, IRenderableTile, ICrafter { +public class TileEscritoire extends TileBase implements ISidedInventory, IRenderableTile, ICrafter, IStreamableGui { public static final short SLOT_ANALYZE = 0; public static final short SLOT_RESULTS_1 = 1; @@ -74,26 +74,32 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender return game; } - public void processTurnResult(GameProfile gameProfile) { - if (!game.isWon()) { + public void choose(GameProfile gameProfile, int index) { + game.choose(index); + processTurnResult(gameProfile); + } + + private void processTurnResult(GameProfile gameProfile) { + if (getGame().getStatus() != EscritoireGame.Status.SUCCESS) { return; } - IIndividual individual = AlleleManager.alleleRegistry.getIndividual(getInternalInventory().getStackInSlot(SLOT_ANALYZE)); + IIndividual individual = AlleleManager.alleleRegistry.getIndividual(getStackInSlot(SLOT_ANALYZE)); if (individual == null) { return; } - for (ItemStack itemstack : individual.getGenome().getPrimary().getResearchBounty(worldObj, gameProfile, individual, game.getBountyLevel())) { + IAlleleSpecies species = individual.getGenome().getPrimary(); + for (ItemStack itemstack : species.getResearchBounty(worldObj, gameProfile, individual, game.getBountyLevel())) { InventoryUtil.addStack(getInternalInventory(), itemstack, SLOT_RESULTS_1, SLOTS_RESULTS_COUNT, true); } } private boolean areProbeSlotsFilled() { int filledSlots = 0; - int required = game.getSampleSize() < SLOTS_INPUT_COUNT ? game.getSampleSize() : SLOTS_INPUT_COUNT; + int required = game.getSampleSize(SLOTS_INPUT_COUNT); for (int i = SLOT_INPUT_1; i < SLOT_INPUT_1 + required; i++) { - if (getInternalInventory().getStackInSlot(i) != null) { + if (getStackInSlot(i) != null) { filledSlots++; } } @@ -102,29 +108,28 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender } public void probe() { - if (!worldObj.isRemote && getInternalInventory().getStackInSlot(SLOT_ANALYZE) != null && areProbeSlotsFilled()) { - game.probe(getInternalInventory().getStackInSlot(SLOT_ANALYZE), this, SLOT_INPUT_1, SLOTS_INPUT_COUNT); + if (worldObj.isRemote) { + return; + } + + ItemStack analyze = getStackInSlot(SLOT_ANALYZE); + + if (analyze != null && areProbeSlotsFilled()) { + game.probe(analyze, this, SLOT_INPUT_1, SLOTS_INPUT_COUNT); } } /* NETWORK */ - @Override - public void writeData(DataOutputStreamForestry data) throws IOException { - super.writeData(data); + public void writeGuiData(DataOutputStreamForestry data) throws IOException { game.writeData(data); } @Override - public void readData(DataInputStreamForestry data) throws IOException { - super.readData(data); + public void readGuiData(DataInputStreamForestry data) throws IOException { game.readData(data); } - public void sendBoard(EntityPlayer player) { - Proxies.net.sendToPlayer(new PacketTileStream(this), player); - } - @Override public TankRenderInfo getResourceTankInfo() { return TankRenderInfo.EMPTY; @@ -160,7 +165,7 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender @Override public boolean canSlotAccept(int slotIndex, ItemStack itemStack) { - if (slotIndex >= SLOT_INPUT_1 && slotIndex < SLOT_INPUT_1 + Math.min(tile.game.getSampleSize(), SLOTS_INPUT_COUNT)) { + if (slotIndex >= SLOT_INPUT_1 && slotIndex < SLOT_INPUT_1 + tile.game.getSampleSize(SLOTS_INPUT_COUNT)) { ItemStack specimen = getStackInSlot(SLOT_ANALYZE); if (specimen == null) { return false; @@ -187,7 +192,7 @@ public class TileEscritoire extends TileBase implements ISidedInventory, IRender } if (SlotUtil.isSlotInRange(slotIndex, SLOT_INPUT_1, SLOTS_INPUT_COUNT)) { - if (slotIndex >= SLOT_INPUT_1 + tile.game.getSampleSize()) { + if (slotIndex >= SLOT_INPUT_1 + tile.game.getSampleSize(SLOTS_INPUT_COUNT)) { return true; } } diff --git a/src/main/java/forestry/factory/recipes/RecipeMemory.java b/src/main/java/forestry/factory/recipes/RecipeMemory.java index 0bc9cf117..a22aa5ac9 100644 --- a/src/main/java/forestry/factory/recipes/RecipeMemory.java +++ b/src/main/java/forestry/factory/recipes/RecipeMemory.java @@ -197,9 +197,7 @@ public class RecipeMemory implements INBTTagable, IStreamable { @Override public void readData(DataInputStreamForestry data) throws IOException { - recipes.clear(); - List newRecipes = data.readStreamables(Recipe.class); - recipes.addAll(newRecipes); + data.readStreamables(recipes, Recipe.class); } public static final class Recipe implements INBTTagable, IStreamable { @@ -209,8 +207,9 @@ public class RecipeMemory implements INBTTagable, IStreamable { private boolean locked; private ItemStack cachedRecipeOutput; + @SuppressWarnings("unused") public Recipe() { - // required for serialization + // required for IStreamable serialization } public Recipe(InventoryCrafting crafting) {