2018-08-28 06:13:14 +00:00
|
|
|
package com.seodisparate.TurnBasedMinecraft.common;
|
|
|
|
|
|
|
|
import java.time.Duration;
|
|
|
|
import java.time.Instant;
|
2018-09-06 08:08:36 +00:00
|
|
|
import java.util.ArrayDeque;
|
2018-08-29 06:09:44 +00:00
|
|
|
import java.util.ArrayList;
|
2018-08-28 06:13:14 +00:00
|
|
|
import java.util.Collection;
|
2018-08-29 06:09:44 +00:00
|
|
|
import java.util.HashMap;
|
2018-08-28 06:13:14 +00:00
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Map;
|
2018-09-04 06:21:49 +00:00
|
|
|
import java.util.PriorityQueue;
|
2018-09-06 08:08:36 +00:00
|
|
|
import java.util.Queue;
|
2018-09-04 06:21:49 +00:00
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2018-08-28 06:13:14 +00:00
|
|
|
|
2018-08-29 06:09:44 +00:00
|
|
|
import com.seodisparate.TurnBasedMinecraft.TurnBasedMinecraftMod;
|
2018-08-30 07:15:20 +00:00
|
|
|
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo;
|
2018-09-06 08:08:36 +00:00
|
|
|
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
|
2018-08-30 07:15:20 +00:00
|
|
|
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketHandler;
|
2018-08-29 06:09:44 +00:00
|
|
|
|
2018-08-28 06:13:14 +00:00
|
|
|
import net.minecraft.entity.Entity;
|
2018-09-05 06:54:06 +00:00
|
|
|
import net.minecraft.entity.EntityLivingBase;
|
2018-08-29 06:09:44 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2018-08-30 07:15:20 +00:00
|
|
|
import net.minecraft.entity.player.EntityPlayerMP;
|
2018-09-05 06:54:06 +00:00
|
|
|
import net.minecraft.util.DamageSource;
|
2018-08-28 06:13:14 +00:00
|
|
|
|
|
|
|
public class Battle
|
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
private final int id;
|
|
|
|
private Map<Integer, Combatant> sideA;
|
|
|
|
private Map<Integer, Combatant> sideB;
|
2018-09-04 06:21:49 +00:00
|
|
|
private Map<Integer, Combatant> players;
|
|
|
|
private PriorityQueue<Combatant> turnOrderQueue;
|
2018-08-28 06:13:14 +00:00
|
|
|
|
|
|
|
private Instant lastUpdated;
|
2018-08-29 06:09:44 +00:00
|
|
|
private State state;
|
2018-09-04 06:21:49 +00:00
|
|
|
private AtomicInteger playerCount;
|
|
|
|
private AtomicInteger undecidedCount;
|
2018-08-29 06:09:44 +00:00
|
|
|
private Duration timer;
|
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
private boolean isServer;
|
|
|
|
private boolean battleEnded;
|
|
|
|
|
2018-08-29 06:09:44 +00:00
|
|
|
public enum State
|
|
|
|
{
|
|
|
|
DECISION,
|
2018-09-05 06:54:06 +00:00
|
|
|
ACTION,
|
2018-08-29 06:09:44 +00:00
|
|
|
HEALTH_CHECK
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Decision
|
|
|
|
{
|
|
|
|
UNDECIDED(0),
|
|
|
|
ATTACK(1),
|
|
|
|
DEFEND(2),
|
|
|
|
FLEE(3),
|
|
|
|
USE_ITEM(4);
|
|
|
|
|
|
|
|
private int value;
|
|
|
|
private static Map<Integer, Decision> map = new HashMap<Integer, Decision>();
|
|
|
|
|
|
|
|
private Decision(int value)
|
|
|
|
{
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getValue()
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
{
|
|
|
|
for(Decision decision : Decision.values())
|
|
|
|
{
|
|
|
|
map.put(decision.value, decision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Decision valueOf(int decisionType)
|
|
|
|
{
|
|
|
|
return map.get(decisionType);
|
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
public Battle(int id, Collection<Entity> sideA, Collection<Entity> sideB, boolean isServer)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-09-06 08:08:36 +00:00
|
|
|
this.isServer = isServer;
|
2018-08-28 06:13:14 +00:00
|
|
|
this.id = id;
|
2018-08-29 06:09:44 +00:00
|
|
|
this.sideA = new Hashtable<Integer, Combatant>();
|
|
|
|
this.sideB = new Hashtable<Integer, Combatant>();
|
2018-09-04 06:21:49 +00:00
|
|
|
players = new Hashtable<Integer, Combatant>();
|
|
|
|
turnOrderQueue = new PriorityQueue<Combatant>(new Combatant.CombatantComparator());
|
|
|
|
playerCount = new AtomicInteger(0);
|
|
|
|
undecidedCount = new AtomicInteger(0);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(sideA != null)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
for(Entity e : sideA)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
EntityInfo entityInfo = TurnBasedMinecraftMod.config.getMatchingEntityInfo(e);
|
|
|
|
if(entityInfo == null && !(e instanceof EntityPlayer))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Combatant newCombatant = new Combatant(e, entityInfo);
|
2018-09-05 06:54:06 +00:00
|
|
|
newCombatant.isSideA = true;
|
2018-09-04 06:21:49 +00:00
|
|
|
this.sideA.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
newCombatant.recalcSpeedOnCompare = true;
|
|
|
|
playerCount.incrementAndGet();
|
|
|
|
players.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
2018-08-29 06:09:44 +00:00
|
|
|
if(sideB != null)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-08-29 06:09:44 +00:00
|
|
|
for(Entity e : sideB)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
EntityInfo entityInfo = TurnBasedMinecraftMod.config.getMatchingEntityInfo(e);
|
|
|
|
if(entityInfo == null && !(e instanceof EntityPlayer))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Combatant newCombatant = new Combatant(e, entityInfo);
|
2018-09-05 06:54:06 +00:00
|
|
|
newCombatant.isSideA = false;
|
2018-09-04 06:21:49 +00:00
|
|
|
this.sideB.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
newCombatant.recalcSpeedOnCompare = true;
|
|
|
|
playerCount.incrementAndGet();
|
|
|
|
players.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-08-28 06:13:14 +00:00
|
|
|
lastUpdated = null;
|
2018-08-29 06:09:44 +00:00
|
|
|
state = State.DECISION;
|
2018-09-04 06:21:49 +00:00
|
|
|
undecidedCount.set(playerCount.get());
|
2018-08-29 06:09:44 +00:00
|
|
|
timer = TurnBasedMinecraftMod.BattleDecisionTime;
|
2018-09-06 08:08:36 +00:00
|
|
|
battleEnded = false;
|
|
|
|
|
|
|
|
notifyPlayersBattleInfo();
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getId()
|
|
|
|
{
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-28 06:13:14 +00:00
|
|
|
public boolean hasCombatant(int entityID)
|
|
|
|
{
|
|
|
|
return sideA.containsKey(entityID) || sideB.containsKey(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasCombatantInSideA(int entityID)
|
|
|
|
{
|
|
|
|
return sideA.containsKey(entityID);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addCombatantToSideA(Entity e)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
EntityInfo entityInfo = TurnBasedMinecraftMod.config.getMatchingEntityInfo(e);
|
|
|
|
if(entityInfo == null && !(e instanceof EntityPlayer))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Combatant newCombatant = new Combatant(e, entityInfo);
|
2018-09-05 06:54:06 +00:00
|
|
|
newCombatant.isSideA = true;
|
2018-09-04 06:21:49 +00:00
|
|
|
sideA.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
newCombatant.recalcSpeedOnCompare = true;
|
|
|
|
playerCount.incrementAndGet();
|
|
|
|
players.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(state == State.DECISION)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
undecidedCount.incrementAndGet();
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id);
|
|
|
|
notifyPlayersBattleInfo();
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addCombatantToSideB(Entity e)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
EntityInfo entityInfo = TurnBasedMinecraftMod.config.getMatchingEntityInfo(e);
|
|
|
|
if(entityInfo == null && !(e instanceof EntityPlayer))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Combatant newCombatant = new Combatant(e, entityInfo);
|
2018-09-05 06:54:06 +00:00
|
|
|
newCombatant.isSideA = false;
|
2018-09-04 06:21:49 +00:00
|
|
|
sideB.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(e instanceof EntityPlayer)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
newCombatant.recalcSpeedOnCompare = true;
|
|
|
|
playerCount.incrementAndGet();
|
|
|
|
players.put(e.getEntityId(), newCombatant);
|
2018-08-29 06:09:44 +00:00
|
|
|
if(state == State.DECISION)
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
undecidedCount.incrementAndGet();
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id);
|
|
|
|
notifyPlayersBattleInfo();
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearCombatants()
|
|
|
|
{
|
|
|
|
sideA.clear();
|
|
|
|
sideB.clear();
|
2018-08-30 07:15:20 +00:00
|
|
|
players.clear();
|
2018-09-04 06:21:49 +00:00
|
|
|
playerCount.set(0);
|
|
|
|
undecidedCount.set(0);
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Combatant> getSideA()
|
|
|
|
{
|
|
|
|
return sideA.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Combatant> getSideB()
|
|
|
|
{
|
|
|
|
return sideB.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Integer> getSideAIDs()
|
|
|
|
{
|
|
|
|
Collection<Integer> sideAIDs = new ArrayList<Integer>(sideA.size());
|
|
|
|
for(Combatant combatant : sideA.values())
|
|
|
|
{
|
|
|
|
sideAIDs.add(combatant.entity.getEntityId());
|
|
|
|
}
|
|
|
|
return sideAIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Collection<Integer> getSideBIDs()
|
|
|
|
{
|
|
|
|
Collection<Integer> sideBIDs = new ArrayList<Integer>(sideB.size());
|
|
|
|
for(Combatant combatant : sideB.values())
|
|
|
|
{
|
|
|
|
sideBIDs.add(combatant.entity.getEntityId());
|
|
|
|
}
|
|
|
|
return sideBIDs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Combatant getCombatantByID(int entityID)
|
|
|
|
{
|
|
|
|
Combatant combatant = sideA.get(entityID);
|
|
|
|
if(combatant == null)
|
|
|
|
{
|
|
|
|
combatant = sideB.get(entityID);
|
|
|
|
}
|
|
|
|
return combatant;
|
|
|
|
}
|
|
|
|
|
2018-09-05 06:54:06 +00:00
|
|
|
public void setDecision(int entityID, Decision decision, int targetIDOrItemID)
|
2018-08-29 06:09:44 +00:00
|
|
|
{
|
|
|
|
if(state != State.DECISION)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2018-09-04 06:21:49 +00:00
|
|
|
Combatant combatant = players.get(entityID);
|
2018-09-05 06:54:06 +00:00
|
|
|
if(combatant == null || combatant.decision != Decision.UNDECIDED)
|
2018-08-29 06:09:44 +00:00
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
return;
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
2018-09-04 06:21:49 +00:00
|
|
|
combatant.decision = decision;
|
2018-09-05 06:54:06 +00:00
|
|
|
if(decision == Decision.ATTACK)
|
|
|
|
{
|
|
|
|
combatant.targetEntityID = targetIDOrItemID;
|
|
|
|
}
|
|
|
|
else if(decision == Decision.USE_ITEM)
|
|
|
|
{
|
|
|
|
combatant.itemToUse = targetIDOrItemID;
|
|
|
|
}
|
2018-09-04 06:21:49 +00:00
|
|
|
undecidedCount.decrementAndGet();
|
2018-08-29 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public State getState()
|
|
|
|
{
|
|
|
|
return state;
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
protected void notifyPlayersBattleInfo()
|
2018-08-30 07:15:20 +00:00
|
|
|
{
|
2018-09-06 08:08:36 +00:00
|
|
|
if(!isServer)
|
2018-08-30 07:15:20 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs());
|
2018-09-04 06:21:49 +00:00
|
|
|
for(Combatant p : players.values())
|
2018-08-30 07:15:20 +00:00
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
PacketHandler.INSTANCE.sendTo(infoPacket, (EntityPlayerMP)p.entity);
|
2018-08-30 07:15:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
/**
|
|
|
|
* @return True if battle has ended
|
|
|
|
*/
|
|
|
|
public boolean update()
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-09-06 08:08:36 +00:00
|
|
|
if(battleEnded)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2018-08-28 06:13:14 +00:00
|
|
|
if(lastUpdated == null)
|
|
|
|
{
|
|
|
|
lastUpdated = Instant.now();
|
2018-09-06 08:08:36 +00:00
|
|
|
return update(Duration.ZERO);
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Instant now = Instant.now();
|
2018-09-06 08:08:36 +00:00
|
|
|
Duration dt = Duration.between(lastUpdated, now);
|
2018-08-28 06:13:14 +00:00
|
|
|
lastUpdated = now;
|
2018-09-06 08:08:36 +00:00
|
|
|
return update(dt);
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-06 08:08:36 +00:00
|
|
|
private void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount)
|
2018-08-28 06:13:14 +00:00
|
|
|
{
|
2018-09-06 08:08:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-08-29 06:09:44 +00:00
|
|
|
switch(state)
|
|
|
|
{
|
|
|
|
case DECISION:
|
|
|
|
timer = timer.minus(dt);
|
2018-09-04 06:21:49 +00:00
|
|
|
if(timer.isNegative() || timer.isZero() || undecidedCount.get() <= 0)
|
2018-08-29 06:09:44 +00:00
|
|
|
{
|
2018-09-06 08:08:36 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 06:54:06 +00:00
|
|
|
state = State.ACTION;
|
2018-08-29 06:09:44 +00:00
|
|
|
timer = TurnBasedMinecraftMod.BattleDecisionTime;
|
2018-09-04 06:21:49 +00:00
|
|
|
turnOrderQueue.clear();
|
|
|
|
for(Combatant c : sideA.values())
|
|
|
|
{
|
|
|
|
turnOrderQueue.add(c);
|
|
|
|
}
|
|
|
|
for(Combatant c : sideB.values())
|
|
|
|
{
|
|
|
|
turnOrderQueue.add(c);
|
|
|
|
}
|
2018-08-29 06:09:44 +00:00
|
|
|
update(Duration.ZERO);
|
|
|
|
}
|
|
|
|
break;
|
2018-09-05 06:54:06 +00:00
|
|
|
case ACTION:
|
|
|
|
{
|
2018-09-04 06:21:49 +00:00
|
|
|
Combatant next = turnOrderQueue.poll();
|
|
|
|
while(next != null)
|
|
|
|
{
|
2018-09-05 06:54:06 +00:00
|
|
|
if(!next.entity.isEntityAlive())
|
|
|
|
{
|
|
|
|
next = turnOrderQueue.poll();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch(next.decision)
|
|
|
|
{
|
|
|
|
case UNDECIDED:
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DID_NOTHING, next.entity.getEntityId(), 0, 0);
|
2018-09-05 06:54:06 +00:00
|
|
|
next = turnOrderQueue.poll();
|
|
|
|
continue;
|
|
|
|
case ATTACK:
|
|
|
|
Combatant target = null;
|
|
|
|
if(next.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
if(next.isSideA)
|
|
|
|
{
|
|
|
|
target = sideB.get(next.targetEntityID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
target = sideA.get(next.targetEntityID);
|
|
|
|
}
|
|
|
|
if(target == null || !target.entity.isEntityAlive())
|
|
|
|
{
|
|
|
|
next = turnOrderQueue.poll();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int hitChance = TurnBasedMinecraftMod.config.getPlayerAttackProbability();
|
|
|
|
if(target.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
hitChance -= TurnBasedMinecraftMod.config.getPlayerEvasion();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hitChance -= target.entityInfo.evasion;
|
|
|
|
}
|
|
|
|
if((int)(Math.random() * 100) < hitChance)
|
|
|
|
{
|
|
|
|
if(target.remainingDefenses <= 0)
|
|
|
|
{
|
|
|
|
// attack
|
|
|
|
// TODO damage via bow and arrow
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = next.entity;
|
|
|
|
((EntityPlayer)next.entity).attackTargetEntityWithCurrentItem(target.entity);
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = null;
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), TurnBasedMinecraftMod.attackingDamage);
|
2018-09-05 06:54:06 +00:00
|
|
|
if(!(target.entity instanceof EntityPlayer) && target.entityInfo.defenseDamage > 0)
|
|
|
|
{
|
|
|
|
if((int)(Math.random() * 100) < target.entityInfo.defenseDamageProbability)
|
|
|
|
{
|
|
|
|
// defense damage
|
|
|
|
DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)target.entity);
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = target.entity;
|
|
|
|
next.entity.attackEntityFrom(defenseDamageSource, target.entityInfo.defenseDamage);
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = null;
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, target.entity.getEntityId(), next.entity.getEntityId(), target.entityInfo.defenseDamage);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// blocked
|
|
|
|
--target.remainingDefenses;
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFEND, target.entity.getEntityId(), next.entity.getEntityId(), 0);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// miss
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.MISS, next.entity.getEntityId(), target.entity.getEntityId(), 0);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(next.isSideA)
|
|
|
|
{
|
|
|
|
int randomTargetIndex = (int)(Math.random() * sideB.size());
|
|
|
|
for(Combatant c : sideB.values())
|
|
|
|
{
|
|
|
|
if(randomTargetIndex-- == 0)
|
|
|
|
{
|
|
|
|
target = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int randomTargetIndex = (int)(Math.random() * sideA.size());
|
|
|
|
for(Combatant c : sideA.values())
|
|
|
|
{
|
|
|
|
if(randomTargetIndex-- == 0)
|
|
|
|
{
|
|
|
|
target = c;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(target == null || !target.entity.isEntityAlive())
|
|
|
|
{
|
|
|
|
next = turnOrderQueue.poll();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int hitChance = next.entityInfo.attackProbability;
|
|
|
|
if(target.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
hitChance -= TurnBasedMinecraftMod.config.getPlayerEvasion();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hitChance -= target.entityInfo.evasion;
|
|
|
|
}
|
|
|
|
if((int)(Math.random() * 100) < hitChance)
|
|
|
|
{
|
|
|
|
if(target.remainingDefenses <= 0)
|
|
|
|
{
|
|
|
|
DamageSource damageSource = DamageSource.causeMobDamage((EntityLivingBase)next.entity);
|
|
|
|
int damageAmount = next.entityInfo.attackPower;
|
|
|
|
if(next.entityInfo.attackVariance > 0)
|
|
|
|
{
|
|
|
|
damageAmount += (int)(Math.random() * (next.entityInfo.attackVariance * 2 + 1)) - next.entityInfo.attackVariance;
|
|
|
|
}
|
|
|
|
// attack
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = next.entity;
|
2018-09-06 08:08:36 +00:00
|
|
|
target.entity.attackEntityFrom(damageSource, damageAmount);
|
2018-09-05 06:54:06 +00:00
|
|
|
TurnBasedMinecraftMod.attackingEntity = null;
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), damageAmount);
|
2018-09-05 06:54:06 +00:00
|
|
|
if(!(target.entity instanceof EntityPlayer) && target.entityInfo.defenseDamage > 0)
|
|
|
|
{
|
|
|
|
if((int)(Math.random() * 100) < target.entityInfo.defenseDamageProbability)
|
|
|
|
{
|
|
|
|
// defense damage
|
|
|
|
DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)target.entity);
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = target.entity;
|
|
|
|
next.entity.attackEntityFrom(defenseDamageSource, target.entityInfo.defenseDamage);
|
|
|
|
TurnBasedMinecraftMod.attackingEntity = null;
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, target.entity.getEntityId(), next.entity.getEntityId(), target.entityInfo.defenseDamage);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// blocked
|
|
|
|
--target.remainingDefenses;
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFEND, target.entity.getEntityId(), next.entity.getEntityId(), 0);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// miss
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.MISS, next.entity.getEntityId(), target.entity.getEntityId(), 0);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case DEFEND:
|
|
|
|
next.remainingDefenses = TurnBasedMinecraftMod.config.getDefenseDuration();
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENDING, next.entity.getEntityId(), 0, 0);
|
2018-09-05 06:54:06 +00:00
|
|
|
break;
|
|
|
|
case FLEE:
|
|
|
|
int fastestEnemySpeed = 0;
|
|
|
|
if(next.isSideA)
|
|
|
|
{
|
|
|
|
for(Combatant c : sideB.values())
|
|
|
|
{
|
|
|
|
if(c.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
if(TurnBasedMinecraftMod.config.getPlayerSpeed() > fastestEnemySpeed)
|
|
|
|
{
|
|
|
|
fastestEnemySpeed = TurnBasedMinecraftMod.config.getPlayerSpeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(c.entityInfo.speed > fastestEnemySpeed)
|
|
|
|
{
|
|
|
|
fastestEnemySpeed = c.entityInfo.speed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(Combatant c : sideA.values())
|
|
|
|
{
|
|
|
|
if(c.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
if(TurnBasedMinecraftMod.config.getPlayerSpeed() > fastestEnemySpeed)
|
|
|
|
{
|
|
|
|
fastestEnemySpeed = TurnBasedMinecraftMod.config.getPlayerSpeed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(c.entityInfo.speed > fastestEnemySpeed)
|
|
|
|
{
|
|
|
|
fastestEnemySpeed = c.entityInfo.speed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int fleeProbability = 0;
|
|
|
|
if(next.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
if(fastestEnemySpeed >= TurnBasedMinecraftMod.config.getPlayerSpeed())
|
|
|
|
{
|
|
|
|
fleeProbability = TurnBasedMinecraftMod.config.getFleeBadProbability();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fleeProbability = TurnBasedMinecraftMod.config.getFleeGoodProbability();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(fastestEnemySpeed >= next.entityInfo.speed)
|
|
|
|
{
|
|
|
|
fleeProbability = TurnBasedMinecraftMod.config.getFleeBadProbability();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fleeProbability = TurnBasedMinecraftMod.config.getFleeGoodProbability();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if((int)(Math.random() * 100) < fleeProbability)
|
|
|
|
{
|
|
|
|
// flee success
|
|
|
|
if(next.isSideA)
|
|
|
|
{
|
|
|
|
sideA.remove(next.entity.getEntityId());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sideB.remove(next.entity.getEntityId());
|
|
|
|
}
|
2018-09-06 08:08:36 +00:00
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 1);
|
2018-09-05 06:54:06 +00:00
|
|
|
if(next.entity instanceof EntityPlayer)
|
|
|
|
{
|
|
|
|
players.remove(next.entity.getEntityId());
|
|
|
|
playerCount.decrementAndGet();
|
2018-09-06 08:08:36 +00:00
|
|
|
PacketHandler.INSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)next.entity);
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 08:08:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// flee fail
|
|
|
|
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 0);
|
|
|
|
}
|
2018-09-05 06:54:06 +00:00
|
|
|
break;
|
|
|
|
case USE_ITEM:
|
|
|
|
break;
|
|
|
|
}
|
2018-09-04 06:21:49 +00:00
|
|
|
next = turnOrderQueue.poll();
|
|
|
|
}
|
2018-09-05 06:54:06 +00:00
|
|
|
for(Combatant c : sideA.values())
|
|
|
|
{
|
|
|
|
c.decision = Decision.UNDECIDED;
|
|
|
|
}
|
|
|
|
for(Combatant c : sideB.values())
|
|
|
|
{
|
|
|
|
c.decision = Decision.UNDECIDED;
|
|
|
|
}
|
|
|
|
state = State.HEALTH_CHECK;
|
|
|
|
update(Duration.ZERO);
|
2018-08-29 06:09:44 +00:00
|
|
|
break;
|
2018-09-05 06:54:06 +00:00
|
|
|
}
|
2018-08-29 06:09:44 +00:00
|
|
|
case HEALTH_CHECK:
|
2018-09-06 08:08:36 +00:00
|
|
|
Queue<Integer> removeQueue = new ArrayDeque<Integer>();
|
|
|
|
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;
|
2018-08-29 06:09:44 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-06 08:08:36 +00:00
|
|
|
return battleEnded;
|
2018-08-28 06:13:14 +00:00
|
|
|
}
|
|
|
|
}
|