Repaired the index

This commit is contained in:
richard committed 2026-10-27 10:10:43 +01:00
commit 01817fbec1
19 files changed
+927

No files matched your search

@@ -0,0 +1,55 @@
package dev.lumareth.improvedAerodynamics;
import net.minecraftforge.common.ForgeConfigSpec;
public final class Config {
private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
static {
BUILDER.push("terrainStreaming");
}
public static final ForgeConfigSpec.DoubleValue LOOKAHEAD_SECONDS = BUILDER
.comment("How many seconds of flight ahead of each ship terrain is loaded for")
.defineInRange("lookaheadSeconds", 3.0D, 0.5D, 20.0D);
public static final ForgeConfigSpec.DoubleValue MIN_SHIP_SPEED = BUILDER
.comment("Ships slower than this (blocks per second) are left to normal player chunk loading")
.defineInRange("minShipSpeed", 8.0D, 0.0D, 1000.0D);
public static final ForgeConfigSpec.IntValue CORRIDOR_MARGIN_CHUNKS = BUILDER
.comment("Extra chunks loaded on each side of a ship's predicted path")
.defineInRange("corridorMarginChunks", 1, 0, 8);
public static final ForgeConfigSpec.IntValue MAX_NEW_CHUNKS_PER_TICK = BUILDER
.comment("Upper limit of chunks newly requested (and newly given an air placeholder) per tick, protects the tick rate")
.defineInRange("maxNewChunksPerTick", 64, 1, 1024);
static {
BUILDER.pop();
BUILDER.push("airPlaceholder");
}
public static final ForgeConfigSpec.BooleanValue ENABLE_AIR_PLACEHOLDER = BUILDER
.comment("Valkyrien Skies treats terrain it has not received yet as solid, so ships hit a wall at unloaded chunks.",
"When enabled, chunks on a ship's path that are not loaded yet are treated as air above the estimated",
"world generation surface until the real terrain arrives. Everything below that stays solid.")
.define("enableAirPlaceholder", false);
public static final ForgeConfigSpec.IntValue PLACEHOLDER_SURFACE_MARGIN = BUILDER
.comment("Blocks above the estimated surface that stay solid, covers trees and structures")
.defineInRange("placeholderSurfaceMargin", 32, 0, 256);
public static final ForgeConfigSpec.IntValue PLACEHOLDER_RELEASE_TICKS = BUILDER
.comment("Ticks a placeholder chunk stays air after no ship needs it anymore, then it goes back to solid")
.defineInRange("placeholderReleaseTicks", 100, 20, 6000);
static {
BUILDER.pop();
}
static final ForgeConfigSpec SPEC = BUILDER.build();
private Config() {
}
}
@@ -0,0 +1,21 @@
package dev.lumareth.improvedAerodynamics;
import com.mojang.logging.LogUtils;
import dev.lumareth.improvedAerodynamics.terrain.LookaheadLoader;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import org.slf4j.Logger;
@Mod(ImprovedAerodynamics.MOD_ID)
public class ImprovedAerodynamics {
public static final String MOD_ID = "improved_aerodynamics";
public static final Logger LOGGER = LogUtils.getLogger();
public ImprovedAerodynamics() {
MinecraftForge.EVENT_BUS.register(LookaheadLoader.class);
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, Config.SPEC);
}
}
@@ -0,0 +1,169 @@
package dev.lumareth.improvedAerodynamics.terrain;
import dev.lumareth.improvedAerodynamics.Config;
import dev.lumareth.improvedAerodynamics.ImprovedAerodynamics;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerChunkCache;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.levelgen.Heightmap;
import org.valkyrienskies.core.internal.VsiCore;
import org.valkyrienskies.core.internal.world.chunks.VsiTerrainUpdate;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import org.valkyrienskies.mod.common.ValkyrienSkiesMod;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Optional: hands VS empty sections for not yet loaded chunks on a ship's path, so physics sees air there instead of
* its solid "unloaded" fallback. Only sections fully above the generator's estimated surface (plus a margin) are
* replaced, everything lower stays solid so a ship can never end up inside terrain that has not loaded yet.
* Once the real chunk loads, VS overwrites every section of it with the actual terrain.
* <p>
* This uses VS internals (its terrain update API and the mixin field listing its loaded chunks), so it switches
* itself off if those are missing.
*/
final class AirPlaceholder {
// Chunks VS has handed to physics, merged into ServerLevel by its MixinServerLevel
private static final Field KNOWN_CHUNKS = findKnownChunks();
private static final int[][] SURFACE_SAMPLES = {{0, 0}, {15, 0}, {0, 15}, {15, 15}, {8, 8}};
private static final Map<ResourceKey<Level>, Long2ObjectOpenHashMap<Placeholder>> PLACEHOLDERS = new HashMap<>();
/** Sections of one chunk that were replaced with air, and the last tick a ship's path still covered it. */
private static final class Placeholder {
final int minSectionY;
final int maxSectionY;
long lastWanted;
Placeholder(int minSectionY, int maxSectionY, long lastWanted) {
this.minSectionY = minSectionY;
this.maxSectionY = maxSectionY;
this.lastWanted = lastWanted;
}
}
private AirPlaceholder() {
}
static void update(ServerLevel level, LongLinkedOpenHashSet corridor, long tick) {
boolean enabled = Config.ENABLE_AIR_PLACEHOLDER.get() && KNOWN_CHUNKS != null;
Long2ObjectOpenHashMap<Placeholder> placeholders = PLACEHOLDERS.get(level.dimension());
if (!enabled && placeholders == null) {
return;
}
Map<ChunkPos, ?> knownChunks = getKnownChunks(level);
if (knownChunks == null) {
return;
}
if (placeholders == null) {
placeholders = new Long2ObjectOpenHashMap<>();
PLACEHOLDERS.put(level.dimension(), placeholders);
}
VsiCore core = ValkyrienSkiesMod.getVsCore();
List<VsiTerrainUpdate> updates = new ArrayList<>();
if (enabled) {
int budget = Config.MAX_NEW_CHUNKS_PER_TICK.get();
int surfaceMargin = Config.PLACEHOLDER_SURFACE_MARGIN.get();
for (long chunk : corridor) {
Placeholder placeholder = placeholders.get(chunk);
if (placeholder != null) {
placeholder.lastWanted = tick;
continue;
}
ChunkPos pos = new ChunkPos(chunk);
// Loaded or already in physics: the real terrain is there or arrives next tick, never cover it with air
if (knownChunks.containsKey(pos) || level.getChunkSource().getChunkNow(pos.x, pos.z) != null) {
continue;
}
if (budget-- <= 0) {
break;
}
int minSectionY = Math.floorDiv(estimateSurface(level, pos) + surfaceMargin, 16) + 1;
int maxSectionY = level.getMaxSection() - 1;
if (minSectionY > maxSectionY) {
continue;
}
for (int sectionY = minSectionY; sectionY <= maxSectionY; sectionY++) {
updates.add(core.newEmptyVoxelShapeUpdate(pos.x, sectionY, pos.z, true));
}
placeholders.put(chunk, new Placeholder(minSectionY, maxSectionY, tick));
}
}
int releaseTicks = Config.PLACEHOLDER_RELEASE_TICKS.get();
ObjectIterator<Long2ObjectMap.Entry<Placeholder>> iterator = placeholders.long2ObjectEntrySet().fastIterator();
while (iterator.hasNext()) {
Long2ObjectMap.Entry<Placeholder> entry = iterator.next();
ChunkPos pos = new ChunkPos(entry.getLongKey());
Placeholder placeholder = entry.getValue();
if (knownChunks.containsKey(pos)) {
// VS loaded the chunk and replaced every section with the real terrain
iterator.remove();
} else if (!enabled || tick - placeholder.lastWanted > releaseTicks) {
// Deleting the sections puts the chunk back to VS's solid "unloaded" state. Terrain updates apply
// in order, so if the chunk loads later its real terrain still wins
for (int sectionY = placeholder.minSectionY; sectionY <= placeholder.maxSectionY; sectionY++) {
updates.add(core.newDeleteTerrainUpdate(pos.x, sectionY, pos.z));
}
iterator.remove();
}
}
if (!updates.isEmpty()) {
VSGameUtilsKt.getShipObjectWorld(level).addTerrainUpdates(VSGameUtilsKt.getDimensionId(level), updates);
}
if (!enabled && placeholders.isEmpty()) {
PLACEHOLDERS.remove(level.dimension());
}
}
static void clear() {
PLACEHOLDERS.clear();
}
/** Highest world generation surface at the chunk's corners and center, sampled from noise without generating the chunk. */
private static int estimateSurface(ServerLevel level, ChunkPos pos) {
ServerChunkCache chunkSource = level.getChunkSource();
int surface = level.getMinBuildHeight();
for (int[] sample : SURFACE_SAMPLES) {
surface = Math.max(surface, chunkSource.getGenerator().getBaseHeight(pos.getMinBlockX() + sample[0],
pos.getMinBlockZ() + sample[1], Heightmap.Types.WORLD_SURFACE_WG, level, chunkSource.randomState()));
}
return surface;
}
@SuppressWarnings("unchecked")
private static Map<ChunkPos, ?> getKnownChunks(ServerLevel level) {
if (KNOWN_CHUNKS == null) {
return null;
}
try {
return (Map<ChunkPos, ?>) KNOWN_CHUNKS.get(level);
} catch (IllegalAccessException e) {
return null;
}
}
private static Field findKnownChunks() {
try {
Field field = ServerLevel.class.getDeclaredField("vs$knownChunks");
field.setAccessible(true);
return field;
} catch (ReflectiveOperationException | RuntimeException e) {
ImprovedAerodynamics.LOGGER.warn("VS's loaded chunk list was not found, the air placeholder is disabled", e);
return null;
}
}
}
@@ -0,0 +1,116 @@
package dev.lumareth.improvedAerodynamics.terrain;
import dev.lumareth.improvedAerodynamics.Config;
import it.unimi.dsi.fastutil.longs.Long2LongMap;
import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap;
import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.TicketType;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.server.ServerStoppedEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.joml.Vector3dc;
import org.joml.primitives.AABBdc;
import org.valkyrienskies.core.api.ships.LoadedServerShip;
import org.valkyrienskies.mod.common.VSGameUtilsKt;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
/**
* Valkyrien Skies only gives its physics engine the terrain of chunks Minecraft has fully loaded, and treats every
* other chunk as solid. Nothing loads terrain ahead of a ship, so fast ships slam into an invisible wall wherever
* the player's view distance has not caught up yet.
* <p>
* This puts a short-lived ticket on every chunk along each fast ship's predicted path, so the terrain is loaded
* (and handed to physics by VS) before the ship gets there.
*/
public final class LookaheadLoader {
private static final int TICKET_LIFETIME = 40;
// Distance 0 gives ticket level 33 (FULL), the lowest level VS feeds to physics. VS also requires the ticket on the chunk itself.
private static final int TICKET_DISTANCE = 0;
private static final TicketType<ChunkPos> SHIP_LOOKAHEAD = TicketType.create("improved_aerodynamics_lookahead",
Comparator.comparingLong(ChunkPos::toLong), TICKET_LIFETIME);
// Last tick each chunk was ticketed, per dimension
private static final Map<ResourceKey<Level>, Long2LongMap> TICKETED = new HashMap<>();
private LookaheadLoader() {
}
// Runs after ServerLevel.tick, so VS has already fed this tick's loaded chunks to physics
@SubscribeEvent
public static void onLevelTick(TickEvent.LevelTickEvent event) {
if (event.phase != TickEvent.Phase.END || !(event.level instanceof ServerLevel level)) {
return;
}
LongLinkedOpenHashSet corridor = collectCorridor(level);
long tick = level.getGameTime();
int budget = Config.MAX_NEW_CHUNKS_PER_TICK.get();
Long2LongMap ticketed = TICKETED.computeIfAbsent(level.dimension(), key -> new Long2LongOpenHashMap());
ticketed.values().removeIf(lastTick -> tick - lastTick >= TICKET_LIFETIME);
for (long chunk : corridor) {
boolean isNew = !ticketed.containsKey(chunk);
if (isNew && budget-- <= 0) {
continue;
}
// Re-adding an existing ticket only refreshes its timeout
ChunkPos pos = new ChunkPos(chunk);
level.getChunkSource().addRegionTicket(SHIP_LOOKAHEAD, pos, TICKET_DISTANCE, pos);
ticketed.put(chunk, tick);
}
AirPlaceholder.update(level, corridor, tick);
}
@SubscribeEvent
public static void onServerStopped(ServerStoppedEvent event) {
TICKETED.clear();
AirPlaceholder.clear();
}
/** World chunks swept by every fast ship's AABB over the lookahead time, nearest first. */
private static LongLinkedOpenHashSet collectCorridor(ServerLevel level) {
LongLinkedOpenHashSet corridor = new LongLinkedOpenHashSet();
String dimension = VSGameUtilsKt.getDimensionId(level);
double minSpeed = Config.MIN_SHIP_SPEED.get();
double lookahead = Config.LOOKAHEAD_SECONDS.get();
int margin = Config.CORRIDOR_MARGIN_CHUNKS.get();
for (LoadedServerShip ship : VSGameUtilsKt.getShipObjectWorld(level).getLoadedShips()) {
if (!dimension.equals(ship.getChunkClaimDimension())) {
continue;
}
Vector3dc velocity = ship.getVelocity();
double speed = Math.sqrt(velocity.x() * velocity.x() + velocity.z() * velocity.z());
if (speed < minSpeed) {
continue;
}
AABBdc box = ship.getWorldAABB();
// Step at most one chunk at a time so no chunk on the path is skipped
int steps = (int) Math.ceil(speed * lookahead / 16D);
for (int step = 0; step <= steps; step++) {
double t = lookahead * step / Math.max(steps, 1);
double dx = velocity.x() * t;
double dz = velocity.z() * t;
int minX = ((int) Math.floor(box.minX() + dx) >> 4) - margin;
int maxX = ((int) Math.floor(box.maxX() + dx) >> 4) + margin;
int minZ = ((int) Math.floor(box.minZ() + dz) >> 4) - margin;
int maxZ = ((int) Math.floor(box.maxZ() + dz) >> 4) + margin;
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
if (!VSGameUtilsKt.isChunkInShipyard(level, x, z)) {
corridor.add(ChunkPos.asLong(x, z));
}
}
}
}
}
return corridor;
}
}
+39
View File
@@ -0,0 +1,39 @@
modLoader = "javafml"
loaderVersion = "${loader_version_range}"
license = "${mod_license}"
[[mods]]
modId = "${mod_id}"
version = "${mod_version}"
displayName = "${mod_name}"
authors = "${mod_authors}"
description = '''${mod_description}'''
[[dependencies."${mod_id}"]]
modId = "forge"
mandatory = true
versionRange = "${forge_version_range}"
ordering = "NONE"
side = "BOTH"
[[dependencies."${mod_id}"]]
modId = "minecraft"
mandatory = true
versionRange = "${minecraft_version_range}"
ordering = "NONE"
side = "BOTH"
[[dependencies."${mod_id}"]]
modId = "valkyrienskies"
mandatory = true
versionRange = "[2.4.11,)"
ordering = "AFTER"
side = "BOTH"
# Optional, for the planned Warium VS flight physics overhaul
[[dependencies."${mod_id}"]]
modId = "valkyrien_warium"
mandatory = false
versionRange = "[1.1.6,)"
ordering = "AFTER"
side = "BOTH"
+6
View File
@@ -0,0 +1,6 @@
{
"pack": {
"description": "improved_aerodynamics resources",
"pack_format": 15
}
}