use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories by Hugobros3.
the class VoxelsStore method readVoxelsDefinitions.
private void readVoxelsDefinitions(Asset f) {
if (f == null)
return;
try {
BufferedReader reader = new BufferedReader(f.reader());
String line = "";
// Voxel voxel = null;
int ln = 0;
int loadedVoxels = 0;
while ((line = reader.readLine()) != null) {
line = line.replace("\t", "");
if (line.startsWith("#")) {
// It's a comment, ignore.
} else {
if (line.startsWith("voxel")) {
String[] splitted = line.split(" ");
if (splitted.length < 2) {
logger().warn("Parse error in file " + f + ", line " + ln + ", malformed voxel tag. Aborting read.");
break;
}
// int id = Integer.parseInt(splitted[2]);
String name = splitted[1];
try {
VoxelDefinitionImplementation voxelType = new VoxelDefinitionImplementation(this, name, reader);
Voxel voxel = voxelType.getVoxelObject();
voxelsByName.put(voxel.getName(), voxel);
loadedVoxels++;
if (name.equals("air"))
air = voxel;
} catch (IllegalVoxelDeclarationException e) {
e.printStackTrace();
}
} else if (line.startsWith("end")) {
logger().warn("Parse error in file " + f + ", line " + ln + ", unexpected 'end' token.");
}
}
ln++;
}
logger().debug("Parsed file " + f + " correctly, loading " + loadedVoxels + " voxels.");
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories by Hugobros3.
the class CubicChunk method pokeInternal.
/**
* The 'core' of the core, this private function is responsible for placing and
* keeping everyone up to snuff on block modifications. It all comes back to this really.
*/
private ActualChunkVoxelContext pokeInternal(final int worldX, final int worldY, final int worldZ, Voxel newVoxel, final int sunlight, final int blocklight, final int metadata, int raw_data, final boolean use_raw_data, final boolean update, final boolean return_context, final WorldModificationCause cause) {
int x = sanitizeCoordinate(worldX);
int y = sanitizeCoordinate(worldY);
int z = sanitizeCoordinate(worldZ);
ActualChunkVoxelContext cell_pre = peek(x, y, z);
Voxel formerVoxel = cell_pre.getVoxel();
assert formerVoxel != null;
FreshFutureCell future = new FreshFutureCell(cell_pre);
if (use_raw_data) {
// We need this for voxel placement logic
newVoxel = world.getContentTranslator().getVoxelForId(VoxelFormat.id(raw_data));
// Build the future from parsing the raw data
future.setVoxel(newVoxel);
future.setSunlight(VoxelFormat.sunlight(raw_data));
future.setBlocklight(VoxelFormat.blocklight(raw_data));
future.setMetaData(VoxelFormat.meta(raw_data));
} else {
// Build the raw data from the set parameters by editing the in-place data
// (because we allow only editing some aspects of the cell data)
raw_data = cell_pre.getData();
if (newVoxel != null) {
raw_data = VoxelFormat.changeId(raw_data, world.getContentTranslator().getIdForVoxel(newVoxel));
future.setVoxel(newVoxel);
}
if (sunlight >= 0) {
raw_data = VoxelFormat.changeSunlight(raw_data, sunlight);
future.setSunlight(sunlight);
}
if (blocklight >= 0) {
raw_data = VoxelFormat.changeBlocklight(raw_data, blocklight);
future.setBlocklight(blocklight);
}
if (metadata >= 0) {
raw_data = VoxelFormat.changeMeta(raw_data, metadata);
future.setMetaData(metadata);
}
}
try {
if (newVoxel == null || formerVoxel.equals(newVoxel)) {
formerVoxel.onModification(cell_pre, future, cause);
} else {
formerVoxel.onRemove(cell_pre, cause);
newVoxel.onPlace(future, cause);
}
} catch (WorldException e) {
// Abort !
if (return_context)
return cell_pre;
else
return null;
}
// Allocate if it makes sense
if (chunkVoxelData == null)
chunkVoxelData = atomicalyCreateInternalData();
chunkVoxelData[x * 32 * 32 + y * 32 + z] = raw_data;
if (newVoxel != null && !formerVoxel.equals(newVoxel))
newVoxel.whenPlaced(future);
// Update lightning
if (update)
lightBaker.computeLightSpread(x, y, z, cell_pre.raw_data, raw_data);
// Increment the modifications counter
compr_uncomittedBlockModifications.incrementAndGet();
// Don't spam the thread creation spawn
occlusion.unbakedUpdates.incrementAndGet();
// Update related summary
if (update)
world.getRegionsSummariesHolder().updateOnBlockPlaced(x, y, z, future);
// Mark the nearby chunks to be re-rendered
if (update) {
int sx = chunkX;
int ex = sx;
int sy = chunkY;
int ey = sy;
int sz = chunkZ;
int ez = sz;
if (x == 0)
sx--;
else if (x == 31)
ex++;
if (y == 0)
sy--;
else if (y == 31)
ey++;
if (z == 0)
sz--;
else if (z == 31)
ez++;
for (int ix = sx; ix <= ex; ix++) for (int iy = sy; iy <= ey; iy++) for (int iz = sz; iz <= ez; iz++) {
Chunk chunk = world.getChunk(ix, iy, iz);
if (chunk != null && chunk instanceof ChunkRenderable)
((ChunkRenderable) chunk).meshUpdater().requestMeshUpdate();
}
}
// If this is a 'master' world, notify remote users of the change !
if (update && world instanceof WorldMaster && !(world instanceof WorldTool)) {
PacketVoxelUpdate packet = new PacketVoxelUpdate(new ActualChunkVoxelContext(chunkX * 32 + x, chunkY * 32 + y, chunkZ * 32 + z, raw_data));
Iterator<WorldUser> pi = this.chunkHolder.users.iterator();
while (pi.hasNext()) {
WorldUser user = pi.next();
if (!(user instanceof RemotePlayer))
continue;
RemotePlayer player = (RemotePlayer) user;
Entity clientEntity = player.getControlledEntity();
if (clientEntity == null)
// Ignore clients that aren't playing
continue;
player.pushPacket(packet);
}
}
if (return_context)
return new ActualChunkVoxelContext(chunkX * 32 + x, chunkY * 32 + y, chunkZ * 32 + z, raw_data);
else
return null;
}
use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories-core by Hugobros3.
the class EntityGroundItem method tick.
@Override
public void tick() {
// this.moveWithCollisionRestrain(0, -0.05, 0);
Vector3d velocity = velocityComponent.getVelocity();
if (world instanceof WorldMaster) {
Voxel voxelIn = world.peekSafely(positionComponent.getLocation()).getVoxel();
boolean inWater = voxelIn.getDefinition().isLiquid();
double terminalVelocity = inWater ? -0.25 : -0.5;
if (velocity.y() > terminalVelocity && !this.isOnGround())
velocity.y = (velocity.y() - 0.016);
if (velocity.y() < terminalVelocity)
velocity.y = (terminalVelocity);
Vector3dc remainingToMove = moveWithCollisionRestrain(velocity.x(), velocity.y(), velocity.z());
if (remainingToMove.y() < -0.02 && this.isOnGround()) {
if (remainingToMove.y() < -0.01) {
// Bounce
double originalDownardsVelocity = velocity.y();
double bounceFactor = 0.15;
velocity.mul(bounceFactor);
velocity.y = (-originalDownardsVelocity * bounceFactor);
// world.getSoundManager().playSoundEffect("./sounds/dogez/weapon/grenades/grenade_bounce.ogg", Mode.NORMAL, getLocation(), 1, 1, 10, 35);
} else
velocity.mul(0d);
}
if (Math.abs(velocity.x()) < 0.02)
velocity.x = (0.0);
if (Math.abs(velocity.z()) < 0.02)
velocity.z = (0.0);
if (Math.abs(velocity.y()) < 0.01)
velocity.y = (0.0);
velocityComponent.setVelocity(velocity);
}
if (world instanceof WorldClient) {
if (this.isOnGround()) {
rotation += 1.0f;
rotation %= 360;
}
}
super.tick();
}
use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories-core by Hugobros3.
the class EntityHumanoid method handleWalkingEtcSounds.
protected void handleWalkingEtcSounds() {
// This is strictly a clientside hack
if (!(getWorld() instanceof WorldClient))
return;
// When the entities are too far from the player, don't play any sounds
if (((WorldClient) getWorld()).getClient().getPlayer().getControlledEntity() != null)
if (((WorldClient) getWorld()).getClient().getPlayer().getControlledEntity().getLocation().distance(this.getLocation()) > 25f)
return;
// Sound stuff
if (isOnGround() && !lastTickOnGround) {
justLanded = true;
metersWalked = 0.0;
}
// Used to trigger landing sound
lastTickOnGround = this.isOnGround();
// Bobbing
Vector3d horizontalSpeed = new Vector3d(this.getVelocityComponent().getVelocity());
horizontalSpeed.y = 0d;
if (isOnGround())
metersWalked += Math.abs(horizontalSpeed.length());
boolean inWater = isInWater();
Voxel voxelStandingOn = world.peekSafely(new Vector3d(this.getLocation()).add(0.0, -0.01, 0.0)).getVoxel();
if (voxelStandingOn == null || !voxelStandingOn.getDefinition().isSolid() && !voxelStandingOn.getDefinition().isLiquid())
return;
VoxelMaterial material = voxelStandingOn.getMaterial();
if (justJumped && !inWater) {
justJumped = false;
getWorld().getSoundManager().playSoundEffect(material.resolveProperty("jumpingSounds"), Mode.NORMAL, getLocation(), (float) (0.9f + Math.sqrt(getVelocityComponent().getVelocity().x() * getVelocityComponent().getVelocity().x() + getVelocityComponent().getVelocity().z() * getVelocityComponent().getVelocity().z()) * 0.1f), 1f).setAttenuationEnd(10);
}
if (justLanded) {
justLanded = false;
getWorld().getSoundManager().playSoundEffect(material.resolveProperty("landingSounds"), Mode.NORMAL, getLocation(), (float) (0.9f + Math.sqrt(getVelocityComponent().getVelocity().x() * getVelocityComponent().getVelocity().x() + getVelocityComponent().getVelocity().z() * getVelocityComponent().getVelocity().z()) * 0.1f), 1f).setAttenuationEnd(10);
}
if (metersWalked > 0.2 * Math.PI * 2) {
metersWalked %= 0.2 * Math.PI * 2;
if (horizontalSpeed.length() <= 0.06)
getWorld().getSoundManager().playSoundEffect(material.resolveProperty("walkingSounds"), Mode.NORMAL, getLocation(), (float) (0.9f + Math.sqrt(getVelocityComponent().getVelocity().x() * getVelocityComponent().getVelocity().x() + getVelocityComponent().getVelocity().z() * getVelocityComponent().getVelocity().z()) * 0.1f), 1f).setAttenuationEnd(10);
else
getWorld().getSoundManager().playSoundEffect(material.resolveProperty("runningSounds"), Mode.NORMAL, getLocation(), (float) (0.9f + Math.sqrt(getVelocityComponent().getVelocity().x() * getVelocityComponent().getVelocity().x() + getVelocityComponent().getVelocity().z() * getVelocityComponent().getVelocity().z()) * 0.1f), 1f).setAttenuationEnd(10);
}
}
use of io.xol.chunkstories.api.voxel.Voxel in project chunkstories-core by Hugobros3.
the class WaterPass method render.
@Override
public void render(RenderingInterface renderer) {
// if(true)
// return;
renderer.setBlendMode(BlendMode.MIX);
renderer.setCullingMode(CullingMode.DISABLED);
GameWindow gameWindow = pipeline.getRenderingInterface().getWindow();
// one for computing the refracted color and putting it in shaded buffer, and another one to read it back and blend it
for (int pass = 1; pass <= 2; pass++) {
Shader liquidBlocksShader = renderer.useShader("blocks_liquid_pass" + pass);
liquidBlocksShader.setUniform1f("viewDistance", world.getClient().getConfiguration().getIntOption("client.rendering.viewDistance"));
renderer.bindTexture2D("waterNormalDeep", renderer.textures().getTexture("./textures/water/deep.png"));
renderer.bindTexture2D("waterNormalShallow", renderer.textures().getTexture("./textures/water/shallow.png"));
renderer.bindTexture2D("lightColors", renderer.textures().getTexture("./textures/environement/light.png"));
Texture2D blocksAlbedoTexture = gameWindow.getClient().getContent().voxels().textures().getDiffuseAtlasTexture();
renderer.bindAlbedoTexture(blocksAlbedoTexture);
liquidBlocksShader.setUniform2f("screenSize", gameWindow.getWidth(), gameWindow.getHeight());
skyRenderer.setupShader(liquidBlocksShader);
liquidBlocksShader.setUniform1f("animationTimer", worldRenderer.getAnimationTimer());
renderer.getCamera().setupShader(liquidBlocksShader);
// Underwater flag
Voxel vox = world.peekSafely(renderer.getCamera().getCameraPosition()).getVoxel();
liquidBlocksShader.setUniform1f("underwater", vox.getDefinition().isLiquid() ? 1 : 0);
if (pass == 1) {
renderer.getRenderTargetManager().setConfiguration(waterRefractionFbo);
renderer.getRenderTargetManager().clearBoundRenderTargetAll();
renderer.bindTexture2D("readbackAlbedoBufferTemp", albedoBuffer);
renderer.bindTexture2D("readbackVoxelLightBufferTemp", voxelLightBuffer);
renderer.bindTexture2D("readbackDepthBufferTemp", zBuffer);
renderer.getRenderTargetManager().setDepthMask(false);
} else if (pass == 2) {
renderer.getRenderTargetManager().setConfiguration(fboGBuffers);
renderer.setBlendMode(BlendMode.DISABLED);
renderer.bindTexture2D("readbackShadedBufferTemp", waterTempTexture);
renderer.getRenderTargetManager().setDepthMask(true);
}
renderer.setObjectMatrix(new Matrix4f());
worldRenderer.getChunksRenderer().renderChunks(renderer);
}
}
Aggregations