Skip to content

Commit

Permalink
fix: landslide
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex4386 committed Jun 24, 2024
1 parent 670c024 commit 66945d2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static Material getBombRock(double silicateLevel, double distanceScale) {
double scaled = distanceScale * redScoriaPercentage;
double s = random.nextDouble();

System.out.println("redScoriaPercentage: "+redScoriaPercentage+", distanceScale:"+distanceScale+", Scaled: " + scaled);
//System.out.println("redScoriaPercentage: "+redScoriaPercentage+", distanceScale:"+distanceScale+", Scaled: " + scaled);

if (s < scaled) {
return Material.COBBLED_DEEPSLATE;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.alex4386.plugin.typhon.volcano.landslide;

import me.alex4386.plugin.typhon.TyphonPlugin;
import me.alex4386.plugin.typhon.TyphonScheduler;
import me.alex4386.plugin.typhon.TyphonSounds;
import me.alex4386.plugin.typhon.TyphonUtils;
import me.alex4386.plugin.typhon.volcano.utils.VolcanoMath;
Expand All @@ -10,17 +11,15 @@
import org.bukkit.block.BlockFace;
import org.bukkit.util.Vector;

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

public class VolcanoLandslide {
public VolcanoVent vent;
public double landslideAngle = 0;
public int initSummitY = Integer.MIN_VALUE;

public Iterator<Block> blockIterator = null
;
public Map<Chunk, Iterator<Block>> iteratorPerChunk = null;

public int landslideTimer = -1;

public VolcanoLandslide(VolcanoVent vent) {
Expand All @@ -37,58 +36,91 @@ public void shutdown() {

public void registerTask() {
if (this.landslideTimer < 0) {
this.landslideTimer = Bukkit.getScheduler().scheduleSyncRepeatingTask(TyphonPlugin.plugin, this::runTask, 0, 1);
this.landslideTimer = TyphonScheduler.registerGlobalTask(
this::runTask, 1
);
}
}

public void unregisterTask() {
if (this.landslideTimer >= 0) {
Bukkit.getScheduler().cancelTask(this.landslideTimer);
TyphonScheduler.unregisterTask(this.landslideTimer);
this.landslideTimer = -1;
}
}

public void clear() {
this.blockIterator = null;
this.iteratorPerChunk = null;
this.initSummitY = Integer.MIN_VALUE;
this.landslideAngle = 0;
}

public void runTask() {
if (this.blockIterator == null) return;

Block randomSrc = null;
if (this.iteratorPerChunk == null) return;
if (this.iteratorPerChunk.isEmpty()) {
this.clear();
return;
}

int count = 5 + (int) (Math.random() * 10);
for (int i = 0; i < count; i++) {
if (!this.blockIterator.hasNext()) {
break;
for (Chunk chunk : this.iteratorPerChunk.keySet()) {
if (this.iteratorPerChunk.get(chunk) == null || !this.iteratorPerChunk.get(chunk).hasNext()) {
this.iteratorPerChunk.remove(chunk);
continue;
}
TyphonScheduler.run(chunk, () -> this.runChunk(chunk));
}

if (Math.random() < 0.1) {
TyphonSounds.DISTANT_EXPLOSION.play(this.vent.location, SoundCategory.BLOCKS, 2f, 1f);
}

Block block = this.blockIterator.next();
this.runPyroclasticFlow();
}

if (randomSrc == null) randomSrc = block;
else if (Math.random() < 0.1) randomSrc = block;
public void runChunk(Chunk chunk) {
Iterator<Block> iterator = this.iteratorPerChunk.get(chunk);
if (iterator == null) return;

int count = 0;
while (iterator.hasNext() && count < 100) {
Block block = iterator.next();
this.runCollapse(block);
count++;
}

if (Math.random() < 0.1 && randomSrc != null) {
TyphonSounds.DISTANT_EXPLOSION.play(randomSrc.getLocation(), SoundCategory.BLOCKS, 2f, 1f);
}

this.runPyroclasticFlow();
if (!this.blockIterator.hasNext()) {
this.blockIterator = null;
if (!iterator.hasNext()) {
this.iteratorPerChunk.remove(chunk);
}
}

public void start() {
if (!this.isConfigured()) return;

if (this.blockIterator == null) {
this.blockIterator = this.getTargetBlocks().iterator();
if (this.iteratorPerChunk == null) {
this.generateIterator();
}
}

public void generateIterator() {
List<Block> blocks = this.getTargetBlocks();
Map<Chunk, List<Block>> blocksPerChunk = new HashMap<>();

for (Block block : blocks) {
Chunk chunk = block.getChunk();
if (!blocksPerChunk.containsKey(chunk)) {
blocksPerChunk.put(chunk, new ArrayList<>());
}

blocksPerChunk.get(chunk).add(block);
}

Map<Chunk, Iterator<Block>> iteratorPerChunk = new HashMap<>();
for (Chunk chunk : blocksPerChunk.keySet()) {
List<Block> blockList = blocksPerChunk.get(chunk);
iteratorPerChunk.put(chunk, blockList.iterator());
}

this.iteratorPerChunk = iteratorPerChunk;
}

public boolean isConfigured() {
Expand All @@ -97,7 +129,9 @@ public boolean isConfigured() {

public void configure() {
this.initSummitY = (int) (this.vent.getSummitBlock().getY() + (this.vent.getRadius() / Math.sqrt(3)));
this.landslideAngle = Math.random() * Math.PI * 2;
if (this.landslideAngle == 0) {
this.landslideAngle = Math.random() * Math.PI * 2;
}
}

public void setLandslideAngle(double angle) {
Expand All @@ -123,12 +157,12 @@ public double getRimY(double x) {
}

public int getFloorY(double x) {
if (x < 0 && x > -this.getRadius()) {
if (x < 0) {
return (int) (this.getRimSummitY() - this.getRadius() - x);
}

int zeroY = this.getRimSummitY() - this.getRadius();
double offset = ((1.0 / 16.0) * x);
double offset = ((1.0 / 12.0) * x);
return (int) Math.round(zeroY - offset);
}

Expand Down Expand Up @@ -186,38 +220,40 @@ public double getLandslideVolume() {
}

public List<Block> getTargetBlocks() {
List<Block> blocks = VolcanoMath.getCircle(this.vent.location.getBlock(), this.getRadius());
double radius = this.getRadius() * 1.25;

Set<Block> blocks = new HashSet<>(VolcanoMath.getCircle(this.vent.location.getBlock(), (int) radius));

double cos = Math.cos(this.landslideAngle);
double sin = Math.sin(this.landslideAngle);

int length = this.getRadius() * 8;
for (int x = 0; x < length; x++) {
for (int z = -this.getRadius(); z < this.getRadius(); z++) {
for (double x = 0; x < length; x += 0.5) {
for (double z = -radius; z < radius; z += 0.5) {
double rotatedX = x * cos - z * sin;
double rotatedZ = x * sin + z * cos;

Block block = this.vent.location.getBlock().getRelative((int) rotatedX, 0, (int) rotatedZ);
if (!blocks.contains(block)) {
blocks.add(block);
}
blocks.add(block);
}
}

return blocks;
return blocks.stream().toList();
}

public void runCollapse(Block block) {
World world = block.getWorld();
Vector vector = this.getVector(block.getLocation());
int y = this.getY(vector);

int currentY = TyphonUtils.getHighestRocklikes(block).getY();
Block topBlock = TyphonUtils.getHighestRocklikes(block);
int currentY = topBlock.getY();
System.out.println("Drilling down from " + currentY + " to " + y);
if (currentY < y) return;

for (int i = currentY; i > y; i--) {
Block targetBlock = block.getRelative(0, i - currentY, 0);
if (targetBlock.getType().isSolid()) {
Block targetBlock = topBlock.getRelative(0, i - currentY, 0);
if (!targetBlock.getType().isAir()) {
Material material = i <= world.getSeaLevel() ?
world.isUltraWarm() ? Material.LAVA : Material.WATER
: Material.AIR;
Expand All @@ -243,7 +279,10 @@ public Block getPyroclasticFlowSource() {
double x = xOffset * cos - zOffset * sin;
double z = xOffset * sin + zOffset * cos;

return TyphonUtils.getHighestRocklikes(this.vent.location.clone().add(x, 0, z).getBlock()).getRelative(BlockFace.UP);
Block baseBlock = this.vent.location.clone().add(x, 255, z).getBlock();
baseBlock = TyphonUtils.getHighestRocklikes(baseBlock);

return TyphonUtils.getHighestRocklikes(baseBlock).getRelative(BlockFace.UP);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ public void setLavaInflux(int currentLavaInflux) {
}

public void queueBlockUpdate(Block block, Material material) {
if (this.queueScheduleId == -1) {
this.registerQueueUpdate();
}

Chunk chunk = block.getChunk();
if (!blockUpdateQueues.containsKey(chunk)) {
blockUpdateQueues.put(chunk, new LinkedList<>());
Expand Down Expand Up @@ -210,6 +214,10 @@ public void registerTask() {
},
1L);
}
this.registerQueueUpdate();
}

public void registerQueueUpdate() {
if (queueScheduleId == -1) {
this.vent.volcano.logger.log(
VolcanoLogClass.LAVA_FLOW,
Expand Down Expand Up @@ -1225,7 +1233,7 @@ public double getDistanceRatio(Location dest) {
double distanceFromVent = Math.max(0, Math.min(distance - radius, coneHeight));
double scaledDistance = distanceFromVent / coneHeight;

System.out.println("coneHeight: "+coneHeight+", distance: "+distance+", radius: "+radius+", distanceFromVent: "+distanceFromVent+", scaledDistance: "+scaledDistance);
//System.out.println("coneHeight: "+coneHeight+", distance: "+distance+", radius: "+radius+", distanceFromVent: "+distanceFromVent+", scaledDistance: "+scaledDistance);

return Math.min(Math.pow(scaledDistance, 2), 1);
}
Expand Down

0 comments on commit 66945d2

Please sign in to comment.