1818import static bwapi .types .UnitType .*;
1919
2020public class Game {
21- final GameData gameData ;
21+ private final GameData gameData ;
2222
23- /// CONSTANT
24- final Map <Integer , Player > players = new HashMap <>();
25- final Map <Integer , Region > regions = new HashMap <>();
26- final Map <Integer , Force > forces = new HashMap <>();
23+ // CONSTANT
24+ private final Map <Integer , Player > players = new HashMap <>();
25+ private final Map <Integer , Region > regions = new HashMap <>();
26+ private final Map <Integer , Force > forces = new HashMap <>();
2727
28- //CHANGING
28+ private final Set <Unit > staticMinerals = new HashSet <>();
29+ private final Set <Unit > staticGeysers = new HashSet <>();
30+ private final Set <Unit > staticNeutralUnits = new HashSet <>();
31+
32+ // CHANGING
2933 final Map <Integer , Unit > units = new HashMap <>();
34+ final Set <Integer > visibleUnits = new HashSet <>();
3035
3136 final Map <Integer , Bullet > bullets = new HashMap <>();
3237
3338 public Game (GameData gameData ) {
3439 this .gameData = gameData ;
40+ }
41+
42+ /*
43+ Call this method in EventHander::OnMatchStart
44+ */
45+ void reset () {
46+ clear ();
3547 init ();
3648 }
3749
50+ private void clear () {
51+ players .clear ();
52+ regions .clear ();
53+ forces .clear ();
54+ staticMinerals .clear ();
55+ staticGeysers .clear ();
56+ staticNeutralUnits .clear ();
57+ units .clear ();
58+ visibleUnits .clear ();
59+ bullets .clear ();
60+ }
61+
62+ void unitShow (final int id ) {
63+ if (!units .containsKey (id )) {
64+ units .put (id , new Unit (gameData .getUnit (id ), this ));
65+ }
66+ visibleUnits .add (id );
67+ }
68+
69+ void unitHide (final int id ) {
70+ visibleUnits .remove (id );
71+ }
72+
3873 private void init () {
3974 for (int id =0 ; id < gameData .getForceCount (); id ++) {
4075 forces .put (id , new Force (gameData .getForce (id ), this ));
@@ -45,67 +80,79 @@ private void init() {
4580 for (int id =0 ; id < gameData .regionCount (); id ++) {
4681 regions .put (id , new Region (gameData .getRegion (id ), this ));
4782 }
83+
84+ for (int id =0 ; id < gameData .getInitialUnitCount (); id ++) {
85+ final Unit unit = new Unit (gameData .getUnit (id ), this );
86+
87+ units .put (id , unit );
88+
89+ if (unit .getType ().isMineralField ()) {
90+ staticMinerals .add (unit );
91+ }
92+ if (unit .getType () == Resource_Vespene_Geyser ) {
93+ staticGeysers .add (unit );
94+ }
95+ if (unit .getPlayer ().equals (neutral ())) {
96+ staticNeutralUnits .add (unit );
97+ }
98+ }
4899 }
49100
50- public Collection <Force > getForces () {
51- return forces .values ();
101+ public Set <Force > getForces () {
102+ return new HashSet <>( forces .values () );
52103 }
53104
54- public Collection <Player > getPlayers () {
55- return players .values ();
105+ public Set <Player > getPlayers () {
106+ return new HashSet <>( players .values () );
56107 }
57108
58109
59- public Collection <Unit > getAllUnits () {
60- Set < Unit > units = new HashSet <>();
61- for ( int id = 0 ; id < gameData . getInitialUnitCount (); id ++ ) {
62- units . add ( new Unit ( gameData . unit ( id ), this ));
110+ public Set <Unit > getAllUnits () {
111+ // simulate current BWAPI behavior
112+ if ( getFrameCount () == 0 ) {
113+ return new HashSet <>( units . values ( ));
63114 }
64- return units ;
65- //return units.values();
115+ return visibleUnits .stream ()
116+ .map (units ::get )
117+ .collect (Collectors .toSet ());
66118 }
67119
68- //TODO
69- public List <Unit > getMinerals () {
70- return null ;
120+ public Set <Unit > getMinerals () {
121+ return getAllUnits ().stream ()
122+ .filter (u ->u .getType ().isMineralField ())
123+ .collect (Collectors .toSet ());
71124 }
72125
73- //TODO
74- public List <Unit > getGeysers () {
75- return null ;
126+ public Set <Unit > getGeysers () {
127+ return getAllUnits ().stream ()
128+ .filter (u ->u .getType () == Resource_Vespene_Geyser )
129+ .collect (Collectors .toSet ());
76130 }
77131
78- //TODO
79- public List <Unit > getNeutralUnits () {
80- return null ;
132+ public Set <Unit > getNeutralUnits () {
133+ return getAllUnits ().stream ()
134+ .filter (u ->u .getPlayer ().equals (neutral ()))
135+ .collect (Collectors .toSet ());
81136 }
82137
83- //TODO
84- public List <Unit > getStaticMinerals () {
85- return null ;
138+ public Set <Unit > getStaticMinerals () {
139+ return new HashSet <>(staticMinerals );
86140 }
87141
88- //TODO
89- public List <Unit > getStaticGeysers () {
90- return null ;
142+ public Set <Unit > getStaticGeysers () {
143+ return new HashSet <>(staticGeysers );
91144 }
92145
93- //TODO
94- public List <Unit > getStaticNeutralUnits () {
95- return null ;
146+ public Set <Unit > getStaticNeutralUnits () {
147+ return new HashSet <>(staticNeutralUnits );
96148 }
97149
98-
99- public Collection <Bullet > getBullets () {
100- //TODO cache this in onFrame
101- final List <Bullet > bullets = new ArrayList <>();
102- for (int i =0 ; i < gameData .bulletCount (); i ++) {
103- bullets .add (new Bullet (gameData .bullet (i ), this ));
104- }
105- return bullets ;
150+ public Set <Bullet > getBullets () {
151+ // TODO
152+ // for (int i=0; i < gameData.bulletCount(); i++) {bullets.add(new Bullet(gameData.bullet(i), this))};
153+ return null ;
106154 }
107155
108-
109156 public Set <Position > getNukeDots () {
110157 return IntStream .range (0 , gameData .nukeDotCount ())
111158 .mapToObj (id -> new Position (gameData .getNukeDotX (id ), gameData .getNukeDotY ((id ))))
@@ -365,7 +412,7 @@ public boolean canBuildHere(final TilePosition position, final UnitType type, fi
365412 return false ;
366413 }
367414
368- //if the unit is a refinery, we just need to check the set of geysers to see if the position
415+ //if the getUnit is a refinery, we just need to check the set of geysers to see if the position
369416 //matches one of them (and the type is still vespene geyser)
370417 if ( type .isRefinery () ) {
371418 for (final Unit g : getGeysers ()) {
@@ -400,7 +447,7 @@ else if (!builder.getType().isFlyingBuilding() && type != Zerg_Nydus_Canal && !t
400447 }
401448 }
402449
403- // Ground unit dimension check
450+ // Ground getUnit dimension check
404451 if (type != Special_Start_Location ) {
405452 final Position targPos = lt .toPosition ().add (type .tileSize ().toPosition ().divide (2 ));
406453 Set <Unit > unitsInRect = getUnitsInRectangle (lt .toPosition (), rb .toPosition (),
0 commit comments