Search in sources :

Example 1 with WorldTool

use of io.xol.chunkstories.world.WorldTool in project chunkstories by Hugobros3.

the class TaskConvertMcChunk method task.

@Override
protected boolean task(TaskExecutor taskExecutor) {
    ConverterWorkerThread cwt = (ConverterWorkerThread) taskExecutor;
    WorldTool csWorld = cwt.world();
    // Is it within our borders ?
    // if (chunkStoriesCurrentChunkX >= 0 && chunkStoriesCurrentChunkX < cwt.size().sizeInChunks * 32 && chunkStoriesCurrentChunkZ >= 0 && chunkStoriesCurrentChunkZ < cwt.size().sizeInChunks * 32)
    {
        // MinecraftChunk minecraftChunk = null;
        try {
            if (minecraftChunk != null) {
                // If it succeed, we first require to load the corresponding chunkstories stuff
                // Ignore the summaries for now
                /*Heightmap summary = csWorld.getRegionsSummariesHolder().aquireRegionSummaryWorldCoordinates(this, chunkStoriesCurrentChunkX, chunkStoriesCurrentChunkZ);
					if(summary != null)
						registeredCS_Summaries.add(summary);*/
                CompoundFence loadRelevantData = new CompoundFence();
                // Then the chunks
                for (int y = 0; y < OfflineWorldConverter.mcWorldHeight; y += 32) {
                    ChunkHolder holder = csWorld.aquireChunkHolderWorldCoordinates(cwt, chunkStoriesCurrentChunkX, y, chunkStoriesCurrentChunkZ);
                    if (holder != null) {
                        loadRelevantData.add(holder.waitForLoading());
                        if (cwt.registeredCS_Holders.add(holder))
                            cwt.chunksAquired++;
                    }
                }
                // Wait for them to actually load
                loadRelevantData.traverse();
                for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) for (int y = 0; y < OfflineWorldConverter.mcWorldHeight; y++) {
                    // Translate each block
                    int mcId = minecraftChunk.getBlockID(x, y, z) & 0xFFF;
                    byte meta = (byte) (minecraftChunk.getBlockMeta(x, y, z) & 0xF);
                    // Ignore air blocks
                    if (mcId != 0) {
                        Mapper mapper = this.mappers.getMapper(mcId, meta);
                        if (mapper == null)
                            continue;
                        if (mapper instanceof NonTrivialMapper) {
                            ((NonTrivialMapper) mapper).output(csWorld, chunkStoriesCurrentChunkX + x, y, chunkStoriesCurrentChunkZ + z, mcId, meta, minecraftRegion, minecraftCurrentChunkXinsideRegion, minecraftCuurrentChunkZinsideRegion, x, y, z);
                        } else {
                            FutureCell future = new FutureCell(csWorld, chunkStoriesCurrentChunkX + x, y, chunkStoriesCurrentChunkZ + z, csWorld.getContent().voxels().air());
                            // Directly set trivial blocks
                            mapper.output(mcId, meta, future);
                            if (!future.getVoxel().isAir())
                                csWorld.pokeSimpleSilently(future);
                        }
                    }
                }
            // Converts external data such as signs
            // SpecialBlocksHandler.processAdditionalStuff(minecraftChunk, csWorld, chunkStoriesCurrentChunkX, 0, chunkStoriesCurrentChunkZ);
            }
        } catch (Exception e) {
            cwt.converter().verbose("Issue with chunk " + minecraftCurrentChunkXinsideRegion + " " + minecraftCuurrentChunkZinsideRegion + " of region " + minecraftRegionX + " " + minecraftRegionZ + ".");
            e.printStackTrace();
        }
    }
    return true;
}
Also used : Mapper(io.xol.chunkstories.api.converter.mappings.Mapper) NonTrivialMapper(io.xol.chunkstories.api.converter.mappings.NonTrivialMapper) FutureCell(io.xol.chunkstories.api.world.cell.FutureCell) CompoundFence(io.xol.chunkstories.util.concurrency.CompoundFence) WorldTool(io.xol.chunkstories.world.WorldTool) ChunkHolder(io.xol.chunkstories.api.world.chunk.ChunkHolder) ConverterWorkerThread(io.xol.chunkstories.converter.ConverterWorkers.ConverterWorkerThread) NonTrivialMapper(io.xol.chunkstories.api.converter.mappings.NonTrivialMapper)

Example 2 with WorldTool

use of io.xol.chunkstories.world.WorldTool in project chunkstories by Hugobros3.

the class ChunkLightBaker method requestLightningUpdate.

@Override
public Fence requestLightningUpdate() {
    unbakedUpdates.incrementAndGet();
    if (world instanceof WorldTool) {
        WorldTool tool = (WorldTool) world;
        if (!tool.isLightningEnabled()) {
            // System.out.println("too soon");
            return new TrivialFence();
        }
    }
    Task fence;
    taskLock.lock();
    if (task == null || task.isDone() || task.isCancelled()) {
        task = new TaskLightChunk(chunk, true);
        chunk.getWorld().getGameContext().tasks().scheduleTask(task);
    }
    fence = task;
    taskLock.unlock();
    return fence;
}
Also used : TrivialFence(io.xol.chunkstories.util.concurrency.TrivialFence) Task(io.xol.chunkstories.api.workers.Task) WorldTool(io.xol.chunkstories.world.WorldTool)

Example 3 with WorldTool

use of io.xol.chunkstories.world.WorldTool 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;
}
Also used : Entity(io.xol.chunkstories.api.entity.Entity) WorldUser(io.xol.chunkstories.api.world.WorldUser) WorldException(io.xol.chunkstories.api.exceptions.world.WorldException) ChunkRenderable(io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable) Chunk(io.xol.chunkstories.api.world.chunk.Chunk) PacketVoxelUpdate(io.xol.chunkstories.api.net.packets.PacketVoxelUpdate) RemotePlayer(io.xol.chunkstories.api.server.RemotePlayer) WorldTool(io.xol.chunkstories.world.WorldTool) Voxel(io.xol.chunkstories.api.voxel.Voxel) WorldMaster(io.xol.chunkstories.api.world.WorldMaster)

Aggregations

WorldTool (io.xol.chunkstories.world.WorldTool)3 Mapper (io.xol.chunkstories.api.converter.mappings.Mapper)1 NonTrivialMapper (io.xol.chunkstories.api.converter.mappings.NonTrivialMapper)1 Entity (io.xol.chunkstories.api.entity.Entity)1 WorldException (io.xol.chunkstories.api.exceptions.world.WorldException)1 PacketVoxelUpdate (io.xol.chunkstories.api.net.packets.PacketVoxelUpdate)1 ChunkRenderable (io.xol.chunkstories.api.rendering.world.chunk.ChunkRenderable)1 RemotePlayer (io.xol.chunkstories.api.server.RemotePlayer)1 Voxel (io.xol.chunkstories.api.voxel.Voxel)1 Task (io.xol.chunkstories.api.workers.Task)1 WorldMaster (io.xol.chunkstories.api.world.WorldMaster)1 WorldUser (io.xol.chunkstories.api.world.WorldUser)1 FutureCell (io.xol.chunkstories.api.world.cell.FutureCell)1 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)1 ChunkHolder (io.xol.chunkstories.api.world.chunk.ChunkHolder)1 ConverterWorkerThread (io.xol.chunkstories.converter.ConverterWorkers.ConverterWorkerThread)1 CompoundFence (io.xol.chunkstories.util.concurrency.CompoundFence)1 TrivialFence (io.xol.chunkstories.util.concurrency.TrivialFence)1