Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix chunk loading
Browse files Browse the repository at this point in the history
  • Loading branch information
dskprt committed Mar 22, 2021
1 parent d895295 commit 83f9cc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion nylium.Core/Networking/GameClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public void LoadChunks(Chunk[] chunks) {
}
j++;
});
}, true);

Short blockCount = new((short) nonAirBlockCount);
UByte bitsPerBlock = new((byte) GameBlock.bitsPerBlock);
Expand Down
19 changes: 15 additions & 4 deletions nylium.Core/World/Chunk.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using nylium.Core.Block;

namespace nylium.Core.World {
Expand Down Expand Up @@ -93,11 +94,21 @@ public void SetBlock(GameBlock block, int x, int y, int z) {
Blocks[y, x, z] = block;
}

public void Iterate(Action<GameBlock> action) {
for(int y = 0; y < Y_SIZE; y++) {
for(int x = 0; x < X_SIZE; x++) {
public void Iterate(Action<GameBlock> action, bool flipXZ = false) {
if(flipXZ) {
for(int y = 0; y < Y_SIZE; y++) {
for(int z = 0; z < Z_SIZE; z++) {
action(GetBlock(x, y, z));
for(int x = 0; x < X_SIZE; x++) {
action(GetBlock(x, y, z));
}
}
}
} else {
for(int y = 0; y < Y_SIZE; y++) {
for(int x = 0; x < X_SIZE; x++) {
for(int z = 0; z < Z_SIZE; z++) {
action(GetBlock(x, y, z));
}
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions nylium.Core/World/Storage/Formats/WaterWorldFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public override void Save() {
public override Chunk Load(int chunkX, int chunkZ) {
Chunk chunk = new(World, chunkX, chunkZ);

for(int id = 0; id < 16; id++) {
int errorCount = 0;

for(int id = 0; id < Chunk.SECTION_COUNT; id++) {
(int, int, int) key = (chunkX, chunkZ, id);

if(ChunkLookup.ContainsKey(key)) {
Expand All @@ -91,19 +93,24 @@ public override Chunk Load(int chunkX, int chunkZ) {
for(int y = 0; y < Chunk.Section.Y_SIZE; y++) {
for(int x = 0; x < Chunk.Section.X_SIZE; x++) {
for(int z = 0; z < Chunk.Section.Z_SIZE; z++) {
section.SetBlock(GameBlock.Create(World, BitConverter.ToUInt16(data, i * sizeof(ushort))), x, y, z);
ushort blockId = BitConverter.ToUInt16(data, i * sizeof(ushort));

if(blockId != 0) {
section.SetBlock(GameBlock.Create(World, BitConverter.ToUInt16(data, i * sizeof(ushort))), x, y, z);
}

i++;
}
}
}

chunk.SetSection(section, id);
} else {
return null;
errorCount++;
}
}

return chunk;
return errorCount >= Chunk.SECTION_COUNT ? null : chunk;
}

public override void Save(Chunk chunk) {
Expand All @@ -116,7 +123,7 @@ public override void Save(Chunk chunk) {
Chunk.Section section = chunk.GetSection(id);

if(section == null) {
section = new(id, chunk);
continue;
}

section.Iterate(block => {
Expand Down

0 comments on commit 83f9cc4

Please sign in to comment.