Skip to content

Commit

Permalink
configurable star seed and limit
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Dec 23, 2023
1 parent fa9b26b commit bb5b63b
Show file tree
Hide file tree
Showing 13 changed files with 6,788 additions and 144 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
mappings libs.yarn
modImplementation libs.fl
modImplementation libs.fapi
modImplementation libs.kaleidoConfig
}

processResources {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ org.gradle.configureondemand=true
# Enable advanced multi-module optimizations (share tiny-remaper instance between projects)
fabric.loom.multiProjectOptimisation=true
# Mod Properties
baseVersion = 0.1.1
baseVersion = 0.2.0
defaultBranch = 1.20.4
branch = 1.20.4
2 changes: 2 additions & 0 deletions libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mc = "1.20.4"
fl = "0.15.2"
yarn = "1.20.4+build.3"
fapi = "0.91.3+1.20.4"
kaleidoConfig = "0.1.1+1.1.0-beta.3"

[plugins]
loom = { id = "fabric-loom", version.ref = "loom" }
Expand All @@ -18,3 +19,4 @@ mc = { group = "mojang", name = "minecraft", version.ref = "mc" }
fl = { group = "net.fabricmc", name = "fabric-loader", version.ref = "fl" }
yarn = { group = "net.fabricmc", name = "yarn", version.ref = "yarn" }
fapi = { group = "net.fabricmc.fabric-api", name = "fabric-api", version.ref = "fapi" }
kaleidoConfig = { group = "folk.sisby", name = "kaleido-config", version.ref = "kaleidoConfig" }
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static void init() {
private static void setInitialStarState(MinecraftClient client, ClientPlayNetworkHandler handler, PacketByteBuf buf, PacketSender responseSender) {
client.execute(() -> {
if (client.world instanceof StarcallerWorld scw) {
scw.starcaller$setSeed(buf.readLong());
scw.starcaller$setIterations(buf.readInt());
scw.starcaller$setGeneratorValues(buf.readLong(), buf.readInt());
updateGrounded(client, handler, buf, responseSender);
updateColors(client, handler, buf, responseSender);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,21 @@
import folk.sisby.starcaller.duck.StarcallerWorld;
import folk.sisby.starcaller.util.StarUtil;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.profiler.Profiler;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.List;
import java.util.function.Supplier;

@Mixin(ClientWorld.class)
public abstract class MixinClientWorld implements StarcallerWorld {
@Unique private long starcaller$seed = Starcaller.STAR_SEED;
@Unique private int starcaller$iterations = Starcaller.STAR_ITERATIONS;
@Unique private List<Star> starcaller$stars;

@Inject(method = "<init>", at = @At("TAIL"))
public void generateStars(ClientPlayNetworkHandler clientPlayNetworkHandler, ClientWorld.Properties properties, RegistryKey<World> registryKey, RegistryEntry<World> registryEntry, int i, int j, Supplier<Profiler> supplier, WorldRenderer worldRenderer, boolean bl, long l, CallbackInfo ci) {
starcaller$stars = StarUtil.generateStars(starcaller$seed, starcaller$iterations);
}
@Unique private long starcaller$seed = 10842L;
@Unique private int starcaller$iterations = 1500;
@Unique private List<Star> starcaller$stars = StarUtil.generateStars(starcaller$seed, starcaller$iterations);

@Inject(method = "method_23787", at = @At("HEAD"), cancellable = true)
public void fullBrightStarsWithSpear(float f, CallbackInfoReturnable<Float> cir) {
Expand Down Expand Up @@ -75,12 +62,10 @@ public void fullBrightStarsWithSpear(float f, CallbackInfoReturnable<Float> cir)
}

@Override
public void starcaller$setSeed(long seed) {
public void starcaller$setGeneratorValues(long seed, int iterations) {
this.starcaller$seed = seed;
}

@Override
public void starcaller$setIterations(int iterations) {
this.starcaller$iterations = iterations;
this.starcaller$stars = StarUtil.generateStars(starcaller$seed, starcaller$iterations);
StarcallerClient.reloadStars((ClientWorld) (Object) this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

Expand All @@ -37,13 +39,26 @@ public void resetStarDebug(BufferBuilder bufferBuilder, CallbackInfoReturnable<B
}

@Inject(method = "renderStars(Lnet/minecraft/client/render/BufferBuilder;)Lnet/minecraft/client/render/BufferBuilder$BuiltBuffer;", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/random/Random;nextDouble()D"), locals = LocalCapture.CAPTURE_FAILHARD)
public void setHasDrawn(BufferBuilder bufferBuilder, CallbackInfoReturnable<BufferBuilder.BuiltBuffer> cir, Random random, int i, double d, double e, double f, double g, double h, double j, double k, double l, double m, double n, double o, double p, double q, double r) {
if (Starcaller.DEBUG_SKY) {
Starcaller.LOGGER.info("Gen Star {} {} {}", j, k, l);
}
public void countSuccessfulStars(BufferBuilder bufferBuilder, CallbackInfoReturnable<BufferBuilder.BuiltBuffer> cir, Random random, int i, double d, double e, double f, double g, double h, double j, double k, double l, double m, double n, double o, double p, double q, double r) {
starIndex++;
}

@ModifyArg(method = "renderStars(Lnet/minecraft/client/render/BufferBuilder;)Lnet/minecraft/client/render/BufferBuilder$BuiltBuffer;", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/math/random/Random;create(J)Lnet/minecraft/util/math/random/Random;"))
public long useCustomSeed(long original) {
if (world instanceof StarcallerWorld scw) {
return scw.starcaller$getSeed();
}
return original;
}

@ModifyConstant(method = "renderStars(Lnet/minecraft/client/render/BufferBuilder;)Lnet/minecraft/client/render/BufferBuilder$BuiltBuffer;", constant = @Constant(intValue = 1500))
public int useCustomLimit(int constant) {
if (world instanceof StarcallerWorld scw) {
return scw.starcaller$getIterations();
}
return constant;
}

@ModifyReceiver(method = "renderStars(Lnet/minecraft/client/render/BufferBuilder;)Lnet/minecraft/client/render/BufferBuilder$BuiltBuffer;", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/VertexConsumer;next()V"))
public VertexConsumer setColorPerStar(VertexConsumer instance, BufferBuilder builder) {
int color = Star.DEFAULT_COLOR;
Expand Down Expand Up @@ -76,60 +91,4 @@ public VertexFormat useColorBuffer(VertexFormat vertexFormat) {
public ShaderProgram useColorProgram(ShaderProgram shaderProgram) {
return GameRenderer.getPositionColorProgram();
}

@Inject(method = "renderStars(Lnet/minecraft/client/render/BufferBuilder;)Lnet/minecraft/client/render/BufferBuilder$BuiltBuffer;", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/BufferBuilder;end()Lnet/minecraft/client/render/BufferBuilder$BuiltBuffer;"))
public void renderDebugStars(BufferBuilder bufferBuilder, CallbackInfoReturnable<BufferBuilder.BuiltBuffer> cir) {
if (Starcaller.DEBUG_SKY) {
// D, E, F -> Z, Y, -X
// X, Y, Z -> -F, E, D

// +Z
bufferBuilder.vertex(100.0F, 0 - 2, 0 - 2).next();
bufferBuilder.vertex(100.0F, 0 - 2, 0 + 2).next();
bufferBuilder.vertex(100.0F, 0 + 2, 0 + 2).next();
bufferBuilder.vertex(100.0F, 0 + 2, 0 - 2).next();

// -Z
bufferBuilder.vertex(-100.0F, 0 - 2, 0).next();
bufferBuilder.vertex(-100.0F, 0, 0 - 2).next();
bufferBuilder.vertex(-100.0F, 0 + 2, 0).next();
bufferBuilder.vertex(-100.0F, 0, 0 + 2).next();

// Y
bufferBuilder.vertex(0 - 8, 100F, 0 - 8).next();
bufferBuilder.vertex(0 + 8, 100F, 0 - 8).next();
bufferBuilder.vertex(0 + 8, 100F, 0 + 8).next();
bufferBuilder.vertex(0 - 8, 100F, 0 + 8).next();

// -Y
bufferBuilder.vertex(0 - 8, -100F, 0).next();
bufferBuilder.vertex(0, -100F, 0 + 8).next();
bufferBuilder.vertex(0 + 8, -100F, 0).next();
bufferBuilder.vertex(0, -100F, 0 - 8).next();

// X
bufferBuilder.vertex(0 - 2, 0 - 2, -100.0F).next();
bufferBuilder.vertex(0 - 2, 0 + 2, -100.0F).next();
bufferBuilder.vertex(0 + 2, 0 + 2, -100.0F).next();
bufferBuilder.vertex(0 + 2, 0 - 2, -100.0F).next();

// -X
bufferBuilder.vertex(0 - 2, 0, 100.0F).next();
bufferBuilder.vertex(0, 0 - 2, 100.0F).next();
bufferBuilder.vertex(0 + 2, 0, 100.0F).next();
bufferBuilder.vertex(0, 0 + 2, 100.0F).next();

// +ZX
bufferBuilder.vertex(70.0F, 0 - 2, -70.0F - 2).next();
bufferBuilder.vertex(70.0F, 0 - 2, -70.0F + 2).next();
bufferBuilder.vertex(70.0F, 0 + 2, -70.0F + 2).next();
bufferBuilder.vertex(70.0F, 0 + 2, -70.0F - 2).next();

// -ZX
bufferBuilder.vertex(-70.0F, 0 - 2, 70.0F).next();
bufferBuilder.vertex(-70.0F, 0, 70.0F - 2).next();
bufferBuilder.vertex(-70.0F, 0 + 2, 70.0F).next();
bufferBuilder.vertex(-70.0F, 0, 70.0F + 2).next();
}
}
}
31 changes: 18 additions & 13 deletions src/main/java/folk/sisby/starcaller/StarState.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,37 @@

public class StarState extends PersistentState {
public static final String KEY_SEED = "seed";
public static final String KEY_ITERATIONS = "iterations";
public static final String KEY_LIMIT = "limit";
public static final String KEY_STARS = "stars";

public long seed;
public int iterations;
public int limit;
public int iterations; // Not persistent, just convenient for the client.
public List<Star> stars;

public static PersistentState.Type<StarState> getPersistentStateType() {
return new PersistentState.Type<>(StarState::new, StarState::fromNbt, null);
public static PersistentState.Type<StarState> getPersistentStateType(long worldSeed) {
return new PersistentState.Type<>(() -> new StarState(worldSeed), nbt -> StarState.fromNbt(nbt, worldSeed), null);
}

public StarState() {
this.seed = Starcaller.STAR_SEED;
this.iterations = Starcaller.STAR_ITERATIONS;
public StarState(long worldSeed) {
this.seed = (Starcaller.CONFIG.starSeed != -1 ? Starcaller.CONFIG.starSeed : worldSeed);
this.limit = Starcaller.CONFIG.starLimit;
this.iterations = StarUtil.getGeneratorIterations(seed, limit);
this.stars = StarUtil.generateStars(this.seed, this.iterations);
markDirty();
}

public StarState(long seed, int iterations, List<Star> stars) {
public StarState(long seed, int limit, int iterations, List<Star> stars) {
this.seed = seed;
this.limit = limit;
this.iterations = iterations;
this.stars = stars;
}

public static StarState fromNbt(NbtCompound nbt) {
long seed = nbt.contains(KEY_SEED) ? nbt.getLong(KEY_SEED) : Starcaller.STAR_SEED;
int iterations = nbt.contains(KEY_ITERATIONS) ? nbt.getInt(KEY_ITERATIONS) : Starcaller.STAR_ITERATIONS;
public static StarState fromNbt(NbtCompound nbt, long worldSeed) {
long seed = nbt.contains(KEY_SEED) ? nbt.getLong(KEY_SEED) : (Starcaller.CONFIG.starSeed != -1 ? Starcaller.CONFIG.starSeed : worldSeed);
int limit = nbt.contains(KEY_LIMIT) ? nbt.getInt(KEY_LIMIT) : Starcaller.CONFIG.starLimit;
int iterations = StarUtil.getGeneratorIterations(seed, limit);
List<Star> stars = StarUtil.generateStars(seed, iterations);
int i = 0;
for (NbtElement starElement : nbt.getList(KEY_STARS, NbtElement.COMPOUND_TYPE)) {
Expand All @@ -44,7 +49,7 @@ public static StarState fromNbt(NbtCompound nbt) {
}
i++;
}
return new StarState(seed, iterations, stars);
return new StarState(seed, limit, iterations, stars);
}

@Override
Expand All @@ -54,7 +59,7 @@ public NbtCompound writeNbt(NbtCompound nbt) {
nbtList.add(star.toNbt());
}
nbt.putLong(KEY_SEED, this.seed);
nbt.putLong(KEY_ITERATIONS, this.iterations);
nbt.putInt(KEY_LIMIT, this.limit);
nbt.put(KEY_STARS, nbtList);
return nbt;
}
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/folk/sisby/starcaller/Starcaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerWorldEvents;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
Expand All @@ -20,25 +21,19 @@
public class Starcaller implements ModInitializer {
public static final String ID = "starcaller";
public static final Logger LOGGER = LoggerFactory.getLogger(ID);
public static final boolean DEBUG_SKY = false;
public static final long STAR_SEED = 10842L;
public static final int STAR_ITERATIONS = 1500;
public static final int STAR_GROUNDED_TICKS = 1200;
public static final String STATE_KEY = "starcaller_stars";

public static final StardustItem STARDUST = Registry.register(Registries.ITEM, id("stardust"), new StardustItem(new FabricItemSettings().maxCount(1)));
public static final SpearItem SPEAR = Registry.register(Registries.ITEM, id("spear"), new SpearItem(new FabricItemSettings().maxCount(1)));

public static final StarcallerConfig CONFIG = StarcallerConfig.createToml(FabricLoader.getInstance().getConfigDir(), "", ID, StarcallerConfig.class);

@Override
public void onInitialize() {
ServerWorldEvents.LOAD.register(((server, world) -> {
if (world.getRegistryKey() == World.OVERWORLD) {
StarState state = world.getPersistentStateManager().getOrCreate(StarState.getPersistentStateType(), STATE_KEY);
if (Starcaller.DEBUG_SKY) {
LOGGER.info("[Starcaller] Start Logging World Stars");
state.stars.forEach(s -> LOGGER.info("[Starcaller] {}", s));
LOGGER.info("[Starcaller] End Logging World Stars");
}
world.getPersistentStateManager().getOrCreate(StarState.getPersistentStateType(world.getSeed()), STATE_KEY);
}
}));
ServerTickEvents.END_WORLD_TICK.register((world -> {
Expand Down Expand Up @@ -67,7 +62,7 @@ public static void freeStar(PlayerEntity cause, ServerWorld world, Star star) {
private static void updateStarGrounded(PlayerEntity cause, ServerWorld world, Star star, long time) {
if (star.groundedTick != time) {
star.groundedTick = time;
world.getPersistentStateManager().get(StarState.getPersistentStateType(), STATE_KEY).markDirty();
world.getPersistentStateManager().get(StarState.getPersistentStateType(world.getSeed()), STATE_KEY).markDirty();
StarcallerNetworking.syncStarGrounded(cause, world, star);
}
}
Expand All @@ -80,6 +75,6 @@ public static void colorStar(PlayerEntity cause, ServerWorld world, Star star, i
TextColor nameColor = cause.getDisplayName().getStyle().getColor();
star.editor = cause.getDisplayName().getString();
star.editorColor = nameColor != null ? nameColor.getRgb() : 0xFFFFFF;
world.getPersistentStateManager().get(StarState.getPersistentStateType(), STATE_KEY).markDirty();
world.getPersistentStateManager().get(StarState.getPersistentStateType(world.getSeed()), STATE_KEY).markDirty();
}
}
19 changes: 19 additions & 0 deletions src/main/java/folk/sisby/starcaller/StarcallerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package folk.sisby.starcaller;

import folk.sisby.kaleido.api.WrappedConfig;
import folk.sisby.kaleido.lib.quiltconfig.api.annotations.Comment;

public class StarcallerConfig extends WrappedConfig {
@Comment("The seed used by star generation.")
@Comment("The default seed generates identical stars to vanilla.")
@Comment("To change the seed or limit of an existing world, delete data/starcaller_stars.dat")
@Comment("To keep existing colorings (but not positions), edit the values in the file manually.")
@Comment("Set to -1 to use the world seed.")
public final Long starSeed = 10842L;

@Comment("The maximum amount of stars the generator may place.")
@Comment("The generator will iterate 3x of the limit, so less may appear.")
@Comment("Star names are provided via indexed translations e.g. star.starcaller.overworld.11")
@Comment("Starcaller provides \"proper names\" up to 780 stars, and fake bayer designations up to 7468 stars.")
public final Integer starLimit = 780;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public interface StarcallerWorld {
int starcaller$getIterations();
List<Star> starcaller$getStars();

void starcaller$setSeed(long seed);
void starcaller$setIterations(int iterations);
void starcaller$setGeneratorValues(long seed, int iterations);

void starcaller$groundStar(PlayerEntity cause, Star star);

Expand Down
Loading

0 comments on commit bb5b63b

Please sign in to comment.