Skip to content

Commit

Permalink
load blocks from registry, actually texture all (most) blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Apr 14, 2020
1 parent ab2d453 commit 7871fb3
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 34 deletions.
1 change: 1 addition & 0 deletions Godotcraft.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ If you want to run it yourself, you need to download the minecraft assets and pu
Its located at `C:\Users\Martin\AppData\Roaming\Godot\app_userdata\Godotcraft` for me.
InventiveTalent has a repo with all the stuff: https://api.github.com/repos/InventivetalentDev/minecraft-assets/zipball/1.15.2

# Data

You also need some data, you can generate those from a vanilla minecraft server (forks work too).
`java -cp minecraft_server.jar net.minecraft.data.Main --all`
You then need to put the data and reports folder into a mcdata folder into the user folder (see above)

# Status

14.04.2020: Rewritten chunk rendering, now its actually working (kinda) https://i.imgur.com/0liIc2B.png
11.04.2020: Chunk rendering? Kinda? Maybe? https://i.imgur.com/sumfigG.png
10.04.2020: Movement! https://streamable.com/951xc2
09.04.2020: More rendering, better culling, texture atlas! https://i.imgur.com/3yl7z1Q.png
Expand Down
1 change: 0 additions & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ movement_jump={
[mono]

debugger_agent/port=50000
debugger_agent/wait_for_debugger=true
debugger_agent/wait_timeout=10000

[rendering]
Expand Down
2 changes: 1 addition & 1 deletion scripts/network/protocol/MinecraftClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private Packet readPacketInternal(int len) {
type = PacketType.of(packetId, currentState, PacketDirection.TO_CLIENT);
}
catch (KeyNotFoundException e) {
GD.Print($"Coulnt find a packet type for id 0x{packetId:X} and state {currentState}");
// GD.Print($"Coulnt find a packet type for id 0x{packetId:X} and state {currentState}");
return null;
}

Expand Down
16 changes: 8 additions & 8 deletions scripts/renderer/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ public bool createMesh(ChunkSection section) {
for (var z = 0; z < 16; z++) {
// get data and check if air
BlockState data = getData(section, x, y, z);
if (data == BlockRegistry.AIR) {
if (data.transparent) {
continue;
}

addedOne = true;

// culling
bool renderFront = true;
if (z < 16 - 1) renderFront = getData(section, x, y, z + 1) == BlockRegistry.AIR;
if (z < 16 - 1) renderFront = getData(section, x, y, z + 1).transparent;
bool renderBack = true;
if (z > 0) renderBack = getData(section, x, y, z - 1) == BlockRegistry.AIR;
if (z > 0) renderBack = getData(section, x, y, z - 1).transparent;
bool renderRight = true;
if (x < 16 - 1) renderRight = getData(section, x + 1, y, z) == BlockRegistry.AIR;
if (x < 16 - 1) renderRight = getData(section, x + 1, y, z).transparent;
bool renderLeft = true;
if (x > 0) renderLeft = getData(section, x - 1, y, z) == BlockRegistry.AIR;
if (x > 0) renderLeft = getData(section, x - 1, y, z).transparent;
bool renderTop = true;
if (y < 16 - 1) renderTop = getData(section, x, y + 1, z) == BlockRegistry.AIR;
if (y < 16 - 1) renderTop = getData(section, x, y + 1, z).transparent;
bool renderBot = true;
if (y > 0) renderBot = getData(section, x, y - 1, z) == BlockRegistry.AIR;
if (y > 0) renderBot = getData(section, x, y - 1, z).transparent;

// add cube
createCube(x, y, z, data, renderFront, renderBack, renderRight, renderLeft, renderTop, renderBot);
Expand Down Expand Up @@ -117,7 +117,7 @@ public void createCube(int x, int y, int z, BlockState data, bool renderFront, b

private void addTri(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, BlockState data) {
// UVMap uv = UVMap.getMap("aa_test");
UVMap uv = UVMap.getMap("dirt");
UVMap uv = UVMap.getMap(data.name);
// if (data == 2) {
// uv = UVMap.getMap("bedrock");
// }
Expand Down
20 changes: 20 additions & 0 deletions scripts/renderer/TextureAtlas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ private Texture createAtlas() {

end:

// we need to implement "fancy" blockstransparent
addAlias("end_portal", "end_portal_frame_side");
addAlias("furnance", "furnance_front");
addAlias("wall_torch", "torch");
addAlias("smooth_stone_slab", "smooth_stone_slab");
addAlias("stone_brick_slab", "stone_bricks");
addAlias("spruce_fence", "spruce_planks");
addAlias("bone_block", "bone_block_side");
addAlias("grass_block", "grass_block_side");
// fixes
addAlias("water", "debug");
addAlias("water_block", "debug");
addAlias("lava", "debug");
addAlias("lava_block", "debug");


ImageTexture imageTexture = new ImageTexture();
imageTexture.CreateFromImage(texture);
imageTexture.Flags &= ~(uint)Texture.FlagsEnum.Filter;
Expand All @@ -80,6 +96,10 @@ private Texture createAtlas() {
return new AtlasTexture {Atlas = imageTexture, Region = new Rect2(0,0,atlasWidth, atlasHeight)};
}

private void addAlias(string name, string alias) {
new UVMap(name, UVMap.getMap(alias).uvMap).register();
}

private List<string> getImages(Directory textureDir) {
List<string> images = new List<string>();
textureDir.ListDirBegin(true, true);
Expand Down
16 changes: 8 additions & 8 deletions scripts/renderer/UVMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Godotcraft.scripts.renderer {
public class UVMap {

private static readonly List<UVMap> maps = new List<UVMap>();
private static readonly Dictionary<string, UVMap> maps = new Dictionary<string, UVMap>();

public string name { get; }
public Vector2[] uvMap { get; }
Expand All @@ -15,18 +15,18 @@ public UVMap(string name, Vector2[] uvMap) {
}

public void register() {
maps.Add(this);
maps.Add(name, this);
}

public static UVMap getMap(string name) {
foreach (var map in maps) {
if (map.name.Equals(name)) {
return map;
}
UVMap map;
maps.TryGetValue(name, out map);
if (map != null) {
return map;
}

GD.Print("Didnt find UVMap for " + name);
return maps[0];
// GD.Print("Didnt find UVMap for " + name);
return maps["debug"];
}

public override string ToString() {
Expand Down
4 changes: 2 additions & 2 deletions scripts/world/ChunkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override void _Ready() {
public void handle(ChunkDataPacket packet) {
if (packet.chunkData.getSectionCount() == 0) return;
creationQueue.Add(packet);
GD.Print("add new packet, size is now " + creationQueue.Count);
// GD.Print("add new packet, size is now " + creationQueue.Count);
}

private bool shouldStartNewThread() {
Expand Down Expand Up @@ -77,7 +77,7 @@ public void doCreation() {
}

watch.Stop();
GD.Print("handled packet, size is now " + creationQueue.Count + ", took " + watch.ElapsedMilliseconds);
// GD.Print("handled packet, size is now " + creationQueue.Count + ", took " + watch.ElapsedMilliseconds);
}
}

Expand Down
55 changes: 45 additions & 10 deletions scripts/world/block/BlockRegistry.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
namespace Godotcraft.scripts.world.block {
using System.Collections.Generic;
using System.IO;
using Godot;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using File = Godot.File;

namespace Godotcraft.scripts.world.block {
public class BlockRegistry {

public static double TotalNumberOfStates = 11336;

public static BlockState AIR = new BlockState();
public static BlockState DIRT = new BlockState();
public static BlockState AIR;
public static BlockState DIRT;

private static Dictionary<uint, BlockState> idToState = new Dictionary<uint, BlockState>();
private static Dictionary<BlockState, uint> stateToId = new Dictionary<BlockState, uint>();
private static Dictionary<string, BlockState> nameToState = new Dictionary<string, BlockState>();

static BlockRegistry() {
loadRegistry();

AIR = nameToState["air"];
DIRT = nameToState["dirt"];
}

private static void loadRegistry() {
File registryFile = new File();
registryFile.Open("user://mcdata/reports/blocks.json", File.ModeFlags.Read);
string content = registryFile.GetAsText();
JObject o = JObject.Parse(content);
foreach (var entry in o) {
string name = entry.Key.Split(':')[1];
JArray states = (entry.Value["states"]) as JArray;
foreach (var state in states) {
uint id = state["id"].Value<uint>();
add(new BlockState(name, id));
}
}
GD.Print($"Loaded {idToState.Count} states");
}

private static void add(BlockState state) {
idToState[state.id] = state;
stateToId[state] = state.id;
nameToState[state.name] = state;
}

public static BlockState GetStateFromGlobalPaletteID(uint id) {
return DIRT;
return idToState[id];
}

public static uint GetGlobalPaletteIDFromState(BlockState state) {
if (state == AIR) {
return 0;
}
else {
return 1;
}
return stateToId[state];
}
}
}
43 changes: 43 additions & 0 deletions scripts/world/block/BlockState.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
namespace Godotcraft.scripts.world.block {
public class BlockState {

public string name { get; }
public uint id { get; }

public bool transparent { get; }

public BlockState(string name, uint id) {
this.name = name;
this.id = id;

if (name.Contains("air") || name.Contains("glass")) {
transparent = true;
}
else {
transparent = false;
}
}

protected bool Equals(BlockState other) {
return name == other.name && id == other.id;
}

public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}

if (ReferenceEquals(this, obj)) {
return true;
}

if (obj.GetType() != this.GetType()) {
return false;
}

return Equals((BlockState) obj);
}

public override int GetHashCode() {
unchecked {
return ((name != null ? name.GetHashCode() : 0) * 397) ^ (int) id;
}
}
}
}
6 changes: 2 additions & 4 deletions scripts/world/palette/IndirectPalette.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using Godot;
using System.Collections.Generic;
using Godotcraft.scripts.network.protocol;
using Godotcraft.scripts.world.block;

Expand All @@ -20,7 +18,7 @@ public uint IdForState(BlockState state) {

public BlockState StateForId(uint id) {
if (id >= idToState.Count) {
GD.Print("Couldn't find state for id " + id + " (palette size is " + idToState.Count + ")");
// GD.Print("Couldn't find state for id " + id + " (palette size is " + idToState.Count + ")");
return BlockRegistry.AIR;
}
return idToState[id];
Expand Down

0 comments on commit 7871fb3

Please sign in to comment.