diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/TurnBasedMinecraftMod.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/TurnBasedMinecraftMod.java index 7089755..73e17bc 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/TurnBasedMinecraftMod.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/TurnBasedMinecraftMod.java @@ -8,9 +8,8 @@ import com.seodisparate.TurnBasedMinecraft.common.Battle; import com.seodisparate.TurnBasedMinecraft.common.BattleManager; import com.seodisparate.TurnBasedMinecraft.common.Config; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleDecision; -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleEntered; -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleExited; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo; +import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleRequestInfo; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler; @@ -39,6 +38,7 @@ public class TurnBasedMinecraftMod private static BattleManager battleManager; private static int packetHandlerID = 0; public static Entity attackingEntity; + public static int attackingDamage = 0; public static Config config; public static Battle currentBattle; @@ -63,16 +63,6 @@ public class TurnBasedMinecraftMod } // register packets - PacketHandler.INSTANCE.registerMessage( - PacketBattleEntered.HandlerBattleEntered.class, - PacketBattleEntered.class, - packetHandlerID++, - Side.CLIENT); - PacketHandler.INSTANCE.registerMessage( - PacketBattleExited.HandlerBattleExited.class, - PacketBattleExited.class, - packetHandlerID++, - Side.CLIENT); PacketHandler.INSTANCE.registerMessage( PacketBattleInfo.HandlerBattleInfo.class, PacketBattleInfo.class, @@ -88,6 +78,11 @@ public class TurnBasedMinecraftMod PacketBattleDecision.class, packetHandlerID++, Side.SERVER); + PacketHandler.INSTANCE.registerMessage( + PacketBattleMessage.HandlerBattleMessage.class, + PacketBattleMessage.class, + packetHandlerID++, + Side.CLIENT); } @EventHandler @@ -111,6 +106,7 @@ public class TurnBasedMinecraftMod logger.debug("Canceled LivingAttackEvent between " + attackingEntity + " and " + event.getEntity()); event.setCanceled(true); } + attackingDamage = (int) event.getAmount(); } public static BattleManager getBattleManager() diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java index 3c2d068..3dc054a 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java @@ -2,19 +2,19 @@ package com.seodisparate.TurnBasedMinecraft.common; import java.time.Duration; import java.time.Instant; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.PriorityQueue; -import java.util.SortedSet; -import java.util.TreeSet; -import java.util.concurrent.PriorityBlockingQueue; +import java.util.Queue; import java.util.concurrent.atomic.AtomicInteger; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo; +import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler; import net.minecraft.entity.Entity; @@ -37,6 +37,9 @@ public class Battle private AtomicInteger undecidedCount; private Duration timer; + private boolean isServer; + private boolean battleEnded; + public enum State { DECISION, @@ -79,8 +82,9 @@ public class Battle } } - public Battle(int id, Collection sideA, Collection sideB) + public Battle(int id, Collection sideA, Collection sideB, boolean isServer) { + this.isServer = isServer; this.id = id; this.sideA = new Hashtable(); this.sideB = new Hashtable(); @@ -129,10 +133,22 @@ public class Battle } } + for(Combatant c : this.sideA.values()) + { + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id); + } + for(Combatant c : this.sideB.values()) + { + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id); + } + lastUpdated = null; state = State.DECISION; undecidedCount.set(playerCount.get()); timer = TurnBasedMinecraftMod.BattleDecisionTime; + battleEnded = false; + + notifyPlayersBattleInfo(); } public int getId() @@ -140,6 +156,21 @@ public class Battle return id; } + public Entity getCombatantEntity(int entityID) + { + Combatant c = sideA.get(entityID); + if(c != null) + { + return c.entity; + } + c = sideB.get(entityID); + if(c != null) + { + return c.entity; + } + return null; + } + public boolean hasCombatant(int entityID) { return sideA.containsKey(entityID) || sideB.containsKey(entityID); @@ -170,6 +201,8 @@ public class Battle undecidedCount.incrementAndGet(); } } + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id); + notifyPlayersBattleInfo(); } public void addCombatantToSideB(Entity e) @@ -192,6 +225,8 @@ public class Battle undecidedCount.incrementAndGet(); } } + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id); + notifyPlayersBattleInfo(); } public void clearCombatants() @@ -271,9 +306,9 @@ public class Battle return state; } - public void notifyPlayersBattleInfo() + protected void notifyPlayersBattleInfo() { - if(TurnBasedMinecraftMod.getBattleManager() == null) + if(!isServer) { return; } @@ -284,29 +319,95 @@ public class Battle } } - public void update() + /** + * @return True if battle has ended + */ + public boolean update() { + if(battleEnded) + { + return true; + } if(lastUpdated == null) { lastUpdated = Instant.now(); - update(Duration.ZERO); + return update(Duration.ZERO); } else { Instant now = Instant.now(); - update(Duration.between(lastUpdated, now)); + Duration dt = Duration.between(lastUpdated, now); lastUpdated = now; + return update(dt); } } - private void update(final Duration dt) + private void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount) { + if(!isServer) + { + return; + } + for(Combatant p : players.values()) + { + if(p.entity.isEntityAlive()) + { + PacketHandler.INSTANCE.sendTo(new PacketBattleMessage(type, from, to, amount), (EntityPlayerMP)p.entity); + } + } + } + + private boolean update(final Duration dt) + { + if(battleEnded) + { + return true; + } switch(state) { case DECISION: timer = timer.minus(dt); if(timer.isNegative() || timer.isZero() || undecidedCount.get() <= 0) { + for(Combatant c : sideA.values()) + { + // picking decision for sideA non-players + if(!(c.entity instanceof EntityPlayer) && c.decision == Decision.UNDECIDED && c.entityInfo != null) + { + int percentage = (int)(Math.random() * 100); + if(percentage < c.entityInfo.decisionAttack) + { + c.decision = Decision.ATTACK; + } + else if(percentage - c.entityInfo.decisionAttack < c.entityInfo.decisionDefend) + { + c.decision = Decision.DEFEND; + } + else if(percentage - c.entityInfo.decisionAttack - c.entityInfo.decisionDefend < c.entityInfo.decisionFlee) + { + c.decision = Decision.FLEE; + } + } + } + for(Combatant c : sideB.values()) + { + if(!(c.entity instanceof EntityPlayer) && c.decision == Decision.UNDECIDED && c.entityInfo != null) + { + int percentage = (int)(Math.random() * 100); + if(percentage < c.entityInfo.decisionAttack) + { + c.decision = Decision.ATTACK; + } + else if(percentage - c.entityInfo.decisionAttack < c.entityInfo.decisionDefend) + { + c.decision = Decision.DEFEND; + } + else if(percentage - c.entityInfo.decisionAttack - c.entityInfo.decisionDefend < c.entityInfo.decisionFlee) + { + c.decision = Decision.FLEE; + } + } + } state = State.ACTION; timer = TurnBasedMinecraftMod.BattleDecisionTime; turnOrderQueue.clear(); @@ -319,7 +420,6 @@ public class Battle turnOrderQueue.add(c); } update(Duration.ZERO); - // TODO assign decisions to non-players } break; case ACTION: @@ -335,6 +435,7 @@ public class Battle switch(next.decision) { case UNDECIDED: + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DID_NOTHING, next.entity.getEntityId(), 0, 0); next = turnOrderQueue.poll(); continue; case ATTACK: @@ -372,6 +473,7 @@ public class Battle TurnBasedMinecraftMod.attackingEntity = next.entity; ((EntityPlayer)next.entity).attackTargetEntityWithCurrentItem(target.entity); TurnBasedMinecraftMod.attackingEntity = null; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), TurnBasedMinecraftMod.attackingDamage); if(!(target.entity instanceof EntityPlayer) && target.entityInfo.defenseDamage > 0) { if((int)(Math.random() * 100) < target.entityInfo.defenseDamageProbability) @@ -381,6 +483,7 @@ public class Battle TurnBasedMinecraftMod.attackingEntity = target.entity; next.entity.attackEntityFrom(defenseDamageSource, target.entityInfo.defenseDamage); TurnBasedMinecraftMod.attackingEntity = null; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, target.entity.getEntityId(), next.entity.getEntityId(), target.entityInfo.defenseDamage); } } } @@ -388,11 +491,13 @@ public class Battle { // blocked --target.remainingDefenses; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFEND, target.entity.getEntityId(), next.entity.getEntityId(), 0); } } else { // miss + sendMessageToAllPlayers(PacketBattleMessage.MessageType.MISS, next.entity.getEntityId(), target.entity.getEntityId(), 0); } } else @@ -447,8 +552,9 @@ public class Battle } // attack TurnBasedMinecraftMod.attackingEntity = next.entity; - target.entity.attackEntityFrom(damageSource, next.entityInfo.attackPower); + target.entity.attackEntityFrom(damageSource, damageAmount); TurnBasedMinecraftMod.attackingEntity = null; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), damageAmount); if(!(target.entity instanceof EntityPlayer) && target.entityInfo.defenseDamage > 0) { if((int)(Math.random() * 100) < target.entityInfo.defenseDamageProbability) @@ -458,6 +564,7 @@ public class Battle TurnBasedMinecraftMod.attackingEntity = target.entity; next.entity.attackEntityFrom(defenseDamageSource, target.entityInfo.defenseDamage); TurnBasedMinecraftMod.attackingEntity = null; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, target.entity.getEntityId(), next.entity.getEntityId(), target.entityInfo.defenseDamage); } } } @@ -465,16 +572,19 @@ public class Battle { // blocked --target.remainingDefenses; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFEND, target.entity.getEntityId(), next.entity.getEntityId(), 0); } } else { // miss + sendMessageToAllPlayers(PacketBattleMessage.MessageType.MISS, next.entity.getEntityId(), target.entity.getEntityId(), 0); } } break; case DEFEND: next.remainingDefenses = TurnBasedMinecraftMod.config.getDefenseDuration(); + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENDING, next.entity.getEntityId(), 0, 0); break; case FLEE: int fastestEnemySpeed = 0; @@ -552,13 +662,19 @@ public class Battle { sideB.remove(next.entity.getEntityId()); } + sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 1); if(next.entity instanceof EntityPlayer) { players.remove(next.entity.getEntityId()); playerCount.decrementAndGet(); - // TODO notify player exited battle + PacketHandler.INSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)next.entity); } } + else + { + // flee fail + sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 0); + } break; case USE_ITEM: break; @@ -578,8 +694,57 @@ public class Battle break; } case HEALTH_CHECK: - // TODO + Queue removeQueue = new ArrayDeque(); + for(Combatant c : sideA.values()) + { + if(!c.entity.isEntityAlive()) + { + removeQueue.add(c.entity.getEntityId()); + if(c.entity instanceof EntityPlayer) + { + PacketHandler.INSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity); + } + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0); + } + } + for(Combatant c : sideB.values()) + { + if(!c.entity.isEntityAlive()) + { + removeQueue.add(c.entity.getEntityId()); + if(c.entity instanceof EntityPlayer) + { + PacketHandler.INSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity); + } + sendMessageToAllPlayers(PacketBattleMessage.MessageType.DIED, c.entity.getEntityId(), 0, 0); + } + } + boolean didRemove = !removeQueue.isEmpty(); + Integer toRemove = removeQueue.poll(); + while(toRemove != null) + { + sideA.remove(toRemove); + sideB.remove(toRemove); + if(players.remove(toRemove) != null) + { + playerCount.decrementAndGet(); + } + toRemove = removeQueue.poll(); + } + if(players.isEmpty() || sideA.isEmpty() || sideB.isEmpty()) + { + battleEnded = true; + sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENDED, 0, 0, 0); + } + else if(didRemove) + { + notifyPlayersBattleInfo(); + } + state = State.DECISION; + undecidedCount.set(playerCount.get()); + timer = TurnBasedMinecraftMod.BattleDecisionTime; break; } + return battleEnded; } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleManager.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleManager.java index 18ddcbd..7566140 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleManager.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleManager.java @@ -6,12 +6,9 @@ import java.util.Hashtable; import java.util.Map; import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleEntered; -import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; import net.minecraftforge.event.entity.living.LivingAttackEvent; public class BattleManager @@ -104,13 +101,7 @@ public class BattleManager { battle.addCombatantToSideA(notInBattle); } - - if(notInBattle instanceof EntityPlayerMP) - { - PacketHandler.INSTANCE.sendTo(new PacketBattleEntered(IDCounter), (EntityPlayerMP)notInBattle); - } - - battle.notifyPlayersBattleInfo(); + return true; } @@ -120,22 +111,8 @@ public class BattleManager { ++IDCounter; } - Battle newBattle = new Battle(IDCounter, sideA, sideB); + Battle newBattle = new Battle(IDCounter, sideA, sideB, true); battleMap.put(IDCounter, newBattle); - for(Entity e : sideA) - { - if(e instanceof EntityPlayerMP) - { - PacketHandler.INSTANCE.sendTo(new PacketBattleEntered(IDCounter), (EntityPlayerMP)e); - } - } - for(Entity e : sideB) - { - if(e instanceof EntityPlayerMP) - { - PacketHandler.INSTANCE.sendTo(new PacketBattleEntered(IDCounter), (EntityPlayerMP)e); - } - } newBattle.notifyPlayersBattleInfo(); return newBattle; } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java index aa8732a..eb0931c 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java @@ -1,5 +1,8 @@ package com.seodisparate.TurnBasedMinecraft.common; +import java.util.ArrayDeque; +import java.util.Queue; + public class BattleUpdater implements Runnable { private BattleManager manager; @@ -14,11 +17,22 @@ public class BattleUpdater implements Runnable @Override public void run() { + Queue endedQueue = new ArrayDeque(); + Integer ended; while(isRunning) { for(Battle e : manager.battleMap.values()) { - e.update(); + if(e.update()) + { + endedQueue.add(e.getId()); + } + } + ended = endedQueue.poll(); + while(ended != null) + { + manager.battleMap.remove(ended); + ended = endedQueue.poll(); } try { Thread.sleep(250); } catch (Exception e) { /* ignored */ } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java index 1eb2325..4164d24 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java @@ -27,9 +27,9 @@ public class Config private Map entityInfoMap; private Set ignoreBattleTypes; private Logger logger; - private int playerSpeed; - private int playerHasteSpeed; - private int playerSlowSpeed; + private int playerSpeed = 50; + private int playerHasteSpeed = 80; + private int playerSlowSpeed = 20; private int playerAttackProbability = 100; private int playerEvasion = 10; private int defenseDuration = 1; @@ -298,6 +298,28 @@ public class Config { eInfo.speed = Integer.parseInt(xmlReader.getElementText()); } + else if(xmlReader.getLocalName().equals("Decision")) + { + do + { + xmlReader.next(); + if(xmlReader.isStartElement()) + { + if(xmlReader.getLocalName().equals("Attack")) + { + eInfo.decisionAttack = Integer.parseInt(xmlReader.getElementText()); + } + else if(xmlReader.getLocalName().equals("Defend")) + { + eInfo.decisionDefend = Integer.parseInt(xmlReader.getElementText()); + } + else if(xmlReader.getLocalName().equals("Flee")) + { + eInfo.decisionFlee = Integer.parseInt(xmlReader.getElementText()); + } + } + } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals("Decision"))); + } } } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals(classType))); entityInfoMap.put(eInfo.classType.getName(), eInfo); @@ -368,6 +390,10 @@ public class Config protected EntityInfo getMatchingEntityInfo(Object entity) { + if(entity == null) + { + return null; + } EntityInfo matching = entityInfoMap.get(entity.getClass().getName()); if(matching.classType.isInstance(entity)) { diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/EntityInfo.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/EntityInfo.java index e4a6ae2..1f7aebe 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/EntityInfo.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/EntityInfo.java @@ -19,6 +19,9 @@ public class EntityInfo public int evasion; public int speed; public Category category; + public int decisionAttack; + public int decisionDefend; + public int decisionFlee; public enum Category { @@ -208,6 +211,9 @@ public class EntityInfo evasion = 15; speed = 50; category = Category.UNKNOWN; + decisionAttack = 70; + decisionDefend = 20; + decisionFlee = 10; } public EntityInfo clone() @@ -230,6 +236,9 @@ public class EntityInfo newEntityInfo.evasion = evasion; newEntityInfo.speed = speed; newEntityInfo.category = category; + newEntityInfo.decisionAttack = decisionAttack; + newEntityInfo.decisionDefend = decisionDefend; + newEntityInfo.decisionFlee = decisionFlee; return newEntityInfo; } } diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleEntered.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleEntered.java deleted file mode 100644 index 35ad906..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleEntered.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common.networking; - -import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; -import com.seodisparate.TurnBasedMinecraft.common.Battle; - -import io.netty.buffer.ByteBuf; -import net.minecraftforge.fml.common.network.simpleimpl.IMessage; -import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; -import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; - -public class PacketBattleEntered implements IMessage -{ - private int battleID; - - public PacketBattleEntered() {} - - public PacketBattleEntered(int battleID) - { - this.battleID = battleID; - } - - @Override - public void fromBytes(ByteBuf buf) - { - battleID = buf.readInt(); - } - - @Override - public void toBytes(ByteBuf buf) - { - buf.writeInt(battleID); - } - - public static class HandlerBattleEntered implements IMessageHandler - { - @Override - public IMessage onMessage(PacketBattleEntered message, MessageContext ctx) - { - TurnBasedMinecraftMod.currentBattle = new Battle(message.battleID, null, null); - return null; - } - } -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleExited.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleExited.java deleted file mode 100644 index e89e061..0000000 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleExited.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.seodisparate.TurnBasedMinecraft.common.networking; - -import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; - -import io.netty.buffer.ByteBuf; -import net.minecraftforge.fml.common.network.simpleimpl.IMessage; -import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; -import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; - -public class PacketBattleExited implements IMessage -{ - @Override - public void fromBytes(ByteBuf buf) - { - } - - @Override - public void toBytes(ByteBuf buf) - { - } - - public static class HandlerBattleExited implements IMessageHandler - { - @Override - public IMessage onMessage(PacketBattleExited message, MessageContext ctx) - { - TurnBasedMinecraftMod.currentBattle = null; - return null; - } - } -} diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java new file mode 100644 index 0000000..a34b796 --- /dev/null +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java @@ -0,0 +1,233 @@ +package com.seodisparate.TurnBasedMinecraft.common.networking; + +import java.util.HashMap; +import java.util.Map; + +import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod; +import com.seodisparate.TurnBasedMinecraft.common.Battle; + +import io.netty.buffer.ByteBuf; +import net.minecraft.client.Minecraft; +import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityLiving; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.scoreboard.ScorePlayerTeam; +import net.minecraft.util.text.TextComponentString; +import net.minecraftforge.fml.common.network.simpleimpl.IMessage; +import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; +import net.minecraftforge.fml.common.network.simpleimpl.MessageContext; + +public class PacketBattleMessage implements IMessage +{ + public enum MessageType + { + ENTERED(0), + FLEE(1), + DIED(2), + ENDED(3), + ATTACK(4), + DEFEND(5), + DEFENSE_DAMAGE(6), + MISS(7), + DEFENDING(8), + DID_NOTHING(9); + + private int value; + private static Map map = new HashMap(); + + private MessageType(int value) + { + this.value = value; + } + + public int getValue() + { + return value; + } + + static + { + for(MessageType type : MessageType.values()) + { + map.put(type.getValue(), type); + } + } + + public static MessageType valueOf(int value) + { + return map.get(value); + } + } + + MessageType messageType; + int entityIDFrom; + int entityIDTo; + int amount; + + public PacketBattleMessage() {} + + public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, int amount) + { + this.messageType = messageType; + this.entityIDFrom = entityIDFrom; + this.entityIDTo = entityIDTo; + this.amount = amount; + } + + @Override + public void fromBytes(ByteBuf buf) + { + messageType = MessageType.valueOf(buf.readInt()); + entityIDFrom = buf.readInt(); + entityIDTo = buf.readInt(); + amount = buf.readInt(); + } + + @Override + public void toBytes(ByteBuf buf) + { + buf.writeInt(messageType.getValue()); + buf.writeInt(entityIDFrom); + buf.writeInt(entityIDTo); + buf.writeInt(amount); + } + + public static class HandlerBattleMessage implements IMessageHandler + { + @Override + public IMessage onMessage(PacketBattleMessage message, MessageContext ctx) + { + Entity fromEntity = Minecraft.getMinecraft().world.getEntityByID(message.entityIDFrom); + String from = "Unknown"; + if(fromEntity != null) + { + if(fromEntity.hasCustomName()) + { + from = fromEntity.getCustomNameTag(); + } + else if(fromEntity instanceof EntityPlayer) + { + from = ScorePlayerTeam.formatPlayerName(fromEntity.getTeam(), fromEntity.getName()); + } + else + { + from = fromEntity.getName(); + } + } + else if(TurnBasedMinecraftMod.currentBattle != null) + { + fromEntity = TurnBasedMinecraftMod.currentBattle.getCombatantEntity(message.entityIDFrom); + if(fromEntity != null) + { + if(fromEntity.hasCustomName()) + { + from = fromEntity.getCustomNameTag(); + } + else if(fromEntity instanceof EntityPlayer) + { + from = ScorePlayerTeam.formatPlayerName(fromEntity.getTeam(), fromEntity.getName()); + } + else + { + from = fromEntity.getName(); + } + } + } + Entity toEntity = Minecraft.getMinecraft().world.getEntityByID(message.entityIDTo); + String to = "Unknown"; + if(toEntity != null) + { + if(toEntity.hasCustomName()) + { + to = toEntity.getCustomNameTag(); + } + else if(toEntity instanceof EntityPlayer) + { + to = ScorePlayerTeam.formatPlayerName(toEntity.getTeam(), toEntity.getName()); + } + else + { + to = toEntity.getName(); + } + } + else if(TurnBasedMinecraftMod.currentBattle != null) + { + toEntity = TurnBasedMinecraftMod.currentBattle.getCombatantEntity(message.entityIDTo); + if(toEntity != null) + { + if(toEntity.hasCustomName()) + { + to = toEntity.getCustomNameTag(); + } + else if(toEntity instanceof EntityPlayer) + { + to = ScorePlayerTeam.formatPlayerName(toEntity.getTeam(), toEntity.getName()); + } + else + { + to = toEntity.getName(); + } + } + } + + switch(message.messageType) + { + case ENTERED: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " entered battle!")); + if(TurnBasedMinecraftMod.currentBattle == null || TurnBasedMinecraftMod.currentBattle.getId() != message.amount) + { + TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false); + } + break; + case FLEE: + if(message.amount != 0) + { + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " fled battle!")); + } + else + { + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " tried to flee battle but failed!")); + } + break; + case DIED: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " died in battle!")); + break; + case ENDED: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + "Battle has ended!")); + TurnBasedMinecraftMod.currentBattle = null; + // TODO kick player out of battle + break; + case ATTACK: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " attacked " + to + " and dealt " + message.amount + " damage!")); + break; + case DEFEND: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " blocked " + to + "'s attack!")); + break; + case DEFENSE_DAMAGE: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " retaliated from " + to + "'s attack and dealt " + message.amount + " damage!")); + break; + case MISS: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " attacked " + to + " but missed!")); + break; + case DEFENDING: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " is defending!")); + break; + case DID_NOTHING: + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( + from + " did nothing!")); + break; + } + return null; + } + } +} diff --git a/src/main/resources/TBM_Config.xml b/src/main/resources/TBM_Config.xml index fedad39..e0828ad 100644 --- a/src/main/resources/TBM_Config.xml +++ b/src/main/resources/TBM_Config.xml @@ -28,12 +28,18 @@ + 5 fire 5 monster 45 + + 100 + 0 + 0 + 2 @@ -41,6 +47,11 @@ 35 monster 75 + + 100 + 0 + 0 + true @@ -48,6 +59,11 @@ 5 monster 25 + + 100 + 0 + 0 + 8 @@ -55,24 +71,44 @@ 25 monster 45 + + 80 + 20 + 0 + 7 40 monster 70 + + 100 + 0 + 0 + 2 40 monster 35 + + 100 + 0 + 0 + 6 35 monster 35 + + 100 + 0 + 0 + true @@ -80,12 +116,22 @@ 35 monster 60 + + 75 + 0 + 25 + 11 2 monster 45 + + 100 + 0 + 0 + 6 @@ -93,6 +139,11 @@ 25 monster 50 + + 80 + 20 + 0 + 3 @@ -100,60 +151,110 @@ 5 monster 25 + + 100 + 0 + 0 + 14 5 monster 45 + + 100 + 0 + 0 + 3 12 monster 35 + + 100 + 0 + 0 + 8 10 monster 50 + + 100 + 0 + 0 + 6 5 animal 35 + + 100 + 0 + 0 + 4 15 monster 10 + + 100 + 0 + 0 + 1 37 monster 35 + + 100 + 0 + 0 + 3 13 monster 30 + + 100 + 0 + 0 + 2 10 monster 30 + + 100 + 0 + 0 + 0 5 passive 60 + + 100 + 0 + 0 + 2 @@ -163,6 +264,11 @@ monster 70 + + 100 + 0 + 0 + 3 @@ -170,24 +276,44 @@ 13 monster 30 + + 100 + 0 + 0 + 9 30 monster 80 + + 100 + 0 + 0 + 13 10 monster 35 + + 100 + 0 + 0 + 5 8 monster 35 + + 100 + 0 + 0 + 8 @@ -195,6 +321,11 @@ 7 monster 65 + + 100 + 0 + 0 + 3 @@ -205,24 +336,44 @@ 5 monster 25 + + 100 + 0 + 0 + 3 5 monster 25 + + 100 + 0 + 0 + 0 35 passive 75 + + 0 + 0 + 90 + 0 10 passive 35 + + 0 + 0 + 90 + 0 @@ -232,102 +383,187 @@ passive 20 + + 0 + 10 + 80 + 0 10 passive 65 + + 0 + 0 + 90 + 0 10 passive 65 + + 0 + 0 + 90 + 1 10 passive 50 + + 65 + 0 + 25 + 0 1 passive 20 + + 0 + 10 + 80 + 0 10 passive 50 + + 0 + 0 + 90 + 1 10 passive 75 + + 0 + 0 + 90 + 0 35 passive 70 + + 0 + 0 + 90 + 0 10 passive 30 + + 0 + 5 + 85 + 0 40 passive 75 + + 0 + 0 + 100 + 0 5 passive 30 + + 0 + 0 + 90 + 0 5 passive 65 + + 0 + 0 + 90 + 0 15 passive 40 + + 0 + 0 + 90 + 0 5 passive 35 + + 0 + 10 + 80 + 4 20 animal 70 + + 80 + 15 + 5 + 0 8 passive 65 + + 0 + 0 + 90 + 10 27 boss 63 + + 100 + 0 + 0 + 8 @@ -335,6 +571,11 @@ wither boss 68 + + 100 + 0 + 0 + \ No newline at end of file