View Javadoc
1 package org.argosfields.battlefield; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.Map; 7 8 import org.argosfields.model.IMobile; 9 import org.argosfields.model.Tile; 10 import org.argosfields.model.Unit; 11 import org.eclipse.swt.SWT; 12 13 /*** 14 * Simulator.java 15 * 16 * @author Xavier Cho 17 * @version $Revision: 1.4 $ $Date: 2003/12/01 07:38:05 $ 18 */ 19 public class Simulator { 20 private BattleField battleField; 21 private Map tiles; 22 private Unit unit; 23 private int[] reachableIndexes; 24 25 public Simulator(final BattleField battleField, final int index) { 26 27 if (battleField == null) { 28 SWT.error(SWT.ERROR_NULL_ARGUMENT); 29 } 30 31 this.battleField = battleField; 32 this.unit = battleField.getUnit(index); 33 34 if (unit == null) { 35 SWT.error(SWT.ERROR_INVALID_ARGUMENT); 36 } 37 38 int movesPerTurn = 0; 39 40 if (unit instanceof IMobile) { 41 movesPerTurn = ((IMobile) unit).getMovesPerTurn(); 42 } 43 44 Moves moves = new Moves(index, movesPerTurn); 45 46 tiles = new HashMap(80); 47 tiles.put(new Integer(index), moves); 48 49 moveAround(index, movesPerTurn); 50 } 51 52 private void moveAround(final int index, final int movesRemaining) { 53 int[] indexes = battleField.getNeighboringIndexes(index); 54 int length = indexes.length; 55 56 Moves moves = (Moves) tiles.get(new Integer(index)); 57 58 for (int i = 0; i < length; i++) { 59 Integer idx = new Integer(indexes[i]); 60 61 Tile tile = battleField.getTile(idx.intValue()); 62 63 if (!unit.canTraverse(tile)) { 64 continue; 65 } 66 67 int cost = tile.getMovementCost(); 68 69 if (movesRemaining >= cost) { 70 int turns = movesRemaining - cost; 71 72 Moves previousMove = (Moves) tiles.get(idx); 73 if (previousMove == null 74 || turns > previousMove.getMovesRemaining()) { 75 76 Moves newMoves = (Moves) moves.clone(); 77 newMoves.move(idx.intValue(), cost); 78 tiles.put(idx, newMoves); 79 if (turns > 0) { 80 moveAround(idx.intValue(), turns); 81 } 82 } 83 } 84 } 85 } 86 87 public int[] getReachableTileIndexes() { 88 this.reachableIndexes = new int[tiles.size()]; 89 int i = 0; 90 Iterator it = tiles.keySet().iterator(); 91 while (it.hasNext()) { 92 reachableIndexes[i++] = ((Integer) it.next()).intValue(); 93 } 94 95 return reachableIndexes; 96 } 97 98 public int[] getShortestPath(final int index) { 99 int[] path = null; 100 101 Moves moves = (Moves) tiles.get(new Integer(index)); 102 if (moves != null) { 103 path = moves.getPath(); 104 } 105 106 return path; 107 } 108 109 private final class Moves implements Cloneable { 110 private ArrayList path; 111 private int origin; 112 private int movesRemaining; 113 114 private Moves(final int origin, final int movesRemaining) { 115 this.path = new ArrayList(30); 116 this.origin = origin; 117 this.movesRemaining = movesRemaining; 118 119 path.add(new Integer(origin)); 120 } 121 122 protected int getOrigin() { 123 return origin; 124 } 125 126 protected int getMovesRemaining() { 127 return movesRemaining; 128 } 129 130 protected int[] getPath() { 131 int size = path.size(); 132 int[] indexes = new int[size]; 133 for (int i = 0; i < size; i++) { 134 indexes[i] = ((Integer) path.get(i)).intValue(); 135 } 136 137 return indexes; 138 } 139 140 protected void move(final int index, final int cost) { 141 path.add(new Integer(index)); 142 movesRemaining -= cost; 143 } 144 145 /*** 146 * @see java.lang.Object#clone() 147 */ 148 protected Object clone() { 149 Moves moves = new Moves(origin, movesRemaining); 150 moves.path = (ArrayList) path.clone(); 151 return moves; 152 } 153 } 154 }

This page was automatically generated by Maven