Skip to content

Commit

Permalink
refactor: refactor world generator
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Jun 16, 2024
1 parent c120150 commit ccee640
Show file tree
Hide file tree
Showing 32 changed files with 560 additions and 183 deletions.
2 changes: 2 additions & 0 deletions Allay-API/src/main/java/org/allaymc/api/AllayAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.allaymc.api.utils.exception.MissingImplementationException;
import org.allaymc.api.utils.exception.MissingRequirementException;
import org.allaymc.api.world.biome.BiomeTypeRegistry;
import org.allaymc.api.world.generator.WorldGenerator;
import org.allaymc.api.world.generator.WorldGeneratorFactory;
import org.allaymc.api.world.storage.WorldStorageFactory;

Expand Down Expand Up @@ -189,6 +190,7 @@ private void defaultAPIRequirements() {
requireImpl(VanillaItemMetaBlockStateBiMap.class, VanillaItemMetaBlockStateBiMap.REGISTRY::set);

// World
requireImpl(WorldGenerator.WorldGeneratorBuilderFactory.class, WorldGenerator.BUILDER_FACTORY::set);
requireImpl(WorldStorageFactory.class, WorldStorageFactory.FACTORY::set);
requireImpl(WorldGeneratorFactory.class, WorldGeneratorFactory.FACTORY::set);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.allaymc.api.utils;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

/**
* String helpers
Expand All @@ -13,6 +12,7 @@
*
* @author daoge_cmd
*/
@Slf4j
@UtilityClass
public class AllayStringUtils {
public static List<String> fastSplit(String str, String delimiter) {
Expand Down Expand Up @@ -99,4 +99,18 @@ public static LinkedList<String> spiltCommandArgs(String cmdLine) {
}
return args;
}

public static Map<String, String> parseOptions(String preset) {
var splits = fastSplit(preset, ";");
var options = new HashMap<String, String>();
for(var split : splits) {
if (!split.contains("=")) {
log.warn("Invalid option: {}", split);
continue;
}
var kv = fastTwoPartSplit(split, "=", "");
options.put(kv[0], kv[1]);
}
return options;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.allaymc.api.world.chunk;

import org.allaymc.api.utils.HashUtils;

/**
* Allay Project 2023/7/1
*
Expand All @@ -13,13 +15,26 @@ default Chunk getChunkByLevelPos(int x, int z) {
return getChunk(x >> 4, z >> 4);
}

Chunk getChunk(long chunkHash);
default Chunk getChunk(long chunkHash) {
return getChunk(
HashUtils.getXFromHashXZ(chunkHash),
HashUtils.getZFromHashXZ(chunkHash)
);
}

int maxChunkX();
default int maxChunkX() {
return Integer.MAX_VALUE;
}

int maxChunkZ();
default int maxChunkZ() {
return Integer.MAX_VALUE;
}

int minChunkX();
default int minChunkX() {
return Integer.MIN_VALUE;
}

int minChunkZ();
default int minChunkZ() {
return Integer.MIN_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
* @author daoge_cmd
*/
public enum ChunkState {
NEW,
GENERATED,
EMPTY,
NOISED,
POPULATED,
LIGHTED,
ENTITY_SPAWNED,
FINISHED
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
package org.allaymc.api.world.generator;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.allaymc.api.utils.AllayStringUtils;
import org.allaymc.api.ApiInstanceHolder;
import org.allaymc.api.world.Dimension;
import org.jetbrains.annotations.ApiStatus;

import java.util.HashMap;
import java.util.Map;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.api.world.generator.function.EntitySpawner;
import org.allaymc.api.world.generator.function.Noiser;
import org.allaymc.api.world.generator.function.Lighter;
import org.allaymc.api.world.generator.function.Populator;

/**
* Allay Project 2023/7/1
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
@Getter
@Slf4j
public abstract class WorldGenerator {
public interface WorldGenerator {

protected String preset;
protected Dimension dimension;
ApiInstanceHolder<WorldGeneratorBuilderFactory> BUILDER_FACTORY = ApiInstanceHolder.create();

public WorldGenerator(String preset) {
this.preset = preset;
static WorldGeneratorBuilder builder() {
return BUILDER_FACTORY.get().create();
}

public void setDimension(Dimension dimension) {
this.dimension = dimension;
Chunk generateFinishedChunkSynchronously(int x, int z);

String getName();

WorldGeneratorType getType();

String getPreset();

void setDimension(Dimension dimension);

interface WorldGeneratorBuilder {
WorldGeneratorBuilder name(String name);

WorldGeneratorBuilder type(WorldGeneratorType type);

WorldGeneratorBuilder preset(String preset);

WorldGeneratorBuilder noisers(Noiser... noisers);

WorldGeneratorBuilder populators(Populator... populators);

WorldGeneratorBuilder lighters(Lighter... lighters);

WorldGeneratorBuilder entitySpawners(EntitySpawner... entitySpawners);

WorldGenerator build();
}

//empty chunk -> 各种 noise-> biome -> calc height map -> generate -> carvers
//feature -> structure place-> calc light -> spawn entity -> full
public abstract void generate(ChunkGenerateContext context);

public abstract String getGeneratorName();

public abstract WorldGeneratorType getType();

public static Map<String, String> parseOptions(String preset) {
var splits = AllayStringUtils.fastSplit(preset, ";");
var options = new HashMap<String, String>();
for(var split : splits) {
if (!split.contains("=")) {
log.warn("Invalid option: {}", split);
continue;
}
var kv = AllayStringUtils.fastTwoPartSplit(split, "=", "");
options.put(kv[0], kv[1]);
}
return options;
interface WorldGeneratorBuilderFactory {
WorldGeneratorBuilder create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public enum WorldGeneratorType {
// The generator id.
private final int id;

@Contract("_ -> new")
public static WorldGeneratorType of(int id) {
Preconditions.checkArgument(id >= 0 && id <= 4);
return values()[id];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.allaymc.api.world.generator.context;

import lombok.Getter;
import org.allaymc.api.world.DimensionInfo;
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public abstract class Context {
@Getter
protected UnsafeChunk currentChunk;

public Context(UnsafeChunk currentChunk) {
this.currentChunk = currentChunk;
}

protected DimensionInfo getDimensionInfo() {
return currentChunk.getDimensionInfo();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.allaymc.api.world.generator.context;

import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class EntitySpawnContext extends OtherChunkAccessibleContext {

public EntitySpawnContext(UnsafeChunk currentChunk, ChunkAccessible chunkAccessor) {
super(currentChunk, chunkAccessor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.allaymc.api.world.generator.context;

import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class LightContext extends Context {
public LightContext(UnsafeChunk currentChunk) {
super(currentChunk);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.allaymc.api.world.generator.context;

import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class NoiseContext extends Context {
public NoiseContext(UnsafeChunk currentChunk) {
super(currentChunk);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.allaymc.api.world.generator.context;

import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public abstract class OtherChunkAccessibleContext extends Context {

protected ChunkAccessible chunkAccessor;

public OtherChunkAccessibleContext(UnsafeChunk currentChunk, ChunkAccessible chunkAccessor) {
super(currentChunk);
this.chunkAccessor = chunkAccessor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.allaymc.api.world.generator.context;

import org.allaymc.api.world.chunk.ChunkAccessible;
import org.allaymc.api.world.chunk.UnsafeChunk;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public class PopulateContext extends OtherChunkAccessibleContext {
public PopulateContext(UnsafeChunk currentChunk, ChunkAccessible chunkAccessor) {
super(currentChunk, chunkAccessor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.allaymc.api.world.generator.function;

import org.allaymc.api.world.generator.context.EntitySpawnContext;

import java.util.function.Function;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public interface EntitySpawner extends Function<EntitySpawnContext, Boolean>, GenerateFunction {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.allaymc.api.world.generator.function;

import org.allaymc.api.world.generator.WorldGenerator;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public interface GenerateFunction {
default void init(WorldGenerator generator) {}

String getName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.allaymc.api.world.generator.function;

import org.allaymc.api.world.generator.context.LightContext;

import java.util.function.Function;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public interface Lighter extends Function<LightContext, Boolean>, GenerateFunction {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.allaymc.api.world.generator.function;

import org.allaymc.api.world.generator.context.NoiseContext;

import java.util.function.Function;

/**
* Allay Project 2024/6/16
*
* @author daoge_cmd
*/
public interface Noiser extends Function<NoiseContext, Boolean>, GenerateFunction {

}
Loading

0 comments on commit ccee640

Please sign in to comment.