View Javadoc

1   package cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.basic;
2   
3   import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
4   import cz.cuni.amis.pogamut.defcon.agent.module.sensor.GameInfo;
5   import cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.IGridCell;
6   import cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.IGridCellId;
7   import cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.flags.BasicFlag;
8   import cz.cuni.amis.pogamut.defcon.consts.UnitType;
9   
10  /**
11   * A single square cell for the experimental grid.
12   * 
13   * @author Radek 'Black_Hand' Pibil
14   * 
15   */
16  public class SymmetricGridCell implements IGridCell {
17  
18  	// protected LinkedList<IGridCellFlag> complexFlags = new
19  	// LinkedList<IGridCellFlag>();
20  
21  	// protected static BasicGrid grid;
22  
23  	protected SymmetricGridCellId cellId;
24  
25  	/*
26  	 * public static void setGrid(BasicGrid grid) { SymmetricGridCell.grid =
27  	 * grid; }
28  	 */
29  
30  	protected static GameInfo gameInfo;
31  
32  	public static void setGameInfo(GameInfo gameInfo) {
33  		SymmetricGridCell.gameInfo = gameInfo;
34  	}
35  
36  	public SymmetricGridCell(SymmetricGridCellId cellId) {
37  		this.cellId = cellId;
38  	}
39  
40  	public SymmetricGridCell(float x, float y) {
41  		cellId = new SymmetricGridCellId(x, y);
42  	}
43  
44  	/*
45  	 * @Override public void setFlag(IGridCellFlag flag) {
46  	 * complexFlags.add(flag); }
47  	 * 
48  	 * @Override public boolean hasFlag(Class<? extends IGridCellFlag> flag) {
49  	 * return getFlag(flag) != null; }
50  	 * 
51  	 * @Override public boolean hasFlag(IGridCellFlag flag) { return
52  	 * complexFlags.contains(flag); }
53  	 */
54  	@Override
55  	public boolean hasFlag(BasicFlag flag) {
56  		return hasFlag(cellId.getX(), cellId.getY(), flag);
57  	}
58  
59  	public static boolean hasFlag(Location location, BasicFlag flag) {
60  		return location != null
61  				&& hasFlag(
62  						(float) location.getX(),
63  						(float) location.getY(),
64  						flag);
65  	}
66  
67  	public static boolean hasFlag(float x, float y, BasicFlag flag) {
68  
69  		if (x < -180f || x > 180f || y < -90f || y > 90f)
70  			return false;
71  
72  		switch (flag) {
73  			case SEA:
74  				return gameInfo.isValidTerritory(-1, x, y, true);
75  			case LAND:
76  				return gameInfo.isValidTerritory(-1, x, y, false);
77  			case OWN_TERRITORY: {
78  				int id = gameInfo.getOwnTeamId();
79  				return gameInfo.isValidTerritory(id, x, y, false)
80  						|| gameInfo.isValidTerritory(id, x, y, true);
81  			}
82  			case ENEMY_TERRITORY: {
83  				Boolean ok = false;
84  				for (int id : gameInfo.getEnemyTeamIds()) {
85  					if (ok = gameInfo.isValidTerritory(id, x, y, false) ||
86  							gameInfo.isValidTerritory(id, x, y, true))
87  						break;
88  				}
89  				return ok;
90  			}
91  			case OWN_PLACEABLE_LAND: {
92  				return gameInfo.isValidPlacementLocation(x, y, UnitType.RADAR);
93  			}
94  			case OWN_PLACEABLE_SEA:
95  				return gameInfo.isValidPlacementLocation(
96  						x, y, UnitType.BATTLE_SHIP);
97  		}
98  		return false;
99  	}
100 
101 	/*
102 	 * @SuppressWarnings("unchecked")
103 	 * 
104 	 * @Override public <T extends IGridCellFlag> T getFlag(Class<? extends T>
105 	 * flag) { for (IGridCellFlag set_flag : complexFlags) { if (flag ==
106 	 * set_flag.getClass()) return (T) set_flag; } return null; }
107 	 *//*
108 		 * @Override public boolean removeFlag(IGridCellFlag flag) { return
109 		 * complexFlags.remove(flag); }
110 		 */
111 	@Override
112 	public IGridCellId getCellId() {
113 		return cellId;
114 	}
115 	/*
116 	 * public CellIndices getCellIndices() { return
117 	 * grid.getCellIndices((SymmetricGridCellId) getCellId()); }
118 	 */
119 }