Skip to content

Commit 8e75a18

Browse files
author
Bytekeeper
committed
New utilities: Reservation-Implementations for GMS and "things" (ie. TilePositions)
1 parent 3b34cfa commit 8e75a18

File tree

4 files changed

+155
-2
lines changed

4 files changed

+155
-2
lines changed

src/main/java/org/bk/ass/manage/GMS.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import bwapi.TechType;
77
import bwapi.UnitType;
88
import bwapi.UpgradeType;
9+
import java.util.Objects;
910

1011
/** Representing gas + minerals + supply */
1112
public final class GMS {
@@ -56,11 +57,15 @@ public GMS add(GMS gms) {
5657
return new GMS(gas + gms.gas, minerals + gms.minerals, supply + gms.supply);
5758
}
5859

60+
public GMS multiply(int factor) {
61+
return new GMS(gas * factor, minerals * factor, supply * factor);
62+
}
63+
5964
/**
6065
* If all components (gas, minerals, supply) of this value are non-negative, returns true if all
6166
* are >= that of the given value. If a component is negative, returns false if the given
62-
* value's component is &gt;= 0. <br>
63-
* Usually all components are &gt;= 0 and this can be used to check if a price could be payed
67+
* value's component is &gt;= 0. <br> Usually all components are &gt;= 0 and this can be used to
68+
* check if a price could be payed
6469
* <em>now</em>. <br>
6570
* If planning ahead is used, components could already be negative. But if the cost (ie. supply)
6671
* is 0, it might still be possible to purchase it immediately.
@@ -75,4 +80,23 @@ && max(0, minerals) >= gms.minerals
7580
public String toString() {
7681
return "gas: " + gas + ", minerals: " + minerals + ", supply: " + supply;
7782
}
83+
84+
@Override
85+
public boolean equals(Object o) {
86+
if (this == o) {
87+
return true;
88+
}
89+
if (o == null || getClass() != o.getClass()) {
90+
return false;
91+
}
92+
GMS gms = (GMS) o;
93+
return gas == gms.gas &&
94+
minerals == gms.minerals &&
95+
supply == gms.supply;
96+
}
97+
98+
@Override
99+
public int hashCode() {
100+
return Objects.hash(gas, minerals, supply);
101+
}
78102
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.bk.ass.manage;
2+
3+
import java.util.function.IntFunction;
4+
5+
/**
6+
* {@link Reservation} implementation for {@link GMS}. Can be used for tracking the remaining
7+
* available resources. (Ie. start each frame by {@link #setGms(GMS)} using the current amount of
8+
* resources, then use {@link GMS-}{@link Lock}s to track reservations.)
9+
*/
10+
public class GMSReservation implements Reservation<GMS> {
11+
12+
private GMS gms = GMS.ZERO;
13+
private final IntFunction<GMS> gmsPrediction;
14+
15+
public GMSReservation(
16+
IntFunction<GMS> gmsPrediction) {
17+
this.gmsPrediction = gmsPrediction;
18+
}
19+
20+
public GMSReservation() {
21+
this(unused -> GMS.ZERO);
22+
}
23+
24+
public void setGms(GMS gms) {
25+
this.gms = gms;
26+
}
27+
28+
public GMS getGms() {
29+
return gms;
30+
}
31+
32+
@Override
33+
public boolean reserve(Lock<GMS> lock, GMS gmsToReserve) {
34+
boolean success = gms.canAfford(gmsToReserve);
35+
gms = gms.subtract(gmsToReserve);
36+
return success;
37+
}
38+
39+
@Override
40+
public boolean itemAvailableInFuture(Lock<GMS> lock, GMS futureItem, int futureFrames) {
41+
GMS futureGMS = gmsPrediction.apply(futureFrames).add(gms);
42+
return futureGMS.canAfford(futureItem);
43+
}
44+
45+
@Override
46+
public void release(Lock<GMS> lock, GMS gmsToRelease) {
47+
gms = gms.add(gmsToRelease);
48+
}
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.bk.ass.manage;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
* Simple {@link Reservation} that allows only one item of the given type to be reserved at a time.
8+
*
9+
* @param <T>
10+
*/
11+
public class SetReservation<T> implements Reservation<T> {
12+
13+
private final Set<T> reservedItems = new HashSet<>();
14+
15+
public void setReservedItems(Set<T> reservedItems) {
16+
this.reservedItems.clear();
17+
this.reservedItems.addAll(reservedItems);
18+
}
19+
20+
@Override
21+
public boolean reserve(Lock<T> lock, T item) {
22+
return reservedItems.add(item);
23+
}
24+
25+
@Override
26+
public void release(Lock<T> lock, T item) {
27+
reservedItems.remove(item);
28+
}
29+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.bk.ass.manage;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class GMSReservationTest {
8+
9+
@Test
10+
void shouldReserveSuccessfullyIfAffordable() {
11+
// GIVEN
12+
GMSReservation sut = new GMSReservation();
13+
sut.setGms(new GMS(1, -1, 0));
14+
15+
// WHEN
16+
boolean reserved = sut.reserve(null, new GMS(1, 0, 0));
17+
18+
// THEN
19+
assertThat(reserved).isTrue();
20+
assertThat(sut.getGms()).isEqualTo(new GMS(0, -1, 0));
21+
}
22+
23+
@Test
24+
void shouldBlockResourcesIfNotAffordable() {
25+
// GIVEN
26+
GMSReservation sut = new GMSReservation();
27+
sut.setGms(new GMS(-1, -1, 0));
28+
29+
// WHEN
30+
boolean reserved = sut.reserve(null, new GMS(1, 1, 0));
31+
32+
// THEN
33+
assertThat(reserved).isFalse();
34+
assertThat(sut.getGms()).isEqualTo(new GMS(-2, -2, 0));
35+
}
36+
37+
@Test
38+
void shouldBeAvailableInFuture() {
39+
// GIVEN
40+
GMSReservation sut = new GMSReservation(frames -> new GMS(1, 1, 1).multiply(frames));
41+
GMS initialGMS = new GMS(-10, -10, -10);
42+
sut.setGms(initialGMS);
43+
44+
// WHEN
45+
boolean reserved = sut.itemAvailableInFuture(null, new GMS(1, 1, 1), 11);
46+
47+
// THEN
48+
assertThat(reserved).isTrue();
49+
assertThat(sut.getGms()).isEqualTo(initialGMS);
50+
}
51+
}

0 commit comments

Comments
 (0)