Minor improvements

This commit is contained in:
Stephen Seo 2018-10-29 14:46:45 +09:00
parent 5dd62cc2bf
commit 3c6c22432e
3 changed files with 77 additions and 28 deletions

View file

@ -703,6 +703,20 @@ public class Battle
battleManager.addRecentlyLeftBattle(c); 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 * @return True if battle has ended
*/ */
@ -726,7 +740,29 @@ public class Battle
long nextInstant = System.nanoTime(); long nextInstant = System.nanoTime();
long dt = nextInstant - lastInstant; long dt = nextInstant - lastInstant;
lastInstant = nextInstant; lastInstant = nextInstant;
try
{
return update(dt); 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) private boolean update(final long dt)
@ -847,7 +883,10 @@ public class Battle
next.remainingDefenses = 0; next.remainingDefenses = 0;
switch(next.decision) Decision decision = next.decision;
next.decision = Decision.UNDECIDED;
switch(decision)
{ {
case UNDECIDED: case UNDECIDED:
debugLog += " undecided"; debugLog += " undecided";
@ -997,6 +1036,8 @@ public class Battle
{ {
debugLog += " to random other side"; debugLog += " to random other side";
if(next.isSideA) if(next.isSideA)
{
if(sideB.size() > 0)
{ {
int randomTargetIndex = random.nextInt(sideB.size()); int randomTargetIndex = random.nextInt(sideB.size());
for(Combatant c : sideB.values()) for(Combatant c : sideB.values())
@ -1008,7 +1049,10 @@ public class Battle
} }
} }
} }
}
else else
{
if(sideA.size() > 0)
{ {
int randomTargetIndex = random.nextInt(sideA.size()); int randomTargetIndex = random.nextInt(sideA.size());
for(Combatant c : sideA.values()) for(Combatant c : sideA.values())
@ -1021,6 +1065,7 @@ public class Battle
} }
} }
} }
}
if(target == null || !target.entity.isEntityAlive() || target == next) if(target == null || !target.entity.isEntityAlive() || target == next)
{ {
continue; continue;
@ -1288,16 +1333,7 @@ public class Battle
} }
} }
debugLog = "Actions almost end"; debugLog = "Actions almost end";
for(Combatant c : sideA.values()) setDecisionState();
{
c.decision = Decision.UNDECIDED;
}
for(Combatant c : sideB.values())
{
c.decision = Decision.UNDECIDED;
}
state = State.DECISION;
undecidedCount.set(players.size());
if(healthCheck()) if(healthCheck())
{ {
combatantsChanged = true; combatantsChanged = true;

View file

@ -65,15 +65,21 @@ public class BattleUpdater implements Runnable
if(!updateRunnable.isFinished()) if(!updateRunnable.isFinished())
{ {
TurnBasedMinecraftMod.logger.warn("Battle (" + entry.getValue().getId() + "; " + entry.getValue().debugLog + ") update hanged for 4 seconds!"); 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())
{
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()) if(!updateRunnable.isFinished())
{ {
// TODO this is an ugly fix to a still-not-found freeze bug in Battle.update() // 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!"); TurnBasedMinecraftMod.logger.error("Battle update will not stop, forcing it to stop (8 seconds)!");
updateThread.stop(); updateThread.stop();
} }
} }
} }
}
if(updateRunnable.isFinished() && updateRunnable.isBattleFinished()) if(updateRunnable.isFinished() && updateRunnable.isBattleFinished())
{ {
iter.remove(); iter.remove();

View file

@ -301,9 +301,16 @@ public class PacketBattleMessage implements IMessage
break; break;
case TURN_END: case TURN_END:
if(TurnBasedMinecraftMod.proxy.getLocalBattle() != null) if(TurnBasedMinecraftMod.proxy.getLocalBattle() != null)
{
if(message.amount == 0)
{ {
TurnBasedMinecraftMod.proxy.displayString("The turn ended!"); TurnBasedMinecraftMod.proxy.displayString("The turn ended!");
} }
else
{
TurnBasedMinecraftMod.proxy.displayString("The turn ended (abnormally due to internal error)!");
}
}
TurnBasedMinecraftMod.proxy.battleGuiTurnEnd(); TurnBasedMinecraftMod.proxy.battleGuiTurnEnd();
break; break;
case SWITCHED_ITEM: case SWITCHED_ITEM: