Create an index of interesting blocks

This commit is contained in:
Tanner Collin 2020-09-24 17:46:00 -06:00
parent 6b2df0c27e
commit b723143299
4 changed files with 37 additions and 1 deletions

View File

@ -214,6 +214,10 @@ LEAVES = [
'dark_oak_leaves', 'dark_oak_leaves',
] ]
INDEXED = [
'chest',
]
NON_SOLID_IDS = set([SINGLE_SNOW]) NON_SOLID_IDS = set([SINGLE_SNOW])
for block_name in NON_SOLID: for block_name in NON_SOLID:
@ -235,6 +239,11 @@ for block_name in LEAVES:
for state in JSON_BLOCKS['minecraft:' + block_name]['states']: for state in JSON_BLOCKS['minecraft:' + block_name]['states']:
LEAF_IDS.add(state['id']) LEAF_IDS.add(state['id'])
INDEXED_IDS = set()
for block_name in INDEXED:
for state in JSON_BLOCKS['minecraft:' + block_name]['states']:
INDEXED_IDS.add(state['id'])
def get(bid): def get(bid):
name = BLOCKS[bid] name = BLOCKS[bid]

View File

@ -367,6 +367,9 @@ class Game:
else: else:
reply = 'nothing open' reply = 'nothing open'
if command == 'loaded':
reply = str(self.g.chunks.get_loaded_area())
if reply: if reply:
print(reply) print(reply)
self.g.chat.send(reply) self.g.chat.send(reply)

View File

@ -40,6 +40,7 @@ class ChunksManager:
self.data = data_manager self.data = data_manager
self.chunks = {} self.chunks = {}
self.biomes = {} self.biomes = {}
self.index = {}
def handle_block(self, block_packet): def handle_block(self, block_packet):
self.set_block_at(block_packet.location.x, block_packet.location.y, block_packet.location.z, block_packet.block_state_id) self.set_block_at(block_packet.location.x, block_packet.location.y, block_packet.location.z, block_packet.block_state_id)
@ -53,7 +54,22 @@ class ChunksManager:
def handle_chunk(self, chunk_packet): def handle_chunk(self, chunk_packet):
for i in chunk_packet.chunks: for i in chunk_packet.chunks:
self.chunks[(chunk_packet.x, i, chunk_packet.z)] = chunk_packet.chunks[i] chunk = chunk_packet.chunks[i]
self.chunks[(chunk_packet.x, i, chunk_packet.z)] = chunk
if chunk.sub_index:
dx = chunk.x * 16
dy = chunk.y * 16
dz = chunk.z * 16
for item_id, locations in chunk.sub_index.items():
if item_id not in self.index:
self.index[item_id] = []
for l in locations:
coords = (dx + l%16, dy + l//256, dz + l%256//16)
self.index[item_id].append(coords)
self.biomes[(chunk_packet.x, None, chunk_packet.z)] = chunk_packet.biomes # FIXME self.biomes[(chunk_packet.x, None, chunk_packet.z)] = chunk_packet.biomes # FIXME
def register(self, connection): def register(self, connection):

View File

@ -10,6 +10,8 @@ from minecraft.networking.types import (
from protocol.types import Nbt, Slot, Entry from protocol.types import Nbt, Slot, Entry
import blocks
class ChunkDataPacket(Packet): class ChunkDataPacket(Packet):
id = 0x20 id = 0x20
@ -75,6 +77,7 @@ class Chunk:
self.z = z self.z = z
self.empty = empty self.empty = empty
self.entities = [] self.entities = []
self.sub_index = {}
def __repr__(self): def __repr__(self):
return 'Chunk(%r, %r, %r)' % (self.x, self.y, self.z) return 'Chunk(%r, %r, %r)' % (self.x, self.y, self.z)
@ -111,6 +114,11 @@ class Chunk:
n = self.palette[n] n = self.palette[n]
self.blocks.append(n) self.blocks.append(n)
if n in blocks.INDEXED_IDS:
if n not in self.sub_index:
self.sub_index[n] = []
self.sub_index[n].append(i)
def write_fields(self, packet_buffer): def write_fields(self, packet_buffer):
pass # TODO pass # TODO