diff --git a/src/harenet/BitPacker.java b/src/harenet/BitPacker.java index 6e41085..7f62ec1 100644 --- a/src/harenet/BitPacker.java +++ b/src/harenet/BitPacker.java @@ -530,61 +530,18 @@ public BitPacker pad() { return this; } - + public static void dumpBytes(byte[] value) { - System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); - System.out.println("| Dumping bytes, length: " + (value.length * 8) + " (" + value.length + " byte(s))"); - System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); - - int count = 0; - for (int j = 0; j < value.length; j++) { - - byte v = value[j]; - - for (int i = 0; i < Byte.SIZE; i++) { - if (((v >> i) & 1) == 1) { - System.out.print("1"); - } - else { - System.out.print("0"); - } - } - - System.out.print(" "); - count++; - if (count == 12) { - System.out.println(); - count = 0; - } - - } - System.out.println(); - System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); + StatementFactory statementFactory = new StatementFactory(); + Statement statement = statementFactory.getInstance(value); + statement.print(); } public void dump() { - System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); - System.out.println("| Dumping bitset, length: " + numBits); - System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); - - int count = 0; - - for (int i = 0; i < numBits; i++) { - System.out.print(data.getBit(i) ? "1" : "0"); - if ((i != 0) && (i % 8 == 7)) { - System.out.print(" "); - count++; - if (count == 12) { - System.out.println(); - count = 0; - } - - } - - } - System.out.println(); - System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); + StatementFactory statementFactory = new StatementFactory(); + Statement statement = statementFactory.getInstance(numBits,data); + statement.print(); } } \ No newline at end of file diff --git a/src/harenet/BitStatement.java b/src/harenet/BitStatement.java new file mode 100644 index 0000000..262e8b0 --- /dev/null +++ b/src/harenet/BitStatement.java @@ -0,0 +1,27 @@ +package harenet; + +public class BitStatement extends Statement { + private int numBits; + private BitArray data; + + public BitStatement(int number,BitArray bitData) { + numBits = number; + data = bitData; + } + + protected void printHeaderContents() { + System.out.println("| Dumping bitset, length: " + numBits); + } + + public void printBody() { + int count = 0; + + for (int i = 0; i < numBits; i++) { + printBit(data.getBit(i)); + if ((i != 0) && (i % 8 == 7)) { + count = countProcess(count); + } + + } + } +} diff --git a/src/harenet/ByteStatement.java b/src/harenet/ByteStatement.java new file mode 100644 index 0000000..35170a4 --- /dev/null +++ b/src/harenet/ByteStatement.java @@ -0,0 +1,28 @@ +package harenet; + +public class ByteStatement extends Statement { + private byte[] value; + + public ByteStatement(byte[] byteValue) { + value = byteValue; + } + + protected void printHeaderContents() { + System.out.println("| Dumping bytes, length: " + (value.length * 8) + " (" + value.length + " byte(s))"); + } + + public void printBody() { + int count = 0; + for (int j = 0; j < value.length; j++) { + + byte v = value[j]; + + for (int i = 0; i < Byte.SIZE; i++) { + printBit(((v >> i) & 1) == 1); + } + + count = countProcess(count); + + } + } +} diff --git a/src/harenet/Statement.java b/src/harenet/Statement.java new file mode 100644 index 0000000..b3923b1 --- /dev/null +++ b/src/harenet/Statement.java @@ -0,0 +1,48 @@ +package harenet; + +public abstract class Statement { + public void print() { + printHeader(); + printBody(); + printFooter(); + } + + public void printHeader() { + printLine(); + printHeaderContents(); + printLine(); + } + + public abstract void printBody(); + + public void printFooter() { + System.out.println(); + printLine(); + } + + private void printLine() { + System.out.println("+--------------- ------------- ------- ------ --- -- -- - -- -- --"); + } + + protected abstract void printHeaderContents(); + + protected void printBit(boolean isOne) { + if (isOne) { + System.out.print("1"); + } + else { + System.out.print("0"); + } + } + + protected int countProcess(int count) { + int tempCount = count; + System.out.print(" "); + tempCount++; + if (tempCount == 12) { + System.out.println(); + tempCount = 0; + } + return tempCount; + } +} diff --git a/src/harenet/StatementFactory.java b/src/harenet/StatementFactory.java new file mode 100644 index 0000000..31ad3f6 --- /dev/null +++ b/src/harenet/StatementFactory.java @@ -0,0 +1,10 @@ +package harenet; + +public class StatementFactory { + public Statement getInstance(byte[] value) { + return new ByteStatement(value); + } + public Statement getInstance(int numBits, BitArray data) { + return new BitStatement(numBits,data); + } +} \ No newline at end of file diff --git a/src/seventh/ai/basic/DefaultAISystem.java b/src/seventh/ai/basic/DefaultAISystem.java index fcdfdfb..665ef49 100644 --- a/src/seventh/ai/basic/DefaultAISystem.java +++ b/src/seventh/ai/basic/DefaultAISystem.java @@ -19,9 +19,11 @@ import seventh.ai.basic.commands.AICommands; import seventh.ai.basic.teamstrategy.CaptureTheFlagTeamStrategy; import seventh.ai.basic.teamstrategy.CommanderTeamStrategy; +import seventh.ai.basic.teamstrategy.DefaultAISystemTeamStrategyFactory; import seventh.ai.basic.teamstrategy.ObjectiveTeamStrategy; import seventh.ai.basic.teamstrategy.TDMTeamStrategy; import seventh.ai.basic.teamstrategy.TeamStrategy; +import seventh.ai.basic.teamstrategy.TeamStrategyFactory; import seventh.game.GameInfo; import seventh.game.PlayerInfo; import seventh.game.PlayerInfos; @@ -169,27 +171,9 @@ public void init(final GameInfo game) { GameType gameType = game.getGameType(); - switch(gameType.getType()) { - case CTF: - this.alliedAIStrategy = new CaptureTheFlagTeamStrategy(this, gameType.getAlliedTeam()); - this.axisAIStrategy = new CaptureTheFlagTeamStrategy(this, gameType.getAxisTeam()); - break; - case OBJ: - this.alliedAIStrategy = new ObjectiveTeamStrategy(this, gameType.getAlliedTeam()); - this.axisAIStrategy = new ObjectiveTeamStrategy(this, gameType.getAxisTeam()); - break; - case CMD: - this.alliedAIStrategy = new CommanderTeamStrategy((CommanderGameType)gameType, this, gameType.getAlliedTeam()); - this.axisAIStrategy = new CommanderTeamStrategy((CommanderGameType)gameType, this, gameType.getAxisTeam()); - break; - case TDM: - default: - this.alliedAIStrategy = new TDMTeamStrategy(this, gameType.getAlliedTeam()); - this.axisAIStrategy = new TDMTeamStrategy(this, gameType.getAxisTeam()); - break; - - } - + TeamStrategyFactory teamStrategyFactory = new DefaultAISystemTeamStrategyFactory(); + this.alliedAIStrategy = teamStrategyFactory.createAlliedAIStrategy(this, gameType); + this.axisAIStrategy = teamStrategyFactory.createAxisAIStrategy(this, gameType); PlayerInfos players = game.getPlayerInfos(); players.forEachPlayerInfo(new PlayerInfoIterator() { diff --git a/src/seventh/ai/basic/FeelSensor.java b/src/seventh/ai/basic/FeelSensor.java index a346ada..c803003 100644 --- a/src/seventh/ai/basic/FeelSensor.java +++ b/src/seventh/ai/basic/FeelSensor.java @@ -21,7 +21,7 @@ public class FeelSensor implements Sensor, OnDamageListener { private FeelMemory memory; private TimeStep timeStep; private Brain brain; - + /** * @param brain */ diff --git a/src/seventh/ai/basic/SensorFactory.java b/src/seventh/ai/basic/SensorFactory.java new file mode 100644 index 0000000..2586f59 --- /dev/null +++ b/src/seventh/ai/basic/SensorFactory.java @@ -0,0 +1,42 @@ +package seventh.ai.basic; + +import seventh.shared.TimeStep; + +public class SensorFactory { + + protected SightSensor sightSensor; + protected SoundSensor soundSensor; + protected FeelSensor feelSensor; + + public SensorFactory(Brain brain){ + this.sightSensor = new SightSensor(brain); + this.soundSensor = new SoundSensor(brain); + this.feelSensor = new FeelSensor(brain); + } + + public FeelSensor getFeelSensor() { + return feelSensor; + } + + public SightSensor getSightSensor() { + return sightSensor; + } + + public SoundSensor getSoundSensor() { + return soundSensor; + } + + public void reset(Brain brain) { + this.sightSensor.reset(brain); + this.soundSensor.reset(brain); + this.feelSensor.reset(brain); + } + + public void update(TimeStep timeStep) { + this.sightSensor.update(timeStep); + this.soundSensor.update(timeStep); + this.feelSensor.update(timeStep); + } + + +} diff --git a/src/seventh/ai/basic/Sensors.java b/src/seventh/ai/basic/Sensors.java index 035b3e1..d3fe866 100644 --- a/src/seventh/ai/basic/Sensors.java +++ b/src/seventh/ai/basic/Sensors.java @@ -13,16 +13,12 @@ */ public class Sensors { - private SightSensor sightSensor; - private SoundSensor soundSensor; - private FeelSensor feelSensor; + private SensorFactory sensorfactory; public Sensors(Brain brain) { - this.sightSensor = new SightSensor(brain); - this.soundSensor = new SoundSensor(brain); - this.feelSensor = new FeelSensor(brain); + this.sensorfactory = new SensorFactory(brain); } @@ -32,30 +28,28 @@ public Sensors(Brain brain) { * @param brain */ public void reset(Brain brain) { - this.sightSensor.reset(brain); - this.soundSensor.reset(brain); - this.feelSensor.reset(brain); + this.sensorfactory.reset(brain); } /** * @return the feelSensor */ public FeelSensor getFeelSensor() { - return feelSensor; + return this.sensorfactory.getFeelSensor(); } /** * @return the sightSensor */ public SightSensor getSightSensor() { - return sightSensor; + return this.sensorfactory.getSightSensor(); } /** * @return the soundSensor */ public SoundSensor getSoundSensor() { - return soundSensor; + return this.sensorfactory.getSoundSensor(); } /** @@ -63,9 +57,7 @@ public SoundSensor getSoundSensor() { * @param timeStep */ public void update(TimeStep timeStep) { - this.sightSensor.update(timeStep); - this.soundSensor.update(timeStep); - this.feelSensor.update(timeStep); + this.sensorfactory.update(timeStep); } } diff --git a/src/seventh/ai/basic/SightSensor.java b/src/seventh/ai/basic/SightSensor.java index 478433b..f7e3e10 100644 --- a/src/seventh/ai/basic/SightSensor.java +++ b/src/seventh/ai/basic/SightSensor.java @@ -30,7 +30,7 @@ public class SightSensor implements Sensor { private Timer updateSight; private List entitiesInView; - + /** * @param width * @param height diff --git a/src/seventh/ai/basic/SoundSensor.java b/src/seventh/ai/basic/SoundSensor.java index 00bd07b..8221d00 100644 --- a/src/seventh/ai/basic/SoundSensor.java +++ b/src/seventh/ai/basic/SoundSensor.java @@ -24,8 +24,7 @@ * @author Tony * */ -public class SoundSensor implements Sensor { - +public class SoundSensor implements Sensor{ /** * Less important sounds */ diff --git a/src/seventh/ai/basic/World.java b/src/seventh/ai/basic/World.java index 275a126..58a3a28 100644 --- a/src/seventh/ai/basic/World.java +++ b/src/seventh/ai/basic/World.java @@ -451,67 +451,160 @@ public Zone findAdjacentZone(Zone zone, int minDistance) { } private Zone getNorthZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x; - int adjacentZoneY = bounds.y - (bounds.height/2 + fuzzy + minDistance); - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new NorthZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getNorthWestZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x - (bounds.width/2 + fuzzy + minDistance); - int adjacentZoneY = bounds.y - (bounds.height/2 + fuzzy + minDistance); - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new NorthWestZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getNorthEastZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x + (bounds.width+(bounds.width/2) + fuzzy + minDistance); - int adjacentZoneY = bounds.y - (bounds.height/2 + fuzzy + minDistance); - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new NorthEastZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getEastZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x + (bounds.width+(bounds.width/2) + fuzzy + minDistance); - int adjacentZoneY = bounds.y; - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new EastZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getSouthZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x; - int adjacentZoneY = bounds.y + (bounds.height+(bounds.height/2) + fuzzy + minDistance); - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new SouthZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getSouthWestZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x - (bounds.width/2 + fuzzy + minDistance); - int adjacentZoneY = bounds.y + (bounds.height+(bounds.height/2) + fuzzy + minDistance); - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new SouthWestZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getSouthEastZone(Rectangle bounds, int fuzzy, int minDistance) { - - int adjacentZoneX = bounds.x + (bounds.width+(bounds.width/2) + fuzzy + minDistance); - int adjacentZoneY = bounds.y + (bounds.height+(bounds.height/2) + fuzzy + minDistance); - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; + return new SouthEastZone().getAdjacentZone(bounds, fuzzy, minDistance); } private Zone getWestZone(Rectangle bounds, int fuzzy, int minDistance) { + return new WestZone().getAdjacentZone(bounds, fuzzy, minDistance); + } + + private abstract class AdjacentZone { + public Zone getAdjacentZone(Rectangle bounds, int fuzzy, int minDistance) { + + int adjacentZoneX = getAdjacentZoneX(bounds, fuzzy, minDistance); + int adjacentZoneY = getAdjacentZoneY(bounds, fuzzy, minDistance); + Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); + return adjacentZone; + } + + protected abstract int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance); + protected abstract int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance); + } + + private class NorthZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x; + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y - (bounds.height/2 + fuzzy + minDistance); + } + + } + + private class NorthWestZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x - (bounds.width/2 + fuzzy + minDistance); + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y - (bounds.height/2 + fuzzy + minDistance); + } + + } + + private class NorthEastZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x + (bounds.width+(bounds.width/2) + fuzzy + minDistance); + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y - (bounds.height/2 + fuzzy + minDistance); + } + + } + + private class EastZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x + (bounds.width+(bounds.width/2) + fuzzy + minDistance); + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y; + } + + } + + private class SouthZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x; + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y + (bounds.height+(bounds.height/2) + fuzzy + minDistance); + } + + } + + private class SouthWestZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x - (bounds.width/2 + fuzzy + minDistance); + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y + (bounds.height+(bounds.height/2) + fuzzy + minDistance); + } + + } + + private class SouthEastZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x + (bounds.width+(bounds.width/2) + fuzzy + minDistance); + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y + (bounds.height+(bounds.height/2) + fuzzy + minDistance); + } + + } + + private class WestZone extends AdjacentZone { + + @Override + protected int getAdjacentZoneX(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.x - (bounds.width/2 + fuzzy + minDistance); + } + + @Override + protected int getAdjacentZoneY(Rectangle bounds, int fuzzy, int minDistance) { + return bounds.y; + } - int adjacentZoneX = bounds.x - (bounds.width/2 + fuzzy + minDistance); - int adjacentZoneY = bounds.y; - Zone adjacentZone = getZone(adjacentZoneX, adjacentZoneY); - return adjacentZone; } /** diff --git a/src/seventh/ai/basic/actions/ActionSubject.java b/src/seventh/ai/basic/actions/ActionSubject.java new file mode 100644 index 0000000..2e8cef1 --- /dev/null +++ b/src/seventh/ai/basic/actions/ActionSubject.java @@ -0,0 +1,16 @@ +package seventh.ai.basic.actions; + +import java.util.List; +import java.util.ArrayList; +import seventh.ai.basic.Brain; +import seventh.shared.TimeStep; + +public abstract class ActionSubject { + private List actions = new ArrayList(); + public void attach(Action action) { actions.add(action); } + public void detach(Action action) { actions.remove(action); } + public void notifyActions(Brain brain, TimeStep timeStep) { + for(Action action : actions) + action.update(brain, timeStep); + } +} diff --git a/src/seventh/ai/basic/group/AIGroupAction.java b/src/seventh/ai/basic/group/AIGroupAction.java index 129b6bc..3146315 100644 --- a/src/seventh/ai/basic/group/AIGroupAction.java +++ b/src/seventh/ai/basic/group/AIGroupAction.java @@ -12,10 +12,18 @@ */ public abstract class AIGroupAction implements Updatable { - public abstract void start(AIGroup aIGroup); + protected Start start; + protected GetAction action; + public abstract void end(AIGroup aIGroup); public abstract void cancel(AIGroup aIGroup); public abstract boolean isFinished(AIGroup aIGroup); - public abstract Action getAction(AIGroup aIGroup); + + public void start(AIGroup aIGroup) { + start.start(aIGroup); + } + public Action getAction(AIGroup aIGroup) { + return action.getAction(aIGroup); + } } diff --git a/src/seventh/ai/basic/group/AIGroupAttackAction.java b/src/seventh/ai/basic/group/AIGroupAttackAction.java index 07add40..b57a8a4 100644 --- a/src/seventh/ai/basic/group/AIGroupAttackAction.java +++ b/src/seventh/ai/basic/group/AIGroupAttackAction.java @@ -32,43 +32,10 @@ public class AIGroupAttackAction extends AIGroupAction { public AIGroupAttackAction(Vector2f position) { this.attackPosition = position; this.attackDirections = new ArrayList<>(); + action = new AttackGetAction(position); + start = new AttackStart(position); } - @Override - public void start(AIGroup aIGroup) { - World world = aIGroup.getWorld(); - - this.attackDirections.addAll(world.getAttackDirections(attackPosition, 150f, aIGroup.groupSize())); - if(attackDirections.isEmpty()) { - attackDirections.add(new AttackDirection(attackPosition)); - } - } - - /* (non-Javadoc) - * @see seventh.ai.basic.group.AIGroupAction#getAction(seventh.ai.basic.group.AIGroup) - */ - @Override - public Action getAction(AIGroup aIGroup) { - if(aIGroup.groupSize()>0) { - World world = aIGroup.getWorld(); - Brain[] members = aIGroup.getMembers(); - Roles roles = aIGroup.getRoles(); - - int j = 0; - for(int i = 0; i < members.length; i++) { - Brain member = members[i]; - if(member!=null) { - if(roles.getAssignedRole(member.getPlayer()) != Role.None) { - AttackDirection dir = attackDirections.get( (j+=1) % attackDirections.size()); - return new SequencedAction("squadAttack") - .addNext(world.getGoals().moveToAction(dir.getDirection())); - } - } - } - } - - return new WaitAction(500); - } /* (non-Javadoc) * @see seventh.ai.basic.group.AIGroupAction#end(seventh.ai.basic.group.AIGroup) diff --git a/src/seventh/ai/basic/group/AIGroupDefendAction.java b/src/seventh/ai/basic/group/AIGroupDefendAction.java index 5bda7cb..22a7a96 100644 --- a/src/seventh/ai/basic/group/AIGroupDefendAction.java +++ b/src/seventh/ai/basic/group/AIGroupDefendAction.java @@ -29,54 +29,10 @@ public class AIGroupDefendAction extends AIGroupAction { public AIGroupDefendAction(Vector2f position) { this.defendPosition = position; this.directionsToDefend = new ArrayList<>(); + action = new DefendGetAction(position); + start = new DefendStart(position); } - @Override - public void start(AIGroup aIGroup) { - World world = aIGroup.getWorld(); - float radius = (float)world.getRandom().getRandomRange(100f, 150f); - directionsToDefend.addAll(world.getAttackDirections(this.defendPosition, radius, 12)); - - } - - /* (non-Javadoc) - * @see seventh.ai.basic.group.AIGroupAction#getAction(seventh.ai.basic.group.AIGroup) - */ - @Override - public Action getAction(AIGroup aIGroup) { - if(aIGroup.groupSize() > 0 ) { - World world = aIGroup.getWorld(); - Brain[] members = aIGroup.getMembers(); -// Roles roles = aIGroup.getRoles(); - - int squadSize = aIGroup.groupSize(); - - if(!directionsToDefend.isEmpty() && squadSize>0) { - int increment = 1; - if(directionsToDefend.size()> squadSize) { - increment = directionsToDefend.size() / squadSize; - } - - int i = 0; - for(int j = 0; j < members.length; j++) { - Brain member = members[j]; - if(member!=null) { - //if(roles.getAssignedRole(member.getPlayer()) != Role.None) - { - AttackDirection dir = directionsToDefend.get( (i += increment) % directionsToDefend.size()); - Vector2f position = new Vector2f(dir.getDirection()); - //Vector2f.Vector2fMA(defendPosition, dir.getDirection(), 10f + world.getRandom().nextInt(100), position); - - return (world.getGoals().guard(position)); - } - } - - } - } - } - - return new WaitAction(500); - } /* (non-Javadoc) * @see seventh.ai.basic.group.AIGroupAction#end(seventh.ai.basic.group.AIGroup) diff --git a/src/seventh/ai/basic/group/AttackGetAction.java b/src/seventh/ai/basic/group/AttackGetAction.java new file mode 100644 index 0000000..bc2f939 --- /dev/null +++ b/src/seventh/ai/basic/group/AttackGetAction.java @@ -0,0 +1,51 @@ +package seventh.ai.basic.group; + +import java.util.ArrayList; +import java.util.List; + +import seventh.ai.basic.AttackDirection; +import seventh.ai.basic.Brain; +import seventh.ai.basic.World; +import seventh.ai.basic.actions.Action; +import seventh.ai.basic.actions.SequencedAction; +import seventh.ai.basic.actions.WaitAction; +import seventh.ai.basic.teamstrategy.Roles; +import seventh.ai.basic.teamstrategy.Roles.Role; +import seventh.math.Vector2f; + +public class AttackGetAction implements GetAction { + private Vector2f attackPosition; + private List attackDirections; + + + public AttackGetAction(Vector2f Position) { + this.attackPosition = Position; + this.attackDirections = new ArrayList<>(); + } + /* (non-Javadoc) + * @see seventh.ai.basic.group.AIGroupAction#getAction(seventh.ai.basic.group.AIGroup) + */ + @Override + public Action getAction(AIGroup aIGroup) { + if(aIGroup.groupSize()>0) { + World world = aIGroup.getWorld(); + Brain[] members = aIGroup.getMembers(); + Roles roles = aIGroup.getRoles(); + + int j = 0; + for(int i = 0; i < members.length; i++) { + Brain member = members[i]; + if(member!=null) { + if(roles.getAssignedRole(member.getPlayer()) != Role.None) { + AttackDirection dir = attackDirections.get( (j+=1) % attackDirections.size()); + return new SequencedAction("squadAttack") + .addNext(world.getGoals().moveToAction(dir.getDirection())); + } + } + } + } + + return new WaitAction(500); + } + +} diff --git a/src/seventh/ai/basic/group/AttackStart.java b/src/seventh/ai/basic/group/AttackStart.java new file mode 100644 index 0000000..c75fc05 --- /dev/null +++ b/src/seventh/ai/basic/group/AttackStart.java @@ -0,0 +1,28 @@ +package seventh.ai.basic.group; + +import java.util.ArrayList; +import java.util.List; + +import seventh.ai.basic.AttackDirection; +import seventh.ai.basic.World; +import seventh.math.Vector2f; + +public class AttackStart implements Start { + + private Vector2f attackPosition; + private List attackDirections; + + public AttackStart(Vector2f Position) { + this.attackPosition = Position; + this.attackDirections = new ArrayList<>(); + } + + public void start(AIGroup aIGroup) { + World world = aIGroup.getWorld(); + + this.attackDirections.addAll(world.getAttackDirections(attackPosition, 150f, aIGroup.groupSize())); + if(attackDirections.isEmpty()) { + attackDirections.add(new AttackDirection(attackPosition)); + } + } +} diff --git a/src/seventh/ai/basic/group/DefendGetAction.java b/src/seventh/ai/basic/group/DefendGetAction.java new file mode 100644 index 0000000..63592ff --- /dev/null +++ b/src/seventh/ai/basic/group/DefendGetAction.java @@ -0,0 +1,58 @@ +package seventh.ai.basic.group; + +import java.util.ArrayList; +import java.util.List; + +import seventh.ai.basic.AttackDirection; +import seventh.ai.basic.Brain; +import seventh.ai.basic.World; +import seventh.ai.basic.actions.Action; +import seventh.ai.basic.actions.WaitAction; +import seventh.math.Vector2f; + +public class DefendGetAction implements GetAction { + + private Vector2f defendPosition; + private List directionsToDefend; + + public DefendGetAction(Vector2f Position) { + this.defendPosition = Position; + this.directionsToDefend = new ArrayList<>(); + } + + public Action getAction(AIGroup aIGroup) { + if(aIGroup.groupSize() > 0 ) { + World world = aIGroup.getWorld(); + Brain[] members = aIGroup.getMembers(); +// Roles roles = aIGroup.getRoles(); + + int squadSize = aIGroup.groupSize(); + + if(!directionsToDefend.isEmpty() && squadSize>0) { + int increment = 1; + if(directionsToDefend.size()> squadSize) { + increment = directionsToDefend.size() / squadSize; + } + + int i = 0; + for(int j = 0; j < members.length; j++) { + Brain member = members[j]; + if(member!=null) { + //if(roles.getAssignedRole(member.getPlayer()) != Role.None) + { + AttackDirection dir = directionsToDefend.get( (i += increment) % directionsToDefend.size()); + Vector2f position = new Vector2f(dir.getDirection()); + //Vector2f.Vector2fMA(defendPosition, dir.getDirection(), 10f + world.getRandom().nextInt(100), position); + + return (world.getGoals().guard(position)); + } + } + + } + } + } + + return new WaitAction(500); + } + +} diff --git a/src/seventh/ai/basic/group/DefendStart.java b/src/seventh/ai/basic/group/DefendStart.java new file mode 100644 index 0000000..918d0b9 --- /dev/null +++ b/src/seventh/ai/basic/group/DefendStart.java @@ -0,0 +1,27 @@ +package seventh.ai.basic.group; + +import java.util.ArrayList; +import java.util.List; + +import seventh.ai.basic.AttackDirection; +import seventh.ai.basic.World; +import seventh.math.Vector2f; + +public class DefendStart implements Start { + + + private Vector2f defendPosition; + private List directionsToDefend; + + public DefendStart(Vector2f Position) { + this.defendPosition = Position; + this.directionsToDefend = new ArrayList<>(); + } + + public void start(AIGroup aIGroup) { + World world = aIGroup.getWorld(); + float radius = (float)world.getRandom().getRandomRange(100f, 150f); + directionsToDefend.addAll(world.getAttackDirections(this.defendPosition, radius, 12)); + + } +} diff --git a/src/seventh/ai/basic/group/GetAction.java b/src/seventh/ai/basic/group/GetAction.java new file mode 100644 index 0000000..3e15c3f --- /dev/null +++ b/src/seventh/ai/basic/group/GetAction.java @@ -0,0 +1,7 @@ +package seventh.ai.basic.group; + +import seventh.ai.basic.actions.Action; + +public interface GetAction { + public Action getAction(AIGroup aIGroup); +} diff --git a/src/seventh/ai/basic/group/Start.java b/src/seventh/ai/basic/group/Start.java new file mode 100644 index 0000000..ff166a5 --- /dev/null +++ b/src/seventh/ai/basic/group/Start.java @@ -0,0 +1,5 @@ +package seventh.ai.basic.group; + +public interface Start { + public void start(AIGroup aIGroup); +} diff --git a/src/seventh/ai/basic/memory/ExpireStrategy.java b/src/seventh/ai/basic/memory/ExpireStrategy.java new file mode 100644 index 0000000..8736988 --- /dev/null +++ b/src/seventh/ai/basic/memory/ExpireStrategy.java @@ -0,0 +1,5 @@ +package seventh.ai.basic.memory; + +public interface ExpireStrategy { + public void expire(); +} diff --git a/src/seventh/ai/basic/memory/FeelExpireStrategy.java b/src/seventh/ai/basic/memory/FeelExpireStrategy.java new file mode 100644 index 0000000..2a06c7b --- /dev/null +++ b/src/seventh/ai/basic/memory/FeelExpireStrategy.java @@ -0,0 +1,13 @@ +package seventh.ai.basic.memory; + +import seventh.game.entities.Entity; + +public class FeelExpireStrategy implements ExpireStrategy{ + protected Entity damager; + protected boolean isValid; + @Override + public void expire() { + this.damager = null; + this.isValid = false; + } +} \ No newline at end of file diff --git a/src/seventh/ai/basic/memory/FeelMemory.java b/src/seventh/ai/basic/memory/FeelMemory.java index d686aa9..6953fd8 100644 --- a/src/seventh/ai/basic/memory/FeelMemory.java +++ b/src/seventh/ai/basic/memory/FeelMemory.java @@ -7,7 +7,6 @@ import seventh.shared.SeventhConstants; import seventh.shared.TimeStep; import seventh.shared.Updatable; - /** * @author Tony * @@ -20,21 +19,18 @@ public class FeelMemory implements Updatable { * @author Tony * */ - public static class FeelMemoryRecord { - private final long expireTime; + public static class FeelMemoryRecord extends MemoryRecord{ private Entity damager; private long timeFelt; private long timeFeltAgo; - private boolean isValid; /** * @param expireTime */ public FeelMemoryRecord(long expireTime) { - this.expireTime = expireTime; - this.isValid = false; + super(expireTime); } @@ -66,17 +62,14 @@ public boolean isValid() { public void checkExpired(TimeStep timeStep) { this.timeFeltAgo = timeStep.getGameClock() - this.timeFelt; if(isExpired(timeStep)) { - expire(); + expireStrategy = new FeelExpireStrategy(); } } /** * Expire this record */ - public void expire() { - this.damager = null; - this.isValid = false; - } + /** * If this entity is expired @@ -141,7 +134,7 @@ public void update(TimeStep timeStep) { */ public void clear() { for(int i = 0; i < this.feelingRecords.length; i++) { - this.feelingRecords[i].expire(); + this.feelingRecords[i].expireStrategy = new FeelExpireStrategy(); } } diff --git a/src/seventh/ai/basic/memory/MemoryRecord.java b/src/seventh/ai/basic/memory/MemoryRecord.java new file mode 100644 index 0000000..fd19478 --- /dev/null +++ b/src/seventh/ai/basic/memory/MemoryRecord.java @@ -0,0 +1,23 @@ +package seventh.ai.basic.memory; + +import seventh.ai.basic.memory.ExpireStrategy; + +public abstract class MemoryRecord { + protected final long expireTime; + protected boolean isValid; + ExpireStrategy expireStrategy; + public MemoryRecord(long expireTime) + { + this.expireTime = expireTime; + this.setValid(false); + } + public long getExpireTime() { + return expireTime; + } + public boolean isValid() { + return isValid; + } + public void setValid(boolean isValid) { + this.isValid = isValid; + } +} \ No newline at end of file diff --git a/src/seventh/ai/basic/memory/SightExpireStrategy.java b/src/seventh/ai/basic/memory/SightExpireStrategy.java new file mode 100644 index 0000000..b615b81 --- /dev/null +++ b/src/seventh/ai/basic/memory/SightExpireStrategy.java @@ -0,0 +1,20 @@ +package seventh.ai.basic.memory; + +import seventh.game.entities.PlayerEntity; +import seventh.game.weapons.Weapon; +import seventh.math.Vector2f; + +public class SightExpireStrategy implements ExpireStrategy { + + protected PlayerEntity entity; + protected Weapon lastSeenWithWeapon; + protected Vector2f lastSeenAt; + protected boolean isValid; + @Override + public void expire() { + this.entity = null; + this.lastSeenWithWeapon = null; + this.lastSeenAt.zeroOut(); + this.isValid = false; + } +} \ No newline at end of file diff --git a/src/seventh/ai/basic/memory/SightMemory.java b/src/seventh/ai/basic/memory/SightMemory.java index 3992ff8..4a96dfe 100644 --- a/src/seventh/ai/basic/memory/SightMemory.java +++ b/src/seventh/ai/basic/memory/SightMemory.java @@ -15,7 +15,7 @@ /** * Visual sight memory -- what this bot has seen, we store in a small cache so that he doesn't immediately forget the * world around him - * + * * * @author Tony * @@ -29,8 +29,7 @@ public class SightMemory implements Updatable { * @author Tony * */ - public static class SightMemoryRecord { - private final long expireTime; + public static class SightMemoryRecord extends MemoryRecord { private PlayerEntity entity; private Weapon lastSeenWithWeapon; @@ -38,15 +37,13 @@ public static class SightMemoryRecord { private long timeSeen; private long seenDelta; - private boolean isValid; /** * @param expireTime */ public SightMemoryRecord(long expireTime) { - this.expireTime = expireTime; + super(expireTime); this.lastSeenAt = new Vector2f(); - this.isValid = false; } @@ -82,20 +79,11 @@ public boolean isValid() { public void checkExpired(TimeStep timeStep) { this.seenDelta = timeStep.getGameClock() - this.timeSeen; if(isExpired(timeStep)) { - expire(); + expireStrategy = new SightExpireStrategy(); } } - /** - * Expire this record - */ - public void expire() { - this.entity = null; - this.lastSeenWithWeapon = null; - - this.lastSeenAt.zeroOut(); - this.isValid = false; - } + /** * If this entity is expired @@ -183,7 +171,7 @@ public void update(TimeStep timeStep) { */ public void clear() { for(int i = 0; i < this.entityRecords.length; i++) { - this.entityRecords[i].expire(); + this.entityRecords[i].expireStrategy = new SightExpireStrategy(); } } diff --git a/src/seventh/ai/basic/memory/SoundExpireStrategy.java b/src/seventh/ai/basic/memory/SoundExpireStrategy.java new file mode 100644 index 0000000..71f1617 --- /dev/null +++ b/src/seventh/ai/basic/memory/SoundExpireStrategy.java @@ -0,0 +1,15 @@ +package seventh.ai.basic.memory; + +import seventh.game.events.SoundEmittedEvent; +import seventh.shared.SoundType; + +public class SoundExpireStrategy implements ExpireStrategy{ + protected SoundEmittedEvent sound; + protected boolean isValid; + @Override + public void expire() { + this.sound.setId(-1); + this.sound.setSoundType(SoundType.MUTE); + this.isValid = false; + } +} \ No newline at end of file diff --git a/src/seventh/ai/basic/memory/SoundMemory.java b/src/seventh/ai/basic/memory/SoundMemory.java index 9bd264e..6db0a37 100644 --- a/src/seventh/ai/basic/memory/SoundMemory.java +++ b/src/seventh/ai/basic/memory/SoundMemory.java @@ -17,7 +17,7 @@ /** * Sound memory -- what this bot has heard, we store in a small cache so that he doesn't immediately forget the * world around him - * + * * * @author Tony * @@ -31,20 +31,17 @@ public class SoundMemory implements Updatable { * @author Tony * */ - public static class SoundMemoryRecord { - private final long expireTime; + public static class SoundMemoryRecord extends MemoryRecord{ private long timeHeard; private long timeHeardAgo; private SoundEmittedEvent sound; - private boolean isValid; /** * @param expireTime */ public SoundMemoryRecord(long expireTime) { - this.expireTime = expireTime; - this.isValid = false; + super(expireTime); this.sound = new SoundEmittedEvent(this, 0, SoundType.MUTE, new Vector2f()); } @@ -79,19 +76,11 @@ public boolean isValid() { public void checkExpired(TimeStep timeStep) { this.timeHeardAgo = timeStep.getGameClock() - this.timeHeard; if(isExpired(timeStep)) { - expire(); + expireStrategy = new SoundExpireStrategy(); } } - /** - * Expire this record - */ - public void expire() { - this.sound.setId(-1); - this.sound.setSoundType(SoundType.MUTE); - this.isValid = false; - } - + /** * If this sound is expired * @@ -153,7 +142,7 @@ public void update(TimeStep timeStep) { */ public void clear() { for(int i = 0; i < this.soundRecords.length; i++) { - this.soundRecords[i].expire(); + this.soundRecords[i].expireStrategy = new SoundExpireStrategy(); } } diff --git a/src/seventh/ai/basic/teamstrategy/DefaultAISystemTeamStrategyFactory.java b/src/seventh/ai/basic/teamstrategy/DefaultAISystemTeamStrategyFactory.java new file mode 100644 index 0000000..d8b1993 --- /dev/null +++ b/src/seventh/ai/basic/teamstrategy/DefaultAISystemTeamStrategyFactory.java @@ -0,0 +1,51 @@ +package seventh.ai.basic.teamstrategy; + +import seventh.ai.AISystem; +import seventh.ai.basic.DefaultAISystem; +import seventh.game.type.GameType; +import seventh.game.type.cmd.CommanderGameType; + +public class DefaultAISystemTeamStrategyFactory extends TeamStrategyFactory { + + @Override + protected TeamStrategy createCTFAlliedAIStrategy(AISystem aiSystem, GameType gameType) { + return new CaptureTheFlagTeamStrategy((DefaultAISystem)aiSystem, gameType.getAlliedTeam()); + } + + @Override + protected TeamStrategy createCTFAxisAIStrategy(AISystem aiSystem, GameType gameType) { + return new CaptureTheFlagTeamStrategy((DefaultAISystem)aiSystem, gameType.getAxisTeam()); + } + + @Override + protected TeamStrategy createOBJAlliedAIStrategy(AISystem aiSystem, GameType gameType) { + return new ObjectiveTeamStrategy((DefaultAISystem)aiSystem, gameType.getAlliedTeam()); + } + + @Override + protected TeamStrategy createOBJAxisAIStrategy(AISystem aiSystem, GameType gameType) { + return new ObjectiveTeamStrategy((DefaultAISystem)aiSystem, gameType.getAxisTeam()); + } + + @Override + protected TeamStrategy createCMDAlliedAIStrategy(AISystem aiSystem, GameType gameType) { + return new CommanderTeamStrategy((CommanderGameType)gameType, (DefaultAISystem)aiSystem, gameType.getAlliedTeam()); + } + + @Override + protected TeamStrategy createCMDAxisAIStrategy(AISystem aiSystem, GameType gameType) { + return new CommanderTeamStrategy((CommanderGameType)gameType, (DefaultAISystem)aiSystem, gameType.getAxisTeam()); + } + + @Override + protected TeamStrategy createTDMAlliedAIStrategy(AISystem aiSystem, GameType gameType) { + return new TDMTeamStrategy((DefaultAISystem)aiSystem, gameType.getAlliedTeam()); + } + + @Override + protected TeamStrategy createTDMAxisAIStrategy(AISystem aiSystem, GameType gameType) { + return new TDMTeamStrategy((DefaultAISystem)aiSystem, gameType.getAxisTeam()); + } + + +} diff --git a/src/seventh/ai/basic/teamstrategy/TeamStrategyFactory.java b/src/seventh/ai/basic/teamstrategy/TeamStrategyFactory.java new file mode 100644 index 0000000..2f5b54e --- /dev/null +++ b/src/seventh/ai/basic/teamstrategy/TeamStrategyFactory.java @@ -0,0 +1,60 @@ +package seventh.ai.basic.teamstrategy; + +import seventh.ai.AISystem; +import seventh.game.type.GameType; + +public abstract class TeamStrategyFactory { + + public TeamStrategy createAlliedAIStrategy(AISystem aiSystem, GameType gameType) { + TeamStrategy teamStrategy; + switch(gameType.getType()) { + case CTF: + teamStrategy = createCTFAlliedAIStrategy(aiSystem, gameType); + break; + case OBJ: + teamStrategy = createOBJAlliedAIStrategy(aiSystem, gameType); + break; + case CMD: + teamStrategy = createCMDAlliedAIStrategy(aiSystem, gameType); + break; + case TDM: + default: + teamStrategy = createTDMAlliedAIStrategy(aiSystem, gameType); + break; + } + return teamStrategy; + } + + public TeamStrategy createAxisAIStrategy(AISystem aiSystem, GameType gameType) { + TeamStrategy teamStrategy; + switch(gameType.getType()) { + case CTF: + teamStrategy = createCTFAxisAIStrategy(aiSystem, gameType); + break; + case OBJ: + teamStrategy = createOBJAxisAIStrategy(aiSystem, gameType); + break; + case CMD: + teamStrategy = createCMDAxisAIStrategy(aiSystem, gameType); + break; + case TDM: + default: + teamStrategy = createTDMAxisAIStrategy(aiSystem, gameType); + break; + } + return teamStrategy; + } + + protected abstract TeamStrategy createCTFAlliedAIStrategy(AISystem aiSystem, GameType gameType); + protected abstract TeamStrategy createCTFAxisAIStrategy(AISystem aiSystem, GameType gameType); + + protected abstract TeamStrategy createOBJAlliedAIStrategy(AISystem aiSystem, GameType gameType); + protected abstract TeamStrategy createOBJAxisAIStrategy(AISystem aiSystem, GameType gameType); + + protected abstract TeamStrategy createCMDAlliedAIStrategy(AISystem aiSystem, GameType gameType); + protected abstract TeamStrategy createCMDAxisAIStrategy(AISystem aiSystem, GameType gameType); + + protected abstract TeamStrategy createTDMAlliedAIStrategy(AISystem aiSystem, GameType gameType); + protected abstract TeamStrategy createTDMAxisAIStrategy(AISystem aiSystem, GameType gameType); + +} diff --git a/src/seventh/client/gfx/WeaponClassDescription.java b/src/seventh/client/gfx/WeaponClassDescription.java new file mode 100644 index 0000000..3a892d5 --- /dev/null +++ b/src/seventh/client/gfx/WeaponClassDescription.java @@ -0,0 +1,241 @@ +package seventh.client.gfx; + +public abstract class WeaponClassDescription { + + protected static final String THOMPSON = "Thompson | 30/180 rnds"; + protected static final String M1_GARAND = "M1 Garand | 8/40 rnds"; + protected static final String SPRINGFIELD = "Springfield | 5/35 rnds"; + protected static final String MP40 = "MP40 | 32/160 rnds"; + protected static final String MP44 = "MP44 | 30/120 rnds"; + protected static final String KAR98 = "KAR-98 | 5/25 rnds"; + protected static final String RISKER = "MG-z | 21/42 rnds"; + protected static final String SHOTGUN = "Shotgun | 5/35 rnds"; + protected static final String ROCKET_LAUNCHER = "M1 | 5 rnds"; + protected static final String FLAME_THROWER = "Flame Thrower"; + + protected static final String PISTOL = "Pistol | 9/27 rnds"; + + public String getDescription() { + String description = ""; + boolean nothingInMessage = true; + + String mainWeaponMessage = getMainWeaponMessage(); + String subWeaponMessage = getSubWeaponMessage(); + String grenadeMassege = getGrenadeMessage(); + + if (mainWeaponMessage != null) { + description += mainWeaponMessage; + nothingInMessage = false; + } + + if (subWeaponMessage != null) { + if (!nothingInMessage) + description += "\n"; + description += subWeaponMessage; + nothingInMessage = false; + } + + if (grenadeMassege != null) { + if (!nothingInMessage) + description += "\n"; + description += grenadeMassege; + nothingInMessage = false; + } + + return description; + } + + protected abstract String getMainWeaponMessage(); + protected abstract String getSubWeaponMessage(); + protected abstract String getGrenadeMessage(); +} + +class ThompsonDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return THOMPSON; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "2 Frag Grenades"; + } + +} + +class M1GarandDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return M1_GARAND; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "2 Smoke Grenades"; + } + +} + +class SpringfieldDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return SPRINGFIELD; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "1 Frag Grenades"; + } + +} + +class Mp40Description extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return MP40; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "2 Frag Grenades"; + } + +} + +class Mp44Description extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return MP44; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "2 Smoke Grenades"; + } + +} + +class Kar98Description extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return KAR98; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "1 Frag Grenade"; + } + +} + +class RiskerDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return RISKER; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return null; + } + +} + +class ShotgunDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return SHOTGUN; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return null; + } + +} + +class RocketLauncherDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return ROCKET_LAUNCHER; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "5 Frag Grenades"; + } + +} + +class FlameThrowerDescription extends WeaponClassDescription { + + @Override + protected String getMainWeaponMessage() { + return FLAME_THROWER; + } + + @Override + protected String getSubWeaponMessage() { + return PISTOL; + } + + @Override + protected String getGrenadeMessage() { + return "2 Frag Grenades"; + } + +} \ No newline at end of file diff --git a/src/seventh/game/type/obj/NonDefenderTypeException.java b/src/seventh/game/type/obj/NonDefenderTypeException.java new file mode 100644 index 0000000..31aa88d --- /dev/null +++ b/src/seventh/game/type/obj/NonDefenderTypeException.java @@ -0,0 +1,7 @@ +package seventh.game.type.obj; + +public class NonDefenderTypeException extends Exception { + public String toString(){ + return "*** ERROR -> defenders must either be a 2(for allies) or 4(for axis) or 'allies' or 'axis' values"; + } +} diff --git a/src/seventh/game/type/obj/NonLeoNativeTypeException.java b/src/seventh/game/type/obj/NonLeoNativeTypeException.java new file mode 100644 index 0000000..1a9b8d9 --- /dev/null +++ b/src/seventh/game/type/obj/NonLeoNativeTypeException.java @@ -0,0 +1,10 @@ +package seventh.game.type.obj; + +import seventh.shared.Cons; + +public class NonLeoNativeTypeException extends Exception { + public String toString(){ + return "*** ERROR -> objectives must either be an Array of objectives or a Java class or custom Leola class"; + } + +} diff --git a/src/seventh/game/type/obj/NonObjectTypeException.java b/src/seventh/game/type/obj/NonObjectTypeException.java new file mode 100644 index 0000000..1b8c699 --- /dev/null +++ b/src/seventh/game/type/obj/NonObjectTypeException.java @@ -0,0 +1,10 @@ +package seventh.game.type.obj; + +import seventh.shared.Cons; + +public class NonObjectTypeException extends Exception { + public String toString(){ + return "*** ERROR -> objectives must either be an Array of objectives or a Java class or custom Leola class"; + } + +} diff --git a/src/seventh/game/type/obj/NotExistScriptFileException.java b/src/seventh/game/type/obj/NotExistScriptFileException.java new file mode 100644 index 0000000..75527bf --- /dev/null +++ b/src/seventh/game/type/obj/NotExistScriptFileException.java @@ -0,0 +1,9 @@ +package seventh.game.type.obj; + +public class NotExistScriptFileException extends Exception{ + + public String toString() { + return "*** ERROR -> No associated script file for objective game type"; + + } +} diff --git a/src/seventh/shared/Arrays.java b/src/seventh/shared/Arrays.java index 2ce7d30..ddb68e8 100644 --- a/src/seventh/shared/Arrays.java +++ b/src/seventh/shared/Arrays.java @@ -12,7 +12,7 @@ * */ public class Arrays { - + /** * Counts the amount of used elements in the array * @@ -52,47 +52,9 @@ public static void clear(T[] array) { * @param comp * @return the supplied array */ - public static T[] sort(T[] array, Comparator comp) { - if (array == null || array.length == 0) { - return array; - } - - quicksort(array, comp, 0, array.length - 1); + public T[] sort(T[] array, Comparator comp) { + SortStrategy sortStrategy = SortStrategyFactory.getSortStrategy(array); + sortStrategy.sort(array, comp); return array; } - - private static void quicksort(T[] array, Comparator comp, int low, int high) { - int i = low; - int j = high; - - T pivot = array[low + (high - low) / 2]; - while (i <= j) { - while (comp.compare(array[i], pivot) < 0) { - i++; - } - while (comp.compare(array[j], pivot) > 0) { - j--; - } - - if (i <= j) { - swap(array, i, j); - i++; - j--; - } - } - - if (low < j) { - quicksort(array, comp, low, j); - } - - if (i < high) { - quicksort(array, comp, i, high); - } - } - - private static void swap(T[] array, int i, int j) { - T temp = array[i]; - array[i] = array[j]; - array[j] = temp; - } } diff --git a/src/seventh/shared/NoSortStrategy.java b/src/seventh/shared/NoSortStrategy.java new file mode 100644 index 0000000..ca30bda --- /dev/null +++ b/src/seventh/shared/NoSortStrategy.java @@ -0,0 +1,16 @@ +package seventh.shared; + +import java.util.Comparator; + +public class NoSortStrategy implements SortStrategy { + + /** + * do not sorting for null array + * + * @param array + * @param comp + */ + public void sort(T[] array, Comparator comp) { + return ; + } +} diff --git a/src/seventh/shared/QuickSortStrategy.java b/src/seventh/shared/QuickSortStrategy.java new file mode 100644 index 0000000..96ec188 --- /dev/null +++ b/src/seventh/shared/QuickSortStrategy.java @@ -0,0 +1,65 @@ +package seventh.shared; + +import java.util.Comparator; + +public class QuickSortStrategy implements SortStrategy { + + /** + * Sorts the supplied array by the quickSort method + * + * @param array + * @param comp + */ + public void sort(T[] array, Comparator comp) { + quickSort(array, comp, 0, array.length-1); + } + + /** + * Sorts the supplied array by the {@link Comparator} + * + * @param array + * @param comp + * @return the supplied array + */ + private static void quickSort(T[] array, Comparator comp, int low, int high) { + int i = low; + int j = high; + + T pivot = array[low + (high - low) / 2]; + while (i <= j) { + while (comp.compare(array[i], pivot) < 0) { + i++; + } + while (comp.compare(array[j], pivot) > 0) { + j--; + } + + if (i <= j) { + swap(array, i, j); + i++; + j--; + } + } + + if (low < j) { + quickSort(array, comp, low, j); + } + + if (i < high) { + quickSort(array, comp, i, high); + } + } + + /** + * Swap the elements in array + * + * @param array + * @param i + * @param j + */ + private static void swap(T[] array, int i, int j) { + T temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } +} diff --git a/src/seventh/shared/SortStrategy.java b/src/seventh/shared/SortStrategy.java new file mode 100644 index 0000000..a91a639 --- /dev/null +++ b/src/seventh/shared/SortStrategy.java @@ -0,0 +1,15 @@ +package seventh.shared; + +import java.util.Comparator; + +public interface SortStrategy { + + /** + * Sorts the supplied array by the {@link Comparator} + * + * @param array + * @param comp + * @return the supplied array + */ + public void sort(T[] array, Comparator comp); +} \ No newline at end of file diff --git a/src/seventh/shared/SortStrategyFactory.java b/src/seventh/shared/SortStrategyFactory.java new file mode 100644 index 0000000..56c4ce9 --- /dev/null +++ b/src/seventh/shared/SortStrategyFactory.java @@ -0,0 +1,17 @@ +package seventh.shared; + +public final class SortStrategyFactory { + + /** + * Return SortStrategy for the supplied array + * + * @param array + * @return SortStrategy + */ + public static SortStrategy getSortStrategy(T[] array) { + if (array == null || array.length == 0) + return new NoSortStrategy(); + else + return new QuickSortStrategy(); + } +}