Skip to content

Commit

Permalink
Merge pull request #1 from Astrinox/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoidMaster authored Nov 10, 2024
2 parents acdb5f4 + 03bd4d3 commit ddad709
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.9
loader_version=0.16.9

# Mod Properties
mod_version=0.0.1-alpha_1.21
mod_version=0.0.2-alpha_1.21
maven_group=astrinox.stellum
archives_base_name=stellum

Expand Down
74 changes: 73 additions & 1 deletion src/main/java/astrinox/stellum/command/DebugCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.DoubleArgumentType;
import com.mojang.brigadier.arguments.FloatArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;

import astrinox.stellum.handlers.explosion.ExplosionHandler;
import astrinox.stellum.handlers.screenshake.Screenshake;
import astrinox.stellum.handlers.screenshake.ScreenshakeHandler;
import astrinox.stellum.util.PerlinNoiseHelper;
import net.minecraft.block.Blocks;
import net.minecraft.command.CommandRegistryAccess;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import static net.minecraft.server.command.CommandManager.*;

public class DebugCommand {
public static final SimpleCommandExceptionType NON_PLAYER = new SimpleCommandExceptionType(
Text.literal("This command can only be used by players"));

public static void register(CommandDispatcher<ServerCommandSource> serverCommandSourceDispatcher,
CommandRegistryAccess commandRegistryAccess,
CommandManager.RegistrationEnvironment registrationEnvironment) {
serverCommandSourceDispatcher.register(literal("stellum")
.then(literal("debug")
.requires(source -> source.hasPermissionLevel(2))
.then(literal("screenshake")
.then(argument("intensity",
FloatArgumentType.floatArg())
Expand All @@ -29,15 +41,75 @@ public static void register(CommandDispatcher<ServerCommandSource> serverCommand
.integer())
.then(argument("fade",
BoolArgumentType.bool())
.executes(DebugCommand::executeScreenshake)))))));
.executes(DebugCommand::executeScreenshake)))))
.then(literal("noise").then(argument("size", IntegerArgumentType.integer())
.then(argument("noiseScale", DoubleArgumentType.doubleArg())
.then(argument("seed", IntegerArgumentType.integer())
.executes(DebugCommand::executeNoise)))))
.then(literal("explosion")
.then(argument("size", IntegerArgumentType.integer())
.then(argument("noiseScale", DoubleArgumentType.doubleArg())
.then(argument("noiseMultiplier", FloatArgumentType.floatArg())
.then(argument("damage", FloatArgumentType.floatArg())
.executes(
DebugCommand::executeExplosion))))))));
}

public static int executeScreenshake(CommandContext<ServerCommandSource> context)
throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
if (!(source.getEntity().isPlayer())) {
throw NON_PLAYER.create();
}
float intensity = context.getArgument("intensity", float.class);
int durationMs = context.getArgument("durationMs", int.class);
boolean fade = context.getArgument("fade", boolean.class);
ScreenshakeHandler.addScreenshake(new Screenshake(intensity, durationMs, fade));
return 1;
}

public static int executeNoise(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
World world = source.getWorld();
double x = source.getPosition().x;
double y = source.getPosition().y;
double z = source.getPosition().z;
PerlinNoiseHelper noise = new PerlinNoiseHelper(context.getArgument("seed", int.class),
context.getArgument("noiseScale", double.class));
int size = context.getArgument("size", int.class);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
if (noise.noise(x + i, y + j, z + k) > 0) {
world.setBlockState(new BlockPos((int) x + i, (int) y + j, (int) z + k),
Blocks.STONE.getDefaultState());
}
}
}
}
return 1;
}

public static int executeExplosion(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerCommandSource source = context.getSource();
World world = source.getWorld();
double noiseScale = context.getArgument("noiseScale", double.class);
float noiseMultiplier = context.getArgument("noiseMultiplier", float.class);

ExplosionHandler explosion = new ExplosionHandler()
.setPos(new BlockPos(
(int) source.getPosition().x,
(int) source.getPosition().y,
(int) source.getPosition().z))
.setSize(context.getArgument("size", int.class))
.setNoiseScale(noiseScale)
.setNoiseMultiplier(noiseMultiplier)
.setHurtEntities(true)
.setBreakBlocks(true)
.setDamage(context.getArgument("damage", float.class));

explosion.trigger(world);

return 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package astrinox.stellum.handlers.explosion;

import java.util.List;
import java.util.Random;

import astrinox.stellum.util.PerlinNoiseHelper;
import net.minecraft.block.Blocks;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.BlockPos.Mutable;
import net.minecraft.world.World;

public class ExplosionHandler {
private BlockPos pos = new BlockPos(0, 0, 0);
private int size = 16;
private double noiseScale = 0.3;
private float noiseMultiplier = 100;
private boolean breakBlocks = true;
private boolean hurtEntities = true;
private float damage = 10;

public ExplosionHandler setPos(BlockPos pos) {
this.pos = pos;
return this;
}

public ExplosionHandler setSize(int size) {
this.size = size;
return this;
}

public ExplosionHandler setNoiseScale(double noiseScale) {
this.noiseScale = noiseScale;
return this;
}

public ExplosionHandler setNoiseMultiplier(float noiseMultiplier) {
this.noiseMultiplier = noiseMultiplier;
return this;
}

public ExplosionHandler setBreakBlocks(boolean breakBlocks) {
this.breakBlocks = breakBlocks;
return this;
}

public ExplosionHandler setHurtEntities(boolean hurtEntities) {
this.hurtEntities = hurtEntities;
return this;
}

public ExplosionHandler setDamage(float damage) {
this.damage = damage;
return this;
}

public void trigger(World world) {
int sizeSquared = size * size;
Mutable blockPos = new Mutable();
PerlinNoiseHelper mainNoise = new PerlinNoiseHelper(new Random().nextLong(), noiseScale);

for (int x = pos.getX() - size; x <= pos.getX() + size; x++) {
for (int y = pos.getY() - size; y <= pos.getY() + size; y++) {
for (int z = pos.getZ() - size; z <= pos.getZ() + size; z++) {
int dx = x - pos.getX();
int dy = y - pos.getY();
int dz = z - pos.getZ();

int distanceSquared = dx * dx + dy * dy + dz * dz;

if (distanceSquared <= sizeSquared + mainNoise.noise(x, y, z) * noiseMultiplier) {
blockPos.set(x, y, z);
if (breakBlocks) {
world.setBlockState(blockPos, Blocks.AIR.getDefaultState());
}
if (hurtEntities) {
Box box = new Box(x - 0.5, y - 0.5, z - 0.5, x + 0.5, y + 0.5, z + 0.5);
List<LivingEntity> entities = world.getEntitiesByClass(LivingEntity.class, box,
e -> e.isAlive());
for (LivingEntity entity : entities) {
entity.damage(world.getDamageSources().generic(), damage);
}
}
}
}
}
}

// TODO: Add effects pass, for things like fire and scorching

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.ArrayList;
import net.minecraft.client.render.Camera;
import net.minecraft.util.math.Vec2f;
import astrinox.stellum.util.EaseHelper;
import astrinox.stellum.util.EasingHelper;
import astrinox.stellum.util.MathHelper;

public class ScreenshakeHandler {
Expand Down Expand Up @@ -38,7 +38,7 @@ private static Vec2f getShake() {
double sum = 0;
for (Screenshake screenshake : screenshakes) {
if (screenshake.fade) {
sum += EaseHelper.easeInQuad(
sum += EasingHelper.easeInQuad(
MathHelper.map(System.currentTimeMillis(), screenshake.startTime, screenshake.endTime,
screenshake.intensity, 0));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// https://easings.net/

public class EaseHelper {
public class EasingHelper {
public static double easeInSine(double x) {
return 1 - Math.cos((x * Math.PI) / 2);
}
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/astrinox/stellum/util/PerlinNoiseHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package astrinox.stellum.util;

import java.util.Random;

public class PerlinNoiseHelper {
private int[] perm;
private double scale;

public PerlinNoiseHelper(long seed, double scale) {
this.scale = scale;
perm = new int[512];
Random random = new Random(seed);
for (int i = 0; i < 256; i++) {
perm[i] = i;
}
for (int i = 0; i < 256; i++) {
int j = random.nextInt(256);
int temp = perm[i];
perm[i] = perm[j];
perm[j] = temp;
}
System.arraycopy(perm, 0, perm, 256, 256);
}

private double fade(double t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}

private double lerp(double t, double a, double b) {
return a + t * (b - a);
}

private double grad(int hash, double x, double y, double z) {
int h = hash & 15;
double u = h < 8 ? x : y;
double v = h < 4 ? y : (h == 12 || h == 14 ? x : z);
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}

public double noise(double x, double y, double z) {
x *= scale;
y *= scale;
z *= scale;

int X = (int) Math.floor(x) & 255;
int Y = (int) Math.floor(y) & 255;
int Z = (int) Math.floor(z) & 255;

x -= Math.floor(x);
y -= Math.floor(y);
z -= Math.floor(z);

double u = fade(x);
double v = fade(y);
double w = fade(z);

int A = perm[X] + Y;
int AA = perm[A] + Z;
int AB = perm[A + 1] + Z;
int B = perm[X + 1] + Y;
int BA = perm[B] + Z;
int BB = perm[B + 1] + Z;

return lerp(w, lerp(v, lerp(u, grad(perm[AA], x, y, z),
grad(perm[BA], x - 1, y, z)),
lerp(u, grad(perm[AB], x, y - 1, z),
grad(perm[BB], x - 1, y - 1, z))),
lerp(v, lerp(u, grad(perm[AA + 1], x, y, z - 1),
grad(perm[BA + 1], x - 1, y, z - 1)),
lerp(u, grad(perm[AB + 1], x, y - 1, z - 1),
grad(perm[BB + 1], x - 1, y - 1, z - 1))));
}

}

0 comments on commit ddad709

Please sign in to comment.