Skip to content

Commit 35734be

Browse files
committed
game Commands
1 parent 79be1da commit 35734be

File tree

8 files changed

+192
-114
lines changed

8 files changed

+192
-114
lines changed

src/main/java/JavaBWAPIBackend/Client.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,25 @@ public class Client {
1919
private LittleEndianPipe pipe;
2020

2121
public static class UnitCommand {
22-
public int type, unit, target, x, y, extra;
22+
public UnitCommand(int type, int unit, int target, int x, int y, int extra) {
23+
this.type = type;
24+
this.unit = unit;
25+
this.target = target;
26+
this.x = x;
27+
this.y = y;
28+
this.extra = extra;
29+
}
30+
int type, unit, target, x, y, extra;
2331
private static final int SIZE = 24;
2432
}
2533

2634
public static class Command {
27-
public int type, value1, value2;
35+
public Command(int type, int value1, int value2) {
36+
this.type = type;
37+
this.value1 = value1;
38+
this.value2 = value2;
39+
}
40+
int type, value1, value2;
2841
private static final int SIZE = 12;
2942
}
3043

src/main/java/bwapi/Force.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public String getName() {
2424

2525
public Set<Player> getPlayers() {
2626
return game.getPlayers().stream()
27-
.filter(p -> this.equals(p.getForce()))
27+
.filter(p -> equals(p.getForce()))
2828
.collect(Collectors.toSet());
2929
}
3030

src/main/java/bwapi/Game.java

Lines changed: 95 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
import bwapi.point.Position;
66
import bwapi.point.TilePosition;
77
import bwapi.point.WalkPosition;
8-
import bwapi.types.GameType;
9-
import bwapi.types.TechType;
10-
import bwapi.types.UnitType;
11-
import bwapi.types.UpgradeType;
8+
import bwapi.types.*;
129
import bwapi.values.*;
1310

1411
import java.util.*;
1512
import java.util.stream.Collectors;
1613
import java.util.stream.IntStream;
1714

15+
import static bwapi.types.CommandType.*;
1816
import static bwapi.types.Race.Zerg;
1917
import static bwapi.types.UnitType.*;
2018

@@ -30,7 +28,7 @@ public class Game {
3028
private final Set<Unit> staticGeysers = new HashSet<>();
3129
private final Set<Unit> staticNeutralUnits = new HashSet<>();
3230

33-
// CHANGING
31+
// CHANGING
3432
final Map<Integer, Unit> units = new HashMap<>();
3533
final Set<Integer> visibleUnits = new HashSet<>();
3634

@@ -71,12 +69,12 @@ void unitHide(final int id) {
7169
visibleUnits.remove(id);
7270
}
7371

74-
void addUnitCommand(final Client.UnitCommand unitCommand) {
75-
gameData.addUnitCommand(unitCommand);
72+
void unitCommand(final int type, final int unit, final int target, final int x, final int y, final int extra) {
73+
gameData.addUnitCommand(new Client.UnitCommand(type, unit, target, x, y, extra));
7674
}
7775

78-
void addCommand(final Client.Command command) {
79-
gameData.addCommand(command);
76+
void command(final int type, final int value1, final int value2) {
77+
gameData.addCommand(new Client.Command(type, value1, value2));
8078
}
8179

8280
private void init() {
@@ -90,6 +88,8 @@ private void init() {
9088
regions.put(id, new Region(gameData.getRegion(id), this));
9189
}
9290

91+
regions.values().forEach(Region::updateNeighbours);
92+
9393
for (int id=0; id < gameData.getInitialUnitCount(); id++) {
9494
final Unit unit = new Unit(gameData.getUnit(id), this);
9595

@@ -226,13 +226,21 @@ public Position getScreenPosition() {
226226
}
227227

228228
//TODO
229-
//public void setScreenPosition(final int x, final int y);
229+
public void setScreenPosition(final int x, final int y) {
230+
command(SetScreenPosition.value, x, y);
231+
}
230232

231-
//public void setScreenPosition(final Position p);
233+
public void setScreenPosition(final Position p) {
234+
setScreenPosition(p.x, p.y);
235+
}
232236

233-
//public void pingMinimap(final int x, final int y);
237+
public void pingMinimap(final int x, final int y) {
238+
command(PingMinimap.value, x, y);
239+
}
234240

235-
//public void pingMinimap(final Position p);
241+
public void pingMinimap(final Position p) {
242+
pingMinimap(p.x, p.y);
243+
}
236244

237245
public boolean isFlagEnabled(final Flag flag) {
238246
return gameData.getFlag(flag.value);
@@ -287,6 +295,7 @@ public String mapHash() {
287295

288296
public boolean isWalkable(final int walkX, final int walkY) {
289297
//TODO bounds check
298+
290299
return gameData.walkable(walkX, walkY);
291300
}
292301

@@ -569,13 +578,18 @@ public List<TilePosition> getStartLocations() {
569578
.collect(Collectors.toList());
570579
}
571580

572-
/*
573-
public void printf(final String cstr_format);
574581

575-
public void sendText(final String cstr_format);
582+
public void printf(final String cstr_format) {
583+
command(Printf.value, gameData.addString(cstr_format), 0);
584+
}
576585

577-
public void sendTextEx(final boolean toAllies, final String cstr_format);
578-
*/
586+
public void sendText(final String cstr_format) {
587+
command(SendText.value, gameData.addString(cstr_format), 0);
588+
}
589+
590+
public void sendTextEx(final boolean toAllies, final String cstr_format) {
591+
command(SendText.value, gameData.addString(cstr_format), toAllies ? 1 : 0);
592+
}
579593

580594
public boolean isInGame() {
581595
return gameData.isInGame();
@@ -597,23 +611,37 @@ public boolean isReplay() {
597611
return gameData.isReplay();
598612
}
599613

600-
//TODO
601-
// public void pauseGame();
602-
//
603-
// public void resumeGame();
604-
//
605-
// public void leaveGame();
606-
//
607-
// public void restartGame();
608-
//
609-
// public void setLocalSpeed(int speed);
610-
//
611-
// public boolean issueCommand(final Collection<Unit> units, final UnitCommand command) {
612-
// units.forEach(u -> u.issueCommand(command));
613-
// }
614+
public void pauseGame() {
615+
command(PauseGame.value, 0, 0);
616+
}
617+
618+
public void resumeGame() {
619+
command(ResumeGame.value, 0, 0);
620+
}
621+
622+
public void leaveGame() {
623+
command(LeaveGame.value, 0, 0);
624+
}
625+
626+
public void restartGame() {
627+
command(RestartGame.value, 0, 0);
628+
}
629+
630+
public void setLocalSpeed(final int speed) {
631+
command(SetLocalSpeed.value, speed, 0);
632+
}
633+
634+
public boolean issueCommand(final Collection<Unit> units, final UnitCommand command) {
635+
return ! units.stream()
636+
.map(u -> u.issueCommand(command))
637+
.collect(Collectors.toList())
638+
.contains(false);
639+
}
614640

615641
public Set<Unit> getSelectedUnits() {
616-
return getAllUnits().stream().filter(Unit::isSelected).collect(Collectors.toSet());
642+
return IntStream.range(0, gameData.selectedUnitCount())
643+
.mapToObj(i -> units.get(gameData.selectedUnit(i)))
644+
.collect(Collectors.toSet());
617645
}
618646

619647
public Player self() {
@@ -650,15 +678,6 @@ public Set<Player> observers() {
650678
.collect(Collectors.toSet());
651679
}
652680

653-
654-
public void setTextSize() {
655-
setTextSize(TextSize.Default);
656-
}
657-
658-
public void setTextSize(final TextSize size) {
659-
//TODO
660-
}
661-
662681
/*
663682
public void drawText(Coordinate ctype, int x, int y, String cstr_format);
664683
@@ -854,38 +873,46 @@ public int getAPM(final boolean includeSelects) {
854873
return includeSelects ? gameData.getBotAPM_selects() : gameData.getBotAPM_noselects();
855874
}
856875

857-
//TODO
858-
//public boolean setMap(final String cstr_mapFileName);
876+
//please just use a valid map here, not going to add checks for this to the java client
877+
public boolean setMap(final String cstr_mapFileName) {
878+
command(SetMap.value, gameData.addString(cstr_mapFileName), 0);
879+
return true;
880+
}
881+
882+
public void setFrameSkip(int frameSkip) {
883+
command(SetFrameSkip.value, frameSkip, 0);
884+
}
859885

860-
//public void setFrameSkip(int frameSkip)
861886

862887
//TODO
863888
public boolean hasPath(Position source, Position destination) {
864889
return false;
865890
}
866891

867-
// public boolean setAlliance(Player player, boolean allied);
868-
//
869-
// public boolean setAlliance(Player player);
870-
//
871-
// public boolean setAlliance(Player player, boolean allied, boolean alliedVictory);
872-
//
873-
// public boolean setVision(Player player);
874-
//
875-
// public boolean setVision(Player player, boolean enabled);
892+
// If you need these please implement (see command and make a PR to the github repo)
893+
// public boolean setAlliance(Player player, boolean allied);
894+
// public boolean setAlliance(Player player);
895+
// public boolean setAlliance(Player player, boolean allied, boolean alliedVictory);
896+
// public boolean setVision(Player player, boolean enabled);
897+
// public void setGUI(bool enabled);
898+
// public int getLastEventTime();
899+
// public void setTextSize();
900+
// public void setTextSize(final TextSize size);
876901

877902
public int elapsedTime() {
878903
return gameData.elapsedTime();
879904
}
880905

881-
//public void setCommandOptimizationLevel(int level)
906+
public void setCommandOptimizationLevel(final int level) {
907+
command(SetCommandOptimizerLevel.value, level, 0);
908+
}
882909

883910
public int countdownTimer() {
884911
return gameData.countdownTimer();
885912
}
886913

887-
public Collection<Region> getAllRegions() {
888-
return regions.values();
914+
public Set<Region> getAllRegions() {
915+
return new HashSet<>(regions.values());
889916
}
890917

891918
//TODO
@@ -898,13 +925,20 @@ public Region getRegionAt(final Position position) {
898925
return null;
899926
}
900927

901-
/*
902-
public int getLastEventTime();
903928

904-
public boolean setRevealAll();
905929

906-
public boolean setRevealAll(boolean reveal);
930+
public boolean setRevealAll() {
931+
setRevealAll(false);
932+
return true;
933+
}
934+
935+
public boolean setRevealAll(boolean reveal) {
936+
command(SetRevealAll.value, reveal ? 1 : 0, 0);
937+
return true;
938+
}
939+
907940

941+
/*
908942
public TilePosition getBuildLocation(UnitType type, TilePosition desiredPosition, int maxRange);
909943
910944
public TilePosition getBuildLocation(UnitType type, TilePosition desiredPosition);

src/main/java/bwapi/Player.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String getName() {
3333

3434
public Set<Unit> getUnits() {
3535
return game.getAllUnits().stream()
36-
.filter(u -> this.equals(u.getPlayer()))
36+
.filter(u -> equals(u.getPlayer()))
3737
.collect(Collectors.toSet());
3838
}
3939

0 commit comments

Comments
 (0)