2016-03-18 18:24:30 -04:00
|
|
|
// Copyright 2015 Matthew Collins
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-04-03 13:26:52 -04:00
|
|
|
pub use steven_blocks as block;
|
2016-03-18 18:24:30 -04:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
2016-04-03 10:08:36 -04:00
|
|
|
use std::collections::VecDeque;
|
2016-03-24 11:39:57 -04:00
|
|
|
use std::hash::BuildHasherDefault;
|
2016-04-03 15:53:40 -04:00
|
|
|
use types::{bit, nibble};
|
|
|
|
use shared::{Position, Direction};
|
2016-03-24 11:39:57 -04:00
|
|
|
use types::hash::FNVHash;
|
2016-03-21 10:05:13 -04:00
|
|
|
use protocol;
|
2016-03-24 17:13:24 -04:00
|
|
|
use render;
|
|
|
|
use collision;
|
|
|
|
use cgmath;
|
2016-03-24 19:27:22 -04:00
|
|
|
use chunk_builder;
|
2016-04-04 17:08:24 -04:00
|
|
|
use ecs;
|
|
|
|
use entity::block_entity;
|
|
|
|
use format;
|
2016-03-18 18:24:30 -04:00
|
|
|
|
2016-03-24 15:20:26 -04:00
|
|
|
pub mod biome;
|
2016-04-20 14:25:51 -04:00
|
|
|
mod storage;
|
2016-03-24 15:20:26 -04:00
|
|
|
|
2016-03-18 18:24:30 -04:00
|
|
|
pub struct World {
|
2016-03-24 11:39:57 -04:00
|
|
|
chunks: HashMap<CPos, Chunk, BuildHasherDefault<FNVHash>>,
|
2016-03-24 17:47:11 -04:00
|
|
|
|
|
|
|
render_list: Vec<(i32, i32, i32)>,
|
2016-04-03 10:08:36 -04:00
|
|
|
|
|
|
|
light_updates: VecDeque<LightUpdate>,
|
2016-04-04 17:08:24 -04:00
|
|
|
|
|
|
|
block_entity_actions: VecDeque<BlockEntityAction>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum BlockEntityAction {
|
|
|
|
Create(Position),
|
|
|
|
Remove(Position),
|
|
|
|
UpdateSignText(Position, format::Component, format::Component, format::Component, format::Component),
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
|
|
enum LightType {
|
|
|
|
Block,
|
|
|
|
Sky
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LightType {
|
2016-04-03 15:53:40 -04:00
|
|
|
fn get_light(self, world: &World, pos: Position) -> u8 {
|
2016-04-03 10:08:36 -04:00
|
|
|
match self {
|
2016-04-03 15:53:40 -04:00
|
|
|
LightType::Block => world.get_block_light(pos),
|
|
|
|
LightType::Sky => world.get_sky_light(pos),
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-03 15:53:40 -04:00
|
|
|
fn set_light(self, world: &mut World, pos: Position, light: u8) {
|
2016-04-03 10:08:36 -04:00
|
|
|
match self {
|
2016-04-03 15:53:40 -04:00
|
|
|
LightType::Block => world.set_block_light(pos, light),
|
|
|
|
LightType::Sky => world.set_sky_light(pos, light),
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LightUpdate {
|
|
|
|
ty: LightType,
|
2016-04-03 15:53:40 -04:00
|
|
|
pos: Position,
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl World {
|
|
|
|
pub fn new() -> World {
|
|
|
|
World {
|
2016-03-24 11:39:57 -04:00
|
|
|
chunks: HashMap::with_hasher(BuildHasherDefault::default()),
|
2016-03-24 17:47:11 -04:00
|
|
|
render_list: vec![],
|
2016-04-03 10:08:36 -04:00
|
|
|
light_updates: VecDeque::new(),
|
2016-04-04 17:08:24 -04:00
|
|
|
block_entity_actions: VecDeque::new(),
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:34:57 -04:00
|
|
|
pub fn is_chunk_loaded(&self, x: i32, z: i32) -> bool {
|
|
|
|
self.chunks.contains_key(&CPos(x, z))
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
pub fn set_block(&mut self, pos: Position, b: block::Block) {
|
|
|
|
if self.set_block_raw(pos, b) {
|
|
|
|
self.update_block(pos);
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
2016-03-26 07:40:53 -04:00
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
fn set_block_raw(&mut self, pos: Position, b: block::Block) -> bool {
|
|
|
|
let cpos = CPos(pos.x >> 4, pos.z >> 4);
|
2016-03-26 10:24:26 -04:00
|
|
|
let chunk = self.chunks.entry(cpos).or_insert_with(|| Chunk::new(cpos));
|
2016-04-04 17:08:24 -04:00
|
|
|
if chunk.set_block(pos.x & 0xF, pos.y, pos.z & 0xF, b) {
|
|
|
|
if chunk.block_entities.contains_key(&pos) {
|
|
|
|
self.block_entity_actions.push_back(BlockEntityAction::Remove(pos));
|
|
|
|
}
|
|
|
|
if block_entity::BlockEntityType::get_block_entity(b).is_some() {
|
|
|
|
self.block_entity_actions.push_back(BlockEntityAction::Create(pos));
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
pub fn update_block(&mut self, pos: Position) {
|
2016-03-26 07:40:53 -04:00
|
|
|
for yy in -1 .. 2 {
|
|
|
|
for zz in -1 .. 2 {
|
|
|
|
for xx in -1 .. 2 {
|
2016-04-03 15:53:40 -04:00
|
|
|
let bp = pos + (xx, yy, zz);
|
|
|
|
let current = self.get_block(bp);
|
|
|
|
let new = current.update_state(self, bp);
|
2016-03-26 07:40:53 -04:00
|
|
|
if current != new {
|
2016-04-03 15:53:40 -04:00
|
|
|
self.set_block_raw(bp, new);
|
2016-03-26 07:40:53 -04:00
|
|
|
}
|
2016-04-03 15:53:40 -04:00
|
|
|
self.set_dirty(bp.x >> 4, bp.y >> 4, bp.z >> 4);
|
|
|
|
self.update_light(bp, LightType::Block);
|
|
|
|
self.update_light(bp, LightType::Sky);
|
2016-03-26 07:40:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_range(&mut self, x1: i32, y1: i32, z1: i32, x2: i32, y2: i32, z2: i32) {
|
|
|
|
for by in y1 .. y2 {
|
|
|
|
for bz in z1 .. z2 {
|
|
|
|
for bx in x1 .. x2 {
|
2016-04-03 15:53:40 -04:00
|
|
|
let bp = Position::new(bx, by, bz);
|
|
|
|
let current = self.get_block(bp);
|
|
|
|
let new = current.update_state(self, bp);
|
2016-04-05 16:41:39 -04:00
|
|
|
let sky_light = self.get_sky_light(bp);
|
|
|
|
let block_light = self.get_block_light(bp);
|
2016-03-26 07:40:53 -04:00
|
|
|
if current != new {
|
2016-04-03 15:53:40 -04:00
|
|
|
self.set_block_raw(bp, new);
|
2016-04-05 16:41:39 -04:00
|
|
|
// Restore old lighting
|
|
|
|
self.set_sky_light(bp, sky_light);
|
|
|
|
self.set_block_light(bp, block_light);
|
2016-03-26 07:40:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
pub fn get_block(&self, pos: Position) -> block::Block {
|
|
|
|
match self.chunks.get(&CPos(pos.x >> 4, pos.z >> 4)) {
|
|
|
|
Some(ref chunk) => chunk.get_block(pos.x & 0xF, pos.y, pos.z & 0xF),
|
2016-03-23 17:07:49 -04:00
|
|
|
None => block::Missing{},
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-19 12:32:13 -04:00
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
fn set_block_light(&mut self, pos: Position, light: u8) {
|
|
|
|
let cpos = CPos(pos.x >> 4, pos.z >> 4);
|
2016-04-03 10:08:36 -04:00
|
|
|
let chunk = self.chunks.entry(cpos).or_insert_with(|| Chunk::new(cpos));
|
2016-04-03 15:53:40 -04:00
|
|
|
chunk.set_block_light(pos.x & 0xF, pos.y, pos.z & 0xF, light);
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:08:24 -04:00
|
|
|
pub fn get_block_light(&self, pos: Position) -> u8 {
|
2016-04-03 15:53:40 -04:00
|
|
|
match self.chunks.get(&CPos(pos.x >> 4, pos.z >> 4)) {
|
|
|
|
Some(ref chunk) => chunk.get_block_light(pos.x & 0xF, pos.y, pos.z & 0xF),
|
2016-04-03 10:08:36 -04:00
|
|
|
None => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
fn set_sky_light(&mut self, pos: Position, light: u8) {
|
|
|
|
let cpos = CPos(pos.x >> 4, pos.z >> 4);
|
2016-04-03 10:08:36 -04:00
|
|
|
let chunk = self.chunks.entry(cpos).or_insert_with(|| Chunk::new(cpos));
|
2016-04-03 15:53:40 -04:00
|
|
|
chunk.set_sky_light(pos.x & 0xF, pos.y, pos.z & 0xF, light);
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
|
2016-04-04 17:08:24 -04:00
|
|
|
pub fn get_sky_light(&self, pos: Position) -> u8 {
|
2016-04-03 15:53:40 -04:00
|
|
|
match self.chunks.get(&CPos(pos.x >> 4, pos.z >> 4)) {
|
|
|
|
Some(ref chunk) => chunk.get_sky_light(pos.x & 0xF, pos.y, pos.z & 0xF),
|
2016-04-03 10:08:36 -04:00
|
|
|
None => 15,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
fn update_light(&mut self, pos: Position, ty: LightType) {
|
2016-04-03 10:08:36 -04:00
|
|
|
self.light_updates.push_back(LightUpdate {
|
|
|
|
ty: ty,
|
2016-04-03 15:53:40 -04:00
|
|
|
pos: pos,
|
2016-04-03 10:08:36 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-04 17:08:24 -04:00
|
|
|
pub fn add_block_entity_action(&mut self, action: BlockEntityAction) {
|
|
|
|
self.block_entity_actions.push_back(action);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self, m: &mut ecs::Manager) {
|
2016-04-03 10:08:36 -04:00
|
|
|
use time;
|
|
|
|
let start = time::precise_time_ns();
|
|
|
|
let mut updates_performed = 0;
|
|
|
|
while !self.light_updates.is_empty() {
|
|
|
|
updates_performed += 1;
|
|
|
|
self.do_light_update();
|
|
|
|
if updates_performed & 0xFFF == 0 {
|
|
|
|
let now = time::precise_time_ns();
|
|
|
|
if (now - start) >= 5000000 { // 5 ms for light updates
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 17:08:24 -04:00
|
|
|
|
|
|
|
let sign_info: ecs::Key<block_entity::sign::SignInfo> = m.get_key();
|
|
|
|
|
|
|
|
while let Some(action) = self.block_entity_actions.pop_front() {
|
|
|
|
match action {
|
|
|
|
BlockEntityAction::Remove(pos) => {
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(pos.x >> 4, pos.z >> 4)) {
|
|
|
|
if let Some(entity) = chunk.block_entities.remove(&pos) {
|
|
|
|
m.remove_entity(entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BlockEntityAction::Create(pos) => {
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(pos.x >> 4, pos.z >> 4)) {
|
|
|
|
// Remove existing entity
|
|
|
|
if let Some(entity) = chunk.block_entities.remove(&pos) {
|
|
|
|
m.remove_entity(entity);
|
|
|
|
}
|
|
|
|
let block = chunk.get_block(pos.x & 0xF, pos.y, pos.z & 0xF);
|
|
|
|
if let Some(entity_type) = block_entity::BlockEntityType::get_block_entity(block) {
|
|
|
|
let entity = entity_type.create_entity(m, pos);
|
|
|
|
chunk.block_entities.insert(pos, entity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
BlockEntityAction::UpdateSignText(pos, line1, line2, line3, line4) => {
|
|
|
|
if let Some(chunk) = self.chunks.get(&CPos(pos.x >> 4, pos.z >> 4)) {
|
|
|
|
if let Some(entity) = chunk.block_entities.get(&pos) {
|
|
|
|
if let Some(sign) = m.get_component_mut(*entity, sign_info) {
|
|
|
|
sign.lines = [
|
|
|
|
line1,
|
|
|
|
line2,
|
|
|
|
line3,
|
|
|
|
line4,
|
|
|
|
];
|
|
|
|
sign.dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn do_light_update(&mut self) {
|
|
|
|
use std::cmp;
|
|
|
|
if let Some(update) = self.light_updates.pop_front() {
|
2016-04-03 15:53:40 -04:00
|
|
|
if update.pos.y < 0 || update.pos.y > 255 || !self.is_chunk_loaded(update.pos.x >> 4, update.pos.z >> 4) {
|
2016-04-03 10:08:36 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:53:40 -04:00
|
|
|
let block = self.get_block(update.pos).get_material();
|
2016-04-03 10:08:36 -04:00
|
|
|
// Find the brightest source of light nearby
|
2016-04-03 15:53:40 -04:00
|
|
|
let mut best = update.ty.get_light(self, update.pos);
|
2016-04-03 10:08:36 -04:00
|
|
|
let old = best;
|
|
|
|
for dir in Direction::all() {
|
2016-04-03 15:53:40 -04:00
|
|
|
let light = update.ty.get_light(self, update.pos.shift(dir));
|
2016-04-03 10:08:36 -04:00
|
|
|
if light > best {
|
|
|
|
best = light;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
best = best.saturating_sub(cmp::max(1, block.absorbed_light));
|
|
|
|
// If the light from the block itself is brighter than the light passing through
|
|
|
|
// it use that.
|
|
|
|
if update.ty == LightType::Block && block.emitted_light != 0 {
|
|
|
|
best = cmp::max(best, block.emitted_light);
|
|
|
|
}
|
|
|
|
// Sky light doesn't decrease when going down at full brightness
|
|
|
|
if update.ty == LightType::Sky
|
|
|
|
&& block.absorbed_light == 0
|
2016-04-03 15:53:40 -04:00
|
|
|
&& update.ty.get_light(self, update.pos.shift(Direction::Up)) == 15 {
|
2016-04-03 10:08:36 -04:00
|
|
|
best = 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do, we are already at the right value
|
|
|
|
if best == old {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Use our new light value
|
2016-04-03 15:53:40 -04:00
|
|
|
update.ty.set_light(self, update.pos, best);
|
2016-04-03 10:08:36 -04:00
|
|
|
// Flag surrounding chunks as dirty
|
|
|
|
for yy in -1 .. 2 {
|
|
|
|
for zz in -1 .. 2 {
|
|
|
|
for xx in -1 .. 2 {
|
2016-04-03 15:53:40 -04:00
|
|
|
let bp = update.pos + (xx, yy, zz);
|
|
|
|
self.set_dirty(bp.x >> 4, bp.y >> 4, bp.z >> 4);
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update surrounding blocks
|
|
|
|
for dir in Direction::all() {
|
2016-04-03 15:53:40 -04:00
|
|
|
self.update_light(update.pos.shift(dir), update.ty);
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 15:00:13 -04:00
|
|
|
pub fn copy_cloud_heightmap(&mut self, data: &mut [u8]) -> bool {
|
|
|
|
let mut dirty = false;
|
|
|
|
for (_, c) in &mut self.chunks {
|
|
|
|
if c.heightmap_dirty {
|
|
|
|
dirty = true;
|
|
|
|
c.heightmap_dirty = false;
|
|
|
|
for xx in 0 .. 16 {
|
|
|
|
for zz in 0 .. 16 {
|
|
|
|
data[
|
|
|
|
(((c.position.0 << 4) as usize + xx) & 0x1FF) +
|
|
|
|
((((c.position.1 << 4) as usize + zz) & 0x1FF) << 9)
|
|
|
|
] = c.heightmap[(zz << 4) | xx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dirty
|
|
|
|
}
|
|
|
|
|
2016-03-24 17:47:11 -04:00
|
|
|
pub fn compute_render_list(&mut self, renderer: &mut render::Renderer) {
|
2016-03-26 07:46:37 -04:00
|
|
|
use chunk_builder;
|
2016-03-24 19:27:22 -04:00
|
|
|
use cgmath::Vector;
|
|
|
|
use std::collections::VecDeque;
|
2016-03-24 17:47:11 -04:00
|
|
|
self.render_list.clear();
|
2016-03-24 19:27:22 -04:00
|
|
|
|
|
|
|
let mut valid_dirs = [false; 6];
|
|
|
|
for dir in Direction::all() {
|
|
|
|
let (ox, oy, oz) = dir.get_offset();
|
|
|
|
let dir_vec = cgmath::Vector3::new(ox as f32, oy as f32, oz as f32);
|
2016-04-03 18:00:00 -04:00
|
|
|
valid_dirs[dir.index()] = renderer.view_vector.dot(dir_vec) > -0.9;
|
2016-03-24 19:27:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let start = (
|
|
|
|
((renderer.camera.pos.x as i32) >> 4),
|
|
|
|
((renderer.camera.pos.y as i32) >> 4),
|
|
|
|
((renderer.camera.pos.z as i32) >> 4)
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut process_queue = VecDeque::with_capacity(self.chunks.len() * 16);
|
|
|
|
process_queue.push_front((Direction::Invalid, start));
|
|
|
|
|
|
|
|
while let Some((from, pos)) = process_queue.pop_front() {
|
|
|
|
let (exists, cull) = if let Some((sec, rendered_on)) = self.get_render_section_mut(pos.0, pos.1, pos.2) {
|
|
|
|
if *rendered_on == renderer.frame_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*rendered_on = renderer.frame_id;
|
|
|
|
|
|
|
|
let min = cgmath::Point3::new(pos.0 as f32 * 16.0, -pos.1 as f32 * 16.0, pos.2 as f32 * 16.0);
|
|
|
|
let bounds = collision::Aabb3::new(min, min + cgmath::Vector3::new(16.0, -16.0, 16.0));
|
2016-03-27 19:18:55 -04:00
|
|
|
if renderer.frustum.contains(bounds) == collision::Relation::Out && from != Direction::Invalid {
|
2016-03-24 19:27:22 -04:00
|
|
|
continue;
|
|
|
|
}
|
2016-03-26 10:24:26 -04:00
|
|
|
(sec.is_some(), sec.map_or(chunk_builder::CullInfo::all_vis(), |v| v.cull_info))
|
2016-03-24 19:27:22 -04:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
self.render_list.push(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
for dir in Direction::all() {
|
|
|
|
let (ox, oy, oz) = dir.get_offset();
|
|
|
|
let opos = (pos.0 + ox, pos.1 + oy, pos.2 + oz);
|
|
|
|
if let Some((_, rendered_on)) = self.get_render_section_mut(opos.0, opos.1, opos.2) {
|
|
|
|
if *rendered_on == renderer.frame_id {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if from == Direction::Invalid || (valid_dirs[dir.index()] && cull.is_visible(from, dir)) {
|
|
|
|
process_queue.push_back((dir.opposite(), opos));
|
2016-03-24 17:13:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 17:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_render_list(&self) -> Vec<((i32, i32, i32), &render::ChunkBuffer)> {
|
|
|
|
self.render_list.iter().map(|v| {
|
|
|
|
let chunk = self.chunks.get(&CPos(v.0, v.2)).unwrap();
|
|
|
|
let sec = chunk.sections[v.1 as usize].as_ref().unwrap();
|
|
|
|
(*v, &sec.render_buffer)
|
|
|
|
}).collect()
|
2016-03-24 17:13:24 -04:00
|
|
|
}
|
|
|
|
|
2016-03-24 19:27:22 -04:00
|
|
|
pub fn get_section_mut(&mut self, x: i32, y: i32, z: i32) -> Option<&mut Section> {
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(x, z)) {
|
|
|
|
if let Some(sec) = chunk.sections[y as usize].as_mut() {
|
|
|
|
return Some(sec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_render_section_mut(&mut self, x: i32, y: i32, z: i32) -> Option<(Option<&mut Section>, &mut u32)> {
|
|
|
|
if y < 0 || y > 15 {
|
|
|
|
return None;
|
|
|
|
}
|
2016-03-24 17:13:24 -04:00
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(x, z)) {
|
2016-03-24 19:27:22 -04:00
|
|
|
let rendered = &mut chunk.sections_rendered_on[y as usize];
|
2016-03-24 17:13:24 -04:00
|
|
|
if let Some(sec) = chunk.sections[y as usize].as_mut() {
|
2016-03-24 19:27:22 -04:00
|
|
|
return Some((Some(sec), rendered));
|
2016-03-24 17:13:24 -04:00
|
|
|
}
|
2016-03-24 19:27:22 -04:00
|
|
|
return Some((None, rendered));
|
2016-03-24 17:13:24 -04:00
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_dirty_chunk_sections(&mut self) -> Vec<(i32, i32, i32)> {
|
2016-03-21 12:51:19 -04:00
|
|
|
let mut out = vec![];
|
2016-03-19 12:32:13 -04:00
|
|
|
for (_, chunk) in &mut self.chunks {
|
|
|
|
for sec in &mut chunk.sections {
|
|
|
|
if let Some(sec) = sec.as_mut() {
|
|
|
|
if !sec.building && sec.dirty {
|
2016-03-24 17:13:24 -04:00
|
|
|
out.push((chunk.position.0, sec.y as i32, chunk.position.1));
|
2016-03-19 12:32:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 12:51:19 -04:00
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2016-04-01 16:13:31 -04:00
|
|
|
fn set_dirty(&mut self, x: i32, y: i32, z: i32) {
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(x, z)) {
|
|
|
|
if let Some(sec) = chunk.sections.get_mut(y as usize).and_then(|v| v.as_mut()) {
|
|
|
|
sec.dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 19:27:22 -04:00
|
|
|
pub fn is_section_dirty(&self, pos: (i32, i32, i32)) -> bool {
|
|
|
|
if let Some(chunk) = self.chunks.get(&CPos(pos.0, pos.2)) {
|
|
|
|
if let Some(sec) = chunk.sections[pos.1 as usize].as_ref() {
|
|
|
|
return sec.dirty && !sec.building;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2016-03-21 12:51:19 -04:00
|
|
|
pub fn set_building_flag(&mut self, pos: (i32, i32, i32)) {
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(pos.0, pos.2)) {
|
|
|
|
if let Some(sec) = chunk.sections[pos.1 as usize].as_mut() {
|
|
|
|
sec.building = true;
|
|
|
|
sec.dirty = false;
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 12:32:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset_building_flag(&mut self, pos: (i32, i32, i32)) {
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&CPos(pos.0, pos.2)) {
|
|
|
|
if let Some(section) = chunk.sections[pos.1 as usize].as_mut() {
|
|
|
|
section.building = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 13:34:12 -04:00
|
|
|
pub fn flag_dirty_all(&mut self) {
|
|
|
|
for (_, chunk) in &mut self.chunks {
|
|
|
|
for sec in &mut chunk.sections {
|
|
|
|
if let Some(sec) = sec.as_mut() {
|
|
|
|
sec.dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 12:32:13 -04:00
|
|
|
pub fn capture_snapshot(&self, x: i32, y: i32, z: i32, w: i32, h: i32, d: i32) -> Snapshot {
|
|
|
|
use std::cmp::{min, max};
|
|
|
|
let mut snapshot = Snapshot {
|
2016-04-20 15:07:39 -04:00
|
|
|
blocks: storage::BlockStorage::new_default((w * h * d) as usize, block::Missing{}),
|
2016-03-19 12:32:13 -04:00
|
|
|
block_light: nibble::Array::new((w * h * d) as usize),
|
|
|
|
sky_light: nibble::Array::new((w * h * d) as usize),
|
|
|
|
biomes: vec![0; (w * d) as usize],
|
|
|
|
|
|
|
|
x: x, y: y, z: z,
|
2016-03-26 06:19:16 -04:00
|
|
|
w: w, _h: h, d: d,
|
2016-03-19 12:32:13 -04:00
|
|
|
};
|
|
|
|
for i in 0 .. (w * h * d) as usize {
|
|
|
|
snapshot.sky_light.set(i, 0xF);
|
|
|
|
}
|
|
|
|
|
|
|
|
let cx1 = x >> 4;
|
|
|
|
let cy1 = y >> 4;
|
|
|
|
let cz1 = z >> 4;
|
|
|
|
let cx2 = (x + w + 15) >> 4;
|
|
|
|
let cy2 = (y + h + 15) >> 4;
|
|
|
|
let cz2 = (z + d + 15) >> 4;
|
|
|
|
|
|
|
|
for cx in cx1 .. cx2 {
|
|
|
|
for cz in cz1 .. cz2 {
|
|
|
|
let chunk = match self.chunks.get(&CPos(cx, cz)) {
|
|
|
|
Some(val) => val,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let x1 = min(16, max(0, x - (cx<<4)));
|
|
|
|
let x2 = min(16, max(0, x + w - (cx<<4)));
|
|
|
|
let z1 = min(16, max(0, z - (cz<<4)));
|
|
|
|
let z2 = min(16, max(0, z + d - (cz<<4)));
|
|
|
|
|
|
|
|
for cy in cy1 .. cy2 {
|
|
|
|
if cy < 0 || cy > 15 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let section = &chunk.sections[cy as usize];
|
|
|
|
let y1 = min(16, max(0, y - (cy<<4)));
|
|
|
|
let y2 = min(16, max(0, y + h - (cy<<4)));
|
|
|
|
|
|
|
|
for yy in y1 .. y2 {
|
|
|
|
for zz in z1 .. z2 {
|
|
|
|
for xx in x1 .. x2 {
|
|
|
|
let ox = xx + (cx << 4);
|
|
|
|
let oy = yy + (cy << 4);
|
|
|
|
let oz = zz + (cz << 4);
|
|
|
|
match section.as_ref() {
|
|
|
|
Some(sec) => {
|
|
|
|
snapshot.set_block(ox, oy, oz, sec.get_block(xx, yy, zz));
|
|
|
|
snapshot.set_block_light(ox, oy, oz, sec.get_block_light(xx, yy, zz));
|
|
|
|
snapshot.set_sky_light(ox, oy, oz, sec.get_sky_light(xx, yy, zz));
|
|
|
|
},
|
|
|
|
None => {
|
2016-03-23 17:07:49 -04:00
|
|
|
snapshot.set_block(ox, oy, oz, block::Air{});
|
2016-03-19 12:32:13 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-24 15:20:26 -04:00
|
|
|
for zz in z1 .. z2 {
|
|
|
|
for xx in x1 .. x2 {
|
|
|
|
let ox = xx + (cx << 4);
|
|
|
|
let oz = zz + (cz << 4);
|
|
|
|
snapshot.set_biome(ox, oz, chunk.get_biome(xx, zz));
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 12:32:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot
|
|
|
|
}
|
2016-03-21 10:05:13 -04:00
|
|
|
|
2016-04-04 17:08:24 -04:00
|
|
|
pub fn unload_chunk(&mut self, x: i32, z: i32, m: &mut ecs::Manager) {
|
|
|
|
if let Some(chunk) = self.chunks.remove(&CPos(x, z)) {
|
|
|
|
for entity in chunk.block_entities.values() {
|
|
|
|
m.remove_entity(*entity);
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 18:34:57 -04:00
|
|
|
}
|
|
|
|
|
2016-03-21 10:05:13 -04:00
|
|
|
pub fn load_chunk(&mut self, x: i32, z: i32, new: bool, mask: u16, data: Vec<u8>) -> Result<(), protocol::Error> {
|
|
|
|
use std::io::{Cursor, Read};
|
|
|
|
use byteorder::ReadBytesExt;
|
|
|
|
use protocol::{VarInt, Serializable, LenPrefixed};
|
|
|
|
|
|
|
|
let mut data = Cursor::new(data);
|
|
|
|
|
|
|
|
let cpos = CPos(x, z);
|
2016-03-21 12:36:50 -04:00
|
|
|
{
|
|
|
|
let chunk = if new {
|
|
|
|
self.chunks.insert(cpos, Chunk::new(cpos));
|
|
|
|
self.chunks.get_mut(&cpos).unwrap()
|
|
|
|
} else {
|
|
|
|
if !self.chunks.contains_key(&cpos) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
self.chunks.get_mut(&cpos).unwrap()
|
|
|
|
};
|
2016-03-21 10:05:13 -04:00
|
|
|
|
2016-03-21 12:36:50 -04:00
|
|
|
for i in 0 .. 16 {
|
2016-04-03 10:08:36 -04:00
|
|
|
if chunk.sections[i].is_none() {
|
|
|
|
let mut fill_sky = chunk.sections.iter()
|
|
|
|
.skip(i)
|
|
|
|
.all(|v| v.is_none());
|
|
|
|
fill_sky &= (mask & !((1 << i) | ((1 << i) - 1))) == 0;
|
|
|
|
if !fill_sky || mask & (1 << i) != 0 {
|
|
|
|
chunk.sections[i] = Some(Section::new(i as u8, fill_sky));
|
|
|
|
}
|
|
|
|
}
|
2016-03-21 12:36:50 -04:00
|
|
|
if mask & (1 << i) == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let section = chunk.sections[i as usize].as_mut().unwrap();
|
|
|
|
section.dirty = true;
|
|
|
|
|
2016-03-25 16:56:45 -04:00
|
|
|
let mut bit_size = try!(data.read_u8());
|
2016-04-20 14:25:51 -04:00
|
|
|
section.blocks.clear();
|
2016-04-02 20:26:31 -04:00
|
|
|
if bit_size == 0 {
|
|
|
|
bit_size = 13;
|
2016-04-03 07:58:53 -04:00
|
|
|
} else {
|
2016-03-21 12:36:50 -04:00
|
|
|
let count = try!(VarInt::read_from(&mut data)).0;
|
|
|
|
for i in 0 .. count {
|
|
|
|
let id = try!(VarInt::read_from(&mut data)).0;
|
2016-04-04 07:37:21 -04:00
|
|
|
let bl = block::Block::by_vanilla_id(id as usize);
|
2016-04-20 14:25:51 -04:00
|
|
|
section.blocks.force_mapping(i as usize, bl);
|
2016-03-21 12:36:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let bits = try!(LenPrefixed::<VarInt, u64>::read_from(&mut data)).data;
|
|
|
|
let m = bit::Map::from_raw(bits, bit_size as usize);
|
|
|
|
|
2016-04-20 14:25:51 -04:00
|
|
|
section.blocks.use_raw(m);
|
|
|
|
|
|
|
|
// Spawn block entities
|
2016-04-04 17:08:24 -04:00
|
|
|
for bi in 0 .. 4096 {
|
2016-04-20 14:25:51 -04:00
|
|
|
let b = section.blocks.get(bi);
|
|
|
|
if block_entity::BlockEntityType::get_block_entity(b).is_some() {
|
2016-04-04 17:08:24 -04:00
|
|
|
let pos = Position::new(
|
|
|
|
(bi & 0xF) as i32,
|
|
|
|
(bi >> 8) as i32,
|
|
|
|
((bi >> 4) & 0xF) as i32
|
|
|
|
) + (chunk.position.0 << 4, (i << 4) as i32, chunk.position.1 << 4);
|
|
|
|
if chunk.block_entities.contains_key(&pos) {
|
|
|
|
self.block_entity_actions.push_back(BlockEntityAction::Remove(pos))
|
|
|
|
}
|
|
|
|
self.block_entity_actions.push_back(BlockEntityAction::Create(pos))
|
|
|
|
}
|
2016-03-21 12:36:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
try!(data.read_exact(&mut section.block_light.data));
|
|
|
|
try!(data.read_exact(&mut section.sky_light.data));
|
|
|
|
}
|
2016-03-24 15:20:26 -04:00
|
|
|
|
|
|
|
if new {
|
|
|
|
try!(data.read_exact(&mut chunk.biomes));
|
|
|
|
}
|
2016-04-01 15:00:13 -04:00
|
|
|
|
|
|
|
chunk.calculate_heightmap();
|
2016-03-21 12:36:50 -04:00
|
|
|
}
|
2016-03-24 15:20:26 -04:00
|
|
|
|
2016-03-21 10:05:13 -04:00
|
|
|
for i in 0 .. 16 {
|
|
|
|
if mask & (1 << i) == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
2016-03-21 12:36:50 -04:00
|
|
|
for pos in [
|
|
|
|
(-1, 0, 0), (1, 0, 0),
|
|
|
|
(0, -1, 0), (0, 1, 0),
|
|
|
|
(0, 0, -1), (0, 0, 1)].into_iter() {
|
|
|
|
self.flag_section_dirty(x + pos.0, i as i32 + pos.1, z + pos.2);
|
2016-03-21 10:05:13 -04:00
|
|
|
}
|
2016-03-26 07:40:53 -04:00
|
|
|
self.update_range(
|
|
|
|
(x<<4) - 1, (i<<4) - 1, (z<<4) - 1,
|
|
|
|
(x<<4) + 17, (i<<4) + 17, (z<<4) + 17
|
|
|
|
);
|
2016-03-21 12:36:50 -04:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2016-03-21 10:05:13 -04:00
|
|
|
|
2016-03-21 12:36:50 -04:00
|
|
|
fn flag_section_dirty(&mut self, x: i32, y: i32, z: i32) {
|
|
|
|
if y < 0 || y > 15 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let cpos = CPos(x, z);
|
|
|
|
if let Some(chunk) = self.chunks.get_mut(&cpos) {
|
|
|
|
if let Some(sec) = chunk.sections[y as usize].as_mut() {
|
|
|
|
sec.dirty = true;
|
2016-03-21 10:05:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 12:32:13 -04:00
|
|
|
}
|
|
|
|
|
2016-04-03 13:26:52 -04:00
|
|
|
impl block::WorldAccess for World {
|
2016-04-03 15:53:40 -04:00
|
|
|
fn get_block(&self, pos: Position) -> block::Block {
|
|
|
|
World::get_block(self, pos)
|
2016-04-03 13:26:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 12:32:13 -04:00
|
|
|
pub struct Snapshot {
|
2016-04-20 15:07:39 -04:00
|
|
|
blocks: storage::BlockStorage,
|
2016-03-19 12:32:13 -04:00
|
|
|
block_light: nibble::Array,
|
|
|
|
sky_light: nibble::Array,
|
|
|
|
biomes: Vec<u8>,
|
|
|
|
|
|
|
|
x: i32,
|
|
|
|
y: i32,
|
|
|
|
z: i32,
|
|
|
|
w: i32,
|
2016-03-26 06:19:16 -04:00
|
|
|
_h: i32,
|
2016-03-19 12:32:13 -04:00
|
|
|
d: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Snapshot {
|
|
|
|
|
|
|
|
pub fn make_relative(&mut self, x: i32, y: i32, z: i32) {
|
|
|
|
self.x = x;
|
|
|
|
self.y = y;
|
|
|
|
self.z = z;
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:07:49 -04:00
|
|
|
pub fn get_block(&self, x: i32, y: i32, z: i32) -> block::Block {
|
2016-04-20 15:07:39 -04:00
|
|
|
self.blocks.get(self.index(x, y, z))
|
2016-03-19 12:32:13 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:07:49 -04:00
|
|
|
pub fn set_block(&mut self, x: i32, y: i32, z: i32, b: block::Block) {
|
2016-03-19 12:32:13 -04:00
|
|
|
let idx = self.index(x, y, z);
|
2016-04-20 15:07:39 -04:00
|
|
|
self.blocks.set(idx, b);
|
2016-03-19 12:32:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_block_light(&self, x: i32, y: i32, z: i32) -> u8 {
|
|
|
|
self.block_light.get(self.index(x, y, z))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_block_light(&mut self, x: i32, y: i32, z: i32, l: u8) {
|
|
|
|
let idx = self.index(x, y, z);
|
|
|
|
self.block_light.set(idx, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_sky_light(&self, x: i32, y: i32, z: i32) -> u8 {
|
|
|
|
self.sky_light.get(self.index(x, y, z))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_sky_light(&mut self, x: i32, y: i32, z: i32, l: u8) {
|
|
|
|
let idx = self.index(x, y, z);
|
|
|
|
self.sky_light.set(idx, l);
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:20:26 -04:00
|
|
|
pub fn get_biome(&self, x: i32, z: i32) -> biome::Biome {
|
2016-04-04 10:05:24 -04:00
|
|
|
biome::Biome::by_id(self.biomes[((x - self.x) + ((z - self.z) * self.w)) as usize] as usize)
|
2016-03-24 15:20:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_biome(&mut self, x: i32, z: i32, b: biome::Biome) {
|
2016-04-04 10:05:24 -04:00
|
|
|
self.biomes[((x - self.x) + ((z - self.z) * self.w)) as usize] = b.id as u8;
|
2016-03-24 15:20:26 -04:00
|
|
|
}
|
|
|
|
|
2016-03-19 12:32:13 -04:00
|
|
|
#[inline]
|
|
|
|
fn index(&self, x: i32, y: i32, z: i32) -> usize {
|
|
|
|
((x - self.x) + ((z - self.z) * self.w) + ((y - self.y) * self.w * self.d)) as usize
|
|
|
|
}
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
|
|
|
|
pub struct CPos(i32, i32);
|
|
|
|
|
|
|
|
pub struct Chunk {
|
|
|
|
position: CPos,
|
|
|
|
|
|
|
|
sections: [Option<Section>; 16],
|
2016-03-24 19:27:22 -04:00
|
|
|
sections_rendered_on: [u32; 16],
|
2016-03-18 18:24:30 -04:00
|
|
|
biomes: [u8; 16 * 16],
|
2016-04-01 15:00:13 -04:00
|
|
|
|
|
|
|
heightmap: [u8; 16 * 16],
|
|
|
|
heightmap_dirty: bool,
|
2016-04-04 17:08:24 -04:00
|
|
|
|
|
|
|
block_entities: HashMap<Position, ecs::Entity, BuildHasherDefault<FNVHash>>,
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Chunk {
|
|
|
|
fn new(pos: CPos) -> Chunk {
|
|
|
|
Chunk {
|
|
|
|
position: pos,
|
|
|
|
sections: [
|
|
|
|
None,None,None,None,
|
|
|
|
None,None,None,None,
|
|
|
|
None,None,None,None,
|
|
|
|
None,None,None,None,
|
|
|
|
],
|
2016-03-24 19:27:22 -04:00
|
|
|
sections_rendered_on: [0; 16],
|
2016-03-18 18:24:30 -04:00
|
|
|
biomes: [0; 16 * 16],
|
2016-04-01 15:00:13 -04:00
|
|
|
heightmap: [0; 16 * 16],
|
|
|
|
heightmap_dirty: true,
|
2016-04-04 17:08:24 -04:00
|
|
|
block_entities: HashMap::with_hasher(BuildHasherDefault::default()),
|
2016-04-01 15:00:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calculate_heightmap(&mut self) {
|
|
|
|
for x in 0 .. 16 {
|
|
|
|
for z in 0 .. 16 {
|
|
|
|
let idx = ((z<<4)|x) as usize;
|
|
|
|
for yy in 0 .. 256 {
|
|
|
|
let sy = 255 - yy;
|
2016-04-03 07:58:53 -04:00
|
|
|
if let block::Air{..} = self.get_block(x, sy, z) {
|
2016-04-02 20:26:31 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-04-01 15:00:13 -04:00
|
|
|
self.heightmap[idx] = sy as u8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
2016-04-01 15:00:13 -04:00
|
|
|
self.heightmap_dirty = true;
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
2016-04-03 10:08:36 -04:00
|
|
|
fn set_block(&mut self, x: i32, y: i32, z: i32, b: block::Block) -> bool {
|
2016-04-03 10:42:54 -04:00
|
|
|
let s_idx = y >> 4;
|
2016-03-18 18:24:30 -04:00
|
|
|
if s_idx < 0 || s_idx > 15 {
|
2016-04-03 10:08:36 -04:00
|
|
|
return false;
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
2016-04-03 10:42:54 -04:00
|
|
|
let s_idx = s_idx as usize;
|
2016-04-03 10:08:36 -04:00
|
|
|
if self.sections[s_idx].is_none() {
|
2016-03-23 17:07:49 -04:00
|
|
|
if let block::Air {} = b {
|
2016-04-03 10:08:36 -04:00
|
|
|
return false;
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
2016-04-03 10:08:36 -04:00
|
|
|
let fill_sky = self.sections.iter()
|
|
|
|
.skip(s_idx)
|
|
|
|
.all(|v| v.is_none());
|
|
|
|
self.sections[s_idx] = Some(Section::new(s_idx as u8, fill_sky));
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
2016-04-01 15:00:13 -04:00
|
|
|
{
|
|
|
|
let section = self.sections[s_idx as usize].as_mut().unwrap();
|
2016-04-03 10:08:36 -04:00
|
|
|
if !section.set_block(x, y & 0xF, z, b) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-01 15:00:13 -04:00
|
|
|
}
|
|
|
|
let idx = ((z<<4)|x) as usize;
|
|
|
|
if self.heightmap[idx] < y as u8 {
|
|
|
|
self.heightmap[idx] = y as u8;
|
|
|
|
self.heightmap_dirty = true;
|
|
|
|
} else if self.heightmap[idx] == y as u8 {
|
|
|
|
// Find a new lowest
|
|
|
|
for yy in 0 .. y {
|
|
|
|
let sy = y - yy - 1;
|
2016-04-03 07:58:53 -04:00
|
|
|
if let block::Air{..} = self.get_block(x, sy, z) {
|
2016-04-02 20:26:31 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-04-01 15:00:13 -04:00
|
|
|
self.heightmap[idx] = sy as u8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.heightmap_dirty = true;
|
|
|
|
}
|
2016-04-03 10:08:36 -04:00
|
|
|
true
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
2016-03-23 17:07:49 -04:00
|
|
|
fn get_block(&self, x: i32, y: i32, z: i32) -> block::Block {
|
2016-03-18 18:24:30 -04:00
|
|
|
let s_idx = y >> 4;
|
|
|
|
if s_idx < 0 || s_idx > 15 {
|
2016-03-23 17:07:49 -04:00
|
|
|
return block::Missing{};
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
match self.sections[s_idx as usize].as_ref() {
|
|
|
|
Some(sec) => sec.get_block(x, y & 0xF, z),
|
2016-03-23 17:07:49 -04:00
|
|
|
None => block::Air{},
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-24 15:20:26 -04:00
|
|
|
|
2016-04-03 10:08:36 -04:00
|
|
|
fn get_block_light(&self, x: i32, y: i32, z: i32) -> u8 {
|
|
|
|
let s_idx = y >> 4;
|
|
|
|
if s_idx < 0 || s_idx > 15 {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
match self.sections[s_idx as usize].as_ref() {
|
|
|
|
Some(sec) => sec.get_block_light(x, y & 0xF, z),
|
|
|
|
None => 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_block_light(&mut self, x: i32, y: i32, z: i32, light: u8) {
|
2016-04-03 10:42:54 -04:00
|
|
|
let s_idx = y >> 4;
|
2016-04-03 10:08:36 -04:00
|
|
|
if s_idx < 0 || s_idx > 15 {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-03 10:42:54 -04:00
|
|
|
let s_idx = s_idx as usize;
|
2016-04-03 10:08:36 -04:00
|
|
|
if self.sections[s_idx].is_none() {
|
|
|
|
if light == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let fill_sky = self.sections.iter()
|
|
|
|
.skip(s_idx)
|
|
|
|
.all(|v| v.is_none());
|
|
|
|
self.sections[s_idx] = Some(Section::new(s_idx as u8, fill_sky));
|
|
|
|
}
|
|
|
|
match self.sections[s_idx].as_mut() {
|
|
|
|
Some(sec) => sec.set_block_light(x, y & 0xF, z, light),
|
|
|
|
None => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_sky_light(&self, x: i32, y: i32, z: i32) -> u8 {
|
|
|
|
let s_idx = y >> 4;
|
|
|
|
if s_idx < 0 || s_idx > 15 {
|
2016-04-03 16:03:56 -04:00
|
|
|
return 15;
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
|
|
|
match self.sections[s_idx as usize].as_ref() {
|
|
|
|
Some(sec) => sec.get_sky_light(x, y & 0xF, z),
|
|
|
|
None => 15,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_sky_light(&mut self, x: i32, y: i32, z: i32, light: u8) {
|
2016-04-03 10:42:54 -04:00
|
|
|
let s_idx = y >> 4;
|
2016-04-03 10:08:36 -04:00
|
|
|
if s_idx < 0 || s_idx > 15 {
|
|
|
|
return;
|
|
|
|
}
|
2016-04-03 10:42:54 -04:00
|
|
|
let s_idx = s_idx as usize;
|
2016-04-03 10:08:36 -04:00
|
|
|
if self.sections[s_idx].is_none() {
|
|
|
|
if light == 15 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let fill_sky = self.sections.iter()
|
|
|
|
.skip(s_idx)
|
|
|
|
.all(|v| v.is_none());
|
|
|
|
self.sections[s_idx] = Some(Section::new(s_idx as u8, fill_sky));
|
|
|
|
}
|
|
|
|
match self.sections[s_idx as usize].as_mut() {
|
|
|
|
Some(sec) => sec.set_sky_light(x, y & 0xF, z, light),
|
|
|
|
None => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 15:20:26 -04:00
|
|
|
fn get_biome(&self, x: i32, z: i32) -> biome::Biome {
|
|
|
|
biome::Biome::by_id(self.biomes[((z<<4)|x) as usize] as usize)
|
|
|
|
}
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
2016-03-24 19:27:22 -04:00
|
|
|
pub struct Section {
|
|
|
|
pub cull_info: chunk_builder::CullInfo,
|
2016-03-24 17:13:24 -04:00
|
|
|
pub render_buffer: render::ChunkBuffer,
|
2016-03-24 19:27:22 -04:00
|
|
|
|
2016-03-18 18:24:30 -04:00
|
|
|
y: u8,
|
|
|
|
|
2016-04-20 14:25:51 -04:00
|
|
|
blocks: storage::BlockStorage,
|
2016-03-18 18:24:30 -04:00
|
|
|
|
|
|
|
block_light: nibble::Array,
|
|
|
|
sky_light: nibble::Array,
|
|
|
|
|
|
|
|
dirty: bool,
|
2016-03-19 12:32:13 -04:00
|
|
|
building: bool,
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Section {
|
2016-04-03 10:08:36 -04:00
|
|
|
fn new(y: u8, fill_sky: bool) -> Section {
|
2016-03-18 18:24:30 -04:00
|
|
|
let mut section = Section {
|
2016-03-24 19:27:22 -04:00
|
|
|
cull_info: chunk_builder::CullInfo::all_vis(),
|
2016-03-24 17:13:24 -04:00
|
|
|
render_buffer: render::ChunkBuffer::new(),
|
2016-03-18 18:24:30 -04:00
|
|
|
y: y,
|
|
|
|
|
2016-04-20 14:25:51 -04:00
|
|
|
blocks: storage::BlockStorage::new(4096),
|
2016-03-18 18:24:30 -04:00
|
|
|
|
|
|
|
block_light: nibble::Array::new(16 * 16 * 16),
|
|
|
|
sky_light: nibble::Array::new(16 * 16 * 16),
|
|
|
|
|
|
|
|
dirty: false,
|
2016-03-19 12:32:13 -04:00
|
|
|
building: false,
|
2016-03-18 18:24:30 -04:00
|
|
|
};
|
2016-04-03 10:08:36 -04:00
|
|
|
if fill_sky {
|
|
|
|
for i in 0 .. 16*16*16 {
|
|
|
|
section.sky_light.set(i, 0xF);
|
|
|
|
}
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
section
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:07:49 -04:00
|
|
|
fn get_block(&self, x: i32, y: i32, z: i32) -> block::Block {
|
2016-04-20 14:25:51 -04:00
|
|
|
self.blocks.get(((y << 8) | (z << 4) | x) as usize)
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
2016-04-03 10:08:36 -04:00
|
|
|
fn set_block(&mut self, x: i32, y: i32, z: i32, b: block::Block) -> bool {
|
2016-04-20 14:25:51 -04:00
|
|
|
if self.blocks.set(((y << 8) | (z << 4) | x) as usize, b) {
|
2016-04-03 10:08:36 -04:00
|
|
|
self.dirty = true;
|
2016-04-20 14:25:51 -04:00
|
|
|
self.set_sky_light(x, y, z, 0);
|
|
|
|
self.set_block_light(x, y, z, 0);
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2016-04-03 10:08:36 -04:00
|
|
|
}
|
2016-03-18 18:24:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_block_light(&self, x: i32, y: i32, z: i32) -> u8 {
|
|
|
|
self.block_light.get(((y << 8) | (z << 4) | x) as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_block_light(&mut self, x: i32, y: i32, z: i32, l: u8) {
|
|
|
|
self.block_light.set(((y << 8) | (z << 4) | x) as usize, l);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_sky_light(&self, x: i32, y: i32, z: i32) -> u8 {
|
|
|
|
self.sky_light.get(((y << 8) | (z << 4) | x) as usize)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_sky_light(&mut self, x: i32, y: i32, z: i32, l: u8) {
|
|
|
|
self.sky_light.set(((y << 8) | (z << 4) | x) as usize, l);
|
|
|
|
}
|
|
|
|
}
|