Archived
0

Flat world

This commit is contained in:
2018-04-28 11:53:43 +03:00
parent e788841f04
commit a55e340cfe
8 changed files with 209 additions and 36 deletions

View File

@@ -7,20 +7,26 @@ package mc.core.world;
/* 16x256x16 */
public interface Chunk {
int getBlockType(int x, int y, int z);
int[] getBlockTypeAsArray();
void setBlockType(int x, int y, int z, int type);
int getBlockMetadata(int x, int y, int z);
int[] getBlockMetadataAsArray();
void setBlockMetadata(int x, int y, int z, int metadata);
int getBlockLight(int x, int y, int z);
int[] getBlockLightAsArray();
void setBlockLight(int x, int y, int z, int lightLevel);
int getSkyLight(int x, int y, int z);
int[] getSkyLightAsArray();
void setSkyLight(int x, int y, int z, int lightLevel);
int getAddition(int x, int y, int z);
int[] getAdditionAsArray();
void setAddition(int x, int y, int z, int value);
int getBiome(int x, int y, int z);
int[] getBiomeAsArray();
void setBiome(int x, int y, int z, int value);
}

19
flat_world/README.MD Normal file
View File

@@ -0,0 +1,19 @@
# Flat world
Плоский мир
## Spring bean
```xml
<bean id="flatWorld" class="mc.world.flat.FlatWorld">
<property name="spawn">
<bean class="mc.core.Location">
<constructor-arg index="0" type="double" value="8"/>
<constructor-arg index="1" type="double" value="6"/>
<constructor-arg index="2" type="double" value="8"/>
</bean>
</property>
</bean>
```
`spawn` - точка спавна

7
flat_world/build.gradle Normal file
View File

@@ -0,0 +1,7 @@
group 'mc'
version '1.0-SNAPSHOT'
dependencies {
/* Core */
compile_excludeCopy project(':core')
}

View File

@@ -0,0 +1,28 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-28
*/
package mc.world.flat;
import lombok.Getter;
import lombok.Setter;
import mc.core.Location;
import mc.core.world.Chunk;
import mc.core.world.World;
public class FlatWorld implements World {
@Getter
@Setter
private Location spawn = new Location(0, 6, 0);
private Chunk chunk = new SimpleChunk();
@Override
public Chunk getChunk(int x, int z) {
return chunk;
}
@Override
public void setChunk(int x, int z, Chunk chunk) {
throw new UnsupportedOperationException();
}
}

View File

@@ -0,0 +1,127 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2018-04-28
*/
package mc.world.flat;
import mc.core.world.Chunk;
import java.util.Arrays;
public class SimpleChunk implements Chunk {
private int[] blocktype = new int[4096];
private int[] blockmeta = new int[2048];
private int[] blocklight = new int[2048];
private int[] skylight = new int[2048];
private int[] addition = new int[2048];
private int[] biometype = new int[256];
SimpleChunk() {
Arrays.fill(blocktype, 0, 256, 7);
Arrays.fill(blocktype, 256, 768, 3);
Arrays.fill(blocktype, 768, 1024, 2);
Arrays.fill(blocktype, 1024, 4096, 0);
Arrays.fill(blockmeta, 0);
Arrays.fill(blocklight, 0);
Arrays.fill(skylight, 0, 512, 0);
Arrays.fill(skylight, 512, 2048, -1);
Arrays.fill(addition, 0, 256, 1);
Arrays.fill(addition, 256, 2048, 0);
Arrays.fill(biometype, 0);
}
@Override
public int getBlockType(int x, int y, int z) {
return 0;
}
@Override
public int[] getBlockTypeAsArray() {
return blocktype;
}
@Override
public void setBlockType(int x, int y, int z, int type) {
}
@Override
public int getBlockMetadata(int x, int y, int z) {
return 0;
}
@Override
public int[] getBlockMetadataAsArray() {
return blockmeta;
}
@Override
public void setBlockMetadata(int x, int y, int z, int metadata) {
}
@Override
public int getBlockLight(int x, int y, int z) {
return 0;
}
@Override
public int[] getBlockLightAsArray() {
return blocklight;
}
@Override
public void setBlockLight(int x, int y, int z, int lightLevel) {
}
@Override
public int getSkyLight(int x, int y, int z) {
return 0;
}
@Override
public int[] getSkyLightAsArray() {
return skylight;
}
@Override
public void setSkyLight(int x, int y, int z, int lightLevel) {
}
@Override
public int getAddition(int x, int y, int z) {
return 0;
}
@Override
public int[] getAdditionAsArray() {
return addition;
}
@Override
public void setAddition(int x, int y, int z, int value) {
}
@Override
public int getBiome(int x, int y, int z) {
return 0;
}
@Override
public int[] getBiomeAsArray() {
return biometype;
}
@Override
public void setBiome(int x, int y, int z, int value) {
}
}

View File

@@ -8,55 +8,37 @@ import lombok.Setter;
import lombok.ToString;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.ByteArrayOutputNetStream;
import mc.core.world.Chunk;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.zip.Deflater;
@Setter
@ToString
public class ChunkDataPacket implements SCPacket {
private static byte[] compressData;
private static final int dataSize = 4096 + 2048 + 2048 + 2048 + 2048 + 256;
private int x, z;
private boolean needInitChunk;
private int yMin,yMax;
private byte[] compressData;
static {
byte[] blocktype = new byte[4096];
Arrays.fill(blocktype, 0, 256, (byte)7);
Arrays.fill(blocktype, 256, 768, (byte)3);
Arrays.fill(blocktype, 768, 1024, (byte)2);
Arrays.fill(blocktype, 1024, 4096, (byte)0);
public void setChunk(Chunk chunk) {
ByteBuffer chunkData = ByteBuffer.allocate(dataSize);
byte[] blockmeta = new byte[2048];
Arrays.fill(blockmeta, (byte)0);
byte[] blocklight = new byte[2048];
Arrays.fill(blocklight, (byte)0);
byte[] skylight = new byte[2048];
Arrays.fill(skylight, 0, 512, (byte) 0);
Arrays.fill(skylight, 512, 2048, (byte)-1);
byte[] addition = new byte[2048];
Arrays.fill(addition, 0, 256, (byte)1);
Arrays.fill(addition, 256, 2048, (byte)0);
byte[] biometype = new byte[256];
Arrays.fill(biometype, (byte)0);
byte[] chunkData = new byte[blocktype.length + blockmeta.length + blocklight.length + skylight.length + addition.length + biometype.length];
System.arraycopy(blocktype, 0, chunkData, 0, blocktype.length);
System.arraycopy(blockmeta, 0, chunkData, blocktype.length, blockmeta.length);
System.arraycopy(blocklight, 0, chunkData, blockmeta.length, blocklight.length);
System.arraycopy(skylight, 0, chunkData, blocklight.length, skylight.length);
System.arraycopy(addition, 0, chunkData, skylight.length, addition.length);
System.arraycopy(biometype, 0, chunkData, addition.length, biometype.length);
Arrays.stream(chunk.getBlockTypeAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getBlockMetadataAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getBlockLightAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getSkyLightAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getAdditionAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getBiomeAsArray()).forEach(i -> chunkData.put((byte) i));
Deflater zlib = new Deflater(Deflater.DEFAULT_COMPRESSION);
zlib.setInput(chunkData);
zlib.setInput(chunkData.array());
zlib.finish();
byte[] preCompileData = new byte[chunkData.length];
byte[] preCompileData = new byte[dataSize];
int compressSize = zlib.deflate(preCompileData);
compressData = new byte[compressSize];

View File

@@ -15,6 +15,7 @@ import mc.core.*;
import mc.core.network.CSPacket;
import mc.core.network.proto_125.netty.wrappers.WrapperNetChannel;
import mc.core.network.proto_125.packets.*;
import mc.core.world.World;
import org.springframework.beans.factory.annotation.Autowired;
import java.lang.reflect.Method;
@@ -28,6 +29,8 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
private Config config;
@Autowired
private PlayerManager playerManager;
@Autowired
private World world;
@Override
public void channelInactive(ChannelHandlerContext context) throws Exception {
@@ -66,7 +69,6 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
}
public void onLoginPacket(Channel channel, LoginPacket packet) {
final Location spawnLoc = new Location(0, 6, 0);
Player player;
Optional<Player> optPlayer = playerManager.getPlayer(packet.getPlayerName());
@@ -80,7 +82,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
}
} else {
player = playerManager.createPlayer(packet.getPlayerName());
player.setLocation(spawnLoc);
player.setLocation(world.getSpawn());
player.setLook(new Look(0f, 0f));
}
@@ -95,7 +97,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
// send Spawn position
SpawnPositionPacket spawnPkt = new SpawnPositionPacket();
spawnPkt.setLocation(spawnLoc);
spawnPkt.setLocation(world.getSpawn());
channel.write(spawnPkt);
// send Player abilities
@@ -124,6 +126,7 @@ public class PacketHandler extends SimpleChannelInboundHandler<CSPacket> {
ChunkDataPacket chDataPkt = new ChunkDataPacket();
chDataPkt.setX(0);
chDataPkt.setZ(0);
chDataPkt.setChunk(world.getChunk(0, 0));
chDataPkt.setNeedInitChunk(true);
chDataPkt.setYMin(1);
chDataPkt.setYMax(0);

View File

@@ -3,3 +3,4 @@ rootProject.name = 'mc-server'
include('core') // Core
include('proto125') // Protocol 1.2.5
include('proto125_netty') // Protocol 1.2.5 (Netty impl.)
include('flat_world')