]> git.seodisparate.com - TurnBasedMinecraftMod/commitdiff
Minor improvements
authorStephen Seo <seo.disparate@gmail.com>
Mon, 29 Oct 2018 05:46:45 +0000 (14:46 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 29 Oct 2018 05:46:45 +0000 (14:46 +0900)
src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java
src/main/java/com/seodisparate/TurnBasedMinecraft/common/BattleUpdater.java
src/main/java/com/seodisparate/TurnBasedMinecraft/common/networking/PacketBattleMessage.java

index e60c591709d9cecf6f9417e4dabd90e42be00da6..4165c96a53c735c836d0cc12df5d85685c86a13e 100644 (file)
@@ -702,7 +702,21 @@ public class Battle
         }
         battleManager.addRecentlyLeftBattle(c);
     }
-    
+
+    private void setDecisionState()
+    {
+        for(Combatant c : sideA.values())
+        {
+            c.decision = Decision.UNDECIDED;
+        }
+        for(Combatant c : sideB.values())
+        {
+            c.decision = Decision.UNDECIDED;
+        }
+        state = State.DECISION;
+        undecidedCount.set(players.size());
+    }
+
     /**
      * @return True if battle has ended
      */
@@ -726,7 +740,29 @@ public class Battle
         long nextInstant = System.nanoTime();
         long dt = nextInstant - lastInstant;
         lastInstant = nextInstant;
-        return update(dt);
+        try
+        {
+            return update(dt);
+        } catch (Throwable t)
+        {
+            TurnBasedMinecraftMod.logger.error("Update: ", t);
+            setDecisionState();
+            boolean changed = false;
+            if(healthCheck())
+            {
+                changed = true;
+            }
+            if(isCreativeCheck())
+            {
+                changed = true;
+            }
+            sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 1);
+            if(changed)
+            {
+                notifyPlayersBattleInfo();
+            }
+            return battleEnded;
+        }
     }
     
     private boolean update(final long dt)
@@ -846,8 +882,11 @@ public class Battle
                     debugLog = next.entity.getName();
                     
                     next.remainingDefenses = 0;
+
+                    Decision decision = next.decision;
+                    next.decision = Decision.UNDECIDED;
                     
-                    switch(next.decision)
+                    switch(decision)
                     {
                     case UNDECIDED:
                         debugLog += " undecided";
@@ -998,25 +1037,31 @@ public class Battle
                                 debugLog += " to random other side";
                                 if(next.isSideA)
                                 {
-                                    int randomTargetIndex = random.nextInt(sideB.size());
-                                    for(Combatant c : sideB.values())
+                                    if(sideB.size() > 0)
                                     {
-                                        if(randomTargetIndex-- == 0)
+                                        int randomTargetIndex = random.nextInt(sideB.size());
+                                        for(Combatant c : sideB.values())
                                         {
-                                            target = c;
-                                            break;
+                                            if(randomTargetIndex-- == 0)
+                                            {
+                                                target = c;
+                                                break;
+                                            }
                                         }
                                     }
                                 }
                                 else
                                 {
-                                    int randomTargetIndex = random.nextInt(sideA.size());
-                                    for(Combatant c : sideA.values())
+                                    if(sideA.size() > 0)
                                     {
-                                        if(randomTargetIndex-- == 0)
+                                        int randomTargetIndex = random.nextInt(sideA.size());
+                                        for(Combatant c : sideA.values())
                                         {
-                                            target = c;
-                                            break;
+                                            if(randomTargetIndex-- == 0)
+                                            {
+                                                target = c;
+                                                break;
+                                            }
                                         }
                                     }
                                 }
@@ -1288,16 +1333,7 @@ public class Battle
                     }
                 }
                 debugLog = "Actions almost end";
-                for(Combatant c : sideA.values())
-                {
-                    c.decision = Decision.UNDECIDED;
-                }
-                for(Combatant c : sideB.values())
-                {
-                    c.decision = Decision.UNDECIDED;
-                }
-                state = State.DECISION;
-                undecidedCount.set(players.size());
+                setDecisionState();
                 if(healthCheck())
                 {
                     combatantsChanged = true;
index 32eda1af85916e279e65190328d21272f12f7410..6b3bcdd424f52a26e994ee05b0a9ef37845b23a0 100644 (file)
@@ -65,12 +65,18 @@ public class BattleUpdater implements Runnable
                         if(!updateRunnable.isFinished())
                         {
                             TurnBasedMinecraftMod.logger.warn("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update hanged for 4 seconds!");
-                            try { updateThread.join(4000); } catch(InterruptedException e){ /* exception ignored */ }
+                            try { updateThread.join(2000); } catch(InterruptedException e){ /* exception ignored */ }
                             if(!updateRunnable.isFinished())
                             {
-                                // TODO this is an ugly fix to a still-not-found freeze bug in Battle.update()
-                                TurnBasedMinecraftMod.logger.error("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update timed out!");
-                                updateThread.stop();
+                                TurnBasedMinecraftMod.logger.error("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update timed out (6 seconds)!");
+                                updateThread.interrupt();
+                                try { updateThread.join(2000); } catch(InterruptedException e){ /* exception ignored */ }
+                                if(!updateRunnable.isFinished())
+                                {
+                                    // TODO this is an ugly fix to a still-not-found freeze bug in Battle.update()
+                                    TurnBasedMinecraftMod.logger.error("Battle update will not stop, forcing it to stop (8 seconds)!");
+                                    updateThread.stop();
+                                }
                             }
                         }
                     }
index f34638083d13c4b5c6be0db243ea972f4c7f5686..6ee54004b6f05e269ca42bc850c305ec646700b4 100644 (file)
@@ -302,7 +302,14 @@ public class PacketBattleMessage implements IMessage
             case TURN_END:
                 if(TurnBasedMinecraftMod.proxy.getLocalBattle() != null)
                 {
-                    TurnBasedMinecraftMod.proxy.displayString("The turn ended!");
+                    if(message.amount == 0)
+                    {
+                        TurnBasedMinecraftMod.proxy.displayString("The turn ended!");
+                    }
+                    else
+                    {
+                        TurnBasedMinecraftMod.proxy.displayString("The turn ended (abnormally due to internal error)!");
+                    }
                 }
                 TurnBasedMinecraftMod.proxy.battleGuiTurnEnd();
                 break;