Skip to content

Commit

Permalink
async terrain gen
Browse files Browse the repository at this point in the history
(water seems to be broken now, though...)
  • Loading branch information
DaMatrix committed Dec 28, 2020
1 parent 64d08db commit cf2672d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ repositories {
}

dependencies {
deobfProvided ("com.github.OpenCubicChunks.CubicChunks:cubicchunks:0d61d787ed052ad53391c3ebe9799e4ed2da3659") {
deobfProvided ("com.github.OpenCubicChunks.CubicChunks:cubicchunks:0cc396a1e791a3a66da7b78ed3be02e460756d4b") {
transitive = false
}
deobfProvided ("com.github.OpenCubicChunks:CubicWorldGen:27de56d2f792513873584b2f8fd9f3082fb259ec") {
Expand Down
21 changes: 15 additions & 6 deletions src/main/java/io/github/terra121/control/TerraTeleport.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.minecraftforge.server.permission.PermissionAPI;

import java.text.DecimalFormat;
import java.util.concurrent.CompletableFuture;

public class TerraTeleport extends Command {

Expand Down Expand Up @@ -125,20 +126,28 @@ public void execute(MinecraftServer server, ICommandSender sender, String[] args
return;
}

CompletableFuture<String> altFuture;
if (alt == null) {
try {
alt = String.valueOf(terrain.heights.getAsync(lon, lat).join() + 1);
sender.sendMessage(ChatHelper.makeTextComponent(new TextElement("Computing destination altitude...", TextFormatting.GRAY)));
altFuture = terrain.heights.getAsync(lon, lat)
.thenApply(a -> String.valueOf(a + 1.0d));
} catch (OutOfProjectionBoundsException e) { //out of bounds, notify user
sender.sendMessage(ChatHelper.makeTitleTextComponent(new TextElement("Invalid coordinates!", TextFormatting.RED)));
sender.sendMessage(ChatHelper.makeTextComponent(new TextElement(TranslateUtil.translate("terra121.error.numbers"), TextFormatting.RED)));
return;
}
} else {
altFuture = CompletableFuture.completedFuture(alt);
}

sender.sendMessage(ChatHelper.makeTitleTextComponent(new TextElement("Teleported to ", TextFormatting.GRAY), new TextElement(new DecimalFormat("##.#####").format(lat), TextFormatting.BLUE),
new TextElement(", ", TextFormatting.GRAY), new TextElement(new DecimalFormat("##.#####").format(lon), TextFormatting.BLUE)));
ICommandSender _sender = sender;
altFuture.thenAccept(s -> FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> {
_sender.sendMessage(ChatHelper.makeTitleTextComponent(new TextElement("Teleported to ", TextFormatting.GRAY), new TextElement(new DecimalFormat("##.#####").format(lat), TextFormatting.BLUE),
new TextElement(", ", TextFormatting.GRAY), new TextElement(new DecimalFormat("##.#####").format(lon), TextFormatting.BLUE)));

FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager().executeCommand(
FMLCommonHandler.instance().getMinecraftServerInstance(), String.format("tp %s %s %s %s", sender.getName(), proj[0], alt, proj[1]));
FMLCommonHandler.instance().getMinecraftServerInstance().getCommandManager().executeCommand(
FMLCommonHandler.instance().getMinecraftServerInstance(), String.format("tp %s %s %s %s", _sender.getName(), proj[0], s, proj[1]));
}));
}
}

Expand Down
28 changes: 23 additions & 5 deletions src/main/java/io/github/terra121/generator/EarthGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -150,13 +151,30 @@ public EarthGenerator(World world) {
}

@Override
public CubePrimer generateCube(int cubeX, int cubeY, int cubeZ) { //legacy compat method
return this.generateCube(cubeX, cubeY, cubeZ, new CubePrimer());
public CubePrimer generateCube(int cubeX, int cubeY, int cubeZ) { //legacy compat method, no longer used
throw new UnsupportedOperationException();
}

@Override
public CubePrimer generateCube(int cubeX, int cubeY, int cubeZ, CubePrimer primer) {
CachedChunkData data = this.cache.getUnchecked(new ChunkPos(cubeX, cubeZ)).join();
public GeneratorReadyState pollAsyncCubeGenerator(int cubeX, int cubeY, int cubeZ) {
CompletableFuture<CachedChunkData> future = this.cache.getUnchecked(new ChunkPos(cubeX, cubeZ));
if (!future.isDone()) {
return GeneratorReadyState.WAITING;
} else if (future.isCompletedExceptionally()) {
return GeneratorReadyState.FAIL;
} else {
return GeneratorReadyState.READY;
}
}

@Override
public Optional<CubePrimer> tryGenerateCube(int cubeX, int cubeY, int cubeZ, CubePrimer primer) {
CompletableFuture<CachedChunkData> future = this.cache.getUnchecked(new ChunkPos(cubeX, cubeZ));
if (!future.isDone() || future.isCompletedExceptionally()) {
return Optional.empty();
}

CachedChunkData data = future.join();

//build ground surfaces
this.generateSurface(cubeX, cubeY, cubeZ, primer, data, this.world.getChunk(cubeX, cubeZ).getBiomeArray());
Expand All @@ -174,7 +192,7 @@ public CubePrimer generateCube(int cubeX, int cubeY, int cubeZ, CubePrimer prime
}
}

return primer;
return Optional.of(primer);
}

private void generateSurface(int cubeX, int cubeY, int cubeZ, CubePrimer primer, CachedChunkData data, byte[] biomes) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/github/terra121/util/http/HostManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ private void handleResponse(@NonNull Channel channel, Object msg) {
request = channel.attr(ATTR_REQUEST).getAndSet(null);
checkState(request != null, "received response on inactive channel?!?");

TerraMod.LOGGER.info(this.host + " " + request.path + ": " + response);

this.activeRequests--; //decrement active requests counter to enable another request to be made

if (!HttpUtil.isKeepAlive(response)) { //response isn't keep-alive, close connection
Expand Down

0 comments on commit cf2672d

Please sign in to comment.