@@ -33,6 +33,27 @@ public class Game {
3333 private final Map <Integer , Unit > units = new HashMap <>();
3434 private final Set <Integer > visibleUnits = new HashSet <>();
3535
36+ //CACHED
37+ private int randomSeed ;
38+ private int revision ;
39+ private boolean debug ;
40+ private Player self ;
41+ private Player enemy ;
42+ private Player neutral ;
43+ private boolean replay ;
44+ private boolean multiplayer ;
45+ private boolean battleNet ;
46+ private List <TilePosition > startLocations ;
47+ private int mapWidth ;
48+ private int mapHeight ;
49+ private String mapFileName ;
50+ private String mapPathName ;
51+ private String mapName ;
52+ private String mapHash ;
53+
54+ private boolean [][] buildable ;
55+ private boolean [][] walkable ;
56+
3657 // USER DEFINED
3758 private TextSize textSize = TextSize .Default ;
3859
@@ -93,6 +114,38 @@ private void init() {
93114 staticNeutralUnits .add (unit );
94115 }
95116 }
117+
118+ randomSeed = gameData .randomSeed ();
119+ revision = gameData .getRevision ();
120+ debug = gameData .isDebug ();
121+ self = players .get (gameData .self ());
122+ enemy = players .get (gameData .enemy ());
123+ neutral = players .get (gameData .neutral ());
124+ replay = gameData .isReplay ();
125+ multiplayer = gameData .isMultiplayer ();
126+ battleNet = gameData .isBattleNet ();
127+ startLocations = IntStream .range (0 , gameData .startLocationCount ())
128+ .mapToObj (i -> new TilePosition (gameData .startLocationX (i ), gameData .startLocationY (i )))
129+ .collect (Collectors .toList ());
130+ mapWidth = gameData .mapWidth ();
131+ mapHeight = gameData .mapHeight ();
132+ mapFileName = gameData .mapFileName ();
133+ mapPathName = gameData .mapPathName ();
134+ mapName = gameData .mapName ();
135+ mapHash = gameData .mapHash ();
136+
137+ buildable = new boolean [mapWidth ][mapHeight ];
138+ for (int i =0 ; i < mapWidth ; i ++) {
139+ for (int j =0 ; j < mapHeight ; j ++) {
140+ buildable [i ][j ] = gameData .buildable (i , j );
141+ }
142+ }
143+ walkable = new boolean [mapWidth *4 ][mapHeight *4 ];
144+ for (int i = 0 ; i < mapWidth * 4 ; i ++) {
145+ for (int j =0 ; j < mapHeight * 4 ; j ++) {
146+ walkable [i ][j ] = gameData .walkable (i , j );
147+ }
148+ }
96149 }
97150
98151 void unitShow (final int id ) {
@@ -282,14 +335,11 @@ public Set<Unit> getUnitsInRectangle(final Position leftTop, final Position righ
282335 }
283336
284337 public Set <Unit > getUnitsInRadius (final int x , final int y , final int radius , final UnitFilter filter ) {
285- return new HashSet <>( );
338+ return getUnitsInRadius ( new Position ( x , y ), radius , filter );
286339 }
287340
288341 public Set <Unit > getUnitsInRadius (final Position center , final int radius , final UnitFilter filter ) {
289- return getAllUnits ().stream ().filter ( u -> {
290- final Position p = u .getPosition ();
291- return center .getApproxDistance (u .getPosition ()) <= radius && filter .operation (u );
292- }).collect (Collectors .toSet ());
342+ return getAllUnits ().stream ().filter ( u -> center .getApproxDistance (u .getPosition ()) <= radius && filter .operation (u )).collect (Collectors .toSet ());
293343 }
294344
295345 public Unit getClosestUnitInRectangle (final Position center , final int left , final int top , final int right , final int bottom , final UnitFilter filter ) {
@@ -303,27 +353,27 @@ public Unit getClosestUnitInRadius(final Position center, final int radius, fina
303353 }
304354
305355 public int mapWidth () {
306- return gameData . mapWidth () ;
356+ return mapWidth ;
307357 }
308358
309359 public int mapHeight () {
310- return gameData . mapHeight () ;
360+ return mapHeight ;
311361 }
312362
313363 public String mapFileName () {
314- return gameData . mapFileName () ;
364+ return mapFileName ;
315365 }
316366
317367 public String mapPathName () {
318- return gameData . mapPathName () ;
368+ return mapPathName ;
319369 }
320370
321371 public String mapName () {
322- return gameData . mapName () ;
372+ return mapName ;
323373 }
324374
325375 public String mapHash () {
326- return gameData . mapHash () ;
376+ return mapHash ;
327377 }
328378
329379 public boolean isWalkable (final int walkX , final int walkY ) {
@@ -334,7 +384,7 @@ public boolean isWalkable(final WalkPosition position) {
334384 if (!position .isValid (this )) {
335385 return false ;
336386 }
337- return gameData . walkable ( position .x , position .y ) ;
387+ return walkable [ position .x ][ position .y ] ;
338388 }
339389
340390 public int getGroundHeight (final int tileX , final int tileY ) {
@@ -364,7 +414,7 @@ public boolean isBuildable(final TilePosition position, final boolean includeBui
364414 if (!position .isValid (this )) {
365415 return false ;
366416 }
367- return gameData . buildable ( position .x , position .y ) && ( includeBuildings ? !gameData .occupied (position .x , position .y ) : true );
417+ return buildable [ position .x ][ position .y ] && ( includeBuildings ? !gameData .occupied (position .x , position .y ) : true );
368418 }
369419
370420 public boolean isVisible (final int tileX , final int tileY ) {
@@ -839,9 +889,7 @@ public boolean canUpgrade(final UpgradeType type, final Unit unit, final boolean
839889 }
840890
841891 public List <TilePosition > getStartLocations () {
842- return IntStream .range (0 , gameData .startLocationCount ())
843- .mapToObj (i -> new TilePosition (gameData .startLocationX (i ), gameData .startLocationY (i )))
844- .collect (Collectors .toList ());
892+ return new ArrayList <>(startLocations );
845893 }
846894
847895
@@ -862,19 +910,19 @@ public boolean isInGame() {
862910 }
863911
864912 public boolean isMultiplayer () {
865- return gameData . isMultiplayer () ;
913+ return multiplayer ;
866914 }
867915
868916 public boolean isBattleNet () {
869- return gameData . isBattleNet () ;
917+ return battleNet ;
870918 }
871919
872920 public boolean isPaused () {
873921 return gameData .isPaused ();
874922 }
875923
876924 public boolean isReplay () {
877- return gameData . isReplay () ;
925+ return replay ;
878926 }
879927
880928 public void pauseGame () {
@@ -911,16 +959,16 @@ public Set<Unit> getSelectedUnits() {
911959 }
912960
913961 public Player self () {
914- return players . get ( gameData . self ()) ;
962+ return self ;
915963 }
916964
917965
918966 public Player enemy () {
919- return players . get ( gameData . enemy ()) ;
967+ return enemy ;
920968 }
921969
922970 public Player neutral () {
923- return players . get ( gameData . neutral ()) ;
971+ return neutral ;
924972 }
925973
926974
@@ -1279,11 +1327,11 @@ public int getRemainingLatencyTime() {
12791327 }
12801328
12811329 public int getRevision () {
1282- return gameData . getRevision () ;
1330+ return revision ;
12831331 }
12841332
12851333 public boolean isDebug () {
1286- return gameData . isDebug () ;
1334+ return debug ;
12871335 }
12881336
12891337 public boolean isLatComEnabled () {
@@ -1429,6 +1477,6 @@ public int getDamageTo(final UnitType toType, final UnitType fromType, final Pla
14291477
14301478 //Since 4.2.0
14311479 public int getRandomSeed () {
1432- return gameData . randomSeed () ;
1480+ return randomSeed ;
14331481 }
14341482}
0 commit comments