build and gather

This commit is contained in:
Guillem George 2026-02-07 16:25:00 +01:00
parent 58f5f4da8b
commit d62bd8410a
5 changed files with 49 additions and 35 deletions

View file

@ -16,11 +16,15 @@ import java.util.function.Supplier;
@NoArgsConstructor
public class AsyncExec<T> {
@Getter
@Setter
private static float ticksPerSecond = 1;
// private CompletableFuture<T> task;
public static void setTicksPerSecond(float ticksPerSecond) {
AsyncExec.ticksPerSecond = ticksPerSecond;
}
public static long ticksToTime(long ticks) {
return Math.ceilDiv(ticks, (long)Math.ceil(ticksPerSecond));
}

View file

@ -4,6 +4,7 @@ import com.epita.creeps.commands.Basics;
import com.epita.creeps.given.vo.geometry.Direction;
import com.epita.creeps.given.vo.response.CommandResponse;
import com.epita.creeps.given.vo.response.InitResponse;
import com.epita.creeps.units.Building;
import com.epita.creeps.units.Citizen;
import kong.unirest.core.HttpResponse;
import kong.unirest.core.JsonNode;
@ -60,32 +61,24 @@ public class Program {
InitResponse initResponse = Basics.connectAccount(login);
login = initResponse.login; // Just in case
AsyncExec.setTicksPerSecond((long)initResponse.setup.ticksPerSeconds);
AsyncExec.setTicksPerSecond((float) initResponse.setup.ticksPerSeconds);
Citizen citizen1 = new Citizen(login, initResponse.citizen1Id);
Citizen citizen2 = new Citizen(login, initResponse.citizen2Id);
for(int i = 0; i < 3; i++) {
citizen1.move(Direction.RIGHT);
citizen2.move(Direction.UP);
citizen1.move(Direction.UP).waitFinished();
while (true) {
citizen1.move(Direction.UP);
citizen2.move(Direction.RIGHT);
citizen1.waitFinished();
citizen2.waitFinished();
// logger.debug("Received actions responses");
// logger.debug("citizen1.error: " + citizen1_resp.error);
// logger.debug("citizen2.error: " + citizen2_resp.error);
}
for(int i = 0; i < 3; i++) {
citizen1.move(Direction.LEFT);
citizen2.move(Direction.DOWN);
citizen1.build(Building.ROAD);
citizen2.gather();
citizen1.waitFinished();
citizen2.waitFinished();
// logger.debug("Received actions responses");
// logger.debug("citizen1.error: " + citizen1_resp.error);
// logger.debug("citizen2.error: " + citizen2_resp.error);
}

View file

@ -23,9 +23,12 @@ import java.util.concurrent.CompletableFuture;
public class Basics {
private static final Logger logger = LoggerFactory.getLogger(Basics.class);
@Getter @Setter
private static String srvUrl;
public static void setSrvUrl(String srvUrl) {
Basics.srvUrl = srvUrl;
}
public static InitResponse connectAccount(String login) {
try {
CompletableFuture<HttpResponse<JsonNode>> resp = AsyncExec.asyncExec(() -> Unirest.post(srvUrl + "/init/" + login).asJson(), 0);

View file

@ -23,36 +23,46 @@ public class Citizen extends Unit {
super(login, citizen_id);
}
public void move(Direction direction) {
public Citizen move(Direction direction) {
sendAction("move:"+direction.direction, 2);
return this;
}
public void observe() {
public Citizen observe() {
sendAction("observe", 1);
return this;
}
public void gather() {
public Citizen gather() {
sendAction("gather", 4);
return this;
}
public void unload() {
public Citizen unload() {
sendAction("unload", 3);
return this;
}
public void farm() {
public Citizen farm() {
sendAction("farm", 10);
return this;
}
public void build(Building building) {
public Citizen build(Building building) {
sendAction("build:" + building.name, 20);
return this;
}
public void spawn(String unit) {
public Citizen spawn(String unit) {
sendAction("spawn:" + unit, 6);
return this;
}
public void refine(String resource) {
public Citizen refine(String resource) {
sendAction("refine:" + resource, 8);
return this;
}
public void sendMessage(String receiver, String message) {
public Citizen sendMessage(String receiver, String message) {
MessageParameter mp = new MessageParameter(receiver, message);
sendActionWithBody("message:send", Json.serialize(mp), 1);
return this;
}
public void fetchMessages() {
public Citizen fetchMessages() {
sendAction("message:fetch", 1);
return this;
}

View file

@ -15,12 +15,16 @@ import org.slf4j.LoggerFactory;
import java.util.concurrent.CompletableFuture;
public abstract class Unit {
public static String srvUrl;
public static final Logger logger = LoggerFactory.getLogger(Unit.class);
protected final String id;
protected final String command_uri;
protected CompletableFuture<HttpResponse<JsonNode>> pendingAction;
protected CompletableFuture<Report> pendingReport;
public static String srvUrl;
public static final Logger logger = LoggerFactory.getLogger(Unit.class);
protected Report lastReport;
public Unit(String login, String id) {
if (srvUrl == null) {
@ -60,14 +64,14 @@ public abstract class Unit {
logger.warn("Tried to wait a citizen with no pending action or report");
return false;
}
Report r = pendingReport.join();
if (r == null) {
lastReport = pendingReport.join();
if (lastReport == null) {
logger.warn("Invalid report received: null value");
return false;
}
logger.debug("Got report: " + r);
logger.debug("Got report: " + lastReport);
return r.errorCode != null;
return lastReport.errorCode != null;
}